Changeset View
Changeset View
Standalone View
Standalone View
test/functional/abc-p2p-compactblocks.py
Show First 20 Lines • Show All 110 Lines • ▼ Show 20 Lines | def add_transactions_to_block(self, block, 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 == 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) | coinbase = create_coinbase(height) | ||||
coinbase.rehash() | coinbase.rehash() | ||||
if spend == None: | if spend is 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) | ||||
Show All 16 Lines | def next_block(self, number, spend=None, script=CScript([OP_TRUE]), block_size=0, extra_txns=0): | ||||
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 != 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]) | ||||
▲ Show 20 Lines • Show All 102 Lines • ▼ Show 20 Lines | def run_test(self): | ||||
# collect spendable outputs now to avoid cluttering the code later on | # collect spendable outputs now to avoid cluttering the code later on | ||||
out = [] | out = [] | ||||
for i in range(100): | for i in range(100): | ||||
out.append(get_spendable_output()) | out.append(get_spendable_output()) | ||||
# Check that compact block also work for big blocks | # Check that compact block also work for big blocks | ||||
# Wait for SENDCMPCT | # Wait for SENDCMPCT | ||||
def received_sendcmpct(): | def received_sendcmpct(): | ||||
return (test_p2p.last_sendcmpct != None) | return (test_p2p.last_sendcmpct is not None) | ||||
wait_until(received_sendcmpct, timeout=30) | wait_until(received_sendcmpct, timeout=30) | ||||
sendcmpct = msg_sendcmpct() | sendcmpct = msg_sendcmpct() | ||||
sendcmpct.version = 1 | sendcmpct.version = 1 | ||||
sendcmpct.announce = True | sendcmpct.announce = True | ||||
test_p2p.send_and_ping(sendcmpct) | test_p2p.send_and_ping(sendcmpct) | ||||
# Exchange headers | # Exchange headers | ||||
def received_getheaders(): | def received_getheaders(): | ||||
return (test_p2p.last_getheaders != None) | return (test_p2p.last_getheaders is not None) | ||||
wait_until(received_getheaders, timeout=30) | wait_until(received_getheaders, timeout=30) | ||||
# Return the favor | # Return the favor | ||||
test_p2p.send_message(test_p2p.last_getheaders) | test_p2p.send_message(test_p2p.last_getheaders) | ||||
# Wait for the header list | # Wait for the header list | ||||
def received_headers(): | def received_headers(): | ||||
return (test_p2p.last_headers != None) | return (test_p2p.last_headers is not None) | ||||
wait_until(received_headers, timeout=30) | wait_until(received_headers, timeout=30) | ||||
# It's like we know about the same headers ! | # It's like we know about the same headers ! | ||||
test_p2p.send_message(test_p2p.last_headers) | test_p2p.send_message(test_p2p.last_headers) | ||||
# Send a block | # Send a block | ||||
b1 = block(1, spend=out[0], block_size=ONE_MEGABYTE + 1) | b1 = block(1, spend=out[0], block_size=ONE_MEGABYTE + 1) | ||||
default_p2p.send_blocks_and_test([self.tip], node) | default_p2p.send_blocks_and_test([self.tip], node) | ||||
# Checks the node to forward it via compact block | # Checks the node to forward it via compact block | ||||
def received_block(): | def received_block(): | ||||
return (test_p2p.last_cmpctblock != None) | return (test_p2p.last_cmpctblock is not None) | ||||
wait_until(received_block, timeout=30) | wait_until(received_block, timeout=30) | ||||
# 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 == b1.sha256 | assert cmpctblk_header.sha256 == b1.sha256 | ||||
# Send a large block with numerous transactions. | # Send a large block with numerous transactions. | ||||
Show All 38 Lines |