aboutsummaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorRyan Ofsky <ryan@ofsky.org>2024-05-15 09:03:47 -0400
committerRyan Ofsky <ryan@ofsky.org>2024-05-15 09:56:17 -0400
commit33303b2b296cdb21b6ade3e95663e9ed58c08753 (patch)
treec41162229465f95bab162a617244e86ba1682018 /src/test
parent42d5a1ff25a8045b6f4c09fa1fb71736dbccc034 (diff)
parent0fb17bf61a40b73a2b81a18e70b3de180c917f22 (diff)
downloadbitcoin-33303b2b296cdb21b6ade3e95663e9ed58c08753.tar.xz
Merge bitcoin/bitcoin#30000: p2p: index TxOrphanage by wtxid, allow entries with same txid
0fb17bf61a40b73a2b81a18e70b3de180c917f22 [log] updates in TxOrphanage (glozow) b16da7eda76944719713be68b61f03d4acdd3e16 [functional test] attackers sending mutated orphans (glozow) 6675f6428d653bf7a53537bd773114f4fb5ba53f [unit test] TxOrphanage handling of same-txid-different-witness txns (glozow) 8923edfc1f12ebc6a074651c084ba7d249074799 [p2p] allow entries with the same txid in TxOrphanage (glozow) c31f148166f01a9167d82501a77823785d28a841 [refactor] TxOrphanage::EraseTx by wtxid (glozow) efcc5930175f31b685adb4627a038d9f0848eb1f [refactor] TxOrphanage::HaveTx only by wtxid (glozow) 7e475b9648bbee04f5825b922ba0399373eaa5a9 [p2p] don't query orphanage by txid (glozow) Pull request description: Part of #27463 in the "make orphan handling more robust" section. Currently the main map in `TxOrphanage` is indexed by txid; we do not allow 2 transactions with the same txid into TxOrphanage. This means that if we receive a transaction and want to store it in orphanage, we'll fail to do so if a same-txid-different-witness version of the tx already exists in the orphanage. The existing orphanage entry can stay until it expires 20 minutes later, or until we find that it is invalid. This means an attacker can try to block/delay us accepting an orphan transaction by sending a mutated version of the child ahead of time. See included test. Prior to #28970, we don't rely on the orphanage for anything and it would be relatively difficult to guess what transaction will go to a node's orphanage. After the parent(s) are accepted, if anybody sends us the correct transaction, we'll end up accepting it. However, this is a bit more painful for 1p1c: it's easier for an attacker to tell when a tx is going to hit a node's orphanage, and we need to store the correct orphan + receive the parent before we'll consider the package. If we start out with a bad orphan, we can't evict it until we receive the parent + try the 1p1c, and then we'll need to download the real child, put it in orphanage, download the parent again, and then retry 1p1c. ACKs for top commit: AngusP: ACK 0fb17bf61a40b73a2b81a18e70b3de180c917f22 itornaza: trACK 0fb17bf61a40b73a2b81a18e70b3de180c917f22 instagibbs: ACK 0fb17bf61a40b73a2b81a18e70b3de180c917f22 theStack: ACK 0fb17bf61a40b73a2b81a18e70b3de180c917f22 sr-gi: crACK [0fb17bf](https://github.com/bitcoin/bitcoin/pull/30000/commits/0fb17bf61a40b73a2b81a18e70b3de180c917f22) stickies-v: ACK 0fb17bf61a40b73a2b81a18e70b3de180c917f22 Tree-SHA512: edcbac7287c628bc27036920c2d4e4f63ec65087fbac1de9319c4f541515d669fc4e5fdc30c8b9a248b720da42b89153d388e91c7bf5caf4bc5b3b931ded1f59
Diffstat (limited to 'src/test')
-rw-r--r--src/test/fuzz/txorphan.cpp15
-rw-r--r--src/test/orphanage_tests.cpp57
2 files changed, 62 insertions, 10 deletions
diff --git a/src/test/fuzz/txorphan.cpp b/src/test/fuzz/txorphan.cpp
index 037c7af77f..7bebb987b1 100644
--- a/src/test/fuzz/txorphan.cpp
+++ b/src/test/fuzz/txorphan.cpp
@@ -104,13 +104,12 @@ 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->GetWitnessHash()));
- Assert(have_tx);
+ Assert(orphanage.HaveTx(ref->GetWitnessHash()));
}
}
},
[&] {
- bool have_tx = orphanage.HaveTx(GenTxid::Txid(tx->GetHash())) || orphanage.HaveTx(GenTxid::Wtxid(tx->GetWitnessHash()));
+ bool have_tx = orphanage.HaveTx(tx->GetWitnessHash());
// 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
{
@@ -118,7 +117,7 @@ FUZZ_TARGET(txorphan, .init = initialize_orphanage)
// have_tx == true -> add_tx == false
Assert(!have_tx || !add_tx);
}
- have_tx = orphanage.HaveTx(GenTxid::Txid(tx->GetHash())) || orphanage.HaveTx(GenTxid::Wtxid(tx->GetWitnessHash()));
+ have_tx = orphanage.HaveTx(tx->GetWitnessHash());
{
bool add_tx = orphanage.AddTx(tx, peer_id);
// if have_tx is still false, it must be too big
@@ -127,15 +126,15 @@ FUZZ_TARGET(txorphan, .init = initialize_orphanage)
}
},
[&] {
- bool have_tx = orphanage.HaveTx(GenTxid::Txid(tx->GetHash())) || orphanage.HaveTx(GenTxid::Wtxid(tx->GetWitnessHash()));
+ bool have_tx = orphanage.HaveTx(tx->GetWitnessHash());
// EraseTx should return 0 if m_orphans doesn't have the tx
{
- Assert(have_tx == orphanage.EraseTx(tx->GetHash()));
+ Assert(have_tx == orphanage.EraseTx(tx->GetWitnessHash()));
}
- have_tx = orphanage.HaveTx(GenTxid::Txid(tx->GetHash())) || orphanage.HaveTx(GenTxid::Wtxid(tx->GetWitnessHash()));
+ have_tx = orphanage.HaveTx(tx->GetWitnessHash());
// have_tx should be false and EraseTx should fail
{
- Assert(!have_tx && !orphanage.EraseTx(tx->GetHash()));
+ Assert(!have_tx && !orphanage.EraseTx(tx->GetWitnessHash()));
}
},
[&] {
diff --git a/src/test/orphanage_tests.cpp b/src/test/orphanage_tests.cpp
index b2643cf678..450bf6a4fc 100644
--- a/src/test/orphanage_tests.cpp
+++ b/src/test/orphanage_tests.cpp
@@ -30,8 +30,8 @@ public:
CTransactionRef RandomOrphan() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
{
LOCK(m_mutex);
- std::map<Txid, OrphanTx>::iterator it;
- it = m_orphans.lower_bound(Txid::FromUint256(InsecureRand256()));
+ std::map<Wtxid, OrphanTx>::iterator it;
+ it = m_orphans.lower_bound(Wtxid::FromUint256(InsecureRand256()));
if (it == m_orphans.end())
it = m_orphans.begin();
return it->second.tx;
@@ -70,6 +70,16 @@ static CTransactionRef MakeTransactionSpending(const std::vector<COutPoint>& out
return MakeTransactionRef(tx);
}
+// Make another (not necessarily valid) tx with the same txid but different wtxid.
+static CTransactionRef MakeMutation(const CTransactionRef& ptx)
+{
+ CMutableTransaction tx(*ptx);
+ tx.vin[0].scriptWitness.stack.push_back({5});
+ auto mutated_tx = MakeTransactionRef(tx);
+ assert(ptx->GetHash() == mutated_tx->GetHash());
+ return mutated_tx;
+}
+
static bool EqualTxns(const std::set<CTransactionRef>& set_txns, const std::vector<CTransactionRef>& vec_txns)
{
if (vec_txns.size() != set_txns.size()) return false;
@@ -180,6 +190,49 @@ BOOST_AUTO_TEST_CASE(DoS_mapOrphans)
BOOST_CHECK(orphanage.CountOrphans() == 0);
}
+BOOST_AUTO_TEST_CASE(same_txid_diff_witness)
+{
+ FastRandomContext det_rand{true};
+ TxOrphanage orphanage;
+ NodeId peer{0};
+
+ std::vector<COutPoint> empty_outpoints;
+ auto parent = MakeTransactionSpending(empty_outpoints, det_rand);
+
+ // Create children to go into orphanage.
+ auto child_normal = MakeTransactionSpending({{parent->GetHash(), 0}}, det_rand);
+ auto child_mutated = MakeMutation(child_normal);
+
+ const auto& normal_wtxid = child_normal->GetWitnessHash();
+ const auto& mutated_wtxid = child_mutated->GetWitnessHash();
+ BOOST_CHECK(normal_wtxid != mutated_wtxid);
+
+ BOOST_CHECK(orphanage.AddTx(child_normal, peer));
+ // EraseTx fails as transaction by this wtxid doesn't exist.
+ BOOST_CHECK_EQUAL(orphanage.EraseTx(mutated_wtxid), 0);
+ BOOST_CHECK(orphanage.HaveTx(normal_wtxid));
+ BOOST_CHECK(!orphanage.HaveTx(mutated_wtxid));
+
+ // Must succeed. Both transactions should be present in orphanage.
+ BOOST_CHECK(orphanage.AddTx(child_mutated, peer));
+ BOOST_CHECK(orphanage.HaveTx(normal_wtxid));
+ BOOST_CHECK(orphanage.HaveTx(mutated_wtxid));
+
+ // Outpoints map should track all entries: check that both are returned as children of the parent.
+ std::set<CTransactionRef> expected_children{child_normal, child_mutated};
+ BOOST_CHECK(EqualTxns(expected_children, orphanage.GetChildrenFromSamePeer(parent, peer)));
+
+ // Erase by wtxid: mutated first
+ BOOST_CHECK_EQUAL(orphanage.EraseTx(mutated_wtxid), 1);
+ BOOST_CHECK(orphanage.HaveTx(normal_wtxid));
+ BOOST_CHECK(!orphanage.HaveTx(mutated_wtxid));
+
+ BOOST_CHECK_EQUAL(orphanage.EraseTx(normal_wtxid), 1);
+ BOOST_CHECK(!orphanage.HaveTx(normal_wtxid));
+ BOOST_CHECK(!orphanage.HaveTx(mutated_wtxid));
+}
+
+
BOOST_AUTO_TEST_CASE(get_children)
{
FastRandomContext det_rand{true};