From 78bd2da267fddf3cb5948b89f16e8bd912c5e549 Mon Sep 17 00:00:00 2001 From: Jack Robison Date: Fri, 27 May 2022 10:05:13 -0400 Subject: [PATCH] cache estimated_timestamp --- hub/elastic_sync/db.py | 23 ++++++++++++++++++----- hub/elastic_sync/service.py | 1 + 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/hub/elastic_sync/db.py b/hub/elastic_sync/db.py index 96ce8fa..4081d24 100644 --- a/hub/elastic_sync/db.py +++ b/hub/elastic_sync/db.py @@ -1,17 +1,30 @@ -from typing import Optional, Set, Dict +from typing import Optional, Set, Dict, List +from concurrent.futures.thread import ThreadPoolExecutor from hub.schema.claim import guess_stream_type from hub.schema.result import Censor -from hub.common import hash160, STREAM_TYPES, CLAIM_TYPES +from hub.common import hash160, STREAM_TYPES, CLAIM_TYPES, LRUCache from hub.db import SecondaryDB from hub.db.common import ResolveResult class ElasticSyncDB(SecondaryDB): + def __init__(self, coin, db_dir: str, secondary_name: str, max_open_files: int = -1, reorg_limit: int = 200, + cache_all_claim_txos: bool = False, cache_all_tx_hashes: bool = False, + blocking_channel_ids: List[str] = None, + filtering_channel_ids: List[str] = None, executor: ThreadPoolExecutor = None, + index_address_status=False): + super().__init__(coin, db_dir, secondary_name, max_open_files, reorg_limit, cache_all_claim_txos, + cache_all_tx_hashes, blocking_channel_ids, filtering_channel_ids, executor, + index_address_status) + self.block_timestamp_cache = LRUCache(1024) + def estimate_timestamp(self, height: int) -> int: + if height in self.block_timestamp_cache: + return self.block_timestamp_cache[height] header = self.prefix_db.header.get(height, deserialize_value=False) - if header: - return int.from_bytes(header[100:104], byteorder='little') - return int(160.6855883050695 * height) + timestamp = int(160.6855883050695 * height) if header else int.from_bytes(header[100:104], byteorder='little') + self.block_timestamp_cache[height] = timestamp + return timestamp def _prepare_claim_metadata(self, claim_hash: bytes, claim: ResolveResult): metadata = self.get_claim_metadata(claim.tx_hash, claim.position) diff --git a/hub/elastic_sync/service.py b/hub/elastic_sync/service.py index 4a09451..c3b4074 100644 --- a/hub/elastic_sync/service.py +++ b/hub/elastic_sync/service.py @@ -243,6 +243,7 @@ class ElasticSyncService(BlockchainReaderService): self._advanced = True def unwind(self): + self.db.block_timestamp_cache.clear() reverted_block_hash = self.db.block_hashes[-1] super().unwind() packed = self.db.prefix_db.undo.get(len(self.db.tx_counts), reverted_block_hash)