Zecale - Reconciling Privacy and Scalability on Smart-Contract Chains  0.5
Reference implementation of the Zecale protocol by Clearmatics
dispatcher_contract.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 __future__ import annotations
6 from zecale.core.utils import get_zecale_dir
7 from zecale.core.aggregated_transaction import AggregatedTransaction
8 from zeth.core.utils import hex_list_to_uint256_list
9 from zeth.core.pairing import PairingParameters
10 from zeth.core.contracts import InstanceDescription, send_contract_call, \
11  get_event_logs_from_tx_receipt
12 from zeth.core.zksnark import IZKSnarkProvider, IVerificationKey
13 from os.path import join
14 from typing import Tuple, Optional, Any
15 
16 
17 ZECALE_DIR = get_zecale_dir()
18 CONTRACTS_DIR = join(ZECALE_DIR, "contracts")
19 DISPATCHER_SOURCE_FILE = join(CONTRACTS_DIR, "ZecaleDispatcher.sol")
20 DISPATCHER_DEPLOY_GAS = 5000000
21 
22 
24  """
25  Wrapper around operations on the zecale dispatcher contract.
26  """
27 
28  def __init__(
29  self,
30  web3: Any,
31  instance_desc: InstanceDescription,
32  zksnark: IZKSnarkProvider):
33  self.web3 = web3
34  self.instance = instance_desc.instantiate(web3)
35  self.zksnark = zksnark
36 
37  @staticmethod
38  def deploy(
39  web3: Any,
40  zksnark: IZKSnarkProvider,
41  pp: PairingParameters,
42  vk: IVerificationKey,
43  eth_addr: str,
44  eth_private_key: Optional[bytes]
45  ) -> Tuple[DispatcherContract, InstanceDescription]:
46  """
47  Deploy the contract, returning an instance of this wrapper, and a
48  description (which can be saved to a file to later instantiate).
49  """
50  vk_evm = zksnark.verification_key_to_contract_parameters(vk, pp)
51  instance_desc = InstanceDescription.deploy(
52  web3,
53  DISPATCHER_SOURCE_FILE,
54  "ZecaleDispatcher",
55  eth_addr,
56  eth_private_key,
57  DISPATCHER_DEPLOY_GAS,
58  {"allow_paths": CONTRACTS_DIR},
59  [vk_evm])
60  return DispatcherContract(web3, instance_desc, zksnark), instance_desc
61 
63  self,
64  pp: PairingParameters,
65  batch: AggregatedTransaction,
66  application_contract_address: str,
67  eth_addr: str,
68  eth_private_key: Optional[bytes]) -> bytes:
69  """
70  Send a batch to the contracts process_batch entry point. Returns the
71  transaction ID.
72  """
73 
74  # Encode the parameters of the entry point and create a local call
75  # object. The proof and inputs are encoded into contract parameters,
76  # and the nested_parameters are passed as raw bytes arrays.
77  proof_evm = self.zksnark.proof_to_contract_parameters(
78  batch.ext_proof.proof, pp)
79  inputs_evm = hex_list_to_uint256_list(batch.ext_proof.inputs)
80 
81  contract_call = self.instance.functions.process_batch(
82  proof_evm,
83  inputs_evm,
84  batch.nested_parameters,
85  application_contract_address)
86 
87  # Broadcast the call
88  return send_contract_call(
89  self.web3,
90  contract_call,
91  eth_addr,
92  eth_private_key,
93  None, # TODO: value (fee?)
94  None)
95 
96  def dump_logs(self, tx_receipt: Any) -> None:
97  """
98  Print out debug log information from a dispatcher invocation
99  """
100  logs = get_event_logs_from_tx_receipt(self.instance, "log", tx_receipt)
101  for event_data in logs:
102  print(f"{event_data.args['a']}: {event_data.args['v']}")
zecale.core.dispatcher_contract.DispatcherContract.zksnark
zksnark
Definition: dispatcher_contract.py:31
zecale.core.dispatcher_contract.DispatcherContract.dump_logs
None dump_logs(self, Any tx_receipt)
Definition: dispatcher_contract.py:96
zecale.core.dispatcher_contract.DispatcherContract.__init__
def __init__(self, Any web3, InstanceDescription instance_desc, IZKSnarkProvider zksnark)
Definition: dispatcher_contract.py:28
zecale.core.utils.get_zecale_dir
str get_zecale_dir()
Definition: utils.py:9
zecale.core.dispatcher_contract.DispatcherContract
Definition: dispatcher_contract.py:23
zecale.core.dispatcher_contract.DispatcherContract.instance
instance
Definition: dispatcher_contract.py:30
zecale.core.aggregated_transaction
Definition: aggregated_transaction.py:1
zecale.core.utils
Definition: utils.py:1
zecale.core.dispatcher_contract.DispatcherContract.process_batch
bytes process_batch(self, PairingParameters pp, AggregatedTransaction batch, str application_contract_address, str eth_addr, Optional[bytes] eth_private_key)
Definition: dispatcher_contract.py:62
zecale.core.dispatcher_contract.DispatcherContract.deploy
Tuple[DispatcherContract, InstanceDescription] deploy(Any web3, IZKSnarkProvider zksnark, PairingParameters pp, IVerificationKey vk, str eth_addr, Optional[bytes] eth_private_key)
Definition: dispatcher_contract.py:38
zecale.core.dispatcher_contract.DispatcherContract.web3
web3
Definition: dispatcher_contract.py:29