forked from bitcoin/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: instantsend cache heights and reduce cs_main contention #6953
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
PastaPastaPasta
wants to merge
6
commits into
dashpay:develop
Choose a base branch
from
PastaPastaPasta:feat/no-cs-main-islock
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+162
−39
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ce5601c
instantsend: cache heights and reduce cs_main contention
PastaPastaPasta 8fc12bc
fixes
PastaPastaPasta 8a5b845
chore: run clang-format
PastaPastaPasta 8bdb71d
refactor: improve logging in CInstantSendManager
PastaPastaPasta a5d87d6
avoid duplicated HasChainLock block
PastaPastaPasta 13727fb
refactor: simplify optional usage
PastaPastaPasta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,9 +18,11 @@ | |
| #include <unordered_lru_cache.h> | ||
|
|
||
| #include <atomic> | ||
| #include <unordered_map> | ||
| #include <optional> | ||
| #include <unordered_set> | ||
|
|
||
| #include <saltedhasher.h> | ||
|
|
||
| class CBlockIndex; | ||
| class CChainState; | ||
| class CDataStream; | ||
|
|
@@ -91,6 +93,14 @@ class CInstantSendManager final : public instantsend::InstantSendSignerParent | |
| mutable Mutex cs_timingsTxSeen; | ||
| Uint256HashMap<int64_t> timingsTxSeen GUARDED_BY(cs_timingsTxSeen); | ||
|
|
||
| mutable Mutex cs_height_cache; | ||
| static constexpr size_t MAX_BLOCK_HEIGHT_CACHE{16384}; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. isn't 16k a bit too much? rotation quorums are not alive that much; also it's very unlikely situation that tx will exist that long before it will be mined. 16k is roughly 28 days |
||
| mutable unordered_lru_cache<uint256, int, StaticSaltedHasher, MAX_BLOCK_HEIGHT_CACHE> m_cached_block_heights | ||
| GUARDED_BY(cs_height_cache); | ||
| mutable int m_cached_tip_height GUARDED_BY(cs_height_cache){-1}; | ||
|
|
||
| void CacheBlockHeightInternal(const uint256& hash, int height) const EXCLUSIVE_LOCKS_REQUIRED(cs_height_cache); | ||
|
|
||
| public: | ||
| CInstantSendManager() = delete; | ||
| CInstantSendManager(const CInstantSendManager&) = delete; | ||
|
|
@@ -114,15 +124,15 @@ class CInstantSendManager final : public instantsend::InstantSendSignerParent | |
|
|
||
| private: | ||
| instantsend::PendingState ProcessPendingInstantSendLocks() | ||
| EXCLUSIVE_LOCKS_REQUIRED(!cs_nonLocked, !cs_pendingLocks, !cs_pendingRetry); | ||
| EXCLUSIVE_LOCKS_REQUIRED(!cs_nonLocked, !cs_pendingLocks, !cs_pendingRetry, !cs_height_cache); | ||
|
|
||
| Uint256HashSet ProcessPendingInstantSendLocks(const Consensus::LLMQParams& llmq_params, int signOffset, bool ban, | ||
| const Uint256HashMap<std::pair<NodeId, instantsend::InstantSendLockPtr>>& pend, | ||
| std::vector<std::pair<NodeId, MessageProcessingResult>>& peer_activity) | ||
| EXCLUSIVE_LOCKS_REQUIRED(!cs_nonLocked, !cs_pendingLocks, !cs_pendingRetry); | ||
| EXCLUSIVE_LOCKS_REQUIRED(!cs_nonLocked, !cs_pendingLocks, !cs_pendingRetry, !cs_height_cache); | ||
| MessageProcessingResult ProcessInstantSendLock(NodeId from, const uint256& hash, | ||
| const instantsend::InstantSendLockPtr& islock) | ||
| EXCLUSIVE_LOCKS_REQUIRED(!cs_nonLocked, !cs_pendingLocks, !cs_pendingRetry); | ||
| EXCLUSIVE_LOCKS_REQUIRED(!cs_nonLocked, !cs_pendingLocks, !cs_pendingRetry, !cs_height_cache); | ||
|
|
||
| void AddNonLockedTx(const CTransactionRef& tx, const CBlockIndex* pindexMined) | ||
| EXCLUSIVE_LOCKS_REQUIRED(!cs_nonLocked, !cs_pendingLocks, !cs_timingsTxSeen); | ||
|
|
@@ -135,7 +145,7 @@ class CInstantSendManager final : public instantsend::InstantSendSignerParent | |
| void RemoveMempoolConflictsForLock(const uint256& hash, const instantsend::InstantSendLock& islock) | ||
| EXCLUSIVE_LOCKS_REQUIRED(!cs_nonLocked, !cs_pendingRetry); | ||
| void ResolveBlockConflicts(const uint256& islockHash, const instantsend::InstantSendLock& islock) | ||
| EXCLUSIVE_LOCKS_REQUIRED(!cs_nonLocked, !cs_pendingLocks, !cs_pendingRetry); | ||
| EXCLUSIVE_LOCKS_REQUIRED(!cs_nonLocked, !cs_pendingLocks, !cs_pendingRetry, !cs_height_cache); | ||
|
|
||
| void WorkThreadMain(PeerManager& peerman) | ||
| EXCLUSIVE_LOCKS_REQUIRED(!cs_nonLocked, !cs_pendingLocks, !cs_pendingRetry); | ||
|
|
@@ -149,14 +159,15 @@ class CInstantSendManager final : public instantsend::InstantSendSignerParent | |
| instantsend::InstantSendLockPtr GetConflictingLock(const CTransaction& tx) const override; | ||
|
|
||
| [[nodiscard]] MessageProcessingResult ProcessMessage(NodeId from, std::string_view msg_type, CDataStream& vRecv) | ||
| EXCLUSIVE_LOCKS_REQUIRED(!cs_pendingLocks); | ||
| EXCLUSIVE_LOCKS_REQUIRED(!cs_pendingLocks, !cs_height_cache); | ||
|
|
||
| void TransactionAddedToMempool(const CTransactionRef& tx) | ||
| EXCLUSIVE_LOCKS_REQUIRED(!cs_nonLocked, !cs_pendingLocks, !cs_pendingRetry, !cs_timingsTxSeen); | ||
| void TransactionRemovedFromMempool(const CTransactionRef& tx); | ||
| void TransactionRemovedFromMempool(const CTransactionRef& tx) EXCLUSIVE_LOCKS_REQUIRED(!cs_height_cache); | ||
| void BlockConnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindex) | ||
| EXCLUSIVE_LOCKS_REQUIRED(!cs_nonLocked, !cs_pendingLocks, !cs_pendingRetry, !cs_timingsTxSeen); | ||
| void BlockDisconnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindexDisconnected); | ||
| EXCLUSIVE_LOCKS_REQUIRED(!cs_nonLocked, !cs_pendingLocks, !cs_pendingRetry, !cs_timingsTxSeen, !cs_height_cache); | ||
| void BlockDisconnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindexDisconnected) | ||
| EXCLUSIVE_LOCKS_REQUIRED(!cs_height_cache); | ||
|
|
||
| bool AlreadyHave(const CInv& inv) const EXCLUSIVE_LOCKS_REQUIRED(!cs_pendingLocks); | ||
| bool GetInstantSendLockByHash(const uint256& hash, instantsend::InstantSendLock& ret) const | ||
|
|
@@ -166,14 +177,19 @@ class CInstantSendManager final : public instantsend::InstantSendSignerParent | |
| void NotifyChainLock(const CBlockIndex* pindexChainLock) | ||
| EXCLUSIVE_LOCKS_REQUIRED(!cs_nonLocked, !cs_pendingRetry); | ||
| void UpdatedBlockTip(const CBlockIndex* pindexNew) | ||
| EXCLUSIVE_LOCKS_REQUIRED(!cs_nonLocked, !cs_pendingRetry); | ||
| EXCLUSIVE_LOCKS_REQUIRED(!cs_nonLocked, !cs_pendingRetry, !cs_height_cache); | ||
|
|
||
| void RemoveConflictingLock(const uint256& islockHash, const instantsend::InstantSendLock& islock); | ||
| void RemoveConflictingLock(const uint256& islockHash, const instantsend::InstantSendLock& islock) | ||
| EXCLUSIVE_LOCKS_REQUIRED(!cs_height_cache); | ||
| void TryEmplacePendingLock(const uint256& hash, const NodeId id, const instantsend::InstantSendLockPtr& islock) override | ||
| EXCLUSIVE_LOCKS_REQUIRED(!cs_pendingLocks); | ||
|
|
||
| size_t GetInstantSendLockCount() const; | ||
|
|
||
| void CacheBlockHeight(const uint256& hash, int height) const EXCLUSIVE_LOCKS_REQUIRED(!cs_height_cache); | ||
| std::optional<int> GetBlockHeight(const uint256& hash) const override EXCLUSIVE_LOCKS_REQUIRED(!cs_height_cache); | ||
| int GetTipHeight() const override EXCLUSIVE_LOCKS_REQUIRED(!cs_height_cache); | ||
|
|
||
| bool IsInstantSendEnabled() const override; | ||
| /** | ||
| * If true, MN should sign all transactions, if false, MN should not sign | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix clang-format issue.
The pipeline indicates clang-format differences at lines 393-395. Please run clang-format to resolve the formatting issue.
🧰 Tools
🪛 GitHub Actions: Clang Diff Format Check
[error] 393-395: Clang format differences found. Apply clang-format changes to src/instantsend/instantsend.cpp. Exit code 1 due to unformatted code.
🤖 Prompt for AI Agents