Change progress callback signature order

This commit is contained in:
nolash 2021-02-20 21:11:09 +01:00
parent b7520ad951
commit 82e2674555
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
5 changed files with 28 additions and 15 deletions

View File

@ -209,6 +209,15 @@ class MemBackend:
self.chain_spec = chain_spec self.chain_spec = chain_spec
self.block_height = 0 self.block_height = 0
self.tx_height = 0 self.tx_height = 0
self.db_session = None
def connect(self):
pass
def disconnect(self):
pass
def set(self, block_height, tx_height): def set(self, block_height, tx_height):

View File

@ -15,7 +15,7 @@ from chainsyncer.filter import SyncFilter
logg = logging.getLogger() logg = logging.getLogger()
def noop_progress(s, block_number, tx_index): def noop_callback(block_number, tx_index, s=None):
logg.debug('({},{}) {}'.format(block_number, tx_index, s)) logg.debug('({},{}) {}'.format(block_number, tx_index, s))
@ -24,12 +24,13 @@ class Syncer:
running_global = True running_global = True
yield_delay=0.005 yield_delay=0.005
def __init__(self, backend, progress_callback=noop_progress): def __init__(self, backend, loop_callback=noop_callback, progress_callback=noop_callback):
self.cursor = None self.cursor = None
self.running = True self.running = True
self.backend = backend self.backend = backend
self.filter = SyncFilter(backend) self.filter = SyncFilter(backend)
self.progress_callback = progress_callback self.progress_callback = progress_callback
self.loop_callback = loop_callback
def chain(self): def chain(self):
@ -45,18 +46,20 @@ class Syncer:
self.filter.add(f) self.filter.add(f)
class BlockSyncer(Syncer): class BlockPollSyncer(Syncer):
def __init__(self, backend, progress_callback=noop_progress): def __init__(self, backend, loop_callback=noop_callback, progress_callback=noop_callback):
super(BlockSyncer, self).__init__(backend, progress_callback) super(BlockPollSyncer, self).__init__(backend, loop_callback, progress_callback)
def loop(self, interval, conn): def loop(self, interval, conn):
g = self.backend.get() g = self.backend.get()
last_tx = g[1] last_tx = g[1]
last_block = g[0] last_block = g[0]
self.progress_callback('loop started', last_block, last_tx) self.progress_callback(last_block, last_tx, 'loop started')
while self.running and Syncer.running_global: while self.running and Syncer.running_global:
if self.loop_callback != None:
self.loop_callback(last_block, last_tx)
while True: while True:
try: try:
block = self.get(conn) block = self.get(conn)
@ -65,16 +68,16 @@ class BlockSyncer(Syncer):
last_block = block.number last_block = block.number
self.process(conn, block) self.process(conn, block)
start_tx = 0 start_tx = 0
self.progress_callback('processed block {}'.format(self.backend.get()), last_block, last_tx) self.progress_callback(last_block, last_tx, 'processed block {}'.format(self.backend.get()))
time.sleep(self.yield_delay) time.sleep(self.yield_delay)
self.progress_callback('loop ended', last_block + 1, last_tx) self.progress_callback(last_block + 1, last_tx, 'loop ended')
time.sleep(interval) time.sleep(interval)
class HeadSyncer(BlockSyncer): class HeadSyncer(BlockPollSyncer):
def __init__(self, backend, progress_callback=noop_progress): def __init__(self, backend, loop_callback=noop_callback, progress_callback=noop_callback):
super(HeadSyncer, self).__init__(backend, progress_callback) super(HeadSyncer, self).__init__(backend, loop_callback, progress_callback)
def process(self, conn, block): def process(self, conn, block):
@ -84,7 +87,7 @@ class HeadSyncer(BlockSyncer):
while True: while True:
try: try:
tx = block.tx(i) tx = block.tx(i)
self.progress_callback('processing {}'.format(repr(tx)), block.number, i) self.progress_callback(block.number, i, 'processing {}'.format(repr(tx)))
self.backend.set(block.number, i) self.backend.set(block.number, i)
self.filter.apply(conn, block, tx) self.filter.apply(conn, block, tx)
except IndexError as e: except IndexError as e:

View File

@ -1,6 +1,9 @@
# standard imports # standard imports
import logging import logging
# external imports
import sqlalchemy
# local imports # local imports
from .error import BackendError from .error import BackendError

View File

@ -1,6 +1,6 @@
[metadata] [metadata]
name = chainsyncer name = chainsyncer
version = 0.0.1a8 version = 0.0.1a10
description = Generic blockchain syncer driver description = Generic blockchain syncer driver
author = Louis Holbrook author = Louis Holbrook
author_email = dev@holbrook.no author_email = dev@holbrook.no

View File

@ -42,5 +42,3 @@ class TestBase(unittest.TestCase):
def tearDown(self): def tearDown(self):
SessionBase.disconnect() SessionBase.disconnect()
os.unlink(self.db_path) os.unlink(self.db_path)