From 22e3c063ef512b3ce4893003839cf726edbf52be Mon Sep 17 00:00:00 2001 From: glozow Date: Mon, 4 Sep 2023 12:04:45 +0100 Subject: [PATCH] [fuzz] test new orphanage methods --- src/test/fuzz/txorphan.cpp | 41 ++++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/src/test/fuzz/txorphan.cpp b/src/test/fuzz/txorphan.cpp index 4881f691eabbe7..cfc3021b76bb17 100644 --- a/src/test/fuzz/txorphan.cpp +++ b/src/test/fuzz/txorphan.cpp @@ -80,7 +80,9 @@ FUZZ_TARGET(txorphan, .init = initialize_orphanage) // trigger orphanage functions LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10 * DEFAULT_MAX_ORPHAN_TRANSACTIONS) { - NodeId peer_id = fuzzed_data_provider.ConsumeIntegral(); + // Limit possible peers to hit more multiple-announcer behavior. + NodeId peer_id = fuzzed_data_provider.ConsumeIntegralInRange(0, 5); + const auto& wtxid = tx->GetWitnessHash(); CallOneOf( fuzzed_data_provider, @@ -91,8 +93,9 @@ FUZZ_TARGET(txorphan, .init = initialize_orphanage) { CTransactionRef ref = orphanage.GetTxToReconsider(peer_id); if (ref) { - bool have_tx = orphanage.HaveTx(GenTxid::Txid(ref->GetHash())) || orphanage.HaveTx(GenTxid::Wtxid(ref->GetHash())); - Assert(have_tx); + Assert(orphanage.HaveTx(GenTxid::Txid(ref->GetHash())) || orphanage.HaveTx(GenTxid::Wtxid(ref->GetHash()))); + Assert(orphanage.HaveTxAndPeer(GenTxid::Txid(ref->GetHash()), peer_id) || orphanage.HaveTxAndPeer(GenTxid::Wtxid(ref->GetHash()), peer_id)); + Assert(orphanage.BytesFromPeer(peer_id) >= ref->GetTotalSize()); } } }, @@ -101,9 +104,15 @@ FUZZ_TARGET(txorphan, .init = initialize_orphanage) // AddTx should return false if tx is too big or already have it // tx weight is unknown, we only check when tx is already in orphanage { + auto total_bytes_before = orphanage.TotalOrphanBytes(); + auto peer_bytes_before = orphanage.BytesFromPeer(peer_id); bool add_tx = orphanage.AddTx(tx, peer_id, {}); // have_tx == true -> add_tx == false Assert(!have_tx || !add_tx); + if (!have_tx && add_tx) { + Assert(orphanage.BytesFromPeer(peer_id) == peer_bytes_before + tx->GetTotalSize()); + Assert(orphanage.TotalOrphanBytes() == total_bytes_before + tx->GetTotalSize()); + } } have_tx = orphanage.HaveTx(GenTxid::Txid(tx->GetHash())) || orphanage.HaveTx(GenTxid::Wtxid(tx->GetHash())); { @@ -127,14 +136,34 @@ FUZZ_TARGET(txorphan, .init = initialize_orphanage) }, [&] { orphanage.EraseForPeer(peer_id); + Assert(orphanage.BytesFromPeer(peer_id) == 0); }, [&] { // test mocktime and expiry SetMockTime(ConsumeTime(fuzzed_data_provider)); - auto limit = fuzzed_data_provider.ConsumeIntegral(); - orphanage.LimitOrphans(limit); - Assert(orphanage.Size() <= limit); + auto count_limit = fuzzed_data_provider.ConsumeIntegral(); + auto size_limit = fuzzed_data_provider.ConsumeIntegral(); + orphanage.LimitOrphans(count_limit, size_limit); + Assert(orphanage.Size() <= count_limit); + Assert(orphanage.TotalOrphanBytes() <= size_limit); + }, + [&] { + // check EraseForBlock + CBlock block; + block.vtx.emplace_back(tx); + orphanage.EraseForBlock(block); + Assert(!orphanage.HaveTx(GenTxid::Wtxid(wtxid))); + }, + [&] { + // EraseOrphanOfPeer + orphanage.EraseOrphanOfPeer(wtxid, peer_id); + Assert(!orphanage.HaveTxAndPeer(GenTxid::Wtxid(wtxid), peer_id)); }); + if (orphanage.Size() == 0) Assert(orphanage.TotalOrphanBytes() == 0); + if (orphanage.HaveTx(GenTxid::Wtxid(wtxid))) { + Assert(orphanage.HaveTx(GenTxid::Txid(tx->GetHash()))); + Assert(orphanage.GetParentTxids(wtxid).has_value()); + } } } }