Skip to content

Commit 5c299ec

Browse files
committed
test: Assert that when we add the max orphan amount that we cannot add anymore and that a random orphan gets dropped
1 parent 5ea335a commit 5c299ec

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

test/functional/p2p_orphan_handling.py

+45
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import time
77

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

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

572+
@cleanup
573+
def test_max_orphan_amount(self):
574+
self.log.info("Check that we never exceed our storage limits for orphans")
575+
576+
node = self.nodes[0]
577+
self.generate(self.wallet, 1)
578+
peer_1 = node.add_p2p_connection(P2PInterface())
579+
580+
self.log.info("Check that orphanage is empty on start of test")
581+
assert len(node.getorphantxs()) == 0
582+
583+
self.log.info("Filling up orphanage with " + str(DEFAULT_MAX_ORPHAN_TRANSACTIONS) + "(DEFAULT_MAX_ORPHAN_TRANSACTIONS) orphans")
584+
orphans = []
585+
parent_orphans = []
586+
for _ in range(DEFAULT_MAX_ORPHAN_TRANSACTIONS):
587+
tx_parent_1 = self.wallet.create_self_transfer()
588+
tx_child_1 = self.wallet.create_self_transfer(utxo_to_spend=tx_parent_1["new_utxo"])
589+
parent_orphans.append(tx_parent_1["tx"])
590+
orphans.append(tx_child_1["tx"])
591+
peer_1.send_message(msg_tx(tx_child_1["tx"]))
592+
593+
peer_1.sync_with_ping()
594+
orphanage = node.getorphantxs()
595+
assert_equal(len(orphanage), DEFAULT_MAX_ORPHAN_TRANSACTIONS)
596+
597+
for orphan in orphans:
598+
assert tx_in_orphanage(node, orphan)
599+
600+
self.log.info("Check that we do not add more than the max orphan amount")
601+
tx_parent_1 = self.wallet.create_self_transfer()
602+
tx_child_1 = self.wallet.create_self_transfer(utxo_to_spend=tx_parent_1["new_utxo"])
603+
peer_1.send_and_ping(msg_tx(tx_child_1["tx"]))
604+
parent_orphans.append(tx_parent_1["tx"])
605+
orphanage = node.getorphantxs()
606+
assert_equal(len(orphanage), DEFAULT_MAX_ORPHAN_TRANSACTIONS)
607+
608+
self.log.info("Clearing the orphanage")
609+
for index, parent_orphan in enumerate(parent_orphans):
610+
peer_1.send_and_ping(msg_tx(parent_orphan))
611+
assert_equal(len(node.getorphantxs()),0)
612+
569613

570614
def run_test(self):
571615
self.nodes[0].setmocktime(int(time.time()))
@@ -582,6 +626,7 @@ def run_test(self):
582626
self.test_same_txid_orphan()
583627
self.test_same_txid_orphan_of_orphan()
584628
self.test_orphan_txid_inv()
629+
self.test_max_orphan_amount()
585630

586631

587632
if __name__ == '__main__':

test/functional/rpc_getorphantxs.py

-1
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,5 @@ def orphan_details_match(self, orphan, tx, verbosity):
125125
self.log.info("Check the transaction hex of orphan")
126126
assert_equal(orphan["hex"], tx["hex"])
127127

128-
129128
if __name__ == '__main__':
130129
GetOrphanTxsTest(__file__).main()

0 commit comments

Comments
 (0)