Changeset View
Changeset View
Standalone View
Standalone View
test/functional/abc-sync-chain.py
Show All 27 Lines | class BaseNode(P2PInterface): | ||||
def send_block(self, block): | def send_block(self, block): | ||||
self.send_message(msg_block(block)) | self.send_message(msg_block(block)) | ||||
class SyncChainTest(BitcoinTestFramework): | class SyncChainTest(BitcoinTestFramework): | ||||
def set_test_params(self): | def set_test_params(self): | ||||
self.num_nodes = 1 | self.num_nodes = 1 | ||||
# Setting minimumchainwork makes sure we test IBD as well as post-IBD | # Setting minimumchainwork makes sure we test IBD as well as post-IBD | ||||
self.extra_args = [ | self.extra_args = [[f"-minimumchainwork={202 + 2 * NUM_IBD_BLOCKS:#x}"]] | ||||
[f"-minimumchainwork={202 + 2 * NUM_IBD_BLOCKS:#x}"]] | |||||
def run_test(self): | def run_test(self): | ||||
node0 = self.nodes[0] | node0 = self.nodes[0] | ||||
node0conn = node0.add_p2p_connection(BaseNode()) | node0conn = node0.add_p2p_connection(BaseNode()) | ||||
tip = int(node0.getbestblockhash(), 16) | tip = int(node0.getbestblockhash(), 16) | ||||
height = node0.getblockcount() + 1 | height = node0.getblockcount() + 1 | ||||
time = node0.getblock(node0.getbestblockhash())['time'] + 1 | time = node0.getblock(node0.getbestblockhash())["time"] + 1 | ||||
blocks = [] | blocks = [] | ||||
for _ in range(NUM_IBD_BLOCKS * 2): | for _ in range(NUM_IBD_BLOCKS * 2): | ||||
block = create_block(tip, create_coinbase(height), time) | block = create_block(tip, create_coinbase(height), time) | ||||
block.solve() | block.solve() | ||||
blocks.append(block) | blocks.append(block) | ||||
tip = block.sha256 | tip = block.sha256 | ||||
height += 1 | height += 1 | ||||
time += 1 | time += 1 | ||||
# Headers need to be sent in-order | # Headers need to be sent in-order | ||||
for b in blocks: | for b in blocks: | ||||
node0conn.send_header(b) | node0conn.send_header(b) | ||||
# Send blocks in some random order | # Send blocks in some random order | ||||
for b in random.sample(blocks, len(blocks)): | for b in random.sample(blocks, len(blocks)): | ||||
node0conn.send_block(b) | node0conn.send_block(b) | ||||
# The node should eventually, completely sync without getting stuck | # The node should eventually, completely sync without getting stuck | ||||
def node_synced(): | def node_synced(): | ||||
return node0.getbestblockhash() == blocks[-1].hash | return node0.getbestblockhash() == blocks[-1].hash | ||||
self.wait_until(node_synced) | self.wait_until(node_synced) | ||||
if __name__ == '__main__': | if __name__ == "__main__": | ||||
SyncChainTest().main() | SyncChainTest().main() |