From d5b494fa2140de147735cd68d7ac694261d78246 Mon Sep 17 00:00:00 2001 From: nolash Date: Tue, 13 Jul 2021 09:21:46 +0200 Subject: [PATCH] Add faucet disburse to pure eth import, remove eth-abi from verify --- apps/data-seeding/eth/import_balance.py | 33 ++++++-- apps/data-seeding/requirements.txt | 2 + apps/data-seeding/verify.py | 108 +++++++++--------------- 3 files changed, 67 insertions(+), 76 deletions(-) diff --git a/apps/data-seeding/eth/import_balance.py b/apps/data-seeding/eth/import_balance.py index 9c9816b5..e70bce49 100644 --- a/apps/data-seeding/eth/import_balance.py +++ b/apps/data-seeding/eth/import_balance.py @@ -41,6 +41,7 @@ from cic_base.eth.syncer import chain_interface from eth_accounts_index import AccountsIndex from eth_contract_registry import Registry from eth_token_index import TokenUniqueSymbolIndex +from erc20_faucet import Faucet logging.basicConfig(level=logging.WARNING) @@ -55,7 +56,7 @@ argparser.add_argument('-c', type=str, default=config_dir, help='config root to argparser.add_argument('--old-chain-spec', type=str, dest='old_chain_spec', default='evm:oldchain:1', help='chain spec') argparser.add_argument('-i', '--chain-spec', type=str, dest='i', help='chain spec') argparser.add_argument('-r', '--registry-address', type=str, dest='r', help='CIC Registry address') -argparser.add_argument('--token-symbol', default='SRF', type=str, dest='token_symbol', help='Token symbol to use for trnsactions') +argparser.add_argument('--token-symbol', default='GFT', type=str, dest='token_symbol', help='Token symbol to use for trnsactions') argparser.add_argument('--head', action='store_true', help='start at current block height (overrides --offset)') argparser.add_argument('--env-prefix', default=os.environ.get('CONFINI_ENV_PREFIX'), dest='env_prefix', type=str, help='environment prefix for variables to overwrite configuration') argparser.add_argument('-q', type=str, default='cic-eth', help='celery queue to submit transaction tasks to') @@ -117,12 +118,16 @@ class Handler: account_index_add_signature = keccak256_string_to_hex('add(address)')[:8] - def __init__(self, conn, chain_spec, user_dir, balances, token_address, signer, gas_oracle, nonce_oracle): + def __init__(self, conn, chain_spec, user_dir, balances, token_address, faucet_address, signer_address, signer, gas_oracle, nonce_oracle): self.token_address = token_address + self.faucet_address = faucet_address self.user_dir = user_dir self.balances = balances self.chain_spec = chain_spec - self.tx_factory = ERC20(chain_spec, signer, gas_oracle, nonce_oracle) + self.nonce_oracle = nonce_oracle + self.gas_oracle = gas_oracle + self.signer_address = signer_address + self.signer = signer def name(self): @@ -164,11 +169,14 @@ class Handler: return # TODO: store token object in handler ,get decimals from there - multiplier = 10**6 + erc20 = ERC20(self.chain_spec, signer=self.signer, gas_oracle=self.gas_oracle, nonce_oracle=self.nonce_oracle) + o = erc20.decimals(self.token_address) + r = conn.do(o) + decimals = erc20.parse_decimals(r) + multiplier = 10 ** decimals balance_full = balance * multiplier logg.info('registered {} originally {} ({}) tx hash {} balance {}'.format(recipient, original_address, u, tx.hash, balance_full)) - - (tx_hash_hex, o) = self.tx_factory.transfer(self.token_address, signer_address, recipient, balance_full) + (tx_hash_hex, o) = erc20.transfer(self.token_address, self.signer_address, recipient, balance_full) logg.info('submitting erc20 transfer tx {} for recipient {}'.format(tx_hash_hex, recipient)) r = conn.do(o) @@ -181,6 +189,10 @@ class Handler: f.write(strip_0x(o['params'][0])) f.close() + faucet = Faucet(self.chain_spec, signer=self.signer, gas_oracle=self.gas_oracle, nonce_oracle=self.nonce_oracle) + (tx_hash, o) = faucet.give_to(self.faucet_address, self.signer_address, recipient) + r = conn.do(o) + def progress_callback(block_number, tx_index): sys.stdout.write(str(block_number).ljust(200) + "\n") @@ -202,6 +214,13 @@ def main(): token_index_address = to_checksum_address(token_index_address) logg.info('found token index address {}'.format(token_index_address)) + # Get Faucet address + o = registry.address_of(config.get('CIC_REGISTRY_ADDRESS'), 'Faucet') + r = conn.do(o) + faucet_address = registry.parse_address_of(r) + faucet_address = to_checksum_address(faucet_address) + logg.info('found faucet {}'.format(faucet_address)) + # Get Sarafu token address token_index = TokenUniqueSymbolIndex(chain_spec) o = token_index.address_of(token_index_address, token_symbol) @@ -244,7 +263,7 @@ def main(): syncer_backend.set(block_offset, 0) syncer = HeadSyncer(syncer_backend, chain_interface, block_callback=progress_callback) - handler = Handler(conn, chain_spec, user_dir, balances, token_address, signer, gas_oracle, nonce_oracle) + handler = Handler(conn, chain_spec, user_dir, balances, token_address, faucet_address, signer_address, signer, gas_oracle, nonce_oracle) syncer.add_filter(handler) syncer.loop(1, conn) diff --git a/apps/data-seeding/requirements.txt b/apps/data-seeding/requirements.txt index f1350e97..b9b7005f 100644 --- a/apps/data-seeding/requirements.txt +++ b/apps/data-seeding/requirements.txt @@ -9,3 +9,5 @@ eth-address-index~=0.1.2a1 eth-contract-registry~=0.5.6a1 eth-accounts-index~=0.0.12a1 eth-erc20~=0.0.10a3 +erc20-faucet~=0.2.2a1 +faker==4.17.1 diff --git a/apps/data-seeding/verify.py b/apps/data-seeding/verify.py index 09813d9b..01bcee82 100644 --- a/apps/data-seeding/verify.py +++ b/apps/data-seeding/verify.py @@ -14,7 +14,6 @@ import urllib.parse # external imports import celery import confini -import eth_abi from chainlib.chain import ChainSpec from chainlib.eth.address import to_checksum_address from chainlib.eth.connection import EthHTTPConnection @@ -33,6 +32,9 @@ from cic_types.models.person import ( from erc20_faucet import Faucet from eth_erc20 import ERC20 from hexathon.parse import strip_0x, add_0x +from eth_contract_registry import Registry +from eth_accounts_index import AccountsIndex +from eth_token_index import TokenUniqueSymbolIndex logging.basicConfig(level=logging.WARNING) logg = logging.getLogger() @@ -43,7 +45,8 @@ custodial_tests = [ 'local_key', 'gas', 'faucet', - 'ussd' + 'ussd', + 'ussd_pins', ] metadata_tests = [ @@ -71,6 +74,7 @@ argparser.add_argument('-i', '--chain-spec', type=str, dest='i', help='chain spe argparser.add_argument('--meta-provider', type=str, dest='meta_provider', default='http://localhost:63380', help='cic-meta url') argparser.add_argument('--ussd-provider', type=str, dest='ussd_provider', default='http://localhost:63315', help='cic-ussd url') argparser.add_argument('--skip-custodial', dest='skip_custodial', action='store_true', help='skip all custodial verifications') +argparser.add_argument('--skip-metadata', dest='skip_metadata', action='store_true', help='skip all metadata verifications') argparser.add_argument('--exclude', action='append', type=str, default=[], help='skip specified verification') argparser.add_argument('--include', action='append', type=str, help='include specified verification') argparser.add_argument('--token-symbol', default='GFT', type=str, dest='token_symbol', help='Token symbol to use for trnsactions') @@ -130,6 +134,11 @@ if args.skip_custodial: for t in custodial_tests: if t not in exclude: exclude.append(t) +if args.skip_metadata: + logg.info('will skip all metadata verifications ({})'.format(','.join(metadata_tests))) + for t in metadata_tests: + if t not in exclude: + exclude.append(t) for t in include: if t not in all_tests: raise ValueError('Cannot include unknown verification "{}"'.format(t)) @@ -263,19 +272,11 @@ class Verifier: def verify_accounts_index(self, address, balance=None): - tx = self.tx_factory.template(ZERO_ADDRESS, self.index_address) - data = keccak256_string_to_hex('have(address)')[:8] - data += eth_abi.encode_single('address', address).hex() - tx = self.tx_factory.set_code(tx, data) - tx = self.tx_factory.normalize(tx) - j = JSONRPCRequest() - o = j.template() - o['method'] = 'eth_call' - o['params'].append(tx) - o = j.finalize(o) + accounts_index = AccountsIndex(self.chain_spec) + o = accounts_index.have(self.index_address, address) r = self.conn.do(o) - logg.debug('index check for {}: {}'.format(address, r)) - n = eth_abi.decode_single('uint256', bytes.fromhex(strip_0x(r))) + n = accounts_index.parse_have(r) + logg.debug('index check for {}: {}'.format(address, n)) if n != 1: raise VerifierError(n, 'accounts index') @@ -427,70 +428,39 @@ def main(): gas_oracle = OverrideGasOracle(conn=conn, limit=8000000) # Get Token registry address - txf = TxFactory(chain_spec, signer=None, gas_oracle=gas_oracle, nonce_oracle=None) - tx = txf.template(ZERO_ADDRESS, config.get('CIC_REGISTRY_ADDRESS')) - - # TODO: replace with cic-eth-registry - registry_addressof_method = keccak256_string_to_hex('addressOf(bytes32)')[:8] - data = add_0x(registry_addressof_method) - data += eth_abi.encode_single('bytes32', b'TokenRegistry').hex() - txf.set_code(tx, data) - - j = JSONRPCRequest() - o = j.template() - o['method'] = 'eth_call' - o['params'].append(txf.normalize(tx)) - o['params'].append('latest') - o = j.finalize(o) + registry = Registry(chain_spec) + o = registry.address_of(config.get('CIC_REGISTRY_ADDRESS'), 'TokenRegistry') r = conn.do(o) - token_index_address = to_checksum_address(eth_abi.decode_single('address', bytes.fromhex(strip_0x(r)))) + token_index_address = registry.parse_address_of(r) + token_index_address = to_checksum_address(token_index_address) logg.info('found token index address {}'.format(token_index_address)) - data = add_0x(registry_addressof_method) - data += eth_abi.encode_single('bytes32', b'AccountRegistry').hex() - txf.set_code(tx, data) - - o = j.template() - o['method'] = 'eth_call' - o['params'].append(txf.normalize(tx)) - o['params'].append('latest') - o = j.finalize(o) + # Get Account registry address + o = registry.address_of(config.get('CIC_REGISTRY_ADDRESS'), 'AccountRegistry') r = conn.do(o) - account_index_address = to_checksum_address(eth_abi.decode_single('address', bytes.fromhex(strip_0x(r)))) + account_index_address = registry.parse_address_of(r) + account_index_address = to_checksum_address(account_index_address) logg.info('found account index address {}'.format(account_index_address)) - data = add_0x(registry_addressof_method) - data += eth_abi.encode_single('bytes32', b'Faucet').hex() - txf.set_code(tx, data) - - o = j.template() - o['method'] = 'eth_call' - o['params'].append(txf.normalize(tx)) - o['params'].append('latest') - o = j.finalize(o) + # Get Faucet address + o = registry.address_of(config.get('CIC_REGISTRY_ADDRESS'), 'Faucet') r = conn.do(o) - faucet_address = to_checksum_address(eth_abi.decode_single('address', bytes.fromhex(strip_0x(r)))) + faucet_address = registry.parse_address_of(r) + faucet_index_address = to_checksum_address(token_index_address) logg.info('found faucet {}'.format(faucet_address)) - - - # Get Sarafu token address - tx = txf.template(ZERO_ADDRESS, token_index_address) - data = add_0x(registry_addressof_method) - h = hashlib.new('sha256') - h.update(token_symbol.encode('utf-8')) - z = h.digest() - data += eth_abi.encode_single('bytes32', z).hex() - txf.set_code(tx, data) - o = j.template() - o['method'] = 'eth_call' - o['params'].append(txf.normalize(tx)) - o['params'].append('latest') - o = j.finalize(o) +# Get Sarafu token address + token_index = TokenUniqueSymbolIndex(chain_spec) + o = token_index.address_of(token_index_address, token_symbol) r = conn.do(o) - sarafu_token_address = to_checksum_address(eth_abi.decode_single('address', bytes.fromhex(strip_0x(r)))) - logg.info('found token address {}'.format(sarafu_token_address)) - + token_address = token_index.parse_address_of(r) + try: + token_address = to_checksum_address(token_address) + except ValueError as e: + logg.critical('lookup failed for token {}: {}'.format(token_symbol, e)) + sys.exit(1) + logg.info('found token address {}'.format(token_address)) + balances = {} f = open('{}/balances.csv'.format(user_dir, 'r')) i = 0 @@ -511,7 +481,7 @@ def main(): f.close() - verifier = Verifier(conn, api, gas_oracle, chain_spec, account_index_address, sarafu_token_address, faucet_address, user_dir, exit_on_error) + verifier = Verifier(conn, api, gas_oracle, chain_spec, account_index_address, token_address, faucet_address, user_dir, exit_on_error) user_new_dir = os.path.join(user_dir, 'new') i = 0