cic-cli/cic/contract/network.py

144 lines
4.7 KiB
Python
Raw Normal View History

2021-10-09 20:37:54 +02:00
# standard imports
import json
2021-10-18 10:47:48 +02:00
import logging
import os
2021-10-09 20:37:54 +02:00
# external imports
from chainlib.chain import ChainSpec
2021-10-09 20:37:54 +02:00
# local imports
2022-03-01 09:17:17 +01:00
from cic.contract.base import Data, data_dir
2021-10-09 20:37:54 +02:00
2021-10-18 10:47:48 +02:00
logg = logging.getLogger(__name__)
2021-10-09 20:37:54 +02:00
class Network(Data):
"""Contains network settings for token deployments across extensions.
2021-10-09 20:37:54 +02:00
Extension targets are defined by the keys immediately following the "resources" key in the network settings file.
:param path: Path to settings directory
:type path: str
:param targets: Extension targets to execute
:type targets: list of str
"""
def __init__(self, path='.', targets=[]):
2021-10-09 20:37:54 +02:00
super(Network, self).__init__()
2021-10-18 10:47:48 +02:00
self.resources = None
2021-10-09 20:37:54 +02:00
self.path = path
self.targets = targets
2021-10-09 20:37:54 +02:00
self.network_path = os.path.join(self.path, 'network.json')
def load(self):
"""Load network settings from file.
"""
2021-10-09 20:37:54 +02:00
super(Network, self).load()
2022-03-01 09:17:17 +01:00
with open(self.network_path, 'r', encoding='utf-8') as f:
o = json.load(f)
2021-10-09 20:37:54 +02:00
2021-10-10 14:49:22 +02:00
self.resources = o['resources']
2021-10-09 20:37:54 +02:00
self.inited = True
def start(self):
"""Initialize network settings with targets chosen at object instantiation.
Will save to network settings file.
"""
2021-10-09 20:37:54 +02:00
super(Network, self).load()
network_template_file_path = os.path.join(data_dir, f'network_template_v{self.version()}.json')
2022-03-01 09:17:17 +01:00
with open(network_template_file_path, encoding='utf-8') as f:
o_part = json.load(f)
2021-10-18 10:47:48 +02:00
self.resources = {}
for v in self.targets:
2021-10-18 10:47:48 +02:00
self.resources[v] = o_part
self.save()
2021-10-18 10:47:48 +02:00
def save(self):
"""Save network settings to file.
"""
2022-03-01 09:17:17 +01:00
with open(self.network_path, 'w', encoding='utf-8') as f:
json.dump({
'resources': self.resources,
}, f, sort_keys=True, indent="\t")
2021-10-09 20:37:54 +02:00
def resource(self, k):
"""Get settings definitions for a given extension.
:param k: Extension key
:type k: str
:rtype: dict
:return: Extension settings
"""
v = self.resources.get(k)
2022-03-01 09:17:17 +01:00
if v is None:
raise AttributeError(f'No defined reference for {k}')
return v
2021-10-18 10:47:48 +02:00
def resource_set(self, resource_key, content_key, reference, key_account=None):
"""Set the values a content part of an extension setting.
The content parts define network application resources. Each entry is keyed by the name of the application. Each value consists of a key_account used to write/deploy to the contract, and the reference (address) of the application resource. If no application resource yet exists on the network for the part, the reference value will be None.
:param resource_key: Extension key
:type resource_key: str
:param content_key: Resource name (e.g. smart contract name)
:type content_key: str
:param reference: Reference to resource on network (e.g. smart contract address)
:type reference: str
:param key_account: Address of account to sign transaction for the resource with
:type key_account: str
"""
2021-10-18 10:47:48 +02:00
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):
"""Retrieve chain spec for the given extension
:param k: Extension key
:type k: str
:rtype: chainlib.chain.ChainSpec
:return: Chain spec object
"""
v = self.resource(k)
return ChainSpec.from_dict(v['chain_spec'])
2021-10-18 10:47:48 +02:00
def set(self, resource_key, chain_spec):
"""Set chain spec for resource.
:param resource_key: Extension key
:type resource_key: str
:param chain_spec: Chain spec to set
:type chain_spec: chainlib.chain.ChainSpec
"""
2021-10-18 10:47:48 +02:00
chain_spec_dict = chain_spec.asdict()
for k in chain_spec_dict.keys():
2022-03-01 09:17:17 +01:00
logg.debug(f'resources: {self.resources}')
2021-10-18 10:47:48 +02:00
self.resources[resource_key]['chain_spec'][k] = chain_spec_dict[k]
2021-10-09 20:37:54 +02:00
def __str__(self):
s = ''
for resource in self.resources.keys():
2022-03-07 10:25:45 +01:00
chainspec = ChainSpec.from_dict(self.resources[resource]['chain_spec'])
s += f'{resource}.chain_spec: {str(chainspec)}\n'
for content_key in self.resources[resource]['contents'].keys():
content_value = self.resources[resource]['contents'][content_key]
2022-03-01 10:30:19 +01:00
if content_value is None:
content_value = ''
2022-03-07 10:25:45 +01:00
s += f'{resource}.contents.{content_key} = {json.dumps(content_value, indent=4, sort_keys=True)}\n'
2021-10-09 20:37:54 +02:00
return s