From cd94fd850b73bb2e0b2221a84e68bba6ca6fa473 Mon Sep 17 00:00:00 2001 From: Lesenechal Remi Date: Fri, 22 Nov 2019 15:52:46 +0100 Subject: [PATCH] move common funciton test helpers --- src/test/multisig_tests.ml | 19 ++----------------- src/test/multisig_v2_tests.ml | 15 ++------------- src/test/test_helpers.ml | 20 ++++++++++++++++++++ 3 files changed, 24 insertions(+), 30 deletions(-) diff --git a/src/test/multisig_tests.ml b/src/test/multisig_tests.ml index 97465d5cf..7091254e1 100644 --- a/src/test/multisig_tests.ml +++ b/src/test/multisig_tests.ml @@ -34,21 +34,6 @@ let str_keys (raw_pkh, raw_pk, raw_sk) = let pkh_str = Signature.Public_key_hash.to_b58check raw_pkh in (pkh_str,pk_str,sk_str) -let sign_message (payload : expression) sk : string result = - let open Tezos_crypto in - let%bind program,_ = get_program () in - let%bind code = - let env = Ast_typed.program_environment program in - Compile.Of_simplified.compile_expression_as_function - ~env ~state:(Typer.Solver.initial_state) payload in - let Compiler.Program.{input=_;output=(Ex_ty payload_ty);body=_} = code in - let%bind payload = - Ligo.Run.Of_michelson.evaluate_michelson code in - let%bind packed_payload = Ligo.Run.Of_michelson.pack_payload payload payload_ty in - let signed_data = Signature.sign sk packed_payload in - let signature_str = Signature.to_b58check signed_data in - ok signature_str - let init_storage threshold counter pkeys = let keys = List.map (fun el -> @@ -73,6 +58,7 @@ let chain_id_zero = e_chain_id @@ Tezos_crypto.Base58.simple_encode (* sign the message 'msg' with 'keys', if 'is_valid'=false the providid signature will be incorrect *) let params counter msg keys is_validl = + let%bind program,_ = get_program () in let aux = fun acc (key,is_valid) -> let (_,_pk,sk) = key in let (pkh,_,_) = str_keys key in @@ -81,7 +67,7 @@ let params counter msg keys is_validl = e_nat counter ; e_string (if is_valid then "MULTISIG" else "XX") ; chain_id_zero ] in - let%bind signature = sign_message payload sk in + let%bind signature = sign_message program payload sk in ok @@ (e_pair (e_key_hash pkh) (e_signature signature))::acc in let%bind signed_msgs = Trace.bind_fold_list aux [] (List.rev @@ List.combine keys is_validl) in ok @@ e_constructor @@ -92,7 +78,6 @@ let params counter msg keys is_validl = ("signatures" , e_typed_list signed_msgs (t_pair (t_key_hash,t_signature)) ) ; ]) - (* Provide one valid signature when the threshold is two of two keys *) let not_enough_1_of_2 () = let%bind program,_ = get_program () in diff --git a/src/test/multisig_v2_tests.ml b/src/test/multisig_v2_tests.ml index cc9cd4715..12fab69df 100644 --- a/src/test/multisig_v2_tests.ml +++ b/src/test/multisig_v2_tests.ml @@ -22,17 +22,6 @@ let compile_main () = open Ast_simplified -let pack_payload (payload:expression) : bytes result = - let%bind program,_ = get_program () in - let%bind code = - let env = Ast_typed.program_environment program in - Compile.Of_simplified.compile_expression_as_function - ~env ~state:(Typer.Solver.initial_state) payload in - let Compiler.Program.{input=_;output=(Ex_ty payload_ty);body=_} = code in - let%bind (payload: Tezos_utils.Michelson.michelson) = - Ligo.Run.Of_michelson.evaluate_michelson code in - Ligo.Run.Of_michelson.pack_payload payload payload_ty - let contract id = let open Proto_alpha_utils.Memory_proto_alpha in let id = List.nth dummy_environment.identities id in @@ -73,7 +62,7 @@ let wrong_addr () = (* sender message is already stored in the message store *) let already_accounted () = let%bind program,_ = get_program () in - let%bind packed_payload = pack_payload empty_message in + let%bind packed_payload = pack_payload program empty_message in let%bind bytes = e_bytes_ofbytes packed_payload in let init_storage = storage 1 [1;2] [(bytes, e_set [e_address@@ addr 1])] in @@ -90,7 +79,7 @@ let already_accounted () = (* successful storing in the message store *) let succeeded_storing () = let%bind program,_ = get_program () in - let%bind packed_payload = pack_payload empty_message in + let%bind packed_payload = pack_payload program empty_message in let%bind bytes = e_bytes_ofbytes packed_payload in let options = let amount = Memory_proto_alpha.Protocol.Alpha_context.Tez.zero in diff --git a/src/test/test_helpers.ml b/src/test/test_helpers.ml index cedef0e4e..612fc2fe2 100644 --- a/src/test/test_helpers.ml +++ b/src/test/test_helpers.ml @@ -29,6 +29,26 @@ let test name f = let test_suite name lst = Test_suite (name , lst) + +open Ast_simplified + +let pack_payload (program:Ast_typed.program) (payload:expression) : bytes result = + let%bind code = + let env = Ast_typed.program_environment program in + Compile.Of_simplified.compile_expression_as_function + ~env ~state:(Typer.Solver.initial_state) payload in + let Compiler.Program.{input=_;output=(Ex_ty payload_ty);body=_} = code in + let%bind (payload: Tezos_utils.Michelson.michelson) = + Ligo.Run.Of_michelson.evaluate_michelson code in + Ligo.Run.Of_michelson.pack_payload payload payload_ty + +let sign_message (program:Ast_typed.program) (payload : expression) sk : string result = + let open Tezos_crypto in + let%bind packed_payload = pack_payload program payload in + let signed_data = Signature.sign sk packed_payload in + let signature_str = Signature.to_b58check signed_data in + ok signature_str + open Ast_simplified.Combinators let expect ?options program entry_point input expecter =