Changeset View
Changeset View
Standalone View
Standalone View
test/functional/abc-p2p-compactblocks.py
Show First 20 Lines • Show All 109 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_tx, n, value, script=CScript([OP_TRUE])): | def create_tx(self, spend_tx, n, value, script=CScript([OP_TRUE])): | ||||
tx = create_tx_with_script(spend_tx, n, b"", value, script) | tx = create_tx_with_script(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): | |||||
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 23 Lines | def next_block(self, number, spend=None, script=CScript( | ||||
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) | 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() | ||||
# 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]) | ||||
# Add transaction until we reach the expected transaction count | # Add transaction until we reach the expected transaction count | ||||
for _ in range(extra_txns): | for _ in range(extra_txns): | ||||
▲ Show 20 Lines • Show All 153 Lines • ▼ Show 20 Lines | def run_test(self): | ||||
# Was it our block ? | # Was it our block ? | ||||
cmpctblk_header = test_p2p.last_cmpctblock.header_and_shortids.header | cmpctblk_header = test_p2p.last_cmpctblock.header_and_shortids.header | ||||
cmpctblk_header.calc_sha256() | cmpctblk_header.calc_sha256() | ||||
assert cmpctblk_header.sha256 == b2.sha256 | assert cmpctblk_header.sha256 == b2.sha256 | ||||
# In order to avoid having to resend a ton of transactions, we invalidate | # In order to avoid having to resend a ton of transactions, we invalidate | ||||
# b2, which will send all its transactions in the mempool. Note that this | # b2, which will send all its transactions in the mempool. Note that this | ||||
# assumes reorgs will insert low-fee transactions back into the mempool. | # assumes reorgs will insert low-fee transactions back into the | ||||
# mempool. | |||||
node.invalidateblock(node.getbestblockhash()) | node.invalidateblock(node.getbestblockhash()) | ||||
# Let's send a compact block and see if the node accepts it. | # Let's send a compact block and see if the node accepts it. | ||||
# Let's modify b2 and use it so that we can reuse the mempool. | # Let's modify b2 and use it so that we can reuse the mempool. | ||||
tx = b2.vtx[0] | tx = b2.vtx[0] | ||||
tx.vout.append(CTxOut(0, CScript([random.randint(0, 256), OP_RETURN]))) | tx.vout.append(CTxOut(0, CScript([random.randint(0, 256), OP_RETURN]))) | ||||
tx.rehash() | tx.rehash() | ||||
b2.vtx[0] = tx | b2.vtx[0] = tx | ||||
Show All 14 Lines |