chainlib-eth

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

ethtester.py (2705B)


      1 # standard imports
      2 import os
      3 import unittest
      4 import logging
      5 
      6 # external imports
      7 import eth_tester
      8 from funga.eth.signer import EIP155Signer
      9 from funga.eth.keystore.dict import DictKeystore
     10 from hexathon import (
     11         strip_0x,
     12         add_0x,
     13         )
     14 from eth import constants
     15 from eth.vm.forks.byzantium import ByzantiumVM
     16 
     17 # local imports
     18 from .base import (
     19         EthTesterSigner,
     20         TestRPCConnection,
     21         )
     22 from chainlib.connection import (
     23         RPCConnection,
     24         ConnType,
     25         )
     26 from chainlib.eth.address import to_checksum_address
     27 from chainlib.chain import ChainSpec
     28 
     29 #logg = logging.getLogger(__name__)
     30 logg = logging.getLogger()
     31 
     32 test_address = bytes.fromhex('Eb3907eCad74a0013c259D5874AE7f22DcBcC95C')
     33 
     34 
     35 def create_tester_signer(keystore):
     36     genesis_params = eth_tester.backends.pyevm.main.get_default_genesis_params({
     37         'gas_limit': 8000000,
     38         'coinbase': test_address, # doesn't seem to work
     39         })
     40     vm_configuration = (
     41                 (constants.GENESIS_BLOCK_NUMBER, ByzantiumVM),
     42     )
     43     genesis_state = eth_tester.PyEVMBackend._generate_genesis_state(num_accounts=30)
     44     eth_backend = eth_tester.PyEVMBackend(
     45             genesis_state=genesis_state,
     46             genesis_parameters=genesis_params,
     47             vm_configuration=vm_configuration,
     48             )
     49     return EthTesterSigner(eth_backend, keystore)
     50 
     51 
     52 class EthTesterCase(unittest.TestCase):
     53 
     54     def __init__(self, foo):
     55         super(EthTesterCase, self).__init__(foo)
     56         self.accounts = []
     57 
     58 
     59     def setUp(self):
     60         self.chain_spec = ChainSpec('evm', 'foochain', 42)
     61         self.keystore = DictKeystore()
     62         eth_tester_instance = create_tester_signer(self.keystore)
     63         self.signer = EIP155Signer(self.keystore)
     64         self.helper = eth_tester_instance
     65         self.backend = self.helper.backend
     66         self.rpc = TestRPCConnection(None, eth_tester_instance, self.signer)
     67 
     68         for a in self.keystore.list():
     69             self.accounts.append(add_0x(to_checksum_address(a)))
     70 
     71         self.publisher = self.accounts[0]
     72 
     73         def rpc_with_tester(chain_spec=self.chain_spec, url=None):
     74             return self.rpc
     75 
     76         RPCConnection.register_constructor(ConnType.CUSTOM, rpc_with_tester, tag='default')
     77         RPCConnection.register_constructor(ConnType.CUSTOM, rpc_with_tester, tag='signer')
     78         RPCConnection.register_location('custom', self.chain_spec, tag='default', exist_ok=True)
     79         RPCConnection.register_location('custom', self.chain_spec, tag='signer', exist_ok=True)
     80 
     81         self.contracts = {}
     82         self.roles = {
     83             'publisher': self.publisher,
     84                 }
     85         
     86 
     87     def tearDown(self):
     88         pass