|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) 2015-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 | + |
| 6 | +from test_framework.mininode import * |
| 7 | +from test_framework.test_framework import BitcoinTestFramework |
| 8 | +from test_framework.util import * |
| 9 | +import time |
| 10 | + |
| 11 | +class TestNode(NodeConnCB): |
| 12 | + def __init__(self): |
| 13 | + NodeConnCB.__init__(self) |
| 14 | + self.connection = None |
| 15 | + self.ping_counter = 1 |
| 16 | + self.last_pong = msg_pong() |
| 17 | + self.block_receive_map = {} |
| 18 | + |
| 19 | + def add_connection(self, conn): |
| 20 | + self.connection = conn |
| 21 | + self.peer_disconnected = False |
| 22 | + |
| 23 | + def on_inv(self, conn, message): |
| 24 | + pass |
| 25 | + |
| 26 | + # Track the last getdata message we receive (used in the test) |
| 27 | + def on_getdata(self, conn, message): |
| 28 | + self.last_getdata = message |
| 29 | + |
| 30 | + def on_block(self, conn, message): |
| 31 | + message.block.calc_sha256() |
| 32 | + try: |
| 33 | + self.block_receive_map[message.block.sha256] += 1 |
| 34 | + except KeyError as e: |
| 35 | + self.block_receive_map[message.block.sha256] = 1 |
| 36 | + |
| 37 | + # Spin until verack message is received from the node. |
| 38 | + # We use this to signal that our test can begin. This |
| 39 | + # is called from the testing thread, so it needs to acquire |
| 40 | + # the global lock. |
| 41 | + def wait_for_verack(self): |
| 42 | + def veracked(): |
| 43 | + return self.verack_received |
| 44 | + return wait_until(veracked, timeout=10) |
| 45 | + |
| 46 | + def wait_for_disconnect(self): |
| 47 | + def disconnected(): |
| 48 | + return self.peer_disconnected |
| 49 | + return wait_until(disconnected, timeout=10) |
| 50 | + |
| 51 | + # Wrapper for the NodeConn's send_message function |
| 52 | + def send_message(self, message): |
| 53 | + self.connection.send_message(message) |
| 54 | + |
| 55 | + def on_pong(self, conn, message): |
| 56 | + self.last_pong = message |
| 57 | + |
| 58 | + def on_close(self, conn): |
| 59 | + self.peer_disconnected = True |
| 60 | + |
| 61 | + # Sync up with the node after delivery of a block |
| 62 | + def sync_with_ping(self, timeout=30): |
| 63 | + def received_pong(): |
| 64 | + return (self.last_pong.nonce == self.ping_counter) |
| 65 | + self.connection.send_message(msg_ping(nonce=self.ping_counter)) |
| 66 | + success = wait_until(received_pong, timeout) |
| 67 | + self.ping_counter += 1 |
| 68 | + return success |
| 69 | + |
| 70 | + def send_mempool(self): |
| 71 | + self.lastInv = [] |
| 72 | + self.send_message(msg_mempool()) |
| 73 | + |
| 74 | +class P2PMempoolTests(BitcoinTestFramework): |
| 75 | + def setup_chain(self): |
| 76 | + initialize_chain_clean(self.options.tmpdir, 2) |
| 77 | + |
| 78 | + def setup_network(self): |
| 79 | + # Start a node with maxuploadtarget of 200 MB (/24h) |
| 80 | + self.nodes = [] |
| 81 | + self.nodes.append(start_node(0, self.options.tmpdir, ["-debug", "-peerbloomfilters=0"])) |
| 82 | + |
| 83 | + def run_test(self): |
| 84 | + #connect a mininode |
| 85 | + aTestNode = TestNode() |
| 86 | + node = NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], aTestNode) |
| 87 | + aTestNode.add_connection(node) |
| 88 | + NetworkThread().start() |
| 89 | + aTestNode.wait_for_verack() |
| 90 | + |
| 91 | + #request mempool |
| 92 | + aTestNode.send_mempool() |
| 93 | + aTestNode.wait_for_disconnect() |
| 94 | + |
| 95 | + #mininode must be disconnected at this point |
| 96 | + assert_equal(len(self.nodes[0].getpeerinfo()), 0) |
| 97 | + |
| 98 | +if __name__ == '__main__': |
| 99 | + P2PMempoolTests().main() |
0 commit comments