Skip to content

Commit 60e6595

Browse files
committed
wallet: Avoid use of Chain::Lock in CWallet::GetKeyBirthTimes
This is a step toward removing the Chain::Lock class and reducing cs_main locking. This change only affects behavior in the case where wallet last block processed falls behind the chain tip, where it will treat the last block processed as the current tip.
1 parent f2c69d5 commit 60e6595

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

src/wallet/wallet.cpp

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3368,12 +3368,12 @@ void CWallet::GetKeyBirthTimes(interfaces::Chain::Lock& locked_chain, std::map<C
33683368
}
33693369

33703370
// map in which we'll infer heights of other keys
3371-
const Optional<int> tip_height = locked_chain.getHeight();
3372-
const int max_height = tip_height && *tip_height > 144 ? *tip_height - 144 : 0; // the tip can be reorganized; use a 144-block safety margin
3373-
std::map<CKeyID, int> mapKeyFirstBlock;
3371+
const int max_height = m_last_block_processed_height > 144 ? m_last_block_processed_height - 144 : 0; // the tip can be reorganized; use a 144-block safety margin
3372+
const uint256 max_block = chain().findAncestorByHeight(m_last_block_processed, max_height);
3373+
std::map<CKeyID, std::pair<uint256, int>> mapKeyFirstBlock;
33743374
for (const CKeyID &keyid : spk_man->GetKeys()) {
33753375
if (mapKeyBirth.count(keyid) == 0)
3376-
mapKeyFirstBlock[keyid] = max_height;
3376+
mapKeyFirstBlock[keyid] = {max_block, max_height};
33773377
}
33783378

33793379
// if there are no such keys, we're done
@@ -3384,23 +3384,25 @@ void CWallet::GetKeyBirthTimes(interfaces::Chain::Lock& locked_chain, std::map<C
33843384
for (const auto& entry : mapWallet) {
33853385
// iterate over all wallet transactions...
33863386
const CWalletTx &wtx = entry.second;
3387-
if (Optional<int> height = locked_chain.getBlockHeight(wtx.m_confirm.hashBlock)) {
3387+
{
33883388
// ... which are already in a block
3389-
for (const CTxOut &txout : wtx.tx->vout) {
3389+
for (const CTxOut& txout : wtx.tx->vout) {
33903390
// iterate over all their outputs
3391-
for (const auto &keyid : GetAffectedKeys(txout.scriptPubKey, *spk_man)) {
3391+
for (const auto& keyid : GetAffectedKeys(txout.scriptPubKey, *spk_man)) {
33923392
// ... and all their affected keys
3393-
std::map<CKeyID, int>::iterator rit = mapKeyFirstBlock.find(keyid);
3394-
if (rit != mapKeyFirstBlock.end() && *height < rit->second)
3395-
rit->second = *height;
3393+
auto rit = mapKeyFirstBlock.find(keyid);
3394+
if (rit != mapKeyFirstBlock.end() && wtx.m_confirm.block_height < rit->second.second) {
3395+
rit->second = {wtx.m_confirm.hashBlock, wtx.m_confirm.block_height};
3396+
}
33963397
}
33973398
}
33983399
}
33993400
}
34003401

3401-
// Extract block timestamps for those keys
3402-
for (const auto& entry : mapKeyFirstBlock)
3403-
mapKeyBirth[entry.first] = locked_chain.getBlockTime(entry.second) - TIMESTAMP_WINDOW; // block times can be 2h off
3402+
for (const auto& entry : mapKeyFirstBlock) {
3403+
int64_t block_time = 0;
3404+
mapKeyBirth[entry.first] = !chain().findBlock(entry.second.first, nullptr, &block_time) ? 0 : block_time - TIMESTAMP_WINDOW; // block times can be 2h off
3405+
}
34043406
}
34053407

34063408
/**

0 commit comments

Comments
 (0)