Skip to content

Commit 4897340

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 e958ff9 commit 4897340

File tree

3 files changed

+15
-20
lines changed

3 files changed

+15
-20
lines changed

src/interfaces/chain.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,6 @@ class LockImpl : public Chain::Lock, public UniqueLock<RecursiveMutex>
8080
assert(block != nullptr);
8181
return block->GetBlockHash();
8282
}
83-
int64_t getBlockTime(int height) override
84-
{
85-
LockAssertion lock(::cs_main);
86-
CBlockIndex* block = ::ChainActive()[height];
87-
assert(block != nullptr);
88-
return block->GetBlockTime();
89-
}
9083
bool haveBlockOnDisk(int height) override
9184
{
9285
LockAssertion lock(::cs_main);

src/interfaces/chain.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,6 @@ class Chain
100100
//! Get block hash. Height must be valid or this function will abort.
101101
virtual uint256 getBlockHash(int height) = 0;
102102

103-
//! Get block time. Height must be valid or this function will abort.
104-
virtual int64_t getBlockTime(int height) = 0;
105-
106103
//! Check that the block is available on disk (i.e. has not been
107104
//! pruned), and contains transactions.
108105
virtual bool haveBlockOnDisk(int height) = 0;

src/wallet/wallet.cpp

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

35563556
// map in which we'll infer heights of other keys
3557-
const Optional<int> tip_height = locked_chain.getHeight();
3558-
const int max_height = tip_height && *tip_height > 144 ? *tip_height - 144 : 0; // the tip can be reorganized; use a 144-block safety margin
3559-
std::map<CKeyID, int> mapKeyFirstBlock;
3557+
std::map<CKeyID, const CWalletTx::Confirmation*> mapKeyFirstBlock;
3558+
CWalletTx::Confirmation max_confirm;
3559+
max_confirm.block_height = GetLastBlockHeight() > 144 ? GetLastBlockHeight() - 144 : 0; // the tip can be reorganized; use a 144-block safety margin
3560+
CHECK_NONFATAL(chain().findAncestorByHeight(GetLastBlockHash(), max_confirm.block_height, FoundBlock().hash(max_confirm.hashBlock)));
35603561
for (const CKeyID &keyid : spk_man->GetKeys()) {
35613562
if (mapKeyBirth.count(keyid) == 0)
3562-
mapKeyFirstBlock[keyid] = max_height;
3563+
mapKeyFirstBlock[keyid] = &max_confirm;
35633564
}
35643565

35653566
// if there are no such keys, we're done
@@ -3570,23 +3571,27 @@ void CWallet::GetKeyBirthTimes(interfaces::Chain::Lock& locked_chain, std::map<C
35703571
for (const auto& entry : mapWallet) {
35713572
// iterate over all wallet transactions...
35723573
const CWalletTx &wtx = entry.second;
3573-
if (Optional<int> height = locked_chain.getBlockHeight(wtx.m_confirm.hashBlock)) {
3574+
if (wtx.m_confirm.status == CWalletTx::CONFIRMED) {
35743575
// ... which are already in a block
35753576
for (const CTxOut &txout : wtx.tx->vout) {
35763577
// iterate over all their outputs
35773578
for (const auto &keyid : GetAffectedKeys(txout.scriptPubKey, *spk_man)) {
35783579
// ... and all their affected keys
3579-
std::map<CKeyID, int>::iterator rit = mapKeyFirstBlock.find(keyid);
3580-
if (rit != mapKeyFirstBlock.end() && *height < rit->second)
3581-
rit->second = *height;
3580+
auto rit = mapKeyFirstBlock.find(keyid);
3581+
if (rit != mapKeyFirstBlock.end() && wtx.m_confirm.block_height < rit->second->block_height) {
3582+
rit->second = &wtx.m_confirm;
3583+
}
35823584
}
35833585
}
35843586
}
35853587
}
35863588

35873589
// Extract block timestamps for those keys
3588-
for (const auto& entry : mapKeyFirstBlock)
3589-
mapKeyBirth[entry.first] = locked_chain.getBlockTime(entry.second) - TIMESTAMP_WINDOW; // block times can be 2h off
3590+
for (const auto& entry : mapKeyFirstBlock) {
3591+
int64_t block_time;
3592+
CHECK_NONFATAL(chain().findBlock(entry.second->hashBlock, FoundBlock().time(block_time)));
3593+
mapKeyBirth[entry.first] = block_time - TIMESTAMP_WINDOW; // block times can be 2h off
3594+
}
35903595
}
35913596

35923597
/**

0 commit comments

Comments
 (0)