Changeset View
Changeset View
Standalone View
Standalone View
test/functional/abc-p2p-fullblocktest.py
Show First 20 Lines • Show All 70 Lines • ▼ Show 20 Lines | 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 | ||||
def create_tx(self, spend, value, script=CScript([OP_TRUE])): | def create_tx(self, spend, value, script=CScript([OP_TRUE])): | ||||
tx = create_tx_with_script(spend.tx, spend.n, b"", value, script) | tx = create_tx_with_script(spend.tx, spend.n, b"", value, script) | ||||
return tx | return tx | ||||
def next_block(self, number, spend=None, script=CScript([OP_TRUE]), block_size=0): | def next_block(self, number, spend=None, | ||||
script=CScript([OP_TRUE]), block_size=0): | |||||
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 | ||||
Show All 22 Lines | def next_block(self, number, spend=None, | ||||
for i in range(4): | for i in range(4): | ||||
tx.vout.append(CTxOut(0, CScript([OP_TRUE]))) | tx.vout.append(CTxOut(0, CScript([OP_TRUE]))) | ||||
spendable_outputs.append(PreviousSpendableOutput(tx, i)) | spendable_outputs.append(PreviousSpendableOutput(tx, i)) | ||||
return tx | return tx | ||||
tx = get_base_transaction() | tx = get_base_transaction() | ||||
# Make it the same format as transaction added for padding and save the size. | # Make it the same format as transaction added for padding and save the size. | ||||
# It's missing the padding output, so we add a constant to account for it. | # It's missing the padding output, so we add a constant to account | ||||
# for it. | |||||
tx.rehash() | tx.rehash() | ||||
base_tx_size = len(tx.serialize()) + 18 | base_tx_size = len(tx.serialize()) + 18 | ||||
# If a specific script is required, add it. | # If a specific script is required, add it. | ||||
if script is not None: | if script is not None: | ||||
tx.vout.append(CTxOut(1, script)) | tx.vout.append(CTxOut(1, script)) | ||||
# Put some random data into the first transaction of the chain to randomize ids. | # Put some random data into the first transaction of the chain to | ||||
# randomize ids. | |||||
tx.vout.append( | tx.vout.append( | ||||
CTxOut(0, CScript([random.randint(0, 256), OP_RETURN]))) | CTxOut(0, CScript([random.randint(0, 256), OP_RETURN]))) | ||||
# Add the transaction to the block | # Add the transaction to the block | ||||
self.add_transactions_to_block(block, [tx]) | self.add_transactions_to_block(block, [tx]) | ||||
# If we have a block size requirement, just fill | # If we have a block size requirement, just fill | ||||
# the block until we get there | # the block until we get there | ||||
current_block_size = len(block.serialize()) | current_block_size = len(block.serialize()) | ||||
while current_block_size < block_size: | while current_block_size < block_size: | ||||
# We will add a new transaction. That means the size of | # We will add a new transaction. That means the size of | ||||
# the field enumerating how many transaction go in the block | # the field enumerating how many transaction go in the block | ||||
# may change. | # may change. | ||||
current_block_size -= len(ser_compact_size(len(block.vtx))) | current_block_size -= len(ser_compact_size(len(block.vtx))) | ||||
current_block_size += len(ser_compact_size(len(block.vtx) + 1)) | current_block_size += len(ser_compact_size(len(block.vtx) + 1)) | ||||
# Create the new transaction | # Create the new transaction | ||||
tx = get_base_transaction() | tx = get_base_transaction() | ||||
# Add padding to fill the block. | # Add padding to fill the block. | ||||
script_length = block_size - current_block_size - base_tx_size | script_length = block_size - current_block_size - base_tx_size | ||||
if script_length > 510000: | if script_length > 510000: | ||||
if script_length < 1000000: | if script_length < 1000000: | ||||
# Make sure we don't find ourselves in a position where we | # Make sure we don't find ourselves in a position where we | ||||
# need to generate a transaction smaller than what we expected. | # need to generate a transaction smaller than what we | ||||
# expected. | |||||
script_length = script_length // 2 | script_length = script_length // 2 | ||||
else: | else: | ||||
script_length = 500000 | script_length = 500000 | ||||
script_pad_len = script_length | script_pad_len = script_length | ||||
script_output = CScript([b'\x00' * script_pad_len]) | script_output = CScript([b'\x00' * script_pad_len]) | ||||
tx.vout.append(CTxOut(0, script_output)) | tx.vout.append(CTxOut(0, script_output)) | ||||
# Add the tx to the list of transactions to be included | # Add the tx to the list of transactions to be included | ||||
▲ Show 20 Lines • Show All 108 Lines • Show Last 20 Lines |