Changeset View
Changeset View
Standalone View
Standalone View
test/functional/mining_basic.py
Show First 20 Lines • Show All 172 Lines • ▼ Show 20 Lines | def run_test(self): | ||||
lambda: node.submitheader(hexdata='ff' * 78)) | lambda: node.submitheader(hexdata='ff' * 78)) | ||||
assert_raises_rpc_error(-25, 'Must submit previous header', | assert_raises_rpc_error(-25, 'Must submit previous header', | ||||
lambda: node.submitheader(hexdata='ff' * 80)) | lambda: node.submitheader(hexdata='ff' * 80)) | ||||
block.nTime += 1 | block.nTime += 1 | ||||
block.solve() | block.solve() | ||||
def chain_tip(b_hash, *, status='headers-only', branchlen=1): | def chain_tip(b_hash, *, status='headers-only', branchlen=1): | ||||
return {'hash': b_hash, 'height': 202, 'branchlen': branchlen, 'status': status} | return {'hash': b_hash, 'height': 202, | ||||
'branchlen': branchlen, 'status': status} | |||||
assert chain_tip(block.hash) not in node.getchaintips() | assert chain_tip(block.hash) not in node.getchaintips() | ||||
node.submitheader(hexdata=block.serialize().hex()) | node.submitheader(hexdata=block.serialize().hex()) | ||||
assert chain_tip(block.hash) in node.getchaintips() | assert chain_tip(block.hash) in node.getchaintips() | ||||
# Noop | # Noop | ||||
node.submitheader(hexdata=CBlockHeader(block).serialize().hex()) | node.submitheader(hexdata=CBlockHeader(block).serialize().hex()) | ||||
assert chain_tip(block.hash) in node.getchaintips() | assert chain_tip(block.hash) in node.getchaintips() | ||||
bad_block_root = copy.deepcopy(block) | bad_block_root = copy.deepcopy(block) | ||||
bad_block_root.hashMerkleRoot += 2 | bad_block_root.hashMerkleRoot += 2 | ||||
bad_block_root.solve() | bad_block_root.solve() | ||||
assert chain_tip(bad_block_root.hash) not in node.getchaintips() | assert chain_tip(bad_block_root.hash) not in node.getchaintips() | ||||
node.submitheader(hexdata=CBlockHeader( | node.submitheader(hexdata=CBlockHeader( | ||||
bad_block_root).serialize().hex()) | bad_block_root).serialize().hex()) | ||||
assert chain_tip(bad_block_root.hash) in node.getchaintips() | assert chain_tip(bad_block_root.hash) in node.getchaintips() | ||||
# Should still reject invalid blocks, even if we have the header: | # Should still reject invalid blocks, even if we have the header: | ||||
assert_equal(node.submitblock( | assert_equal(node.submitblock( | ||||
hexdata=bad_block_root.serialize().hex()), 'bad-txnmrklroot') | hexdata=bad_block_root.serialize().hex()), 'bad-txnmrklroot') | ||||
assert_equal(node.submitblock( | assert_equal(node.submitblock( | ||||
hexdata=bad_block_root.serialize().hex()), 'bad-txnmrklroot') | hexdata=bad_block_root.serialize().hex()), 'bad-txnmrklroot') | ||||
assert chain_tip(bad_block_root.hash) in node.getchaintips() | assert chain_tip(bad_block_root.hash) in node.getchaintips() | ||||
# We know the header for this invalid block, so should just return early without error: | # We know the header for this invalid block, so should just return | ||||
# early without error: | |||||
node.submitheader(hexdata=CBlockHeader( | node.submitheader(hexdata=CBlockHeader( | ||||
bad_block_root).serialize().hex()) | bad_block_root).serialize().hex()) | ||||
assert chain_tip(bad_block_root.hash) in node.getchaintips() | assert chain_tip(bad_block_root.hash) in node.getchaintips() | ||||
bad_block_lock = copy.deepcopy(block) | bad_block_lock = copy.deepcopy(block) | ||||
bad_block_lock.vtx[0].nLockTime = 2**32 - 1 | bad_block_lock.vtx[0].nLockTime = 2**32 - 1 | ||||
bad_block_lock.vtx[0].rehash() | bad_block_lock.vtx[0].rehash() | ||||
bad_block_lock.hashMerkleRoot = bad_block_lock.calc_merkle_root() | bad_block_lock.hashMerkleRoot = bad_block_lock.calc_merkle_root() | ||||
Show All 11 Lines | def run_test(self): | ||||
# Should reject invalid header right away | # Should reject invalid header right away | ||||
bad_block_time = copy.deepcopy(block) | bad_block_time = copy.deepcopy(block) | ||||
bad_block_time.nTime = 1 | bad_block_time.nTime = 1 | ||||
bad_block_time.solve() | bad_block_time.solve() | ||||
assert_raises_rpc_error(-25, 'time-too-old', lambda: node.submitheader( | assert_raises_rpc_error(-25, 'time-too-old', lambda: node.submitheader( | ||||
hexdata=CBlockHeader(bad_block_time).serialize().hex())) | hexdata=CBlockHeader(bad_block_time).serialize().hex())) | ||||
# Should ask for the block from a p2p node, if they announce the header as well: | # Should ask for the block from a p2p node, if they announce the header | ||||
# as well: | |||||
node.add_p2p_connection(P2PDataStore()) | node.add_p2p_connection(P2PDataStore()) | ||||
# Drop the first getheaders | # Drop the first getheaders | ||||
node.p2p.wait_for_getheaders(timeout=5) | node.p2p.wait_for_getheaders(timeout=5) | ||||
node.p2p.send_blocks_and_test(blocks=[block], node=node) | node.p2p.send_blocks_and_test(blocks=[block], node=node) | ||||
# Must be active now: | # Must be active now: | ||||
assert chain_tip(block.hash, status='active', | assert chain_tip(block.hash, status='active', | ||||
branchlen=0) in node.getchaintips() | branchlen=0) in node.getchaintips() | ||||
Show All 16 Lines |