From 206a585fd123d568ee0fb7be7712580b94a2a32c Mon Sep 17 00:00:00 2001 From: Louis Holbrook Date: Tue, 30 Mar 2021 16:33:09 +0000 Subject: [PATCH] Add declarator to tasker registry instantiation --- apps/cic-eth/cic_eth/registry.py | 33 ++++++ .../cic_eth/runnable/daemons/tasker.py | 15 +-- apps/cic-eth/cic_eth/runnable/transfer.py | 100 ++++++++++++++++++ apps/cic-eth/cic_eth/runnable/view.py | 18 +--- apps/cic-eth/cic_eth/version.py | 2 +- apps/cic-eth/setup.cfg | 1 + 6 files changed, 148 insertions(+), 21 deletions(-) create mode 100644 apps/cic-eth/cic_eth/registry.py create mode 100644 apps/cic-eth/cic_eth/runnable/transfer.py diff --git a/apps/cic-eth/cic_eth/registry.py b/apps/cic-eth/cic_eth/registry.py new file mode 100644 index 00000000..247bf994 --- /dev/null +++ b/apps/cic-eth/cic_eth/registry.py @@ -0,0 +1,33 @@ +# standard imports +import logging + +# external imports +from cic_eth_registry import CICRegistry +from cic_eth_registry.lookup.declarator import AddressDeclaratorLookup +from cic_eth_registry.lookup.tokenindex import TokenIndexLookup + +logg = logging.getLogger() + + +def connect_token_registry(rpc, chain_spec): + registry = CICRegistry(chain_spec, rpc) + token_registry_address = registry.by_name('TokenRegistry') + logg.debug('using token registry address {}'.format(token_registry_address)) + lookup = TokenIndexLookup(token_registry_address) + CICRegistry.add_lookup(lookup) + + +def connect_declarator(rpc, chain_spec, trusted_addresses): + registry = CICRegistry(chain_spec, rpc) + declarator_address = registry.by_name('AddressDeclarator') + logg.debug('using declarator address {}'.format(declarator_address)) + lookup = AddressDeclaratorLookup(declarator_address, trusted_addresses) + CICRegistry.add_lookup(lookup) + + +def connect(rpc, chain_spec, registry_address): + CICRegistry.address = registry_address + registry = CICRegistry(chain_spec, rpc) + registry_address = registry.by_name('ContractRegistry') + + return registry diff --git a/apps/cic-eth/cic_eth/runnable/daemons/tasker.py b/apps/cic-eth/cic_eth/runnable/daemons/tasker.py index 2c679524..fd316be0 100644 --- a/apps/cic-eth/cic_eth/runnable/daemons/tasker.py +++ b/apps/cic-eth/cic_eth/runnable/daemons/tasker.py @@ -16,8 +16,6 @@ from chainlib.eth.connection import EthUnixSignerConnection from chainlib.chain import ChainSpec # local imports -from cic_eth_registry import CICRegistry - from cic_eth.eth import erc20 from cic_eth.eth import tx from cic_eth.eth import account @@ -33,6 +31,11 @@ from cic_eth.db.models.base import SessionBase from cic_eth.db.models.otx import Otx from cic_eth.db import dsn_from_config from cic_eth.ext import tx +from cic_eth.registry import ( + connect as connect_registry, + connect_declarator, + connect_token_registry, + ) logging.basicConfig(level=logging.WARNING) logg = logging.getLogger() @@ -121,8 +124,6 @@ RPCConnection.register_location(config.get('SIGNER_SOCKET_PATH'), chain_spec, 's Otx.tracing = config.true('TASKS_TRACE_QUEUE_STATUS') -CICRegistry.address = config.get('CIC_REGISTRY_ADDRESS') - def main(): argv = ['worker'] @@ -145,8 +146,8 @@ def main(): # Callback.ssl_ca_file = config.get('SSL_CA_FILE') rpc = RPCConnection.connect(chain_spec, 'default') - registry = CICRegistry(chain_spec, rpc) - registry_address = registry.by_name('ContractRegistry') + + connect_registry(rpc, chain_spec, config.get('CIC_REGISTRY_ADDRESS')) trusted_addresses_src = config.get('CIC_TRUST_ADDRESS') if trusted_addresses_src == None: @@ -155,6 +156,8 @@ def main(): trusted_addresses = trusted_addresses_src.split(',') for address in trusted_addresses: logg.info('using trusted address {}'.format(address)) + connect_declarator(rpc, chain_spec, trusted_addresses) + connect_token_registry(rpc, chain_spec) current_app.worker_main(argv) diff --git a/apps/cic-eth/cic_eth/runnable/transfer.py b/apps/cic-eth/cic_eth/runnable/transfer.py new file mode 100644 index 00000000..6f408785 --- /dev/null +++ b/apps/cic-eth/cic_eth/runnable/transfer.py @@ -0,0 +1,100 @@ +#!/usr/bin/python +import sys +import os +import logging +import uuid +import json +import argparse + +# external imports +import celery +import confini +import redis +from xdg.BaseDirectory import xdg_config_home +from chainlib.eth.address import to_checksum_address + +# local imports +from cic_eth.api import Api + +logging.basicConfig(level=logging.WARNING) +logg = logging.getLogger('create_account_script') +logging.getLogger('confini').setLevel(logging.WARNING) +logging.getLogger('gnupg').setLevel(logging.WARNING) + +default_config_dir = os.environ.get('CONFINI_DIR', '/usr/local/etc/cic') + +argparser = argparse.ArgumentParser() +argparser.add_argument('--no-register', dest='no_register', action='store_true', help='Do not register new account in on-chain accounts index') +argparser.add_argument('-c', type=str, default=default_config_dir, help='config file') +argparser.add_argument('-i', '--chain-spec', dest='i', type=str, help='chain spec') +argparser.add_argument('--token-symbol', dest='token_symbol', type=str, help='Symbol of token to transfer') +argparser.add_argument('--redis-host', dest='redis_host', type=str, help='redis host to use for task submission') +argparser.add_argument('--redis-port', dest='redis_port', type=int, help='redis host to use for task submission') +argparser.add_argument('--redis-db', dest='redis_db', type=int, help='redis db to use for task submission and callback') +argparser.add_argument('--redis-host-callback', dest='redis_host_callback', default='localhost', type=str, help='redis host to use for callback') +argparser.add_argument('--redis-port-callback', dest='redis_port_callback', default=6379, type=int, help='redis port to use for callback') +argparser.add_argument('--timeout', default=20.0, type=float, help='Callback timeout') +argparser.add_argument('-q', type=str, default='cic-eth', help='Task queue') +argparser.add_argument('-v', action='store_true', help='Be verbose') +argparser.add_argument('-vv', action='store_true', help='Be more verbose') +argparser.add_argument('sender', type=str, help='Transaction sender') +argparser.add_argument('recipient', type=str, help='Transaction recipient') +argparser.add_argument('value', type=int, help='Transaction value with decimals') +args = argparser.parse_args() + +if args.vv: + logg.setLevel(logging.DEBUG) +if args.v: + logg.setLevel(logging.INFO) + +config_dir = args.c +config = confini.Config(config_dir, os.environ.get('CONFINI_ENV_PREFIX')) +config.process() +args_override = { + 'CIC_CHAIN_SPEC': getattr(args, 'i'), + 'REDIS_HOST': getattr(args, 'redis_host'), + 'REDIS_PORT': getattr(args, 'redis_port'), + 'REDIS_DB': getattr(args, 'redis_db'), + } +config.dict_override(args_override, 'cli') +config.add(to_checksum_address(args.sender), '_SENDER', True) +config.add(to_checksum_address(args.recipient), '_RECIPIENT', True) +config.add(args.value, '_VALUE', True) +config.add(args.token_symbol, '_SYMBOL', True) +if config.get('_SYMBOL') == None: + raise ValueError('gas transfers not yet supported; token symbol required') +celery_app = celery.Celery(broker=config.get('CELERY_BROKER_URL'), backend=config.get('CELERY_RESULT_URL')) + + +def main(): + redis_host = config.get('REDIS_HOST') + redis_port = config.get('REDIS_PORT') + redis_db = config.get('REDIS_DB') + redis_channel = str(uuid.uuid4()) + r = redis.Redis(redis_host, redis_port, redis_db) + + ps = r.pubsub() + ps.subscribe(redis_channel) + ps.get_message() + + api = Api( + config.get('CIC_CHAIN_SPEC'), + queue=args.q, + callback_param='{}:{}:{}:{}'.format(args.redis_host_callback, args.redis_port_callback, redis_db, redis_channel), + callback_task='cic_eth.callbacks.redis.redis', + callback_queue=args.q, + ) + + #register = not args.no_register + #logg.debug('register {}'.format(register)) + #t = api.create_account(register=register) + t = api.transfer(config.get('_SENDER'), config.get('_RECIPIENT'), config.get('_VALUE'), config.get('_SYMBOL')) + + ps.get_message() + o = ps.get_message(timeout=args.timeout) + m = json.loads(o['data']) + print(m['result']) + + +if __name__ == '__main__': + main() diff --git a/apps/cic-eth/cic_eth/runnable/view.py b/apps/cic-eth/cic_eth/runnable/view.py index 66e08a01..50adce37 100644 --- a/apps/cic-eth/cic_eth/runnable/view.py +++ b/apps/cic-eth/cic_eth/runnable/view.py @@ -14,8 +14,6 @@ import datetime # external imports import confini import celery -from cic_eth_registry import CICRegistry -from cic_eth_registry.lookup.declarator import AddressDeclaratorLookup from chainlib.chain import ChainSpec from chainlib.eth.connection import EthHTTPConnection from hexathon import add_0x @@ -27,6 +25,7 @@ from cic_eth.db.enum import ( status_str, LockEnum, ) +from cic_eth.registry import connect as connect_registry logging.basicConfig(level=logging.WARNING) logg = logging.getLogger() @@ -147,28 +146,19 @@ def render_lock(o, **kwargs): return s -def connect_registry(registry_address, chain_spec, rpc): - CICRegistry.address = registry_address - registry = CICRegistry(chain_spec, rpc) - declarator_address = registry.by_name('AddressDeclarator') - lookup = AddressDeclaratorLookup(declarator_address, trusted_addresses) - registry.add_lookup(lookup) - return registry - - # TODO: move each command to submodule def main(): txs = [] renderer = render_tx if len(config.get('_QUERY')) > 66: - registry = connect_registry(registry_address, chain_spec, rpc) + registry = connect_registry(rpc, chain_spec, registry_address) admin_api.tx(chain_spec, tx_raw=config.get('_QUERY'), registry=registry, renderer=renderer) elif len(config.get('_QUERY')) > 42: - registry = connect_registry(registry_address, chain_spec, rpc) + registry = connect_registry(rpc, chain_spec, registry_address) admin_api.tx(chain_spec, tx_hash=config.get('_QUERY'), registry=registry, renderer=renderer) elif len(config.get('_QUERY')) == 42: - registry = connect_registry(registry_address, chain_spec, rpc) + registry = connect_registry(rpc, chain_spec, registry_address) txs = admin_api.account(chain_spec, config.get('_QUERY'), include_recipient=False, renderer=render_account) renderer = render_account elif len(config.get('_QUERY')) >= 4 and config.get('_QUERY')[:4] == 'lock': diff --git a/apps/cic-eth/cic_eth/version.py b/apps/cic-eth/cic_eth/version.py index 0d7e8016..0245ce93 100644 --- a/apps/cic-eth/cic_eth/version.py +++ b/apps/cic-eth/cic_eth/version.py @@ -10,7 +10,7 @@ version = ( 0, 10, 1, - 'beta.1', + 'beta.2', ) version_object = semver.VersionInfo( diff --git a/apps/cic-eth/setup.cfg b/apps/cic-eth/setup.cfg index 8d7b8b94..87476a3f 100644 --- a/apps/cic-eth/setup.cfg +++ b/apps/cic-eth/setup.cfg @@ -54,3 +54,4 @@ console_scripts = # TODO: Merge this with ctl when subcmds sorted to submodules cic-eth-tag = cic_eth.runnable.tag:main cic-eth-resend = cic_eth.runnable.resend:main + cic-eth-transfer = cic_eth.runnable.transfer:main