From 837f7d428a7f21c1bdfbc76be57c4b38ae841fcd Mon Sep 17 00:00:00 2001 From: Sergi Delgado Segura Date: Tue, 2 Jun 2020 19:49:50 +0200 Subject: [PATCH] watcher - Fixes cache setup for regtest Cache setup in regtest was raising some warnings due to the blocks not being found --- teos/watcher.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/teos/watcher.py b/teos/watcher.py index de16500..3d7120d 100644 --- a/teos/watcher.py +++ b/teos/watcher.py @@ -61,14 +61,19 @@ class LocatorCache: # Not doing so implies store temporary variables in the Watcher and initialising the cache as None. target_block_hash = last_known_block for _ in range(self.cache_size): - target_block = block_processor.get_block(target_block_hash) - # In some setups, like regtest, it could be the case that there are no enough previous blocks. - if target_block: - locators = {compute_locator(txid): txid for txid in target_block.get("tx")} - self.cache.update(locators) - self.blocks[target_block_hash] = locators - target_block_hash = target_block.get("previousblockhash") + # In those cases we pull as many as we can. + if target_block_hash: + target_block = block_processor.get_block(target_block_hash) + if not target_block: + break + else: + break + + locators = {compute_locator(txid): txid for txid in target_block.get("tx")} + self.cache.update(locators) + self.blocks[target_block_hash] = locators + target_block_hash = target_block.get("previousblockhash") self.blocks = OrderedDict(reversed((list(self.blocks.items()))))