|
| 1 | +#!/usr/bin/env python2 |
| 2 | +# Copyright (c) 2016 The Bitcoin Core developers |
| 3 | +# Distributed under the MIT/X11 software license, see the accompanying |
| 4 | +# file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 5 | +# |
| 6 | + |
| 7 | +from test_framework.mininode import * |
| 8 | +from test_framework.test_framework import BitcoinTestFramework |
| 9 | +from test_framework.util import * |
| 10 | +import time |
| 11 | + |
| 12 | +''' |
| 13 | +FeeFilterTest -- test processing of feefilter messages |
| 14 | +''' |
| 15 | + |
| 16 | +def hashToHex(hash): |
| 17 | + return format(hash, '064x').decode('utf-8') |
| 18 | + |
| 19 | +# Wait up to 60 secs to see if the testnode has received all the expected invs |
| 20 | +def allInvsMatch(invsExpected, testnode): |
| 21 | + for x in xrange(60): |
| 22 | + with mininode_lock: |
| 23 | + if (sorted(invsExpected) == sorted(testnode.txinvs)): |
| 24 | + return True; |
| 25 | + time.sleep(1) |
| 26 | + return False; |
| 27 | + |
| 28 | +# TestNode: bare-bones "peer". Used to track which invs are received from a node |
| 29 | +# and to send the node feefilter messages. |
| 30 | +class TestNode(SingleNodeConnCB): |
| 31 | + def __init__(self): |
| 32 | + SingleNodeConnCB.__init__(self) |
| 33 | + self.txinvs = [] |
| 34 | + |
| 35 | + def on_inv(self, conn, message): |
| 36 | + for i in message.inv: |
| 37 | + if (i.type == 1): |
| 38 | + self.txinvs.append(hashToHex(i.hash)) |
| 39 | + |
| 40 | + def clear_invs(self): |
| 41 | + with mininode_lock: |
| 42 | + self.txinvs = [] |
| 43 | + |
| 44 | + def send_filter(self, feerate): |
| 45 | + self.send_message(msg_feefilter(feerate)) |
| 46 | + self.sync_with_ping() |
| 47 | + |
| 48 | +class FeeFilterTest(BitcoinTestFramework): |
| 49 | + def setup_network(self): |
| 50 | + # Node1 will be used to generate txs which should be relayed from Node0 |
| 51 | + # to our test node |
| 52 | + self.nodes = [] |
| 53 | + self.nodes.append(start_node(0, self.options.tmpdir, ["-debug", "-logtimemicros"])) |
| 54 | + self.nodes.append(start_node(1, self.options.tmpdir, ["-debug", "-logtimemicros"])) |
| 55 | + connect_nodes(self.nodes[0], 1) |
| 56 | + |
| 57 | + def run_test(self): |
| 58 | + node1 = self.nodes[1] |
| 59 | + # Get out of IBD |
| 60 | + node1.generate(1) |
| 61 | + sync_blocks(self.nodes) |
| 62 | + |
| 63 | + # Setup the p2p connections and start up the network thread. |
| 64 | + test_node = TestNode() |
| 65 | + connection = NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], test_node) |
| 66 | + test_node.add_connection(connection) |
| 67 | + NetworkThread().start() |
| 68 | + test_node.wait_for_verack() |
| 69 | + |
| 70 | + # Test that invs are received for all txs at feerate of 20 sat/byte |
| 71 | + node1.settxfee(Decimal("0.00020000")) |
| 72 | + txids = [node1.sendtoaddress(node1.getnewaddress(), 1) for x in xrange(3)] |
| 73 | + assert(allInvsMatch(txids, test_node)) |
| 74 | + test_node.clear_invs() |
| 75 | + |
| 76 | + # Set a filter of 15 sat/byte |
| 77 | + test_node.send_filter(15000) |
| 78 | + |
| 79 | + # Test that txs are still being received (paying 20 sat/byte) |
| 80 | + txids = [node1.sendtoaddress(node1.getnewaddress(), 1) for x in xrange(3)] |
| 81 | + assert(allInvsMatch(txids, test_node)) |
| 82 | + test_node.clear_invs() |
| 83 | + |
| 84 | + # Change tx fee rate to 10 sat/byte and test they are no longer received |
| 85 | + node1.settxfee(Decimal("0.00010000")) |
| 86 | + [node1.sendtoaddress(node1.getnewaddress(), 1) for x in xrange(3)] |
| 87 | + sync_mempools(self.nodes) # must be sure node 0 has received all txs |
| 88 | + time.sleep(10) # wait 10 secs to be sure its doesn't relay any |
| 89 | + assert(allInvsMatch([], test_node)) |
| 90 | + test_node.clear_invs() |
| 91 | + |
| 92 | + # Remove fee filter and check that txs are received again |
| 93 | + test_node.send_filter(0) |
| 94 | + txids = [node1.sendtoaddress(node1.getnewaddress(), 1) for x in xrange(3)] |
| 95 | + assert(allInvsMatch(txids, test_node)) |
| 96 | + test_node.clear_invs() |
| 97 | + |
| 98 | +if __name__ == '__main__': |
| 99 | + FeeFilterTest().main() |
0 commit comments