Exemplo n.º 1
0
    def __init__(self, name: str, channel_manager, peer_id, channel_name,
                 level_db_identity):
        self.__channel_service: ChannelService = channel_manager
        self.__channel_name = channel_name
        self.__pre_validate_strategy = self.__pre_validate
        self.__peer_id = peer_id
        self.__level_db = None
        self.__level_db_path = ""
        self.__level_db, self.__level_db_path = util.init_level_db(
            level_db_identity=f"{level_db_identity}_{channel_name}",
            allow_rename_path=False)
        self.__txQueue = AgingCache(
            max_age_seconds=conf.MAX_TX_QUEUE_AGING_SECONDS,
            default_item_status=TransactionStatusInQueue.normal)
        self.__unconfirmedBlockQueue = queue.Queue()
        self.__blockchain = BlockChain(self.__level_db, channel_name)
        self.__peer_type = None
        self.__consensus = None
        self.__consensus_algorithm = None
        self.candidate_blocks = CandidateBlocks()
        self.__block_height_sync_lock = threading.Lock()
        self.__block_height_thread_pool = ThreadPoolExecutor(
            1, 'BlockHeightSyncThread')
        self.__block_height_future: Future = None
        self.__subscribe_target_peer_stub = None
        self.__block_generation_scheduler = BlockGenerationScheduler(
            self.__channel_name)
        self.__precommit_block: Block = None
        self.set_peer_type(loopchain_pb2.PEER)
        self.name = name
        self.__service_status = status_code.Service.online

        self.epoch: Epoch = None
Exemplo n.º 2
0
    def __init__(self, channel_service: 'ChannelService', peer_id: str,
                 channel_name: str, store_id: str):
        self.__channel_service: ChannelService = channel_service
        self.__channel_name = channel_name
        self.__peer_id = peer_id

        self.__tx_queue = AgingCache(
            max_age_seconds=conf.MAX_TX_QUEUE_AGING_SECONDS,
            default_item_status=TransactionStatusInQueue.normal)
        self.blockchain = BlockChain(channel_name, store_id, self)
        self.__peer_type = None
        self.__consensus_algorithm = None
        self.candidate_blocks = CandidateBlocks(self.blockchain)
        self.__block_height_sync_bad_targets = {}
        self.__block_height_sync_lock = threading.Lock()
        self.__block_height_thread_pool: ThreadPoolExecutor = ThreadPoolExecutor(
            1, 'BlockHeightSyncThread')
        self.__block_height_future: Future = None
        self.set_peer_type(loopchain_pb2.PEER)
        self.__service_status = status_code.Service.online

        # old_block_hashes[height][new_block_hash] = old_block_hash
        self.__old_block_hashes: DefaultDict[int,
                                             Dict[Hash32,
                                                  Hash32]] = defaultdict(dict)
        self.epoch: Epoch = None
Exemplo n.º 3
0
    def __init__(self,
                 channel_service: 'ChannelService',
                 channel: str = None,
                 **kwargs):
        CommonThread.__init__(self)
        Publisher.__init__(self, [
            Consensus.EVENT_COMPLETE_CONSENSUS,
            Consensus.EVENT_LEADER_COMPLAIN_F_1,
            Consensus.EVENT_LEADER_COMPLAIN_2F_1, Consensus.EVENT_MAKE_BLOCK
        ])

        self.channel_name = channel
        self.__channel_service = channel_service
        self.__peer_manager = channel_service.peer_manager
        self.__last_epoch: Epoch = None
        self.__precommit_block: Block = None
        self.__epoch: Epoch = None
        self.__leader_id = None
        self.__tx_queue = AgingCache(
            max_age_seconds=conf.MAX_TX_QUEUE_AGING_SECONDS,
            default_item_status=TransactionStatusInQueue.normal)
        self.__sleep_time = None
        self.__run_logic = None
        self.__block_generation_scheduler = BlockGenerationScheduler(
            self.channel_name)

        self.__init_data()
Exemplo n.º 4
0
 def __init__(self, channel_service: 'ChannelService', channel: str=None, **kwargs):
     Publisher.__init__(self, ["complete_consensus", "leader_complain_f_1", "leader_complain_2f_1", "make_block"])
     self.__channel_service = channel_service
     self.__peer_manager = channel_service.peer_manager
     self.channel_name = channel
     self.__last_epoch: Epoch = None
     self.__precommit_block: Block = None
     self.__epoch: Epoch = None
     self.__leader_id = None
     self.__tx_queue = AgingCache(max_age_seconds=conf.MAX_TX_QUEUE_AGING_SECONDS,
                                  default_item_status=TransactionStatusInQueue.normal)
Exemplo n.º 5
0
    def test_aging_cache_getitem(self):
        # GIVEN
        cache = AgingCache(max_age_seconds=5)
        for i in range(10):
            cache[i] = f"value_{i}"
            # util.logger.spam(f"test_cache cache size({len(cache)}) max_size({cache.maxlen})")
        util.logger.spam(f"test_aging_cache_pop cache size({len(cache)}) max_age_seconds({cache.max_age_seconds})")
        self.assertEqual(len(cache), 10)

        # WHEN
        while cache:
            item = cache.pop_item()
            util.logger.spam(f"in cache item({item})")

        # THEN
        self.assertEqual(len(cache), 0)
Exemplo n.º 6
0
    def test_aging_cache_pop(self):
        # GIVEN
        cache = AgingCache(max_age_seconds=5)
        for i in range(10):
            cache[i] = f"value_{i}"
            # util.logger.spam(f"test_cache cache size({len(cache)}) max_size({cache.maxlen})")
        util.logger.spam(f"test_aging_cache_pop cache size({len(cache)}) max_age_seconds({cache.max_age_seconds})")
        self.assertGreaterEqual(len(cache), 10)

        # WHEN
        for i in range(10):
            # util.logger.spam(f"in loop cache({cache}))")
            if i in cache:
                cache.pop(i)

        # THEN
        self.assertEqual(len(cache), 0)
Exemplo n.º 7
0
    def test_aging_cache_set_get_status(self):
        # GIVEN
        cache = AgingCache(max_age_seconds=5)
        for i in range(10):
            cache[i] = f"value_{i}"
            # util.logger.spam(f"test_cache cache size({len(cache)}) max_size({cache.maxlen})")
        util.logger.spam(f"test_aging_cache_pop cache size({len(cache)}) max_age_seconds({cache.max_age_seconds})")
        self.assertGreaterEqual(len(cache), 10)

        # WHEN
        cache.set_item_status(5, "Some Status")
        for i in range(10):
            item_status = cache.get_item_status(i)
            util.logger.spam(f"in cache item({item_status})")

        # THEN
        self.assertEqual(len(cache), 10)
        self.assertEqual(cache.get_item_status(5), "Some Status")
Exemplo n.º 8
0
    def test_aging_cache_pop_item_in_status(self):
        # GIVEN
        cache = AgingCache(max_age_seconds=5)
        for i in range(10):
            cache[i] = f"value_{i}"
            # util.logger.spam(f"test_cache cache size({len(cache)}) max_size({cache.maxlen})")
        util.logger.spam(f"test_aging_cache_pop cache size({len(cache)}) max_age_seconds({cache.max_age_seconds})")
        self.assertGreaterEqual(len(cache), 10)

        # WHEN
        cache.set_item_status(5, "Some Status")
        item_count = 0
        for i in range(10):
            item = cache.pop_item_in_status()
            if item:
                item_count += 1

        # THEN
        self.assertEqual(item_count, 9)
Exemplo n.º 9
0
    def test_aging_cache_get(self):
        # GIVEN
        cache = AgingCache(max_age_seconds=5)
        for i in range(10):
            cache[i] = f"value_{i}"
            # util.logger.spam(f"test_cache cache size({len(cache)}) max_size({cache.maxlen})")
        util.logger.spam(f"test_aging_cache_pop cache size({len(cache)}) max_age_seconds({cache.max_age_seconds})")
        self.assertEqual(len(cache), 10)

        expect_none = cache.get(100, None)
        self.assertIsNone(expect_none)

        # WHEN
        for i in range(10):
            if i in cache:
                item = cache[i]
                util.logger.spam(f"in cache item({item})")

        # THEN
        self.assertEqual(len(cache), 10)
Exemplo n.º 10
0
    def test_aging_cache(self):
        # GIVEN
        cache = AgingCache(max_age_seconds=5)
        for i in range(100):
            cache[i] = f"value_{i}"
            # util.logger.spam(f"test_cache cache size({len(cache)}) max_size({cache.maxlen})")
        util.logger.spam(f"test_aging_cache cache size({len(cache)}) max_age_seconds({cache.max_age_seconds})")

        # WHEN
        for i in list(range(100))[-10:]:
            # util.logger.spam(f"({cache[i]})")
            self.assertTrue(i in cache)

        self.assertGreaterEqual(len(cache), 10)
        time.sleep(6)

        # THEN
        cache["aaa"] = "AAA"
        self.assertEqual(len(cache), 1)
        util.logger.spam(f"after test_cache cache size({len(cache)}) max_age_seconds({cache.max_age_seconds})")
Exemplo n.º 11
0
    def test_aging_cache_get_item_in_status(self):
        # GIVEN
        cache = AgingCache(max_age_seconds=5)
        for i in range(10):
            cache[i] = f"value_{i}"
            # util.logger.spam(f"test_cache cache size({len(cache)}) max_size({cache.maxlen})")
        util.logger.spam(f"test_aging_cache_pop cache size({len(cache)}) max_age_seconds({cache.max_age_seconds})")
        self.assertGreaterEqual(len(cache), 10)

        # WHEN
        for i in range(10):
            item = cache.get_item_in_status(AgingCache.DEFAULT_ITEM_STATUS, "Some Status")
            util.logger.spam(f"item({item})")

        # THEN
        item_count = 0
        for i in range(10):
            item = cache.get_item_in_status(AgingCache.DEFAULT_ITEM_STATUS, "Some Status")
            if item:
                item_count += 1

        self.assertEqual(len(cache), 10)
        self.assertEqual(item_count, 0)
Exemplo n.º 12
0
    def test_aging_cache_set_item_status_by_time(self):
        # GIVEN
        cache = AgingCache(max_age_seconds=5)
        for i in range(10):
            cache[i] = f"value_{i}"
            # util.logger.spam(f"test_cache cache size({len(cache)}) max_size({cache.maxlen})")
        util.logger.spam(f"test_aging_cache_pop cache size({len(cache)}) max_age_seconds({cache.max_age_seconds})")
        self.assertGreaterEqual(len(cache), 10)

        # WHEN
        time.sleep(1)
        cache.set_item_status_by_time(int(time.time()), "timeout")
        for i in range(10, 20):
            cache[i] = f"value_{i}"

        # THEN
        timeout_count = 0
        for i in range(20):
            item_status = cache.get_item_status(i)
            if item_status == "timeout":
                timeout_count += 1
            # util.logger.spam(f"in cache item({item_status})")

        self.assertEqual(timeout_count, 10)
Exemplo n.º 13
0
 def get_tx_queue(self):
     return AgingCache(max_age_seconds=10)