diff --git a/counterpartylib/lib/backend/__init__.py b/counterpartylib/lib/backend/__init__.py index 80fa7ca5e6..93b0ac6800 100644 --- a/counterpartylib/lib/backend/__init__.py +++ b/counterpartylib/lib/backend/__init__.py @@ -245,8 +245,4 @@ def init_mempool_cache(): logger.debug('Mempool cache initialized: {}s for {} transactions'.format(time.time() - start, len(txhash_list) + len(vin_txhash_list))) -def clear_mempool_cache(): - logger.debug('Clear mempool cache') - BACKEND().RAW_TRANSACTIONS_CACHE = {} - # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/counterpartylib/lib/backend/addrindex.py b/counterpartylib/lib/backend/addrindex.py index cbd2761dfd..6f677c71f8 100644 --- a/counterpartylib/lib/backend/addrindex.py +++ b/counterpartylib/lib/backend/addrindex.py @@ -160,8 +160,10 @@ def getrawmempool(): def sendrawtransaction(tx_hex): return rpc('sendrawtransaction', [tx_hex]) - +# TODO: move to __init__.py RAW_TRANSACTIONS_CACHE = {} +RAW_TRANSACTIONS_CACHE_KEYS = [] +RAW_TRANSACTIONS_CACHE_SIZE = 10000 def getrawtransaction_batch(txhash_list, verbose=False): tx_hash_call_id = {} @@ -185,6 +187,11 @@ def getrawtransaction_batch(txhash_list, verbose=False): tx_hex = response['result'] tx_hash = tx_hash_call_id[response['id']] RAW_TRANSACTIONS_CACHE[tx_hash] = tx_hex + RAW_TRANSACTIONS_CACHE_KEYS.append(tx_hash) + while len(RAW_TRANSACTIONS_CACHE_KEYS) > RAW_TRANSACTIONS_CACHE_SIZE: + first_hash = RAW_TRANSACTIONS_CACHE_KEYS[0] + del(RAW_TRANSACTIONS_CACHE[first_hash]) + RAW_TRANSACTIONS_CACHE_KEYS.pop(0) else: raise BackendRPCError('{}'.format(response['error'])) diff --git a/counterpartylib/lib/backend/btcd.py b/counterpartylib/lib/backend/btcd.py index 1df20cb7ce..59dfef2f99 100644 --- a/counterpartylib/lib/backend/btcd.py +++ b/counterpartylib/lib/backend/btcd.py @@ -77,7 +77,7 @@ def get_chunks(l, n): responses += rpc_call(chunk) ''' for query in payload: - responses += rpc_call(query) + responses.append(rpc_call(query)) return responses @@ -111,7 +111,10 @@ def getrawtransaction(tx_hash, verbose=False): def getrawmempool(): return rpc('getrawmempool', []) +# TODO: move to __init__.py RAW_TRANSACTIONS_CACHE = {} +RAW_TRANSACTIONS_CACHE_KEYS = [] +RAW_TRANSACTIONS_CACHE_SIZE = 10000 def getrawtransaction_batch(txhash_list, verbose=False): tx_hash_call_id = {} @@ -130,13 +133,14 @@ def getrawtransaction_batch(txhash_list, verbose=False): if len(payload) > 0: batch_responses = rpc_batch(payload) - for response in batch_responses: - if 'error' not in response or response['error'] is None: - tx_hex = response['result'] - tx_hash = tx_hash_call_id[response['id']] - RAW_TRANSACTIONS_CACHE[tx_hash] = tx_hex - else: - raise BackendRPCError('{}'.format(response['error'])) + for tx_hex in batch_responses: + tx_hash = tx_hex['txid'] + RAW_TRANSACTIONS_CACHE[tx_hash] = tx_hex + RAW_TRANSACTIONS_CACHE_KEYS.append(tx_hash) + while len(RAW_TRANSACTIONS_CACHE_KEYS) > RAW_TRANSACTIONS_CACHE_SIZE: + first_hash = RAW_TRANSACTIONS_CACHE_KEYS[0] + del(RAW_TRANSACTIONS_CACHE[first_hash]) + RAW_TRANSACTIONS_CACHE_KEYS.pop(0) result = {} for tx_hash in txhash_list: diff --git a/counterpartylib/lib/blocks.py b/counterpartylib/lib/blocks.py index 3a75018cf7..501c348608 100644 --- a/counterpartylib/lib/blocks.py +++ b/counterpartylib/lib/blocks.py @@ -1012,7 +1012,6 @@ def follow(db): # When newly caught up, check for conservation of assets. if block_index == block_count: - backend.clear_mempool_cache() if config.CHECK_ASSET_CONSERVATION: check.asset_conservation(db)