16 from test_commands
import mock
17 from test_commands
import scenario
19 from os.path
import join, exists
22 from typing
import Dict, List, Any
32 print(f
" Alice : {token_instance.functions.balanceOf(alice).call()}")
33 print(f
" Bob : {token_instance.functions.balanceOf(bob).call()}")
34 print(f
" Charlie : {token_instance.functions.balanceOf(charlie).call()}")
35 print(f
" Mixer : {token_instance.functions.balanceOf(mixer).call()}")
42 token_amount: int) -> str:
43 return token_instance.functions.approve(
45 Web3.toWei(token_amount,
'ether')).transact({
'from': owner_address})
51 spender_address: str) -> str:
52 return token_instance.functions.allowance(owner_address, spender_address) \
60 web3, eth = mock.open_test_web3()
63 deployer_eth_address = eth.accounts[0]
64 bob_eth_address = eth.accounts[1]
65 alice_eth_address = eth.accounts[2]
66 charlie_eth_address = eth.accounts[3]
68 keystore = mock.init_test_keystore()
71 token_instance =
deploy_token(web3, deployer_eth_address,
None, 4000000)
74 prover_client =
ProverClient(mock.TEST_PROVER_SERVER_ENDPOINT)
75 prover_config = prover_client.get_configuration()
76 pp = prover_config.pairing_parameters
77 assert prover_client.get_configuration().zksnark_name == zksnark_name
80 tree_depth = constants.ZETH_MERKLE_TREE_DEPTH
81 zeth_client, _contract_desc = MixerClient.deploy(
86 token_instance.address,
90 tree_depth, tree_hash)
91 mixer_instance = zeth_client.mixer_instance
94 def _mk_wallet(name: str, sk: ZethAddressPriv) -> Wallet:
95 wallet_dir = join(mock.TEST_NOTE_DIR, name +
"-erc")
96 if exists(wallet_dir):
99 shutil.rmtree(wallet_dir)
100 return Wallet(mixer_instance, name, wallet_dir, sk, tree_hash)
101 sk_alice = keystore[
"Alice"].addr_sk
102 sk_bob = keystore[
"Bob"].addr_sk
103 sk_charlie = keystore[
"Charlie"].addr_sk
104 alice_wallet = _mk_wallet(
'alice', sk_alice)
105 bob_wallet = _mk_wallet(
'bob', sk_bob)
106 charlie_wallet = _mk_wallet(
'charlie', sk_charlie)
111 out_ev: List[MixOutputEvents]) \
112 -> Dict[str, List[ZethNoteDescription]]:
115 'alice': alice_wallet.receive_notes(out_ev, pp),
116 'bob': bob_wallet.receive_notes(out_ev, pp),
117 'charlie': charlie_wallet.receive_notes(out_ev, pp),
119 alice_wallet.update_and_save_state(block_num)
120 bob_wallet.update_and_save_state(block_num)
121 charlie_wallet.update_and_save_state(block_num)
122 block_num = block_num + 1
125 print(
"[INFO] 4. Running tests (asset mixed: ERC20 token)...")
131 deployer_eth_address,
133 EtherValue(2*scenario.BOB_DEPOSIT_ETH,
'ether'))
134 print(
"- Initial balances: ")
140 zeth_client.mixer_instance.address)
145 result_deposit_bob_to_bob = scenario.bob_deposit(
152 except Exception
as e:
156 zeth_client.mixer_instance.address)
157 print(f
"[ERROR] Bob deposit failed! (msg: {e})")
158 print(
"The allowance for Mixer from Bob is: ", allowance_mixer)
161 print(
"- Bob approves the transfer of ETHToken to the Mixer")
165 zeth_client.mixer_instance.address,
166 scenario.BOB_DEPOSIT_ETH)
167 eth.waitForTransactionReceipt(tx_hash)
171 zeth_client.mixer_instance.address)
172 print(
"- The allowance for the Mixer from Bob is:", allowance_mixer)
174 result_deposit_bob_to_bob = scenario.bob_deposit(
175 zeth_client, prover_client, mk_tree, bob_eth_address, keystore)
177 print(
"- Balances after Bob's deposit: ")
183 zeth_client.mixer_instance.address
188 received_notes = _receive_notes(
189 result_deposit_bob_to_bob.output_events)
190 recovered_notes_alice = received_notes[
'alice']
191 assert(len(recovered_notes_alice) == 0), \
192 "Alice decrypted a ciphertext that was not encrypted with her key!"
198 recovered_notes_bob = received_notes[
'bob']
199 assert(len(recovered_notes_bob) == 2), \
200 f
"Bob recovered {len(recovered_notes_bob)} notes from deposit, expected 2"
201 input_bob_to_charlie = recovered_notes_bob[0].as_input()
204 result_transfer_bob_to_charlie = scenario.bob_to_charlie(
208 input_bob_to_charlie,
213 result_double_spending =
None
215 result_double_spending = scenario.bob_to_charlie(
219 input_bob_to_charlie,
222 except Exception
as e:
223 print(f
"Bob's double spending successfully rejected! (msg: {e})")
224 assert(result_double_spending
is None),
"Bob spent the same note twice!"
226 print(
"- Balances after Bob's transfer to Charlie: ")
232 zeth_client.mixer_instance.address
236 received_notes = _receive_notes(
237 result_transfer_bob_to_charlie.output_events)
238 note_descs_charlie = received_notes[
'charlie']
239 assert(len(note_descs_charlie) == 1), \
240 f
"Charlie decrypted {len(note_descs_charlie)}. Expected 1!"
242 _ = scenario.charlie_withdraw(
246 note_descs_charlie[0].as_input(),
250 print(
"- Balances after Charlie's withdrawal: ")
256 zeth_client.mixer_instance.address
261 result_double_spending =
None
265 result_double_spending = scenario.charlie_double_withdraw(
270 note_descs_charlie[0].as_input(),
273 except Exception
as e:
274 print(f
"Charlie's double spending successfully rejected! (msg: {e})")
275 print(
"Balances after Charlie's double withdrawal attempt: ")
276 assert(result_double_spending
is None), \
277 "Charlie managed to withdraw the same note twice!"
283 zeth_client.mixer_instance.address)
289 print(
"- Bob approves the transfer of ETHToken to the Mixer")
293 zeth_client.mixer_instance.address,
294 scenario.BOB_DEPOSIT_ETH)
295 eth.waitForTransactionReceipt(tx_hash)
299 zeth_client.mixer_instance.address)
300 print(
"- The allowance for the Mixer from Bob is:", allowance_mixer)
302 result_deposit_bob_to_bob = scenario.charlie_corrupt_bob_deposit(
313 received_notes = _receive_notes(
314 result_deposit_bob_to_bob.output_events)
315 recovered_notes_bob = received_notes[
'bob']
316 assert(len(recovered_notes_bob) == 2), \
317 f
"Bob recovered {len(recovered_notes_bob)} notes from deposit, expected 2"
319 print(
"- Balances after Bob's last deposit: ")
325 zeth_client.mixer_instance.address)
328 "========================================\n" +
330 "========================================\n")
333 if __name__ ==
'__main__':