You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

p2p-feefilter.py 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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."""
  6. from test_framework.mininode import *
  7. from test_framework.test_framework import BitcoinTestFramework
  8. from test_framework.util import *
  9. import time
  10. def hashToHex(hash):
  11. return format(hash, '064x')
  12. # Wait up to 60 secs to see if the testnode has received all the expected invs
  13. def allInvsMatch(invsExpected, testnode):
  14. for x in range(60):
  15. with mininode_lock:
  16. if (sorted(invsExpected) == sorted(testnode.txinvs)):
  17. return True
  18. time.sleep(1)
  19. return False
  20. # TestNode: bare-bones "peer". Used to track which invs are received from a node
  21. # and to send the node feefilter messages.
  22. class TestNode(SingleNodeConnCB):
  23. def __init__(self):
  24. SingleNodeConnCB.__init__(self)
  25. self.txinvs = []
  26. def on_inv(self, conn, message):
  27. for i in message.inv:
  28. if (i.type == 1):
  29. self.txinvs.append(hashToHex(i.hash))
  30. def clear_invs(self):
  31. with mininode_lock:
  32. self.txinvs = []
  33. def send_filter(self, feerate):
  34. self.send_message(msg_feefilter(feerate))
  35. self.sync_with_ping()
  36. class FeeFilterTest(BitcoinTestFramework):
  37. def __init__(self):
  38. super().__init__()
  39. self.num_nodes = 2
  40. self.setup_clean_chain = False
  41. def setup_network(self):
  42. # Node1 will be used to generate txs which should be relayed from Node0
  43. # to our test node
  44. self.nodes = []
  45. self.nodes.append(start_node(0, self.options.tmpdir))
  46. self.nodes.append(start_node(1, self.options.tmpdir))
  47. connect_nodes(self.nodes[0], 1)
  48. def run_test(self):
  49. node1 = self.nodes[1]
  50. node0 = self.nodes[0]
  51. # Get out of IBD
  52. node1.generate(1)
  53. sync_blocks(self.nodes)
  54. # Setup the p2p connections and start up the network thread.
  55. test_node = TestNode()
  56. connection = NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], test_node)
  57. test_node.add_connection(connection)
  58. NetworkThread().start()
  59. test_node.wait_for_verack()
  60. # Test that invs are received for all txs at feerate of 20 sat/byte
  61. node1.settxfee(Decimal("0.00020000"))
  62. txids = [node1.sendtoaddress(node1.getnewaddress(), 1) for x in range(3)]
  63. assert(allInvsMatch(txids, test_node))
  64. test_node.clear_invs()
  65. # Set a filter of 15 sat/byte
  66. test_node.send_filter(15000)
  67. # Test that txs are still being received (paying 20 sat/byte)
  68. txids = [node1.sendtoaddress(node1.getnewaddress(), 1) for x in range(3)]
  69. assert(allInvsMatch(txids, test_node))
  70. test_node.clear_invs()
  71. # Change tx fee rate to 10 sat/byte and test they are no longer received
  72. node1.settxfee(Decimal("0.00010000"))
  73. [node1.sendtoaddress(node1.getnewaddress(), 1) for x in range(3)]
  74. sync_mempools(self.nodes) # must be sure node 0 has received all txs
  75. # Send one transaction from node0 that should be received, so that we
  76. # we can sync the test on receipt (if node1's txs were relayed, they'd
  77. # be received by the time this node0 tx is received). This is
  78. # unfortunately reliant on the current relay behavior where we batch up
  79. # to 35 entries in an inv, which means that when this next transaction
  80. # is eligible for relay, the prior transactions from node1 are eligible
  81. # as well.
  82. node0.settxfee(Decimal("0.00020000"))
  83. txids = [node0.sendtoaddress(node0.getnewaddress(), 1)]
  84. assert(allInvsMatch(txids, test_node))
  85. test_node.clear_invs()
  86. # Remove fee filter and check that txs are received again
  87. test_node.send_filter(0)
  88. txids = [node1.sendtoaddress(node1.getnewaddress(), 1) for x in range(3)]
  89. assert(allInvsMatch(txids, test_node))
  90. test_node.clear_invs()
  91. if __name__ == '__main__':
  92. FeeFilterTest().main()