From 7d3ef498120d73320a0d567096a443c7cd90da36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Proust?= Date: Thu, 8 Jun 2023 17:13:08 +0200 Subject: [PATCH 1/4] Proto: default-staking parameters for when the value is unset --- .../lib_protocol/delegate_staking_parameters.ml | 10 +++++++++- .../lib_protocol/staking_parameters_repr.ml | 2 ++ .../lib_protocol/staking_parameters_repr.mli | 3 +++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/proto_alpha/lib_protocol/delegate_staking_parameters.ml b/src/proto_alpha/lib_protocol/delegate_staking_parameters.ml index 7982a66ac465..2a933b5814dd 100644 --- a/src/proto_alpha/lib_protocol/delegate_staking_parameters.ml +++ b/src/proto_alpha/lib_protocol/delegate_staking_parameters.ml @@ -24,7 +24,15 @@ (*****************************************************************************) let of_delegate ctxt delegate = - Storage.Contract.Staking_parameters.get ctxt (Contract_repr.Implicit delegate) + let open Lwt_result_syntax in + let* t = + Storage.Contract.Staking_parameters.find + ctxt + (Contract_repr.Implicit delegate) + in + match t with + | None -> return Staking_parameters_repr.default + | Some t -> return t let raw_pending_updates ctxt delegate = Storage.Contract.Pending_staking_parameters.bindings diff --git a/src/proto_alpha/lib_protocol/staking_parameters_repr.ml b/src/proto_alpha/lib_protocol/staking_parameters_repr.ml index 5393f26bfb9b..e2080c48283f 100644 --- a/src/proto_alpha/lib_protocol/staking_parameters_repr.ml +++ b/src/proto_alpha/lib_protocol/staking_parameters_repr.ml @@ -32,6 +32,8 @@ let maximum_baking_over_staking_edge = (* max is 1 expressed in portions of 1_000_000_000 *) 1_000_000_000l +let default = {staking_over_baking_limit = 0l; baking_over_staking_edge = 0l} + type error += Invalid_staking_parameters let () = diff --git a/src/proto_alpha/lib_protocol/staking_parameters_repr.mli b/src/proto_alpha/lib_protocol/staking_parameters_repr.mli index 1e371c973c08..1c47f99de799 100644 --- a/src/proto_alpha/lib_protocol/staking_parameters_repr.mli +++ b/src/proto_alpha/lib_protocol/staking_parameters_repr.mli @@ -28,6 +28,9 @@ type t = private { baking_over_staking_edge : int32; (* as a portion of 1_000_000_000 *) } +(** Value used when unset *) +val default : t + type error += Invalid_staking_parameters val make : -- GitLab From 2b4614909648401b60d1be674e396b7928905ae7 Mon Sep 17 00:00:00 2001 From: Lucas Randazzo Date: Thu, 8 Jun 2023 18:09:54 +0200 Subject: [PATCH 2/4] Proto/AI: access delegate parameters in alpha context --- src/proto_alpha/lib_protocol/alpha_context.mli | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/proto_alpha/lib_protocol/alpha_context.mli b/src/proto_alpha/lib_protocol/alpha_context.mli index 522186053bb0..6fcfd1635a1c 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/alpha_context.mli @@ -2266,6 +2266,11 @@ module Delegate : sig Signature.Public_key_hash.t -> Staking_parameters_repr.t -> context tzresult Lwt.t + + val of_delegate : + context -> + Signature.Public_key_hash.t -> + Staking_parameters_repr.t tzresult Lwt.t end end -- GitLab From b440bc921873ae3fd7faf3f08e5e83620da82f3d Mon Sep 17 00:00:00 2001 From: Lucas Randazzo Date: Thu, 8 Jun 2023 18:10:26 +0200 Subject: [PATCH 3/4] Proto/Apply: add error for staking when delegate refuses costaking --- src/proto_alpha/lib_protocol/apply.ml | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/proto_alpha/lib_protocol/apply.ml b/src/proto_alpha/lib_protocol/apply.ml index b94378349bdd..f9bdf675710e 100644 --- a/src/proto_alpha/lib_protocol/apply.ml +++ b/src/proto_alpha/lib_protocol/apply.ml @@ -44,6 +44,7 @@ type error += | Invalid_sender of Destination.t | Invalid_self_transaction_destination | Staking_for_nondelegate_while_costaking_disabled + | Staking_to_delegate_that_refuses_costaking | Invalid_nonzero_transaction_amount of Tez.t | Invalid_unstake_request_amount of {requested_amount : Z.t} | Invalid_staking_parameters_sender @@ -239,6 +240,23 @@ let () = (function | Staking_for_nondelegate_while_costaking_disabled -> Some () | _ -> None) (fun () -> Staking_for_nondelegate_while_costaking_disabled) ; + let staking_to_delegate_that_refuses_costaking_description = + "The delegate currently does not accept staking operations from sources \ + other than itself: its `staking_over_baking_limit` parameter is set to 0." + in + register_error_kind + `Permanent + ~id:"operations.staking_to_delegate_that_refuses_costaking" + ~title:"Staking to delegate that does not accept external staking" + ~description:staking_to_delegate_that_refuses_costaking_description + ~pp:(fun ppf () -> + Format.pp_print_string + ppf + staking_to_delegate_that_refuses_costaking_description) + Data_encoding.unit + (function + | Staking_to_delegate_that_refuses_costaking -> Some () | _ -> None) + (fun () -> Staking_to_delegate_that_refuses_costaking) ; register_error_kind `Permanent ~id:"operations.invalid_nonzero_transaction_amount" -- GitLab From 5fcac50b262b31aa1b24ce14672db348df41408a Mon Sep 17 00:00:00 2001 From: Lucas Randazzo Date: Thu, 8 Jun 2023 18:10:56 +0200 Subject: [PATCH 4/4] Proto/Apply: raise error on staking when delegate refuses costaking --- src/proto_alpha/lib_protocol/apply.ml | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/proto_alpha/lib_protocol/apply.ml b/src/proto_alpha/lib_protocol/apply.ml index f9bdf675710e..6224b6fd9893 100644 --- a/src/proto_alpha/lib_protocol/apply.ml +++ b/src/proto_alpha/lib_protocol/apply.ml @@ -371,6 +371,16 @@ let apply_stake ~ctxt ~sender ~amount ~destination ~before_operation = let*? () = error_unless allowed Staking_for_nondelegate_while_costaking_disabled in + let* {staking_over_baking_limit; _} = + Delegate.Staking_parameters.of_delegate ctxt delegate + in + let forbidden = + Signature.Public_key_hash.(delegate <> sender) + && Compare.Int32.(staking_over_baking_limit = 0l) + in + let*? () = + error_when forbidden Staking_to_delegate_that_refuses_costaking + in let* ctxt, balance_updates = Staking.stake ctxt ~sender ~delegate amount in -- GitLab