Skip to content

Commit 493d077

Browse files
refactor: simplify optional usage
1 parent 0e1f114 commit 493d077

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
@@ -147,11 +147,9 @@ std::variant<uint256, CTransactionRef, std::monostate> CInstantSendManager::Proc
147147
const auto tx = GetTransaction(nullptr, &mempool, islock->txid, Params().GetConsensus(), hashBlock);
148148
const bool found_transaction{tx != nullptr};
149149
// we ignore failure here as we must be able to propagate the lock even if we don't have the TX locally
150-
int minedHeight{-1};
151-
if (found_transaction && !hashBlock.IsNull()) {
152-
if (auto h = GetBlockHeight(hashBlock)) {
153-
minedHeight = *h;
154-
} else {
150+
std::optional<int> minedHeight = GetBlockHeight(hashBlock);
151+
if (found_transaction) {
152+
if (!minedHeight.has_value()) {
155153
const CBlockIndex* pindexMined = WITH_LOCK(::cs_main, return m_chainstate.m_blockman.LookupBlockIndex(hashBlock));
156154
if (pindexMined != nullptr) {
157155
CacheBlockHeight(pindexMined->GetBlockHash(), pindexMined->nHeight);
@@ -160,7 +158,7 @@ std::variant<uint256, CTransactionRef, std::monostate> CInstantSendManager::Proc
160158
}
161159
// Let's see if the TX that was locked by this islock is already mined in a ChainLocked block. If yes,
162160
// we can simply ignore the islock, as the ChainLock implies locking of all TXs in that chain
163-
if (clhandler.HasChainLock(minedHeight, hashBlock)) {
161+
if (minedHeight.has_value() && clhandler.HasChainLock(*minedHeight, hashBlock)) {
164162
LogPrint(BCLog::INSTANTSEND, /* Continued */
165163
"CInstantSendManager::%s -- txlock=%s, islock=%s: dropping islock as it already got a "
166164
"ChainLock in block %s, peer=%d\n",
@@ -171,8 +169,8 @@ std::variant<uint256, CTransactionRef, std::monostate> CInstantSendManager::Proc
171169

172170
if (found_transaction) {
173171
db.WriteNewInstantSendLock(hash, islock);
174-
if (minedHeight >= 0) {
175-
db.WriteInstantSendLockMined(hash, minedHeight);
172+
if (minedHeight.has_value()) {
173+
db.WriteInstantSendLockMined(hash, *minedHeight);
176174
}
177175
} else {
178176
// put it in a separate pending map and try again later
@@ -748,6 +746,9 @@ void CInstantSendManager::CacheBlockHeight(const uint256& hash, int height) cons
748746

749747
std::optional<int> CInstantSendManager::GetBlockHeight(const uint256& hash) const
750748
{
749+
if (hash.IsNull()) {
750+
return std::nullopt;
751+
}
751752
{
752753
LOCK(cs_height_cache);
753754
int cached_height{0};

0 commit comments

Comments
 (0)