def test_getblocktxn_requests(self, node, test_node, version):
    with_witness = (version == 2)

    def test_getblocktxn_response(compact_block, peer, expected_result):
        msg = msg_cmpctblock(compact_block.to_p2p())
        peer.send_and_ping(msg)
        with mininode_lock:
            assert ('getblocktxn' in peer.last_message)
            absolute_indexes = peer.last_message['getblocktxn'].block_txn_request.to_absolute()
        assert_equal(absolute_indexes, expected_result)

    def test_tip_after_message(node, peer, msg, tip):
        peer.send_and_ping(msg)
        assert_equal(int(node.getbestblockhash(), 16), tip)
    utxo = self.utxos.pop(0)
    block = self.build_block_with_transactions(node, utxo, 5)
    self.utxos.append([block.vtx[(- 1)].sha256, 0, block.vtx[(- 1)].vout[0].nValue])
    comp_block = HeaderAndShortIDs()
    comp_block.initialize_from_block(block, use_witness=with_witness)
    test_getblocktxn_response(comp_block, test_node, [1, 2, 3, 4, 5])
    msg_bt = msg_blocktxn()
    if with_witness:
        msg_bt = msg_witness_blocktxn()
    msg_bt.block_transactions = BlockTransactions(block.sha256, block.vtx[1:])
    test_tip_after_message(node, test_node, msg_bt, block.sha256)
    utxo = self.utxos.pop(0)
    block = self.build_block_with_transactions(node, utxo, 5)
    self.utxos.append([block.vtx[(- 1)].sha256, 0, block.vtx[(- 1)].vout[0].nValue])
    comp_block.initialize_from_block(block, prefill_list=[0, 1, 5], use_witness=with_witness)
    test_getblocktxn_response(comp_block, test_node, [2, 3, 4])
    msg_bt.block_transactions = BlockTransactions(block.sha256, block.vtx[2:5])
    test_tip_after_message(node, test_node, msg_bt, block.sha256)
    utxo = self.utxos.pop(0)
    block = self.build_block_with_transactions(node, utxo, 5)
    self.utxos.append([block.vtx[(- 1)].sha256, 0, block.vtx[(- 1)].vout[0].nValue])
    test_node.send_and_ping(msg_tx(block.vtx[1]))
    assert (block.vtx[1].hash in node.getrawmempool())
    comp_block.initialize_from_block(block, prefill_list=[0, 2, 3, 4], use_witness=with_witness)
    test_getblocktxn_response(comp_block, test_node, [5])
    msg_bt.block_transactions = BlockTransactions(block.sha256, [block.vtx[5]])
    test_tip_after_message(node, test_node, msg_bt, block.sha256)
    utxo = self.utxos.pop(0)
    block = self.build_block_with_transactions(node, utxo, 10)
    self.utxos.append([block.vtx[(- 1)].sha256, 0, block.vtx[(- 1)].vout[0].nValue])
    for tx in block.vtx[1:]:
        test_node.send_message(msg_tx(tx))
    test_node.sync_with_ping()
    mempool = node.getrawmempool()
    for tx in block.vtx[1:]:
        assert (tx.hash in mempool)
    with mininode_lock:
        test_node.last_message.pop('getblocktxn', None)
    comp_block.initialize_from_block(block, prefill_list=[0], use_witness=with_witness)
    test_tip_after_message(node, test_node, msg_cmpctblock(comp_block.to_p2p()), block.sha256)
    with mininode_lock:
        assert ('getblocktxn' not in test_node.last_message)
def test_getblocktxn_requests(self, node, test_node, version):
    with_witness = (version == 2)

    def test_getblocktxn_response(compact_block, peer, expected_result):
        msg = msg_cmpctblock(compact_block.to_p2p())
        peer.send_and_ping(msg)
        with mininode_lock:
            assert ('getblocktxn' in peer.last_message)
            absolute_indexes = peer.last_message['getblocktxn'].block_txn_request.to_absolute()
        assert_equal(absolute_indexes, expected_result)

    def test_tip_after_message(node, peer, msg, tip):
        peer.send_and_ping(msg)
        assert_equal(int(node.getbestblockhash(), 16), tip)
    utxo = self.utxos.pop(0)
    block = self.build_block_with_transactions(node, utxo, 5)
    self.utxos.append([block.vtx[(- 1)].sha256, 0, block.vtx[(- 1)].vout[0].nValue])
    comp_block = HeaderAndShortIDs()
    comp_block.initialize_from_block(block, use_witness=with_witness)
    test_getblocktxn_response(comp_block, test_node, [1, 2, 3, 4, 5])
    msg_bt = msg_blocktxn()
    if with_witness:
        msg_bt = msg_witness_blocktxn()
    msg_bt.block_transactions = BlockTransactions(block.sha256, block.vtx[1:])
    test_tip_after_message(node, test_node, msg_bt, block.sha256)
    utxo = self.utxos.pop(0)
    block = self.build_block_with_transactions(node, utxo, 5)
    self.utxos.append([block.vtx[(- 1)].sha256, 0, block.vtx[(- 1)].vout[0].nValue])
    comp_block.initialize_from_block(block, prefill_list=[0, 1, 5], use_witness=with_witness)
    test_getblocktxn_response(comp_block, test_node, [2, 3, 4])
    msg_bt.block_transactions = BlockTransactions(block.sha256, block.vtx[2:5])
    test_tip_after_message(node, test_node, msg_bt, block.sha256)
    utxo = self.utxos.pop(0)
    block = self.build_block_with_transactions(node, utxo, 5)
    self.utxos.append([block.vtx[(- 1)].sha256, 0, block.vtx[(- 1)].vout[0].nValue])
    test_node.send_and_ping(msg_tx(block.vtx[1]))
    assert (block.vtx[1].hash in node.getrawmempool())
    comp_block.initialize_from_block(block, prefill_list=[0, 2, 3, 4], use_witness=with_witness)
    test_getblocktxn_response(comp_block, test_node, [5])
    msg_bt.block_transactions = BlockTransactions(block.sha256, [block.vtx[5]])
    test_tip_after_message(node, test_node, msg_bt, block.sha256)
    utxo = self.utxos.pop(0)
    block = self.build_block_with_transactions(node, utxo, 10)
    self.utxos.append([block.vtx[(- 1)].sha256, 0, block.vtx[(- 1)].vout[0].nValue])
    for tx in block.vtx[1:]:
        test_node.send_message(msg_tx(tx))
    test_node.sync_with_ping()
    mempool = node.getrawmempool()
    for tx in block.vtx[1:]:
        assert (tx.hash in mempool)
    with mininode_lock:
        test_node.last_message.pop('getblocktxn', None)
    comp_block.initialize_from_block(block, prefill_list=[0], use_witness=with_witness)
    test_tip_after_message(node, test_node, msg_cmpctblock(comp_block.to_p2p()), block.sha256)
    with mininode_lock:
        assert ('getblocktxn' not in test_node.last_message)