def run(self):
    self.wait_for_verack()
    test_number = 1
    for test_instance in self.test_generator.get_tests():
        [block, block_outcome, tip] = [None, None, None]
        [tx, tx_outcome] = [None, None]
        invqueue = []
        for test_obj in test_instance.blocks_and_transactions:
            b_or_t = test_obj[0]
            outcome = test_obj[1]
            if isinstance(b_or_t, CBlock):
                block = b_or_t
                block_outcome = outcome
                tip = block.sha256
                if (len(test_obj) >= 3):
                    tip = test_obj[2]
                first_block_with_hash = True
                if (self.block_store.get(block.sha256) is not None):
                    first_block_with_hash = False
                with mininode_lock:
                    self.block_store.add_block(block)
                    for c in self.connections:
                        if (first_block_with_hash and (block.sha256 in c.cb.block_request_map) and (c.cb.block_request_map[block.sha256] == True)):
                            c.send_message(msg_block(block))
                        else:
                            c.cb.block_request_map[block.sha256] = False
                if test_instance.sync_every_block:
                    if (outcome == True):
                        [c.cb.send_inv(block) for c in self.connections]
                        self.sync_blocks(block.sha256, 1)
                    else:
                        [c.send_message(msg_block(block)) for c in self.connections]
                        [c.cb.send_ping(self.ping_counter) for c in self.connections]
                        self.wait_for_pings(self.ping_counter, timeout=300)
                        self.ping_counter += 1
                    if (not self.check_results(tip, outcome)):
                        raise AssertionError(('Test failed at test %d' % test_number))
                else:
                    block_header = CBlockHeader(block)
                    [c.cb.send_header(block_header) for c in self.connections]
            elif isinstance(b_or_t, CBlockHeader):
                block_header = b_or_t
                self.block_store.add_header(block_header)
                [c.cb.send_header(block_header) for c in self.connections]
            else:
                assert isinstance(b_or_t, CTransaction)
                tx = b_or_t
                tx_outcome = outcome
                with mininode_lock:
                    self.tx_store.add_transaction(tx)
                    for c in self.connections:
                        c.cb.tx_request_map[tx.sha256] = False
                if test_instance.sync_every_tx:
                    [c.cb.send_inv(tx) for c in self.connections]
                    self.sync_transaction(tx.sha256, 1)
                    if (not self.check_mempool(tx.sha256, outcome)):
                        raise AssertionError(('Test failed at test %d' % test_number))
                else:
                    invqueue.append(CInv(1, tx.sha256))
            if (len(invqueue) == MAX_INV_SZ):
                [c.send_message(msg_inv(invqueue)) for c in self.connections]
                invqueue = []
        if ((not test_instance.sync_every_block) and (block is not None)):
            if (len(invqueue) > 0):
                [c.send_message(msg_inv(invqueue)) for c in self.connections]
                invqueue = []
            self.sync_blocks(block.sha256, len(test_instance.blocks_and_transactions))
            if (not self.check_results(tip, block_outcome)):
                raise AssertionError(('Block test failed at test %d' % test_number))
        if ((not test_instance.sync_every_tx) and (tx is not None)):
            if (len(invqueue) > 0):
                [c.send_message(msg_inv(invqueue)) for c in self.connections]
                invqueue = []
            self.sync_transaction(tx.sha256, len(test_instance.blocks_and_transactions))
            if (not self.check_mempool(tx.sha256, tx_outcome)):
                raise AssertionError(('Mempool test failed at test %d' % test_number))
        logger.info(('Test %d: PASS' % test_number))
        test_number += 1
    [c.disconnect_node() for c in self.connections]
    self.wait_for_disconnections()
    self.block_store.close()
    self.tx_store.close()
def run(self):
    self.wait_for_verack()
    test_number = 1
    for test_instance in self.test_generator.get_tests():
        [block, block_outcome, tip] = [None, None, None]
        [tx, tx_outcome] = [None, None]
        invqueue = []
        for test_obj in test_instance.blocks_and_transactions:
            b_or_t = test_obj[0]
            outcome = test_obj[1]
            if isinstance(b_or_t, CBlock):
                block = b_or_t
                block_outcome = outcome
                tip = block.sha256
                if (len(test_obj) >= 3):
                    tip = test_obj[2]
                first_block_with_hash = True
                if (self.block_store.get(block.sha256) is not None):
                    first_block_with_hash = False
                with mininode_lock:
                    self.block_store.add_block(block)
                    for c in self.connections:
                        if (first_block_with_hash and (block.sha256 in c.cb.block_request_map) and (c.cb.block_request_map[block.sha256] == True)):
                            c.send_message(msg_block(block))
                        else:
                            c.cb.block_request_map[block.sha256] = False
                if test_instance.sync_every_block:
                    if (outcome == True):
                        [c.cb.send_inv(block) for c in self.connections]
                        self.sync_blocks(block.sha256, 1)
                    else:
                        [c.send_message(msg_block(block)) for c in self.connections]
                        [c.cb.send_ping(self.ping_counter) for c in self.connections]
                        self.wait_for_pings(self.ping_counter, timeout=300)
                        self.ping_counter += 1
                    if (not self.check_results(tip, outcome)):
                        raise AssertionError(('Test failed at test %d' % test_number))
                else:
                    block_header = CBlockHeader(block)
                    [c.cb.send_header(block_header) for c in self.connections]
            elif isinstance(b_or_t, CBlockHeader):
                block_header = b_or_t
                self.block_store.add_header(block_header)
                [c.cb.send_header(block_header) for c in self.connections]
            else:
                assert isinstance(b_or_t, CTransaction)
                tx = b_or_t
                tx_outcome = outcome
                with mininode_lock:
                    self.tx_store.add_transaction(tx)
                    for c in self.connections:
                        c.cb.tx_request_map[tx.sha256] = False
                if test_instance.sync_every_tx:
                    [c.cb.send_inv(tx) for c in self.connections]
                    self.sync_transaction(tx.sha256, 1)
                    if (not self.check_mempool(tx.sha256, outcome)):
                        raise AssertionError(('Test failed at test %d' % test_number))
                else:
                    invqueue.append(CInv(1, tx.sha256))
            if (len(invqueue) == MAX_INV_SZ):
                [c.send_message(msg_inv(invqueue)) for c in self.connections]
                invqueue = []
        if ((not test_instance.sync_every_block) and (block is not None)):
            if (len(invqueue) > 0):
                [c.send_message(msg_inv(invqueue)) for c in self.connections]
                invqueue = []
            self.sync_blocks(block.sha256, len(test_instance.blocks_and_transactions))
            if (not self.check_results(tip, block_outcome)):
                raise AssertionError(('Block test failed at test %d' % test_number))
        if ((not test_instance.sync_every_tx) and (tx is not None)):
            if (len(invqueue) > 0):
                [c.send_message(msg_inv(invqueue)) for c in self.connections]
                invqueue = []
            self.sync_transaction(tx.sha256, len(test_instance.blocks_and_transactions))
            if (not self.check_mempool(tx.sha256, tx_outcome)):
                raise AssertionError(('Mempool test failed at test %d' % test_number))
        logger.info(('Test %d: PASS' % test_number))
        test_number += 1
    [c.disconnect_node() for c in self.connections]
    self.wait_for_disconnections()
    self.block_store.close()
    self.tx_store.close()