Refactor commitment mechanism, add reveal tests to hashlock contract

This commit is contained in:
John David Pressman 2020-02-13 21:54:16 -08:00
parent f766840311
commit 5764f53ed1
2 changed files with 207 additions and 17 deletions

View File

@ -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)

View File

@ -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 ;
]