eth-faucet

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

test_period.py (5337B)


      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.tx import TxFactory
     12 from chainlib.eth.address import to_checksum_address
     13 from chainlib.eth.gas import balance
     14 from chainlib.eth.gas import Gas
     15 from chainlib.eth.gas import OverrideGasOracle
     16 from chainlib.eth.contract import ABIContractEncoder
     17 from chainlib.eth.contract import ABIContractType
     18 from chainlib.eth.block import block_by_number
     19 
     20 # local imports
     21 from eth_faucet import EthFaucet
     22 from eth_faucet.period import PeriodSimple
     23 from eth_faucet.unittest import TestFaucetPeriodBase
     24 
     25 logging.basicConfig(level=logging.DEBUG)
     26 logg = logging.getLogger()
     27 
     28 moddir = os.path.dirname(__file__)
     29 datadir = os.path.join(moddir, '..', 'eth_faucet', 'data')
     30 
     31 
     32 class TestFaucetPeriod(TestFaucetPeriodBase):
     33 
     34     def test_period_basic(self):
     35         nonce_oracle = RPCNonceOracle(self.accounts[0], self.conn)
     36         c = EthFaucet(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
     37         (tx_hash_hex, o) = c.give_to(self.address, self.accounts[0], self.accounts[1])
     38         self.conn.do(o)
     39         o = receipt(tx_hash_hex)
     40         r = self.conn.do(o)
     41         self.assertEqual(r['status'], 1)
     42 
     43         nonce_oracle = RPCNonceOracle(self.accounts[2], self.conn)
     44         c = EthFaucet(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
     45         (tx_hash_hex, o) = c.gimme(self.address, self.accounts[2])
     46         self.conn.do(o)
     47         o = receipt(tx_hash_hex)
     48         r = self.conn.do(o)
     49         self.assertEqual(r['status'], 1)
     50 
     51 
     52     def test_period(self):
     53         nonce_oracle = RPCNonceOracle(self.accounts[0], self.conn)
     54         c = TxFactory(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
     55         enc = ABIContractEncoder()
     56         enc.method('setPeriod')
     57         enc.typ(ABIContractType.UINT256)
     58         enc.uint256(100)
     59         data = enc.get()
     60         tx = c.template(self.accounts[0], self.period_store_address, use_nonce=True)
     61         tx = c.set_code(tx, data)
     62         (tx_hash_hex, o) = c.finalize(tx)
     63         self.conn.do(o)
     64         o = receipt(tx_hash_hex)
     65         r = self.conn.do(o)
     66         self.assertEqual(r['status'], 1)
     67 
     68         nonce_oracle = RPCNonceOracle(self.accounts[0], self.conn)
     69         c = EthFaucet(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
     70         (tx_hash_hex, o) = c.give_to(self.address, self.accounts[0], self.accounts[2])
     71         self.conn.do(o)
     72         o = receipt(tx_hash_hex)
     73         r = self.conn.do(o)
     74         self.assertEqual(r['status'], 1)
     75 
     76         (tx_hash_hex, o) = c.give_to(self.address, self.accounts[0], self.accounts[2])
     77         self.conn.do(o)
     78         o = receipt(tx_hash_hex)
     79         r = self.conn.do(o)
     80         self.assertEqual(r['status'], 0)
     81 
     82         self.backend.time_travel(self.start_time + 6000)
     83 
     84         (tx_hash_hex, o) = c.give_to(self.address, self.accounts[0], self.accounts[2])
     85         self.conn.do(o)
     86         o = receipt(tx_hash_hex)
     87         r = self.conn.do(o)
     88         self.assertEqual(r['status'], 1)
     89 
     90 
     91     def test_balance(self):
     92         o = balance(self.accounts[1])
     93         r = self.conn.do(o)
     94         prebalance = int(r, 16)
     95 
     96         self.set_threshold(prebalance / 2)
     97 
     98         nonce_oracle = RPCNonceOracle(self.accounts[1], self.conn)
     99         c = EthFaucet(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
    100         (tx_hash_hex, o) = c.gimme(self.address, self.accounts[1])
    101         self.conn.do(o)
    102         o = receipt(tx_hash_hex)
    103         r = self.conn.do(o)
    104         self.assertEqual(r['status'], 0)
    105 
    106         self.set_threshold(prebalance)
    107 
    108         (tx_hash_hex, o) = c.gimme(self.address, self.accounts[1])
    109         self.conn.do(o)
    110         o = receipt(tx_hash_hex)
    111         r = self.conn.do(o)
    112         self.assertEqual(r['status'], 1)
    113 
    114 
    115     def test_period_front(self):
    116         nonce_oracle = RPCNonceOracle(self.accounts[0], self.conn)
    117         c = TxFactory(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
    118         enc = ABIContractEncoder()
    119         enc.method('setPeriod')
    120         enc.typ(ABIContractType.UINT256)
    121         enc.uint256(100)
    122         data = enc.get()
    123         tx = c.template(self.accounts[0], self.period_store_address, use_nonce=True)
    124         tx = c.set_code(tx, data)
    125         (tx_hash_hex, o) = c.finalize(tx)
    126         self.conn.do(o)
    127    
    128         nonce_oracle = RPCNonceOracle(self.accounts[0], self.conn)
    129         c = EthFaucet(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
    130         (tx_hash_hex, o) = c.gimme(self.address, self.accounts[0])
    131         self.conn.do(o)
    132         o = receipt(tx_hash_hex)
    133         r = self.conn.do(o)
    134         o = block_by_number(r['block_number'])
    135         r = self.conn.do(o)
    136         thistime = r['timestamp']
    137 
    138         o = c.next_time(self.address, self.accounts[0], sender_address=self.accounts[0])
    139         r = self.conn.do(o)
    140         nexttime = int(r, 16)
    141         self.assertEqual(nexttime, thistime+100)
    142 
    143         o = c.check(self.address, self.accounts[0], sender_address=self.accounts[0])
    144         r = self.conn.do(o)
    145         checked = int(r, 16)
    146         self.assertEqual(checked, 0)
    147 
    148 
    149 if __name__ == '__main__':
    150     unittest.main()