Changeset View
Changeset View
Standalone View
Standalone View
test/functional/p2p_compactblocks.py
Show All 18 Lines | from test_framework.messages import ( | ||||
CBlockHeader, | CBlockHeader, | ||||
CInv, | CInv, | ||||
COutPoint, | COutPoint, | ||||
CTransaction, | CTransaction, | ||||
CTxIn, | CTxIn, | ||||
CTxOut, | CTxOut, | ||||
FromHex, | FromHex, | ||||
HeaderAndShortIDs, | HeaderAndShortIDs, | ||||
MSG_BLOCK, | |||||
msg_block, | msg_block, | ||||
msg_blocktxn, | msg_blocktxn, | ||||
msg_cmpctblock, | msg_cmpctblock, | ||||
msg_getblocktxn, | msg_getblocktxn, | ||||
msg_getdata, | msg_getdata, | ||||
msg_getheaders, | msg_getheaders, | ||||
msg_headers, | msg_headers, | ||||
msg_inv, | msg_inv, | ||||
▲ Show 20 Lines • Show All 42 Lines • ▼ Show 20 Lines | class TestP2PConn(P2PInterface): | ||||
def on_headers(self, message): | def on_headers(self, message): | ||||
self.block_announced = True | self.block_announced = True | ||||
for x in self.last_message["headers"].headers: | for x in self.last_message["headers"].headers: | ||||
x.calc_sha256() | x.calc_sha256() | ||||
self.announced_blockhashes.add(x.sha256) | self.announced_blockhashes.add(x.sha256) | ||||
def on_inv(self, message): | def on_inv(self, message): | ||||
for x in self.last_message["inv"].inv: | for x in self.last_message["inv"].inv: | ||||
if x.type == 2: | if x.type == MSG_BLOCK: | ||||
self.block_announced = True | self.block_announced = True | ||||
self.announced_blockhashes.add(x.hash) | self.announced_blockhashes.add(x.hash) | ||||
# Requires caller to hold mininode_lock | # Requires caller to hold mininode_lock | ||||
def received_block_announcement(self): | def received_block_announcement(self): | ||||
return self.block_announced | return self.block_announced | ||||
def clear_block_announcement(self): | def clear_block_announcement(self): | ||||
▲ Show 20 Lines • Show All 318 Lines • ▼ Show 20 Lines | class CompactBlocksTest(BitcoinTestFramework): | ||||
# to be successfully reconstructed. | # to be successfully reconstructed. | ||||
def test_compactblock_requests(self, node, test_node, version): | def test_compactblock_requests(self, node, test_node, version): | ||||
# Try announcing a block with an inv or header, expect a compactblock | # Try announcing a block with an inv or header, expect a compactblock | ||||
# request | # request | ||||
for announce in ["inv", "header"]: | for announce in ["inv", "header"]: | ||||
block = self.build_block_on_tip(node) | block = self.build_block_on_tip(node) | ||||
if announce == "inv": | if announce == "inv": | ||||
test_node.send_message(msg_inv([CInv(2, block.sha256)])) | test_node.send_message( | ||||
msg_inv([CInv(MSG_BLOCK, block.sha256)])) | |||||
wait_until(lambda: "getheaders" in test_node.last_message, | wait_until(lambda: "getheaders" in test_node.last_message, | ||||
timeout=30, lock=mininode_lock) | timeout=30, lock=mininode_lock) | ||||
test_node.send_header_for_blocks([block]) | test_node.send_header_for_blocks([block]) | ||||
else: | else: | ||||
test_node.send_header_for_blocks([block]) | test_node.send_header_for_blocks([block]) | ||||
test_node.wait_for_getdata([block.sha256], timeout=30) | test_node.wait_for_getdata([block.sha256], timeout=30) | ||||
assert_equal(test_node.last_message["getdata"].inv[0].type, 4) | assert_equal(test_node.last_message["getdata"].inv[0].type, 4) | ||||
▲ Show 20 Lines • Show All 184 Lines • ▼ Show 20 Lines | def test_incorrect_blocktxn_response(self, node, test_node, version): | ||||
block.sha256, [ordered_txs[5]] + ordered_txs[7:]) | block.sha256, [ordered_txs[5]] + ordered_txs[7:]) | ||||
test_node.send_and_ping(msg) | test_node.send_and_ping(msg) | ||||
# Tip should not have updated | # Tip should not have updated | ||||
assert_equal(int(node.getbestblockhash(), 16), block.hashPrevBlock) | assert_equal(int(node.getbestblockhash(), 16), block.hashPrevBlock) | ||||
# We should receive a getdata request | # We should receive a getdata request | ||||
test_node.wait_for_getdata([block.sha256], timeout=10) | test_node.wait_for_getdata([block.sha256], timeout=10) | ||||
assert test_node.last_message["getdata"].inv[0].type == 2 | assert test_node.last_message["getdata"].inv[0].type == MSG_BLOCK | ||||
# Deliver the block | # Deliver the block | ||||
test_node.send_and_ping(msg_block(block)) | test_node.send_and_ping(msg_block(block)) | ||||
assert_equal(int(node.getbestblockhash(), 16), block.sha256) | assert_equal(int(node.getbestblockhash(), 16), block.sha256) | ||||
def test_getblocktxn_handler(self, node, test_node, version): | def test_getblocktxn_handler(self, node, test_node, version): | ||||
# bitcoind will not send blocktxn responses for blocks whose height is | # bitcoind will not send blocktxn responses for blocks whose height is | ||||
# more than 10 blocks deep. | # more than 10 blocks deep. | ||||
▲ Show 20 Lines • Show All 298 Lines • Show Last 20 Lines |