chainsyncer/chainsyncer/backend/memory.py

113 lines
3.0 KiB
Python
Raw Normal View History

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
logg = logging.getLogger(__name__)
2021-04-10 00:30:08 +02:00
2021-04-15 17:16:31 +02:00
class MemBackend(Backend):
"""Disposable syncer backend. Keeps syncer state in memory.
2021-04-10 00:30:08 +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
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
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):
"""NOOP as memory backend implements no connection.
"""
2021-04-10 00:30:08 +02:00
pass
def disconnect(self):
"""NOOP as memory backend implements no connection.
"""
2021-04-10 00:30:08 +02:00
pass
def set(self, block_height, tx_height):
"""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):
"""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):
"""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):
"""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):
"""Set filter at index as completed for the current block / tx state.
:param n: Filter index
:type n: int
"""
v = 1 << n
2021-04-15 14:11:06 +02:00
self.flags |= v
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):
"""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):
return "syncer membackend {} chain {} cursor {}".format(self.object_id, self.chain(), self.get())