Skip to content

Commit

Permalink
test: wallet, add coverage for crash on duplicated disconnectBlock call
Browse files Browse the repository at this point in the history
  • Loading branch information
furszy committed Jan 29, 2025
1 parent 46375b4 commit 4229839
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/wallet/test/wallet_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1021,5 +1021,41 @@ BOOST_FIXTURE_TEST_CASE(wallet_sync_tx_invalid_state_test, TestingSetup)
HasReason("DB error adding transaction to wallet, write failed"));
}

/**
* Verify the wallet does not crash when the same block is disconnected twice due to an incompatible coinbase transaction state.
*/
BOOST_FIXTURE_TEST_CASE(wallet_crash_during_disconnectBlock, TestChain100Setup)
{
const auto& chainman = Assert(m_node.chainman);
const int64_t BLOCK_TIME = WITH_LOCK(chainman->GetMutex(), return chainman->ActiveChain().Tip()->GetBlockTimeMax() + 5);
SetMockTime(BLOCK_TIME); // to by-pass the wallet birth time.

CKey key;
key.MakeNewKey(true);
CreateAndProcessBlock({}, GetScriptForRawPubKey(key.GetPubKey()));
auto wallet = CreateSyncedWallet(*m_node.chain, WITH_LOCK(chainman->GetMutex(), return chainman->ActiveChain()), key);
BOOST_CHECK(GetBalance(*wallet, /*min_depth=*/0, false).m_mine_immature == 50 * COIN);

// Fetch the latest block data.
CBlockIndex* tip = WITH_LOCK(chainman->GetMutex(), return chainman->ActiveChain().Tip());
CBlock block_data;
m_node.chain->findBlock(tip->GetBlockHash(), interfaces::FoundBlock().data(block_data));

// Construct block info for disconnection
interfaces::BlockInfo info(*tip->phashBlock);
info.data = &block_data;
info.prev_hash = tip->phashBlock;
info.height = tip->nHeight;
info.chain_time_max = tip->GetBlockTimeMax();

// Disconnect block twice
wallet->blockDisconnected(info);
wallet->blockDisconnected(info);

// Ensure the balance was updated correctly.
BOOST_CHECK(GetBalance(*wallet, /*min_depth=*/0, false).m_mine_immature == 0 * COIN);
}


BOOST_AUTO_TEST_SUITE_END()
} // namespace wallet

0 comments on commit 4229839

Please sign in to comment.