Skip to content

Commit f06016d

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

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
@@ -3303,8 +3303,10 @@ int CWallet::GetTxDepthInMainChain(const CWalletTx& wtx) const
33033303
{
33043304
AssertLockHeld(cs_wallet);
33053305
if (auto* conf = wtx.state<TxStateConfirmed>()) {
3306+
assert(conf->confirmed_block_height >= 0);
33063307
return GetLastBlockHeight() - conf->confirmed_block_height + 1;
33073308
} else if (auto* conf = wtx.state<TxStateConflicted>()) {
3309+
assert(conf->conflicting_block_height >= 0);
33083310
return -1 * (GetLastBlockHeight() - conf->conflicting_block_height + 1);
33093311
} else {
33103312
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)