Skip to content

Commit aafef95

Browse files
refactor: improve optional usage
1 parent 49ee1bd commit aafef95

File tree

3 files changed

+9
-17
lines changed

3 files changed

+9
-17
lines changed

src/evo/cbtx.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,23 +85,20 @@ auto CachedGetQcHashesQcIndexedHashes(const CBlockIndex* pindexPrev, const llmq:
8585
for (const auto& blockIndex : vecBlockIndexes) {
8686
uint256 block_hash{blockIndex->GetBlockHash()};
8787

88-
std::pair<uint256, int> qc_hash;
89-
if (auto cached = qc_hashes_cached[llmqType].get(block_hash)) {
90-
qc_hash = *cached;
91-
} else {
88+
std::optional<std::pair<uint256, int>> qc_hash = qc_hashes_cached[llmqType].get(block_hash);
89+
if (!qc_hash.has_value()) {
9290
auto [pqc, dummy_hash] = quorum_block_processor.GetMinedCommitment(llmqType, block_hash);
9391
if (dummy_hash == uint256::ZERO) {
9492
// this should never happen
9593
return std::nullopt;
9694
}
97-
qc_hash.first = ::SerializeHash(pqc);
98-
qc_hash.second = rotation_enabled ? pqc.quorumIndex : 0;
99-
qc_hashes_cached[llmqType].insert(block_hash, qc_hash);
95+
qc_hash = {::SerializeHash(pqc), rotation_enabled ? pqc.quorumIndex : 0};
96+
qc_hashes_cached[llmqType].insert(block_hash, *qc_hash);
10097
}
10198
if (rotation_enabled) {
102-
map_indexed_hashes[qc_hash.second] = qc_hash.first;
99+
map_indexed_hashes[qc_hash->second] = qc_hash->first;
103100
} else {
104-
vec_hashes.emplace_back(qc_hash.first);
101+
vec_hashes.emplace_back(qc_hash->first);
105102
}
106103
}
107104
}

src/evo/creditpool.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,6 @@ static std::optional<CreditPoolDataPerBlock> GetCreditDataFromBlock(const gsl::n
6565
return std::nullopt;
6666
}
6767

68-
CreditPoolDataPerBlock blockData;
69-
7068
static Mutex cache_mutex;
7169
static Uint256LruHashMap<CreditPoolDataPerBlock> block_data_cache GUARDED_BY(cache_mutex){
7270
static_cast<size_t>(Params().CreditPoolPeriodBlocks()) * 2};
@@ -84,7 +82,7 @@ static std::optional<CreditPoolDataPerBlock> GetCreditDataFromBlock(const gsl::n
8482
return std::nullopt;
8583
}
8684

87-
85+
CreditPoolDataPerBlock blockData;
8886
if (const auto opt_cbTx = GetTxPayload<CCbTx>(block.vtx[0]->vExtraPayload); opt_cbTx) {
8987
blockData.credit_pool = opt_cbTx->creditPoolBalance;
9088
} else {
@@ -120,15 +118,14 @@ std::optional<CCreditPool> CCreditPoolManager::GetFromCache(const CBlockIndex& b
120118
if (!DeploymentActiveAt(block_index, Params().GetConsensus(), Consensus::DEPLOYMENT_V20)) return CCreditPool{};
121119

122120
const uint256 block_hash = block_index.GetBlockHash();
123-
CCreditPool pool;
124121
{
125122
LOCK(cache_mutex);
126123
if (auto cached = creditPoolCache.get(block_hash)) {
127124
return *cached;
128125
}
129126
}
130127
if (block_index.nHeight % DISK_SNAPSHOT_PERIOD == 0) {
131-
if (evoDb.Read(std::make_pair(DB_CREDITPOOL_SNAPSHOT, block_hash), pool)) {
128+
if (CCreditPool pool; evoDb.Read(std::make_pair(DB_CREDITPOOL_SNAPSHOT, block_hash), pool)) {
132129
LOCK(cache_mutex);
133130
creditPoolCache.insert(block_hash, pool);
134131
return pool;

src/instantsend/db.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,9 +277,7 @@ InstantSendLockPtr CInstantSendDb::GetInstantSendLockByHashInternal(const uint25
277277
}
278278
}
279279

280-
InstantSendLockPtr ret;
281-
282-
ret = std::make_shared<InstantSendLock>();
280+
InstantSendLockPtr ret{std::make_shared<InstantSendLock>()};
283281
bool exists = db->Read(std::make_tuple(DB_ISLOCK_BY_HASH, hash), *ret);
284282
if (!exists || (::SerializeHash(*ret) != hash)) {
285283
ret = std::make_shared<InstantSendLock>();

0 commit comments

Comments
 (0)