5
5
6
6
import time
7
7
8
+ from test_framework .mempool_util import tx_in_orphanage
8
9
from test_framework .messages import (
9
10
CInv ,
10
11
CTxInWitness ,
41
42
# for one peer and y seconds for another, use specific values instead.
42
43
TXREQUEST_TIME_SKIP = NONPREF_PEER_TX_DELAY + TXID_RELAY_DELAY + OVERLOADED_PEER_TX_DELAY + 1
43
44
45
+ DEFAULT_MAX_ORPHAN_TRANSACTIONS = 100
46
+
44
47
def cleanup (func ):
45
48
# Time to fastfoward (using setmocktime) in between subtests to ensure they do not interfere with
46
49
# 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):
566
569
assert tx_child ["txid" ] in node_mempool
567
570
assert_equal (node .getmempoolentry (tx_child ["txid" ])["wtxid" ], tx_child ["wtxid" ])
568
571
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
+
569
613
570
614
def run_test (self ):
571
615
self .nodes [0 ].setmocktime (int (time .time ()))
@@ -582,6 +626,7 @@ def run_test(self):
582
626
self .test_same_txid_orphan ()
583
627
self .test_same_txid_orphan_of_orphan ()
584
628
self .test_orphan_txid_inv ()
629
+ self .test_max_orphan_amount ()
585
630
586
631
587
632
if __name__ == '__main__' :
0 commit comments