From c35247b8e64e56ced295a7b1cc4f78e4fccda9d1 Mon Sep 17 00:00:00 2001 From: Louis Holbrook Date: Mon, 8 Feb 2021 21:39:50 +0000 Subject: [PATCH] Consolidate cic-eth tooling --- apps/cic-eth/CHANGELOG | 2 + apps/cic-eth/cic_eth/runnable/create.py | 77 ++++++++++++++++++++ apps/cic-eth/cic_eth/runnable/resend.py | 95 +++++++++++++++++++++++++ apps/cic-eth/cic_eth/version.py | 2 +- apps/cic-eth/setup.cfg | 7 +- 5 files changed, 181 insertions(+), 2 deletions(-) create mode 100644 apps/cic-eth/cic_eth/runnable/create.py create mode 100644 apps/cic-eth/cic_eth/runnable/resend.py diff --git a/apps/cic-eth/CHANGELOG b/apps/cic-eth/CHANGELOG index 266e4814..445a22e6 100644 --- a/apps/cic-eth/CHANGELOG +++ b/apps/cic-eth/CHANGELOG @@ -9,6 +9,8 @@ * Add pure tcp and redis task api callbacks * Add optional outgoing log status tracing * Add lock lister and lock/unlock cli tool + * Add resend executable tool + * Add account create executable tool - 0.9.0 * Require chain spec parameter in api * Pass chain spec between tasks diff --git a/apps/cic-eth/cic_eth/runnable/create.py b/apps/cic-eth/cic_eth/runnable/create.py new file mode 100644 index 00000000..6ceb811c --- /dev/null +++ b/apps/cic-eth/cic_eth/runnable/create.py @@ -0,0 +1,77 @@ +#!/usr/bin/python +#import socket +import sys +import os +import logging +import uuid +import json + +import celery +from cic_eth.api import Api +import confini +import argparse +import redis + +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('--redis-host', dest='redis_host', default='localhost', type=str, help='redis host to use for task submission') +argparser.add_argument('--redis-port', dest='redis_port', default=6379, type=int, help='redis host to use for task submission') +argparser.add_argument('--redis-db', dest='redis_db', default=0, 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') +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') +celery_app = celery.Celery(broker=config.get('CELERY_BROKER_URL'), backend=config.get('CELERY_RESULT_URL')) + +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) + +ps.get_message() +m = ps.get_message(timeout=args.timeout) +print(json.loads(m['data'])) diff --git a/apps/cic-eth/cic_eth/runnable/resend.py b/apps/cic-eth/cic_eth/runnable/resend.py new file mode 100644 index 00000000..b455b772 --- /dev/null +++ b/apps/cic-eth/cic_eth/runnable/resend.py @@ -0,0 +1,95 @@ +# standard imports +import logging +import argparse +import re +import os + +# third-party imports +import celery +import confini +import web3 +from cic_registry import CICRegistry +from cic_registry.chain import ChainSpec +from cic_registry.chain import ChainRegistry + +# local imports +from cic_eth.eth.rpc import RpcClient +from cic_eth.api.api_admin import AdminApi + +logging.basicConfig(level=logging.WARNING) +logg = logging.getLogger() + +logging.getLogger('web3').setLevel(logging.WARNING) +logging.getLogger('urllib3').setLevel(logging.WARNING) + +default_config_dir = os.path.join('/usr/local/etc/cic-eth') + + +argparser = argparse.ArgumentParser() +argparser.add_argument('-c', type=str, default=default_config_dir, help='config root to use') +argparser.add_argument('-p', '--provider', dest='p', default='http://localhost:8545', type=str, help='Web3 provider url (http only)') +argparser.add_argument('-i', '--chain-spec', dest='i', type=str, default='Ethereum:1', help='Chain specification string') +argparser.add_argument('--unlock', action='store_true', help='Append task to unlock account') +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('-v', action='store_true', help='Be verbose') +argparser.add_argument('-vv', action='store_true', help='Be more verbose') +argparser.add_argument('tx_hash', type=str, help='Transaction hash') +args = argparser.parse_args() + + +if args.vv: + logg.setLevel(logging.DEBUG) +elif args.v: + logg.setLevel(logging.INFO) + +config_dir = os.path.join(args.c) +os.makedirs(config_dir, 0o777, True) +config = confini.Config(config_dir, args.env_prefix) +config.process() +args_override = { + 'ETH_PROVIDER': getattr(args, 'p'), + 'CIC_CHAIN_SPEC': getattr(args, 'i'), + } + +# override args +config.censor('PASSWORD', 'DATABASE') +config.censor('PASSWORD', 'SSL') +logg.debug('config loaded from {}:\n{}'.format(config_dir, config)) + +chain_spec = ChainSpec.from_chain_str(args.i) +chain_str = str(chain_spec) + +re_websocket = re.compile('^wss?://') +re_http = re.compile('^https?://') +blockchain_provider = config.get('ETH_PROVIDER') +if re.match(re_websocket, blockchain_provider) != None: + blockchain_provider = web3.Web3.WebsocketProvider(blockchain_provider) +elif re.match(re_http, blockchain_provider) != None: + blockchain_provider = web3.Web3.HTTPProvider(blockchain_provider) +else: + raise ValueError('unknown provider url {}'.format(blockchain_provider)) + +def web3_constructor(): + w3 = web3.Web3(blockchain_provider) + return (blockchain_provider, w3) +RpcClient.set_constructor(web3_constructor) + +celery_app = celery.Celery(broker=config.get('CELERY_BROKER_URL'), backend=config.get('CELERY_RESULT_URL')) + +c = RpcClient(chain_spec) + +CICRegistry.init(c.w3, config.get('CIC_REGISTRY_ADDRESS'), chain_spec) +chain_registry = ChainRegistry(chain_spec) +CICRegistry.add_chain_registry(chain_registry) +CICRegistry.add_path(config.get('ETH_ABI_DIR')) +CICRegistry.load_for(chain_spec) + + +def main(): + api = AdminApi(c) + tx_details = api.tx(chain_spec, args.tx_hash) + t = api.resend(args.tx_hash, chain_str, unlock=True) + + +if __name__ == '__main__': + main() diff --git a/apps/cic-eth/cic_eth/version.py b/apps/cic-eth/cic_eth/version.py index 6a95f248..891deebc 100644 --- a/apps/cic-eth/cic_eth/version.py +++ b/apps/cic-eth/cic_eth/version.py @@ -10,7 +10,7 @@ version = ( 0, 10, 0, - 'alpha.22', + 'alpha.23', ) version_object = semver.VersionInfo( diff --git a/apps/cic-eth/setup.cfg b/apps/cic-eth/setup.cfg index 5d5a5c4a..770fb684 100644 --- a/apps/cic-eth/setup.cfg +++ b/apps/cic-eth/setup.cfg @@ -40,10 +40,15 @@ scripts = [options.entry_points] console_scripts = + # daemons cic-eth-tasker = cic_eth.runnable.tasker:main cic-eth-manager = cic_eth.runnable.manager:main - cic-eth-tag = cic_eth.runnable.tag:main cic-eth-dispatcher = cic_eth.runnable.dispatcher:main cic-eth-retrier = cic_eth.runnable.retry:main + # tools + cic-eth-create = cic_eth.runnable.create:main cic-eth-inspect = cic_eth.runnable.view:main cic-eth-ctl = cic_eth.runnable.ctrl:main + # 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