eth-stat-syncer

Cache live EVM blockchain stats
git clone git://holbrook.no/eth-stat-syncer.git
Log | Files | Refs

commit 84e89dbfaec7586517c109eb1da54dc8bd841f83
parent a72fcbe9199adad53ceae447ac65c53e08974b22
Author: nolash <dev@holbrook.no>
Date:   Thu,  8 Apr 2021 22:15:57 +0200

Add store to disk

Diffstat:
Meth_stat_syncer/runnable/syncer.py | 8++++++--
Meth_stat_syncer/store.py | 88++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------
2 files changed, 73 insertions(+), 23 deletions(-)

diff --git a/eth_stat_syncer/runnable/syncer.py b/eth_stat_syncer/runnable/syncer.py @@ -18,7 +18,10 @@ from chainlib.eth.connection import EthHTTPConnection from chainlib.eth.block import block_latest # local imports -from eth_stat_syncer.store import GasAggregator +from eth_stat_syncer.store import ( + GasAggregator, + RunStore, + ) logging.basicConfig(level=logging.WARNING) logg = logging.getLogger() @@ -68,7 +71,8 @@ class GasPriceFilter(SyncFilter): def main(): - gas_aggregator = GasAggregator(360) + gas_store = RunStore() + gas_aggregator = GasAggregator(gas_store, 360) gas_filter = GasPriceFilter(chain_spec, gas_aggregator) o = block_latest() diff --git a/eth_stat_syncer/store.py b/eth_stat_syncer/store.py @@ -1,18 +1,42 @@ # standard imports +import os import logging import datetime logg = logging.getLogger().getChild(__name__) + +class RunStore: + + def __init__(self, basedir='/run'): + self.procstore = os.path.join(basedir, 'eth_stat_syncerd') + + + def put(self, o, postfix): + for k in o: + file_path = os.path.join(self.procstore, postfix, k) + d = os.path.dirname(file_path) + try: + os.stat(d) + except FileNotFoundError: + os.makedirs(d) + f = open(file_path, 'w') + f.write(str(o[k])) + f.close() + + class GasAggregator: - def __init__(self, capacity): + def __init__(self, store, capacity): + self.store = store self.avg = 0 self.count = 0 self.timestamp = datetime.datetime.utcnow() self.buffer_cursor = 0 self.buffer_capacity = capacity - self.buffer = [None] * self.buffer_capacity + self.buffer_average = [None] * self.buffer_capacity + self.buffer_high = [None] * self.buffer_capacity + self.buffer_low = [None] * self.buffer_capacity self.initial = False self.aggr = 0 self.local_aggr = 0 @@ -43,10 +67,14 @@ class GasAggregator: self.local_count = 0 if not self.initial: for i in range(self.buffer_capacity): - self.buffer[i] = v + self.buffer_average[i] = v + self.buffer_high[i] = self.local_high + self.buffer_low[i] = self.local_low self.initial = True else: - self.buffer[self.buffer_cursor] = v + self.buffer_average[self.buffer_cursor] = v + self.buffer_low[self.buffer_cursor] = self.local_low + self.buffer_high[self.buffer_cursor] = self.local_high self.buffer_cursor += 1 self.buffer_cursor %= self.buffer_capacity @@ -60,26 +88,44 @@ class GasAggregator: def get(self, n=0): if n == 0: n = self.capacity - - cursor = self.buffer_cursor - aggr = 0 - i = 0 - while i < n: - v = self.buffer[cursor] - aggr += v - cursor -= 1 - if cursor < 0: - cursor = self.buffer_capacity - 1 - i += 1 + + aggrs = { + 'average': 0, + 'low': 0, + 'high': 0, + } + for o in [ + (self.buffer_average, 'average',), + (self.buffer_low, 'low',), + (self.buffer_high, 'high',), + ]: + cursor = self.buffer_cursor + aggr = 0 + i = 0 + while i < n: + v = o[0][cursor] + aggrs[o[1]] += v + cursor -= 1 + if cursor < 0: + cursor = self.buffer_capacity - 1 + i += 1 - return int(aggr / n) + return { + 'average': int(aggrs['average']/ n), + 'high': int(aggrs['high']/ n), + 'low': int(aggrs['low']/ n), + } def block_callback(self, block, tx): logg.info('synced {}'.format(block)) if self.process(): - - v = self.get(10) - logg.info('last 6 average now {}'.format(v)) - v = self.get(360) - logg.info('last 360 average now {}'.format(v)) + last = self.get(1) + self.store.put(last, 'block') + minute = self.get(6) + self.store.put(minute, 'minute') + hour = self.get(360) + self.store.put(hour, 'hour') + self.store.put({ + 'average': int(self.aggr / self.count), + }, 'all')