From 5ec4a88c35e9e04c303cd83d56b228b417311524 Mon Sep 17 00:00:00 2001 From: Jack Robison Date: Tue, 25 Feb 2020 14:17:52 -0500 Subject: [PATCH] block hash lru cache --- lbry/wallet/server/daemon.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lbry/wallet/server/daemon.py b/lbry/wallet/server/daemon.py index 28c0d686f..493183a08 100644 --- a/lbry/wallet/server/daemon.py +++ b/lbry/wallet/server/daemon.py @@ -3,6 +3,7 @@ import itertools import json import time from functools import wraps +from pylru import lrucache import aiohttp @@ -42,6 +43,7 @@ class Daemon: self._height = None self.available_rpcs = {} self.connector = aiohttp.TCPConnector() + self._block_hash_cache = lrucache(1000000) async def close(self): if self.connector: @@ -219,9 +221,20 @@ class Daemon: async def block_hex_hashes(self, first, count): """Return the hex hashes of count block starting at height first.""" + if first + count < (self.cached_height() or 0) - 200: + return await self._cached_block_hex_hashes(first, count) params_iterable = ((h, ) for h in range(first, first + count)) return await self._send_vector('getblockhash', params_iterable) + async def _cached_block_hex_hashes(self, first, count): + """Return the hex hashes of count block starting at height first.""" + cached = self._block_hash_cache.get((first, count)) + if cached: + return cached + params_iterable = ((h, ) for h in range(first, first + count)) + self._block_hash_cache[(first, count)] = await self._send_vector('getblockhash', params_iterable) + return self._block_hash_cache[(first, count)] + async def deserialised_block(self, hex_hash): """Return the deserialised block with the given hex hash.""" return await self._send_single('getblock', (hex_hash, True))