eth-faucet

Gas token gifter with controls from time intervals, amounts and access
Info | Log | Files | Refs | README

base.py (6722B)


      1 # standard imports
      2 import os
      3 import unittest
      4 import logging
      5 
      6 # external imports
      7 from chainlib.connection import RPCConnection
      8 from chainlib.eth.nonce import RPCNonceOracle
      9 from chainlib.eth.unittest.ethtester import EthTesterCase
     10 from chainlib.eth.tx import receipt
     11 from chainlib.eth.address import to_checksum_address
     12 from chainlib.eth.gas import balance
     13 from chainlib.eth.gas import Gas
     14 from chainlib.eth.gas import OverrideGasOracle
     15 from chainlib.eth.contract import ABIContractEncoder
     16 from chainlib.eth.tx import TxFactory
     17 from chainlib.eth.contract import ABIContractType
     18 from chainlib.eth.block import block_by_number
     19 from eth_accounts_index.registry import AccountRegistry
     20 
     21 # local import
     22 from eth_faucet import EthFaucet
     23 from eth_faucet.period import PeriodSimple
     24 
     25 logging.basicConfig(level=logging.DEBUG)
     26 logg = logging.getLogger()
     27 
     28 moddir = os.path.dirname(__file__)
     29 datadir = os.path.join(moddir, '..', 'data')
     30 
     31 
     32 class TestFaucetBase(EthTesterCase):
     33 
     34     def setUp(self):
     35         super(TestFaucetBase, self).setUp()
     36         self.conn = RPCConnection.connect(self.chain_spec, 'default')
     37         nonce_oracle = RPCNonceOracle(self.accounts[0], self.conn)
     38         c = EthFaucet(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
     39         (tx_hash_hex, o) = c.constructor(self.accounts[0])
     40         r = self.conn.do(o)
     41         
     42         o = receipt(r)
     43         r = self.conn.do(o)
     44         self.address = to_checksum_address(r['contract_address'])
     45         logg.debug('faucet contract {}'.format(self.address))
     46 
     47 
     48 class TestFaucetRegistryBase(TestFaucetBase):
     49 
     50     def setUpRegistry(self):
     51         nonce_oracle = RPCNonceOracle(self.accounts[0], self.conn)
     52         c = AccountRegistry(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
     53         (tx_hash, o) = c.constructor(self.accounts[0])
     54         self.conn = RPCConnection.connect(self.chain_spec, 'default')
     55         r = self.conn.do(o)
     56         o = receipt(r)
     57         r = self.conn.do(o)
     58         self.registry_address = to_checksum_address(r['contract_address'])
     59 
     60         (tx_hash_hex, o) = c.add_writer(self.registry_address, self.accounts[0], self.accounts[0])
     61         self.conn.do(o)
     62         o = receipt(tx_hash_hex)
     63         r = self.conn.do(o)
     64         self.assertEqual(r['status'], 1)
     65 
     66         (tx_hash_hex, o) = c.add(self.registry_address, self.accounts[0], self.accounts[1])
     67         self.conn.do(o)
     68         o = receipt(tx_hash_hex)
     69         r = self.conn.do(o)
     70         self.assertEqual(r['status'], 1)
     71 
     72         c = EthFaucet(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
     73         (tx_hash_hex, o) = c.set_registry(self.address, self.accounts[0], self.registry_address)
     74         self.conn.do(o)
     75         o = receipt(tx_hash_hex)
     76         r = self.conn.do(o)
     77         self.assertEqual(r['status'], 1)
     78 
     79 
     80     def setUp(self):
     81         super(TestFaucetRegistryBase, self).setUp()
     82         self.setUpRegistry()
     83 
     84 
     85 class TestFaucetPeriodBase(TestFaucetBase):
     86 
     87     def setUpPeriod(self):
     88         self.conn = RPCConnection.connect(self.chain_spec, 'default')
     89         nonce_oracle = RPCNonceOracle(self.accounts[0], self.conn)
     90         c = PeriodSimple(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
     91         (tx_hash_hex, o) = c.constructor(self.accounts[0])
     92         r = self.conn.do(o)
     93 
     94         f = open(os.path.join(datadir, 'PeriodSimple.bin'))
     95         period_store_bytecode = f.read()
     96         f.close()
     97         enc = ABIContractEncoder()
     98         code = enc.get()
     99 
    100         c = TxFactory(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
    101         tx = c.template(self.accounts[0], None, use_nonce=True)
    102         tx = c.set_code(tx, period_store_bytecode)
    103         (tx_hash_hex, o) =  c.build(tx)
    104         self.conn.do(o)
    105         o = receipt(tx_hash_hex)
    106         r = self.conn.do(o)
    107         self.assertEqual(r['status'], 1)
    108         self.period_store_address = r['contract_address']
    109 
    110         o = block_by_number(r['block_number'])
    111         r = self.conn.do(o)
    112 
    113         try:
    114             self.start_time = int(r['timestamp'], 16)
    115         except TypeError:
    116             self.start_time = int(r['timestamp'])
    117 
    118         c = EthFaucet(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
    119         (tx_hash_hex, o) = c.set_period_checker(self.address, self.accounts[0], self.period_store_address)
    120         self.conn.do(o)
    121         o = receipt(tx_hash_hex)
    122         r = self.conn.do(o)
    123         self.assertEqual(r['status'], 1)
    124 
    125         nonce_oracle = RPCNonceOracle(self.accounts[0], self.conn)
    126         c = TxFactory(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
    127         enc = ABIContractEncoder()
    128         enc.method('setPoker')
    129         enc.typ(ABIContractType.ADDRESS)
    130         enc.address(self.address)
    131         data = enc.get()
    132         tx = c.template(self.accounts[0], self.period_store_address, use_nonce=True)
    133         tx = c.set_code(tx, data)
    134         (tx_hash_hex, o) = c.finalize(tx)
    135         self.conn.do(o)
    136         o = receipt(tx_hash_hex)
    137         r = self.conn.do(o)
    138         self.assertEqual(r['status'], 1)
    139 
    140         self.period = 0
    141         self.threshold = 0
    142 
    143 
    144     def setUp(self):
    145         super(TestFaucetPeriodBase, self).setUp()
    146         self.setUpPeriod()
    147 
    148 
    149     def set_period(self, period):
    150         nonce_oracle = RPCNonceOracle(self.accounts[0], self.conn)
    151         c = TxFactory(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
    152         enc = ABIContractEncoder()
    153         enc.method('setPeriod')
    154         enc.typ(ABIContractType.UINT256)
    155         enc.uint256(period)
    156         data = enc.get()
    157         tx = c.template(self.accounts[0], self.period_store_address, use_nonce=True)
    158         tx = c.set_code(tx, data)
    159         (tx_hash_hex, o) = c.finalize(tx)
    160         self.conn.do(o)
    161         o = receipt(tx_hash_hex)
    162         r = self.conn.do(o)
    163         self.assertEqual(r['status'], 1)
    164         self.period = period
    165 
    166 
    167     def set_threshold(self, threshold):
    168         nonce_oracle = RPCNonceOracle(self.accounts[0], self.conn)
    169         c = TxFactory(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
    170         enc = ABIContractEncoder()
    171         enc.method('setBalanceThreshold')
    172         enc.typ(ABIContractType.UINT256)
    173         enc.uint256(threshold)
    174         data = enc.get()
    175         tx = c.template(self.accounts[0], self.period_store_address, use_nonce=True)
    176         tx = c.set_code(tx, data)
    177         (tx_hash_hex, o) = c.finalize(tx)
    178         self.conn.do(o)
    179         o = receipt(tx_hash_hex)
    180         r = self.conn.do(o)
    181         self.assertEqual(r['status'], 1)
    182 
    183         self.threshold = threshold
    184 
    185 
    186 class TestFaucetFullBase(TestFaucetRegistryBase, TestFaucetPeriodBase):
    187 
    188     def setUp(self):
    189         super(TestFaucetFullBase, self).setUp()
    190         self.setUpPeriod()