55
66import time
77
8+ from test_framework .mempool_util import tx_in_orphanage
89from test_framework .messages import (
910 CInv ,
1011 CTxInWitness ,
4142# for one peer and y seconds for another, use specific values instead.
4243TXREQUEST_TIME_SKIP = NONPREF_PEER_TX_DELAY + TXID_RELAY_DELAY + OVERLOADED_PEER_TX_DELAY + 1
4344
45+ DEFAULT_MAX_ORPHAN_TRANSACTIONS = 100
46+
4447def 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
587632if __name__ == '__main__' :
0 commit comments