Skip to content

Commit dbed701

Browse files
committed
wallet: Add asserts to detect unset transaction height values
Also document GetTxDepthInMainChain preconditions better
1 parent cdd6644 commit dbed701

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

src/wallet/wallet.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3296,8 +3296,10 @@ int CWallet::GetTxDepthInMainChain(const CWalletTx& wtx) const
32963296
{
32973297
AssertLockHeld(cs_wallet);
32983298
if (auto* conf = wtx.state<TxStateConfirmed>()) {
3299+
assert(conf->confirmed_block_height >= 0);
32993300
return GetLastBlockHeight() - conf->confirmed_block_height + 1;
33003301
} else if (auto* conf = wtx.state<TxStateConflicted>()) {
3302+
assert(conf->conflicting_block_height >= 0);
33013303
return -1 * (GetLastBlockHeight() - conf->conflicting_block_height + 1);
33023304
} else {
33033305
return 0;

src/wallet/wallet.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,13 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
503503
* <0 : conflicts with a transaction this deep in the blockchain
504504
* 0 : in memory pool, waiting to be included in a block
505505
* >=1 : this many blocks deep in the main chain
506+
*
507+
* Preconditions: it is only valid to call this function when the wallet is
508+
* online and the block index is loaded. So this cannot be called by
509+
* bitcoin-wallet tool code or by wallet migration code. If this is called
510+
* without the wallet being online, it won't be able able to determine the
511+
* the height of the last block processed, or the heights of blocks
512+
* referenced in transaction, and might cause assert failures.
506513
*/
507514
int GetTxDepthInMainChain(const CWalletTx& wtx) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
508515
bool IsTxInMainChain(const CWalletTx& wtx) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)

0 commit comments

Comments
 (0)