def test_compactblock_requests(self, node, test_node, version, segwit):
    for announce in ['inv', 'header']:
        block = self.build_block_on_tip(node, segwit=segwit)
        with mininode_lock:
            test_node.last_message.pop('getdata', None)
        if (announce == 'inv'):
            test_node.send_message(msg_inv([CInv(2, block.sha256)]))
            wait_until((lambda : ('getheaders' in test_node.last_message)), timeout=30, lock=mininode_lock)
            test_node.send_header_for_blocks([block])
        else:
            test_node.send_header_for_blocks([block])
        wait_until((lambda : ('getdata' in test_node.last_message)), timeout=30, lock=mininode_lock)
        assert_equal(len(test_node.last_message['getdata'].inv), 1)
        assert_equal(test_node.last_message['getdata'].inv[0].type, 4)
        assert_equal(test_node.last_message['getdata'].inv[0].hash, block.sha256)
        comp_block = HeaderAndShortIDs()
        comp_block.header = CBlockHeader(block)
        comp_block.nonce = 0
        [k0, k1] = comp_block.get_siphash_keys()
        coinbase_hash = block.vtx[0].sha256
        if (version == 2):
            coinbase_hash = block.vtx[0].calc_sha256(True)
        comp_block.shortids = [calculate_shortid(k0, k1, coinbase_hash)]
        test_node.send_and_ping(msg_cmpctblock(comp_block.to_p2p()))
        assert_equal(int(node.getbestblockhash(), 16), block.hashPrevBlock)
        with mininode_lock:
            assert ('getblocktxn' in test_node.last_message)
            absolute_indexes = test_node.last_message['getblocktxn'].block_txn_request.to_absolute()
        assert_equal(absolute_indexes, [0])
        if (version == 2):
            msg = msg_witness_blocktxn()
        else:
            msg = msg_blocktxn()
        msg.block_transactions.blockhash = block.sha256
        msg.block_transactions.transactions = [block.vtx[0]]
        test_node.send_and_ping(msg)
        assert_equal(int(node.getbestblockhash(), 16), block.sha256)
def test_compactblock_requests(self, node, test_node, version, segwit):
    for announce in ['inv', 'header']:
        block = self.build_block_on_tip(node, segwit=segwit)
        with mininode_lock:
            test_node.last_message.pop('getdata', None)
        if (announce == 'inv'):
            test_node.send_message(msg_inv([CInv(2, block.sha256)]))
            wait_until((lambda : ('getheaders' in test_node.last_message)), timeout=30, lock=mininode_lock)
            test_node.send_header_for_blocks([block])
        else:
            test_node.send_header_for_blocks([block])
        wait_until((lambda : ('getdata' in test_node.last_message)), timeout=30, lock=mininode_lock)
        assert_equal(len(test_node.last_message['getdata'].inv), 1)
        assert_equal(test_node.last_message['getdata'].inv[0].type, 4)
        assert_equal(test_node.last_message['getdata'].inv[0].hash, block.sha256)
        comp_block = HeaderAndShortIDs()
        comp_block.header = CBlockHeader(block)
        comp_block.nonce = 0
        [k0, k1] = comp_block.get_siphash_keys()
        coinbase_hash = block.vtx[0].sha256
        if (version == 2):
            coinbase_hash = block.vtx[0].calc_sha256(True)
        comp_block.shortids = [calculate_shortid(k0, k1, coinbase_hash)]
        test_node.send_and_ping(msg_cmpctblock(comp_block.to_p2p()))
        assert_equal(int(node.getbestblockhash(), 16), block.hashPrevBlock)
        with mininode_lock:
            assert ('getblocktxn' in test_node.last_message)
            absolute_indexes = test_node.last_message['getblocktxn'].block_txn_request.to_absolute()
        assert_equal(absolute_indexes, [0])
        if (version == 2):
            msg = msg_witness_blocktxn()
        else:
            msg = msg_blocktxn()
        msg.block_transactions.blockhash = block.sha256
        msg.block_transactions.transactions = [block.vtx[0]]
        test_node.send_and_ping(msg)
        assert_equal(int(node.getbestblockhash(), 16), block.sha256)