From 24e2e62924f4c8edddf2d353295b61cf27bfa0b5 Mon Sep 17 00:00:00 2001 From: nolash Date: Mon, 18 Oct 2021 10:47:48 +0200 Subject: [PATCH] Add eth initializer --- cic/cmd/ext.py | 30 +++++++++++++++++++++++++++++ cic/data/network_template_v0.json | 1 + cic/ext/eth/start.py | 21 ++++++++++++++++++++ cic/meta.py | 2 +- cic/network.py | 32 +++++++++++++++++++++++++------ cic/proof.py | 2 +- cic/runnable/cic_cmd.py | 4 ++++ cic/token.py | 2 +- eth_requirements.txt | 6 ++++++ setup.cfg | 3 --- setup.py | 14 ++++++++++++++ 11 files changed, 105 insertions(+), 12 deletions(-) create mode 100644 cic/cmd/ext.py create mode 100644 cic/ext/eth/start.py create mode 100644 eth_requirements.txt diff --git a/cic/cmd/ext.py b/cic/cmd/ext.py new file mode 100644 index 0000000..789938d --- /dev/null +++ b/cic/cmd/ext.py @@ -0,0 +1,30 @@ +# standard imports +import importlib + +# external imports +from chainlib.chain import ChainSpec +# local imports +from cic.network import Network + + +def process_args(argparser): + argparser.add_argument('--registry', required=True, type=str, help='contract registry address') + argparser.add_argument('-d', '--directory', type=str, dest='directory', default='.', help='directory') + argparser.add_argument('-p', type=str, help='RPC endpoint') + argparser.add_argument('-i', type=str, help='chain spec string') + argparser.add_argument('target', help='target to initialize') + + +def validate_args(args): + pass + + +def execute(config, eargs): + cn = Network(eargs.directory, targets=eargs.target) + cn.load() + + + chain_spec = ChainSpec.from_chain_str(eargs.i) + m = importlib.import_module('cic.ext.{}.start'.format(eargs.target)) + m.extension_start(cn, registry_address=eargs.registry, chain_spec=chain_spec, rpc_provider=config.get('RPC_PROVIDER')) + diff --git a/cic/data/network_template_v0.json b/cic/data/network_template_v0.json index 741c09c..bb6fe35 100644 --- a/cic/data/network_template_v0.json +++ b/cic/data/network_template_v0.json @@ -1,4 +1,5 @@ { + "registry": null, "chain_spec": { "arch": null, "fork": null, diff --git a/cic/ext/eth/start.py b/cic/ext/eth/start.py new file mode 100644 index 0000000..b718ec4 --- /dev/null +++ b/cic/ext/eth/start.py @@ -0,0 +1,21 @@ +# external imports +from cic_eth_registry import CICRegistry +from chainlib.eth.connection import RPCConnection + + +def extension_start(network, *args, **kwargs): + CICRegistry.address = kwargs['registry_address'] + + RPCConnection.register_location(kwargs['rpc_provider'], kwargs['chain_spec']) + conn = RPCConnection.connect(kwargs['chain_spec']) + + registry = CICRegistry(kwargs['chain_spec'], conn) + + address_declarator = registry.by_name('AddressDeclarator') + network.resource_set('eth', 'address_declarator', address_declarator) + + token_index = registry.by_name('TokenRegistry') + network.resource_set('eth', 'token_index', token_index) + + network.set('eth', kwargs['chain_spec']) + network.save() diff --git a/cic/meta.py b/cic/meta.py index f50bf4c..3deb634 100644 --- a/cic/meta.py +++ b/cic/meta.py @@ -50,7 +50,7 @@ class Meta(Data): f.close() f = open(self.meta_path, 'w') - json.dump(o, f) + json.dump(o, f, sort_keys=True, indent="\t") f.close() diff --git a/cic/network.py b/cic/network.py index f21fed7..2a5b7dc 100644 --- a/cic/network.py +++ b/cic/network.py @@ -1,6 +1,7 @@ # standard imports import os import json +import logging # external imports from chainlib.chain import ChainSpec @@ -11,12 +12,14 @@ from .base import ( data_dir, ) +logg = logging.getLogger(__name__) + class Network(Data): def __init__(self, path='.', targets=[]): super(Network, self).__init__() - self.references = None + self.resources = None self.path = path self.targets = targets self.network_path = os.path.join(self.path, 'network.json') @@ -43,13 +46,18 @@ class Network(Data): o_part = json.load(f) f.close() - f = open(self.network_path, 'w') - o = {'resources': {}} - + self.resources = {} for v in self.targets: - o['resources'][v] = o_part + self.resources[v] = o_part - json.dump(o, f) + self.save() + + + def save(self): + f = open(self.network_path, 'w') + json.dump({ + 'resources': self.resources, + }, f, sort_keys=True, indent="\t") f.close() @@ -60,11 +68,23 @@ class Network(Data): return v + def resource_set(self, resource_key, content_key, reference, key_account=None): + self.resources[resource_key]['contents'][content_key]['reference'] = reference + self.resources[resource_key]['contents'][content_key]['key_account'] = key_account + + def chain_spec(self, k): v = self.resource(k) return ChainSpec.from_dict(v['chain_spec']) + def set(self, resource_key, chain_spec): + chain_spec_dict = chain_spec.asdict() + for k in chain_spec_dict.keys(): + logg.debug('resources {}'.format(self.resources)) + self.resources[resource_key]['chain_spec'][k] = chain_spec_dict[k] + + def __str__(self): s = '' for k in self.resources.keys(): diff --git a/cic/proof.py b/cic/proof.py index 53fde91..fc0ab82 100644 --- a/cic/proof.py +++ b/cic/proof.py @@ -53,7 +53,7 @@ class Proof(Data): f.close() f = open(self.proof_path, 'w') - json.dump(o, f) + json.dump(o, f, sort_keys=True, indent="\t") f.close() diff --git a/cic/runnable/cic_cmd.py b/cic/runnable/cic_cmd.py index c16b645..48cf2a3 100644 --- a/cic/runnable/cic_cmd.py +++ b/cic/runnable/cic_cmd.py @@ -9,6 +9,7 @@ import importlib import chainlib.cli import cic.cmd.init as cmd_init import cic.cmd.show as cmd_show +import cic.cmd.ext as cmd_ext import cic.cmd.export as cmd_export logging.basicConfig(level=logging.WARNING) @@ -30,6 +31,9 @@ sub_show = sub.add_parser('show', help='display summary of current state of cic cmd_show.process_args(sub_show) sub_export = sub.add_parser('export', help='export cic data directory state to a specified target') cmd_export.process_args(sub_export) +sub_ext = sub.add_parser('ext', help='extension helpers') +cmd_ext.process_args(sub_ext) + args = argparser.parse_args(sys.argv[1:]) diff --git a/cic/token.py b/cic/token.py index d10965b..d54ffeb 100644 --- a/cic/token.py +++ b/cic/token.py @@ -56,7 +56,7 @@ class Token(Data): o['supply'] = self.supply f = open(self.token_path, 'w') - json.dump(o, f) + json.dump(o, f, sort_keys=True, indent="\t") f.close() diff --git a/eth_requirements.txt b/eth_requirements.txt new file mode 100644 index 0000000..2b836b5 --- /dev/null +++ b/eth_requirements.txt @@ -0,0 +1,6 @@ +chainlib-eth>=0.0.10a1,<0.1.0 +funga-eth>=0.5.1a1,<0.6.0 +eth-token-index>=0.2.4a1,<0.3.0 +eth-address-index>=0.2.4a1,<0.3.0 +okota>=0.2.4a6,<0.3.0 +cic_eth_registry>=0.6.1a1,<0.7.0 diff --git a/setup.cfg b/setup.cfg index cdb72d7..e72f239 100644 --- a/setup.cfg +++ b/setup.cfg @@ -29,6 +29,3 @@ packages = cic cic.runnable cic.ext.eth - -[options.extras_require] -eth = chainlib-eth>=0.0.10a1,<0.1.0; funga-eth>=0.5.1a1,<0.6.0; eth-token-index>=0.2.4a1,<0.3.0; eth-address-index>=0.2.4a1,<0.3.0, okota>=0.2.4a6,<0.3.0 diff --git a/setup.py b/setup.py index 7f9956f..d768fe5 100644 --- a/setup.py +++ b/setup.py @@ -12,6 +12,20 @@ while True: requirements.append(l.rstrip()) f.close() +eth_requirements = [] +f = open('eth_requirements.txt', 'r') +while True: + l = f.readline() + if l == '': + break + eth_requirements.append(l.rstrip()) +f.close() + + setup( install_requires=requirements, + extras_require={ + 'eth': eth_requirements, + }, + )