def run_test(self):
    self.test_node = TestNode()
    self.segwit_node = TestNode()
    self.old_node = TestNode()
    connections = []
    connections.append(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], self.test_node))
    connections.append(NodeConn('127.0.0.1', p2p_port(1), self.nodes[1], self.segwit_node, services=(NODE_NETWORK | NODE_WITNESS)))
    connections.append(NodeConn('127.0.0.1', p2p_port(1), self.nodes[1], self.old_node, services=NODE_NETWORK))
    self.test_node.add_connection(connections[0])
    self.segwit_node.add_connection(connections[1])
    self.old_node.add_connection(connections[2])
    NetworkThread().start()
    self.test_node.wait_for_verack()
    self.make_utxos()
    self.log.info('Running tests, pre-segwit activation:')
    self.log.info('Testing SENDCMPCT p2p message... ')
    self.test_sendcmpct(self.nodes[0], self.test_node, 1)
    sync_blocks(self.nodes)
    self.test_sendcmpct(self.nodes[1], self.segwit_node, 2, old_node=self.old_node)
    sync_blocks(self.nodes)
    self.log.info('Testing compactblock construction...')
    self.test_compactblock_construction(self.nodes[0], self.test_node, 1, False)
    sync_blocks(self.nodes)
    self.test_compactblock_construction(self.nodes[1], self.segwit_node, 2, False)
    sync_blocks(self.nodes)
    self.log.info('Testing compactblock requests... ')
    self.test_compactblock_requests(self.nodes[0], self.test_node, 1, False)
    sync_blocks(self.nodes)
    self.test_compactblock_requests(self.nodes[1], self.segwit_node, 2, False)
    sync_blocks(self.nodes)
    self.log.info('Testing getblocktxn requests...')
    self.test_getblocktxn_requests(self.nodes[0], self.test_node, 1)
    sync_blocks(self.nodes)
    self.test_getblocktxn_requests(self.nodes[1], self.segwit_node, 2)
    sync_blocks(self.nodes)
    self.log.info('Testing getblocktxn handler...')
    self.test_getblocktxn_handler(self.nodes[0], self.test_node, 1)
    sync_blocks(self.nodes)
    self.test_getblocktxn_handler(self.nodes[1], self.segwit_node, 2)
    self.test_getblocktxn_handler(self.nodes[1], self.old_node, 1)
    sync_blocks(self.nodes)
    self.log.info('Testing compactblock requests/announcements not at chain tip...')
    self.test_compactblocks_not_at_tip(self.nodes[0], self.test_node)
    sync_blocks(self.nodes)
    self.test_compactblocks_not_at_tip(self.nodes[1], self.segwit_node)
    self.test_compactblocks_not_at_tip(self.nodes[1], self.old_node)
    sync_blocks(self.nodes)
    self.log.info('Testing handling of incorrect blocktxn responses...')
    self.test_incorrect_blocktxn_response(self.nodes[0], self.test_node, 1)
    sync_blocks(self.nodes)
    self.test_incorrect_blocktxn_response(self.nodes[1], self.segwit_node, 2)
    sync_blocks(self.nodes)
    self.log.info('Testing end-to-end block relay...')
    self.request_cb_announcements(self.test_node, self.nodes[0], 1)
    self.request_cb_announcements(self.old_node, self.nodes[1], 1)
    self.request_cb_announcements(self.segwit_node, self.nodes[1], 2)
    self.test_end_to_end_block_relay(self.nodes[0], [self.segwit_node, self.test_node, self.old_node])
    self.test_end_to_end_block_relay(self.nodes[1], [self.segwit_node, self.test_node, self.old_node])
    self.log.info('Testing handling of invalid compact blocks...')
    self.test_invalid_tx_in_compactblock(self.nodes[0], self.test_node, False)
    self.test_invalid_tx_in_compactblock(self.nodes[1], self.segwit_node, False)
    self.test_invalid_tx_in_compactblock(self.nodes[1], self.old_node, False)
    self.log.info('Testing reconstructing compact blocks from all peers...')
    self.test_compactblock_reconstruction_multiple_peers(self.nodes[1], self.segwit_node, self.old_node)
    sync_blocks(self.nodes)
    self.log.info('Advancing to segwit activation')
    self.activate_segwit(self.nodes[1])
    self.log.info('Running tests, post-segwit activation...')
    self.log.info('Testing compactblock construction...')
    self.test_compactblock_construction(self.nodes[1], self.old_node, 1, True)
    self.test_compactblock_construction(self.nodes[1], self.segwit_node, 2, True)
    sync_blocks(self.nodes)
    self.log.info('Testing compactblock requests (unupgraded node)... ')
    self.test_compactblock_requests(self.nodes[0], self.test_node, 1, True)
    self.log.info('Testing getblocktxn requests (unupgraded node)...')
    self.test_getblocktxn_requests(self.nodes[0], self.test_node, 1)
    self.log.info('Syncing nodes...')
    assert (self.nodes[0].getbestblockhash() != self.nodes[1].getbestblockhash())
    while (self.nodes[0].getblockcount() > self.nodes[1].getblockcount()):
        block_hash = self.nodes[0].getblockhash((self.nodes[1].getblockcount() + 1))
        self.nodes[1].submitblock(self.nodes[0].getblock(block_hash, False))
    assert_equal(self.nodes[0].getbestblockhash(), self.nodes[1].getbestblockhash())
    self.log.info('Testing compactblock requests (segwit node)... ')
    self.test_compactblock_requests(self.nodes[1], self.segwit_node, 2, True)
    self.log.info('Testing getblocktxn requests (segwit node)...')
    self.test_getblocktxn_requests(self.nodes[1], self.segwit_node, 2)
    sync_blocks(self.nodes)
    self.log.info('Testing getblocktxn handler (segwit node should return witnesses)...')
    self.test_getblocktxn_handler(self.nodes[1], self.segwit_node, 2)
    self.test_getblocktxn_handler(self.nodes[1], self.old_node, 1)
    self.log.info('Testing end-to-end block relay...')
    self.request_cb_announcements(self.test_node, self.nodes[0], 1)
    self.request_cb_announcements(self.old_node, self.nodes[1], 1)
    self.request_cb_announcements(self.segwit_node, self.nodes[1], 2)
    self.test_end_to_end_block_relay(self.nodes[1], [self.segwit_node, self.test_node, self.old_node])
    self.log.info('Testing handling of invalid compact blocks...')
    self.test_invalid_tx_in_compactblock(self.nodes[0], self.test_node, False)
    self.test_invalid_tx_in_compactblock(self.nodes[1], self.segwit_node, True)
    self.test_invalid_tx_in_compactblock(self.nodes[1], self.old_node, True)
    self.log.info('Testing invalid index in cmpctblock message...')
    self.test_invalid_cmpctblock_message()
def run_test(self):
    self.test_node = TestNode()
    self.segwit_node = TestNode()
    self.old_node = TestNode()
    connections = []
    connections.append(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], self.test_node))
    connections.append(NodeConn('127.0.0.1', p2p_port(1), self.nodes[1], self.segwit_node, services=(NODE_NETWORK | NODE_WITNESS)))
    connections.append(NodeConn('127.0.0.1', p2p_port(1), self.nodes[1], self.old_node, services=NODE_NETWORK))
    self.test_node.add_connection(connections[0])
    self.segwit_node.add_connection(connections[1])
    self.old_node.add_connection(connections[2])
    NetworkThread().start()
    self.test_node.wait_for_verack()
    self.make_utxos()
    self.log.info('Running tests, pre-segwit activation:')
    self.log.info('Testing SENDCMPCT p2p message... ')
    self.test_sendcmpct(self.nodes[0], self.test_node, 1)
    sync_blocks(self.nodes)
    self.test_sendcmpct(self.nodes[1], self.segwit_node, 2, old_node=self.old_node)
    sync_blocks(self.nodes)
    self.log.info('Testing compactblock construction...')
    self.test_compactblock_construction(self.nodes[0], self.test_node, 1, False)
    sync_blocks(self.nodes)
    self.test_compactblock_construction(self.nodes[1], self.segwit_node, 2, False)
    sync_blocks(self.nodes)
    self.log.info('Testing compactblock requests... ')
    self.test_compactblock_requests(self.nodes[0], self.test_node, 1, False)
    sync_blocks(self.nodes)
    self.test_compactblock_requests(self.nodes[1], self.segwit_node, 2, False)
    sync_blocks(self.nodes)
    self.log.info('Testing getblocktxn requests...')
    self.test_getblocktxn_requests(self.nodes[0], self.test_node, 1)
    sync_blocks(self.nodes)
    self.test_getblocktxn_requests(self.nodes[1], self.segwit_node, 2)
    sync_blocks(self.nodes)
    self.log.info('Testing getblocktxn handler...')
    self.test_getblocktxn_handler(self.nodes[0], self.test_node, 1)
    sync_blocks(self.nodes)
    self.test_getblocktxn_handler(self.nodes[1], self.segwit_node, 2)
    self.test_getblocktxn_handler(self.nodes[1], self.old_node, 1)
    sync_blocks(self.nodes)
    self.log.info('Testing compactblock requests/announcements not at chain tip...')
    self.test_compactblocks_not_at_tip(self.nodes[0], self.test_node)
    sync_blocks(self.nodes)
    self.test_compactblocks_not_at_tip(self.nodes[1], self.segwit_node)
    self.test_compactblocks_not_at_tip(self.nodes[1], self.old_node)
    sync_blocks(self.nodes)
    self.log.info('Testing handling of incorrect blocktxn responses...')
    self.test_incorrect_blocktxn_response(self.nodes[0], self.test_node, 1)
    sync_blocks(self.nodes)
    self.test_incorrect_blocktxn_response(self.nodes[1], self.segwit_node, 2)
    sync_blocks(self.nodes)
    self.log.info('Testing end-to-end block relay...')
    self.request_cb_announcements(self.test_node, self.nodes[0], 1)
    self.request_cb_announcements(self.old_node, self.nodes[1], 1)
    self.request_cb_announcements(self.segwit_node, self.nodes[1], 2)
    self.test_end_to_end_block_relay(self.nodes[0], [self.segwit_node, self.test_node, self.old_node])
    self.test_end_to_end_block_relay(self.nodes[1], [self.segwit_node, self.test_node, self.old_node])
    self.log.info('Testing handling of invalid compact blocks...')
    self.test_invalid_tx_in_compactblock(self.nodes[0], self.test_node, False)
    self.test_invalid_tx_in_compactblock(self.nodes[1], self.segwit_node, False)
    self.test_invalid_tx_in_compactblock(self.nodes[1], self.old_node, False)
    self.log.info('Testing reconstructing compact blocks from all peers...')
    self.test_compactblock_reconstruction_multiple_peers(self.nodes[1], self.segwit_node, self.old_node)
    sync_blocks(self.nodes)
    self.log.info('Advancing to segwit activation')
    self.activate_segwit(self.nodes[1])
    self.log.info('Running tests, post-segwit activation...')
    self.log.info('Testing compactblock construction...')
    self.test_compactblock_construction(self.nodes[1], self.old_node, 1, True)
    self.test_compactblock_construction(self.nodes[1], self.segwit_node, 2, True)
    sync_blocks(self.nodes)
    self.log.info('Testing compactblock requests (unupgraded node)... ')
    self.test_compactblock_requests(self.nodes[0], self.test_node, 1, True)
    self.log.info('Testing getblocktxn requests (unupgraded node)...')
    self.test_getblocktxn_requests(self.nodes[0], self.test_node, 1)
    self.log.info('Syncing nodes...')
    assert (self.nodes[0].getbestblockhash() != self.nodes[1].getbestblockhash())
    while (self.nodes[0].getblockcount() > self.nodes[1].getblockcount()):
        block_hash = self.nodes[0].getblockhash((self.nodes[1].getblockcount() + 1))
        self.nodes[1].submitblock(self.nodes[0].getblock(block_hash, False))
    assert_equal(self.nodes[0].getbestblockhash(), self.nodes[1].getbestblockhash())
    self.log.info('Testing compactblock requests (segwit node)... ')
    self.test_compactblock_requests(self.nodes[1], self.segwit_node, 2, True)
    self.log.info('Testing getblocktxn requests (segwit node)...')
    self.test_getblocktxn_requests(self.nodes[1], self.segwit_node, 2)
    sync_blocks(self.nodes)
    self.log.info('Testing getblocktxn handler (segwit node should return witnesses)...')
    self.test_getblocktxn_handler(self.nodes[1], self.segwit_node, 2)
    self.test_getblocktxn_handler(self.nodes[1], self.old_node, 1)
    self.log.info('Testing end-to-end block relay...')
    self.request_cb_announcements(self.test_node, self.nodes[0], 1)
    self.request_cb_announcements(self.old_node, self.nodes[1], 1)
    self.request_cb_announcements(self.segwit_node, self.nodes[1], 2)
    self.test_end_to_end_block_relay(self.nodes[1], [self.segwit_node, self.test_node, self.old_node])
    self.log.info('Testing handling of invalid compact blocks...')
    self.test_invalid_tx_in_compactblock(self.nodes[0], self.test_node, False)
    self.test_invalid_tx_in_compactblock(self.nodes[1], self.segwit_node, True)
    self.test_invalid_tx_in_compactblock(self.nodes[1], self.old_node, True)
    self.log.info('Testing invalid index in cmpctblock message...')
    self.test_invalid_cmpctblock_message()