Add tests

This commit is contained in:
lash 2023-02-03 11:41:48 +00:00
parent aafe47eb19
commit 962506aa3a
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
10 changed files with 234 additions and 18 deletions

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1 @@
[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"FaucetAmountChange","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_recipient","type":"address"},{"indexed":true,"internalType":"address","name":"_token","type":"address"},{"indexed":false,"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"FaucetFail","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"_sealState","type":"uint256"},{"indexed":false,"internalType":"address","name":"_registry","type":"address"},{"indexed":false,"internalType":"address","name":"_periodChecker","type":"address"}],"name":"FaucetStateChange","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_recipient","type":"address"},{"indexed":true,"internalType":"address","name":"_token","type":"address"},{"indexed":false,"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"FaucetUsed","type":"event"},{"inputs":[],"name":"amount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"gimme","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_recipient","type":"address"}],"name":"giveTo","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"maxSealState","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"periodChecker","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"registry","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_state","type":"uint256"}],"name":"seal","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"sealState","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_v","type":"uint256"}],"name":"setAmount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_checker","type":"address"}],"name":"setPeriodChecker","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_registry","type":"address"}],"name":"setRegistry","outputs":[],"stateMutability":"nonpayable","type":"function"},{"stateMutability":"payable","type":"receive"}]

View File

@ -0,0 +1 @@
608060405234801561001057600080fd5b50336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555060008054906101000a900473ffffffffffffffffffffffffffffffffffffffff16600160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506107c9806100c16000396000f3fe608060405234801561001057600080fd5b506004361061009a576000357c0100000000000000000000000000000000000000000000000000000000900480638da5cb5b116100785780638da5cb5b14610109578063b1a997ac14610127578063c23697a814610143578063ef78d4fd146101735761009a565b80630f3a9f651461009f5780634b6bdf1d146100bb5780634ee577b4146100d9575b600080fd5b6100b960048036038101906100b49190610499565b610191565b005b6100c3610229565b6040516100d09190610507565b60405180910390f35b6100f360048036038101906100ee919061054e565b61024f565b604051610100919061058a565b60405180910390f35b610111610267565b60405161011e9190610507565b60405180910390f35b610141600480360381019061013c919061054e565b61028b565b005b61015d6004803603810190610158919061054e565b610400565b60405161016a91906105c0565b60405180910390f35b61017b610458565b604051610188919061058a565b60405180910390f35b3373ffffffffffffffffffffffffffffffffffffffff1660008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff161461021f576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161021690610638565b60405180910390fd5b8060028190555050565b600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b60036020528060005260406000206000915090505481565b60008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b60008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614806103325750600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16145b610371576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610368906106a4565b60405180910390fd5b61037a81610400565b6103b9576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016103b090610710565b60405180910390fd5b42600360008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000208190555050565b6000600254600360008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205461044f919061075f565b42109050919050565b60025481565b600080fd5b6000819050919050565b61047681610463565b811461048157600080fd5b50565b6000813590506104938161046d565b92915050565b6000602082840312156104af576104ae61045e565b5b60006104bd84828501610484565b91505092915050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b60006104f1826104c6565b9050919050565b610501816104e6565b82525050565b600060208201905061051c60008301846104f8565b92915050565b61052b816104e6565b811461053657600080fd5b50565b60008135905061054881610522565b92915050565b6000602082840312156105645761056361045e565b5b600061057284828501610539565b91505092915050565b61058481610463565b82525050565b600060208201905061059f600083018461057b565b92915050565b60008115159050919050565b6105ba816105a5565b82525050565b60006020820190506105d560008301846105b1565b92915050565b600082825260208201905092915050565b7f4552525f4e4f545f4f574e455200000000000000000000000000000000000000600082015250565b6000610622600d836105db565b915061062d826105ec565b602082019050919050565b6000602082019050818103600083015261065181610615565b9050919050565b7f4552525f41434345535300000000000000000000000000000000000000000000600082015250565b600061068e600a836105db565b915061069982610658565b602082019050919050565b600060208201905081810360008301526106bd81610681565b9050919050565b7f4552525f5052454d415455524500000000000000000000000000000000000000600082015250565b60006106fa600d836105db565b9150610705826106c4565b602082019050919050565b60006020820190508181036000830152610729816106ed565b9050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b600061076a82610463565b915061077583610463565b925082820190508082111561078d5761078c610730565b5b9291505056fea2646970667358221220e4dde66325e75216a8f628405c6cf3dde4da48b21720174ad6a51f40b9e4b38764736f6c63430008110033

View File

@ -0,0 +1 @@
[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"_value","type":"uint256"}],"name":"PeriodChange","type":"event"},{"inputs":[{"internalType":"address","name":"_subject","type":"address"}],"name":"check","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"lastUsed","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"period","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_subject","type":"address"}],"name":"poke","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"poker","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_period","type":"uint256"}],"name":"setPeriod","outputs":[],"stateMutability":"nonpayable","type":"function"}]

View File

@ -0,0 +1,63 @@
# Author: Louis Holbrook <dev@holbrook.no> 0826EDA1702D1E87C6E2875121D2E7BB88C2A746
# SPDX-License-Identifier: GPL-3.0-or-later
# File-version: 1
# Description: Python interface to abi and bin files for faucet contracts
# standard imports
import logging
import json
import os
# external imports
from chainlib.eth.tx import TxFactory
from chainlib.eth.constant import ZERO_ADDRESS
from chainlib.eth.contract import (
abi_decode_single,
ABIContractEncoder,
ABIContractType,
)
from erc20_faucet import Faucet
from hexathon import add_0x
logg = logging.getLogger().getChild(__name__)
moddir = os.path.dirname(__file__)
datadir = os.path.join(moddir, 'data')
class EthFaucet(Faucet):
__abi = None
__bytecode = None
__address = None
@staticmethod
def abi():
if EthFaucet.__abi == None:
f = open(os.path.join(datadir, 'EthFaucet.json'), 'r')
EthFaucet.__abi = json.load(f)
f.close()
return EthFaucet.__abi
@staticmethod
def bytecode():
if EthFaucet.__bytecode == None:
f = open(os.path.join(datadir, 'EthFaucet.bin'))
EthFaucet.__bytecode = f.read()
f.close()
return EthFaucet.__bytecode
@staticmethod
def gas(code=None):
return 2000000
# TODO: allow multiple overriders
def constructor(self, sender_address):
code = EthFaucet.bytecode()
enc = ABIContractEncoder()
code += enc.get()
tx = self.template(sender_address, None, use_nonce=True)
tx = self.set_code(tx, code)
return self.build(tx)

2
python/requirements.txt Normal file
View File

@ -0,0 +1,2 @@
chainlib-eth~=0.4.8
erc20-faucet̃~=0.5.0

View File

@ -0,0 +1,3 @@
eth_tester==0.5.0b3
py-evm==0.3.0a20
eth-erc20~=0.1.2a1

131
python/tests/test_basic.py Normal file
View File

@ -0,0 +1,131 @@
# standard imports
import os
import unittest
import logging
# external imports
from chainlib.connection import RPCConnection
from chainlib.eth.nonce import RPCNonceOracle
from chainlib.eth.unittest.ethtester import EthTesterCase
from chainlib.eth.tx import receipt
from chainlib.eth.address import to_checksum_address
from chainlib.eth.gas import balance
from chainlib.eth.gas import Gas
from chainlib.eth.gas import OverrideGasOracle
# local imports
from eth_faucet.faucet import EthFaucet
logging.basicConfig(level=logging.DEBUG)
logg = logging.getLogger()
class TestFaucet(EthTesterCase):
def setUp(self):
super(TestFaucet, self).setUp()
self.conn = RPCConnection.connect(self.chain_spec, 'default')
nonce_oracle = RPCNonceOracle(self.accounts[0], self.conn)
c = EthFaucet(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
(tx_hash_hex, o) = c.constructor(self.accounts[0])
r = self.conn.do(o)
o = receipt(r)
r = self.conn.do(o)
self.address = to_checksum_address(r['contract_address'])
logg.debug('faucet contract {}'.format(self.address))
def test_basic(self):
nonce_oracle = RPCNonceOracle(self.accounts[0], self.conn)
c = EthFaucet(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
(tx_hash_hex, o) = c.give_to(self.address, self.accounts[0], self.accounts[2])
self.conn.do(o)
o = balance(self.accounts[9])
r = self.conn.do(o)
prebalance = int(r, 16)
o = receipt(tx_hash_hex)
r = self.conn.do(o)
self.assertEqual(r['status'], 1)
o = balance(self.accounts[2])
r = self.conn.do(o)
self.assertEqual(int(r, 16), prebalance)
(tx_hash, o) = c.set_amount(self.address, self.accounts[0], 1000)
r = self.conn.do(o)
o = receipt(tx_hash_hex)
r = self.conn.do(o)
self.assertEqual(r['status'], 1)
(tx_hash_hex, o) = c.give_to(self.address, self.accounts[0], self.accounts[2])
self.conn.do(o)
o = receipt(tx_hash_hex)
r = self.conn.do(o)
self.assertEqual(r['status'], 0)
contract_gas_oracle = OverrideGasOracle(limit=21055, conn=self.conn)
cg = Gas(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle, gas_oracle=contract_gas_oracle)
(tx_hash_hex, o) = cg.create(self.accounts[0], self.address, 1000)
self.conn.do(o)
o = receipt(tx_hash_hex)
r = self.conn.do(o)
self.assertEqual(r['status'], 1)
(tx_hash_hex, o) = c.give_to(self.address, self.accounts[0], self.accounts[2])
self.conn.do(o)
o = receipt(tx_hash_hex)
r = self.conn.do(o)
self.assertEqual(r['status'], 1)
o = balance(self.accounts[2])
r = self.conn.do(o)
self.assertEqual(int(r, 16), prebalance + 1000)
def test_basic(self):
nonce_oracle = RPCNonceOracle(self.accounts[0], self.conn)
c = EthFaucet(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
contract_gas_oracle = OverrideGasOracle(limit=21055, conn=self.conn)
cg = Gas(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle, gas_oracle=contract_gas_oracle)
(tx_hash_hex, o) = cg.create(self.accounts[0], self.address, 1000)
self.conn.do(o)
o = receipt(tx_hash_hex)
r = self.conn.do(o)
self.assertEqual(r['status'], 1)
(tx_hash, o) = c.set_amount(self.address, self.accounts[0], 1000)
r = self.conn.do(o)
o = balance(self.accounts[1])
r = self.conn.do(o)
prebalance = int(r, 16)
nonce_oracle = RPCNonceOracle(self.accounts[1], self.conn)
gas_price = 1000000000
gas_oracle = OverrideGasOracle(limit=100000, price=gas_price, conn=self.conn)
c = EthFaucet(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle)
(tx_hash_hex, o) = c.gimme(self.address, self.accounts[1])
self.conn.do(o)
o = receipt(tx_hash_hex)
r = self.conn.do(o)
logg.debug('rrrr {}'.format(r))
cost = r['gas_used'] * gas_price
self.assertEqual(r['status'], 1)
o = balance(self.accounts[1])
r = self.conn.do(o)
self.assertEqual(int(r, 16), prebalance - cost + 1000)
if __name__ == '__main__':
unittest.main()

View File

@ -8,16 +8,16 @@ contract EthFacuet {
address public registry;
address public periodChecker;
uint256 public sealState;
uint256 public value;
uint256 public amount;
uint8 constant REGISTRY_STATE = 1;
uint8 constant PERIODCHECKER_STATE = 2;
uint8 constant VALUE_STATE = 4;
uint256 constant public maxSealState = 7;
event FaucetUsed(address indexed _recipient, address indexed _token, uint256 _value);
event FaucetFail(address indexed _recipient, address indexed _token, uint256 _value);
event FaucetAmountChange(uint256 _value);
event FaucetUsed(address indexed _recipient, address indexed _token, uint256 _amount);
event FaucetFail(address indexed _recipient, address indexed _token, uint256 _amount);
event FaucetAmountChange(uint256 _amount);
event FaucetStateChange(uint256 indexed _sealState, address _registry, address _periodChecker);
constructor() {
@ -32,12 +32,12 @@ contract EthFacuet {
return uint256(sealState);
}
function setValue(uint256 _v) public returns(uint256) {
function setAmount(uint256 _v) public returns(uint256) {
require(msg.sender == owner, 'ERR_NOT_OWNER');
require(sealState & VALUE_STATE == 0, 'ERR_SEALED');
value = _v;
emit FaucetAmountChange(value);
return value;
amount = _v;
emit FaucetAmountChange(amount);
return amount;
}
function setPeriodChecker(address _checker) public {
@ -74,11 +74,11 @@ contract EthFacuet {
(_ok, _result) = registry.call(abi.encodeWithSignature("have(address)", _recipient));
if (!_ok) {
emit FaucetFail(_recipient, address(0), value);
emit FaucetFail(_recipient, address(0), amount);
revert('ERR_TRANSFER');
}
emit FaucetUsed(_recipient, address(0), value);
emit FaucetUsed(_recipient, address(0), amount);
return true;
}
@ -93,16 +93,16 @@ contract EthFacuet {
}
function get() public returns(uint256) {
check(msg.sender);
payable(msg.sender).transfer(value);
return value;
function gimme() public returns(uint256) {
require(check(msg.sender));
payable(msg.sender).transfer(amount);
return amount;
}
function to(address _recipient) public returns(uint256) {
check(_recipient);
payable(_recipient).transfer(value);
return value;
function giveTo(address _recipient) public returns(uint256) {
require(check(_recipient));
payable(_recipient).transfer(amount);
return amount;
}
receive () payable external {

13
solidity/Makefile Normal file
View File

@ -0,0 +1,13 @@
SOLC = /usr/bin/solc
all:
$(SOLC) --bin EthFaucet.sol --evm-version byzantium | awk 'NR>3' > EthFaucet.bin
truncate -s -1 EthFaucet.bin
$(SOLC) --abi EthFaucet.sol --evm-version byzantium | awk 'NR>3' > EthFaucet.json
$(SOLC) --bin PeriodSimple.sol --evm-version byzantium | awk 'NR>3' > PeriodSimple.bin
truncate -s -1 PeriodSimple.bin
$(SOLC) --abi PeriodSimple.sol --evm-version byzantium | awk 'NR==4' > PeriodSimple.json
install: all
cp -v *{json,bin} ../python/eth_faucet/data/