From 53b65eda74d8956e824b9fd370eac170f4d182c3 Mon Sep 17 00:00:00 2001 From: 4tochka Date: Mon, 13 May 2019 11:16:50 +0400 Subject: [PATCH] connector --- pybtc/connector/connector.py | 8 +++--- pybtc/connector/utxo.py | 49 ++++++++++++++++++------------------ 2 files changed, 28 insertions(+), 29 deletions(-) diff --git a/pybtc/connector/connector.py b/pybtc/connector/connector.py index 562edb1..f0ec294 100644 --- a/pybtc/connector/connector.py +++ b/pybtc/connector/connector.py @@ -77,7 +77,6 @@ class Connector: self.tx_processing_time = 0 self.non_cached_blocks = 0 self.total_received_tx_time = 0 - self.checkpoints = deque() self.tt = 0 self.yy = 0 self.start_time = time.time() @@ -374,10 +373,11 @@ class Connector: self.block_headers_cache.set(block["hash"], block["height"]) self.last_block_height = block["height"] if self.utxo_data: - await self.utxo.destroy_utxo() - try: self.checkpoints.append(block["checkpoint"]) + try: self.utxo.checkpoints.append(block["checkpoint"]) except: pass - if len(self.utxo.cached) > self.utxo.size_limit and not self.utxo.save_process: + if len(self.utxo.cached) > self.utxo.size_limit and \ + not self.utxo.save_process and \ + self.utxo.checkpoints: self.loop.create_task(self.utxo.save_utxo()) diff --git a/pybtc/connector/utxo.py b/pybtc/connector/utxo.py index 85d94c0..ada5531 100644 --- a/pybtc/connector/utxo.py +++ b/pybtc/connector/utxo.py @@ -8,8 +8,8 @@ class UTXO(): def __init__(self, db_pool, loop, log, cache_size): self.cached = LRU() self.missed = set() - self.destroyed = deque() self.deleted = LRU() + self.checkpoints = deque() self.log = log self.loaded = OrderedDict() self.maturity = 100 @@ -40,44 +40,48 @@ class UTXO(): def remove(self, outpoint): del self.cached[outpoint] - async def destroy_utxo(self): - while self.destroyed: - outpoint = self.destroyed.pop() - try: - del self.cached[outpoint] - self.destroyed_utxo += 1 - except: - try: - del self.loaded[outpoint] - self.destroyed_utxo += 1 - except: - self.destroyed_utxo += 1 - pass - async def save_utxo(self): # save to db tail from cache - if self.save_process or not self.cached: - return + if self.save_process or not self.cached: return + if not self.checkpoints: return + self.save_process = True try: + checkpoint = self.checkpoints.pop() lb = 0 block_changed = False + checkpoint_found = False utxo = set() - # self.log.critical(">>" + str(len(self.cached))) while self.cached: i = self.cached.pop() if lb != i[1][0] >> 42: block_changed = True lb = i[1][0] >> 42 + if lb - 1 == checkpoint: + checkpoint_found = True + if len(self.cached) <= self.size_limit: - if block_changed: + if block_changed and checkpoint_found: break utxo.add((i[0],b"".join((int_to_c_int(i[1][0]), int_to_c_int(i[1][1]), i[1][2])))) if block_changed: self.cached.append({i[0]: i[1]}) + if not checkpoint_found: + for i in reversed(utxo): + d = i[1] + pointer = c_int_to_int(d) + f = c_int_len(pointer) + amount = c_int_to_int(d[f:]) + f += c_int_len(amount) + address = d[f:] + self.cached.append({i[0]: (pointer, amount, address)}) + self.log.critical("checkpoint not found") + return + self.log.critical("found checkpoint " + str(lb)) + # self.log.critical(">" + str(len(self.cached))) # # block_height @@ -119,7 +123,7 @@ class UTXO(): "WHERE name = 'last_block';", lb) self.saved_utxo += len(utxo) self.deleted_utxo += len(d) - + self.log.critical("saved " + str(len(utxo))) # # remove from cache # for key in a: # try: @@ -140,11 +144,6 @@ class UTXO(): self._requests += 1 try: i = self.cached.delete(key) - # self.destroyed.append(key) - # try: - # self.destroyed[block_height].add(key) - # except: - # self.destroyed[block_height] = {key} self._hit += 1 return i except: