From 63241c253262c573e0408c85023604a1e681e890 Mon Sep 17 00:00:00 2001 From: pasta Date: Wed, 28 Aug 2024 19:22:38 -0500 Subject: [PATCH] feat: add islock timing statistics --- src/llmq/instantsend.cpp | 21 +++++++++++++++++++++ src/llmq/instantsend.h | 3 +++ 2 files changed, 24 insertions(+) diff --git a/src/llmq/instantsend.cpp b/src/llmq/instantsend.cpp index 037dd2f1f6..7821126a9c 100644 --- a/src/llmq/instantsend.cpp +++ b/src/llmq/instantsend.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #include @@ -790,6 +791,17 @@ PeerMsgRet CInstantSendManager::ProcessMessageInstantSendLock(const CNode& pfrom LogPrint(BCLog::INSTANTSEND, "CInstantSendManager::%s -- txid=%s, islock=%s: received islock, peer=%d\n", __func__, islock->txid.ToString(), hash.ToString(), pfrom.GetId()); + auto time_diff = [&] () -> int64_t { + LOCK(cs_timingsTxSeen); + if (auto it = timingsTxSeen.find(islock->txid); it != timingsTxSeen.end()) { + // This is the normal case where we received the TX before the islock + return GetTimeMillis() - it->second; + } + // But if we received the islock and don't know when we got the tx, then say 0, to indicate we received the islock first. + return 0; + }(); + statsClient.timing("islock_ms", time_diff); + LOCK(cs_pendingLocks); pendingInstantSendLocks.emplace(hash, std::make_pair(pfrom.GetId(), islock)); return {}; @@ -1179,6 +1191,15 @@ void CInstantSendManager::AddNonLockedTx(const CTransactionRef& tx, const CBlock ++it; } } + + { + LOCK(cs_timingsTxSeen); + // Only insert the time the first time we see the tx, as we sometimes try to resign + if (auto it = timingsTxSeen.find(tx->GetHash()); it == timingsTxSeen.end()) { + timingsTxSeen[tx->GetHash()] = GetTimeMillis(); + } + } + LogPrint(BCLog::INSTANTSEND, "CInstantSendManager::%s -- txid=%s, pindexMined=%s\n", __func__, tx->GetHash().ToString(), pindexMined ? pindexMined->GetBlockHash().ToString() : ""); } diff --git a/src/llmq/instantsend.h b/src/llmq/instantsend.h index 733d2afd7a..78a57e0e94 100644 --- a/src/llmq/instantsend.h +++ b/src/llmq/instantsend.h @@ -254,6 +254,9 @@ class CInstantSendManager : public CRecoveredSigsListener mutable Mutex cs_pendingRetry; std::unordered_set pendingRetryTxs GUARDED_BY(cs_pendingRetry); + mutable Mutex cs_timingsTxSeen; + std::unordered_map timingsTxSeen GUARDED_BY(cs_timingsTxSeen); + public: explicit CInstantSendManager(CChainLocksHandler& _clhandler, CChainState& chainstate, CConnman& _connman, CQuorumManager& _qman, CSigningManager& _sigman, CSigSharesManager& _shareman,