cache estimated_timestamp
This commit is contained in:
parent
0901f67d89
commit
78bd2da267
2 changed files with 19 additions and 5 deletions
|
@ -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.claim import guess_stream_type
|
||||||
from hub.schema.result import Censor
|
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 import SecondaryDB
|
||||||
from hub.db.common import ResolveResult
|
from hub.db.common import ResolveResult
|
||||||
|
|
||||||
|
|
||||||
class ElasticSyncDB(SecondaryDB):
|
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:
|
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)
|
header = self.prefix_db.header.get(height, deserialize_value=False)
|
||||||
if header:
|
timestamp = int(160.6855883050695 * height) if header else int.from_bytes(header[100:104], byteorder='little')
|
||||||
return int.from_bytes(header[100:104], byteorder='little')
|
self.block_timestamp_cache[height] = timestamp
|
||||||
return int(160.6855883050695 * height)
|
return timestamp
|
||||||
|
|
||||||
def _prepare_claim_metadata(self, claim_hash: bytes, claim: ResolveResult):
|
def _prepare_claim_metadata(self, claim_hash: bytes, claim: ResolveResult):
|
||||||
metadata = self.get_claim_metadata(claim.tx_hash, claim.position)
|
metadata = self.get_claim_metadata(claim.tx_hash, claim.position)
|
||||||
|
|
|
@ -243,6 +243,7 @@ class ElasticSyncService(BlockchainReaderService):
|
||||||
self._advanced = True
|
self._advanced = True
|
||||||
|
|
||||||
def unwind(self):
|
def unwind(self):
|
||||||
|
self.db.block_timestamp_cache.clear()
|
||||||
reverted_block_hash = self.db.block_hashes[-1]
|
reverted_block_hash = self.db.block_hashes[-1]
|
||||||
super().unwind()
|
super().unwind()
|
||||||
packed = self.db.prefix_db.undo.get(len(self.db.tx_counts), reverted_block_hash)
|
packed = self.db.prefix_db.undo.get(len(self.db.tx_counts), reverted_block_hash)
|
||||||
|
|
Loading…
Reference in a new issue