From 1b0b1a1fcb5609168def4aa2e15f0c0a2f4659b5 Mon Sep 17 00:00:00 2001 From: Cryp Toon Date: Sat, 23 Nov 2024 19:09:03 +0100 Subject: [PATCH] Avoid problems wrong blockcount at service providers --- bitcoinlib/services/services.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/bitcoinlib/services/services.py b/bitcoinlib/services/services.py index 42e0709c..3a1dcab5 100644 --- a/bitcoinlib/services/services.py +++ b/bitcoinlib/services/services.py @@ -508,11 +508,18 @@ def blockcount(self): current_timestamp = time.time() if self._blockcount_update < current_timestamp - BLOCK_COUNT_CACHE_TIME: new_count = self._provider_execute('blockcount') - if not self._blockcount or (new_count and new_count > self._blockcount): + if last_cache_blockcount > new_count: + _logger.warning(f"New block count ({new_count}) is lower than block count in cache " + f"({last_cache_blockcount}). Will try to find provider consensus") + blockcounts = [last_cache_blockcount] + for _ in range(5): + blockcounts.append(self._provider_execute('blockcount')) + # return third last blockcount in list, assume last 2 and first 3 could be wrong + self._blockcount = sorted(blockcounts)[-2] + self._blockcount_update = current_timestamp + elif not self._blockcount or (new_count and new_count > self._blockcount): self._blockcount = new_count self._blockcount_update = current_timestamp - if last_cache_blockcount > self._blockcount: - return last_cache_blockcount # Store result in cache if len(self.results) and list(self.results.keys())[0] != 'caching': self.cache.store_blockcount(self._blockcount)