2016-08-26 23:05:26 +02:00
|
|
|
#!/usr/bin/env python3
|
2021-07-28 13:57:16 +02:00
|
|
|
# Copyright (c) 2015-2021 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,
|
|
|
|
)
|
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
|
2022-08-18 20:23:15 +02:00
|
|
|
except Exception:
|
2016-08-26 23:05:26 +02:00
|
|
|
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
|
2019-12-06 15:37:49 +01:00
|
|
|
self.supports_cli = False
|
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")
|
2020-11-10 18:02:31 +01:00
|
|
|
self.generate(self.nodes[0], 1, sync_fun=self.no_op)
|
2016-11-07 19:24:32 +01:00
|
|
|
assert_equal(self.nodes[0].getblockcount(), 1)
|
2020-11-10 18:02:31 +01:00
|
|
|
hashZ = self.generate(self.nodes[1], 2, sync_fun=self.no_op)[-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")
|
2020-11-10 18:02:31 +01:00
|
|
|
hashC = self.generate(self.nodes[0], 3, sync_fun=self.no_op)[-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")
|
2020-11-10 18:02:31 +01:00
|
|
|
hashG = self.generate(self.nodes[1], 3, sync_fun=self.no_op)[-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])
|
2020-09-17 09:46:07 +02:00
|
|
|
self.connect_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")
|
2020-11-10 18:02:31 +01:00
|
|
|
self.generate(self.nodes[0], 1, sync_fun=self.no_op)
|
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")
|
2020-11-10 18:02:31 +01:00
|
|
|
self.generate(self.nodes[2], 4, sync_fun=self.no_op)
|
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])
|
2020-09-17 09:46:07 +02:00
|
|
|
self.connect_nodes(1, 2)
|
|
|
|
self.connect_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__':
|
2024-07-16 23:05:14 +02:00
|
|
|
PreciousTest(__file__).main()
|