From 7756bfda9396bd64f5757fca1c0442ae827659c9 Mon Sep 17 00:00:00 2001 From: John David Pressman Date: Wed, 22 Jan 2020 01:30:04 -0800 Subject: [PATCH 1/7] Add rough draft of hashlock contract --- src/test/contracts/hashlock.mligo | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 src/test/contracts/hashlock.mligo diff --git a/src/test/contracts/hashlock.mligo b/src/test/contracts/hashlock.mligo new file mode 100644 index 000000000..b30f2bc9b --- /dev/null +++ b/src/test/contracts/hashlock.mligo @@ -0,0 +1,16 @@ +type storage = { + hashed: bytes; + unused: bool; +} + +type parameter = { + hashable: bytes; + message: unit -> operation list; +} + +let main ((p,s): parameter * storage) : operation list * storage = + if ((Crypto.sha256 p.hashable) = s.hashed) && s.unused + then + let s: storage = {hashed = s.hashed; unused = false} in + ((p.message ()), s) + else (([]: operation list), s) From d8783874241a448cb4bc1ca21bdc57844ffeed18 Mon Sep 17 00:00:00 2001 From: John David Pressman Date: Wed, 22 Jan 2020 01:40:23 -0800 Subject: [PATCH 2/7] Add salt to hashlock contract --- src/test/contracts/hashlock.mligo | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/test/contracts/hashlock.mligo b/src/test/contracts/hashlock.mligo index b30f2bc9b..acd7fcf9b 100644 --- a/src/test/contracts/hashlock.mligo +++ b/src/test/contracts/hashlock.mligo @@ -9,6 +9,9 @@ type parameter = { } let main ((p,s): parameter * storage) : operation list * storage = + (* We have to use a hash salted with the solvers address, otherwise a baker + could steal *) + let salted: bytes = Bytes.concat p.hashable (Bytes.pack sender) in if ((Crypto.sha256 p.hashable) = s.hashed) && s.unused then let s: storage = {hashed = s.hashed; unused = false} in From 3a320c26c81a5f841050d245bad0c342a0e0d7f6 Mon Sep 17 00:00:00 2001 From: John David Pressman Date: Fri, 24 Jan 2020 03:29:00 -0800 Subject: [PATCH 3/7] Modify hashlock to use commit-reveal and add non-working commit test --- src/stages/ast_simplified/combinators.ml | 1 + src/stages/ast_simplified/combinators.mli | 1 + src/test/contracts/hashlock.mligo | 45 ++++++++++-- src/test/hash_lock_tests.ml | 89 +++++++++++++++++++++++ src/test/test.ml | 1 + 5 files changed, 130 insertions(+), 7 deletions(-) create mode 100644 src/test/hash_lock_tests.ml diff --git a/src/stages/ast_simplified/combinators.ml b/src/stages/ast_simplified/combinators.ml index a082e2fa9..851d27d1f 100644 --- a/src/stages/ast_simplified/combinators.ml +++ b/src/stages/ast_simplified/combinators.ml @@ -33,6 +33,7 @@ let t_address : type_expression = make_t @@ T_constant (TC_address) let t_signature : type_expression = make_t @@ T_constant (TC_signature) let t_key : type_expression = make_t @@ T_constant (TC_key) let t_key_hash : type_expression = make_t @@ T_constant (TC_key_hash) +let t_timestamp : type_expression = make_t @@ T_constant (TC_timestamp) let t_option o : type_expression = make_t @@ T_operator (TC_option o) let t_list t : type_expression = make_t @@ T_operator (TC_list t) let t_variable n : type_expression = make_t @@ T_variable (Var.of_name n) diff --git a/src/stages/ast_simplified/combinators.mli b/src/stages/ast_simplified/combinators.mli index 2bc534748..b783e6c43 100644 --- a/src/stages/ast_simplified/combinators.mli +++ b/src/stages/ast_simplified/combinators.mli @@ -21,6 +21,7 @@ val t_unit : type_expression val t_address : type_expression val t_key : type_expression val t_key_hash : type_expression +val t_timestamp : type_expression val t_signature : type_expression (* val t_option : type_expression -> type_expression diff --git a/src/test/contracts/hashlock.mligo b/src/test/contracts/hashlock.mligo index acd7fcf9b..1f9dbb3a4 100644 --- a/src/test/contracts/hashlock.mligo +++ b/src/test/contracts/hashlock.mligo @@ -1,19 +1,50 @@ +type commit = { + date: timestamp; + hashed: bytes; +} + +type commit_set = (address, commit) big_map + type storage = { hashed: bytes; unused: bool; + commits: commit_set; } -type parameter = { +type reveal = { hashable: bytes; message: unit -> operation list; } -let main ((p,s): parameter * storage) : operation list * storage = - (* We have to use a hash salted with the solvers address, otherwise a baker - could steal *) - let salted: bytes = Bytes.concat p.hashable (Bytes.pack sender) in - if ((Crypto.sha256 p.hashable) = s.hashed) && s.unused +type parameter = +| Commit of unit +| Reveal of reveal + +(* We use hash-commit so that a baker can't steal *) +let commit ((p,s): unit * storage) : operation list * storage = + let salted: bytes = Bytes.concat s.hashed (Bytes.pack sender) in + let commit: commit = {date = Current.time + 86400; hashed = salted;} in + let updated_map: commit_set = Big_map.update sender (Some commit) s.commits in + let s = {hashed = s.hashed; unused = s.unused; commits = updated_map} in + (([]: operation list), s) + +let reveal ((p,s): reveal * storage) : operation list * storage = + let commit: commit = + match (Big_map.find_opt sender s.commits) with + | Some c -> c + | None -> (failwith "You haven't made a commitment to hash against yet.": commit) + in + if Current.time < commit.date + then (failwith "It hasn't been 24 hours since your commit yet.": operation list * storage) + else + let salted = Bytes.concat (Crypto.sha256 p.hashable) (Bytes.pack sender) in + if ((Crypto.sha256 salted) = commit.hashed) && s.unused then - let s: storage = {hashed = s.hashed; unused = false} in + let s: storage = {hashed = s.hashed; unused = false; commits = s.commits} in ((p.message ()), s) else (([]: operation list), s) + +let main ((p,s): parameter * storage) : operation list * storage = + match p with + | Commit -> commit ((), s) + | Reveal r -> reveal (r, s) diff --git a/src/test/hash_lock_tests.ml b/src/test/hash_lock_tests.ml new file mode 100644 index 000000000..b14e13cfe --- /dev/null +++ b/src/test/hash_lock_tests.ml @@ -0,0 +1,89 @@ +open Trace +open Test_helpers +open Ast_simplified + +let type_file f = + let%bind simplified = Ligo.Compile.Of_source.compile f (Syntax_name "cameligo") in + let%bind typed,state = Ligo.Compile.Of_simplified.compile simplified in + ok @@ (typed,state) + +let get_program = + let s = ref None in + fun () -> match !s with + | Some s -> ok s + | None -> ( + let%bind program = type_file "./contracts/hashlock.mligo" in + s := Some program ; + ok program + ) + +let compile_main () = + let%bind simplified = Ligo.Compile.Of_source.compile "./contracts/hashlock.mligo" (Syntax_name "cameligo") in + let%bind typed_prg,_ = Ligo.Compile.Of_simplified.compile simplified in + let%bind mini_c_prg = Ligo.Compile.Of_typed.compile typed_prg in + let%bind michelson_prg = Ligo.Compile.Of_mini_c.aggregate_and_compile_contract mini_c_prg "main" in + let%bind (_contract: Tezos_utils.Michelson.michelson) = + (* fails if the given entry point is not a valid contract *) + Ligo.Compile.Of_michelson.build_contract michelson_prg in + ok () + +let empty_op_list = + (e_typed_list [] t_operation) +let empty_message = e_lambda (Var.of_name "arguments") + (Some t_unit) (Some (t_list t_operation)) + empty_op_list + +let call msg = e_constructor "Call" msg +let mk_time st = + match Memory_proto_alpha.Protocol.Alpha_context.Timestamp.of_notation st with + | Some s -> ok s + | None -> simple_fail "bad timestamp notation" +let to_sec t = Tezos_utils.Time.Protocol.to_seconds t +let storage hashed commits = + e_ez_record [("hashed", hashed); + ("unused", e_bool false); + ("commits", commits)] + +let (first_committer , first_contract) = + let open Proto_alpha_utils.Memory_proto_alpha in + let id = List.nth dummy_environment.identities 0 in + let kt = id.implicit_contract in + Protocol.Alpha_context.Contract.to_b58check kt , kt + +let commit () = + let%bind program,_ = get_program () in + let%bind predecessor_timestamp = mk_time "2000-01-01T00:10:10Z" in + let%bind lock_time = mk_time "2000-01-02T00:10:11Z" in + let test_hash = e_bytes_raw (sha_256_hash (Bytes.of_string "hello world")) in + let salted_hash = e_bytes_raw (sha_256_hash + (Bytes.concat + Bytes.empty + [Bytes.of_string first_committer; + (sha_256_hash (Bytes.of_string "hello world"))] + )) + in + let pre_commits = e_typed_big_map [] t_address (t_record_ez [("date", t_timestamp); + ("hashed", t_bytes)]) + in + let init_storage = storage test_hash pre_commits in + let commit = + e_ez_record [("date", e_timestamp + (Int64.to_int (to_sec lock_time))); + ("hashed", salted_hash)] + in + let post_commits = e_big_map [((e_address first_committer), commit)] + in + let post_storage = storage salted_hash post_commits in + let options = + Proto_alpha_utils.Memory_proto_alpha.make_options + ~predecessor_timestamp + ~payer:first_contract + () + in + expect_eq ~options program "commit" + (e_pair (e_unit ()) init_storage) (e_pair empty_op_list post_storage) + +let main = test_suite "Hashlock" [ + test "compile" compile_main ; + test "commit" commit ; +] diff --git a/src/test/test.ml b/src/test/test.ml index b63e1ad5f..99ba02674 100644 --- a/src/test/test.ml +++ b/src/test/test.ml @@ -14,5 +14,6 @@ let () = Multisig_v2_tests.main ; Replaceable_id_tests.main ; Time_lock_tests.main ; + Hash_lock_tests.main ; ] ; () From 62a4482ff3034427ad3b8541a7767d10c30473a3 Mon Sep 17 00:00:00 2001 From: John David Pressman Date: Fri, 24 Jan 2020 13:28:02 -0800 Subject: [PATCH 4/7] Change commit.salted_hash to be hash, reorder salt in tests, still failing --- src/test/contracts/hashlock.mligo | 6 +++--- src/test/hash_lock_tests.ml | 23 +++++++++++------------ 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/test/contracts/hashlock.mligo b/src/test/contracts/hashlock.mligo index 1f9dbb3a4..028f0baec 100644 --- a/src/test/contracts/hashlock.mligo +++ b/src/test/contracts/hashlock.mligo @@ -1,6 +1,6 @@ type commit = { date: timestamp; - hashed: bytes; + salted_hash: bytes; } type commit_set = (address, commit) big_map @@ -23,7 +23,7 @@ type parameter = (* We use hash-commit so that a baker can't steal *) let commit ((p,s): unit * storage) : operation list * storage = let salted: bytes = Bytes.concat s.hashed (Bytes.pack sender) in - let commit: commit = {date = Current.time + 86400; hashed = salted;} in + let commit: commit = {date = Current.time + 86400; salted_hash = Crypto.sha256 salted;} in let updated_map: commit_set = Big_map.update sender (Some commit) s.commits in let s = {hashed = s.hashed; unused = s.unused; commits = updated_map} in (([]: operation list), s) @@ -38,7 +38,7 @@ let reveal ((p,s): reveal * storage) : operation list * storage = then (failwith "It hasn't been 24 hours since your commit yet.": operation list * storage) else let salted = Bytes.concat (Crypto.sha256 p.hashable) (Bytes.pack sender) in - if ((Crypto.sha256 salted) = commit.hashed) && s.unused + if ((Crypto.sha256 salted) = commit.salted_hash) && s.unused then let s: storage = {hashed = s.hashed; unused = false; commits = s.commits} in ((p.message ()), s) diff --git a/src/test/hash_lock_tests.ml b/src/test/hash_lock_tests.ml index b14e13cfe..b82fa5af2 100644 --- a/src/test/hash_lock_tests.ml +++ b/src/test/hash_lock_tests.ml @@ -39,9 +39,9 @@ let mk_time st = | Some s -> ok s | None -> simple_fail "bad timestamp notation" let to_sec t = Tezos_utils.Time.Protocol.to_seconds t -let storage hashed commits = +let storage hashed used commits = e_ez_record [("hashed", hashed); - ("unused", e_bool false); + ("unused", e_bool used); ("commits", commits)] let (first_committer , first_contract) = @@ -54,26 +54,25 @@ let commit () = let%bind program,_ = get_program () in let%bind predecessor_timestamp = mk_time "2000-01-01T00:10:10Z" in let%bind lock_time = mk_time "2000-01-02T00:10:11Z" in - let test_hash = e_bytes_raw (sha_256_hash (Bytes.of_string "hello world")) in + let test_hash_raw = sha_256_hash (Bytes.of_string "hello world") in + let test_hash = e_bytes_raw test_hash_raw in + let%bind packed_sender = pack_payload program (e_bytes_string first_committer) in let salted_hash = e_bytes_raw (sha_256_hash - (Bytes.concat - Bytes.empty - [Bytes.of_string first_committer; - (sha_256_hash (Bytes.of_string "hello world"))] - )) + (Bytes.concat Bytes.empty [test_hash_raw; + packed_sender])) in let pre_commits = e_typed_big_map [] t_address (t_record_ez [("date", t_timestamp); - ("hashed", t_bytes)]) + ("salted_hash", t_bytes)]) in - let init_storage = storage test_hash pre_commits in + let init_storage = storage test_hash true pre_commits in let commit = e_ez_record [("date", e_timestamp (Int64.to_int (to_sec lock_time))); - ("hashed", salted_hash)] + ("salted_hash", salted_hash)] in let post_commits = e_big_map [((e_address first_committer), commit)] in - let post_storage = storage salted_hash post_commits in + let post_storage = storage salted_hash true post_commits in let options = Proto_alpha_utils.Memory_proto_alpha.make_options ~predecessor_timestamp From f76684031156e7a21627fc92a70bf94ae924132b Mon Sep 17 00:00:00 2001 From: John David Pressman Date: Thu, 13 Feb 2020 04:35:21 -0800 Subject: [PATCH 5/7] Fix commit test for hashlock --- src/test/contracts/hashlock.mligo | 6 ++++-- src/test/hash_lock_tests.ml | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/test/contracts/hashlock.mligo b/src/test/contracts/hashlock.mligo index 028f0baec..d158bbfd0 100644 --- a/src/test/contracts/hashlock.mligo +++ b/src/test/contracts/hashlock.mligo @@ -22,8 +22,10 @@ type parameter = (* We use hash-commit so that a baker can't steal *) let commit ((p,s): unit * storage) : operation list * storage = - let salted: bytes = Bytes.concat s.hashed (Bytes.pack sender) in - let commit: commit = {date = Current.time + 86400; salted_hash = Crypto.sha256 salted;} in + let salted : bytes = Crypto.sha256 (Bytes.concat + s.hashed + (Bytes.pack sender)) in + let commit: commit = {date = Current.time + 86400; salted_hash = salted;} in let updated_map: commit_set = Big_map.update sender (Some commit) s.commits in let s = {hashed = s.hashed; unused = s.unused; commits = updated_map} in (([]: operation list), s) diff --git a/src/test/hash_lock_tests.ml b/src/test/hash_lock_tests.ml index b82fa5af2..de83d13ca 100644 --- a/src/test/hash_lock_tests.ml +++ b/src/test/hash_lock_tests.ml @@ -56,7 +56,7 @@ let commit () = let%bind lock_time = mk_time "2000-01-02T00:10:11Z" in let test_hash_raw = sha_256_hash (Bytes.of_string "hello world") in let test_hash = e_bytes_raw test_hash_raw in - let%bind packed_sender = pack_payload program (e_bytes_string first_committer) in + let%bind packed_sender = pack_payload program (e_address first_committer) in let salted_hash = e_bytes_raw (sha_256_hash (Bytes.concat Bytes.empty [test_hash_raw; packed_sender])) @@ -72,7 +72,7 @@ let commit () = in let post_commits = e_big_map [((e_address first_committer), commit)] in - let post_storage = storage salted_hash true post_commits in + let post_storage = storage test_hash true post_commits in let options = Proto_alpha_utils.Memory_proto_alpha.make_options ~predecessor_timestamp From 5764f53ed125f1cc6ef011e794db0c616fa26675 Mon Sep 17 00:00:00 2001 From: John David Pressman Date: Thu, 13 Feb 2020 21:54:16 -0800 Subject: [PATCH 6/7] Refactor commitment mechanism, add reveal tests to hashlock contract --- src/test/contracts/hashlock.mligo | 24 ++-- src/test/hash_lock_tests.ml | 200 ++++++++++++++++++++++++++++-- 2 files changed, 207 insertions(+), 17 deletions(-) diff --git a/src/test/contracts/hashlock.mligo b/src/test/contracts/hashlock.mligo index d158bbfd0..1f6fa02a1 100644 --- a/src/test/contracts/hashlock.mligo +++ b/src/test/contracts/hashlock.mligo @@ -17,20 +17,20 @@ type reveal = { } type parameter = -| Commit of unit +| Commit of bytes | Reveal of reveal (* We use hash-commit so that a baker can't steal *) -let commit ((p,s): unit * storage) : operation list * storage = - let salted : bytes = Crypto.sha256 (Bytes.concat - s.hashed - (Bytes.pack sender)) in - let commit: commit = {date = Current.time + 86400; salted_hash = salted;} in +let commit ((p,s): bytes * storage) : operation list * storage = + let commit: commit = {date = Current.time + 86400; salted_hash = p;} in let updated_map: commit_set = Big_map.update sender (Some commit) s.commits in let s = {hashed = s.hashed; unused = s.unused; commits = updated_map} in (([]: operation list), s) let reveal ((p,s): reveal * storage) : operation list * storage = + if not s.unused + then (failwith "This contract has already been used.": operation list * storage) + else let commit: commit = match (Big_map.find_opt sender s.commits) with | Some c -> c @@ -39,14 +39,18 @@ let reveal ((p,s): reveal * storage) : operation list * storage = if Current.time < commit.date then (failwith "It hasn't been 24 hours since your commit yet.": operation list * storage) else - let salted = Bytes.concat (Crypto.sha256 p.hashable) (Bytes.pack sender) in - if ((Crypto.sha256 salted) = commit.salted_hash) && s.unused + let salted = Crypto.sha256 (Bytes.concat p.hashable (Bytes.pack sender)) in + if (salted <> commit.salted_hash) + then (failwith "This reveal doesn't match your commitment.": operation list * storage) + else + if (s.hashed = Crypto.sha256 p.hashable) then let s: storage = {hashed = s.hashed; unused = false; commits = s.commits} in ((p.message ()), s) - else (([]: operation list), s) + else (failwith "Your commitment did not match the storage hash.": + operation list * storage) let main ((p,s): parameter * storage) : operation list * storage = match p with - | Commit -> commit ((), s) + | Commit c -> commit (c, s) | Reveal r -> reveal (r, s) diff --git a/src/test/hash_lock_tests.ml b/src/test/hash_lock_tests.ml index de83d13ca..98e4ffea7 100644 --- a/src/test/hash_lock_tests.ml +++ b/src/test/hash_lock_tests.ml @@ -27,12 +27,6 @@ let compile_main () = Ligo.Compile.Of_michelson.build_contract michelson_prg in ok () -let empty_op_list = - (e_typed_list [] t_operation) -let empty_message = e_lambda (Var.of_name "arguments") - (Some t_unit) (Some (t_list t_operation)) - empty_op_list - let call msg = e_constructor "Call" msg let mk_time st = match Memory_proto_alpha.Protocol.Alpha_context.Timestamp.of_notation st with @@ -50,6 +44,13 @@ let (first_committer , first_contract) = let kt = id.implicit_contract in Protocol.Alpha_context.Contract.to_b58check kt , kt +let empty_op_list = + (e_typed_list [] t_operation) +let empty_message = e_lambda (Var.of_name "arguments") + (Some t_unit) (Some (t_list t_operation)) + empty_op_list + + let commit () = let%bind program,_ = get_program () in let%bind predecessor_timestamp = mk_time "2000-01-01T00:10:10Z" in @@ -60,6 +61,7 @@ let commit () = let salted_hash = e_bytes_raw (sha_256_hash (Bytes.concat Bytes.empty [test_hash_raw; packed_sender])) + in let pre_commits = e_typed_big_map [] t_address (t_record_ez [("date", t_timestamp); ("salted_hash", t_bytes)]) @@ -80,9 +82,193 @@ let commit () = () in expect_eq ~options program "commit" - (e_pair (e_unit ()) init_storage) (e_pair empty_op_list post_storage) + (e_pair salted_hash init_storage) (e_pair empty_op_list post_storage) + +(* Test that the contract fails if we haven't committed before revealing the answer *) +let reveal_no_commit () = + let%bind program,_ = get_program () in + let empty_message = empty_message in + let reveal = e_ez_record [("hashable", e_bytes_string "hello world"); + ("message", empty_message)] + in + let test_hash_raw = sha_256_hash (Bytes.of_string "hello world") in + let test_hash = e_bytes_raw test_hash_raw in + let pre_commits = e_typed_big_map [] t_address (t_record_ez [("date", t_timestamp); + ("salted_hash", t_bytes)]) + in + let init_storage = storage test_hash true pre_commits in + expect_string_failwith program "reveal" + (e_pair reveal init_storage) + "You haven't made a commitment to hash against yet." + +(* Test that the contract fails if our commit isn't 24 hours old yet *) +let reveal_young_commit () = + let%bind program,_ = get_program () in + let empty_message = empty_message in + let reveal = e_ez_record [("hashable", e_bytes_string "hello world"); + ("message", empty_message)] + in + let%bind predecessor_timestamp = mk_time "2000-01-01T00:10:10Z" in + let%bind lock_time = mk_time "2000-01-02T00:10:11Z" in + let test_hash_raw = sha_256_hash (Bytes.of_string "hello world") in + let test_hash = e_bytes_raw test_hash_raw in + let%bind packed_sender = pack_payload program (e_address first_committer) in + let salted_hash = e_bytes_raw (sha_256_hash + (Bytes.concat Bytes.empty [test_hash_raw; + packed_sender])) in + let commit = + e_ez_record [("date", e_timestamp + (Int64.to_int (to_sec lock_time))); + ("salted_hash", salted_hash)] + in + let commits = e_big_map [((e_address first_committer), commit)] + in + let init_storage = storage test_hash true commits in + let options = + Proto_alpha_utils.Memory_proto_alpha.make_options + ~predecessor_timestamp + ~payer:first_contract + () + in + expect_string_failwith ~options program "reveal" + (e_pair reveal init_storage) + "It hasn't been 24 hours since your commit yet." + +(* Test that the contract fails if our reveal doesn't meet our commitment *) +let reveal_breaks_commit () = + let%bind program,_ = get_program () in + let empty_message = empty_message in + let reveal = e_ez_record [("hashable", e_bytes_string "hello world"); + ("message", empty_message)] + in + let%bind predecessor_timestamp = mk_time "2000-01-01T00:10:10Z" in + let test_hash_raw = sha_256_hash (Bytes.of_string "hello world") in + let test_hash = e_bytes_raw test_hash_raw in + let%bind packed_sender = pack_payload program (e_address first_committer) in + let salted_hash = e_bytes_raw (sha_256_hash + (Bytes.concat Bytes.empty [Bytes.of_string "hello"; + packed_sender])) in + let commit = + e_ez_record [("date", e_timestamp + (Int64.to_int (to_sec predecessor_timestamp))); + ("salted_hash", salted_hash)] + in + let commits = e_big_map [((e_address first_committer), commit)] + in + let init_storage = storage test_hash true commits in + let options = + Proto_alpha_utils.Memory_proto_alpha.make_options + ~predecessor_timestamp + ~payer:first_contract + () + in + expect_string_failwith ~options program "reveal" + (e_pair reveal init_storage) + "This reveal doesn't match your commitment." + +(* Test that the contract fails if we reveal the wrong bytes for the stored hash *) +let reveal_wrong_commit () = + let%bind program,_ = get_program () in + let empty_message = empty_message in + let reveal = e_ez_record [("hashable", e_bytes_string "hello"); + ("message", empty_message)] + in + let%bind predecessor_timestamp = mk_time "2000-01-01T00:10:10Z" in + let test_hash_raw = sha_256_hash (Bytes.of_string "hello world") in + let test_hash = e_bytes_raw test_hash_raw in + let%bind packed_sender = pack_payload program (e_address first_committer) in + let salted_hash = e_bytes_raw (sha_256_hash + (Bytes.concat Bytes.empty [Bytes.of_string "hello"; + packed_sender])) in + let commit = + e_ez_record [("date", e_timestamp + (Int64.to_int (to_sec predecessor_timestamp))); + ("salted_hash", salted_hash)] + in + let commits = e_big_map [((e_address first_committer), commit)] + in + let init_storage = storage test_hash true commits in + let options = + Proto_alpha_utils.Memory_proto_alpha.make_options + ~predecessor_timestamp + ~payer:first_contract + () + in + expect_string_failwith ~options program "reveal" + (e_pair reveal init_storage) + "Your commitment did not match the storage hash." + +(* Test that the contract fails if we try to reuse it after unused flag changed *) +let reveal_no_reuse () = + let%bind program,_ = get_program () in + let empty_message = empty_message in + let reveal = e_ez_record [("hashable", e_bytes_string "hello"); + ("message", empty_message)] + in + let%bind predecessor_timestamp = mk_time "2000-01-01T00:10:10Z" in + let test_hash_raw = sha_256_hash (Bytes.of_string "hello world") in + let test_hash = e_bytes_raw test_hash_raw in + let%bind packed_sender = pack_payload program (e_address first_committer) in + let salted_hash = e_bytes_raw (sha_256_hash + (Bytes.concat Bytes.empty [Bytes.of_string "hello"; + packed_sender])) in + let commit = + e_ez_record [("date", e_timestamp + (Int64.to_int (to_sec predecessor_timestamp))); + ("salted_hash", salted_hash)] + in + let commits = e_big_map [((e_address first_committer), commit)] + in + let init_storage = storage test_hash false commits in + let options = + Proto_alpha_utils.Memory_proto_alpha.make_options + ~predecessor_timestamp + ~payer:first_contract + () + in + expect_string_failwith ~options program "reveal" + (e_pair reveal init_storage) + "This contract has already been used." + +(* Test that the contract executes successfully with valid commit-reveal *) +let reveal () = + let%bind program,_ = get_program () in + let empty_message = empty_message in + let reveal = e_ez_record [("hashable", e_bytes_string "hello world"); + ("message", empty_message)] + in + let%bind predecessor_timestamp = mk_time "2000-01-01T00:10:10Z" in + let test_hash_raw = sha_256_hash (Bytes.of_string "hello world") in + let test_hash = e_bytes_raw test_hash_raw in + let%bind packed_sender = pack_payload program (e_address first_committer) in + let salted_hash = e_bytes_raw (sha_256_hash + (Bytes.concat Bytes.empty [Bytes.of_string "hello world"; + packed_sender])) in + let commit = + e_ez_record [("date", e_timestamp + (Int64.to_int (to_sec predecessor_timestamp))); + ("salted_hash", salted_hash)] + in + let commits = e_big_map [((e_address first_committer), commit)] + in + let init_storage = storage test_hash true commits in + let post_storage = storage test_hash false commits in + let options = + Proto_alpha_utils.Memory_proto_alpha.make_options + ~predecessor_timestamp + ~payer:first_contract + () + in + expect_eq ~options program "reveal" + (e_pair reveal init_storage) (e_pair empty_op_list post_storage) let main = test_suite "Hashlock" [ test "compile" compile_main ; test "commit" commit ; + test "reveal (fail if no commitment)" reveal_no_commit ; + test "reveal (fail if commit too young)" reveal_young_commit ; + test "reveal (fail if breaks commitment)" reveal_breaks_commit ; + test "reveal (fail if wrong bytes for hash)" reveal_wrong_commit ; + test "reveal (fail if attempt to reuse)" reveal_no_reuse ; + test "reveal" reveal ; ] From eaa0c12d6371f3427b8092a4b77cdb588156cf41 Mon Sep 17 00:00:00 2001 From: John David Pressman Date: Thu, 13 Feb 2020 22:00:25 -0800 Subject: [PATCH 7/7] e_ez_record -> e_record_ez in hashlock contract tests --- src/test/hash_lock_tests.ml | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/test/hash_lock_tests.ml b/src/test/hash_lock_tests.ml index 98e4ffea7..bff1e8219 100644 --- a/src/test/hash_lock_tests.ml +++ b/src/test/hash_lock_tests.ml @@ -34,7 +34,7 @@ let mk_time st = | None -> simple_fail "bad timestamp notation" let to_sec t = Tezos_utils.Time.Protocol.to_seconds t let storage hashed used commits = - e_ez_record [("hashed", hashed); + e_record_ez [("hashed", hashed); ("unused", e_bool used); ("commits", commits)] @@ -68,7 +68,7 @@ let commit () = in let init_storage = storage test_hash true pre_commits in let commit = - e_ez_record [("date", e_timestamp + e_record_ez [("date", e_timestamp (Int64.to_int (to_sec lock_time))); ("salted_hash", salted_hash)] in @@ -88,7 +88,7 @@ let commit () = let reveal_no_commit () = let%bind program,_ = get_program () in let empty_message = empty_message in - let reveal = e_ez_record [("hashable", e_bytes_string "hello world"); + let reveal = e_record_ez [("hashable", e_bytes_string "hello world"); ("message", empty_message)] in let test_hash_raw = sha_256_hash (Bytes.of_string "hello world") in @@ -105,7 +105,7 @@ let reveal_no_commit () = let reveal_young_commit () = let%bind program,_ = get_program () in let empty_message = empty_message in - let reveal = e_ez_record [("hashable", e_bytes_string "hello world"); + let reveal = e_record_ez [("hashable", e_bytes_string "hello world"); ("message", empty_message)] in let%bind predecessor_timestamp = mk_time "2000-01-01T00:10:10Z" in @@ -117,7 +117,7 @@ let reveal_young_commit () = (Bytes.concat Bytes.empty [test_hash_raw; packed_sender])) in let commit = - e_ez_record [("date", e_timestamp + e_record_ez [("date", e_timestamp (Int64.to_int (to_sec lock_time))); ("salted_hash", salted_hash)] in @@ -138,7 +138,7 @@ let reveal_young_commit () = let reveal_breaks_commit () = let%bind program,_ = get_program () in let empty_message = empty_message in - let reveal = e_ez_record [("hashable", e_bytes_string "hello world"); + let reveal = e_record_ez [("hashable", e_bytes_string "hello world"); ("message", empty_message)] in let%bind predecessor_timestamp = mk_time "2000-01-01T00:10:10Z" in @@ -149,7 +149,7 @@ let reveal_breaks_commit () = (Bytes.concat Bytes.empty [Bytes.of_string "hello"; packed_sender])) in let commit = - e_ez_record [("date", e_timestamp + e_record_ez [("date", e_timestamp (Int64.to_int (to_sec predecessor_timestamp))); ("salted_hash", salted_hash)] in @@ -170,7 +170,7 @@ let reveal_breaks_commit () = let reveal_wrong_commit () = let%bind program,_ = get_program () in let empty_message = empty_message in - let reveal = e_ez_record [("hashable", e_bytes_string "hello"); + let reveal = e_record_ez [("hashable", e_bytes_string "hello"); ("message", empty_message)] in let%bind predecessor_timestamp = mk_time "2000-01-01T00:10:10Z" in @@ -181,7 +181,7 @@ let reveal_wrong_commit () = (Bytes.concat Bytes.empty [Bytes.of_string "hello"; packed_sender])) in let commit = - e_ez_record [("date", e_timestamp + e_record_ez [("date", e_timestamp (Int64.to_int (to_sec predecessor_timestamp))); ("salted_hash", salted_hash)] in @@ -202,7 +202,7 @@ let reveal_wrong_commit () = let reveal_no_reuse () = let%bind program,_ = get_program () in let empty_message = empty_message in - let reveal = e_ez_record [("hashable", e_bytes_string "hello"); + let reveal = e_record_ez [("hashable", e_bytes_string "hello"); ("message", empty_message)] in let%bind predecessor_timestamp = mk_time "2000-01-01T00:10:10Z" in @@ -213,7 +213,7 @@ let reveal_no_reuse () = (Bytes.concat Bytes.empty [Bytes.of_string "hello"; packed_sender])) in let commit = - e_ez_record [("date", e_timestamp + e_record_ez [("date", e_timestamp (Int64.to_int (to_sec predecessor_timestamp))); ("salted_hash", salted_hash)] in @@ -234,7 +234,7 @@ let reveal_no_reuse () = let reveal () = let%bind program,_ = get_program () in let empty_message = empty_message in - let reveal = e_ez_record [("hashable", e_bytes_string "hello world"); + let reveal = e_record_ez [("hashable", e_bytes_string "hello world"); ("message", empty_message)] in let%bind predecessor_timestamp = mk_time "2000-01-01T00:10:10Z" in @@ -245,7 +245,7 @@ let reveal () = (Bytes.concat Bytes.empty [Bytes.of_string "hello world"; packed_sender])) in let commit = - e_ez_record [("date", e_timestamp + e_record_ez [("date", e_timestamp (Int64.to_int (to_sec predecessor_timestamp))); ("salted_hash", salted_hash)] in