Skip to content

Commit c192119

Browse files
Merge #6851: refactor: introduce new helpers Uint256HashMap, Uint256HashSet, Uint256LruHashMap
c461dcc fix: review comments (Konstantin Akimov) 46a28e6 fmt: apply clang-format (Konstantin Akimov) c452f88 refactor: leftover usages that has not been replaced by automatic script (Konstantin Akimov) 85e6af4 refactor: use Uint256LruHashMap all overcodebase (Konstantin Akimov) c8c6d29 refactor: use Uint256HashMap all over codebase (Konstantin Akimov) 3aaf330 refactor: use Uint256HashSet all over codebase (Konstantin Akimov) d7cd9ab refactor: define helpers Uint256HashMap, Uint256HashSet, Uint256LruHashMap for code simplification (Konstantin Akimov) Pull request description: ## Issue being fixed or feature implemented Instead typing `std::unordered_map<uint256, CDeterministicMNList, StaticSaltedHasher> mnListsCache` better to just write `Uint256HashMap<CDeterministicMNList> mnListsCache` to minimize a boiler code. This PR aims to replace #6839, but this PR reduces scope significantly, no extra header is introduced. ## What was done? Main idea is authored by kwvg's PR: template <typename T> using Uint256HashMap = std::unordered_map<uint256, T, StaticSaltedHasher>; These helpers are used to prepare a PR: find src/ -name '*.cpp' -exec sed -i 's/std::unordered_set<uint256, StaticSaltedHasher>/Uint256HashSet/' {} \; find src/ -name '*.cpp' -exec sed -i 's/std::unordered_map<uint256, \([a-zA-Z0-9:]*\), StaticSaltedHasher>/Uint256HashMap<\1>/' {} \; find src/ -name '*.cpp' -exec sed -i 's/unordered_lru_cache<uint256, \([a-zA-Z0-9:]*\), StaticSaltedHasher>/Uint256LruHashMap<\1>/' {} \; Leftover changes are done manually. Please, note, that compilation time is not affected by including `<unordered_set>` and `<unordered_map>` because they are already included indirectly by chain `<saltedhasher.h> -> <hash.h> -> <serialize.h>` ## How Has This Been Tested? Grep all over codebase by `StaticSaltedHasher` ## Breaking Changes N/A ## Checklist: - [ ] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have added or updated relevant unit/integration/functional/e2e tests - [ ] I have made corresponding changes to the documentation - [x] I have assigned this pull request to a milestone ACKs for top commit: kwvg: utACK c461dcc Tree-SHA512: 2d8d0722554f5f89102d4d5380bfda01f2214cf8da82dbf300753e373a0b78ef6248ca626bd07f5bb467c5670ae9733d34aa020a1d268d3074dbc77fc5fc953b
2 parents d9ff98c + c461dcc commit c192119

32 files changed

+147
-125
lines changed

src/chainlock/chainlock.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ chainlock::ChainLockSig CChainLocksHandler::GetBestChainLock() const
114114
return bestChainLock;
115115
}
116116

117-
void CChainLocksHandler::UpdateTxFirstSeenMap(const std::unordered_set<uint256, StaticSaltedHasher>& tx, const int64_t& time)
117+
void CChainLocksHandler::UpdateTxFirstSeenMap(const Uint256HashSet& tx, const int64_t& time)
118118
{
119119
AssertLockNotHeld(cs);
120120
LOCK(cs);

src/chainlock/chainlock.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class CChainLocksHandler final : public chainlock::ChainLockSignerParent
5959
const CBlockIndex* bestChainLockBlockIndex GUARDED_BY(cs){nullptr};
6060
const CBlockIndex* lastNotifyChainLockBlockIndex GUARDED_BY(cs){nullptr};
6161

62-
std::unordered_map<uint256, std::chrono::seconds, StaticSaltedHasher> txFirstSeenTime GUARDED_BY(cs);
62+
Uint256HashMap<std::chrono::seconds> txFirstSeenTime GUARDED_BY(cs);
6363

6464
std::map<uint256, std::chrono::seconds> seenChainLocks GUARDED_BY(cs);
6565

@@ -87,8 +87,7 @@ class CChainLocksHandler final : public chainlock::ChainLockSignerParent
8787
EXCLUSIVE_LOCKS_REQUIRED(!cs);
8888
chainlock::ChainLockSig GetBestChainLock() const
8989
EXCLUSIVE_LOCKS_REQUIRED(!cs);
90-
void UpdateTxFirstSeenMap(const std::unordered_set<uint256, StaticSaltedHasher>& tx, const int64_t& time) override
91-
EXCLUSIVE_LOCKS_REQUIRED(!cs);
90+
void UpdateTxFirstSeenMap(const Uint256HashSet& tx, const int64_t& time) override EXCLUSIVE_LOCKS_REQUIRED(!cs);
9291

9392
[[nodiscard]] MessageProcessingResult ProcessNewChainLock(NodeId from, const chainlock::ChainLockSig& clsig,
9493
const uint256& hash) override

src/chainlock/signing.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ void ChainLockSigner::UpdateBlockHashTxidMap(const uint256& hash, const std::vec
158158
if (it == blockTxs.end()) {
159159
// We must create this entry even if there are no lockable transactions in the block, so that TrySignChainTip
160160
// later knows about this block
161-
it = blockTxs.emplace(hash, std::make_shared<std::unordered_set<uint256, StaticSaltedHasher>>()).first;
161+
it = blockTxs.emplace(hash, std::make_shared<Uint256HashSet>()).first;
162162
}
163163
auto& txids = *it->second;
164164
for (const auto& tx : vtx) {
@@ -200,7 +200,7 @@ ChainLockSigner::BlockTxs::mapped_type ChainLockSigner::GetBlockTxs(const uint25
200200
return nullptr;
201201
}
202202

203-
ret = std::make_shared<std::unordered_set<uint256, StaticSaltedHasher>>();
203+
ret = std::make_shared<Uint256HashSet>();
204204
for (const auto& tx : block.vtx) {
205205
if (tx->IsCoinBase() || tx->vin.empty()) {
206206
continue;
@@ -243,10 +243,10 @@ MessageProcessingResult ChainLockSigner::HandleNewRecoveredSig(const llmq::CReco
243243
return m_clhandler.ProcessNewChainLock(-1, clsig, ::SerializeHash(clsig));
244244
}
245245

246-
std::vector<std::shared_ptr<std::unordered_set<uint256, StaticSaltedHasher>>> ChainLockSigner::Cleanup()
246+
std::vector<std::shared_ptr<Uint256HashSet>> ChainLockSigner::Cleanup()
247247
{
248248
AssertLockNotHeld(cs_signer);
249-
std::vector<std::shared_ptr<std::unordered_set<uint256, StaticSaltedHasher>>> removed;
249+
std::vector<std::shared_ptr<Uint256HashSet>> removed;
250250
LOCK2(::cs_main, cs_signer);
251251
for (auto it = blockTxs.begin(); it != blockTxs.end();) {
252252
const auto* pindex = m_chainstate.m_blockman.LookupBlockIndex(it->first);

src/chainlock/signing.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class ChainLockSignerParent
3434
virtual bool IsEnabled() const = 0;
3535
virtual bool IsTxSafeForMining(const uint256& txid) const = 0;
3636
[[nodiscard]] virtual MessageProcessingResult ProcessNewChainLock(NodeId from, const ChainLockSig& clsig, const uint256& hash) = 0;
37-
virtual void UpdateTxFirstSeenMap(const std::unordered_set<uint256, StaticSaltedHasher>& tx, const int64_t& time) = 0;
37+
virtual void UpdateTxFirstSeenMap(const Uint256HashSet& tx, const int64_t& time) = 0;
3838
};
3939

4040
class ChainLockSigner final : public llmq::CRecoveredSigsListener
@@ -52,7 +52,7 @@ class ChainLockSigner final : public llmq::CRecoveredSigsListener
5252
struct BlockHasher {
5353
size_t operator()(const uint256& hash) const { return ReadLE64(hash.begin()); }
5454
};
55-
using BlockTxs = std::unordered_map<uint256, std::shared_ptr<std::unordered_set<uint256, StaticSaltedHasher>>, BlockHasher>;
55+
using BlockTxs = std::unordered_map<uint256, std::shared_ptr<Uint256HashSet>, BlockHasher>;
5656

5757
private:
5858
mutable Mutex cs_signer;
@@ -80,8 +80,7 @@ class ChainLockSigner final : public llmq::CRecoveredSigsListener
8080
[[nodiscard]] MessageProcessingResult HandleNewRecoveredSig(const llmq::CRecoveredSig& recoveredSig) override
8181
EXCLUSIVE_LOCKS_REQUIRED(!cs_signer);
8282

83-
[[nodiscard]] std::vector<std::shared_ptr<std::unordered_set<uint256, StaticSaltedHasher>>> Cleanup()
84-
EXCLUSIVE_LOCKS_REQUIRED(!cs_signer);
83+
[[nodiscard]] std::vector<std::shared_ptr<Uint256HashSet>> Cleanup() EXCLUSIVE_LOCKS_REQUIRED(!cs_signer);
8584

8685
private:
8786
[[nodiscard]] BlockTxs::mapped_type GetBlockTxs(const uint256& blockHash)

src/evo/cbtx.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ auto CachedGetQcHashesQcIndexedHashes(const CBlockIndex* pindexPrev, const llmq:
5858

5959
static Mutex cs_cache;
6060
static std::map<Consensus::LLMQType, std::vector<const CBlockIndex*>> quorums_cached GUARDED_BY(cs_cache);
61-
static std::map<Consensus::LLMQType, unordered_lru_cache<uint256, std::pair<uint256, int>, StaticSaltedHasher>> qc_hashes_cached
62-
GUARDED_BY(cs_cache);
61+
static std::map<Consensus::LLMQType, Uint256LruHashMap<std::pair<uint256, int>>> qc_hashes_cached GUARDED_BY(cs_cache);
6362
static QcHashMap qcHashes_cached GUARDED_BY(cs_cache);
6463
static QcIndexedHashMap qcIndexedHashes_cached GUARDED_BY(cs_cache);
6564

src/evo/creditpool.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ static std::optional<CreditPoolDataPerBlock> GetCreditDataFromBlock(const gsl::n
6868
CreditPoolDataPerBlock blockData;
6969

7070
static Mutex cache_mutex;
71-
static unordered_lru_cache<uint256, CreditPoolDataPerBlock, StaticSaltedHasher> block_data_cache GUARDED_BY(
72-
cache_mutex){static_cast<size_t>(Params().CreditPoolPeriodBlocks()) * 2};
71+
static Uint256LruHashMap<CreditPoolDataPerBlock> block_data_cache GUARDED_BY(cache_mutex){
72+
static_cast<size_t>(Params().CreditPoolPeriodBlocks()) * 2};
7373
if (LOCK(cache_mutex); block_data_cache.get(block_index->GetBlockHash(), blockData)) {
7474
return blockData;
7575
}

src/evo/creditpool.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ class CCreditPoolManager
111111
private:
112112
static constexpr size_t CreditPoolCacheSize = 1000;
113113
Mutex cache_mutex;
114-
unordered_lru_cache<uint256, CCreditPool, StaticSaltedHasher> creditPoolCache GUARDED_BY(cache_mutex) {CreditPoolCacheSize};
114+
Uint256LruHashMap<CCreditPool> creditPoolCache GUARDED_BY(cache_mutex){CreditPoolCacheSize};
115115

116116
CEvoDB& evoDb;
117117

src/evo/deterministicmns.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -628,8 +628,8 @@ class CDeterministicMNManager
628628

629629
CEvoDB& m_evoDb;
630630

631-
std::unordered_map<uint256, CDeterministicMNList, StaticSaltedHasher> mnListsCache GUARDED_BY(cs);
632-
std::unordered_map<uint256, CDeterministicMNListDiff, StaticSaltedHasher> mnListDiffsCache GUARDED_BY(cs);
631+
Uint256HashMap<CDeterministicMNList> mnListsCache GUARDED_BY(cs);
632+
Uint256HashMap<CDeterministicMNListDiff> mnListDiffsCache GUARDED_BY(cs);
633633
const CBlockIndex* tipIndex GUARDED_BY(cs) {nullptr};
634634
const CBlockIndex* m_initial_snapshot_index GUARDED_BY(cs) {nullptr};
635635

src/evo/mnhftx.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ class CMNHFManager : public AbstractEHFManager
100100
static constexpr size_t MNHFCacheSize = 1000;
101101
Mutex cs_cache;
102102
// versionBit <-> height
103-
unordered_lru_cache<uint256, Signals, StaticSaltedHasher> mnhfCache GUARDED_BY(cs_cache) {MNHFCacheSize};
103+
Uint256LruHashMap<Signals> mnhfCache GUARDED_BY(cs_cache){MNHFCacheSize};
104104

105105
public:
106106
explicit CMNHFManager(CEvoDB& evoDb);

src/instantsend/db.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ void CInstantSendDb::WriteInstantSendLockArchived(CDBBatch& batch, const uint256
116116
batch.Write(std::make_tuple(DB_ARCHIVED_BY_HASH, hash), true);
117117
}
118118

119-
std::unordered_map<uint256, InstantSendLockPtr, StaticSaltedHasher> CInstantSendDb::RemoveConfirmedInstantSendLocks(int nUntilHeight)
119+
Uint256HashMap<InstantSendLockPtr> CInstantSendDb::RemoveConfirmedInstantSendLocks(int nUntilHeight)
120120
{
121121
LOCK(cs_db);
122122
if (nUntilHeight <= best_confirmed_height) {
@@ -133,7 +133,7 @@ std::unordered_map<uint256, InstantSendLockPtr, StaticSaltedHasher> CInstantSend
133133
it->Seek(firstKey);
134134

135135
CDBBatch batch(*db);
136-
std::unordered_map<uint256, InstantSendLockPtr, StaticSaltedHasher> ret;
136+
Uint256HashMap<InstantSendLockPtr> ret;
137137
while (it->Valid()) {
138138
decltype(firstKey) curKey;
139139
if (!it->GetKey(curKey) || std::get<0>(curKey) != DB_MINED_BY_HEIGHT_AND_HASH) {
@@ -359,7 +359,7 @@ std::vector<uint256> CInstantSendDb::RemoveChainedInstantSendLocks(const uint256
359359
std::vector<uint256> result;
360360

361361
std::vector<uint256> stack;
362-
std::unordered_set<uint256, StaticSaltedHasher> added;
362+
Uint256HashSet added;
363363
stack.emplace_back(txid);
364364

365365
CDBBatch batch(*db);

0 commit comments

Comments
 (0)