2021-05-18 19:13:57 +02:00
|
|
|
# standard imports
|
|
|
|
import logging
|
|
|
|
import json
|
|
|
|
import re
|
|
|
|
import base64
|
|
|
|
|
2021-07-10 17:46:14 +02:00
|
|
|
# external imports
|
|
|
|
from hexathon import add_0x
|
|
|
|
|
2021-05-18 19:13:57 +02:00
|
|
|
# local imports
|
|
|
|
from cic_cache.cache import (
|
|
|
|
BloomCache,
|
|
|
|
DataCache,
|
|
|
|
)
|
|
|
|
|
|
|
|
logg = logging.getLogger(__name__)
|
2021-07-10 17:46:14 +02:00
|
|
|
#logg = logging.getLogger()
|
2021-05-18 19:13:57 +02:00
|
|
|
|
|
|
|
re_transactions_all_bloom = r'/tx/(\d+)?/?(\d+)/?'
|
2021-07-10 17:46:14 +02:00
|
|
|
re_transactions_account_bloom = r'/tx/user/((0x)?[a-fA-F0-9]+)(/(\d+)(/(\d+))?)?/?'
|
|
|
|
re_transactions_all_data = r'/txa/(\d+)?/?(\d+)/?'
|
2021-05-18 19:13:57 +02:00
|
|
|
|
|
|
|
DEFAULT_LIMIT = 100
|
|
|
|
|
|
|
|
|
|
|
|
def process_transactions_account_bloom(session, env):
|
|
|
|
r = re.match(re_transactions_account_bloom, env.get('PATH_INFO'))
|
|
|
|
if not r:
|
|
|
|
return None
|
|
|
|
|
|
|
|
address = r[1]
|
|
|
|
if r[2] == None:
|
2021-07-10 17:46:14 +02:00
|
|
|
address = add_0x(address)
|
|
|
|
offset = 0
|
2021-05-18 19:13:57 +02:00
|
|
|
if r.lastindex > 2:
|
2021-07-10 17:46:14 +02:00
|
|
|
offset = r[4]
|
|
|
|
limit = DEFAULT_LIMIT
|
|
|
|
if r.lastindex > 4:
|
|
|
|
limit = r[6]
|
2021-05-18 19:13:57 +02:00
|
|
|
|
|
|
|
c = BloomCache(session)
|
|
|
|
(lowest_block, highest_block, bloom_filter_block, bloom_filter_tx) = c.load_transactions_account(address, offset, limit)
|
|
|
|
|
|
|
|
o = {
|
|
|
|
'alg': 'sha256',
|
|
|
|
'low': lowest_block,
|
|
|
|
'high': highest_block,
|
|
|
|
'block_filter': base64.b64encode(bloom_filter_block).decode('utf-8'),
|
|
|
|
'blocktx_filter': base64.b64encode(bloom_filter_tx).decode('utf-8'),
|
|
|
|
'filter_rounds': 3,
|
|
|
|
}
|
|
|
|
|
|
|
|
j = json.dumps(o)
|
|
|
|
|
|
|
|
return ('application/json', j.encode('utf-8'),)
|
|
|
|
|
|
|
|
|
|
|
|
def process_transactions_all_bloom(session, env):
|
|
|
|
r = re.match(re_transactions_all_bloom, env.get('PATH_INFO'))
|
|
|
|
if not r:
|
|
|
|
return None
|
|
|
|
|
|
|
|
offset = DEFAULT_LIMIT
|
|
|
|
if r.lastindex > 0:
|
|
|
|
offset = r[1]
|
|
|
|
limit = 0
|
|
|
|
if r.lastindex > 1:
|
|
|
|
limit = r[2]
|
|
|
|
|
|
|
|
c = BloomCache(session)
|
|
|
|
(lowest_block, highest_block, bloom_filter_block, bloom_filter_tx) = c.load_transactions(offset, limit)
|
|
|
|
|
|
|
|
o = {
|
|
|
|
'alg': 'sha256',
|
|
|
|
'low': lowest_block,
|
|
|
|
'high': highest_block,
|
|
|
|
'block_filter': base64.b64encode(bloom_filter_block).decode('utf-8'),
|
|
|
|
'blocktx_filter': base64.b64encode(bloom_filter_tx).decode('utf-8'),
|
|
|
|
'filter_rounds': 3,
|
|
|
|
}
|
|
|
|
|
|
|
|
j = json.dumps(o)
|
|
|
|
|
|
|
|
return ('application/json', j.encode('utf-8'),)
|
|
|
|
|
|
|
|
|
|
|
|
def process_transactions_all_data(session, env):
|
|
|
|
r = re.match(re_transactions_all_data, env.get('PATH_INFO'))
|
|
|
|
if not r:
|
|
|
|
return None
|
|
|
|
if env.get('HTTP_X_CIC_CACHE_MODE') != 'all':
|
|
|
|
return None
|
|
|
|
|
2021-07-25 12:19:22 +02:00
|
|
|
logg.debug('got data request {}'.format(env))
|
|
|
|
block_offset = r[1]
|
|
|
|
block_end = r[2]
|
2021-06-02 17:11:15 +02:00
|
|
|
if int(r[2]) < int(r[1]):
|
2021-05-18 19:13:57 +02:00
|
|
|
raise ValueError('cart before the horse, dude')
|
|
|
|
|
|
|
|
c = DataCache(session)
|
2021-07-25 15:07:28 +02:00
|
|
|
(lowest_block, highest_block, tx_cache) = c.load_transactions_with_data(0, 0, block_offset, block_end, oldest=True) # oldest needs to be settable
|
2021-05-18 19:13:57 +02:00
|
|
|
|
|
|
|
for r in tx_cache:
|
|
|
|
r['date_block'] = r['date_block'].timestamp()
|
|
|
|
|
|
|
|
o = {
|
|
|
|
'low': lowest_block,
|
|
|
|
'high': highest_block,
|
|
|
|
'data': tx_cache,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
j = json.dumps(o)
|
|
|
|
|
|
|
|
return ('application/json', j.encode('utf-8'),)
|