Skip to content

Commit 8a553c9

Browse files
ryanofskyfurszy
andcommitted
wallet: Add TxStateString function for debugging and logging
Co-authored-by: furszy <[email protected]>
1 parent dcfbf3c commit 8a553c9

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/wallet/transaction.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@ struct TxStateConfirmed {
2929
int position_in_block;
3030

3131
explicit TxStateConfirmed(const uint256& block_hash, int height, int index) : confirmed_block_hash(block_hash), confirmed_block_height(height), position_in_block(index) {}
32+
std::string toString() const { return strprintf("Confirmed (block=%s, height=%i, index=%i)", confirmed_block_hash.ToString(), confirmed_block_height, position_in_block); }
3233
};
3334

3435
//! State of transaction added to mempool.
3536
struct TxStateInMempool {
37+
std::string toString() const { return strprintf("InMempool"); }
3638
};
3739

3840
//! State of rejected transaction that conflicts with a confirmed block.
@@ -41,6 +43,7 @@ struct TxStateConflicted {
4143
int conflicting_block_height;
4244

4345
explicit TxStateConflicted(const uint256& block_hash, int height) : conflicting_block_hash(block_hash), conflicting_block_height(height) {}
46+
std::string toString() const { return strprintf("Conflicted (block=%s, height=%i)", conflicting_block_hash.ToString(), conflicting_block_height); }
4447
};
4548

4649
//! State of transaction not confirmed or conflicting with a known block and
@@ -51,6 +54,7 @@ struct TxStateInactive {
5154
bool abandoned;
5255

5356
explicit TxStateInactive(bool abandoned = false) : abandoned(abandoned) {}
57+
std::string toString() const { return strprintf("Inactive (abandoned=%i)", abandoned); }
5458
};
5559

5660
//! State of transaction loaded in an unrecognized state with unexpected hash or
@@ -62,6 +66,7 @@ struct TxStateUnrecognized {
6266
int index;
6367

6468
TxStateUnrecognized(const uint256& block_hash, int index) : block_hash(block_hash), index(index) {}
69+
std::string toString() const { return strprintf("Unrecognized (block=%s, index=%i)", block_hash.ToString(), index); }
6570
};
6671

6772
//! All possible CWalletTx states
@@ -109,6 +114,12 @@ static inline int TxStateSerializedIndex(const TxState& state)
109114
}, state);
110115
}
111116

117+
//! Return TxState or SyncTxState as a string for logging or debugging.
118+
template<typename T>
119+
std::string TxStateString(const T& state)
120+
{
121+
return std::visit([](const auto& s) { return s.toString(); }, state);
122+
}
112123

113124
/**
114125
* Cachable amount subdivided into watchonly and spendable parts.

src/wallet/wallet.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1129,7 +1129,7 @@ CWalletTx* CWallet::AddToWallet(CTransactionRef tx, const TxState& state, const
11291129
}
11301130

11311131
//// debug print
1132-
WalletLogPrintf("AddToWallet %s %s%s\n", hash.ToString(), (fInsertedNew ? "new" : ""), (fUpdated ? "update" : ""));
1132+
WalletLogPrintf("AddToWallet %s %s%s %s\n", hash.ToString(), (fInsertedNew ? "new" : ""), (fUpdated ? "update" : ""), TxStateString(state));
11331133

11341134
// Write to disk
11351135
if (fInsertedNew || fUpdated)

0 commit comments

Comments
 (0)