perf(liquidity): reuse active pool probe read

This commit is contained in:
Ricardo Guilherme Schmidt
2026-07-18 19:56:38 -03:00
parent fab26b58c9
commit 3bb02e1531
3 changed files with 87 additions and 16 deletions
+16 -2
View File
@@ -451,6 +451,7 @@ void NewPositionRuntime::buildQuoteInputAsync(
const ActiveNetworkSnapshot& network,
bool walletOpen,
bool forceRefresh,
WalletAccountRead preloadedPoolRead,
std::function<bool()> shouldContinue,
std::function<void(QJsonObject, QJsonObject)> callback)
{
@@ -475,6 +476,7 @@ void NewPositionRuntime::buildQuoteInputAsync(
QPointer<NewPositionRuntime> guard(this);
m_sequencer->readAccounts({ configId }, forceRefresh,
[guard, request, network, walletOpen, forceRefresh,
preloadedPoolRead = std::move(preloadedPoolRead),
shouldContinue,
callback = std::move(callback)](QVector<WalletAccountRead> configReads) mutable {
if (!guard || !shouldContinue())
@@ -511,13 +513,21 @@ void NewPositionRuntime::buildQuoteInputAsync(
pair.value(QStringLiteral("currentTickId")).toString(),
pair.value(QStringLiteral("clockId")).toString(),
};
guard->m_sequencer->readAccounts(fixedIds, forceRefresh,
const bool reusePreloadedPool = preloadedPoolRead.ok()
&& preloadedPoolRead.accountId == fixedIds.value(2);
QStringList readIds = fixedIds;
if (reusePreloadedPool)
readIds.removeAt(2);
guard->m_sequencer->readAccounts(readIds, forceRefresh,
[guard, requestObject, network, walletOpen, config, pair,
preloadedPoolRead = std::move(preloadedPoolRead), reusePreloadedPool,
shouldContinue,
callback = std::move(callback)](
QVector<WalletAccountRead> fixedReads) mutable {
if (!guard || !shouldContinue())
return;
if (reusePreloadedPool)
fixedReads.insert(2, std::move(preloadedPoolRead));
QStringList selectedIds;
for (const QString& key : {
QStringLiteral("holdingAId"),
@@ -593,11 +603,13 @@ void NewPositionRuntime::quoteFromAccountsAsync(
const ActiveNetworkSnapshot& network,
bool walletOpen,
bool forceRefresh,
WalletAccountRead preloadedPoolRead,
std::function<bool()> shouldContinue,
ResultCallback callback)
{
QPointer<NewPositionRuntime> guard(this);
buildQuoteInputAsync(request, network, walletOpen, forceRefresh,
std::move(preloadedPoolRead),
shouldContinue,
[guard, shouldContinue,
callback = std::move(callback)](
@@ -637,6 +649,7 @@ void NewPositionRuntime::quoteAsync(const QVariantMap& request,
|| !m_sequencer || !m_sequencer->isConfigured()) {
quoteFromAccountsAsync(
request, network, walletOpen, forceRefresh,
{},
std::move(shouldContinue), std::move(callback));
return;
}
@@ -672,6 +685,7 @@ void NewPositionRuntime::quoteAsync(const QVariantMap& request,
}
guard->quoteFromAccountsAsync(
request, network, walletOpen, forceRefresh,
std::move(read),
std::move(shouldContinue), std::move(callback));
});
}
@@ -705,7 +719,7 @@ void NewPositionRuntime::submitAsync(const QVariantMap& request,
return guard
&& guard->submitIsCurrent(submitGeneration, walletGeneration);
};
buildQuoteInputAsync(request, network, true, true,
buildQuoteInputAsync(request, network, true, true, {},
shouldContinue,
[guard, quoteHash, submitGeneration, walletGeneration,
shouldContinue](
+3
View File
@@ -15,6 +15,7 @@ class AmmClient;
class WalletProvider;
class SequencerClient;
struct WalletAccount;
struct WalletAccountRead;
class NewPositionRuntime : public QObject {
public:
@@ -75,12 +76,14 @@ private:
const ActiveNetworkSnapshot& network,
bool walletOpen,
bool forceRefresh,
WalletAccountRead preloadedPoolRead,
std::function<bool()> shouldContinue,
std::function<void(QJsonObject, QJsonObject)> callback);
void quoteFromAccountsAsync(const QVariantMap& request,
const ActiveNetworkSnapshot& network,
bool walletOpen,
bool forceRefresh,
WalletAccountRead preloadedPoolRead,
std::function<bool()> shouldContinue,
ResultCallback callback);
void submitPlanAsync(QJsonObject input,
+68 -14
View File
@@ -309,17 +309,35 @@ namespace {
AmmClientResult pairIds(const QJsonObject&) const override
{
++pairIdsCalls;
const QString tokenAId = useValidPairIds
? QString(64, QLatin1Char('2')) : QStringLiteral("token-a");
const QString tokenBId = useValidPairIds
? QString(64, QLatin1Char('3')) : QStringLiteral("token-b");
const QString poolId = useValidPairIds
? pairPoolId : QStringLiteral("pool");
const QString vaultAId = useValidPairIds
? QString(64, QLatin1Char('4')) : QStringLiteral("vault-a");
const QString vaultBId = useValidPairIds
? QString(64, QLatin1Char('5')) : QStringLiteral("vault-b");
const QString lpDefinitionId = useValidPairIds
? QString(64, QLatin1Char('6')) : QStringLiteral("lp");
const QString lpLockHoldingId = useValidPairIds
? QString(64, QLatin1Char('7')) : QStringLiteral("lp-lock");
const QString currentTickId = useValidPairIds
? QString(64, QLatin1Char('8')) : QStringLiteral("tick");
const QString clockId = useValidPairIds
? QString(64, QLatin1Char('9')) : QStringLiteral("clock");
return success({
{ QStringLiteral("status"), QStringLiteral("ok") },
{ QStringLiteral("tokenAId"), QStringLiteral("token-a") },
{ QStringLiteral("tokenBId"), QStringLiteral("token-b") },
{ QStringLiteral("poolId"), QStringLiteral("pool") },
{ QStringLiteral("vaultAId"), QStringLiteral("vault-a") },
{ QStringLiteral("vaultBId"), QStringLiteral("vault-b") },
{ QStringLiteral("lpDefinitionId"), QStringLiteral("lp") },
{ QStringLiteral("lpLockHoldingId"), QStringLiteral("lp-lock") },
{ QStringLiteral("currentTickId"), QStringLiteral("tick") },
{ QStringLiteral("clockId"), QStringLiteral("clock") },
{ QStringLiteral("tokenAId"), tokenAId },
{ QStringLiteral("tokenBId"), tokenBId },
{ QStringLiteral("poolId"), poolId },
{ QStringLiteral("vaultAId"), vaultAId },
{ QStringLiteral("vaultBId"), vaultBId },
{ QStringLiteral("lpDefinitionId"), lpDefinitionId },
{ QStringLiteral("lpLockHoldingId"), lpLockHoldingId },
{ QStringLiteral("currentTickId"), currentTickId },
{ QStringLiteral("clockId"), clockId },
});
}
@@ -408,6 +426,8 @@ namespace {
mutable QStringList freshLpAccountIds;
mutable QStringList normalizedAccountIds;
QString normalizedBalanceHex = QString(32, QLatin1Char('0'));
bool useValidPairIds = false;
QString pairPoolId;
};
ActiveNetworkSnapshot readyNetwork()
@@ -898,6 +918,11 @@ int main(int argc, char** argv)
return 1;
poolProbeClient.normalizedBalanceHex = QString(32, QLatin1Char('1'));
poolProbeClient.useValidPairIds = true;
poolProbeClient.pairPoolId = poolIdHex;
const int requestsBeforeActiveProbe = poolProbeServer.requestCount();
const int poolReadsBeforeActiveProbe =
poolProbeClient.normalizedAccountIds.count(poolIdHex);
poolProbeResult.clear();
poolProbeRuntime.quoteAsync(
poolProbeRequest, readyNetwork(), true, true, true,
@@ -907,12 +932,41 @@ int main(int argc, char** argv)
return 1;
if (!expect(poolProbeResult.value(QStringLiteral("poolStatus")).toString()
== QStringLiteral("active_pool")
&& poolProbeServer.requestCount() == 3
&& poolProbeServer.requestCount() == requestsBeforeActiveProbe + 10
&& poolProbeClient.normalizedAccountIds.count(poolIdHex)
== poolReadsBeforeActiveProbe + 1
&& poolProbeClient.pairIdsCalls == 1
&& poolProbeClient.quoteCalls == 1,
"nondefault pool should run one validating full quote"))
"active pool probe should reuse its fresh pool read"))
return 1;
const QString mismatchedPoolId = QString(64, QLatin1Char('d'));
poolProbeClient.pairPoolId = mismatchedPoolId;
const int requestsBeforeMismatch = poolProbeServer.requestCount();
const int sourcePoolReadsBeforeMismatch =
poolProbeClient.normalizedAccountIds.count(poolIdHex);
const int pairPoolReadsBeforeMismatch =
poolProbeClient.normalizedAccountIds.count(mismatchedPoolId);
poolProbeResult.clear();
poolProbeRuntime.quoteAsync(
poolProbeRequest, readyNetwork(), true, true, true,
[&](QVariantMap result) { poolProbeResult = std::move(result); });
if (!expect(waitForCondition([&]() { return !poolProbeResult.isEmpty(); }),
"mismatched pool probe should complete"))
return 1;
if (!expect(poolProbeResult.value(QStringLiteral("poolStatus")).toString()
== QStringLiteral("active_pool")
&& poolProbeServer.requestCount() == requestsBeforeMismatch + 11
&& poolProbeClient.normalizedAccountIds.count(poolIdHex)
== sourcePoolReadsBeforeMismatch + 1
&& poolProbeClient.normalizedAccountIds.count(mismatchedPoolId)
== pairPoolReadsBeforeMismatch + 1
&& poolProbeClient.pairIdsCalls == 2
&& poolProbeClient.quoteCalls == 2,
"pool mismatch should retain the full forced quote"))
return 1;
const int requestsBeforeFailedProbe = poolProbeServer.requestCount();
poolProbeServer.failNextRequest();
poolProbeResult.clear();
poolProbeRuntime.quoteAsync(
@@ -923,9 +977,9 @@ int main(int argc, char** argv)
return 1;
if (!expect(poolProbeResult.value(QStringLiteral("code")).toString()
== QStringLiteral("account_read_failed")
&& poolProbeServer.requestCount() == 4
&& poolProbeClient.pairIdsCalls == 1
&& poolProbeClient.quoteCalls == 1,
&& poolProbeServer.requestCount() == requestsBeforeFailedProbe + 1
&& poolProbeClient.pairIdsCalls == 2
&& poolProbeClient.quoteCalls == 2,
"failed pool probe should wait for the next one-account retry"))
return 1;