test_basic.py (5564B)
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 16 # local import 17 from eth_faucet import EthFaucet 18 from eth_faucet.unittest import TestFaucetBase 19 20 logging.basicConfig(level=logging.DEBUG) 21 logg = logging.getLogger() 22 23 24 class TestFaucet(TestFaucetBase): 25 26 def test_basic(self): 27 nonce_oracle = RPCNonceOracle(self.accounts[0], self.conn) 28 c = EthFaucet(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 29 (tx_hash_hex, o) = c.give_to(self.address, self.accounts[0], self.accounts[2]) 30 self.conn.do(o) 31 o = receipt(tx_hash_hex) 32 r = self.conn.do(o) 33 self.assertEqual(r['status'], 1) 34 35 nonce_oracle = RPCNonceOracle(self.accounts[2], self.conn) 36 c = EthFaucet(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 37 (tx_hash_hex, o) = c.gimme(self.address, self.accounts[2]) 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 44 def test_basic_funding(self): 45 o = balance(self.accounts[2]) 46 r = self.conn.do(o) 47 prebalance = int(r, 16) 48 49 nonce_oracle = RPCNonceOracle(self.accounts[0], self.conn) 50 c = EthFaucet(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 51 (tx_hash_hex, o) = c.give_to(self.address, self.accounts[0], self.accounts[2]) 52 self.conn.do(o) 53 o = receipt(tx_hash_hex) 54 r = self.conn.do(o) 55 self.assertEqual(r['status'], 1) 56 57 o = balance(self.accounts[2]) 58 r = self.conn.do(o) 59 self.assertEqual(int(r, 16), prebalance) 60 61 (tx_hash, o) = c.set_amount(self.address, self.accounts[0], 1000) 62 r = self.conn.do(o) 63 o = receipt(tx_hash_hex) 64 r = self.conn.do(o) 65 self.assertEqual(r['status'], 1) 66 67 (tx_hash_hex, o) = c.give_to(self.address, self.accounts[0], self.accounts[2]) 68 self.conn.do(o) 69 o = receipt(tx_hash_hex) 70 r = self.conn.do(o) 71 self.assertEqual(r['status'], 0) 72 73 contract_gas_oracle = OverrideGasOracle(limit=21055, conn=self.conn) 74 cg = Gas(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle, gas_oracle=contract_gas_oracle) 75 (tx_hash_hex, o) = cg.create(self.accounts[0], self.address, 1000) 76 self.conn.do(o) 77 78 o = receipt(tx_hash_hex) 79 r = self.conn.do(o) 80 self.assertEqual(r['status'], 1) 81 82 (tx_hash_hex, o) = c.give_to(self.address, self.accounts[0], self.accounts[2]) 83 self.conn.do(o) 84 85 o = receipt(tx_hash_hex) 86 r = self.conn.do(o) 87 self.assertEqual(r['status'], 1) 88 89 o = balance(self.accounts[2]) 90 r = self.conn.do(o) 91 self.assertEqual(int(r, 16), prebalance + 1000) 92 93 94 def test_basic_result(self): 95 nonce_oracle = RPCNonceOracle(self.accounts[0], self.conn) 96 c = EthFaucet(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 97 98 contract_gas_oracle = OverrideGasOracle(limit=21055, conn=self.conn) 99 cg = Gas(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle, gas_oracle=contract_gas_oracle) 100 (tx_hash_hex, o) = cg.create(self.accounts[0], self.address, 1000) 101 self.conn.do(o) 102 103 o = receipt(tx_hash_hex) 104 r = self.conn.do(o) 105 self.assertEqual(r['status'], 1) 106 107 (tx_hash, o) = c.set_amount(self.address, self.accounts[0], 1000) 108 r = self.conn.do(o) 109 110 o = balance(self.accounts[1]) 111 r = self.conn.do(o) 112 prebalance = int(r, 16) 113 114 nonce_oracle = RPCNonceOracle(self.accounts[1], self.conn) 115 gas_price = 1000000000 116 gas_oracle = OverrideGasOracle(limit=100000, price=gas_price, conn=self.conn) 117 c = EthFaucet(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle) 118 (tx_hash_hex, o) = c.gimme(self.address, self.accounts[1]) 119 self.conn.do(o) 120 121 o = receipt(tx_hash_hex) 122 r = self.conn.do(o) 123 cost = r['gas_used'] * gas_price 124 self.assertEqual(r['status'], 1) 125 126 o = balance(self.accounts[1]) 127 r = self.conn.do(o) 128 self.assertEqual(int(r, 16), prebalance - cost + 1000) 129 130 131 def test_payable_with_tx_data(self): 132 nonce_oracle = RPCNonceOracle(self.accounts[0], self.conn) 133 c = EthFaucet(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 134 135 o = balance(self.accounts[0]) 136 r = self.conn.do(o) 137 prebalance = int(r, 16) 138 139 gas_price = 1000000000 140 contract_gas_oracle = OverrideGasOracle(limit=21055, price=gas_price, conn=self.conn) 141 cg = Gas(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle, gas_oracle=contract_gas_oracle) 142 (tx_hash_hex, o) = cg.create(self.accounts[0], self.address, 1000, data='0x0') 143 self.conn.do(o) 144 145 o = receipt(tx_hash_hex) 146 r = self.conn.do(o) 147 cost = r['gas_used'] * gas_price 148 self.assertEqual(r['status'], 0) 149 150 o = balance(self.accounts[0]) 151 r = self.conn.do(o) 152 self.assertEqual(int(r, 16), prebalance - cost) 153 154 155 156 if __name__ == '__main__': 157 unittest.main()