wallet: Also update a CWalletTx's WalletTXOs states

When the state of a CWalletTx changes, we need to change the state in
the WalletTXOs too.
This commit is contained in:
Ava Chow 2024-02-20 11:54:46 -05:00
parent 204e49e373
commit 531aa39036
4 changed files with 10 additions and 26 deletions

View file

@ -55,4 +55,12 @@ void CWalletTx::CopyFrom(const CWalletTx& _tx)
*this = _tx;
m_txos.clear();
}
void CWalletTx::SetState(const TxState& state)
{
m_state = state;
for (auto [_, txo] : m_txos) {
txo->SetState(state);
}
}
} // namespace wallet

View file

@ -347,7 +347,7 @@ public:
template<typename T> const T* state() const { return std::get_if<T>(&m_state); }
template<typename T> T* state() { return std::get_if<T>(&m_state); }
void SetState(const TxState& state) { m_state = state; }
void SetState(const TxState& state);
const TxState& GetState() const { return m_state; }
//! Update transaction state when attaching to a chain, filling in heights

View file

@ -148,7 +148,6 @@ static void RefreshMempoolStatus(CWallet& wallet, CWalletTx& tx, interfaces::Cha
}
if (state) {
tx.SetState(*state);
wallet.RefreshSingleTxTXOs(tx);
}
}
@ -1112,13 +1111,6 @@ CWalletTx* CWallet::AddToWallet(CTransactionRef tx, const TxState& state, const
{
if (state.index() != wtx.GetState().index()) {
wtx.SetState(state);
for (unsigned int i = 0; i < wtx.tx->vout.size(); ++i) {
COutPoint outpoint(wtx.GetHash(), i);
auto it = m_txos.find(outpoint);
if (it != m_txos.end()) {
it->second.SetState(state);
}
}
fUpdated = true;
} else {
assert(TxStateSerializedIndex(wtx.GetState()) == TxStateSerializedIndex(state));
@ -1151,10 +1143,6 @@ CWalletTx* CWallet::AddToWallet(CTransactionRef tx, const TxState& state, const
MarkInputsDirty(desc_tx->tx);
for (unsigned int i = 0; i < desc_tx->tx->vout.size(); ++i) {
COutPoint outpoint(desc_tx->GetHash(), i);
auto it = m_txos.find(outpoint);
if (it != m_txos.end()) {
it->second.SetState(inactive_state);
}
std::pair<TxSpends::const_iterator, TxSpends::const_iterator> range = mapTxSpends.equal_range(outpoint);
for (TxSpends::const_iterator it = range.first; it != range.second; ++it) {
const auto wit = mapWallet.find(it->second);
@ -1422,11 +1410,6 @@ void CWallet::RecursiveUpdateTxState(WalletBatch* batch, const uint256& tx_hash,
if (batch) batch->WriteTx(wtx);
// Iterate over all its outputs, and update those tx states as well (if applicable)
for (unsigned int i = 0; i < wtx.tx->vout.size(); ++i) {
COutPoint outpoint(Txid::FromUint256(wtx.GetHash()), i);
auto it = m_txos.find(outpoint);
if (it != m_txos.end()) {
it->second.SetState(wtx.GetState());
}
std::pair<TxSpends::const_iterator, TxSpends::const_iterator> range = mapTxSpends.equal_range(COutPoint(Txid::FromUint256(now), i));
for (TxSpends::const_iterator iter = range.first; iter != range.second; ++iter) {
if (!done.count(iter->second)) {
@ -2074,13 +2057,6 @@ bool CWallet::SubmitTxMemoryPoolAndRelay(CWalletTx& wtx, std::string& err_string
bool ret = chain().broadcastTransaction(wtx.tx, m_default_max_tx_fee, relay, err_string);
if (ret) {
wtx.SetState(TxStateInMempool{});
for (unsigned int i = 0; i < wtx.tx->vout.size(); ++i) {
COutPoint outpoint(wtx.GetHash(), i);
auto it = m_txos.find(outpoint);
if (it != m_txos.end()) {
it->second.SetState(TxStateInMempool{});
}
}
}
return ret;
}

View file

@ -429,7 +429,7 @@ private:
std::unordered_map<CScript, std::vector<ScriptPubKeyMan*>, SaltedSipHasher> m_cached_spks;
//! Set of both spent and unspent transaction outputs owned by this wallet
mutable std::unordered_map<COutPoint, WalletTXO, SaltedOutpointHasher> m_txos GUARDED_BY(cs_wallet);
std::unordered_map<COutPoint, WalletTXO, SaltedOutpointHasher> m_txos GUARDED_BY(cs_wallet);
/**
* Catch wallet up to current chain, scanning new blocks, updating the best