Skip to content

Commit

Permalink
Merge bitcoin/bitcoin#31040: test: Assert that when we add the max or…
Browse files Browse the repository at this point in the history
…phan amount that we cannot add anymore and that a random orphan gets dropped

5c299ec test: Assert that when we add the max orphan amount that we cannot add anymore and that a random orphan gets dropped (kevkevinpal)

Pull request description:

  After joining the bitcoin pr review club about bitcoin/bitcoin#30793

  I learned about [`CVE-2012-3789`](https://github.com/bitcoin/bitcoin/blob/master/src/net_processing.cpp#L4693)

  So I was motivated to write a functional test that covers this part of the code,

  This test should add the max number of orphans to a nodes orphanage and then attempt to add another, then asserts that the number of orphans is still at the max amount

ACKs for top commit:
  achow101:
    ACK 5c299ec
  rkrux:
    ACK 5c299ec
  instagibbs:
    ACK 5c299ec
  tdb3:
    ACK 5c299ec

Tree-SHA512: 687bba337978e0945e94af71632998221e5565a5d83cf5a59ecf2ee52c7262d8ff907b94dceea3b80bed441dd19b24790b2904e88e1da14d30827c5469fcb4d3
  • Loading branch information
achow101 committed Oct 25, 2024
2 parents 9a7206a + 5c299ec commit 25dacae
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
45 changes: 45 additions & 0 deletions test/functional/p2p_orphan_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import time

from test_framework.mempool_util import tx_in_orphanage
from test_framework.messages import (
CInv,
CTxInWitness,
Expand Down Expand Up @@ -41,6 +42,8 @@
# for one peer and y seconds for another, use specific values instead.
TXREQUEST_TIME_SKIP = NONPREF_PEER_TX_DELAY + TXID_RELAY_DELAY + OVERLOADED_PEER_TX_DELAY + 1

DEFAULT_MAX_ORPHAN_TRANSACTIONS = 100

def cleanup(func):
# Time to fastfoward (using setmocktime) in between subtests to ensure they do not interfere with
# one another, in seconds. Equal to 12 hours, which is enough to expire anything that may exist
Expand Down Expand Up @@ -566,6 +569,47 @@ def test_orphan_txid_inv(self):
assert tx_child["txid"] in node_mempool
assert_equal(node.getmempoolentry(tx_child["txid"])["wtxid"], tx_child["wtxid"])

@cleanup
def test_max_orphan_amount(self):
self.log.info("Check that we never exceed our storage limits for orphans")

node = self.nodes[0]
self.generate(self.wallet, 1)
peer_1 = node.add_p2p_connection(P2PInterface())

self.log.info("Check that orphanage is empty on start of test")
assert len(node.getorphantxs()) == 0

self.log.info("Filling up orphanage with " + str(DEFAULT_MAX_ORPHAN_TRANSACTIONS) + "(DEFAULT_MAX_ORPHAN_TRANSACTIONS) orphans")
orphans = []
parent_orphans = []
for _ in range(DEFAULT_MAX_ORPHAN_TRANSACTIONS):
tx_parent_1 = self.wallet.create_self_transfer()
tx_child_1 = self.wallet.create_self_transfer(utxo_to_spend=tx_parent_1["new_utxo"])
parent_orphans.append(tx_parent_1["tx"])
orphans.append(tx_child_1["tx"])
peer_1.send_message(msg_tx(tx_child_1["tx"]))

peer_1.sync_with_ping()
orphanage = node.getorphantxs()
assert_equal(len(orphanage), DEFAULT_MAX_ORPHAN_TRANSACTIONS)

for orphan in orphans:
assert tx_in_orphanage(node, orphan)

self.log.info("Check that we do not add more than the max orphan amount")
tx_parent_1 = self.wallet.create_self_transfer()
tx_child_1 = self.wallet.create_self_transfer(utxo_to_spend=tx_parent_1["new_utxo"])
peer_1.send_and_ping(msg_tx(tx_child_1["tx"]))
parent_orphans.append(tx_parent_1["tx"])
orphanage = node.getorphantxs()
assert_equal(len(orphanage), DEFAULT_MAX_ORPHAN_TRANSACTIONS)

self.log.info("Clearing the orphanage")
for index, parent_orphan in enumerate(parent_orphans):
peer_1.send_and_ping(msg_tx(parent_orphan))
assert_equal(len(node.getorphantxs()),0)


def run_test(self):
self.nodes[0].setmocktime(int(time.time()))
Expand All @@ -582,6 +626,7 @@ def run_test(self):
self.test_same_txid_orphan()
self.test_same_txid_orphan_of_orphan()
self.test_orphan_txid_inv()
self.test_max_orphan_amount()


if __name__ == '__main__':
Expand Down
1 change: 0 additions & 1 deletion test/functional/rpc_getorphantxs.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,5 @@ def orphan_details_match(self, orphan, tx, verbosity):
self.log.info("Check the transaction hex of orphan")
assert_equal(orphan["hex"], tx["hex"])


if __name__ == '__main__':
GetOrphanTxsTest(__file__).main()

0 comments on commit 25dacae

Please sign in to comment.