chainsyncer/chainsyncer/unittest/base.py

65 lines
1.5 KiB
Python
Raw Normal View History

2021-04-15 11:15:48 +02:00
# standard imports
import os
2021-04-15 14:11:06 +02:00
import logging
2021-04-15 11:15:48 +02:00
# external imports
from hexathon import add_0x
# local imports
from chainsyncer.driver import HistorySyncer
from chainsyncer.error import NoBlockForYou
2021-04-15 14:11:06 +02:00
logg = logging.getLogger().getChild(__name__)
2021-04-15 11:15:48 +02:00
class MockTx:
2021-04-15 14:11:06 +02:00
def __init__(self, index, tx_hash):
2021-04-15 11:15:48 +02:00
self.hash = tx_hash
self.index = index
class MockBlock:
def __init__(self, number, txs):
self.number = number
self.txs = txs
def tx(self, i):
return MockTx(i, self.txs[i])
class TestSyncer(HistorySyncer):
def __init__(self, backend, tx_counts=[]):
self.tx_counts = tx_counts
super(TestSyncer, self).__init__(backend)
def get(self, conn):
if self.backend.block_height == self.backend.target_block:
self.running = False
raise NoBlockForYou()
return []
block_txs = []
2021-04-15 14:11:06 +02:00
if self.backend.block_height < len(self.tx_counts):
for i in range(self.tx_counts[self.backend.block_height]):
block_txs.append(add_0x(os.urandom(32).hex()))
logg.debug('get tx height {}'.format(self.backend.tx_height))
2021-04-15 11:15:48 +02:00
return MockBlock(self.backend.block_height, block_txs)
2021-04-15 14:11:06 +02:00
# TODO: implement mock conn instead, and use HeadSyncer.process
2021-04-15 11:15:48 +02:00
def process(self, conn, block):
i = 0
for tx in block.txs:
2021-04-15 14:11:06 +02:00
self.process_single(conn, block, block.tx(i))
self.backend.reset_filter()
2021-04-15 11:15:48 +02:00
i += 1
self.backend.set(self.backend.block_height + 1, 0)