2021-04-10 00:30:08 +02:00
|
|
|
|
# standard imports
|
|
|
|
|
import logging
|
|
|
|
|
|
2021-04-15 17:16:31 +02:00
|
|
|
|
# local imports
|
|
|
|
|
from .base import Backend
|
|
|
|
|
|
2021-08-27 12:23:11 +02:00
|
|
|
|
logg = logging.getLogger(__name__)
|
2021-04-10 00:30:08 +02:00
|
|
|
|
|
|
|
|
|
|
2021-04-15 17:16:31 +02:00
|
|
|
|
class MemBackend(Backend):
|
2021-08-27 12:23:11 +02:00
|
|
|
|
"""Disposable syncer backend. Keeps syncer state in memory.
|
2021-04-10 00:30:08 +02:00
|
|
|
|
|
2021-08-27 12:23:11 +02:00
|
|
|
|
Filter bitfield is interpreted right to left.
|
|
|
|
|
|
|
|
|
|
:param chain_spec: Chain spec context of syncer
|
|
|
|
|
:type chain_spec: chainlib.chain.ChainSpec
|
|
|
|
|
:param object_id: Unique id for the syncer session.
|
|
|
|
|
:type object_id: str
|
|
|
|
|
:param target_block: Block height to terminate sync at
|
|
|
|
|
:type target_block: int
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def __init__(self, chain_spec, object_id, target_block=None, block_height=0, tx_height=0, flags=0):
|
|
|
|
|
super(MemBackend, self).__init__(object_id)
|
2021-04-10 00:30:08 +02:00
|
|
|
|
self.chain_spec = chain_spec
|
2021-08-27 12:23:11 +02:00
|
|
|
|
self.block_height_offset = block_height
|
|
|
|
|
self.block_height_cursor = block_height
|
|
|
|
|
self.tx_height_offset = tx_height
|
|
|
|
|
self.tx_height_cursor = tx_height
|
|
|
|
|
self.block_height_target = target_block
|
2021-04-10 00:30:08 +02:00
|
|
|
|
self.db_session = None
|
2021-08-27 12:23:11 +02:00
|
|
|
|
self.flags = flags
|
2021-04-15 09:59:45 +02:00
|
|
|
|
self.filter_names = []
|
2021-04-10 00:30:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def connect(self):
|
2021-08-27 12:23:11 +02:00
|
|
|
|
"""NOOP as memory backend implements no connection.
|
|
|
|
|
"""
|
2021-04-10 00:30:08 +02:00
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def disconnect(self):
|
2021-08-27 12:23:11 +02:00
|
|
|
|
"""NOOP as memory backend implements no connection.
|
|
|
|
|
"""
|
2021-04-10 00:30:08 +02:00
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def set(self, block_height, tx_height):
|
2021-08-27 12:23:11 +02:00
|
|
|
|
"""Set the syncer state.
|
|
|
|
|
|
|
|
|
|
:param block_height: New block height
|
|
|
|
|
:type block_height: int
|
|
|
|
|
:param tx_height: New transaction height in block
|
|
|
|
|
:type tx_height: int
|
|
|
|
|
"""
|
|
|
|
|
logg.debug('memory backend received {} {}'.format(block_height, tx_height))
|
|
|
|
|
self.block_height_cursor = block_height
|
|
|
|
|
self.tx_height_cursor = tx_height
|
2021-04-10 00:30:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get(self):
|
2021-08-27 12:23:11 +02:00
|
|
|
|
"""Get the current syncer state
|
|
|
|
|
|
|
|
|
|
:rtype: tuple
|
|
|
|
|
:returns: block height / tx index tuple, and filter flags value
|
|
|
|
|
"""
|
|
|
|
|
return ((self.block_height_cursor, self.tx_height_cursor), self.flags)
|
2021-04-10 00:30:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def target(self):
|
2021-08-27 12:23:11 +02:00
|
|
|
|
"""Returns the syncer target.
|
|
|
|
|
|
|
|
|
|
:rtype: tuple
|
|
|
|
|
:returns: block height / tx index tuple
|
|
|
|
|
"""
|
|
|
|
|
return (self.block_height_target, self.flags)
|
2021-04-10 00:30:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def register_filter(self, name):
|
2021-08-27 12:23:11 +02:00
|
|
|
|
"""Adds a filter identifier to the syncer.
|
|
|
|
|
|
|
|
|
|
:param name: Filter name
|
|
|
|
|
:type name: str
|
|
|
|
|
"""
|
2021-04-15 09:59:45 +02:00
|
|
|
|
self.filter_names.append(name)
|
2021-04-15 17:16:31 +02:00
|
|
|
|
self.filter_count += 1
|
2021-04-10 00:30:08 +02:00
|
|
|
|
|
|
|
|
|
|
2021-09-26 19:32:08 +02:00
|
|
|
|
def begin_filter(self, n):
|
2021-08-27 12:23:11 +02:00
|
|
|
|
"""Set filter at index as completed for the current block / tx state.
|
|
|
|
|
|
|
|
|
|
:param n: Filter index
|
|
|
|
|
:type n: int
|
|
|
|
|
"""
|
2021-04-15 15:06:07 +02:00
|
|
|
|
v = 1 << n
|
2021-04-15 14:11:06 +02:00
|
|
|
|
self.flags |= v
|
2021-04-15 15:06:07 +02:00
|
|
|
|
logg.debug('set filter {} {}'.format(self.filter_names[n], v))
|
2021-04-15 14:11:06 +02:00
|
|
|
|
|
|
|
|
|
|
2021-09-26 19:32:08 +02:00
|
|
|
|
def complete_filter(self, n):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
2021-04-15 14:11:06 +02:00
|
|
|
|
def reset_filter(self):
|
2021-08-27 12:23:11 +02:00
|
|
|
|
"""Set all filters to unprocessed for the current block / tx state.
|
|
|
|
|
"""
|
2021-04-15 14:11:06 +02:00
|
|
|
|
logg.debug('reset filters')
|
|
|
|
|
self.flags = 0
|
2021-04-10 00:30:08 +02:00
|
|
|
|
|
2021-04-15 17:16:31 +02:00
|
|
|
|
|
2021-04-10 00:30:08 +02:00
|
|
|
|
def __str__(self):
|
2021-08-27 12:23:11 +02:00
|
|
|
|
return "syncer membackend {} chain {} cursor {}".format(self.object_id, self.chain(), self.get())
|