Changeset View
Changeset View
Standalone View
Standalone View
test/functional/abc-p2p-compactblocks.py
Show First 20 Lines • Show All 90 Lines • ▼ Show 20 Lines | def run_test(self): | ||||
self.test = TestManager(self, self.options.tmpdir) | self.test = TestManager(self, self.options.tmpdir) | ||||
self.test.add_all_connections(self.nodes) | self.test.add_all_connections(self.nodes) | ||||
# Start up network handling in another thread | # Start up network handling in another thread | ||||
NetworkThread().start() | NetworkThread().start() | ||||
# Set the blocksize to 2MB as initial condition | # Set the blocksize to 2MB as initial condition | ||||
self.nodes[0].setexcessiveblock(self.excessive_block_size) | self.nodes[0].setexcessiveblock(self.excessive_block_size) | ||||
self.test.run() | self.test.run() | ||||
def add_transactions_to_block(self, block, tx_list): | |||||
[tx.rehash() for tx in 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_tx, n, value, script=CScript([OP_TRUE])): | def create_tx(self, spend_tx, n, value, script=CScript([OP_TRUE])): | ||||
tx = create_transaction(spend_tx, n, b"", value, script) | tx = create_transaction(spend_tx, n, b"", value, script) | ||||
return tx | return tx | ||||
def next_block(self, number, spend=None, script=CScript([OP_TRUE]), block_size=0, extra_txns=0): | def next_block(self, number, spend=None, script=CScript([OP_TRUE]), block_size=0, extra_txns=0): | ||||
assert number not in self.blocks | |||||
if self.tip == None: | if self.tip == 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) | coinbase = create_coinbase(height) | ||||
coinbase.rehash() | coinbase.rehash() | ||||
if spend == None: | if spend == None: | ||||
# We need to have something to spend to fill the block. | # We need to have something to spend to fill the block. | ||||
assert_equal(block_size, 0) | assert_equal(block_size, 0) | ||||
block = create_block(base_block_hash, coinbase, block_time) | block = create_block(base_block_hash, coinbase, block_time) | ||||
else: | else: | ||||
# all but one satoshi to fees | # all but one satoshi to fees | ||||
coinbase.vout[0].nValue += spend.tx.vout[spend.n].nValue - 1 | coinbase.vout[0].nValue += spend.tx.vout[spend.n].nValue - 1 | ||||
coinbase.rehash() | coinbase.rehash() | ||||
block = create_block(base_block_hash, coinbase, block_time) | block = create_block(base_block_hash, coinbase, block_time) | ||||
# Make sure we have plenty enough to spend going forward. | # Make sure we have plenty engough to spend going forward. | ||||
spendable_outputs = deque([spend]) | spendable_outputs = deque([spend]) | ||||
def get_base_transaction(): | def get_base_transaction(): | ||||
# Create the new transaction | # Create the new transaction | ||||
tx = CTransaction() | tx = CTransaction() | ||||
# Spend from one of the spendable outputs | # Spend from one of the spendable outputs | ||||
spend = spendable_outputs.popleft() | spend = spendable_outputs.popleft() | ||||
spend.tx.rehash() | |||||
tx.vin.append(CTxIn(COutPoint(spend.tx.sha256, spend.n))) | tx.vin.append(CTxIn(COutPoint(spend.tx.sha256, spend.n))) | ||||
# Add spendable outputs | # Add spendable outputs | ||||
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)) | ||||
pad_tx(tx) | |||||
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 != None: | if script != 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 | ||||
block.vtx.append(tx) | self.add_transactions_to_block(block, [tx]) | ||||
# Add transaction until we reach the expected transaction count | # Add transaction until we reach the expected transaction count | ||||
block.vtx.extend([get_base_transaction() | for _ in range(extra_txns): | ||||
for _ in range(extra_txns)]) | self.add_transactions_to_block(block, [get_base_transaction()]) | ||||
# 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()) | ||||
overage_bytes = 0 | |||||
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)) | ||||
# Add padding to fill the block. | |||||
left_to_fill = block_size - current_block_size | |||||
# Don't go over the 1 mb limit for a txn | |||||
if left_to_fill > 500000: | |||||
# Make sure we eat up non-divisible by 100 amounts quickly | |||||
# Also keep transaction less than 1 MB | |||||
left_to_fill = 500000 + left_to_fill % 100 | |||||
# Create the new transaction | # Create the new transaction | ||||
tx = get_base_transaction() | tx = get_base_transaction() | ||||
pad_tx(tx, left_to_fill - overage_bytes) | |||||
if len(tx.serialize()) + current_block_size > block_size: | # Add padding to fill the block. | ||||
# Our padding was too big try again | script_length = block_size - current_block_size - base_tx_size | ||||
overage_bytes += 1 | if script_length > 510000: | ||||
continue | if script_length < 1000000: | ||||
# Make sure we don't find ourselves in a position where we | |||||
# need to generate a transaction smaller than what we expected. | |||||
script_length = script_length // 2 | |||||
else: | |||||
script_length = 500000 | |||||
script_output = CScript([b'\x00' * script_length]) | |||||
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 | ||||
# in the block. | # in the block. | ||||
block.vtx.append(tx) | self.add_transactions_to_block(block, [tx]) | ||||
current_block_size += len(tx.serialize()) | current_block_size += len(tx.serialize()) | ||||
# Now that we added a bunch of transaction, we need to recompute | # Now that we added a bunch of transaction, we need to recompute | ||||
# the merkle root. | # the merkle root. | ||||
make_conform_to_ctor(block) | make_conform_to_ctor(block) | ||||
block.hashMerkleRoot = block.calc_merkle_root() | |||||
# Check that the block size is what's expected | # Check that the block size is what's expected | ||||
if block_size > 0: | if block_size > 0: | ||||
assert len(block.serialize()) == block_size, "Block size wrong {} != {}".format( | assert_equal(len(block.serialize()), block_size) | ||||
len(block.serialize()), block_size) | |||||
block.hashMerkleRoot = block.calc_merkle_root() | |||||
# Do PoW, which is cheap on regnet | # Do PoW, which is cheap on regnet | ||||
block.solve() | block.solve() | ||||
self.tip = block | self.tip = block | ||||
self.block_heights[block.sha256] = height | self.block_heights[block.sha256] = height | ||||
assert number not in self.blocks | |||||
self.blocks[number] = block | self.blocks[number] = block | ||||
return block | return block | ||||
def get_tests(self): | def get_tests(self): | ||||
self.genesis_hash = int(self.nodes[0].getbestblockhash(), 16) | self.genesis_hash = int(self.nodes[0].getbestblockhash(), 16) | ||||
self.block_heights[self.genesis_hash] = 0 | self.block_heights[self.genesis_hash] = 0 | ||||
spendable_outputs = [] | spendable_outputs = [] | ||||
▲ Show 20 Lines • Show All 137 Lines • Show Last 20 Lines |