Skip to content

Commit

Permalink
Merge pull request #14358 from MinaProtocol/feature/use-proof-cache-more
Browse files Browse the repository at this point in the history
[berkeley] Use Proof_cache for more transaction snark tests, make tests deterministic
  • Loading branch information
mrmr1993 authored Oct 17, 2023
2 parents 08d678a + 57f53ca commit 2825f09
Show file tree
Hide file tree
Showing 15 changed files with 154 additions and 49 deletions.
4 changes: 3 additions & 1 deletion src/lib/transaction_snark/test/fee_payer/dune
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
yojson
sexplib0
integers
ppx_deriving_yojson.runtime
result
;; local libraries
mina_base.import
pickles
Expand All @@ -35,7 +37,7 @@
transaction_snark_tests
)
(library_flags -linkall)
(inline_tests (flags -verbose -show-counts))
(inline_tests (flags -verbose -show-counts) (deps proof_cache.json))
(preprocess
(pps ppx_snarky ppx_version ppx_jane))
(instrumentation (backend bisect_ppx)))
13 changes: 13 additions & 0 deletions src/lib/transaction_snark/test/fee_payer/fee_payer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ open Mina_transaction

let%test_module "Fee payer tests" =
( module struct
let proof_cache =
Result.ok_or_failwith @@ Pickles.Proof_cache.of_yojson
@@ Yojson.Safe.from_file "proof_cache.json"

let () = Transaction_snark.For_tests.set_proof_cache proof_cache

let `VK vk, `Prover zkapp_prover = Lazy.force U.trivial_zkapp

let memo = Signed_command_memo.create_from_string_exn "Fee payer tests"
Expand Down Expand Up @@ -316,4 +322,11 @@ let%test_module "Fee payer tests" =
List.is_empty
(Zkapp_command.account_updates_list zkapp_command) ) ;
U.check_zkapp_command_with_merges_exn ledger [ zkapp_command ] ) ) )

let () =
match Sys.getenv "PROOF_CACHE_OUT" with
| Some path ->
Yojson.Safe.to_file path @@ Pickles.Proof_cache.to_yojson proof_cache
| None ->
()
end )
1 change: 1 addition & 0 deletions src/lib/transaction_snark/test/fee_payer/proof_cache.json

Large diffs are not rendered by default.

Large diffs are not rendered by default.

18 changes: 13 additions & 5 deletions src/lib/transaction_snark/test/util.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
3 changes: 2 additions & 1 deletion src/lib/transaction_snark/test/util.mli
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
open Core_kernel
open Mina_base
open Snark_params

Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/lib/transaction_snark/test/zkapp_deploy/dune
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
base
core_kernel
yojson
ppx_deriving_yojson.runtime
result
;; local libraries
mina_base.import
pickles
Expand All @@ -32,7 +34,7 @@
transaction_snark_tests
)
(library_flags -linkall)
(inline_tests (flags -verbose -show-counts))
(inline_tests (flags -verbose -show-counts) (deps proof_cache.json))
(preprocess
(pps ppx_snarky ppx_version ppx_jane))
(instrumentation (backend bisect_ppx)))

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions src/lib/transaction_snark/test/zkapp_deploy/zkapp_deploy.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ open Mina_base

let%test_module "zkApp deploy tests" =
( module struct
let proof_cache =
Result.ok_or_failwith @@ Pickles.Proof_cache.of_yojson
@@ Yojson.Safe.from_file "proof_cache.json"

let () = Transaction_snark.For_tests.set_proof_cache proof_cache

let memo = Signed_command_memo.create_from_string_exn "zkApp deploy tests"

let constraint_constants = U.constraint_constants
Expand Down Expand Up @@ -178,4 +184,11 @@ let%test_module "zkApp deploy tests" =
U.check_zkapp_command_with_merges_exn ledger
~expected_failure:(Invalid_fee_excess, Pass_2)
[ zkapp_command ] ) ) )

let () =
match Sys.getenv "PROOF_CACHE_OUT" with
| Some path ->
Yojson.Safe.to_file path @@ Pickles.Proof_cache.to_yojson proof_cache
| None ->
()
end )
4 changes: 3 additions & 1 deletion src/lib/transaction_snark/test/zkapp_payments/dune
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
core_kernel
yojson
sexplib0
ppx_deriving_yojson.runtime
result
;; local libraries
logger
mina_base.import
Expand All @@ -36,7 +38,7 @@
mina_transaction
)
(library_flags -linkall)
(inline_tests (flags -verbose -show-counts))
(inline_tests (flags -verbose -show-counts) (deps proof_cache.json))
(preprocess
(pps ppx_snarky ppx_version ppx_jane ppx_mina))
(instrumentation (backend bisect_ppx)))

Large diffs are not rendered by default.

15 changes: 14 additions & 1 deletion src/lib/transaction_snark/test/zkapp_payments/zkapp_payments.ml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ open Mina_base

let%test_module "Zkapp payments tests" =
( module struct
let proof_cache =
Result.ok_or_failwith @@ Pickles.Proof_cache.of_yojson
@@ Yojson.Safe.from_file "proof_cache.json"

let () = Transaction_snark.For_tests.set_proof_cache proof_cache

let memo = Signed_command_memo.create_from_string_exn "Zkapp payments tests"

[@@@warning "-32"]
Expand Down Expand Up @@ -112,7 +118,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))
Expand Down Expand Up @@ -289,4 +295,11 @@ let%test_module "Zkapp payments tests" =
~expected_failure:
(Transaction_status.Failure.Overflow, Pass_2)
ledger [ zkapp_command ] ) ) )

let () =
match Sys.getenv "PROOF_CACHE_OUT" with
| Some path ->
Yojson.Safe.to_file path @@ Pickles.Proof_cache.to_yojson proof_cache
| None ->
()
end )
4 changes: 3 additions & 1 deletion src/lib/transaction_snark/test/zkapp_preconditions/dune
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
base
core_kernel
yojson
ppx_deriving_yojson.runtime
result
;; local libraries
mina_base.import
data_hash_lib
Expand Down Expand Up @@ -39,7 +41,7 @@
random_oracle_input
zkapp_command_builder)
(library_flags -linkall)
(inline_tests (flags -verbose -show-counts))
(inline_tests (flags -verbose -show-counts) (deps proof_cache.json))
(preprocess
(pps ppx_snarky ppx_version ppx_jane))
(instrumentation (backend bisect_ppx)))

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,17 @@ module U = Transaction_snark_tests.Util
module Spec = Transaction_snark.For_tests.Update_states_spec
open Mina_base

let proof_cache =
lazy
( Result.ok_or_failwith @@ Pickles.Proof_cache.of_yojson
@@ Yojson.Safe.from_file "proof_cache.json" )

let%test_module "Valid_while precondition tests" =
( module struct
let proof_cache = Lazy.force proof_cache

let () = Transaction_snark.For_tests.set_proof_cache proof_cache

let constraint_constants = U.constraint_constants

let `VK vk, `Prover zkapp_prover = Lazy.force U.trivial_zkapp
Expand Down Expand Up @@ -99,6 +108,10 @@ let%test_module "Valid_while precondition tests" =

let%test_module "Protocol state precondition tests" =
( module struct
let proof_cache = Lazy.force proof_cache

let () = Transaction_snark.For_tests.set_proof_cache proof_cache

let `VK vk, `Prover zkapp_prover = Lazy.force U.trivial_zkapp

let constraint_constants = U.constraint_constants
Expand Down Expand Up @@ -402,6 +415,10 @@ let%test_module "Protocol state precondition tests" =

let%test_module "Account precondition tests" =
( module struct
let proof_cache = Lazy.force proof_cache

let () = Transaction_snark.For_tests.set_proof_cache proof_cache

let `VK vk, `Prover zkapp_prover = Lazy.force U.trivial_zkapp

let zkapp_prover_and_vk = (zkapp_prover, vk)
Expand Down Expand Up @@ -696,7 +713,9 @@ let%test_module "Account precondition tests" =
let account_creation_fee =
Currency.Fee.to_nanomina_int constraint_constants.account_creation_fee
in
Quickcheck.test ~trials:5 Signature_lib.Keypair.gen ~f:(fun new_kp ->
Quickcheck.test ~trials:5
(Quickcheck.Generator.tuple2 Signature_lib.Keypair.gen
Signature_lib.Keypair.gen ) ~f:(fun (new_kp, token_account) ->
Mina_ledger.Ledger.with_ledger ~depth:U.ledger_depth ~f:(fun ledger ->
Async.Thread_safe.block_on_async_exn (fun () ->
let module Init_ledger =
Expand All @@ -715,7 +734,6 @@ let%test_module "Account precondition tests" =
Account_id.derive_token_id
~owner:(Account_id.create token_owner_pk Token_id.default)
in
let token_account = Keypair.create () in
let token_account_pk =
Public_key.compress token_account.public_key
in
Expand Down Expand Up @@ -993,4 +1011,11 @@ let%test_module "Account precondition tests" =
failwith
"Expected transaction to fail due to invalid account \
precondition in the fee payer" ) )

let () =
match Sys.getenv_opt "PROOF_CACHE_OUT" with
| Some path ->
Yojson.Safe.to_file path @@ Pickles.Proof_cache.to_yojson proof_cache
| None ->
()
end )

0 comments on commit 2825f09

Please sign in to comment.