From f06016d77d848133fc6568f287bb86b644c9fa69 Mon Sep 17 00:00:00 2001 From: Ryan Ofsky Date: Wed, 27 Sep 2023 13:16:52 -0400 Subject: [PATCH] wallet: Add asserts to detect unset transaction height values Also document GetTxDepthInMainChain preconditions better --- src/wallet/wallet.cpp | 2 ++ src/wallet/wallet.h | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 0758cd9048d..ecf18fbe783 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -3303,8 +3303,10 @@ int CWallet::GetTxDepthInMainChain(const CWalletTx& wtx) const { AssertLockHeld(cs_wallet); if (auto* conf = wtx.state()) { + assert(conf->confirmed_block_height >= 0); return GetLastBlockHeight() - conf->confirmed_block_height + 1; } else if (auto* conf = wtx.state()) { + assert(conf->conflicting_block_height >= 0); return -1 * (GetLastBlockHeight() - conf->conflicting_block_height + 1); } else { return 0; diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index 9333493a6ee..08328871590 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -503,6 +503,13 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati * <0 : conflicts with a transaction this deep in the blockchain * 0 : in memory pool, waiting to be included in a block * >=1 : this many blocks deep in the main chain + * + * Preconditions: it is only valid to call this function when the wallet is + * online and the block index is loaded. So this cannot be called by + * bitcoin-wallet tool code or by wallet migration code. If this is called + * without the wallet being online, it won't be able able to determine the + * the height of the last block processed, or the heights of blocks + * referenced in transaction, and might cause assert failures. */ int GetTxDepthInMainChain(const CWalletTx& wtx) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet); bool IsTxInMainChain(const CWalletTx& wtx) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)