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

Code before:
        assert_equal(self.nodes[0].getblockcount(), 1442)
        self.nodes[0].getblock(all_blocks[1438].hash)
        assert_equal(self.nodes[0].getbestblockhash(), all_blocks[1438].hash)
        assert_raises_jsonrpc(-1, "Block not found on disk", self.nodes[0].getblock, all_blocks[1439].hash)
        print("Successfully reorged to longer chain from non-whitelisted peer")

        # 8. Create a chain which is invalid at a height longer than the
        # current chain, but which has more blocks on top of that
        block_1441f = create_block(all_blocks[1436].sha256, create_coinbase(1441), all_blocks[1436].nTime+1)
        block_1441f.solve()
        block_1442f = create_block(block_1441f.sha256, create_coinbase(1440), block_1441f.nTime+1)
        block_1442f.solve()
        block_1443 = create_block(block_1442f.sha256, create_coinbase(1441), block_1442f.nTime+1)
        # block_1443 spends a coinbase below maturity!
        block_1443.vtx.append(create_transaction(block_1442f.vtx[0], 0, b"42", 1))
        block_1443.hashMerkleRoot = block_1443.calc_merkle_root()
        block_1443.solve()
        block_1442 = create_block(block_1443.sha256, create_coinbase(1444), block_1443.nTime+1)
        block_1442.solve()

        # Now send all the headers on the chain and enough blocks to trigger reorg
        headers_message = msg_headers()
        headers_message.headers.append(CBlockHeader(block_1441f))
        headers_message.headers.append(CBlockHeader(block_1442f))
        headers_message.headers.append(CBlockHeader(block_1443))
        headers_message.headers.append(CBlockHeader(block_1442))
        test_node.send_message(headers_message)

        test_node.sync_with_ping()
        tip_entry_found = False
        for x in self.nodes[0].getchaintips():
            if x['hash'] == block_1442.hash:
                assert_equal(x['status'], "headers-only")
                tip_entry_found = True
        assert(tip_entry_found)
        assert_raises_jsonrpc(-1, "Block not found on disk", self.nodes[0].getblock, block_1442.hash)

        test_node.send_message(msg_block(block_1441f))
        test_node.send_message(msg_block(block_1442f))

        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")


Review comment (suggestion):
```suggestion
        headers_message.headers.append(CBlockHeader(block_1444))
```

Code after:
        assert_equal(self.nodes[0].getblockcount(), 1442)
        self.nodes[0].getblock(all_blocks[1438].hash)
        assert_equal(self.nodes[0].getbestblockhash(), all_blocks[1438].hash)
        assert_raises_jsonrpc(-1, "Block not found on disk", self.nodes[0].getblock, all_blocks[1439].hash)
        print("Successfully reorged to longer chain from non-whitelisted peer")

        # 8. Create a chain which is invalid at a height longer than the
        # current chain, but which has more blocks on top of that
        block_1441f = create_block(all_blocks[1436].sha256, create_coinbase(1441), all_blocks[1436].nTime+1)
        block_1441f.solve()
        block_1442f = create_block(block_1441f.sha256, create_coinbase(1440), block_1441f.nTime+1)
        block_1442f.solve()
        block_1443 = create_block(block_1442f.sha256, create_coinbase(1441), block_1442f.nTime+1)
        # block_1443 spends a coinbase below maturity!
        block_1443.vtx.append(create_transaction(block_1442f.vtx[0], 0, b"42", 1))
        block_1443.hashMerkleRoot = block_1443.calc_merkle_root()
        block_1443.solve()
        block_1444 = create_block(block_1443.sha256, create_coinbase(1444), block_1443.nTime+1)
        block_1444.solve()

        # Now send all the headers on the chain and enough blocks to trigger reorg
        headers_message = msg_headers()
        headers_message.headers.append(CBlockHeader(block_1441f))
        headers_message.headers.append(CBlockHeader(block_1442f))
        headers_message.headers.append(CBlockHeader(block_1443))
        headers_message.headers.append(CBlockHeader(block_1444))
        test_node.send_message(headers_message)

        test_node.sync_with_ping()
        tip_entry_found = False
        for x in self.nodes[0].getchaintips():
            if x['hash'] == block_1444.hash:
                assert_equal(x['status'], "headers-only")
                tip_entry_found = True
        assert(tip_entry_found)
        assert_raises_jsonrpc(-1, "Block not found on disk", self.nodes[0].getblock, block_1444.hash)

        test_node.send_message(msg_block(block_1441f))
        test_node.send_message(msg_block(block_1442f))

        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")