Skip to content

Commit 9c59f9c

Browse files
committed
Fix ZapSelectTx to sync wallet spends
Signed-off-by: Anthony Fieroni <[email protected]>
1 parent 7bcc42b commit 9c59f9c

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
@@ -787,4 +787,37 @@ BOOST_FIXTURE_TEST_CASE(CreateWalletFromFile, TestChain100Setup)
787787
TestUnloadWallet(std::move(wallet));
788788
}
789789

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

src/wallet/wallet.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3074,9 +3074,11 @@ DBErrors CWallet::ZapSelectTx(std::vector<uint256>& vHashIn, std::vector<uint256
30743074
{
30753075
AssertLockHeld(cs_wallet);
30763076
DBErrors nZapSelectTxRet = WalletBatch(*database, "cr+").ZapSelectTx(vHashIn, vHashOut);
3077-
for (uint256 hash : vHashOut) {
3077+
for (const uint256& hash : vHashOut) {
30783078
const auto& it = mapWallet.find(hash);
30793079
wtxOrdered.erase(it->second.m_it_wtxOrdered);
3080+
for (const auto& txin : it->second.tx->vin)
3081+
mapTxSpends.erase(txin.prevout);
30803082
mapWallet.erase(it);
30813083
NotifyTransactionChanged(this, hash, CT_DELETED);
30823084
}

0 commit comments

Comments
 (0)