def test_getblocktxn_handler(self, test_node):
    version = test_node.cmpct_version
    node = self.nodes[0]
    MAX_GETBLOCKTXN_DEPTH = 10
    chain_height = node.getblockcount()
    current_height = chain_height
    while (current_height >= (chain_height - MAX_GETBLOCKTXN_DEPTH)):
        block_hash = node.getblockhash(current_height)
        block = FromHex(CBlock(), node.getblock(block_hash, False))
        msg = msg_getblocktxn()
        msg.block_txn_request = BlockTransactionsRequest(int(block_hash, 16), [])
        num_to_request = random.randint(1, len(block.vtx))
        msg.block_txn_request.from_absolute(sorted(random.sample(range(len(block.vtx)), num_to_request)))
        test_node.send_message(msg)
        wait_until((lambda : ('blocktxn' in test_node.last_message)), timeout=10, lock=mininode_lock)
        [tx.calc_sha256() for tx in block.vtx]
        with mininode_lock:
            assert_equal(test_node.last_message['blocktxn'].block_transactions.blockhash, int(block_hash, 16))
            all_indices = msg.block_txn_request.to_absolute()
            for index in all_indices:
                tx = test_node.last_message['blocktxn'].block_transactions.transactions.pop(0)
                tx.calc_sha256()
                assert_equal(tx.sha256, block.vtx[index].sha256)
                if (version == 1):
                    assert tx.wit.is_null()
                else:
                    assert_equal(tx.calc_sha256(True), block.vtx[index].calc_sha256(True))
            test_node.last_message.pop('blocktxn', None)
        current_height -= 1
    block_hash = node.getblockhash(current_height)
    msg.block_txn_request = BlockTransactionsRequest(int(block_hash, 16), [0])
    with mininode_lock:
        test_node.last_message.pop('block', None)
        test_node.last_message.pop('blocktxn', None)
    test_node.send_and_ping(msg)
    with mininode_lock:
        test_node.last_message['block'].block.calc_sha256()
        assert_equal(test_node.last_message['block'].block.sha256, int(block_hash, 16))
        assert ('blocktxn' not in test_node.last_message)
def test_getblocktxn_handler(self, test_node):
    version = test_node.cmpct_version
    node = self.nodes[0]
    MAX_GETBLOCKTXN_DEPTH = 10
    chain_height = node.getblockcount()
    current_height = chain_height
    while (current_height >= (chain_height - MAX_GETBLOCKTXN_DEPTH)):
        block_hash = node.getblockhash(current_height)
        block = FromHex(CBlock(), node.getblock(block_hash, False))
        msg = msg_getblocktxn()
        msg.block_txn_request = BlockTransactionsRequest(int(block_hash, 16), [])
        num_to_request = random.randint(1, len(block.vtx))
        msg.block_txn_request.from_absolute(sorted(random.sample(range(len(block.vtx)), num_to_request)))
        test_node.send_message(msg)
        wait_until((lambda : ('blocktxn' in test_node.last_message)), timeout=10, lock=mininode_lock)
        [tx.calc_sha256() for tx in block.vtx]
        with mininode_lock:
            assert_equal(test_node.last_message['blocktxn'].block_transactions.blockhash, int(block_hash, 16))
            all_indices = msg.block_txn_request.to_absolute()
            for index in all_indices:
                tx = test_node.last_message['blocktxn'].block_transactions.transactions.pop(0)
                tx.calc_sha256()
                assert_equal(tx.sha256, block.vtx[index].sha256)
                if (version == 1):
                    assert tx.wit.is_null()
                else:
                    assert_equal(tx.calc_sha256(True), block.vtx[index].calc_sha256(True))
            test_node.last_message.pop('blocktxn', None)
        current_height -= 1
    block_hash = node.getblockhash(current_height)
    msg.block_txn_request = BlockTransactionsRequest(int(block_hash, 16), [0])
    with mininode_lock:
        test_node.last_message.pop('block', None)
        test_node.last_message.pop('blocktxn', None)
    test_node.send_and_ping(msg)
    with mininode_lock:
        test_node.last_message['block'].block.calc_sha256()
        assert_equal(test_node.last_message['block'].block.sha256, int(block_hash, 16))
        assert ('blocktxn' not in test_node.last_message)