Add eth initializer

This commit is contained in:
nolash 2021-10-18 10:47:48 +02:00
parent 817fc40d84
commit 24e2e62924
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
11 changed files with 105 additions and 12 deletions

30
cic/cmd/ext.py Normal file
View File

@ -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'))

View File

@ -1,4 +1,5 @@
{ {
"registry": null,
"chain_spec": { "chain_spec": {
"arch": null, "arch": null,
"fork": null, "fork": null,

21
cic/ext/eth/start.py Normal file
View File

@ -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()

View File

@ -50,7 +50,7 @@ class Meta(Data):
f.close() f.close()
f = open(self.meta_path, 'w') f = open(self.meta_path, 'w')
json.dump(o, f) json.dump(o, f, sort_keys=True, indent="\t")
f.close() f.close()

View File

@ -1,6 +1,7 @@
# standard imports # standard imports
import os import os
import json import json
import logging
# external imports # external imports
from chainlib.chain import ChainSpec from chainlib.chain import ChainSpec
@ -11,12 +12,14 @@ from .base import (
data_dir, data_dir,
) )
logg = logging.getLogger(__name__)
class Network(Data): class Network(Data):
def __init__(self, path='.', targets=[]): def __init__(self, path='.', targets=[]):
super(Network, self).__init__() super(Network, self).__init__()
self.references = None self.resources = None
self.path = path self.path = path
self.targets = targets self.targets = targets
self.network_path = os.path.join(self.path, 'network.json') self.network_path = os.path.join(self.path, 'network.json')
@ -43,13 +46,18 @@ class Network(Data):
o_part = json.load(f) o_part = json.load(f)
f.close() f.close()
f = open(self.network_path, 'w') self.resources = {}
o = {'resources': {}}
for v in self.targets: 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() f.close()
@ -60,11 +68,23 @@ class Network(Data):
return v 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): def chain_spec(self, k):
v = self.resource(k) v = self.resource(k)
return ChainSpec.from_dict(v['chain_spec']) 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): def __str__(self):
s = '' s = ''
for k in self.resources.keys(): for k in self.resources.keys():

View File

@ -53,7 +53,7 @@ class Proof(Data):
f.close() f.close()
f = open(self.proof_path, 'w') f = open(self.proof_path, 'w')
json.dump(o, f) json.dump(o, f, sort_keys=True, indent="\t")
f.close() f.close()

View File

@ -9,6 +9,7 @@ import importlib
import chainlib.cli import chainlib.cli
import cic.cmd.init as cmd_init import cic.cmd.init as cmd_init
import cic.cmd.show as cmd_show import cic.cmd.show as cmd_show
import cic.cmd.ext as cmd_ext
import cic.cmd.export as cmd_export import cic.cmd.export as cmd_export
logging.basicConfig(level=logging.WARNING) 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) cmd_show.process_args(sub_show)
sub_export = sub.add_parser('export', help='export cic data directory state to a specified target') sub_export = sub.add_parser('export', help='export cic data directory state to a specified target')
cmd_export.process_args(sub_export) 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:]) args = argparser.parse_args(sys.argv[1:])

View File

@ -56,7 +56,7 @@ class Token(Data):
o['supply'] = self.supply o['supply'] = self.supply
f = open(self.token_path, 'w') f = open(self.token_path, 'w')
json.dump(o, f) json.dump(o, f, sort_keys=True, indent="\t")
f.close() f.close()

6
eth_requirements.txt Normal file
View File

@ -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

View File

@ -29,6 +29,3 @@ packages =
cic cic
cic.runnable cic.runnable
cic.ext.eth 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

View File

@ -12,6 +12,20 @@ while True:
requirements.append(l.rstrip()) requirements.append(l.rstrip())
f.close() 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( setup(
install_requires=requirements, install_requires=requirements,
extras_require={
'eth': eth_requirements,
},
) )