Zecale - Reconciling Privacy and Scalability on Smart-Contract Chains  0.5
Reference implementation of the Zecale protocol by Clearmatics
command_context.py
Go to the documentation of this file.
1 # Copyright (c) 2015-2022 Clearmatics Technologies Ltd
2 #
3 # SPDX-License-Identifier: LGPL-3.0+
4 
5 from zecale.cli.defaults import AGGREGATOR_CONFIG_FILE_DEFAULT, \
6  AGGREGATOR_SERVER_ENDPOINT_DEFAULT, INSTANCE_FILE_DEFAULT
7 from zecale.core.aggregator_client import AggregatorClient
8 from zecale.core.aggregator_config import AggregatorConfiguration
9 from zecale.core.dispatcher_contract import DispatcherContract
10 from zeth.core.zksnark import IZKSnarkProvider
11 from zeth.core.contracts import InstanceDescription
12 from zeth.cli.utils import get_eth_network, open_web3_from_network, \
13  load_eth_address, load_eth_private_key
14 import json
15 from os.path import exists
16 from os import unlink
17 from typing import Tuple, Optional, Any
18 
19 # pylint: disable=too-many-instance-attributes
20 
21 
23  """
24  Carries command-independent parameters from top-level command to
25  sub-commands. Performs some basic operations common to commands, based on
26  the current context and configuration.
27  """
28 
29  def __init__(
30  self,
31  aggregator_server: str = AGGREGATOR_SERVER_ENDPOINT_DEFAULT,
32  aggregator_config_file: str = AGGREGATOR_CONFIG_FILE_DEFAULT,
33  instance_file: str = INSTANCE_FILE_DEFAULT,
34  eth_network: Optional[str] = None,
35  eth_addr: Optional[str] = None,
36  eth_private_key: Optional[str] = None):
37  # TODO: Separate nested and wrapper snarks
38  self.aggregator_server = aggregator_server
39  self.aggregator_config_file = aggregator_config_file
40  self.instance_file = instance_file
41  self.eth_network = eth_network
42  self.eth_addr = eth_addr
43  self.eth_private_key = eth_private_key
44  self._web3: Optional[Any] = None
45  self._aggregator_client: Optional[AggregatorClient] = None
46  self._aggregator_config: Optional[AggregatorConfiguration] = None
47  self._dispatcher_contract: Optional[DispatcherContract] = None
48 
49  def get_eth_key_and_address(self) -> Tuple[str, Optional[bytes]]:
50  return (
51  load_eth_address(self.eth_addr),
52  load_eth_private_key(self.eth_private_key))
53 
54  def get_web3(self) -> Any:
55  """
56  Create and cache web3 connection.
57  """
58  if not self._web3:
59  self._web3 = open_web3_from_network(get_eth_network(self.eth_network))
60  return self._web3
61 
62  def get_aggregator_client(self) -> AggregatorClient:
63  """
64  Return an aggregator client for the appropriate endpoint. Created and
65  cached when this function is first called.
66  """
67  if not self._aggregator_client:
69  return self._aggregator_client
70 
71  def get_aggregator_configuration(self) -> AggregatorConfiguration:
72  """
73  Load the AggregatorConfiguration from a file, or request it from the
74  aggregator server.
75  """
76  if self._aggregator_config is not None:
77  return self._aggregator_config
78 
79  if exists(self.aggregator_config_file):
80  with open(self.aggregator_config_file, "r") as aggregator_config_f:
81  try:
82  self._aggregator_config = \
83  AggregatorConfiguration.from_json_dict(
84  json.load(aggregator_config_f))
85  return self._aggregator_config
86  except Exception as ex:
87  print(f"removing `{self.aggregator_config_file}`: {ex}.")
88  unlink(self.aggregator_config_file)
89 
90  aggregator_client = self.get_aggregator_client()
91  self._aggregator_config = aggregator_client.get_configuration()
92 
93  with open(self.aggregator_config_file, "w") as aggregator_config_f:
94  json.dump(self._aggregator_config.to_json_dict(), aggregator_config_f)
95 
96  return self._aggregator_config
97 
98  def get_nested_snark(self) -> IZKSnarkProvider:
99  return self.get_aggregator_configuration().nested_snark
100 
101  def get_wrapper_snark(self) -> IZKSnarkProvider:
102  return self.get_aggregator_configuration().wrapper_snark
103 
104  def get_dispatcher_contract(self) -> Any:
105  """
106  Load (and cache) the dispatcher contract instance.
107  """
108  if not self._dispatcher_contract:
109  with open(self.instance_file, "r") as instance_f:
110  instance_dict = json.load(instance_f)
111  instance = InstanceDescription.from_json_dict(instance_dict)
113  self.get_web3(), instance, self.get_wrapper_snark())
114  return self._dispatcher_contract
zecale.cli.command_context.CommandContext.eth_private_key
eth_private_key
Definition: command_context.py:36
zecale.cli.command_context.CommandContext.eth_network
eth_network
Definition: command_context.py:34
zecale.cli.defaults
Definition: defaults.py:1
zecale.cli.command_context.CommandContext
Definition: command_context.py:22
zecale.core.dispatcher_contract.DispatcherContract
Definition: dispatcher_contract.py:23
zecale.cli.command_context.CommandContext._aggregator_client
_aggregator_client
Definition: command_context.py:68
zecale.cli.command_context.CommandContext.get_web3
Any get_web3(self)
Definition: command_context.py:54
zecale.cli.command_context.CommandContext.eth_addr
eth_addr
Definition: command_context.py:35
zecale.cli.command_context.CommandContext.get_nested_snark
IZKSnarkProvider get_nested_snark(self)
Definition: command_context.py:98
zecale.cli.command_context.CommandContext.get_aggregator_configuration
AggregatorConfiguration get_aggregator_configuration(self)
Definition: command_context.py:71
zecale.cli.command_context.CommandContext.instance_file
instance_file
Definition: command_context.py:33
zecale.core.aggregator_config
Definition: aggregator_config.py:1
zecale.cli.command_context.CommandContext.get_dispatcher_contract
Any get_dispatcher_contract(self)
Definition: command_context.py:104
zecale.cli.command_context.CommandContext.aggregator_config_file
aggregator_config_file
Definition: command_context.py:32
zecale.core.aggregator_client.AggregatorClient
Definition: aggregator_client.py:18
zecale.cli.command_context.CommandContext._dispatcher_contract
_dispatcher_contract
Definition: command_context.py:112
zecale.cli.command_context.CommandContext.__init__
def __init__(self, str aggregator_server=AGGREGATOR_SERVER_ENDPOINT_DEFAULT, str aggregator_config_file=AGGREGATOR_CONFIG_FILE_DEFAULT, str instance_file=INSTANCE_FILE_DEFAULT, Optional[str] eth_network=None, Optional[str] eth_addr=None, Optional[str] eth_private_key=None)
Definition: command_context.py:29
zecale.cli.command_context.CommandContext.get_wrapper_snark
IZKSnarkProvider get_wrapper_snark(self)
Definition: command_context.py:101
zecale.core.aggregator_client
Definition: aggregator_client.py:1
zecale.cli.command_context.CommandContext.get_eth_key_and_address
Tuple[str, Optional[bytes]] get_eth_key_and_address(self)
Definition: command_context.py:49
zecale.cli.command_context.CommandContext._web3
_web3
Definition: command_context.py:59
zecale.cli.command_context.CommandContext.aggregator_server
aggregator_server
Definition: command_context.py:31
zecale.cli.command_context.CommandContext._aggregator_config
_aggregator_config
Definition: command_context.py:82
zecale.cli.command_context.CommandContext.get_aggregator_client
AggregatorClient get_aggregator_client(self)
Definition: command_context.py:62
zecale.core.dispatcher_contract
Definition: dispatcher_contract.py:1