Repository: dogecoin/dogecoin
File: qa/rpc-tests/p2p-acceptblock.py (Python)

Code before:
        test_node.sync_with_ping()
        self.nodes[0].getblock(block_1441f.hash)
        self.nodes[0].getblock(block_1442f.hash)

        test_node.send_message(msg_block(block_1443))

        # At this point we've sent an obviously-bogus block,
        # and we must get disconnected
        assert_equal(test_node.wait_for_disconnect(), True)
        print("Successfully got disconnected after sending an invalid block")

        # recreate our malicious node
        test_node = TestNode()   # connects to node (not whitelisted)
        connections[0] = NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], test_node)
        test_node.add_connection(connections[0])
        test_node.wait_for_verack()

        # We should have failed reorg and switched back to 1442 (but have block 1443)
        assert_equal(self.nodes[0].getblockcount(), 1442)
        assert_equal(self.nodes[0].getbestblockhash(), all_blocks[1438].hash)
        assert_equal(self.nodes[0].getblock(block_1443.hash)["confirmations"], -1)

        # Now send a new header on the invalid chain, indicating we're forked
        # off, and expect to get disconnected
        block_1445 = create_block(block_1442.sha256, create_coinbase(1445), block_1442.nTime+1)
        block_1445.solve()
        headers_message = msg_headers()
        headers_message.headers.append(CBlockHeader(block_1445))
        test_node.send_message(headers_message)
        assert_equal(test_node.wait_for_disconnect(), True)
        print("Successfully got disconnected after building on an invalid block")

        # 9. Connect node1 to node0 and ensure it is able to sync
        connect_nodes(self.nodes[0], 1)
        sync_blocks([self.nodes[0], self.nodes[1]])
        print("Successfully synced nodes 1 and 0")

        [ c.disconnect_node() for c in connections ]

if __name__ == '__main__':
    AcceptBlockTest().main()


Review comment (suggestion):
```suggestion
        block_1445 = create_block(block_1444.sha256, create_coinbase(1445), block_1444.nTime+1)
        block_1445.solve()
```

Code after:
        test_node.sync_with_ping()
        self.nodes[0].getblock(block_1441f.hash)
        self.nodes[0].getblock(block_1442f.hash)

        test_node.send_message(msg_block(block_1443))

        # At this point we've sent an obviously-bogus block,
        # and we must get disconnected
        assert_equal(test_node.wait_for_disconnect(), True)
        print("Successfully got disconnected after sending an invalid block")

        # recreate our malicious node
        test_node = TestNode()   # connects to node (not whitelisted)
        connections[0] = NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], test_node)
        test_node.add_connection(connections[0])
        test_node.wait_for_verack()

        # We should have failed reorg and switched back to 1442 (but have block 1443)
        assert_equal(self.nodes[0].getblockcount(), 1442)
        assert_equal(self.nodes[0].getbestblockhash(), all_blocks[1438].hash)
        assert_equal(self.nodes[0].getblock(block_1443.hash)["confirmations"], -1)

        # Now send a new header on the invalid chain, indicating we're forked
        # off, and expect to get disconnected
        block_1445 = create_block(block_1444.sha256, create_coinbase(1445), block_1444.nTime+1)
        block_1445.solve()
        headers_message = msg_headers()
        headers_message.headers.append(CBlockHeader(block_1445))
        test_node.send_message(headers_message)
        assert_equal(test_node.wait_for_disconnect(), True)
        print("Successfully got disconnected after building on an invalid block")

        # 9. Connect node1 to node0 and ensure it is able to sync
        connect_nodes(self.nodes[0], 1)
        sync_blocks([self.nodes[0], self.nodes[1]])
        print("Successfully synced nodes 1 and 0")

        [ c.disconnect_node() for c in connections ]

if __name__ == '__main__':
    AcceptBlockTest().main()