2016-08-26 23:05:26 +02:00
|
|
|
#!/usr/bin/env python3
|
2019-02-21 02:03:13 +01:00
|
|
|
# Copyright (c) 2015-2019 The Bitcoin Core developers
|
2016-08-26 23:05:26 +02:00
|
|
|
# Distributed under the MIT software license, see the accompanying
|
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2017-01-18 00:34:40 +01:00
|
|
|
"""Test the preciousblock RPC."""
|
2016-08-26 23:05:26 +02:00
|
|
|
|
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
2016-11-07 19:24:32 +01:00
|
|
|
from test_framework.util import (
|
|
|
|
assert_equal,
|
2019-08-27 21:13:56 +02:00
|
|
|
connect_nodes,
|
2016-11-07 19:24:32 +01:00
|
|
|
)
|
2016-08-26 23:05:26 +02:00
|
|
|
|
|
|
|
def unidirectional_node_sync_via_rpc(node_src, node_dest):
|
|
|
|
blocks_to_copy = []
|
|
|
|
blockhash = node_src.getbestblockhash()
|
|
|
|
while True:
|
|
|
|
try:
|
2019-02-19 23:43:44 +01:00
|
|
|
assert len(node_dest.getblock(blockhash, False)) > 0
|
2016-08-26 23:05:26 +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)
|
2019-02-19 23:43:44 +01:00
|
|
|
assert node_dest.submitblock(blockdata) in (None, 'inconclusive')
|
2016-08-26 23:05:26 +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-06-10 00:21:21 +02:00
|
|
|
def set_test_params(self):
|
2016-11-07 19:24:32 +01:00
|
|
|
self.setup_clean_chain = True
|
|
|
|
self.num_nodes = 3
|
2016-08-26 23:05:26 +02:00
|
|
|
|
|
|
|
def setup_network(self):
|
2017-04-03 15:34:04 +02:00
|
|
|
self.setup_nodes()
|
2016-08-26 23:05:26 +02:00
|
|
|
|
|
|
|
def run_test(self):
|
2017-03-08 00:46:17 +01:00
|
|
|
self.log.info("Ensure submitblock can in principle reorg to a competing chain")
|
2018-09-15 11:32:12 +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-07 19:24:32 +01:00
|
|
|
assert_equal(self.nodes[0].getblockcount(), 1)
|
2018-09-15 11:32:12 +02:00
|
|
|
hashZ = self.nodes[1].generatetoaddress(2, gen_address(1))[-1]
|
2016-11-07 19:24:32 +01:00
|
|
|
assert_equal(self.nodes[1].getblockcount(), 2)
|
2016-08-26 23:05:26 +02:00
|
|
|
node_sync_via_rpc(self.nodes[0:3])
|
2016-11-07 19:24:32 +01:00
|
|
|
assert_equal(self.nodes[0].getbestblockhash(), hashZ)
|
2016-08-26 23:05:26 +02:00
|
|
|
|
2017-03-08 00:46:17 +01:00
|
|
|
self.log.info("Mine blocks A-B-C on Node 0")
|
2018-09-15 11:32:12 +02:00
|
|
|
hashC = self.nodes[0].generatetoaddress(3, gen_address(0))[-1]
|
2016-11-07 19:24:32 +01:00
|
|
|
assert_equal(self.nodes[0].getblockcount(), 5)
|
2017-03-08 00:46:17 +01:00
|
|
|
self.log.info("Mine competing blocks E-F-G on Node 1")
|
2018-09-15 11:32:12 +02:00
|
|
|
hashG = self.nodes[1].generatetoaddress(3, gen_address(1))[-1]
|
2016-11-07 19:24:32 +01:00
|
|
|
assert_equal(self.nodes[1].getblockcount(), 5)
|
2019-02-19 23:43:44 +01:00
|
|
|
assert hashC != hashG
|
2017-03-08 00:46:17 +01:00
|
|
|
self.log.info("Connect nodes and check no reorg occurs")
|
2016-08-26 23:05:26 +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-08-27 21:13:56 +02:00
|
|
|
connect_nodes(self.nodes[0], 1)
|
2016-11-07 19:24:32 +01:00
|
|
|
assert_equal(self.nodes[0].getbestblockhash(), hashC)
|
|
|
|
assert_equal(self.nodes[1].getbestblockhash(), hashG)
|
2017-03-08 00:46:17 +01:00
|
|
|
self.log.info("Make Node0 prefer block G")
|
2016-08-26 23:05:26 +02:00
|
|
|
self.nodes[0].preciousblock(hashG)
|
2016-11-07 19:24:32 +01:00
|
|
|
assert_equal(self.nodes[0].getbestblockhash(), hashG)
|
2017-03-08 00:46:17 +01:00
|
|
|
self.log.info("Make Node0 prefer block C again")
|
2016-08-26 23:05:26 +02:00
|
|
|
self.nodes[0].preciousblock(hashC)
|
2016-11-07 19:24:32 +01:00
|
|
|
assert_equal(self.nodes[0].getbestblockhash(), hashC)
|
2017-03-08 00:46:17 +01:00
|
|
|
self.log.info("Make Node1 prefer block C")
|
2016-08-26 23:05:26 +02:00
|
|
|
self.nodes[1].preciousblock(hashC)
|
2019-04-07 00:19:45 +02:00
|
|
|
self.sync_blocks(self.nodes[0:2]) # wait because node 1 may not have downloaded hashC
|
2016-11-07 19:24:32 +01:00
|
|
|
assert_equal(self.nodes[1].getbestblockhash(), hashC)
|
2017-03-08 00:46:17 +01:00
|
|
|
self.log.info("Make Node1 prefer block G again")
|
2016-08-26 23:05:26 +02:00
|
|
|
self.nodes[1].preciousblock(hashG)
|
2016-11-07 19:24:32 +01:00
|
|
|
assert_equal(self.nodes[1].getbestblockhash(), hashG)
|
2017-03-08 00:46:17 +01:00
|
|
|
self.log.info("Make Node0 prefer block G again")
|
2016-08-26 23:05:26 +02:00
|
|
|
self.nodes[0].preciousblock(hashG)
|
2016-11-07 19:24:32 +01:00
|
|
|
assert_equal(self.nodes[0].getbestblockhash(), hashG)
|
2017-03-08 00:46:17 +01:00
|
|
|
self.log.info("Make Node1 prefer block C again")
|
2016-08-26 23:05:26 +02:00
|
|
|
self.nodes[1].preciousblock(hashC)
|
2016-11-07 19:24:32 +01:00
|
|
|
assert_equal(self.nodes[1].getbestblockhash(), hashC)
|
2017-03-08 00:46:17 +01:00
|
|
|
self.log.info("Mine another block (E-F-G-)H on Node 0 and reorg Node 1")
|
2018-09-15 11:32:12 +02:00
|
|
|
self.nodes[0].generatetoaddress(1, gen_address(0))
|
2016-11-07 19:24:32 +01:00
|
|
|
assert_equal(self.nodes[0].getblockcount(), 6)
|
2019-04-07 00:19:45 +02:00
|
|
|
self.sync_blocks(self.nodes[0:2])
|
2016-08-26 23:05:26 +02:00
|
|
|
hashH = self.nodes[0].getbestblockhash()
|
2016-11-07 19:24:32 +01:00
|
|
|
assert_equal(self.nodes[1].getbestblockhash(), hashH)
|
2017-03-08 00:46:17 +01:00
|
|
|
self.log.info("Node1 should not be able to prefer block C anymore")
|
2016-08-26 23:05:26 +02:00
|
|
|
self.nodes[1].preciousblock(hashC)
|
2016-11-07 19:24:32 +01:00
|
|
|
assert_equal(self.nodes[1].getbestblockhash(), hashH)
|
2017-03-08 00:46:17 +01:00
|
|
|
self.log.info("Mine competing blocks I-J-K-L on Node 2")
|
2018-09-15 11:32:12 +02:00
|
|
|
self.nodes[2].generatetoaddress(4, gen_address(2))
|
2016-11-07 19:24:32 +01:00
|
|
|
assert_equal(self.nodes[2].getblockcount(), 6)
|
2016-08-26 23:05:26 +02:00
|
|
|
hashL = self.nodes[2].getbestblockhash()
|
2017-03-08 00:46:17 +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-08-27 21:13:56 +02:00
|
|
|
connect_nodes(self.nodes[1], 2)
|
|
|
|
connect_nodes(self.nodes[0], 2)
|
2016-11-07 19:24:32 +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-08 00:46:17 +01:00
|
|
|
self.log.info("Make Node1 prefer block L")
|
2016-08-26 23:05:26 +02:00
|
|
|
self.nodes[1].preciousblock(hashL)
|
2016-11-07 19:24:32 +01:00
|
|
|
assert_equal(self.nodes[1].getbestblockhash(), hashL)
|
2017-03-08 00:46:17 +01:00
|
|
|
self.log.info("Make Node2 prefer block H")
|
2016-08-26 23:05:26 +02:00
|
|
|
self.nodes[2].preciousblock(hashH)
|
2016-11-07 19:24:32 +01:00
|
|
|
assert_equal(self.nodes[2].getbestblockhash(), hashH)
|
2016-08-26 23:05:26 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
PreciousTest().main()
|