Skip to content

Commit 4fc9224

Browse files
committed
Merge #18850: wallet: Fix ZapSelectTx to sync wallet spends
9c59f9c Fix ZapSelectTx to sync wallet spends (Anthony Fieroni) Pull request description: Signed-off-by: Anthony Fieroni <[email protected]> ACKs for top commit: achow101: ACK 9c59f9c ryanofsky: Code review ACK 9c59f9c. Only change since last review tweaking the for loop as suggested jonatack: ACK 9c59f9c tested rebased on current master b33136b and the new unit test does indeed fail without the change. meshcollider: utACK 9c59f9c Tree-SHA512: 71672a5ab0c659550c3a40577614ea896412b79566b5672636ab18765e4c71b9d0a990d94dc6b6e623b03a05737022b04026b5699438809c7c54782d0fd0a5d2
2 parents a426317 + 9c59f9c commit 4fc9224

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/wallet/test/wallet_tests.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,4 +791,37 @@ BOOST_FIXTURE_TEST_CASE(CreateWalletFromFile, TestChain100Setup)
791791
TestUnloadWallet(std::move(wallet));
792792
}
793793

794+
BOOST_FIXTURE_TEST_CASE(ZapSelectTx, TestChain100Setup)
795+
{
796+
auto chain = interfaces::MakeChain(m_node);
797+
auto wallet = TestLoadWallet(*chain);
798+
CKey key;
799+
key.MakeNewKey(true);
800+
AddKey(*wallet, key);
801+
802+
std::string error;
803+
m_coinbase_txns.push_back(CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())).vtx[0]);
804+
auto block_tx = TestSimpleSpend(*m_coinbase_txns[0], 0, coinbaseKey, GetScriptForRawPubKey(key.GetPubKey()));
805+
CreateAndProcessBlock({block_tx}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
806+
807+
SyncWithValidationInterfaceQueue();
808+
809+
{
810+
auto block_hash = block_tx.GetHash();
811+
auto prev_hash = m_coinbase_txns[0]->GetHash();
812+
813+
LOCK(wallet->cs_wallet);
814+
BOOST_CHECK(wallet->HasWalletSpend(prev_hash));
815+
BOOST_CHECK_EQUAL(wallet->mapWallet.count(block_hash), 1);
816+
817+
std::vector<uint256> vHashIn{ block_hash }, vHashOut;
818+
BOOST_CHECK_EQUAL(wallet->ZapSelectTx(vHashIn, vHashOut), DBErrors::LOAD_OK);
819+
820+
BOOST_CHECK(!wallet->HasWalletSpend(prev_hash));
821+
BOOST_CHECK_EQUAL(wallet->mapWallet.count(block_hash), 0);
822+
}
823+
824+
TestUnloadWallet(std::move(wallet));
825+
}
826+
794827
BOOST_AUTO_TEST_SUITE_END()

src/wallet/wallet.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3120,9 +3120,11 @@ DBErrors CWallet::ZapSelectTx(std::vector<uint256>& vHashIn, std::vector<uint256
31203120
{
31213121
AssertLockHeld(cs_wallet);
31223122
DBErrors nZapSelectTxRet = WalletBatch(*database, "cr+").ZapSelectTx(vHashIn, vHashOut);
3123-
for (uint256 hash : vHashOut) {
3123+
for (const uint256& hash : vHashOut) {
31243124
const auto& it = mapWallet.find(hash);
31253125
wtxOrdered.erase(it->second.m_it_wtxOrdered);
3126+
for (const auto& txin : it->second.tx->vin)
3127+
mapTxSpends.erase(txin.prevout);
31263128
mapWallet.erase(it);
31273129
NotifyTransactionChanged(this, hash, CT_DELETED);
31283130
}

0 commit comments

Comments
 (0)