Zecale - Reconciling Privacy and Scalability on Smart-Contract Chains  0.5
Reference implementation of the Zecale protocol by Clearmatics
aggregator_client.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.api import aggregator_pb2_grpc
6 from zecale.api import aggregator_pb2
7 from zecale.core.aggregated_transaction import AggregatedTransaction
8 from zecale.core.aggregator_config import AggregatorConfiguration
9 from zecale.core.nested_transaction import NestedTransaction
10 from zecale.core.proto_utils import aggregator_configuration_from_proto, \
11  nested_transaction_to_proto, aggregated_transaction_from_proto
12 from zeth.core.zksnark import IZKSnarkProvider, IVerificationKey
13 import grpc
14 from google.protobuf import empty_pb2
15 import json
16 
17 
19  """
20  Interface to Aggregator RPC calls. Interface uses the in-memory version of
21  objects, internally converting to the protobuf versions.
22  """
23  def __init__(self, endpoint: str):
24  self.endpoint = endpoint
25 
26  def get_configuration(self) -> AggregatorConfiguration:
27  with grpc.insecure_channel(self.endpoint) as channel:
28  stub = aggregator_pb2_grpc.AggregatorStub(channel) # type: ignore
29  config_proto = stub.GetConfiguration(empty_pb2.Empty())
30  return aggregator_configuration_from_proto(config_proto)
31 
33  self, wrapper_zksnark: IZKSnarkProvider) -> IVerificationKey:
34  with grpc.insecure_channel(self.endpoint) as channel:
35  stub = aggregator_pb2_grpc.AggregatorStub(channel) # type: ignore
36  vk_proto = stub.GetVerificationKey(empty_pb2.Empty())
37  return wrapper_zksnark.verification_key_from_proto(vk_proto)
38 
40  self, nested_zksnark: IZKSnarkProvider, vk: IVerificationKey) -> str:
41  with grpc.insecure_channel(self.endpoint) as channel:
42  stub = aggregator_pb2_grpc.AggregatorStub(channel) # type: ignore
43  vk_proto = nested_zksnark.verification_key_to_proto(vk)
44  vk_hash_json = stub.GetNestedVerificationKeyHash(vk_proto).hash
45  return json.loads(vk_hash_json)
46 
48  self,
49  nested_zksnark: IZKSnarkProvider,
50  vk: IVerificationKey,
51  app_name: str) -> None:
52  """
53  Register an application. Throw an error with message if this fails for any
54  reason.
55  """
56  app_desc = aggregator_pb2.ApplicationDescription()
57  app_desc.application_name = app_name
58  app_desc.vk.CopyFrom(nested_zksnark.verification_key_to_proto(vk)) \
59  # pylint: disable=no-member
60  with grpc.insecure_channel(self.endpoint) as channel:
61  stub = aggregator_pb2_grpc.AggregatorStub(channel) # type: ignore
62  stub.RegisterApplication(app_desc)
63 
65  self,
66  nested_zksnark: IZKSnarkProvider,
67  nested_tx: NestedTransaction) -> None:
68  """
69  Submit a nested transaction to the aggregator.
70  """
71  assert isinstance(nested_zksnark, IZKSnarkProvider)
72  assert isinstance(nested_tx, NestedTransaction)
73  nested_tx_proto = nested_transaction_to_proto(nested_zksnark, nested_tx)
74  with grpc.insecure_channel(self.endpoint) as channel:
75  stub = aggregator_pb2_grpc.AggregatorStub(channel) # type: ignore
76  stub.SubmitNestedTransaction(nested_tx_proto)
77 
79  self,
80  wrapper_zksnark: IZKSnarkProvider,
81  name: str) -> AggregatedTransaction:
82  """
83  Request an aggregated transaction.
84  """
85  agg_tx_request = aggregator_pb2.AggregatedTransactionRequest()
86  agg_tx_request.application_name = name
87  with grpc.insecure_channel(self.endpoint) as channel:
88  stub = aggregator_pb2_grpc.AggregatorStub(channel) # type: ignore
89  agg_tx_proto = stub.GenerateAggregatedTransaction(agg_tx_request)
90  return aggregated_transaction_from_proto(wrapper_zksnark, agg_tx_proto)
zecale.api
Definition: __init__.py:1
zecale.core.nested_transaction
Definition: nested_transaction.py:1
zecale.core.aggregated_transaction
Definition: aggregated_transaction.py:1
zecale.core.aggregator_client.AggregatorClient.get_configuration
AggregatorConfiguration get_configuration(self)
Definition: aggregator_client.py:26
zecale.core.proto_utils.nested_transaction_to_proto
aggregator_pb2.NestedTransaction nested_transaction_to_proto(IZKSnarkProvider zksnark, NestedTransaction tx)
Definition: proto_utils.py:25
zecale.core.aggregator_client.AggregatorClient.endpoint
endpoint
Definition: aggregator_client.py:24
zecale.core.aggregator_client.AggregatorClient.get_verification_key
IVerificationKey get_verification_key(self, IZKSnarkProvider wrapper_zksnark)
Definition: aggregator_client.py:32
zecale.core.aggregator_config
Definition: aggregator_config.py:1
zecale.core.aggregator_client.AggregatorClient.get_nested_verification_key_hash
str get_nested_verification_key_hash(self, IZKSnarkProvider nested_zksnark, IVerificationKey vk)
Definition: aggregator_client.py:39
zecale.core.aggregator_client.AggregatorClient.get_aggregated_transaction
AggregatedTransaction get_aggregated_transaction(self, IZKSnarkProvider wrapper_zksnark, str name)
Definition: aggregator_client.py:78
zecale.core.proto_utils.aggregated_transaction_from_proto
AggregatedTransaction aggregated_transaction_from_proto(IZKSnarkProvider zksnark, aggregator_pb2.AggregatedTransaction aggregated_transaction_proto)
Definition: proto_utils.py:38
zecale.core.proto_utils.aggregator_configuration_from_proto
AggregatorConfiguration aggregator_configuration_from_proto(aggregator_pb2.AggregatorConfiguration aggregator_config_proto)
Definition: proto_utils.py:13
zecale.core.aggregator_client.AggregatorClient.__init__
def __init__(self, str endpoint)
Definition: aggregator_client.py:23
zecale.core.aggregator_client.AggregatorClient
Definition: aggregator_client.py:18
zecale.core.aggregator_client.AggregatorClient.register_application
None register_application(self, IZKSnarkProvider nested_zksnark, IVerificationKey vk, str app_name)
Definition: aggregator_client.py:47
zecale.core.proto_utils
Definition: proto_utils.py:1
zecale.core.aggregator_client.AggregatorClient.submit_nested_transaction
None submit_nested_transaction(self, IZKSnarkProvider nested_zksnark, NestedTransaction nested_tx)
Definition: aggregator_client.py:64