Skip to content

Commit 5c6e98e

Browse files
refactor: replace pair with structured PendingISLockFromPeer in CInstantSendManager
Updated the CInstantSendManager to use a new struct, PendingISLockFromPeer, for better clarity and type safety. This change replaces the use of std::pair for storing node ID and InstantSendLockPtr, enhancing code readability and maintainability across multiple functions handling instant send locks.
1 parent 368eebb commit 5c6e98e

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

src/instantsend/instantsend.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ MessageProcessingResult CInstantSendManager::ProcessMessage(NodeId from, std::st
168168
}
169169

170170
LOCK(cs_pendingLocks);
171-
pendingInstantSendLocks.emplace(hash, std::make_pair(from, islock));
171+
pendingInstantSendLocks.emplace(hash, instantsend::PendingISLockFromPeer{from, islock});
172172
return ret;
173173
}
174174

@@ -239,7 +239,7 @@ instantsend::PendingState CInstantSendManager::ProcessPendingInstantSendLocks()
239239

240240
Uint256HashSet CInstantSendManager::ProcessPendingInstantSendLocks(
241241
const Consensus::LLMQParams& llmq_params, int signOffset, bool ban,
242-
const Uint256HashMap<std::pair<NodeId, instantsend::InstantSendLockPtr>>& pend,
242+
const Uint256HashMap<instantsend::PendingISLockFromPeer>& pend,
243243
std::vector<std::pair<NodeId, MessageProcessingResult>>& peer_activity)
244244
{
245245
CBLSBatchVerifier<NodeId, uint256> batchVerifier(false, true, 8);
@@ -249,8 +249,8 @@ Uint256HashSet CInstantSendManager::ProcessPendingInstantSendLocks(
249249
size_t alreadyVerified = 0;
250250
for (const auto& p : pend) {
251251
const auto& hash = p.first;
252-
auto nodeId = p.second.first;
253-
const auto& islock = p.second.second;
252+
auto nodeId = p.second.node_id;
253+
const auto& islock = p.second.islock;
254254

255255
if (batchVerifier.badSources.count(nodeId)) {
256256
continue;
@@ -321,8 +321,8 @@ Uint256HashSet CInstantSendManager::ProcessPendingInstantSendLocks(
321321
}
322322
for (const auto& p : pend) {
323323
const auto& hash = p.first;
324-
auto nodeId = p.second.first;
325-
const auto& islock = p.second.second;
324+
auto nodeId = p.second.node_id;
325+
const auto& islock = p.second.islock;
326326

327327
if (batchVerifier.badMessages.count(hash)) {
328328
LogPrint(BCLog::INSTANTSEND, "CInstantSendManager::%s -- txid=%s, islock=%s: invalid sig in islock, peer=%d\n",
@@ -399,7 +399,7 @@ MessageProcessingResult CInstantSendManager::ProcessInstantSendLock(NodeId from,
399399
} else {
400400
// put it in a separate pending map and try again later
401401
LOCK(cs_pendingLocks);
402-
pendingNoTxInstantSendLocks.try_emplace(hash, std::make_pair(from, islock));
402+
pendingNoTxInstantSendLocks.try_emplace(hash, instantsend::PendingISLockFromPeer{from, islock});
403403
}
404404

405405
// This will also add children TXs to pendingRetryTxs
@@ -442,11 +442,11 @@ void CInstantSendManager::TransactionAddedToMempool(const CTransactionRef& tx)
442442
LOCK(cs_pendingLocks);
443443
auto it = pendingNoTxInstantSendLocks.begin();
444444
while (it != pendingNoTxInstantSendLocks.end()) {
445-
if (it->second.second->txid == tx->GetHash()) {
445+
if (it->second.islock->txid == tx->GetHash()) {
446446
// we received an islock earlier
447447
LogPrint(BCLog::INSTANTSEND, "CInstantSendManager::%s -- txid=%s, islock=%s\n", __func__,
448448
tx->GetHash().ToString(), it->first.ToString());
449-
islock = it->second.second;
449+
islock = it->second.islock;
450450
pendingInstantSendLocks.try_emplace(it->first, it->second);
451451
pendingNoTxInstantSendLocks.erase(it);
452452
break;
@@ -539,7 +539,7 @@ void CInstantSendManager::AddNonLockedTx(const CTransactionRef& tx, const CBlock
539539
LOCK(cs_pendingLocks);
540540
auto it = pendingNoTxInstantSendLocks.begin();
541541
while (it != pendingNoTxInstantSendLocks.end()) {
542-
if (it->second.second->txid == tx->GetHash()) {
542+
if (it->second.islock->txid == tx->GetHash()) {
543543
// we received an islock earlier, let's put it back into pending and verify/lock
544544
LogPrint(BCLog::INSTANTSEND, "CInstantSendManager::%s -- txid=%s, islock=%s\n", __func__,
545545
tx->GetHash().ToString(), it->first.ToString());
@@ -626,7 +626,7 @@ void CInstantSendManager::TryEmplacePendingLock(const uint256& hash, const NodeI
626626
if (db.KnownInstantSendLock(hash)) return;
627627
LOCK(cs_pendingLocks);
628628
if (!pendingInstantSendLocks.count(hash)) {
629-
pendingInstantSendLocks.emplace(hash, std::make_pair(id, islock));
629+
pendingInstantSendLocks.emplace(hash, instantsend::PendingISLockFromPeer{id, islock});
630630
}
631631
}
632632

@@ -848,11 +848,11 @@ bool CInstantSendManager::GetInstantSendLockByHash(const uint256& hash, instants
848848
LOCK(cs_pendingLocks);
849849
auto it = pendingInstantSendLocks.find(hash);
850850
if (it != pendingInstantSendLocks.end()) {
851-
islock = it->second.second;
851+
islock = it->second.islock;
852852
} else {
853853
auto itNoTx = pendingNoTxInstantSendLocks.find(hash);
854854
if (itNoTx != pendingNoTxInstantSendLocks.end()) {
855-
islock = itNoTx->second.second;
855+
islock = itNoTx->second.islock;
856856
} else {
857857
return false;
858858
}
@@ -889,7 +889,7 @@ bool CInstantSendManager::IsWaitingForTx(const uint256& txHash) const
889889
LOCK(cs_pendingLocks);
890890
auto it = pendingNoTxInstantSendLocks.begin();
891891
while (it != pendingNoTxInstantSendLocks.end()) {
892-
if (it->second.second->txid == txHash) {
892+
if (it->second.islock->txid == txHash) {
893893
LogPrint(BCLog::INSTANTSEND, "CInstantSendManager::%s -- txid=%s, islock=%s\n", __func__, txHash.ToString(),
894894
it->first.ToString());
895895
return true;

src/instantsend/instantsend.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ struct DbWrapperParams;
3838
namespace instantsend {
3939
class InstantSendSigner;
4040

41+
struct PendingISLockFromPeer {
42+
NodeId node_id;
43+
InstantSendLockPtr islock;
44+
};
45+
4146
struct PendingState {
4247
bool m_pending_work{false};
4348
std::vector<std::pair<NodeId, MessageProcessingResult>> m_peer_activity{};
@@ -69,9 +74,9 @@ class CInstantSendManager final : public instantsend::InstantSendSignerParent
6974

7075
mutable Mutex cs_pendingLocks;
7176
// Incoming and not verified yet
72-
Uint256HashMap<std::pair<NodeId, instantsend::InstantSendLockPtr>> pendingInstantSendLocks GUARDED_BY(cs_pendingLocks);
77+
Uint256HashMap<instantsend::PendingISLockFromPeer> pendingInstantSendLocks GUARDED_BY(cs_pendingLocks);
7378
// Tried to verify but there is no tx yet
74-
Uint256HashMap<std::pair<NodeId, instantsend::InstantSendLockPtr>> pendingNoTxInstantSendLocks GUARDED_BY(cs_pendingLocks);
79+
Uint256HashMap<instantsend::PendingISLockFromPeer> pendingNoTxInstantSendLocks GUARDED_BY(cs_pendingLocks);
7580

7681
// TXs which are neither IS locked nor ChainLocked. We use this to determine for which TXs we need to retry IS
7782
// locking of child TXs
@@ -117,7 +122,7 @@ class CInstantSendManager final : public instantsend::InstantSendSignerParent
117122
EXCLUSIVE_LOCKS_REQUIRED(!cs_nonLocked, !cs_pendingLocks, !cs_pendingRetry);
118123

119124
Uint256HashSet ProcessPendingInstantSendLocks(const Consensus::LLMQParams& llmq_params, int signOffset, bool ban,
120-
const Uint256HashMap<std::pair<NodeId, instantsend::InstantSendLockPtr>>& pend,
125+
const Uint256HashMap<instantsend::PendingISLockFromPeer>& pend,
121126
std::vector<std::pair<NodeId, MessageProcessingResult>>& peer_activity)
122127
EXCLUSIVE_LOCKS_REQUIRED(!cs_nonLocked, !cs_pendingLocks, !cs_pendingRetry);
123128
MessageProcessingResult ProcessInstantSendLock(NodeId from, const uint256& hash,

0 commit comments

Comments
 (0)