Changeset View
Changeset View
Standalone View
Standalone View
test/functional/abc_p2p_fullblocktest.py
Show All 30 Lines | |||||
) | ) | ||||
from test_framework.p2p import P2PDataStore | from test_framework.p2p import P2PDataStore | ||||
from test_framework.script import OP_RETURN, OP_TRUE, CScript | from test_framework.script import OP_RETURN, OP_TRUE, CScript | ||||
from test_framework.test_framework import BitcoinTestFramework | from test_framework.test_framework import BitcoinTestFramework | ||||
from test_framework.util import assert_equal | from test_framework.util import assert_equal | ||||
class PreviousSpendableOutput: | class PreviousSpendableOutput: | ||||
def __init__(self, tx=CTransaction(), n=-1): | def __init__(self, tx=CTransaction(), n=-1): | ||||
self.tx = tx | self.tx = tx | ||||
# the output we're spending | # the output we're spending | ||||
self.n = n | self.n = n | ||||
class FullBlockTest(BitcoinTestFramework): | class FullBlockTest(BitcoinTestFramework): | ||||
def set_test_params(self): | def set_test_params(self): | ||||
self.num_nodes = 1 | self.num_nodes = 1 | ||||
self.setup_clean_chain = True | self.setup_clean_chain = True | ||||
self.block_heights = {} | self.block_heights = {} | ||||
self.tip = None | self.tip = None | ||||
self.blocks = {} | self.blocks = {} | ||||
self.excessive_block_size = 100 * ONE_MEGABYTE | self.excessive_block_size = 100 * ONE_MEGABYTE | ||||
self.extra_args = [['-whitelist=noban@127.0.0.1', | self.extra_args = [ | ||||
f"-excessiveblocksize={self.excessive_block_size}"]] | [ | ||||
"-whitelist=noban@127.0.0.1", | |||||
f"-excessiveblocksize={self.excessive_block_size}", | |||||
] | |||||
] | |||||
self.supports_cli = False | self.supports_cli = False | ||||
# The default timeout is not enough when submitting large blocks with | # The default timeout is not enough when submitting large blocks with | ||||
# TSAN enabled | # TSAN enabled | ||||
self.rpc_timeout = 360 | self.rpc_timeout = 360 | ||||
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) | ||||
def next_block(self, number, spend=None, | def next_block(self, number, spend=None, script=CScript([OP_TRUE]), block_size=0): | ||||
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 32 Lines | def next_block(self, number, spend=None, script=CScript([OP_TRUE]), block_size=0): | ||||
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 | # Put some random data into the first transaction of the chain to | ||||
# randomize ids. | # 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: | ||||
Show All 12 Lines | def next_block(self, number, spend=None, script=CScript([OP_TRUE]), block_size=0): | ||||
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 | # need to generate a transaction smaller than what we | ||||
# expected. | # 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 | ||||
# in the block. | # in the block. | ||||
self.add_transactions_to_block(block, [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 | ||||
▲ Show 20 Lines • Show All 58 Lines • ▼ Show 20 Lines | def run_test(self): | ||||
# block of maximal size | # block of maximal size | ||||
block(17, spend=out[16], block_size=self.excessive_block_size) | block(17, spend=out[16], block_size=self.excessive_block_size) | ||||
peer.send_blocks_and_test([self.tip], node) | peer.send_blocks_and_test([self.tip], node) | ||||
# Reject oversized blocks with bad-blk-length error | # Reject oversized blocks with bad-blk-length error | ||||
block(18, spend=out[17], block_size=self.excessive_block_size + 1) | block(18, spend=out[17], block_size=self.excessive_block_size + 1) | ||||
peer.send_blocks_and_test( | peer.send_blocks_and_test( | ||||
[self.tip], node, success=False, reject_reason='bad-blk-length', timeout=360) | [self.tip], node, success=False, reject_reason="bad-blk-length", timeout=360 | ||||
) | |||||
# Disconnect all the peers now so our node doesn't try to relay the next | # Disconnect all the peers now so our node doesn't try to relay the next | ||||
# large block, which could cause a timeout during the test shutdown. | # large block, which could cause a timeout during the test shutdown. | ||||
node.disconnect_p2ps() | node.disconnect_p2ps() | ||||
# Rewind bad block. | # Rewind bad block. | ||||
self.tip = self.blocks[17] | self.tip = self.blocks[17] | ||||
# Submit a very large block via RPC | # Submit a very large block via RPC | ||||
large_block = block( | large_block = block(33, spend=out[17], block_size=self.excessive_block_size) | ||||
33, spend=out[17], block_size=self.excessive_block_size) | |||||
assert_equal(node.submitblock(ToHex(large_block)), None) | assert_equal(node.submitblock(ToHex(large_block)), None) | ||||
if __name__ == '__main__': | if __name__ == "__main__": | ||||
FullBlockTest().main() | FullBlockTest().main() |