sync.py (2446B)
1 # standard imports 2 import logging 3 import sys 4 5 # external imports 6 from chainlib.chain import ChainSpec 7 from chainlib.eth.connection import EthHTTPConnection 8 from chainsyncer.backend.memory import MemBackend 9 from chainlib.eth.block import block_latest 10 from chainsyncer.driver import HistorySyncer 11 12 # local imports 13 from taint import Tainter 14 from taint.store.file import FileStore 15 from taint.account import Account 16 from taint.cache import CacheSyncBackend 17 from taint.sync.eth import EthCacheSyncer 18 19 logging.basicConfig(level=logging.INFO) 20 logging.getLogger('taint.cache').setLevel(logging.DEBUG) 21 logg = logging.getLogger() 22 23 store_dir = '/home/lash/tmp/storedir' 24 25 store = FileStore(store_dir) 26 27 chain_spec = ChainSpec('evm', 'foo', 42, 'bar') 28 29 ifc = Tainter(chain_spec, 8000, store=store) 30 31 rpc = EthHTTPConnection('http://localhost:8545') 32 33 #o = block_latest() 34 #stop = rpc.do(o) 35 start = 12423955 36 start -= 1 37 #stop = 12424184 38 stop = start 39 stop += 1 40 syncer_backend = MemBackend(chain_spec, None, target_block=stop) 41 syncer_backend.set(start, 0) 42 43 44 class MonitorFilter: 45 46 def __init__(self, name='sync'): 47 self.name = name 48 49 50 def tick(self, block_number, tx_index): 51 s = '{} sync block {} tx {}'.format(self.name, block_number, tx_index) 52 sys.stdout.write('{:<100s}\r'.format(s)) 53 54 55 def filter(self, rpc, block, tx, session=None): 56 self.tick(block.number, tx.index) 57 58 59 class MatchFilter: 60 61 def filter(self, rpc, block, tx, session=None): 62 logg.info('>>>>>>>>>>>>>>>>> block {} tx {}'.format(block, tx)) 63 64 65 66 def block_monitor(block, Tx=None): 67 s = 'sync block {} ({} txs)'.format(block.number, len(block.txs)) 68 sys.stdout.write('{:<100s}\r'.format(s)) 69 70 syncer = HistorySyncer(syncer_backend, block_callback=block_monitor) 71 72 account = Account(chain_spec, bytes.fromhex('60c2fb18578665eb92636e7727e54e6b7c75f7ed'), tags=[b'foo']) 73 ifc.add_subject(account) 74 75 account = Account(chain_spec, bytes.fromhex('2bd15ebe0fac6831a9d12190a599385bee5515ad'), tags=[b'bar']) 76 ifc.add_object(account) 77 78 syncer.add_filter(MonitorFilter()) 79 syncer.add_filter(ifc) 80 81 syncer.loop(0, rpc) 82 83 ifc.save() 84 85 for a in ifc.subjects.values(): 86 print(str(a)) 87 88 for a in ifc.objects.values(): 89 print(str(a)) 90 91 cache_backend = CacheSyncBackend(ifc, chain_spec, None, start_block=start, target_block=stop, tick_callback=MonitorFilter(name='cache').tick) 92 syncer = EthCacheSyncer(cache_backend) 93 syncer.add_filter(MatchFilter()) 94 syncer.loop(0, rpc)