Changeset View
Changeset View
Standalone View
Standalone View
test/functional/feature_block.py
Show First 20 Lines • Show All 1,044 Lines • ▼ Show 20 Lines | def run_test(self): | ||||
self.log.info( | self.log.info( | ||||
"Test a re-org of one week's worth of blocks (1088 blocks)") | "Test a re-org of one week's worth of blocks (1088 blocks)") | ||||
self.move_tip(88) | self.move_tip(88) | ||||
LARGE_REORG_SIZE = 1088 | LARGE_REORG_SIZE = 1088 | ||||
blocks = [] | blocks = [] | ||||
spend = out[32] | spend = out[32] | ||||
for i in range(89, LARGE_REORG_SIZE + 89): | for i in range(89, LARGE_REORG_SIZE + 89): | ||||
b = self.next_block(i, spend) | b = self.next_block(i, spend, version=4) | ||||
tx = CTransaction() | tx = CTransaction() | ||||
script_length = LEGACY_MAX_BLOCK_SIZE - len(b.serialize()) - 69 | script_length = LEGACY_MAX_BLOCK_SIZE - len(b.serialize()) - 69 | ||||
script_output = CScript([b'\x00' * script_length]) | script_output = CScript([b'\x00' * script_length]) | ||||
tx.vout.append(CTxOut(0, script_output)) | tx.vout.append(CTxOut(0, script_output)) | ||||
tx.vin.append(CTxIn(COutPoint(b.vtx[1].sha256, 0))) | tx.vin.append(CTxIn(COutPoint(b.vtx[1].sha256, 0))) | ||||
b = self.update_block(i, [tx]) | b = self.update_block(i, [tx]) | ||||
assert_equal(len(b.serialize()), LEGACY_MAX_BLOCK_SIZE) | assert_equal(len(b.serialize()), LEGACY_MAX_BLOCK_SIZE) | ||||
blocks.append(b) | blocks.append(b) | ||||
self.save_spendable_output() | self.save_spendable_output() | ||||
spend = self.get_spendable_output() | spend = self.get_spendable_output() | ||||
self.send_blocks(blocks, True, timeout=960) | self.send_blocks(blocks, True, timeout=960) | ||||
chain1_tip = i | chain1_tip = i | ||||
# now create alt chain of same length | # now create alt chain of same length | ||||
self.move_tip(88) | self.move_tip(88) | ||||
blocks2 = [] | blocks2 = [] | ||||
for i in range(89, LARGE_REORG_SIZE + 89): | for i in range(89, LARGE_REORG_SIZE + 89): | ||||
blocks2.append(self.next_block("alt" + str(i))) | blocks2.append(self.next_block("alt" + str(i), version=4)) | ||||
self.send_blocks(blocks2, False, force_send=True) | self.send_blocks(blocks2, False, force_send=True) | ||||
# extend alt chain to trigger re-org | # extend alt chain to trigger re-org | ||||
block = self.next_block("alt" + str(chain1_tip + 1)) | block = self.next_block("alt" + str(chain1_tip + 1), version=4) | ||||
self.send_blocks([block], True, timeout=960) | self.send_blocks([block], True, timeout=960) | ||||
# ... and re-org back to the first chain | # ... and re-org back to the first chain | ||||
self.move_tip(chain1_tip) | self.move_tip(chain1_tip) | ||||
block = self.next_block(chain1_tip + 1) | block = self.next_block(chain1_tip + 1, version=4) | ||||
self.send_blocks([block], False, force_send=True) | self.send_blocks([block], False, force_send=True) | ||||
block = self.next_block(chain1_tip + 2) | block = self.next_block(chain1_tip + 2, version=4) | ||||
self.send_blocks([block], True, timeout=960) | self.send_blocks([block], True, timeout=960) | ||||
self.log.info("Reject a block with an invalid block header version") | |||||
b_v1 = self.next_block('b_v1', version=1) | |||||
self.send_blocks( | |||||
[b_v1], | |||||
success=False, | |||||
force_send=True, | |||||
reject_reason='bad-version(0x00000001)') | |||||
self.move_tip(chain1_tip + 2) | |||||
b_cb34 = self.next_block('b_cb34', version=4) | |||||
b_cb34.vtx[0].vin[0].scriptSig = b_cb34.vtx[0].vin[0].scriptSig[:-1] | |||||
b_cb34.vtx[0].rehash() | |||||
b_cb34.hashMerkleRoot = b_cb34.calc_merkle_root() | |||||
b_cb34.solve() | |||||
self.send_blocks( | |||||
[b_cb34], | |||||
success=False, | |||||
reject_reason='bad-cb-height', | |||||
reconnect=True) | |||||
# Helper methods | # Helper methods | ||||
################ | ################ | ||||
def add_transactions_to_block(self, block, tx_list): | def add_transactions_to_block(self, block, tx_list): | ||||
[tx.rehash() for tx in tx_list] | [tx.rehash() for tx in tx_list] | ||||
block.vtx.extend(tx_list) | block.vtx.extend(tx_list) | ||||
# this is a little handier to use than the version in blocktools.py | # this is a little handier to use than the version in blocktools.py | ||||
Show All 17 Lines | |||||
def create_and_sign_transaction( | def create_and_sign_transaction( | ||||
self, spend_tx, value, script=CScript([OP_TRUE])): | self, spend_tx, value, script=CScript([OP_TRUE])): | ||||
tx = self.create_tx(spend_tx, 0, value, script) | tx = self.create_tx(spend_tx, 0, value, script) | ||||
self.sign_tx(tx, spend_tx) | self.sign_tx(tx, spend_tx) | ||||
tx.rehash() | tx.rehash() | ||||
return tx | return tx | ||||
def next_block(self, number, spend=None, additional_coinbase_value=0, | def next_block(self, number, spend=None, additional_coinbase_value=0, | ||||
script=CScript([OP_TRUE]), solve=True): | script=CScript([OP_TRUE]), solve=True, *, version=1): | ||||
if self.tip is None: | if self.tip is None: | ||||
base_block_hash = self.genesis_hash | base_block_hash = self.genesis_hash | ||||
block_time = int(time.time()) + 1 | block_time = int(time.time()) + 1 | ||||
else: | else: | ||||
base_block_hash = self.tip.sha256 | base_block_hash = self.tip.sha256 | ||||
block_time = self.tip.nTime + 1 | block_time = self.tip.nTime + 1 | ||||
# First create the coinbase | # First create the coinbase | ||||
height = self.block_heights[base_block_hash] + 1 | height = self.block_heights[base_block_hash] + 1 | ||||
coinbase = create_coinbase(height, self.coinbase_pubkey) | coinbase = create_coinbase(height, self.coinbase_pubkey) | ||||
coinbase.vout[0].nValue += additional_coinbase_value | coinbase.vout[0].nValue += additional_coinbase_value | ||||
coinbase.rehash() | coinbase.rehash() | ||||
if spend is None: | if spend is None: | ||||
block = create_block(base_block_hash, coinbase, block_time) | block = create_block( | ||||
base_block_hash, | |||||
coinbase, | |||||
block_time, | |||||
version=version) | |||||
else: | else: | ||||
# all but one satoshi to fees | # all but one satoshi to fees | ||||
coinbase.vout[0].nValue += spend.vout[0].nValue - 1 | coinbase.vout[0].nValue += spend.vout[0].nValue - 1 | ||||
coinbase.rehash() | coinbase.rehash() | ||||
block = create_block(base_block_hash, coinbase, block_time) | block = create_block( | ||||
base_block_hash, | |||||
coinbase, | |||||
block_time, | |||||
version=version) | |||||
# spend 1 satoshi | # spend 1 satoshi | ||||
tx = self.create_tx(spend, 0, 1, script) | tx = self.create_tx(spend, 0, 1, script) | ||||
self.sign_tx(tx, spend) | self.sign_tx(tx, spend) | ||||
self.add_transactions_to_block(block, [tx]) | self.add_transactions_to_block(block, [tx]) | ||||
block.hashMerkleRoot = block.calc_merkle_root() | block.hashMerkleRoot = block.calc_merkle_root() | ||||
if solve: | if solve: | ||||
block.solve() | block.solve() | ||||
self.tip = block | self.tip = block | ||||
▲ Show 20 Lines • Show All 72 Lines • Show Last 20 Lines |