From 3ccf6b5ec277a212d9183a483972569afb417db8 Mon Sep 17 00:00:00 2001 From: "iguerNL@Functori" Date: Thu, 14 Jul 2022 13:29:16 +0200 Subject: [PATCH] Proto/Scoru: add details when attempting to cement a too recent commit --- .../lib_protocol/sc_rollup_errors.ml | 31 +++++++++++++++---- .../lib_protocol/sc_rollup_stake_storage.ml | 9 ++++-- .../lib_protocol/sc_rollup_stake_storage.mli | 2 +- .../integration/operations/test_sc_rollup.ml | 7 +++-- .../test/unit/test_sc_rollup_storage.ml | 10 ++++-- 5 files changed, 44 insertions(+), 15 deletions(-) diff --git a/src/proto_alpha/lib_protocol/sc_rollup_errors.ml b/src/proto_alpha/lib_protocol/sc_rollup_errors.ml index 0e66e086f536..af122706389f 100644 --- a/src/proto_alpha/lib_protocol/sc_rollup_errors.ml +++ b/src/proto_alpha/lib_protocol/sc_rollup_errors.ml @@ -35,7 +35,11 @@ type error += | (* `Temporary *) Sc_rollup_remove_lcc | (* `Temporary *) Sc_rollup_staker_backtracked | (* `Temporary *) Sc_rollup_too_far_ahead - | (* `Temporary *) Sc_rollup_too_recent + | (* `Temporary *) + Sc_rollup_commitment_too_recent of { + current_level : Raw_level_repr.t; + min_level : Raw_level_repr.t; + } | (* `Temporary *) Sc_rollup_unknown_commitment of Sc_rollup_commitment_repr.Hash.t @@ -303,13 +307,28 @@ let () = in register_error_kind `Temporary - ~id:"Sc_rollup_too_recent" + ~id:"Sc_rollup_commitment_too_recent" ~title:"Commitment too recent" ~description - ~pp:(fun ppf () -> Format.fprintf ppf "%s" description) - Data_encoding.empty - (function Sc_rollup_too_recent -> Some () | _ -> None) - (fun () -> Sc_rollup_too_recent) ; + ~pp:(fun ppf (current_level, min_level) -> + Format.fprintf + ppf + "%s@ Current level: %a,@ minimal level: %a" + description + Raw_level_repr.pp + current_level + Raw_level_repr.pp + min_level) + Data_encoding.( + obj2 + (req "current_level" Raw_level_repr.encoding) + (req "min_level" Raw_level_repr.encoding)) + (function + | Sc_rollup_commitment_too_recent {current_level; min_level} -> + Some (current_level, min_level) + | _ -> None) + (fun (current_level, min_level) -> + Sc_rollup_commitment_too_recent {current_level; min_level}) ; let description = "Unknown commitment." in register_error_kind `Temporary diff --git a/src/proto_alpha/lib_protocol/sc_rollup_stake_storage.ml b/src/proto_alpha/lib_protocol/sc_rollup_stake_storage.ml index 959c564df0ad..0c24a69dc6c8 100644 --- a/src/proto_alpha/lib_protocol/sc_rollup_stake_storage.ml +++ b/src/proto_alpha/lib_protocol/sc_rollup_stake_storage.ml @@ -355,10 +355,13 @@ let cement_commitment ctxt rollup new_lcc = Store.Commitment_added.get (ctxt, rollup) new_lcc in let* () = + let current_level = (Raw_context.current_level ctxt).level in + let min_level = + Raw_level_repr.add new_lcc_added refutation_deadline_blocks + in fail_when - (let level = (Raw_context.current_level ctxt).level in - Raw_level_repr.(level < add new_lcc_added refutation_deadline_blocks)) - Sc_rollup_too_recent + Raw_level_repr.(current_level < min_level) + (Sc_rollup_commitment_too_recent {current_level; min_level}) in (* update LCC *) let* ctxt, lcc_size_diff = diff --git a/src/proto_alpha/lib_protocol/sc_rollup_stake_storage.mli b/src/proto_alpha/lib_protocol/sc_rollup_stake_storage.mli index b5481e05849a..172e1d9155b9 100644 --- a/src/proto_alpha/lib_protocol/sc_rollup_stake_storage.mli +++ b/src/proto_alpha/lib_protocol/sc_rollup_stake_storage.mli @@ -103,7 +103,7 @@ val publish_commitment : {li [Sc_rollup_does_not_exist] if [rollup] does not exist} {li [Sc_rollup_unknown_commitment] if [commitment] does not exist} {li [Sc_rollup_parent_not_lcc] if [commitment] is not the child of the last cemented commitment} - {li [Sc_rollup_too_recent] if [commitment] has not passed its deadline} + {li [Sc_rollup_commitment_too_recent] if [commitment] has not passed its deadline} {li [Sc_rollup_no_stakers] if there are zero stakers} {li [Sc_rollup_disputed] if at least one staker is not staked on [commitment]} } *) diff --git a/src/proto_alpha/lib_protocol/test/integration/operations/test_sc_rollup.ml b/src/proto_alpha/lib_protocol/test/integration/operations/test_sc_rollup.ml index e6b0457ae9ac..b3dbf2c441ab 100644 --- a/src/proto_alpha/lib_protocol/test/integration/operations/test_sc_rollup.ml +++ b/src/proto_alpha/lib_protocol/test/integration/operations/test_sc_rollup.ml @@ -694,11 +694,14 @@ let test_challenge_window_period_boundaries () = challenge window period. *) let* () = let expect_apply_failure = function - | Environment.Ecoproto_error (Sc_rollup_errors.Sc_rollup_too_recent as e) + | Environment.Ecoproto_error + (Sc_rollup_errors.Sc_rollup_commitment_too_recent _ as e) :: _ -> Assert.test_error_encodings e ; return_unit - | _ -> failwith "It should have failed with [Sc_rollup_too_recent]" + | _ -> + failwith + "It should have failed with [Sc_rollup_commitment_too_recent]" in commit_and_cement_after_n_bloc ~expect_apply_failure diff --git a/src/proto_alpha/lib_protocol/test/unit/test_sc_rollup_storage.ml b/src/proto_alpha/lib_protocol/test/unit/test_sc_rollup_storage.ml index cd6c41e770d8..e03b309adb36 100644 --- a/src/proto_alpha/lib_protocol/test/unit/test_sc_rollup_storage.ml +++ b/src/proto_alpha/lib_protocol/test/unit/test_sc_rollup_storage.ml @@ -930,7 +930,7 @@ let test_cement_fail_too_recent () = compressed_state = Sc_rollup_repr.State_hash.zero; } in - let* c1, _level, ctxt = + let* c1, level, ctxt = lift @@ Sc_rollup_stake_storage.Internal_for_tests.refine_stake ctxt @@ -938,20 +938,24 @@ let test_cement_fail_too_recent () = staker commitment in + let min_cementation_level = Raw_level_repr.add level challenge_window in let* () = assert_fails_with ~loc:__LOC__ (Sc_rollup_stake_storage.cement_commitment ctxt rollup c1) - Sc_rollup_errors.Sc_rollup_too_recent + (Sc_rollup_errors.Sc_rollup_commitment_too_recent + {current_level = level; min_level = min_cementation_level}) in let ctxt = Raw_context.Internal_for_tests.add_level ctxt (challenge_window - 1) in + let level = (Raw_context.current_level ctxt).level in let* () = assert_fails_with ~loc:__LOC__ (Sc_rollup_stake_storage.cement_commitment ctxt rollup c1) - Sc_rollup_errors.Sc_rollup_too_recent + (Sc_rollup_errors.Sc_rollup_commitment_too_recent + {current_level = level; min_level = min_cementation_level}) in assert_true ctxt -- GitLab