Skip to content

Commit b64081b

Browse files
meshcolliderPastaPastaPasta
authored andcommitted
Merge bitcoin#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
1 parent bba596d commit b64081b

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
@@ -1360,4 +1360,37 @@ BOOST_FIXTURE_TEST_CASE(dummy_input_size_test, TestChain100Setup)
13601360
BOOST_CHECK_EQUAL(CalculateNestedKeyhashInputSize(true), DUMMY_NESTED_P2PKH_INPUT_SIZE + 1);
13611361
}
13621362

1363+
BOOST_FIXTURE_TEST_CASE(ZapSelectTx, TestChain100Setup)
1364+
{
1365+
auto chain = interfaces::MakeChain(m_node);
1366+
auto wallet = TestLoadWallet(m_node);
1367+
CKey key;
1368+
key.MakeNewKey(true);
1369+
AddKey(*wallet, key);
1370+
1371+
std::string error;
1372+
m_coinbase_txns.push_back(CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())).vtx[0]);
1373+
auto block_tx = TestSimpleSpend(*m_coinbase_txns[0], 0, coinbaseKey, GetScriptForRawPubKey(key.GetPubKey()));
1374+
CreateAndProcessBlock({block_tx}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
1375+
1376+
SyncWithValidationInterfaceQueue();
1377+
1378+
{
1379+
auto block_hash = block_tx.GetHash();
1380+
auto prev_hash = m_coinbase_txns[0]->GetHash();
1381+
1382+
LOCK(wallet->cs_wallet);
1383+
BOOST_CHECK(wallet->HasWalletSpend(prev_hash));
1384+
BOOST_CHECK_EQUAL(wallet->mapWallet.count(block_hash), 1);
1385+
1386+
std::vector<uint256> vHashIn{ block_hash }, vHashOut;
1387+
BOOST_CHECK_EQUAL(wallet->ZapSelectTx(vHashIn, vHashOut), DBErrors::LOAD_OK);
1388+
1389+
BOOST_CHECK(!wallet->HasWalletSpend(prev_hash));
1390+
BOOST_CHECK_EQUAL(wallet->mapWallet.count(block_hash), 0);
1391+
}
1392+
1393+
TestUnloadWallet(std::move(wallet));
1394+
}
1395+
13631396
BOOST_AUTO_TEST_SUITE_END()

src/wallet/wallet.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3912,9 +3912,11 @@ DBErrors CWallet::ZapSelectTx(std::vector<uint256>& vHashIn, std::vector<uint256
39123912
WalletLogPrintf("ZapSelectTx started for %d transactions...\n", vHashIn.size());
39133913

39143914
DBErrors nZapSelectTxRet = WalletBatch(GetDatabase()).ZapSelectTx(vHashIn, vHashOut);
3915-
for (uint256 hash : vHashOut) {
3915+
for (const uint256& hash : vHashOut) {
39163916
const auto& it = mapWallet.find(hash);
39173917
wtxOrdered.erase(it->second.m_it_wtxOrdered);
3918+
for (const auto& txin : it->second.tx->vin)
3919+
mapTxSpends.erase(txin.prevout);
39183920
mapWallet.erase(it);
39193921
NotifyTransactionChanged(this, hash, CT_DELETED);
39203922
}

0 commit comments

Comments
 (0)