|
| 1 | +#!/usr/bin/env python2 |
| 2 | +# |
| 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 | +from test_framework.blocktools import create_block, create_coinbase |
| 12 | + |
| 13 | +''' |
| 14 | +AcceptBlockTest -- test processing of unrequested blocks. |
| 15 | +
|
| 16 | +Since behavior differs when receiving unrequested blocks from whitelisted peers |
| 17 | +versus non-whitelisted peers, this tests the behavior of both (effectively two |
| 18 | +separate tests running in parallel). |
| 19 | +
|
| 20 | +Setup: two nodes, node0 and node1, not connected to each other. Node0 does not |
| 21 | +whitelist localhost, but node1 does. They will each be on their own chain for |
| 22 | +this test. |
| 23 | +
|
| 24 | +We have one NodeConn connection to each, test_node and white_node respectively. |
| 25 | +
|
| 26 | +The test: |
| 27 | +1. Generate one block on each node, to leave IBD. |
| 28 | +
|
| 29 | +2. Mine a new block on each tip, and deliver to each node from node's peer. |
| 30 | + The tip should advance. |
| 31 | +
|
| 32 | +3. Mine a block that forks the previous block, and deliver to each node from |
| 33 | + corresponding peer. |
| 34 | + Node0 should not process this block (just accept the header), because it is |
| 35 | + unrequested and doesn't have more work than the tip. |
| 36 | + Node1 should process because this is coming from a whitelisted peer. |
| 37 | +
|
| 38 | +4. Send another block that builds on the forking block. |
| 39 | + Node0 should process this block but be stuck on the shorter chain, because |
| 40 | + it's missing an intermediate block. |
| 41 | + Node1 should reorg to this longer chain. |
| 42 | +
|
| 43 | +5. Send a duplicate of the block in #3 to Node0. |
| 44 | + Node0 should not process the block because it is unrequested, and stay on |
| 45 | + the shorter chain. |
| 46 | +
|
| 47 | +6. Send Node0 an inv for the height 3 block produced in #4 above. |
| 48 | + Node0 should figure out that Node0 has the missing height 2 block and send a |
| 49 | + getdata. |
| 50 | +
|
| 51 | +7. Send Node0 the missing block again. |
| 52 | + Node0 should process and the tip should advance. |
| 53 | +''' |
| 54 | + |
| 55 | +# TestNode: bare-bones "peer". Used mostly as a conduit for a test to sending |
| 56 | +# p2p messages to a node, generating the messages in the main testing logic. |
| 57 | +class TestNode(NodeConnCB): |
| 58 | + def __init__(self): |
| 59 | + NodeConnCB.__init__(self) |
| 60 | + self.create_callback_map() |
| 61 | + self.connection = None |
| 62 | + |
| 63 | + def add_connection(self, conn): |
| 64 | + self.connection = conn |
| 65 | + |
| 66 | + # Track the last getdata message we receive (used in the test) |
| 67 | + def on_getdata(self, conn, message): |
| 68 | + self.last_getdata = message |
| 69 | + |
| 70 | + # Spin until verack message is received from the node. |
| 71 | + # We use this to signal that our test can begin. This |
| 72 | + # is called from the testing thread, so it needs to acquire |
| 73 | + # the global lock. |
| 74 | + def wait_for_verack(self): |
| 75 | + while True: |
| 76 | + with mininode_lock: |
| 77 | + if self.verack_received: |
| 78 | + return |
| 79 | + time.sleep(0.05) |
| 80 | + |
| 81 | + # Wrapper for the NodeConn's send_message function |
| 82 | + def send_message(self, message): |
| 83 | + self.connection.send_message(message) |
| 84 | + |
| 85 | +class AcceptBlockTest(BitcoinTestFramework): |
| 86 | + def add_options(self, parser): |
| 87 | + parser.add_option("--testbinary", dest="testbinary", |
| 88 | + default=os.getenv("BITCOIND", "bitcoind"), |
| 89 | + help="bitcoind binary to test") |
| 90 | + |
| 91 | + def setup_chain(self): |
| 92 | + initialize_chain_clean(self.options.tmpdir, 2) |
| 93 | + |
| 94 | + def setup_network(self): |
| 95 | + # Node0 will be used to test behavior of processing unrequested blocks |
| 96 | + # from peers which are not whitelisted, while Node1 will be used for |
| 97 | + # the whitelisted case. |
| 98 | + self.nodes = [] |
| 99 | + self.nodes.append(start_node(0, self.options.tmpdir, ["-debug"], |
| 100 | + binary=self.options.testbinary)) |
| 101 | + self.nodes.append(start_node(1, self.options.tmpdir, |
| 102 | + ["-debug", "-whitelist=127.0.0.1"], |
| 103 | + binary=self.options.testbinary)) |
| 104 | + |
| 105 | + def run_test(self): |
| 106 | + # Setup the p2p connections and start up the network thread. |
| 107 | + test_node = TestNode() # connects to node0 (not whitelisted) |
| 108 | + white_node = TestNode() # connects to node1 (whitelisted) |
| 109 | + |
| 110 | + connections = [] |
| 111 | + connections.append(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], test_node)) |
| 112 | + connections.append(NodeConn('127.0.0.1', p2p_port(1), self.nodes[1], white_node)) |
| 113 | + test_node.add_connection(connections[0]) |
| 114 | + white_node.add_connection(connections[1]) |
| 115 | + |
| 116 | + NetworkThread().start() # Start up network handling in another thread |
| 117 | + |
| 118 | + # Test logic begins here |
| 119 | + test_node.wait_for_verack() |
| 120 | + white_node.wait_for_verack() |
| 121 | + |
| 122 | + # 1. Have both nodes mine a block (leave IBD) |
| 123 | + [ n.generate(1) for n in self.nodes ] |
| 124 | + tips = [ int ("0x" + n.getbestblockhash() + "L", 0) for n in self.nodes ] |
| 125 | + |
| 126 | + # 2. Send one block that builds on each tip. |
| 127 | + # This should be accepted. |
| 128 | + blocks_h2 = [] # the height 2 blocks on each node's chain |
| 129 | + for i in xrange(2): |
| 130 | + blocks_h2.append(create_block(tips[i], create_coinbase(), time.time()+1)) |
| 131 | + blocks_h2[i].solve() |
| 132 | + test_node.send_message(msg_block(blocks_h2[0])) |
| 133 | + white_node.send_message(msg_block(blocks_h2[1])) |
| 134 | + |
| 135 | + time.sleep(1) |
| 136 | + assert_equal(self.nodes[0].getblockcount(), 2) |
| 137 | + assert_equal(self.nodes[1].getblockcount(), 2) |
| 138 | + print "First height 2 block accepted by both nodes" |
| 139 | + |
| 140 | + # 3. Send another block that builds on the original tip. |
| 141 | + blocks_h2f = [] # Blocks at height 2 that fork off the main chain |
| 142 | + for i in xrange(2): |
| 143 | + blocks_h2f.append(create_block(tips[i], create_coinbase(), blocks_h2[i].nTime+1)) |
| 144 | + blocks_h2f[i].solve() |
| 145 | + test_node.send_message(msg_block(blocks_h2f[0])) |
| 146 | + white_node.send_message(msg_block(blocks_h2f[1])) |
| 147 | + |
| 148 | + time.sleep(1) # Give time to process the block |
| 149 | + for x in self.nodes[0].getchaintips(): |
| 150 | + if x['hash'] == blocks_h2f[0].hash: |
| 151 | + assert_equal(x['status'], "headers-only") |
| 152 | + |
| 153 | + for x in self.nodes[1].getchaintips(): |
| 154 | + if x['hash'] == blocks_h2f[1].hash: |
| 155 | + assert_equal(x['status'], "valid-headers") |
| 156 | + |
| 157 | + print "Second height 2 block accepted only from whitelisted peer" |
| 158 | + |
| 159 | + # 4. Now send another block that builds on the forking chain. |
| 160 | + blocks_h3 = [] |
| 161 | + for i in xrange(2): |
| 162 | + blocks_h3.append(create_block(blocks_h2f[i].sha256, create_coinbase(), blocks_h2f[i].nTime+1)) |
| 163 | + blocks_h3[i].solve() |
| 164 | + test_node.send_message(msg_block(blocks_h3[0])) |
| 165 | + white_node.send_message(msg_block(blocks_h3[1])) |
| 166 | + |
| 167 | + time.sleep(1) |
| 168 | + # Since the earlier block was not processed by node0, the new block |
| 169 | + # can't be fully validated. |
| 170 | + for x in self.nodes[0].getchaintips(): |
| 171 | + if x['hash'] == blocks_h3[0].hash: |
| 172 | + assert_equal(x['status'], "headers-only") |
| 173 | + |
| 174 | + # But this block should be accepted by node0 since it has more work. |
| 175 | + try: |
| 176 | + self.nodes[0].getblock(blocks_h3[0].hash) |
| 177 | + print "Unrequested more-work block accepted from non-whitelisted peer" |
| 178 | + except: |
| 179 | + raise AssertionError("Unrequested more work block was not processed") |
| 180 | + |
| 181 | + # Node1 should have accepted and reorged. |
| 182 | + assert_equal(self.nodes[1].getblockcount(), 3) |
| 183 | + print "Successfully reorged to length 3 chain from whitelisted peer" |
| 184 | + |
| 185 | + # 5. Test handling of unrequested block on the node that didn't process |
| 186 | + # Should still not be processed (even though it has a child that has more |
| 187 | + # work). |
| 188 | + test_node.send_message(msg_block(blocks_h2f[0])) |
| 189 | + |
| 190 | + # Here, if the sleep is too short, the test could falsely succeed (if the |
| 191 | + # node hasn't processed the block by the time the sleep returns, and then |
| 192 | + # the node processes it and incorrectly advances the tip). |
| 193 | + # But this would be caught later on, when we verify that an inv triggers |
| 194 | + # a getdata request for this block. |
| 195 | + time.sleep(1) |
| 196 | + assert_equal(self.nodes[0].getblockcount(), 2) |
| 197 | + print "Unrequested block that would complete more-work chain was ignored" |
| 198 | + |
| 199 | + # 6. Try to get node to request the missing block. |
| 200 | + # Poke the node with an inv for block at height 3 and see if that |
| 201 | + # triggers a getdata on block 2 (it should if block 2 is missing). |
| 202 | + with mininode_lock: |
| 203 | + # Clear state so we can check the getdata request |
| 204 | + test_node.last_getdata = None |
| 205 | + test_node.send_message(msg_inv([CInv(2, blocks_h3[0].sha256)])) |
| 206 | + |
| 207 | + time.sleep(1) |
| 208 | + with mininode_lock: |
| 209 | + getdata = test_node.last_getdata |
| 210 | + |
| 211 | + # Check that the getdata is for the right block |
| 212 | + assert_equal(len(getdata.inv), 1) |
| 213 | + assert_equal(getdata.inv[0].hash, blocks_h2f[0].sha256) |
| 214 | + print "Inv at tip triggered getdata for unprocessed block" |
| 215 | + |
| 216 | + # 7. Send the missing block for the third time (now it is requested) |
| 217 | + test_node.send_message(msg_block(blocks_h2f[0])) |
| 218 | + |
| 219 | + time.sleep(1) |
| 220 | + assert_equal(self.nodes[0].getblockcount(), 3) |
| 221 | + print "Successfully reorged to length 3 chain from non-whitelisted peer" |
| 222 | + |
| 223 | + [ c.disconnect_node() for c in connections ] |
| 224 | + |
| 225 | +if __name__ == '__main__': |
| 226 | + AcceptBlockTest().main() |
0 commit comments