2016-10-18 21:35:27 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# Copyright (c) 2015 The Bitcoin Core developers
|
|
|
|
# Distributed under the MIT software license, see the accompanying
|
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2019-01-07 10:55:35 +01:00
|
|
|
"""Test the preciousblock RPC."""
|
2016-10-18 21:35:27 +02:00
|
|
|
|
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
2016-11-10 11:22:26 +01:00
|
|
|
from test_framework.util import (
|
|
|
|
assert_equal,
|
2019-09-18 20:41:14 +02:00
|
|
|
connect_nodes,
|
2018-03-13 17:45:06 +01:00
|
|
|
sync_blocks,
|
2016-11-10 11:22:26 +01:00
|
|
|
)
|
2016-10-18 21:35:27 +02:00
|
|
|
|
|
|
|
def unidirectional_node_sync_via_rpc(node_src, node_dest):
|
|
|
|
blocks_to_copy = []
|
|
|
|
blockhash = node_src.getbestblockhash()
|
|
|
|
while True:
|
|
|
|
try:
|
2021-08-27 21:03:02 +02:00
|
|
|
assert len(node_dest.getblock(blockhash, False)) > 0
|
2016-10-18 21:35:27 +02:00
|
|
|
break
|
|
|
|
except:
|
|
|
|
blocks_to_copy.append(blockhash)
|
|
|
|
blockhash = node_src.getblockheader(blockhash, True)['previousblockhash']
|
|
|
|
blocks_to_copy.reverse()
|
|
|
|
for blockhash in blocks_to_copy:
|
|
|
|
blockdata = node_src.getblock(blockhash, False)
|
2021-08-27 21:03:02 +02:00
|
|
|
assert node_dest.submitblock(blockdata) in (None, 'inconclusive')
|
2016-10-18 21:35:27 +02:00
|
|
|
|
|
|
|
def node_sync_via_rpc(nodes):
|
|
|
|
for node_src in nodes:
|
|
|
|
for node_dest in nodes:
|
|
|
|
if node_src is node_dest:
|
|
|
|
continue
|
|
|
|
unidirectional_node_sync_via_rpc(node_src, node_dest)
|
|
|
|
|
|
|
|
class PreciousTest(BitcoinTestFramework):
|
2017-09-01 18:47:13 +02:00
|
|
|
def set_test_params(self):
|
2016-11-10 11:22:26 +01:00
|
|
|
self.setup_clean_chain = True
|
|
|
|
self.num_nodes = 3
|
2016-10-18 21:35:27 +02:00
|
|
|
|
|
|
|
def setup_network(self):
|
2017-05-02 20:02:55 +02:00
|
|
|
self.setup_nodes()
|
2016-10-18 21:35:27 +02:00
|
|
|
|
|
|
|
def run_test(self):
|
2017-03-09 21:16:20 +01:00
|
|
|
self.log.info("Ensure submitblock can in principle reorg to a competing chain")
|
2018-09-17 20:00:41 +02:00
|
|
|
gen_address = lambda i: self.nodes[i].get_deterministic_priv_key().address # A non-wallet address to mine to
|
|
|
|
self.nodes[0].generatetoaddress(1, gen_address(0))
|
2016-11-10 11:22:26 +01:00
|
|
|
assert_equal(self.nodes[0].getblockcount(), 1)
|
2018-09-17 20:00:41 +02:00
|
|
|
hashZ = self.nodes[1].generatetoaddress(2, gen_address(1))[-1]
|
2016-11-10 11:22:26 +01:00
|
|
|
assert_equal(self.nodes[1].getblockcount(), 2)
|
2016-10-18 21:35:27 +02:00
|
|
|
node_sync_via_rpc(self.nodes[0:3])
|
2016-11-10 11:22:26 +01:00
|
|
|
assert_equal(self.nodes[0].getbestblockhash(), hashZ)
|
2016-10-18 21:35:27 +02:00
|
|
|
|
2017-03-09 21:16:20 +01:00
|
|
|
self.log.info("Mine blocks A-B-C on Node 0")
|
2018-09-17 20:00:41 +02:00
|
|
|
hashC = self.nodes[0].generatetoaddress(3, gen_address(0))[-1]
|
2016-11-10 11:22:26 +01:00
|
|
|
assert_equal(self.nodes[0].getblockcount(), 5)
|
2017-03-09 21:16:20 +01:00
|
|
|
self.log.info("Mine competing blocks E-F-G on Node 1")
|
2018-09-17 20:00:41 +02:00
|
|
|
hashG = self.nodes[1].generatetoaddress(3, gen_address(1))[-1]
|
2016-11-10 11:22:26 +01:00
|
|
|
assert_equal(self.nodes[1].getblockcount(), 5)
|
2021-08-27 21:03:02 +02:00
|
|
|
assert hashC != hashG
|
2017-03-09 21:16:20 +01:00
|
|
|
self.log.info("Connect nodes and check no reorg occurs")
|
2016-10-18 21:35:27 +02:00
|
|
|
# Submit competing blocks via RPC so any reorg should occur before we proceed (no way to wait on inaction for p2p sync)
|
|
|
|
node_sync_via_rpc(self.nodes[0:2])
|
2019-09-18 20:41:14 +02:00
|
|
|
connect_nodes(self.nodes[0], 1)
|
2016-11-10 11:22:26 +01:00
|
|
|
assert_equal(self.nodes[0].getbestblockhash(), hashC)
|
|
|
|
assert_equal(self.nodes[1].getbestblockhash(), hashG)
|
2017-03-09 21:16:20 +01:00
|
|
|
self.log.info("Make Node0 prefer block G")
|
2016-10-18 21:35:27 +02:00
|
|
|
self.nodes[0].preciousblock(hashG)
|
2016-11-10 11:22:26 +01:00
|
|
|
assert_equal(self.nodes[0].getbestblockhash(), hashG)
|
2017-03-09 21:16:20 +01:00
|
|
|
self.log.info("Make Node0 prefer block C again")
|
2016-10-18 21:35:27 +02:00
|
|
|
self.nodes[0].preciousblock(hashC)
|
2016-11-10 11:22:26 +01:00
|
|
|
assert_equal(self.nodes[0].getbestblockhash(), hashC)
|
2017-03-09 21:16:20 +01:00
|
|
|
self.log.info("Make Node1 prefer block C")
|
2016-10-18 21:35:27 +02:00
|
|
|
self.nodes[1].preciousblock(hashC)
|
2018-03-13 17:45:06 +01:00
|
|
|
sync_blocks(self.nodes[0:2]) # wait because node 1 may not have downloaded hashC
|
2016-11-10 11:22:26 +01:00
|
|
|
assert_equal(self.nodes[1].getbestblockhash(), hashC)
|
2017-03-09 21:16:20 +01:00
|
|
|
self.log.info("Make Node1 prefer block G again")
|
2016-10-18 21:35:27 +02:00
|
|
|
self.nodes[1].preciousblock(hashG)
|
2016-11-10 11:22:26 +01:00
|
|
|
assert_equal(self.nodes[1].getbestblockhash(), hashG)
|
2017-03-09 21:16:20 +01:00
|
|
|
self.log.info("Make Node0 prefer block G again")
|
2016-10-18 21:35:27 +02:00
|
|
|
self.nodes[0].preciousblock(hashG)
|
2016-11-10 11:22:26 +01:00
|
|
|
assert_equal(self.nodes[0].getbestblockhash(), hashG)
|
2017-03-09 21:16:20 +01:00
|
|
|
self.log.info("Make Node1 prefer block C again")
|
2016-10-18 21:35:27 +02:00
|
|
|
self.nodes[1].preciousblock(hashC)
|
2016-11-10 11:22:26 +01:00
|
|
|
assert_equal(self.nodes[1].getbestblockhash(), hashC)
|
2017-03-09 21:16:20 +01:00
|
|
|
self.log.info("Mine another block (E-F-G-)H on Node 0 and reorg Node 1")
|
2018-09-17 20:00:41 +02:00
|
|
|
self.nodes[0].generatetoaddress(1, gen_address(0))
|
2016-11-10 11:22:26 +01:00
|
|
|
assert_equal(self.nodes[0].getblockcount(), 6)
|
2020-04-14 12:00:16 +02:00
|
|
|
self.sync_blocks(self.nodes[0:2])
|
2016-10-18 21:35:27 +02:00
|
|
|
hashH = self.nodes[0].getbestblockhash()
|
2016-11-10 11:22:26 +01:00
|
|
|
assert_equal(self.nodes[1].getbestblockhash(), hashH)
|
2017-03-09 21:16:20 +01:00
|
|
|
self.log.info("Node1 should not be able to prefer block C anymore")
|
2016-10-18 21:35:27 +02:00
|
|
|
self.nodes[1].preciousblock(hashC)
|
2016-11-10 11:22:26 +01:00
|
|
|
assert_equal(self.nodes[1].getbestblockhash(), hashH)
|
2017-03-09 21:16:20 +01:00
|
|
|
self.log.info("Mine competing blocks I-J-K-L on Node 2")
|
2018-09-17 20:00:41 +02:00
|
|
|
self.nodes[2].generatetoaddress(4, gen_address(2))
|
2016-11-10 11:22:26 +01:00
|
|
|
assert_equal(self.nodes[2].getblockcount(), 6)
|
2016-10-18 21:35:27 +02:00
|
|
|
hashL = self.nodes[2].getbestblockhash()
|
2017-03-09 21:16:20 +01:00
|
|
|
self.log.info("Connect nodes and check no reorg occurs")
|
2016-12-19 09:47:08 +01:00
|
|
|
node_sync_via_rpc(self.nodes[1:3])
|
2019-09-18 20:41:14 +02:00
|
|
|
connect_nodes(self.nodes[1], 2)
|
|
|
|
connect_nodes(self.nodes[0], 2)
|
2016-11-10 11:22:26 +01:00
|
|
|
assert_equal(self.nodes[0].getbestblockhash(), hashH)
|
|
|
|
assert_equal(self.nodes[1].getbestblockhash(), hashH)
|
|
|
|
assert_equal(self.nodes[2].getbestblockhash(), hashL)
|
2017-03-09 21:16:20 +01:00
|
|
|
self.log.info("Make Node1 prefer block L")
|
2016-10-18 21:35:27 +02:00
|
|
|
self.nodes[1].preciousblock(hashL)
|
2016-11-10 11:22:26 +01:00
|
|
|
assert_equal(self.nodes[1].getbestblockhash(), hashL)
|
2017-03-09 21:16:20 +01:00
|
|
|
self.log.info("Make Node2 prefer block H")
|
2016-10-18 21:35:27 +02:00
|
|
|
self.nodes[2].preciousblock(hashH)
|
2016-11-10 11:22:26 +01:00
|
|
|
assert_equal(self.nodes[2].getbestblockhash(), hashH)
|
2016-10-18 21:35:27 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
PreciousTest().main()
|