chainlib-eth

Ethereum implementation of the chainlib interface
Info | Log | Files | Refs | README | LICENSE

fixtures_ethtester.py (2535B)


      1 # standard imports
      2 import os
      3 import logging
      4 
      5 # external imports
      6 import eth_tester
      7 import pytest
      8 from funga.eth.signer import EIP155Signer
      9 from funga.eth.keystore.dict import DictKeystore
     10 
     11 # local imports
     12 from chainlib.eth.unittest.base import *
     13 from chainlib.connection import (
     14         RPCConnection,
     15         ConnType,
     16         )
     17 from chainlib.eth.unittest.ethtester import create_tester_signer
     18 from chainlib.eth.address import to_checksum_address
     19 
     20 logg = logging.getLogger(__name__)
     21 
     22 
     23 @pytest.fixture(scope='function')
     24 def eth_keystore():
     25     return DictKeystore()
     26 
     27 
     28 @pytest.fixture(scope='function')
     29 def init_eth_tester(
     30         eth_keystore,
     31         ):
     32     return create_tester_signer(eth_keystore) 
     33 
     34 
     35 @pytest.fixture(scope='function')
     36 def call_sender(
     37         eth_accounts,
     38         ):
     39     return eth_accounts[0]
     40 
     41 
     42 @pytest.fixture(scope='function')
     43 def eth_rpc(
     44         default_chain_spec,
     45         init_eth_rpc,
     46         ):
     47     return RPCConnection.connect(default_chain_spec, 'default')
     48 
     49 
     50 @pytest.fixture(scope='function')
     51 def eth_accounts(
     52         init_eth_tester,
     53         ):
     54     addresses = list(init_eth_tester.get_accounts())
     55     for address in addresses:
     56         balance = init_eth_tester.get_balance(address)
     57         logg.debug('prefilled account {} balance {}'.format(address, balance))
     58     return addresses
     59 
     60 
     61 @pytest.fixture(scope='function')
     62 def eth_empty_accounts(
     63         eth_keystore,
     64         init_eth_tester,
     65         ):
     66     a = []
     67     for i in range(10):
     68         #address = init_eth_tester.new_account()
     69         address = eth_keystore.new()
     70         checksum_address = add_0x(to_checksum_address(address))
     71         a.append(checksum_address)
     72         logg.info('added address {}'.format(checksum_address))
     73     return a
     74 
     75 
     76 @pytest.fixture(scope='function')
     77 def eth_signer(
     78         eth_keystore,
     79         ):
     80     return EIP155Signer(eth_keystore)
     81 
     82 
     83 @pytest.fixture(scope='function')
     84 def init_eth_rpc(
     85         default_chain_spec,
     86         init_eth_tester,
     87         eth_signer,
     88         ):
     89 
     90     rpc_conn = TestRPCConnection(None, init_eth_tester, eth_signer)
     91     def rpc_with_tester(url=None, chain_spec=default_chain_spec):
     92         return rpc_conn
     93 
     94     RPCConnection.register_constructor(ConnType.CUSTOM, rpc_with_tester, tag='default')
     95     RPCConnection.register_constructor(ConnType.CUSTOM, rpc_with_tester, tag='signer')
     96     RPCConnection.register_location('custom', default_chain_spec, tag='default', exist_ok=True)
     97     RPCConnection.register_location('custom', default_chain_spec, tag='signer', exist_ok=True)
     98     return None