chainsyncer/chainsyncer/unittest/base.py

68 lines
1.4 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
2021-08-26 10:09:47 +02:00
from chainsyncer.driver.history import HistorySyncer
2021-04-15 11:15:48 +02:00
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 MockConn:
def do(self, o):
pass
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
def apply_receipt(self, rcpt):
self.rcpt = rcpt
2021-04-15 11:15:48 +02:00
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):
2021-08-26 10:09:47 +02:00
def __init__(self, backend, chain_interface, tx_counts=[]):
2021-04-15 11:15:48 +02:00
self.tx_counts = tx_counts
2021-08-26 10:09:47 +02:00
super(TestSyncer, self).__init__(backend, chain_interface)
2021-04-15 11:15:48 +02:00
def get(self, conn):
(pair, fltr) = self.backend.get()
(target_block, fltr) = self.backend.target()
block_height = pair[0]
if block_height == target_block:
2021-04-15 11:15:48 +02:00
self.running = False
raise NoBlockForYou()
return []
block_txs = []
if block_height < len(self.tx_counts):
for i in range(self.tx_counts[block_height]):
2021-04-15 14:11:06 +02:00
block_txs.append(add_0x(os.urandom(32).hex()))
return MockBlock(block_height, block_txs)