Skip to content

Commit 864476b

Browse files
PastaPastaPastaknstUdjinM6
authored
refactor(coinjoin): convert nConfirmedHeight to std::optional (dashpay#5108)
## Issue being fixed or feature implemented Avoids magic numbers ## What was done? converted to std::optional ## How Has This Been Tested? make check ## Breaking Changes none ## Checklist: - [x] I have performed a self-review of my own code - [x] I have commented my code, particularly in hard-to-understand areas - [x] I have added or updated relevant unit/integration/functional/e2e tests - [x] I have made corresponding changes to the documentation **For repository code-owners and collaborators only** - [x] I have assigned this pull request to a milestone --------- Co-authored-by: Konstantin Akimov <[email protected]> Co-authored-by: UdjinM6 <[email protected]>
1 parent 633bd05 commit 864476b

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

src/coinjoin/coinjoin.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ bool CCoinJoinBroadcastTx::CheckSignature(const CBLSPublicKey& blsPubKey) const
122122
bool CCoinJoinBroadcastTx::IsExpired(const CBlockIndex* pindex, const llmq::CChainLocksHandler& clhandler) const
123123
{
124124
// expire confirmed DSTXes after ~1h since confirmation or chainlocked confirmation
125-
if (nConfirmedHeight == -1 || pindex->nHeight < nConfirmedHeight) return false; // not mined yet
126-
if (pindex->nHeight - nConfirmedHeight > 24) return true; // mined more than an hour ago
125+
if (!nConfirmedHeight.has_value() || pindex->nHeight < *nConfirmedHeight) return false; // not mined yet
126+
if (pindex->nHeight - *nConfirmedHeight > 24) return true; // mined more than an hour ago
127127
return clhandler.HasChainLock(pindex->nHeight, *pindex->phashBlock);
128128
}
129129

@@ -471,7 +471,7 @@ void CCoinJoin::NotifyChainLock(const CBlockIndex* pindex, const llmq::CChainLoc
471471
}
472472
}
473473

474-
void CCoinJoin::UpdateDSTXConfirmedHeight(const CTransactionRef& tx, int nHeight)
474+
void CCoinJoin::UpdateDSTXConfirmedHeight(const CTransactionRef& tx, std::optional<int> nHeight)
475475
{
476476
AssertLockHeld(cs_mapdstx);
477477

@@ -481,14 +481,14 @@ void CCoinJoin::UpdateDSTXConfirmedHeight(const CTransactionRef& tx, int nHeight
481481
}
482482

483483
it->second.SetConfirmedHeight(nHeight);
484-
LogPrint(BCLog::COINJOIN, "CCoinJoin::%s -- txid=%s, nHeight=%d\n", __func__, tx->GetHash().ToString(), nHeight);
484+
LogPrint(BCLog::COINJOIN, "CCoinJoin::%s -- txid=%s, nHeight=%d\n", __func__, tx->GetHash().ToString(), nHeight.value_or(-1));
485485
}
486486

487487
void CCoinJoin::TransactionAddedToMempool(const CTransactionRef& tx)
488488
{
489489
AssertLockNotHeld(cs_mapdstx);
490490
LOCK(cs_mapdstx);
491-
UpdateDSTXConfirmedHeight(tx, -1);
491+
UpdateDSTXConfirmedHeight(tx, std::nullopt);
492492
}
493493

494494
void CCoinJoin::BlockConnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindex)
@@ -506,7 +506,7 @@ void CCoinJoin::BlockDisconnected(const std::shared_ptr<const CBlock>& pblock, c
506506
AssertLockNotHeld(cs_mapdstx);
507507
LOCK(cs_mapdstx);
508508
for (const auto& tx : pblock->vtx) {
509-
UpdateDSTXConfirmedHeight(tx, -1);
509+
UpdateDSTXConfirmedHeight(tx, std::nullopt);
510510
}
511511
}
512512

src/coinjoin/coinjoin.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,8 @@ class CCoinJoinBroadcastTx
265265
{
266266
private:
267267
// memory only
268-
// when corresponding tx is 0-confirmed or conflicted, nConfirmedHeight is -1
269-
int nConfirmedHeight{-1};
268+
// when corresponding tx is 0-confirmed or conflicted, nConfirmedHeight is std::nullopt
269+
std::optional<int> nConfirmedHeight{std::nullopt};
270270

271271
public:
272272
CTransactionRef tx;
@@ -322,7 +322,7 @@ class CCoinJoinBroadcastTx
322322
bool Sign();
323323
[[nodiscard]] bool CheckSignature(const CBLSPublicKey& blsPubKey) const;
324324

325-
void SetConfirmedHeight(int nConfirmedHeightIn) { nConfirmedHeight = nConfirmedHeightIn; }
325+
void SetConfirmedHeight(std::optional<int> nConfirmedHeightIn) { assert(nConfirmedHeightIn == std::nullopt || *nConfirmedHeightIn > 0); nConfirmedHeight = nConfirmedHeightIn; }
326326
bool IsExpired(const CBlockIndex* pindex, const llmq::CChainLocksHandler& clhandler) const;
327327
[[nodiscard]] bool IsValidStructure() const;
328328
};
@@ -505,11 +505,11 @@ class CCoinJoin
505505
static void UpdatedBlockTip(const CBlockIndex* pindex, const llmq::CChainLocksHandler& clhandler, const std::unique_ptr<CMasternodeSync>& mn_sync);
506506
static void NotifyChainLock(const CBlockIndex* pindex, const llmq::CChainLocksHandler& clhandler, const std::unique_ptr<CMasternodeSync>& mn_sync);
507507

508-
static void UpdateDSTXConfirmedHeight(const CTransactionRef& tx, int nHeight);
509508
static void TransactionAddedToMempool(const CTransactionRef& tx) LOCKS_EXCLUDED(cs_mapdstx);
510509
static void BlockConnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindex) LOCKS_EXCLUDED(cs_mapdstx);
511510
static void BlockDisconnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex*) LOCKS_EXCLUDED(cs_mapdstx);
512-
511+
private:
512+
static void UpdateDSTXConfirmedHeight(const CTransactionRef& tx, std::optional<int> nHeight);
513513
};
514514

515515
#endif // BITCOIN_COINJOIN_COINJOIN_H

0 commit comments

Comments
 (0)