2021-04-15 11:15:48 +02:00
# standard imports
import os
2021-04-15 14:11:06 +02:00
import logging
2022-03-17 23:07:19 +01:00
import hashlib
2021-04-15 11:15:48 +02:00
# external imports
from hexathon import add_0x
2022-03-17 23:07:19 +01:00
from shep . state import State
2021-04-15 11:15:48 +02:00
# local imports
2022-03-17 23:07:19 +01: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
2021-04-15 15:27:19 +02:00
class MockConn :
2021-08-27 14:16:46 +02:00
""" Noop connection mocker.
2021-04-15 15:27:19 +02:00
2021-08-27 14:16:46 +02:00
: param o : Object to execute rpc call for
: type o : dict
"""
2021-04-15 15:27:19 +02:00
def do ( self , o ) :
pass
2021-04-15 11:15:48 +02:00
class MockTx :
2021-08-27 14:16:46 +02:00
""" Minimal mocked tx object.
2021-04-15 11:15:48 +02:00
2021-08-27 14:16:46 +02:00
: param index : Transaction index in block
: type index : int
: param tx_hash : Transaction hash
: type tx_hash : str
"""
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
2021-04-15 15:27:19 +02:00
def apply_receipt ( self , rcpt ) :
2021-08-27 14:16:46 +02:00
""" Save receipt source in mock tx object.
: param rcpt : Transaction receipt
: type rcpt : dict
"""
2021-04-15 15:27:19 +02:00
self . rcpt = rcpt
2021-04-15 11:15:48 +02:00
class MockBlock :
def __init__ ( self , number , txs ) :
2021-08-27 14:16:46 +02:00
""" Minimal mocked block object.
: param number : Block number
: type number : int
: param txs : Transaction list to include in block
: type txs : list
"""
2021-04-15 11:15:48 +02:00
self . number = number
self . txs = txs
def tx ( self , i ) :
2021-08-27 14:16:46 +02:00
""" Get block transaction at given index.
: param i : Transaction index
: type i : int
"""
2021-04-15 11:15:48 +02:00
return MockTx ( i , self . txs [ i ] )
2022-03-17 23:07:19 +01:00
class MockStore ( State ) :
2021-04-15 11:15:48 +02:00
2022-03-17 23:07:19 +01:00
def __init__ ( self , bits = 0 ) :
super ( MockStore , self ) . __init__ ( bits , check_alias = False )
2021-04-15 11:15:48 +02:00
2022-03-17 23:07:19 +01:00
2022-03-18 00:48:23 +01:00
def start ( self , offset = 0 , target = - 1 ) :
2022-03-17 23:07:19 +01:00
pass
2021-04-15 11:15:48 +02:00
2021-08-27 14:16:46 +02:00
2022-03-17 23:07:19 +01:00
class MockFilter :
def __init__ ( self , name , brk = False , z = None ) :
self . name = name
if z == None :
h = hashlib . sha256 ( )
h . update ( self . name . encode ( ' utf-8 ' ) )
z = h . digest ( )
self . z = z
self . brk = brk
2022-03-18 00:48:23 +01:00
self . contents = [ ]
2022-03-17 23:07:19 +01:00
def sum ( self ) :
return self . z
def common_name ( self ) :
return self . name
def filter ( self , conn , block , tx ) :
2022-03-18 00:48:23 +01:00
self . contents . append ( ( block . number , tx . index , tx . hash , ) )
2022-03-17 23:07:19 +01:00
return self . brk
#class TestSyncer(HistorySyncer):
# """Unittest extension of history syncer driver.
#
# :param backend: Syncer backend
# :type backend: chainsyncer.backend.base.Backend implementation
# :param chain_interface: Chain interface
# :type chain_interface: chainlib.interface.ChainInterface implementation
# :param tx_counts: List of integer values defining how many mock transactions to generate per block. Mock blocks will be generated for each element in list.
# :type tx_counts: list
# """
#
# def __init__(self, backend, chain_interface, tx_counts=[]):
# self.tx_counts = tx_counts
# super(TestSyncer, self).__init__(backend, chain_interface)
#
#
# def get(self, conn):
# """Implements the block getter of chainsyncer.driver.base.Syncer.
#
# :param conn: RPC connection
# :type conn: chainlib.connection.RPCConnection
# :raises NoBlockForYou: End of mocked block array reached
# :rtype: chainsyncer.unittest.base.MockBlock
# :returns: Mock block.
# """
# (pair, fltr) = self.backend.get()
# (target_block, fltr) = self.backend.target()
# block_height = pair[0]
#
# if block_height == target_block:
# self.running = False
# raise NoBlockForYou()
#
# block_txs = []
# if block_height < len(self.tx_counts):
# for i in range(self.tx_counts[block_height]):
# block_txs.append(add_0x(os.urandom(32).hex()))
#
# return MockBlock(block_height, block_txs)