Skip to content

Commit

Permalink
Fix BTCD wrapper and clean correctly raw transactions cache (FIFO)
Browse files Browse the repository at this point in the history
  • Loading branch information
ouziel-slama committed Apr 20, 2015
1 parent 3f874ed commit bb3e0ce
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
4 changes: 0 additions & 4 deletions counterpartylib/lib/backend/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 8 additions & 1 deletion counterpartylib/lib/backend/addrindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand All @@ -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']))

Expand Down
20 changes: 12 additions & 8 deletions counterpartylib/lib/backend/btcd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 = {}
Expand All @@ -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:
Expand Down
1 change: 0 additions & 1 deletion counterpartylib/lib/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit bb3e0ce

Please sign in to comment.