Changeset View
Changeset View
Standalone View
Standalone View
test/functional/utxocommit.py
| Show All 15 Lines | from test_framework.mininode import (CBlockHeader, | ||||
| msg_block, | msg_block, | ||||
| msg_headers) | msg_headers) | ||||
| from test_framework.test_framework import BitcoinTestFramework | from test_framework.test_framework import BitcoinTestFramework | ||||
| from test_framework.util import * | from test_framework.util import * | ||||
| from test_framework.blocktools import * | from test_framework.blocktools import * | ||||
| NO_COMMITMENT = '0000000000000000000000000000000000000000000000000000000000000000' | NO_COMMITMENT = '0000000000000000000000000000000000000000000000000000000000000000' | ||||
| # UTXO version 0 magic | |||||
| PREFIX = b"\x6a\x24UTX0" | |||||
| class UtxoCommitTest(BitcoinTestFramework): | class UtxoCommitTest(BitcoinTestFramework): | ||||
| def set_test_params(self): | def set_test_params(self): | ||||
| self.setup_clean_chain = True | self.setup_clean_chain = True | ||||
| self.num_nodes = 5 | self.num_nodes = 5 | ||||
| "Setup network starts one node and one node_conn to it" | "Setup network starts one node and one node_conn to it" | ||||
| ▲ Show 20 Lines • Show All 99 Lines • ▼ Show 20 Lines | def send_blocks(self, blocks): | ||||
| self.node_conn.sync_with_ping(20) | self.node_conn.sync_with_ping(20) | ||||
| # Check commitment | # Check commitment | ||||
| txoutsetinfo = self.node.gettxoutsetinfo() | txoutsetinfo = self.node.gettxoutsetinfo() | ||||
| self.current_commitment = txoutsetinfo['commitment_calculated'] | self.current_commitment = txoutsetinfo['commitment_calculated'] | ||||
| self.is_maintaining = txoutsetinfo['commitment'] != NO_COMMITMENT | self.is_maintaining = txoutsetinfo['commitment'] != NO_COMMITMENT | ||||
| if self.is_maintaining: | if self.is_maintaining: | ||||
| assert_equal(self.current_commitment, txoutsetinfo['commitment']) | assert_equal(self.current_commitment, txoutsetinfo['commitment']) | ||||
| def check_coinbase(self): | |||||
| bhash = self.node.generate(1) | |||||
| block = FromHex(CBlock(), self.node.getblock(bhash[0], False)) | |||||
| expected = hex_str_to_bytes(self.current_commitment) | |||||
| assert_equal(block.get_coinbase_commitment(PREFIX), expected) | |||||
| def run_test(self): | def run_test(self): | ||||
| self.setup_blocks() | self.setup_blocks() | ||||
| self.log.info("Node 1: Add blocks and record commitment") | self.log.info("Node 1: Add blocks and record commitment") | ||||
| self.send_headers(self.blocks[1:201]) | self.send_headers(self.blocks[1:201]) | ||||
| self.commitments = [0] | self.commitments = [0] | ||||
| for b in self.blocks[1:201]: | for b in self.blocks[1:201]: | ||||
| Show All 10 Lines | def run_test(self): | ||||
| # ** Node 2 | # ** Node 2 | ||||
| self.log.info( | self.log.info( | ||||
| "Node 2: assumevalid=150, send some blocks, send all headers, send remaining blocks") | "Node 2: assumevalid=150, send some blocks, send all headers, send remaining blocks") | ||||
| self.next_node(['-assumevalid=' + hex(self.blocks[150].sha256)]) | self.next_node(['-assumevalid=' + hex(self.blocks[150].sha256)]) | ||||
| self.send_blocks(self.blocks[1:21]) | self.send_blocks(self.blocks[1:21]) | ||||
| assert_equal(self.current_commitment, self.commitments[20]) | assert_equal(self.current_commitment, self.commitments[20]) | ||||
| assert(self.is_maintaining) | assert(self.is_maintaining) | ||||
| self.check_coinbase() | |||||
| # sending all headers should activate assumevalid | # sending all headers should activate assumevalid | ||||
| self.send_headers(self.blocks[1:2001]) | self.send_headers(self.blocks[1:2001]) | ||||
| self.send_headers(self.blocks[2001:]) | self.send_headers(self.blocks[2001:]) | ||||
| self.send_blocks(self.blocks[21:41]) | self.send_blocks(self.blocks[21:41]) | ||||
| assert_equal(self.current_commitment, self.commitments[40]) | assert_equal(self.current_commitment, self.commitments[40]) | ||||
| assert(not self.is_maintaining) | assert(not self.is_maintaining) | ||||
| self.send_blocks(self.blocks[41:151]) | self.send_blocks(self.blocks[41:151]) | ||||
| assert_equal(self.current_commitment, self.commitments[150]) | assert_equal(self.current_commitment, self.commitments[150]) | ||||
| assert (not self.is_maintaining) | assert (not self.is_maintaining) | ||||
| # expect switch to assumevalid=false | # expect switch to assumevalid=false | ||||
| self.send_blocks(self.blocks[151:152]) | self.send_blocks(self.blocks[151:152]) | ||||
| assert_equal(self.current_commitment, self.commitments[151]) | assert_equal(self.current_commitment, self.commitments[151]) | ||||
| assert(self.is_maintaining) | assert(self.is_maintaining) | ||||
| self.send_blocks(self.blocks[152:201]) | self.send_blocks(self.blocks[152:201]) | ||||
| assert_equal(self.current_commitment, self.commitments[200]) | assert_equal(self.current_commitment, self.commitments[200]) | ||||
| assert(not (self.current_commitment == self.commitments[199])) | assert(not (self.current_commitment == self.commitments[199])) | ||||
| assert(self.is_maintaining) | assert(self.is_maintaining) | ||||
| self.check_coinbase() | |||||
| # ** Node 3 | # ** Node 3 | ||||
| self.log.info("Node 3: reorg after 175 blocks") | self.log.info("Node 3: reorg after 175 blocks") | ||||
| self.next_node() | self.next_node() | ||||
| self.send_blocks(self.altblocks[1:175]) | self.send_blocks(self.altblocks[1:175]) | ||||
| assert(not self.current_commitment == self.commitments[174]) | assert(not self.current_commitment == self.commitments[174]) | ||||
| assert(self.is_maintaining) | assert(self.is_maintaining) | ||||
| # reorg | # reorg | ||||
| self.send_blocks(self.blocks[150:180]) | self.send_blocks(self.blocks[150:180]) | ||||
| assert_equal(self.current_commitment, self.commitments[179]) | assert_equal(self.current_commitment, self.commitments[179]) | ||||
| assert(self.is_maintaining) | assert(self.is_maintaining) | ||||
| self.check_coinbase() | |||||
| if __name__ == '__main__': | if __name__ == '__main__': | ||||
| UtxoCommitTest().main() | UtxoCommitTest().main() | ||||