Skip to content

Commit 262a78b

Browse files
committed
wallet, refactor: Add CWalletTx::updateState function
No change in behavior, this just moves code which updates transaction state to a new method so it can be used after offline processes such as wallet migration.
1 parent d724bb5 commit 262a78b

File tree

3 files changed

+34
-17
lines changed

3 files changed

+34
-17
lines changed

src/wallet/transaction.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
#include <wallet/transaction.h>
66

7+
#include <interfaces/chain.h>
8+
9+
using interfaces::FoundBlock;
10+
711
namespace wallet {
812
bool CWalletTx::IsEquivalentTo(const CWalletTx& _tx) const
913
{
@@ -25,6 +29,27 @@ int64_t CWalletTx::GetTxTime() const
2529
return n ? n : nTimeReceived;
2630
}
2731

32+
void CWalletTx::updateState(interfaces::Chain& chain)
33+
{
34+
bool active;
35+
auto lookup_block = [&](const uint256& hash, int& height, TxState& state) {
36+
// If tx block (or conflicting block) was reorged out of chain
37+
// while the wallet was shutdown, change tx status to UNCONFIRMED
38+
// and reset block height, hash, and index. ABANDONED tx don't have
39+
// associated blocks and don't need to be updated. The case where a
40+
// transaction was reorged out while online and then reconfirmed
41+
// while offline is covered by the rescan logic.
42+
if (!chain.findBlock(hash, FoundBlock().inActiveChain(active).height(height)) || !active) {
43+
state = TxStateInactive{};
44+
}
45+
};
46+
if (auto* conf = state<TxStateConfirmed>()) {
47+
lookup_block(conf->confirmed_block_hash, conf->confirmed_block_height, m_state);
48+
} else if (auto* conf = state<TxStateConflicted>()) {
49+
lookup_block(conf->conflicting_block_hash, conf->conflicting_block_height, m_state);
50+
}
51+
}
52+
2853
void CWalletTx::CopyFrom(const CWalletTx& _tx)
2954
{
3055
*this = _tx;

src/wallet/transaction.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
#include <variant>
2222
#include <vector>
2323

24+
namespace interfaces {
25+
class Chain;
26+
} // namespace interfaces
27+
2428
namespace wallet {
2529
//! State of transaction confirmed in a block.
2630
struct TxStateConfirmed {
@@ -325,6 +329,10 @@ class CWalletTx
325329
template<typename T> const T* state() const { return std::get_if<T>(&m_state); }
326330
template<typename T> T* state() { return std::get_if<T>(&m_state); }
327331

332+
//! Update transaction state when attaching to a chain, filling in heights
333+
//! of conflicted and confirmed blocks
334+
void updateState(interfaces::Chain& chain);
335+
328336
bool isAbandoned() const { return state<TxStateInactive>() && state<TxStateInactive>()->abandoned; }
329337
bool isConflicted() const { return state<TxStateConflicted>(); }
330338
bool isInactive() const { return state<TxStateInactive>(); }

src/wallet/wallet.cpp

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,23 +1184,7 @@ bool CWallet::LoadToWallet(const uint256& hash, const UpdateWalletTxFn& fill_wtx
11841184
// If wallet doesn't have a chain (e.g when using bitcoin-wallet tool),
11851185
// don't bother to update txn.
11861186
if (HaveChain()) {
1187-
bool active;
1188-
auto lookup_block = [&](const uint256& hash, int& height, TxState& state) {
1189-
// If tx block (or conflicting block) was reorged out of chain
1190-
// while the wallet was shutdown, change tx status to UNCONFIRMED
1191-
// and reset block height, hash, and index. ABANDONED tx don't have
1192-
// associated blocks and don't need to be updated. The case where a
1193-
// transaction was reorged out while online and then reconfirmed
1194-
// while offline is covered by the rescan logic.
1195-
if (!chain().findBlock(hash, FoundBlock().inActiveChain(active).height(height)) || !active) {
1196-
state = TxStateInactive{};
1197-
}
1198-
};
1199-
if (auto* conf = wtx.state<TxStateConfirmed>()) {
1200-
lookup_block(conf->confirmed_block_hash, conf->confirmed_block_height, wtx.m_state);
1201-
} else if (auto* conf = wtx.state<TxStateConflicted>()) {
1202-
lookup_block(conf->conflicting_block_hash, conf->conflicting_block_height, wtx.m_state);
1203-
}
1187+
wtx.updateState(chain());
12041188
}
12051189
if (/* insertion took place */ ins.second) {
12061190
wtx.m_it_wtxOrdered = wtxOrdered.insert(std::make_pair(wtx.nOrderPos, &wtx));

0 commit comments

Comments
 (0)