From 9986785fa2fd1ef298114cf658835263d9c9e199 Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Tue, 17 Oct 2023 15:37:41 +0100 Subject: [PATCH] Make payments test deterministic, using a quickcheck generator --- .../transaction_union/transaction_union.ml | 92 +++++++++++-------- src/lib/transaction_snark/test/util.ml | 18 +++- src/lib/transaction_snark/test/util.mli | 3 +- .../test/zkapp_payments/zkapp_payments.ml | 2 +- 4 files changed, 72 insertions(+), 43 deletions(-) diff --git a/src/lib/transaction_snark/test/transaction_union/transaction_union.ml b/src/lib/transaction_snark/test/transaction_union/transaction_union.ml index 69117e17c9c..965614e7274 100644 --- a/src/lib/transaction_snark/test/transaction_union/transaction_union.ml +++ b/src/lib/transaction_snark/test/transaction_union/transaction_union.ml @@ -171,7 +171,7 @@ let%test_module "Transaction union tests" = let%test_unit "new_account" = Test_util.with_randomness 123456789 (fun () -> - let wallets = U.Wallet.random_wallets () in + let wallets = Quickcheck.random_value (U.Wallet.random_wallets ()) in Ledger.with_ledger ~depth:ledger_depth ~f:(fun ledger -> Array.iter (Array.sub wallets ~pos:1 ~len:(Array.length wallets - 1)) @@ -243,7 +243,10 @@ let%test_module "Transaction union tests" = let%test_unit "account creation fee - user commands" = Test_util.with_randomness 123456789 (fun () -> - let wallets = U.Wallet.random_wallets ~n:3 () |> Array.to_list in + let wallets = + Quickcheck.random_value (U.Wallet.random_wallets ~n:3 ()) + |> Array.to_list + in let sender = List.hd_exn wallets in let receivers = List.tl_exn wallets in let txns_per_receiver = 2 in @@ -294,7 +297,10 @@ let%test_module "Transaction union tests" = let%test_unit "account creation fee - fee transfers" = Test_util.with_randomness 123456789 (fun () -> - let receivers = U.Wallet.random_wallets ~n:3 () |> Array.to_list in + let receivers = + Quickcheck.random_value (U.Wallet.random_wallets ~n:3 ()) + |> Array.to_list + in let txns_per_receiver = 3 in let fee = 8_000_000_000 in Ledger.with_ledger ~depth:ledger_depth ~f:(fun ledger -> @@ -329,7 +335,9 @@ let%test_module "Transaction union tests" = let%test_unit "account creation fee - coinbase" = Test_util.with_randomness 123456789 (fun () -> - let wallets = U.Wallet.random_wallets ~n:3 () in + let wallets = + Quickcheck.random_value (U.Wallet.random_wallets ~n:3 ()) + in let receiver = wallets.(0) in let other = wallets.(1) in let dummy_account = wallets.(2) in @@ -386,7 +394,7 @@ let%test_module "Transaction union tests" = ~carryforward1 ~carryforward2 = let module T = (val Lazy.force U.snark_module) in Test_util.with_randomness 123456789 (fun () -> - let wallets = U.Wallet.random_wallets () in + let wallets = Quickcheck.random_value (U.Wallet.random_wallets ()) in (*let state_body = Lazy.force state_body in let state_body_hash = Lazy.force state_body_hash in*) let state_body_hash1, state_body1 = state_hash_and_body1 in @@ -674,7 +682,7 @@ let%test_module "Transaction union tests" = charges fee" = Test_util.with_randomness 123456789 (fun () -> Ledger.with_ledger ~depth:ledger_depth ~f:(fun ledger -> - let wallets = U.Wallet.random_wallets ~n:2 () in + let wallets = Quickcheck.random_value (U.Wallet.random_wallets ~n:2 ()) in let signer = wallets.(0).private_key in let fee_payer_pk = wallets.(0).account.public_key in let source_pk = fee_payer_pk in @@ -711,7 +719,7 @@ let%test_module "Transaction union tests" = let%test_unit "transfer non-default tokens to an existing account" = Test_util.with_randomness 123456789 (fun () -> Ledger.with_ledger ~depth:ledger_depth ~f:(fun ledger -> - let wallets = U.Wallet.random_wallets ~n:2 () in + let wallets = Quickcheck.random_value (U.Wallet.random_wallets ~n:2 ()) in let signer = wallets.(0).private_key in let fee_payer_pk = wallets.(0).account.public_key in let source_pk = fee_payer_pk in @@ -760,7 +768,7 @@ let%test_module "Transaction union tests" = transfer" = Test_util.with_randomness 123456789 (fun () -> Ledger.with_ledger ~depth:ledger_depth ~f:(fun ledger -> - let wallets = U.Wallet.random_wallets ~n:2 () in + let wallets = Quickcheck.random_value (U.Wallet.random_wallets ~n:2 ()) in let signer = wallets.(0).private_key in let fee_payer_pk = wallets.(0).account.public_key in let source_pk = fee_payer_pk in @@ -799,7 +807,7 @@ let%test_module "Transaction union tests" = let%test_unit "insufficient source balance for non-default token transfer" = Test_util.with_randomness 123456789 (fun () -> Ledger.with_ledger ~depth:ledger_depth ~f:(fun ledger -> - let wallets = U.Wallet.random_wallets ~n:2 () in + let wallets = Quickcheck.random_value (U.Wallet.random_wallets ~n:2 ()) in let signer = wallets.(0).private_key in let fee_payer_pk = wallets.(0).account.public_key in let source_pk = fee_payer_pk in @@ -837,7 +845,7 @@ let%test_module "Transaction union tests" = let%test_unit "transfer non-existing source" = Test_util.with_randomness 123456789 (fun () -> Ledger.with_ledger ~depth:ledger_depth ~f:(fun ledger -> - let wallets = U.Wallet.random_wallets ~n:2 () in + let wallets = Quickcheck.random_value (U.Wallet.random_wallets ~n:2 ()) in let signer = wallets.(0).private_key in let fee_payer_pk = wallets.(0).account.public_key in let source_pk = wallets.(1).account.public_key in @@ -868,7 +876,9 @@ let%test_module "Transaction union tests" = let%test_unit "delegation delegatee does not exist" = Test_util.with_randomness 123456789 (fun () -> Ledger.with_ledger ~depth:ledger_depth ~f:(fun ledger -> - let wallets = U.Wallet.random_wallets ~n:2 () in + let wallets = + Quickcheck.random_value (U.Wallet.random_wallets ~n:2 ()) + in let signer = wallets.(0).private_key in let fee_payer_pk = wallets.(0).account.public_key in let source_pk = fee_payer_pk in @@ -901,7 +911,9 @@ let%test_module "Transaction union tests" = let%test_unit "delegation delegator does not exist" = Test_util.with_randomness 123456789 (fun () -> Ledger.with_ledger ~depth:ledger_depth ~f:(fun ledger -> - let wallets = U.Wallet.random_wallets ~n:3 () in + let wallets = + Quickcheck.random_value (U.Wallet.random_wallets ~n:3 ()) + in let signer = wallets.(0).private_key in let fee_payer_pk = wallets.(0).account.public_key in let receiver_pk = wallets.(2).account.public_key in @@ -932,7 +944,9 @@ let%test_module "Transaction union tests" = let%test_unit "Payment to and from same key" = Test_util.with_randomness 123456789 (fun () -> Ledger.with_ledger ~depth:ledger_depth ~f:(fun ledger -> - let wallets = U.Wallet.random_wallets ~n:2 () in + let wallets = + Quickcheck.random_value (U.Wallet.random_wallets ~n:2 ()) + in let signer = wallets.(0).private_key in let fee_payer_pk = wallets.(0).account.public_key in let receiver_pk = wallets.(0).account.public_key in @@ -959,7 +973,9 @@ let%test_module "Transaction union tests" = let%test_unit "Payment with source balance insufficient- untimed" = Test_util.with_randomness 123456789 (fun () -> Ledger.with_ledger ~depth:ledger_depth ~f:(fun ledger -> - let wallets = U.Wallet.random_wallets ~n:2 () in + let wallets = + Quickcheck.random_value (U.Wallet.random_wallets ~n:2 ()) + in let signer = wallets.(0).private_key in let fee_payer_pk = wallets.(0).account.public_key in let receiver_pk = wallets.(1).account.public_key in @@ -979,7 +995,9 @@ let%test_module "Transaction union tests" = let%test_unit "Payment with insufficient account creation fee- untimed" = Test_util.with_randomness 123456789 (fun () -> Ledger.with_ledger ~depth:ledger_depth ~f:(fun ledger -> - let wallets = U.Wallet.random_wallets ~n:2 () in + let wallets = + Quickcheck.random_value (U.Wallet.random_wallets ~n:2 ()) + in let signer = wallets.(0).private_key in let fee_payer_pk = wallets.(0).account.public_key in let receiver_pk = wallets.(1).account.public_key in @@ -1000,7 +1018,9 @@ let%test_module "Transaction union tests" = let%test_unit "timed account - transactions" = Test_util.with_randomness 123456789 (fun () -> - let wallets = U.Wallet.random_wallets ~n:3 () in + let wallets = + Quickcheck.random_value (U.Wallet.random_wallets ~n:3 ()) + in let sender = wallets.(0) in let receivers = Array.to_list wallets |> List.tl_exn in let txns_per_receiver = 2 in @@ -1074,7 +1094,7 @@ let%test_module "Transaction union tests" = (*let%test_unit "create own new token" = Test_util.with_randomness 123456789 (fun () -> Ledger.with_ledger ~depth:ledger_depth ~f:(fun ledger -> - let wallets = U.Wallet.random_wallets ~n:1 () in + let wallets = Quickcheck.random_value (U.Wallet.random_wallets ~n:1 ()) in let signer = wallets.(0).private_key in (* Fee payer is the new token owner. *) let fee_payer_pk = wallets.(0).account.public_key in @@ -1110,7 +1130,7 @@ let%test_module "Transaction union tests" = let%test_unit "create new token for a different pk" = Test_util.with_randomness 123456789 (fun () -> Ledger.with_ledger ~depth:ledger_depth ~f:(fun ledger -> - let wallets = U.Wallet.random_wallets ~n:2 () in + let wallets = Quickcheck.random_value (U.Wallet.random_wallets ~n:2 ()) in let signer = wallets.(0).private_key in (* Fee payer and new token owner are distinct. *) let fee_payer_pk = wallets.(0).account.public_key in @@ -1146,7 +1166,7 @@ let%test_module "Transaction union tests" = let%test_unit "create new token for a different pk new accounts disabled" = Test_util.with_randomness 123456789 (fun () -> Ledger.with_ledger ~depth:ledger_depth ~f:(fun ledger -> - let wallets = U.Wallet.random_wallets ~n:2 () in + let wallets = Quickcheck.random_value (U.Wallet.random_wallets ~n:2 ()) in let signer = wallets.(0).private_key in (* Fee payer and new token owner are distinct. *) let fee_payer_pk = wallets.(0).account.public_key in @@ -1182,7 +1202,7 @@ let%test_module "Transaction union tests" = let%test_unit "create own new token account" = Test_util.with_randomness 123456789 (fun () -> Ledger.with_ledger ~depth:ledger_depth ~f:(fun ledger -> - let wallets = U.Wallet.random_wallets ~n:2 () in + let wallets = Quickcheck.random_value (U.Wallet.random_wallets ~n:2 ()) in let signer = wallets.(0).private_key in (* Fee-payer and receiver are the same, token owner differs. *) let fee_payer_pk = wallets.(0).account.public_key in @@ -1231,7 +1251,7 @@ let%test_module "Transaction union tests" = let%test_unit "create new token account for a different pk" = Test_util.with_randomness 123456789 (fun () -> Ledger.with_ledger ~depth:ledger_depth ~f:(fun ledger -> - let wallets = U.Wallet.random_wallets ~n:3 () in + let wallets = Quickcheck.random_value (U.Wallet.random_wallets ~n:3 ()) in let signer = wallets.(0).private_key in (* Fee-payer, receiver, and token owner differ. *) let fee_payer_pk = wallets.(0).account.public_key in @@ -1281,7 +1301,7 @@ let%test_module "Transaction union tests" = token" = Test_util.with_randomness 123456789 (fun () -> Ledger.with_ledger ~depth:ledger_depth ~f:(fun ledger -> - let wallets = U.Wallet.random_wallets ~n:2 () in + let wallets = Quickcheck.random_value (U.Wallet.random_wallets ~n:2 ()) in let signer = wallets.(0).private_key in (* Fee-payer and token owner are the same, receiver differs. *) let fee_payer_pk = wallets.(0).account.public_key in @@ -1330,7 +1350,7 @@ let%test_module "Transaction union tests" = let%test_unit "create new own locked token account in a locked token" = Test_util.with_randomness 123456789 (fun () -> Ledger.with_ledger ~depth:ledger_depth ~f:(fun ledger -> - let wallets = U.Wallet.random_wallets ~n:2 () in + let wallets = Quickcheck.random_value (U.Wallet.random_wallets ~n:2 ()) in let signer = wallets.(0).private_key in (* Fee-payer and receiver are the same, token owner differs. *) let fee_payer_pk = wallets.(0).account.public_key in @@ -1380,7 +1400,7 @@ let%test_module "Transaction union tests" = fee-payer" = Test_util.with_randomness 123456789 (fun () -> Ledger.with_ledger ~depth:ledger_depth ~f:(fun ledger -> - let wallets = U.Wallet.random_wallets ~n:3 () in + let wallets = Quickcheck.random_value (U.Wallet.random_wallets ~n:3 ()) in let signer = wallets.(0).private_key in (* Fee-payer, receiver, and token owner differ. *) let fee_payer_pk = wallets.(0).account.public_key in @@ -1424,7 +1444,7 @@ let%test_module "Transaction union tests" = non-owner fee-payer" = Test_util.with_randomness 123456789 (fun () -> Ledger.with_ledger ~depth:ledger_depth ~f:(fun ledger -> - let wallets = U.Wallet.random_wallets ~n:3 () in + let wallets = Quickcheck.random_value (U.Wallet.random_wallets ~n:3 ()) in let signer = wallets.(0).private_key in (* Fee-payer, receiver, and token owner differ. *) let fee_payer_pk = wallets.(0).account.public_key in @@ -1467,7 +1487,7 @@ let%test_module "Transaction union tests" = let%test_unit "create new token account fails if account exists" = Test_util.with_randomness 123456789 (fun () -> Ledger.with_ledger ~depth:ledger_depth ~f:(fun ledger -> - let wallets = U.Wallet.random_wallets ~n:3 () in + let wallets = Quickcheck.random_value (U.Wallet.random_wallets ~n:3 ()) in let signer = wallets.(0).private_key in (* Fee-payer, receiver, and token owner differ. *) let fee_payer_pk = wallets.(0).account.public_key in @@ -1513,7 +1533,7 @@ let%test_module "Transaction union tests" = let%test_unit "create new token account fails if receiver is token owner" = Test_util.with_randomness 123456789 (fun () -> Ledger.with_ledger ~depth:ledger_depth ~f:(fun ledger -> - let wallets = U.Wallet.random_wallets ~n:2 () in + let wallets = Quickcheck.random_value (U.Wallet.random_wallets ~n:2 ()) in let signer = wallets.(0).private_key in (* Receiver and token owner are the same, fee-payer differs. *) let fee_payer_pk = wallets.(0).account.public_key in @@ -1559,7 +1579,7 @@ let%test_module "Transaction union tests" = doesn't own the token" = Test_util.with_randomness 123456789 (fun () -> Ledger.with_ledger ~depth:ledger_depth ~f:(fun ledger -> - let wallets = U.Wallet.random_wallets ~n:3 () in + let wallets = Quickcheck.random_value (U.Wallet.random_wallets ~n:3 ()) in let signer = wallets.(0).private_key in (* Fee-payer, receiver, and token owner differ. *) let fee_payer_pk = wallets.(0).account.public_key in @@ -1601,7 +1621,7 @@ let%test_module "Transaction union tests" = also the account creation target and does not exist" = Test_util.with_randomness 123456789 (fun () -> Ledger.with_ledger ~depth:ledger_depth ~f:(fun ledger -> - let wallets = U.Wallet.random_wallets ~n:3 () in + let wallets = Quickcheck.random_value (U.Wallet.random_wallets ~n:3 ()) in let signer = wallets.(0).private_key in (* Fee-payer, receiver, and token owner are the same. *) let fee_payer_pk = wallets.(0).account.public_key in @@ -1637,7 +1657,7 @@ let%test_module "Transaction union tests" = let%test_unit "create new token account works for default token" = Test_util.with_randomness 123456789 (fun () -> Ledger.with_ledger ~depth:ledger_depth ~f:(fun ledger -> - let wallets = U.Wallet.random_wallets ~n:2 () in + let wallets = Quickcheck.random_value (U.Wallet.random_wallets ~n:2 ()) in let signer = wallets.(0).private_key in (* Fee-payer and receiver are the same, token owner differs. *) let fee_payer_pk = wallets.(0).account.public_key in @@ -1681,7 +1701,7 @@ let%test_module "Transaction union tests" = let%test_unit "mint tokens in owner's account" = Test_util.with_randomness 123456789 (fun () -> Ledger.with_ledger ~depth:ledger_depth ~f:(fun ledger -> - let wallets = U.Wallet.random_wallets ~n:1 () in + let wallets = Quickcheck.random_value (U.Wallet.random_wallets ~n:1 ()) in let signer = wallets.(0).private_key in (* Fee-payer, receiver, and token owner are the same. *) let fee_payer_pk = wallets.(0).account.public_key in @@ -1726,7 +1746,7 @@ let%test_module "Transaction union tests" = let%test_unit "mint tokens in another pk's account" = Test_util.with_randomness 123456789 (fun () -> Ledger.with_ledger ~depth:ledger_depth ~f:(fun ledger -> - let wallets = U.Wallet.random_wallets ~n:2 () in + let wallets = Quickcheck.random_value (U.Wallet.random_wallets ~n:2 ()) in let signer = wallets.(0).private_key in (* Fee-payer and token owner are the same, receiver differs. *) let fee_payer_pk = wallets.(0).account.public_key in @@ -1777,7 +1797,7 @@ let%test_module "Transaction union tests" = token owner" = Test_util.with_randomness 123456789 (fun () -> Ledger.with_ledger ~depth:ledger_depth ~f:(fun ledger -> - let wallets = U.Wallet.random_wallets ~n:2 () in + let wallets = Quickcheck.random_value (U.Wallet.random_wallets ~n:2 ()) in let signer = wallets.(0).private_key in (* Fee-payer and token owner are the same, receiver differs. *) let fee_payer_pk = wallets.(0).account.public_key in @@ -1821,7 +1841,7 @@ let%test_module "Transaction union tests" = = Test_util.with_randomness 123456789 (fun () -> Ledger.with_ledger ~depth:ledger_depth ~f:(fun ledger -> - let wallets = U.Wallet.random_wallets ~n:2 () in + let wallets = Quickcheck.random_value (U.Wallet.random_wallets ~n:2 ()) in let signer = wallets.(0).private_key in (* Fee-payer and token owner are the same, receiver differs. *) let fee_payer_pk = wallets.(0).account.public_key in @@ -1861,7 +1881,7 @@ let%test_module "Transaction union tests" = to mint" = Test_util.with_randomness 123456789 (fun () -> Ledger.with_ledger ~depth:ledger_depth ~f:(fun ledger -> - let wallets = U.Wallet.random_wallets ~n:2 () in + let wallets = Quickcheck.random_value (U.Wallet.random_wallets ~n:2 ()) in let signer = wallets.(0).private_key in (* Fee-payer and receiver are the same, token owner differs. *) let fee_payer_pk = wallets.(0).account.public_key in @@ -1907,7 +1927,7 @@ let%test_module "Transaction union tests" = let%test_unit "mint tokens fails if the receiver account is not present" = Test_util.with_randomness 123456789 (fun () -> Ledger.with_ledger ~depth:ledger_depth ~f:(fun ledger -> - let wallets = U.Wallet.random_wallets ~n:2 () in + let wallets = Quickcheck.random_value (U.Wallet.random_wallets ~n:2 ()) in let signer = wallets.(0).private_key in (* Fee-payer and fee payer are the same, receiver differs. *) let fee_payer_pk = wallets.(0).account.public_key in diff --git a/src/lib/transaction_snark/test/util.ml b/src/lib/transaction_snark/test/util.ml index b9fab627abb..8d3c5534b25 100644 --- a/src/lib/transaction_snark/test/util.ml +++ b/src/lib/transaction_snark/test/util.ml @@ -438,19 +438,27 @@ module Wallet = struct type t = { private_key : Private_key.t; account : Account.t } let random_wallets ?(n = min (Int.pow 2 ledger_depth) (1 lsl 10)) () = - let random_wallet () : t = - let private_key = Private_key.create () in + let open Quickcheck.Generator.Let_syntax in + let random_wallet : t Quickcheck.Generator.t = + let%bind private_key = Private_key.gen in let public_key = Public_key.compress (Public_key.of_private_key_exn private_key) in let account_id = Account_id.create public_key Token_id.default in + let%map amount = Int.gen_incl 0 99 in { private_key ; account = - Account.create account_id - (Balance.of_mina_int_exn (50 + Random.int 100)) + Account.create account_id (Balance.of_mina_int_exn (50 + amount)) } in - Array.init n ~f:(fun _ -> random_wallet ()) + Quickcheck.Generator.create (fun ~size ~random -> + (* It appears that we should accumulate some state here, but [random] + does this internally for us as we call the generator. See e.g. the + definition of [Quickcheck.Generator.bind] for the general pattern that + we apply here. + *) + Array.init n ~f:(fun _ -> + Quickcheck.Generator.generate random_wallet ~size ~random ) ) let user_command ~fee_payer ~receiver_pk amt fee nonce memo = let payload : Signed_command.Payload.t = diff --git a/src/lib/transaction_snark/test/util.mli b/src/lib/transaction_snark/test/util.mli index 393c6316bca..25a426f9bd3 100644 --- a/src/lib/transaction_snark/test/util.mli +++ b/src/lib/transaction_snark/test/util.mli @@ -1,3 +1,4 @@ +open Core_kernel open Mina_base open Snark_params @@ -117,7 +118,7 @@ val pending_coinbase_stack_target : module Wallet : sig type t = { private_key : Signature_lib.Private_key.t; account : Account.t } - val random_wallets : ?n:int -> unit -> t array + val random_wallets : ?n:int -> unit -> t array Quickcheck.Generator.t val user_command_with_wallet : t array diff --git a/src/lib/transaction_snark/test/zkapp_payments/zkapp_payments.ml b/src/lib/transaction_snark/test/zkapp_payments/zkapp_payments.ml index 5740a37fe0f..66019e53a72 100644 --- a/src/lib/transaction_snark/test/zkapp_payments/zkapp_payments.ml +++ b/src/lib/transaction_snark/test/zkapp_payments/zkapp_payments.ml @@ -112,7 +112,7 @@ let%test_module "Zkapp payments tests" = let%test_unit "merkle_root_after_zkapp_command_exn_immutable" = Test_util.with_randomness 123456789 (fun () -> - let wallets = U.Wallet.random_wallets () in + let wallets = Quickcheck.random_value (U.Wallet.random_wallets ()) in Ledger.with_ledger ~depth:U.ledger_depth ~f:(fun ledger -> Array.iter (Array.sub wallets ~pos:1 ~len:(Array.length wallets - 1))