Merge #11726: Cleanups + nit fixes for walletdir PR
[bitcoinplatinum.git] / test / functional / p2p-feefilter.py
blobff4bed0efd026dcc2f61881629000c65ea813064
1 #!/usr/bin/env python3
2 # Copyright (c) 2016 The Bitcoin Core developers
3 # Distributed under the MIT software license, see the accompanying
4 # file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 """Test processing of feefilter messages."""
7 from test_framework.mininode import *
8 from test_framework.test_framework import BitcoinTestFramework
9 from test_framework.util import *
10 import time
13 def hashToHex(hash):
14 return format(hash, '064x')
16 # Wait up to 60 secs to see if the testnode has received all the expected invs
17 def allInvsMatch(invsExpected, testnode):
18 for x in range(60):
19 with mininode_lock:
20 if (sorted(invsExpected) == sorted(testnode.txinvs)):
21 return True
22 time.sleep(1)
23 return False
25 class TestNode(P2PInterface):
26 def __init__(self):
27 super().__init__()
28 self.txinvs = []
30 def on_inv(self, message):
31 for i in message.inv:
32 if (i.type == 1):
33 self.txinvs.append(hashToHex(i.hash))
35 def clear_invs(self):
36 with mininode_lock:
37 self.txinvs = []
39 class FeeFilterTest(BitcoinTestFramework):
40 def set_test_params(self):
41 self.num_nodes = 2
43 def run_test(self):
44 node1 = self.nodes[1]
45 node0 = self.nodes[0]
46 # Get out of IBD
47 node1.generate(1)
48 sync_blocks(self.nodes)
50 # Setup the p2p connections and start up the network thread.
51 self.nodes[0].add_p2p_connection(TestNode())
52 network_thread_start()
53 self.nodes[0].p2p.wait_for_verack()
55 # Test that invs are received for all txs at feerate of 20 sat/byte
56 node1.settxfee(Decimal("0.00020000"))
57 txids = [node1.sendtoaddress(node1.getnewaddress(), 1) for x in range(3)]
58 assert(allInvsMatch(txids, self.nodes[0].p2p))
59 self.nodes[0].p2p.clear_invs()
61 # Set a filter of 15 sat/byte
62 self.nodes[0].p2p.send_and_ping(msg_feefilter(15000))
64 # Test that txs are still being received (paying 20 sat/byte)
65 txids = [node1.sendtoaddress(node1.getnewaddress(), 1) for x in range(3)]
66 assert(allInvsMatch(txids, self.nodes[0].p2p))
67 self.nodes[0].p2p.clear_invs()
69 # Change tx fee rate to 10 sat/byte and test they are no longer received
70 node1.settxfee(Decimal("0.00010000"))
71 [node1.sendtoaddress(node1.getnewaddress(), 1) for x in range(3)]
72 sync_mempools(self.nodes) # must be sure node 0 has received all txs
74 # Send one transaction from node0 that should be received, so that we
75 # we can sync the test on receipt (if node1's txs were relayed, they'd
76 # be received by the time this node0 tx is received). This is
77 # unfortunately reliant on the current relay behavior where we batch up
78 # to 35 entries in an inv, which means that when this next transaction
79 # is eligible for relay, the prior transactions from node1 are eligible
80 # as well.
81 node0.settxfee(Decimal("0.00020000"))
82 txids = [node0.sendtoaddress(node0.getnewaddress(), 1)]
83 assert(allInvsMatch(txids, self.nodes[0].p2p))
84 self.nodes[0].p2p.clear_invs()
86 # Remove fee filter and check that txs are received again
87 self.nodes[0].p2p.send_and_ping(msg_feefilter(0))
88 txids = [node1.sendtoaddress(node1.getnewaddress(), 1) for x in range(3)]
89 assert(allInvsMatch(txids, self.nodes[0].p2p))
90 self.nodes[0].p2p.clear_invs()
92 if __name__ == '__main__':
93 FeeFilterTest().main()