Skip to content

Commit

Permalink
add code_gen test for python template
Browse files Browse the repository at this point in the history
  • Loading branch information
yanyanho committed Jul 27, 2020
1 parent 224c50d commit 5cceea7
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 11 deletions.
2 changes: 1 addition & 1 deletion contract/mixer/abi/Groth16Mixer.abi
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{'inputs': [{'internalType': 'uint256', 'name': 'mk_depth', 'type': 'uint256'}, {'internalType': 'address', 'name': 'token', 'type': 'address'}, {'internalType': 'uint256[2]', 'name': 'Alpha', 'type': 'uint256[2]'}, {'internalType': 'uint256[2]', 'name': 'Beta1', 'type': 'uint256[2]'}, {'internalType': 'uint256[2]', 'name': 'Beta2', 'type': 'uint256[2]'}, {'internalType': 'uint256[2]', 'name': 'Delta1', 'type': 'uint256[2]'}, {'internalType': 'uint256[2]', 'name': 'Delta2', 'type': 'uint256[2]'}, {'internalType': 'uint256[]', 'name': 'ABC_coords', 'type': 'uint256[]'}], 'payable': False, 'stateMutability': 'nonpayable', 'type': 'constructor'}, {'anonymous': False, 'inputs': [{'indexed': False, 'internalType': 'string', 'name': 'message', 'type': 'string'}], 'name': 'LogDebug', 'type': 'event'}, {'anonymous': False, 'inputs': [{'indexed': False, 'internalType': 'bytes32', 'name': 'message', 'type': 'bytes32'}], 'name': 'LogDebug', 'type': 'event'}, {'anonymous': False, 'inputs': [{'indexed': False, 'internalType': 'bytes32', 'name': 'root', 'type': 'bytes32'}, {'indexed': False, 'internalType': 'bytes32[2]', 'name': 'nullifiers', 'type': 'bytes32[2]'}, {'indexed': False, 'internalType': 'bytes32[2]', 'name': 'commitments', 'type': 'bytes32[2]'}, {'indexed': False, 'internalType': 'bytes[2]', 'name': 'ciphertexts', 'type': 'bytes[2]'}], 'name': 'LogMix', 'type': 'event'}, {'constant': True, 'inputs': [{'internalType': 'uint256[9]', 'name': 'primary_inputs', 'type': 'uint256[9]'}], 'name': 'assemble_hsig', 'outputs': [{'internalType': 'bytes32', 'name': 'hsig', 'type': 'bytes32'}], 'payable': False, 'stateMutability': 'pure', 'type': 'function'}, {'constant': True, 'inputs': [{'internalType': 'uint256', 'name': 'index', 'type': 'uint256'}, {'internalType': 'uint256[9]', 'name': 'primary_inputs', 'type': 'uint256[9]'}], 'name': 'assemble_nullifier', 'outputs': [{'internalType': 'bytes32', 'name': 'nf', 'type': 'bytes32'}], 'payable': False, 'stateMutability': 'pure', 'type': 'function'}, {'constant': True, 'inputs': [{'internalType': 'uint256[9]', 'name': 'primary_inputs', 'type': 'uint256[9]'}], 'name': 'assemble_public_values', 'outputs': [{'internalType': 'uint256', 'name': 'vpub_in', 'type': 'uint256'}, {'internalType': 'uint256', 'name': 'vpub_out', 'type': 'uint256'}], 'payable': False, 'stateMutability': 'pure', 'type': 'function'}, {'constant': True, 'inputs': [], 'name': 'get_constants', 'outputs': [{'internalType': 'uint256', 'name': 'js_in', 'type': 'uint256'}, {'internalType': 'uint256', 'name': 'js_out', 'type': 'uint256'}, {'internalType': 'uint256', 'name': 'num_inputs', 'type': 'uint256'}], 'payable': False, 'stateMutability': 'pure', 'type': 'function'}, {'constant': False, 'inputs': [{'internalType': 'bytes32', 'name': 'commitment', 'type': 'bytes32'}], 'name': 'insert', 'outputs': [], 'payable': False, 'stateMutability': 'nonpayable', 'type': 'function'}, {'constant': False, 'inputs': [{'internalType': 'uint256[2]', 'name': 'a', 'type': 'uint256[2]'}, {'internalType': 'uint256[4]', 'name': 'b', 'type': 'uint256[4]'}, {'internalType': 'uint256[2]', 'name': 'c', 'type': 'uint256[2]'}, {'internalType': 'uint256[4]', 'name': 'vk', 'type': 'uint256[4]'}, {'internalType': 'uint256', 'name': 'sigma', 'type': 'uint256'}, {'internalType': 'uint256[9]', 'name': 'input', 'type': 'uint256[9]'}, {'internalType': 'bytes[2]', 'name': 'ciphertexts', 'type': 'bytes[2]'}], 'name': 'mix', 'outputs': [], 'payable': True, 'stateMutability': 'payable', 'type': 'function'}, {'constant': True, 'inputs': [], 'name': 'token', 'outputs': [{'internalType': 'address', 'name': '', 'type': 'address'}], 'payable': False, 'stateMutability': 'view', 'type': 'function'}, {'constant': True, 'inputs': [{'internalType': 'address', 'name': 'from', 'type': 'address'}, {'internalType': 'uint256', 'name': 'value', 'type': 'uint256'}, {'internalType': 'bytes', 'name': 'data', 'type': 'bytes'}], 'name': 'tokenFallback', 'outputs': [], 'payable': False, 'stateMutability': 'pure', 'type': 'function'}]
[{"inputs": [{"internalType": "uint256", "name": "mk_depth", "type": "uint256"}, {"internalType": "address", "name": "token", "type": "address"}, {"internalType": "uint256[2]", "name": "Alpha", "type": "uint256[2]"}, {"internalType": "uint256[2]", "name": "Beta1", "type": "uint256[2]"}, {"internalType": "uint256[2]", "name": "Beta2", "type": "uint256[2]"}, {"internalType": "uint256[2]", "name": "Delta1", "type": "uint256[2]"}, {"internalType": "uint256[2]", "name": "Delta2", "type": "uint256[2]"}, {"internalType": "uint256[]", "name": "ABC_coords", "type": "uint256[]"}], "payable": false, "stateMutability": "nonpayable", "type": "constructor"}, {"anonymous": false, "inputs": [{"indexed": false, "internalType": "string", "name": "message", "type": "string"}], "name": "LogDebug", "type": "event"}, {"anonymous": false, "inputs": [{"indexed": false, "internalType": "bytes32", "name": "message", "type": "bytes32"}], "name": "LogDebug", "type": "event"}, {"anonymous": false, "inputs": [{"indexed": false, "internalType": "bytes32", "name": "root", "type": "bytes32"}, {"indexed": false, "internalType": "bytes32[2]", "name": "nullifiers", "type": "bytes32[2]"}, {"indexed": false, "internalType": "bytes32[2]", "name": "commitments", "type": "bytes32[2]"}, {"indexed": false, "internalType": "bytes[2]", "name": "ciphertexts", "type": "bytes[2]"}], "name": "LogMix", "type": "event"}, {"constant": true, "inputs": [{"internalType": "uint256[9]", "name": "primary_inputs", "type": "uint256[9]"}], "name": "assemble_hsig", "outputs": [{"internalType": "bytes32", "name": "hsig", "type": "bytes32"}], "payable": false, "stateMutability": "pure", "type": "function"}, {"constant": true, "inputs": [{"internalType": "uint256", "name": "index", "type": "uint256"}, {"internalType": "uint256[9]", "name": "primary_inputs", "type": "uint256[9]"}], "name": "assemble_nullifier", "outputs": [{"internalType": "bytes32", "name": "nf", "type": "bytes32"}], "payable": false, "stateMutability": "pure", "type": "function"}, {"constant": true, "inputs": [{"internalType": "uint256[9]", "name": "primary_inputs", "type": "uint256[9]"}], "name": "assemble_public_values", "outputs": [{"internalType": "uint256", "name": "vpub_in", "type": "uint256"}, {"internalType": "uint256", "name": "vpub_out", "type": "uint256"}], "payable": false, "stateMutability": "pure", "type": "function"}, {"constant": true, "inputs": [], "name": "get_constants", "outputs": [{"internalType": "uint256", "name": "js_in", "type": "uint256"}, {"internalType": "uint256", "name": "js_out", "type": "uint256"}, {"internalType": "uint256", "name": "num_inputs", "type": "uint256"}], "payable": false, "stateMutability": "pure", "type": "function"}, {"constant": false, "inputs": [{"internalType": "bytes32", "name": "commitment", "type": "bytes32"}], "name": "insert", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function"}, {"constant": false, "inputs": [{"internalType": "uint256[2]", "name": "a", "type": "uint256[2]"}, {"internalType": "uint256[4]", "name": "b", "type": "uint256[4]"}, {"internalType": "uint256[2]", "name": "c", "type": "uint256[2]"}, {"internalType": "uint256[4]", "name": "vk", "type": "uint256[4]"}, {"internalType": "uint256", "name": "sigma", "type": "uint256"}, {"internalType": "uint256[9]", "name": "input", "type": "uint256[9]"}, {"internalType": "bytes[2]", "name": "ciphertexts", "type": "bytes[2]"}], "name": "mix", "outputs": [], "payable": true, "stateMutability": "payable", "type": "function"}, {"constant": true, "inputs": [], "name": "token", "outputs": [{"internalType": "address", "name": "", "type": "address"}], "payable": false, "stateMutability": "view", "type": "function"}, {"constant": true, "inputs": [{"internalType": "address", "name": "from", "type": "address"}, {"internalType": "uint256", "name": "value", "type": "uint256"}, {"internalType": "bytes", "name": "data", "type": "bytes"}], "name": "tokenFallback", "outputs": [], "payable": false, "stateMutability": "pure", "type": "function"}]
83 changes: 83 additions & 0 deletions contract/mixer/abi/Groth16Mixer.python
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# template for codegen
from client.bcosclient import (
BcosClient
)
from client.datatype_parser import DatatypeParser
import json


class Groth16Mixer: # name of abi
address = None
contract_abi_string = '''[{"inputs": [{"internalType": "uint256", "name": "mk_depth", "type": "uint256"}, {"internalType": "address", "name": "token", "type": "address"}, {"internalType": "uint256[2]", "name": "Alpha", "type": "uint256[2]"}, {"internalType": "uint256[2]", "name": "Beta1", "type": "uint256[2]"}, {"internalType": "uint256[2]", "name": "Beta2", "type": "uint256[2]"}, {"internalType": "uint256[2]", "name": "Delta1", "type": "uint256[2]"}, {"internalType": "uint256[2]", "name": "Delta2", "type": "uint256[2]"}, {"internalType": "uint256[]", "name": "ABC_coords", "type": "uint256[]"}], "payable": false, "stateMutability": "nonpayable", "type": "constructor"}, {"anonymous": false, "inputs": [{"indexed": false, "internalType": "string", "name": "message", "type": "string"}], "name": "LogDebug", "type": "event", "topic": "0xd44da6836c8376d1693e8b9cacf1c39b9bed3599164ad6d8e60902515f83938e"}, {"anonymous": false, "inputs": [{"indexed": false, "internalType": "bytes32", "name": "message", "type": "bytes32"}], "name": "LogDebug", "type": "event", "topic": "0x05e46912c9be87d8a6830598db8544b61884d9d22f3921597a9a6e8a340914b3"}, {"anonymous": false, "inputs": [{"indexed": false, "internalType": "bytes32", "name": "root", "type": "bytes32"}, {"indexed": false, "internalType": "bytes32[2]", "name": "nullifiers", "type": "bytes32[2]"}, {"indexed": false, "internalType": "bytes32[2]", "name": "commitments", "type": "bytes32[2]"}, {"indexed": false, "internalType": "bytes[2]", "name": "ciphertexts", "type": "bytes[2]"}], "name": "LogMix", "type": "event", "topic": "0x36ed7c3f2ecfb5a5226c478b034d33144c060afe361be291e948f861dcddc618"}, {"constant": true, "inputs": [{"internalType": "uint256[9]", "name": "primary_inputs", "type": "uint256[9]"}], "name": "assemble_hsig", "outputs": [{"internalType": "bytes32", "name": "hsig", "type": "bytes32"}], "payable": false, "stateMutability": "pure", "type": "function"}, {"constant": true, "inputs": [{"internalType": "uint256", "name": "index", "type": "uint256"}, {"internalType": "uint256[9]", "name": "primary_inputs", "type": "uint256[9]"}], "name": "assemble_nullifier", "outputs": [{"internalType": "bytes32", "name": "nf", "type": "bytes32"}], "payable": false, "stateMutability": "pure", "type": "function"}, {"constant": true, "inputs": [{"internalType": "uint256[9]", "name": "primary_inputs", "type": "uint256[9]"}], "name": "assemble_public_values", "outputs": [{"internalType": "uint256", "name": "vpub_in", "type": "uint256"}, {"internalType": "uint256", "name": "vpub_out", "type": "uint256"}], "payable": false, "stateMutability": "pure", "type": "function"}, {"constant": true, "inputs": [], "name": "get_constants", "outputs": [{"internalType": "uint256", "name": "js_in", "type": "uint256"}, {"internalType": "uint256", "name": "js_out", "type": "uint256"}, {"internalType": "uint256", "name": "num_inputs", "type": "uint256"}], "payable": false, "stateMutability": "pure", "type": "function"}, {"constant": false, "inputs": [{"internalType": "bytes32", "name": "commitment", "type": "bytes32"}], "name": "insert", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function"}, {"constant": false, "inputs": [{"internalType": "uint256[2]", "name": "a", "type": "uint256[2]"}, {"internalType": "uint256[4]", "name": "b", "type": "uint256[4]"}, {"internalType": "uint256[2]", "name": "c", "type": "uint256[2]"}, {"internalType": "uint256[4]", "name": "vk", "type": "uint256[4]"}, {"internalType": "uint256", "name": "sigma", "type": "uint256"}, {"internalType": "uint256[9]", "name": "input", "type": "uint256[9]"}, {"internalType": "bytes[2]", "name": "ciphertexts", "type": "bytes[2]"}], "name": "mix", "outputs": [], "payable": true, "stateMutability": "payable", "type": "function"}, {"constant": true, "inputs": [], "name": "token", "outputs": [{"internalType": "address", "name": "", "type": "address"}], "payable": false, "stateMutability": "view", "type": "function"}, {"constant": true, "inputs": [{"internalType": "address", "name": "from", "type": "address"}, {"internalType": "uint256", "name": "value", "type": "uint256"}, {"internalType": "bytes", "name": "data", "type": "bytes"}], "name": "tokenFallback", "outputs": [], "payable": false, "stateMutability": "pure", "type": "function"}]'''
contract_abi = None
data_parser = DatatypeParser()
client = None

def __init__(self, address):
self.client = BcosClient()
self.address = address
self.contract_abi = json.loads(self.contract_abi_string)
self.data_parser.set_abi(self.contract_abi)

def deploy(self, contract_bin_file):
result = self.client.deployFromFile(contract_bin_file)
self.address = result["contractAddress"]
return result

# ------------------------------------------
def assemble_hsig(self, primary_inputs):
func_name = 'assemble_hsig'
args = [primary_inputs]
result = self.client.call(self.address, self.contract_abi, func_name, args)
return result

# ------------------------------------------
def assemble_nullifier(self, index, primary_inputs):
func_name = 'assemble_nullifier'
args = [index, primary_inputs]
result = self.client.call(self.address, self.contract_abi, func_name, args)
return result

# ------------------------------------------
def assemble_public_values(self, primary_inputs):
func_name = 'assemble_public_values'
args = [primary_inputs]
result = self.client.call(self.address, self.contract_abi, func_name, args)
return result

# ------------------------------------------
def get_constants(self):
func_name = 'get_constants'
args = []
result = self.client.call(self.address, self.contract_abi, func_name, args)
return result

# ------------------------------------------
def insert(self, commitment):
func_name = 'insert'
args = [commitment]
receipt = self.client.sendRawTransactionGetReceipt(self.address, self.contract_abi, func_name, args)
outputresult = self.data_parser.parse_receipt_output(func_name, receipt['output'])
return outputresult, receipt

# ------------------------------------------
def mix(self, a, b, c, vk, sigma, input, ciphertexts):
func_name = 'mix'
args = [a, b, c, vk, sigma, input, ciphertexts]
receipt = self.client.sendRawTransactionGetReceipt(self.address, self.contract_abi, func_name, args)
outputresult = self.data_parser.parse_receipt_output(func_name, receipt['output'])
return outputresult, receipt

# ------------------------------------------
def token(self):
func_name = 'token'
args = []
result = self.client.call(self.address, self.contract_abi, func_name, args)
return result

# ------------------------------------------
def tokenFallback(self, from, value, data):
func_name = 'tokenFallback'
args = [to_checksum_address(from), value, data]
result = self.client.call(self.address, self.contract_abi, func_name, args)
return result
6 changes: 3 additions & 3 deletions python_web3/codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
'''

import sys
from client.datatype_parser import DatatypeParser
from utils.abi import (
from python_web3.client.datatype_parser import DatatypeParser
from python_web3.utils.abi import (
filter_by_type
)
import os
Expand All @@ -30,7 +30,7 @@ class ABICodegen:
# four spaces for indent
indent = " "
template = ""
template_file = "client/codegen_template.py"
template_file = "./python_web3/client/codegen_template.py"

def __init__(self, abi_file):
if len(abi_file) > 0:
Expand Down
25 changes: 18 additions & 7 deletions test_commands/scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
# Copyright (c) 2015-2020 Clearmatics Technologies Ltd
#
# SPDX-License-Identifier: LGPL-3.0+
import os

from python_web3.codegen import ABICodegen
from zeth.mixer_client import MixerClient, OwnershipKeyPair, joinsplit_sign, \
encrypt_notes, get_dummy_input_and_address, compute_h_sig, \
JoinsplitSigVerificationKey
Expand Down Expand Up @@ -502,10 +504,19 @@ def compile_mixer() -> Interface:
set_solc_version(SOL_COMPILER_VERSION)
compiled_sol = compile_files([path_to_token], allow_paths=allowed_path)
mixer_interface = compiled_sol[path_to_token + ":Groth16Mixer"]
fo = open("./contract/mixer/abi/Groth16Mixer.abi", "w")
fo1 = open("./contract/mixer/abi/Groth16Mixer.bin", "w")
fo.write(str(mixer_interface["abi"]))
fo.close()
fo1.write(str(mixer_interface["bin"]))
fo1.close()
return mixer_interface
# fo = open("./contract/mixer/abi/Groth16Mixer.abi", "w")
# fo1 = open("./contract/mixer/abi/Groth16Mixer.bin", "w")
# fo.write(str(mixer_interface["abi"]))
# fo.close()
# fo1.write(str(mixer_interface["bin"]))
# fo1.close()
return mixer_interface

def code_gen(abi_file):
codegen = ABICodegen("./contract/mixer/abi/"+abi_file)
template = codegen.gen_all()
name = codegen.name + '.py'
# outputfile = os.path.join("./contract/mixer/abi/", )
fo = open("./contract/mixer/abi/Groth16Mixer.python", "w")
fo.write(template)
fo.close()
1 change: 1 addition & 0 deletions test_commands/test_erc_token_transfer_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ def main() -> None:
client.ecdsa_account = deployer_ac
client.keypair = deployer_keypair
mixer_interface = scenario.compile_mixer()
# scenario.code_gen("Groth16Mixer.abi")
mixerTransactionRecipient = client.sendRawTransactionGetReceipt("", mixer_interface['abi'], None, constructArgs, mixer_interface['bin'], 30000000, 15)
print("mixerTransactionRecipient: ", mixerTransactionRecipient)
mixer_address = mixerTransactionRecipient['contractAddress']
Expand Down

0 comments on commit 5cceea7

Please sign in to comment.