Skip to content

Commit 13727fb

Browse files
refactor: simplify optional usage
1 parent a5d87d6 commit 13727fb

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

src/instantsend/instantsend.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -388,11 +388,9 @@ MessageProcessingResult CInstantSendManager::ProcessInstantSendLock(NodeId from,
388388
const auto tx = GetTransaction(nullptr, &mempool, islock->txid, Params().GetConsensus(), hashBlock);
389389
const bool found_transaction{tx != nullptr};
390390
// we ignore failure here as we must be able to propagate the lock even if we don't have the TX locally
391-
int minedHeight{-1};
392-
if (found_transaction && !hashBlock.IsNull()) {
393-
if (auto h = GetBlockHeight(hashBlock)) {
394-
minedHeight = *h;
395-
} else {
391+
std::optional<int> minedHeight = GetBlockHeight(hashBlock);
392+
if (found_transaction) {
393+
if (!minedHeight.has_value()) {
396394
const CBlockIndex* pindexMined = WITH_LOCK(::cs_main, return m_chainstate.m_blockman.LookupBlockIndex(hashBlock));
397395
if (pindexMined != nullptr) {
398396
CacheBlockHeight(pindexMined->GetBlockHash(), pindexMined->nHeight);
@@ -401,7 +399,7 @@ MessageProcessingResult CInstantSendManager::ProcessInstantSendLock(NodeId from,
401399
}
402400
// Let's see if the TX that was locked by this islock is already mined in a ChainLocked block. If yes,
403401
// we can simply ignore the islock, as the ChainLock implies locking of all TXs in that chain
404-
if (clhandler.HasChainLock(minedHeight, hashBlock)) {
402+
if (minedHeight.has_value() && clhandler.HasChainLock(*minedHeight, hashBlock)) {
405403
LogPrint(BCLog::INSTANTSEND, /* Continued */
406404
"CInstantSendManager::%s -- txlock=%s, islock=%s: dropping islock as it already got a " /* Continued */
407405
"ChainLock in block %s, peer=%d\n",
@@ -412,8 +410,8 @@ MessageProcessingResult CInstantSendManager::ProcessInstantSendLock(NodeId from,
412410

413411
if (found_transaction) {
414412
db.WriteNewInstantSendLock(hash, islock);
415-
if (minedHeight >= 0) {
416-
db.WriteInstantSendLockMined(hash, minedHeight);
413+
if (minedHeight.has_value()) {
414+
db.WriteInstantSendLockMined(hash, *minedHeight);
417415
}
418416
} else {
419417
// put it in a separate pending map and try again later
@@ -978,6 +976,9 @@ void CInstantSendManager::CacheBlockHeight(const uint256& hash, int height) cons
978976

979977
std::optional<int> CInstantSendManager::GetBlockHeight(const uint256& hash) const
980978
{
979+
if (hash.IsNull()) {
980+
return std::nullopt;
981+
}
981982
{
982983
LOCK(cs_height_cache);
983984
int cached_height{0};

0 commit comments

Comments
 (0)