Changeset View
Changeset View
Standalone View
Standalone View
test/functional/abc-p2p-fullblocktest.py
- This file was copied to test/functional/abc-p2p-compactblocks.py.
Show All 24 Lines | |||||
class PreviousSpendableOutput(): | class PreviousSpendableOutput(): | ||||
def __init__(self, tx=CTransaction(), n=-1): | def __init__(self, tx=CTransaction(), n=-1): | ||||
self.tx = tx | self.tx = tx | ||||
self.n = n # the output we're spending | self.n = n # the output we're spending | ||||
# TestNode: A peer we use to send messages to bitcoind, and store responses. | |||||
class TestNode(NodeConnCB): | |||||
def __init__(self): | |||||
self.last_sendcmpct = None | |||||
self.last_cmpctblock = None | |||||
self.last_getheaders = None | |||||
self.last_headers = None | |||||
super().__init__() | |||||
def on_sendcmpct(self, conn, message): | |||||
self.last_sendcmpct = message | |||||
def on_cmpctblock(self, conn, message): | |||||
self.last_cmpctblock = message | |||||
self.last_cmpctblock.header_and_shortids.header.calc_sha256() | |||||
def on_getheaders(self, conn, message): | |||||
self.last_getheaders = message | |||||
def on_headers(self, conn, message): | |||||
self.last_headers = message | |||||
for x in self.last_headers.headers: | |||||
x.calc_sha256() | |||||
def clear_block_data(self): | |||||
with mininode_lock: | |||||
self.last_sendcmpct = None | |||||
self.last_cmpctblock = None | |||||
class FullBlockTest(ComparisonTestFramework): | class FullBlockTest(ComparisonTestFramework): | ||||
# Can either run this test as 1 node with expected answers, or two and compare them. | # Can either run this test as 1 node with expected answers, or two and compare them. | ||||
# Change the "outcome" variable from each TestInstance object to only do | # Change the "outcome" variable from each TestInstance object to only do | ||||
# the comparison. | # the comparison. | ||||
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 = [['-norelaypriority', | self.extra_args = [['-whitelist=127.0.0.1', | ||||
'-whitelist=127.0.0.1', | |||||
'-limitancestorcount=9999', | |||||
'-limitancestorsize=9999', | |||||
'-limitdescendantcount=9999', | |||||
'-limitdescendantsize=9999', | |||||
'-maxmempool=999', | |||||
"-excessiveblocksize=%d" | "-excessiveblocksize=%d" | ||||
% self.excessive_block_size]] | % self.excessive_block_size]] | ||||
def add_options(self, parser): | def add_options(self, parser): | ||||
super().add_options(parser) | super().add_options(parser) | ||||
parser.add_option( | parser.add_option( | ||||
"--runbarelyexpensive", dest="runbarelyexpensive", default=True) | "--runbarelyexpensive", dest="runbarelyexpensive", default=True) | ||||
▲ Show 20 Lines • Show All 321 Lines • ▼ Show 20 Lines | def get_tests(self): | ||||
tip(30) | tip(30) | ||||
# Max sigops in one p2sh txn | # Max sigops in one p2sh txn | ||||
max_p2sh_sigops = CScript([OP_CHECKSIG] * (p2sh_sigops_limit)) | max_p2sh_sigops = CScript([OP_CHECKSIG] * (p2sh_sigops_limit)) | ||||
block(32, spend=out[23], block_size=ONE_MEGABYTE + 1) | block(32, spend=out[23], block_size=ONE_MEGABYTE + 1) | ||||
update_block(32, [spend_p2sh_tx(max_p2sh_sigops)]) | update_block(32, [spend_p2sh_tx(max_p2sh_sigops)]) | ||||
yield accepted() | yield accepted() | ||||
# Check that compact block also work for big blocks | |||||
node = self.nodes[0] | |||||
peer = TestNode() | |||||
peer.add_connection(NodeConn('127.0.0.1', p2p_port(0), node, peer)) | |||||
# Start up network handling in another thread and wait for connection | |||||
# to be etablished | |||||
NetworkThread().start() | |||||
peer.wait_for_verack() | |||||
# Wait for SENDCMPCT | |||||
def received_sendcmpct(): | |||||
return (peer.last_sendcmpct != None) | |||||
wait_until(received_sendcmpct, timeout=30) | |||||
sendcmpct = msg_sendcmpct() | |||||
sendcmpct.version = 1 | |||||
sendcmpct.announce = True | |||||
peer.send_and_ping(sendcmpct) | |||||
# Exchange headers | |||||
def received_getheaders(): | |||||
return (peer.last_getheaders != None) | |||||
wait_until(received_getheaders, timeout=30) | |||||
# Return the favor | |||||
peer.send_message(peer.last_getheaders) | |||||
# Wait for the header list | |||||
def received_headers(): | |||||
return (peer.last_headers != None) | |||||
wait_until(received_headers, timeout=30) | |||||
# It's like we know about the same headers ! | |||||
peer.send_message(peer.last_headers) | |||||
# Send a block | |||||
b33 = block(33, spend=out[24], block_size=ONE_MEGABYTE + 1) | |||||
yield accepted() | |||||
# Checks the node to forward it via compact block | |||||
def received_block(): | |||||
return (peer.last_cmpctblock != None) | |||||
wait_until(received_block, timeout=30) | |||||
# Was it our block ? | |||||
cmpctblk_header = peer.last_cmpctblock.header_and_shortids.header | |||||
cmpctblk_header.calc_sha256() | |||||
assert(cmpctblk_header.sha256 == b33.sha256) | |||||
# Send a bigger block | |||||
peer.clear_block_data() | |||||
b34 = block(34, spend=out[25], block_size=8 * ONE_MEGABYTE) | |||||
yield accepted() | |||||
# Checks the node forwards it via compact block | |||||
wait_until(received_block, timeout=30) | |||||
# Was it our block ? | |||||
cmpctblk_header = peer.last_cmpctblock.header_and_shortids.header | |||||
cmpctblk_header.calc_sha256() | |||||
assert(cmpctblk_header.sha256 == b34.sha256) | |||||
# Let's send a compact block and see if the node accepts it. | |||||
# First, we generate the block and send all transaction to the mempool | |||||
b35 = block(35, spend=out[26], block_size=8 * ONE_MEGABYTE) | |||||
for i in range(1, len(b35.vtx)): | |||||
node.sendrawtransaction(ToHex(b35.vtx[i]), True) | |||||
# Now we create the compact block and send it | |||||
comp_block = HeaderAndShortIDs() | |||||
comp_block.initialize_from_block(b35) | |||||
peer.send_and_ping(msg_cmpctblock(comp_block.to_p2p())) | |||||
# Check that compact block is received properly | |||||
assert(int(node.getbestblockhash(), 16) == b35.sha256) | |||||
if __name__ == '__main__': | if __name__ == '__main__': | ||||
FullBlockTest().main() | FullBlockTest().main() |