From fe2fa1433b07689e9edc2afa4e0e435852b0e15e Mon Sep 17 00:00:00 2001 From: Julien Tesson Date: Mon, 9 Oct 2023 19:25:16 +0200 Subject: [PATCH 01/26] Revert "Proto: remove operation Set_deposits_limit" This reverts commit 88f0099b96f47ee9d689253be9f4b9baac7bae91. --- .../lib_protocol/alpha_context.mli | 16 +++++ src/proto_alpha/lib_protocol/apply.ml | 32 +++++++++- src/proto_alpha/lib_protocol/apply_results.ml | 62 +++++++++++++++++++ .../lib_protocol/apply_results.mli | 4 ++ .../lib_protocol/delegate_services.ml | 59 ++++++++++++------ .../lib_protocol/delegate_services.mli | 7 +++ .../lib_protocol/delegate_storage.ml | 11 ++++ .../lib_protocol/delegate_storage.mli | 12 ++++ .../lib_protocol/operation_repr.ml | 26 +++++++- .../lib_protocol/operation_repr.mli | 13 ++++ src/proto_alpha/lib_protocol/validate.ml | 3 +- 11 files changed, 223 insertions(+), 22 deletions(-) diff --git a/src/proto_alpha/lib_protocol/alpha_context.mli b/src/proto_alpha/lib_protocol/alpha_context.mli index 9da78e2cb21a..c3dcb3429971 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/alpha_context.mli @@ -2177,6 +2177,12 @@ end module Delegate : sig val check_not_tz4 : Signature.public_key_hash -> unit tzresult + val frozen_deposits_limit : + context -> public_key_hash -> Tez.t option tzresult Lwt.t + + val set_frozen_deposits_limit : + context -> public_key_hash -> Tez.t option -> context Lwt.t + val fold : context -> order:[`Sorted | `Undefined] -> @@ -4297,6 +4303,8 @@ module Kind : sig type event = Event_kind + type set_deposits_limit = Set_deposits_limit_kind + type increase_paid_storage = Increase_paid_storage_kind type update_consensus_key = Update_consensus_key_kind @@ -4341,6 +4349,7 @@ module Kind : sig | Delegation_manager_kind : delegation manager | Event_manager_kind : event manager | Register_global_constant_manager_kind : register_global_constant manager + | Set_deposits_limit_manager_kind : set_deposits_limit manager | Increase_paid_storage_manager_kind : increase_paid_storage manager | Update_consensus_key_manager_kind : update_consensus_key manager | Transfer_ticket_manager_kind : transfer_ticket manager @@ -4478,6 +4487,9 @@ and _ manager_operation = value : Script.lazy_expr; } -> Kind.register_global_constant manager_operation + | Set_deposits_limit : + Tez.t option + -> Kind.set_deposits_limit manager_operation | Increase_paid_storage : { amount_in_bytes : Z.t; destination : Contract_hash.t; @@ -4728,6 +4740,8 @@ module Operation : sig val register_global_constant_case : Kind.register_global_constant Kind.manager case + val set_deposits_limit_case : Kind.set_deposits_limit Kind.manager case + val increase_paid_storage_case : Kind.increase_paid_storage Kind.manager case @@ -4783,6 +4797,8 @@ module Operation : sig val register_global_constant_case : Kind.register_global_constant case + val set_deposits_limit_case : Kind.set_deposits_limit case + val increase_paid_storage_case : Kind.increase_paid_storage case val transfer_ticket_case : Kind.transfer_ticket case diff --git a/src/proto_alpha/lib_protocol/apply.ml b/src/proto_alpha/lib_protocol/apply.ml index a8c9e44156be..db088b6ea89e 100644 --- a/src/proto_alpha/lib_protocol/apply.ml +++ b/src/proto_alpha/lib_protocol/apply.ml @@ -31,6 +31,7 @@ open Alpha_context type error += | Faulty_validation_wrong_slot + | Set_deposits_limit_on_unregistered_delegate of Signature.Public_key_hash.t | Error_while_taking_fees | Update_consensus_key_on_unregistered_delegate of Signature.Public_key_hash.t | Empty_transaction of Contract.t @@ -62,6 +63,21 @@ let () = Data_encoding.empty (function Faulty_validation_wrong_slot -> Some () | _ -> None) (fun () -> Faulty_validation_wrong_slot) ; + register_error_kind + `Temporary + ~id:"operation.set_deposits_limit_on_unregistered_delegate" + ~title:"Set deposits limit on an unregistered delegate" + ~description:"Cannot set deposits limit on an unregistered delegate." + ~pp:(fun ppf c -> + Format.fprintf + ppf + "Cannot set a deposits limit on the unregistered delegate %a." + Signature.Public_key_hash.pp + c) + Data_encoding.(obj1 (req "delegate" Signature.Public_key_hash.encoding)) + (function + | Set_deposits_limit_on_unregistered_delegate c -> Some c | _ -> None) + (fun c -> Set_deposits_limit_on_unregistered_delegate c) ; let error_while_taking_fees_description = "There was an error while taking the fees, which should not happen and \ @@ -1326,6 +1342,19 @@ let apply_manager_operation : } in return (ctxt, result, []) + | Set_deposits_limit limit -> + let*! is_registered = Delegate.registered ctxt source in + let*? () = + error_unless + is_registered + (Set_deposits_limit_on_unregistered_delegate source) + in + let*! ctxt = Delegate.set_frozen_deposits_limit ctxt source limit in + return + ( ctxt, + Set_deposits_limit_result + {consumed_gas = Gas.consumed ~since:ctxt_before_op ~until:ctxt}, + [] ) | Increase_paid_storage {amount_in_bytes; destination} -> let* ctxt = Contract.increase_paid_storage ctxt destination ~amount_in_bytes @@ -1691,7 +1720,8 @@ let burn_manager_storage_fees : size_of_constant = payload.size_of_constant; global_address = payload.global_address; } ) - | Update_consensus_key_result _ -> return (ctxt, storage_limit, smopr) + | Set_deposits_limit_result _ | Update_consensus_key_result _ -> + return (ctxt, storage_limit, smopr) | Increase_paid_storage_result _ -> return (ctxt, storage_limit, smopr) | Transfer_ticket_result payload -> let consumed = payload.paid_storage_size_diff in diff --git a/src/proto_alpha/lib_protocol/apply_results.ml b/src/proto_alpha/lib_protocol/apply_results.ml index 4c43948711b7..556c7fe66d51 100644 --- a/src/proto_alpha/lib_protocol/apply_results.ml +++ b/src/proto_alpha/lib_protocol/apply_results.ml @@ -58,6 +58,10 @@ type _ successful_manager_operation_result = global_address : Script_expr_hash.t; } -> Kind.register_global_constant successful_manager_operation_result + | Set_deposits_limit_result : { + consumed_gas : Gas.Arith.fp; + } + -> Kind.set_deposits_limit successful_manager_operation_result | Increase_paid_storage_result : { balance_updates : Receipt.balance_updates; consumed_gas : Gas.Arith.fp; @@ -485,6 +489,21 @@ module Manager_result = struct | Update_consensus_key_result {consumed_gas} -> consumed_gas) ~inj:(fun consumed_gas -> Update_consensus_key_result {consumed_gas}) + let set_deposits_limit_case = + make + ~op_case:Operation.Encoding.Manager_operations.set_deposits_limit_case + ~encoding: + Data_encoding.( + obj1 (dft "consumed_milligas" Gas.Arith.n_fp_encoding Gas.Arith.zero)) + ~select:(function + | Successful_manager_result (Set_deposits_limit_result _ as op) -> + Some op + | _ -> None) + ~kind:Kind.Set_deposits_limit_manager_kind + ~proj:(function + | Set_deposits_limit_result {consumed_gas} -> consumed_gas) + ~inj:(fun consumed_gas -> Set_deposits_limit_result {consumed_gas}) + let increase_paid_storage_case = make ~op_case:Operation.Encoding.Manager_operations.increase_paid_storage_case @@ -886,6 +905,7 @@ let successful_manager_operation_result_encoding : make Manager_result.origination_case; make Manager_result.delegation_case; make Manager_result.update_consensus_key_case; + make Manager_result.set_deposits_limit_case; make Manager_result.increase_paid_storage_case; make Manager_result.sc_rollup_originate_case; ] @@ -973,6 +993,10 @@ let equal_manager_kind : | Kind.Event_manager_kind, Kind.Event_manager_kind -> Some Eq | Kind.Event_manager_kind, _ -> None | Kind.Register_global_constant_manager_kind, _ -> None + | Kind.Set_deposits_limit_manager_kind, Kind.Set_deposits_limit_manager_kind + -> + Some Eq + | Kind.Set_deposits_limit_manager_kind, _ -> None | ( Kind.Increase_paid_storage_manager_kind, Kind.Increase_paid_storage_manager_kind ) -> Some Eq @@ -1576,6 +1600,17 @@ module Encoding = struct Some (op, res) | _ -> None) + let set_deposits_limit_case = + make_manager_case + Operation.Encoding.set_deposits_limit_case + Manager_result.set_deposits_limit_case + (function + | Contents_and_result + ( (Manager_operation {operation = Set_deposits_limit _; _} as op), + res ) -> + Some (op, res) + | _ -> None) + let increase_paid_storage_case = make_manager_case Operation.Encoding.increase_paid_storage_case @@ -1749,6 +1784,7 @@ let common_cases = origination_case; delegation_case; register_global_constant_case; + set_deposits_limit_case; increase_paid_storage_case; update_consensus_key_case; transfer_ticket_case; @@ -2152,6 +2188,32 @@ let kind_equal : } ) -> Some Eq | Manager_operation {operation = Register_global_constant _; _}, _ -> None + | ( Manager_operation {operation = Set_deposits_limit _; _}, + Manager_operation_result + {operation_result = Applied (Set_deposits_limit_result _); _} ) -> + Some Eq + | ( Manager_operation {operation = Set_deposits_limit _; _}, + Manager_operation_result + {operation_result = Backtracked (Set_deposits_limit_result _, _); _} ) + -> + Some Eq + | ( Manager_operation {operation = Set_deposits_limit _; _}, + Manager_operation_result + { + operation_result = + Failed (Alpha_context.Kind.Set_deposits_limit_manager_kind, _); + _; + } ) -> + Some Eq + | ( Manager_operation {operation = Set_deposits_limit _; _}, + Manager_operation_result + { + operation_result = + Skipped Alpha_context.Kind.Set_deposits_limit_manager_kind; + _; + } ) -> + Some Eq + | Manager_operation {operation = Set_deposits_limit _; _}, _ -> None | ( Manager_operation {operation = Increase_paid_storage _; _}, Manager_operation_result {operation_result = Applied (Increase_paid_storage_result _); _} ) -> diff --git a/src/proto_alpha/lib_protocol/apply_results.mli b/src/proto_alpha/lib_protocol/apply_results.mli index 1f8edf1eb8e9..4ac991e30ae1 100644 --- a/src/proto_alpha/lib_protocol/apply_results.mli +++ b/src/proto_alpha/lib_protocol/apply_results.mli @@ -161,6 +161,10 @@ and _ successful_manager_operation_result = global_address : Script_expr_hash.t; } -> Kind.register_global_constant successful_manager_operation_result + | Set_deposits_limit_result : { + consumed_gas : Gas.Arith.fp; + } + -> Kind.set_deposits_limit successful_manager_operation_result | Increase_paid_storage_result : { balance_updates : Receipt.balance_updates; consumed_gas : Gas.Arith.fp; diff --git a/src/proto_alpha/lib_protocol/delegate_services.ml b/src/proto_alpha/lib_protocol/delegate_services.ml index 474e6ad84a85..c1dd5c5e1d94 100644 --- a/src/proto_alpha/lib_protocol/delegate_services.ml +++ b/src/proto_alpha/lib_protocol/delegate_services.ml @@ -113,6 +113,7 @@ type info = { current_frozen_deposits : Tez.t; frozen_deposits : Tez.t; staking_balance : Tez.t; + frozen_deposits_limit : Tez.t option; delegated_contracts : Contract.t list; delegated_balance : Tez.t; total_delegated_stake : Tez.t; @@ -132,6 +133,7 @@ let info_encoding = current_frozen_deposits; frozen_deposits; staking_balance; + frozen_deposits_limit; delegated_contracts; delegated_balance; total_delegated_stake; @@ -146,29 +148,30 @@ let info_encoding = current_frozen_deposits, frozen_deposits, staking_balance, + frozen_deposits_limit, delegated_contracts, delegated_balance, - total_delegated_stake, - staking_denominator, deactivated, grace_period ), - (voting_info, (active_consensus_key, pending_consensus_keys)) )) + ( (total_delegated_stake, staking_denominator), + (voting_info, (active_consensus_key, pending_consensus_keys)) ) )) (fun ( ( full_balance, current_frozen_deposits, frozen_deposits, staking_balance, + frozen_deposits_limit, delegated_contracts, delegated_balance, - total_delegated_stake, - staking_denominator, deactivated, grace_period ), - (voting_info, (active_consensus_key, pending_consensus_keys)) ) -> + ( (total_delegated_stake, staking_denominator), + (voting_info, (active_consensus_key, pending_consensus_keys)) ) ) -> { full_balance; current_frozen_deposits; frozen_deposits; staking_balance; + frozen_deposits_limit; delegated_contracts; delegated_balance; total_delegated_stake; @@ -180,28 +183,31 @@ let info_encoding = pending_consensus_keys; }) (merge_objs - (obj10 + (obj9 (req "full_balance" Tez.encoding) (req "current_frozen_deposits" Tez.encoding) (req "frozen_deposits" Tez.encoding) (req "staking_balance" Tez.encoding) + (opt "frozen_deposits_limit" Tez.encoding) (req "delegated_contracts" (list Contract.encoding)) (req "delegated_balance" Tez.encoding) - (req "total_delegated_stake" Tez.encoding) - (req "staking_denominator" Staking_pseudotoken.For_RPC.encoding) (req "deactivated" bool) (req "grace_period" Cycle.encoding)) (merge_objs - Vote.delegate_info_encoding (obj2 - (req "active_consensus_key" Signature.Public_key_hash.encoding) - (dft - "pending_consensus_keys" - (list - (obj2 - (req "cycle" Cycle.encoding) - (req "pkh" Signature.Public_key_hash.encoding))) - [])))) + (req "total_delegated_stake" Tez.encoding) + (req "staking_denominator" Staking_pseudotoken.For_RPC.encoding)) + (merge_objs + Vote.delegate_info_encoding + (obj2 + (req "active_consensus_key" Signature.Public_key_hash.encoding) + (dft + "pending_consensus_keys" + (list + (obj2 + (req "cycle" Cycle.encoding) + (req "pkh" Signature.Public_key_hash.encoding))) + []))))) let participation_info_encoding = let open Data_encoding in @@ -355,6 +361,15 @@ module S = struct ~output:Tez.encoding RPC_path.(path / "staking_balance") + let frozen_deposits_limit = + RPC_service.get_service + ~description: + "Returns the frozen deposits limit for the given delegate or none if \ + no limit is set." + ~query:RPC_query.empty + ~output:(Data_encoding.option Tez.encoding) + RPC_path.(path / "frozen_deposits_limit") + let delegated_contracts = RPC_service.get_service ~description: @@ -553,6 +568,7 @@ let register () = in let* frozen_deposits = Delegate.initial_frozen_deposits ctxt pkh in let* staking_balance = Delegate.For_RPC.staking_balance ctxt pkh in + let* frozen_deposits_limit = Delegate.frozen_deposits_limit ctxt pkh in let*! delegated_contracts = Delegate.delegated_contracts ctxt pkh in let* delegated_balance = Delegate.For_RPC.delegated_balance ctxt pkh in let* total_delegated_stake = @@ -578,6 +594,7 @@ let register () = current_frozen_deposits; frozen_deposits; staking_balance; + frozen_deposits_limit; delegated_contracts; delegated_balance; total_delegated_stake; @@ -632,6 +649,9 @@ let register () = register1 ~chunked:false S.staking_balance (fun ctxt pkh () () -> let* () = check_delegate_registered ctxt pkh in Delegate.For_RPC.staking_balance ctxt pkh) ; + register1 ~chunked:false S.frozen_deposits_limit (fun ctxt pkh () () -> + let* () = check_delegate_registered ctxt pkh in + Delegate.frozen_deposits_limit ctxt pkh) ; register1 ~chunked:true S.delegated_contracts (fun ctxt pkh () () -> let* () = check_delegate_registered ctxt pkh in let*! contracts = Delegate.delegated_contracts ctxt pkh in @@ -717,6 +737,9 @@ let unstaked_frozen_deposits ctxt block pkh = let staking_balance ctxt block pkh = RPC_context.make_call1 S.staking_balance ctxt block pkh () () +let frozen_deposits_limit ctxt block pkh = + RPC_context.make_call1 S.frozen_deposits_limit ctxt block pkh () () + let delegated_contracts ctxt block pkh = RPC_context.make_call1 S.delegated_contracts ctxt block pkh () () diff --git a/src/proto_alpha/lib_protocol/delegate_services.mli b/src/proto_alpha/lib_protocol/delegate_services.mli index 2bd1d6e2f14e..5a9daa6f7e28 100644 --- a/src/proto_alpha/lib_protocol/delegate_services.mli +++ b/src/proto_alpha/lib_protocol/delegate_services.mli @@ -58,6 +58,7 @@ type info = { current_frozen_deposits : Tez.t; frozen_deposits : Tez.t; staking_balance : Tez.t; + frozen_deposits_limit : Tez.t option; delegated_contracts : Contract.t list; delegated_balance : Tez.t; total_delegated_stake : Tez.t; @@ -111,6 +112,12 @@ val staking_balance : Signature.Public_key_hash.t -> Tez.t shell_tzresult Lwt.t +val frozen_deposits_limit : + 'a #RPC_context.simple -> + 'a -> + Signature.Public_key_hash.t -> + Tez.t option shell_tzresult Lwt.t + val delegated_contracts : 'a #RPC_context.simple -> 'a -> diff --git a/src/proto_alpha/lib_protocol/delegate_storage.ml b/src/proto_alpha/lib_protocol/delegate_storage.ml index ece5ee476ce4..884da581994d 100644 --- a/src/proto_alpha/lib_protocol/delegate_storage.ml +++ b/src/proto_alpha/lib_protocol/delegate_storage.ml @@ -269,6 +269,17 @@ let current_frozen_deposits ctxt delegate = let*? total = Tez_repr.(own_frozen +? staked_frozen) in return total +let frozen_deposits_limit ctxt delegate = + Storage.Contract.Frozen_deposits_limit.find + ctxt + (Contract_repr.Implicit delegate) + +let set_frozen_deposits_limit ctxt delegate limit = + Storage.Contract.Frozen_deposits_limit.add_or_remove + ctxt + (Contract_repr.Implicit delegate) + limit + let spendable_balance ctxt delegate = let contract = Contract_repr.Implicit delegate in Storage.Contract.Spendable_balance.get ctxt contract diff --git a/src/proto_alpha/lib_protocol/delegate_storage.mli b/src/proto_alpha/lib_protocol/delegate_storage.mli index c2e3aab197b7..69522d8b0eab 100644 --- a/src/proto_alpha/lib_protocol/delegate_storage.mli +++ b/src/proto_alpha/lib_protocol/delegate_storage.mli @@ -31,6 +31,7 @@ It also groups "trivial" getters/setters related to delegates. It is responsible for maintaining the following tables: + - {!Storage.Contract.Frozen_deposits_limit} - {!Storage.Delegates} *) @@ -115,6 +116,17 @@ val initial_frozen_deposits_of_previous_cycle : val current_frozen_deposits : Raw_context.t -> Signature.public_key_hash -> Tez_repr.t tzresult Lwt.t +val frozen_deposits_limit : + Raw_context.t -> + Signature.Public_key_hash.t -> + Tez_repr.t option tzresult Lwt.t + +val set_frozen_deposits_limit : + Raw_context.t -> + Signature.Public_key_hash.t -> + Tez_repr.t option -> + Raw_context.t Lwt.t + val spendable_balance : Raw_context.t -> Signature.public_key_hash -> Tez_repr.tez tzresult Lwt.t diff --git a/src/proto_alpha/lib_protocol/operation_repr.ml b/src/proto_alpha/lib_protocol/operation_repr.ml index 1a9681d589cb..657abe2de0c6 100644 --- a/src/proto_alpha/lib_protocol/operation_repr.ml +++ b/src/proto_alpha/lib_protocol/operation_repr.ml @@ -72,6 +72,8 @@ module Kind = struct type event = Event_kind + type set_deposits_limit = Set_deposits_limit_kind + type increase_paid_storage = Increase_paid_storage_kind type update_consensus_key = Update_consensus_key_kind @@ -116,6 +118,7 @@ module Kind = struct | Delegation_manager_kind : delegation manager | Event_manager_kind : event manager | Register_global_constant_manager_kind : register_global_constant manager + | Set_deposits_limit_manager_kind : set_deposits_limit manager | Increase_paid_storage_manager_kind : increase_paid_storage manager | Update_consensus_key_manager_kind : update_consensus_key manager | Transfer_ticket_manager_kind : transfer_ticket manager @@ -320,6 +323,9 @@ and _ manager_operation = value : Script_repr.lazy_expr; } -> Kind.register_global_constant manager_operation + | Set_deposits_limit : + Tez_repr.t option + -> Kind.set_deposits_limit manager_operation | Increase_paid_storage : { amount_in_bytes : Z.t; destination : Contract_hash.t; @@ -407,6 +413,7 @@ let manager_kind : type kind. kind manager_operation -> kind Kind.manager = | Origination _ -> Kind.Origination_manager_kind | Delegation _ -> Kind.Delegation_manager_kind | Register_global_constant _ -> Kind.Register_global_constant_manager_kind + | Set_deposits_limit _ -> Kind.Set_deposits_limit_manager_kind | Increase_paid_storage _ -> Kind.Increase_paid_storage_manager_kind | Update_consensus_key _ -> Kind.Update_consensus_key_manager_kind | Transfer_ticket _ -> Kind.Transfer_ticket_manager_kind @@ -675,7 +682,18 @@ module Encoding = struct inj = (fun value -> Register_global_constant {value}); } - (* Tag 5 was for Set_deposits_limit. *) + let set_deposits_limit_case = + MCase + { + tag = 5; + name = "set_deposits_limit"; + encoding = obj1 (opt "limit" Tez_repr.encoding); + select = + (function + | Manager (Set_deposits_limit _ as op) -> Some op | _ -> None); + proj = (function Set_deposits_limit key -> key); + inj = (fun key -> Set_deposits_limit key); + } let increase_paid_storage_case = MCase @@ -1443,7 +1461,8 @@ module Encoding = struct let register_global_constant_case = make_manager_case 111 Manager_operations.register_global_constant_case - (* 112 was for Set_deposits_limit. *) + let set_deposits_limit_case = + make_manager_case 112 Manager_operations.set_deposits_limit_case let increase_paid_storage_case = make_manager_case 113 Manager_operations.increase_paid_storage_case @@ -1531,6 +1550,7 @@ module Encoding = struct PCase transaction_case; PCase origination_case; PCase delegation_case; + PCase set_deposits_limit_case; PCase increase_paid_storage_case; PCase update_consensus_key_case; PCase drain_delegate_case; @@ -1985,6 +2005,8 @@ let equal_manager_operation_kind : | Delegation _, _ -> None | Register_global_constant _, Register_global_constant _ -> Some Eq | Register_global_constant _, _ -> None + | Set_deposits_limit _, Set_deposits_limit _ -> Some Eq + | Set_deposits_limit _, _ -> None | Increase_paid_storage _, Increase_paid_storage _ -> Some Eq | Increase_paid_storage _, _ -> None | Update_consensus_key _, Update_consensus_key _ -> Some Eq diff --git a/src/proto_alpha/lib_protocol/operation_repr.mli b/src/proto_alpha/lib_protocol/operation_repr.mli index caccb54f0232..65201b3ef212 100644 --- a/src/proto_alpha/lib_protocol/operation_repr.mli +++ b/src/proto_alpha/lib_protocol/operation_repr.mli @@ -106,6 +106,8 @@ module Kind : sig type event = Event_kind + type set_deposits_limit = Set_deposits_limit_kind + type increase_paid_storage = Increase_paid_storage_kind type update_consensus_key = Update_consensus_key_kind @@ -150,6 +152,7 @@ module Kind : sig | Delegation_manager_kind : delegation manager | Event_manager_kind : event manager | Register_global_constant_manager_kind : register_global_constant manager + | Set_deposits_limit_manager_kind : set_deposits_limit manager | Increase_paid_storage_manager_kind : increase_paid_storage manager | Update_consensus_key_manager_kind : update_consensus_key manager | Transfer_ticket_manager_kind : transfer_ticket manager @@ -367,6 +370,12 @@ and _ manager_operation = value : Script_repr.lazy_expr; } -> Kind.register_global_constant manager_operation + (* [Set_deposits_limit] sets an optional limit for frozen deposits + of a contract at a lower value than the maximum limit. When None, + the limit in unset back to the default maximum limit. *) + | Set_deposits_limit : + Tez_repr.t option + -> Kind.set_deposits_limit manager_operation (* [Increase_paid_storage] allows a sender to pay to increase the paid storage of some contract by some amount. *) | Increase_paid_storage : { @@ -719,6 +728,8 @@ module Encoding : sig val register_global_constant_case : Kind.register_global_constant Kind.manager case + val set_deposits_limit_case : Kind.set_deposits_limit Kind.manager case + val increase_paid_storage_case : Kind.increase_paid_storage Kind.manager case val transfer_ticket_case : Kind.transfer_ticket Kind.manager case @@ -777,6 +788,8 @@ module Encoding : sig val register_global_constant_case : Kind.register_global_constant case + val set_deposits_limit_case : Kind.set_deposits_limit case + val increase_paid_storage_case : Kind.increase_paid_storage case val transfer_ticket_case : Kind.transfer_ticket case diff --git a/src/proto_alpha/lib_protocol/validate.ml b/src/proto_alpha/lib_protocol/validate.ml index e57dc23b4dd0..7a29526578bf 100644 --- a/src/proto_alpha/lib_protocol/validate.ml +++ b/src/proto_alpha/lib_protocol/validate.ml @@ -2124,7 +2124,8 @@ module Manager = struct return_unit | Delegation (Some pkh) -> Delegate.check_not_tz4 pkh | Update_consensus_key pk -> Delegate.Consensus_key.check_not_tz4 pk - | Delegation None | Increase_paid_storage _ -> return_unit + | Delegation None | Set_deposits_limit _ | Increase_paid_storage _ -> + return_unit | Transfer_ticket {contents; ty; _} -> let* remaining_gas = consume_decoding_gas remaining_gas contents in let* (_ : Gas.Arith.fp) = consume_decoding_gas remaining_gas ty in -- GitLab From 00c5830ef69eefb7a5ba46d1199d43fbb8c4c35c Mon Sep 17 00:00:00 2001 From: Julien Tesson Date: Thu, 26 Oct 2023 12:53:09 +0200 Subject: [PATCH 02/26] Proto/delegate_cycles: apply deposit limit in auto-staking --- src/proto_alpha/lib_protocol/delegate_cycles.ml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/proto_alpha/lib_protocol/delegate_cycles.ml b/src/proto_alpha/lib_protocol/delegate_cycles.ml index 95245cffc5ad..4c665fb95b7b 100644 --- a/src/proto_alpha/lib_protocol/delegate_cycles.ml +++ b/src/proto_alpha/lib_protocol/delegate_cycles.ml @@ -134,6 +134,14 @@ let adjust_frozen_stakes ctxt : ctxt full_staking_balance in + let* deposit_limit = + Delegate_storage.frozen_deposits_limit ctxt delegate + in + let optimal_frozen = + match deposit_limit with + | None -> optimal_frozen + | Some deposit_limit -> Tez_repr.min optimal_frozen deposit_limit + in let* ctxt, new_balance_updates = if Tez_repr.(optimal_frozen > own_frozen) then let*? optimal_to_stake = Tez_repr.(optimal_frozen -? own_frozen) in -- GitLab From 921a50aef9bd1c22742f8100deb25d41160ad6d0 Mon Sep 17 00:00:00 2001 From: Julien Tesson Date: Tue, 10 Oct 2023 08:15:00 +0200 Subject: [PATCH 03/26] Revert "Client: remove operation Set_deposits_limit" This reverts commit 65860e48e8a7313cee2cfdb06079827e1dc6a9eb. --- .../lib_client/client_proto_context.ml | 36 +++++++ .../lib_client/client_proto_context.mli | 27 +++++ src/proto_alpha/lib_client/injection.ml | 22 ++-- .../lib_client/operation_result.ml | 12 +++ .../client_proto_context_commands.ml | 102 ++++++++++++++++++ .../lib_sc_rollup_node/daemon_helpers.ml | 2 +- 6 files changed, 191 insertions(+), 10 deletions(-) diff --git a/src/proto_alpha/lib_client/client_proto_context.ml b/src/proto_alpha/lib_client/client_proto_context.ml index 6349f5a1c230..410dcfb9f80e 100644 --- a/src/proto_alpha/lib_client/client_proto_context.ml +++ b/src/proto_alpha/lib_client/client_proto_context.ml @@ -100,6 +100,9 @@ let get_contract_all_ticket_balances (rpc : #rpc_context) ~chain ~block contract let ticket_balances_encoding = Plugin.RPC.Contract.ticket_balances_encoding +let get_frozen_deposits_limit (rpc : #rpc_context) ~chain ~block delegate = + Alpha_services.Delegate.frozen_deposits_limit rpc (chain, block) delegate + let parse_expression arg = Lwt.return (Micheline_parser.no_parsing_error @@ -453,6 +456,39 @@ let drain_delegate cctxt ~chain ~block ?confirmations ?dry_run ?verbose_signing | Apply_results.Single_and_result ((Drain_delegate _ as op), result) -> return (oph, op, result) +let set_deposits_limit cctxt ~chain ~block ?confirmations ?dry_run + ?verbose_signing ?simulation ?fee contract ~src_pk ~manager_sk + ~fee_parameter limit_opt = + let operation = Set_deposits_limit limit_opt in + let operation = + Injection.prepare_manager_operation + ~fee:(Limit.of_option fee) + ~gas_limit:Limit.unknown + ~storage_limit:Limit.unknown + operation + in + let operation = Annotated_manager_operation.Single_manager operation in + Injection.inject_manager_operation + cctxt + ~chain + ~block + ?confirmations + ?dry_run + ?verbose_signing + ?simulation + ~source:contract + ~fee:(Limit.of_option fee) + ~gas_limit:Limit.unknown + ~storage_limit:Limit.unknown + ~src_pk + ~src_sk:manager_sk + ~fee_parameter + operation + >>=? fun (oph, _, op, result) -> + match Apply_results.pack_contents_list op result with + | Apply_results.Single_and_result ((Manager_operation _ as op), result) -> + return (oph, op, result) + let increase_paid_storage cctxt ~chain ~block ?force ?dry_run ?verbose_signing ?fee ?confirmations ?simulation ~source ~destination ~src_pk ~manager_sk ~fee_parameter ~amount_in_bytes () = diff --git a/src/proto_alpha/lib_client/client_proto_context.mli b/src/proto_alpha/lib_client/client_proto_context.mli index 4eb654f6e646..786f0dfac345 100644 --- a/src/proto_alpha/lib_client/client_proto_context.mli +++ b/src/proto_alpha/lib_client/client_proto_context.mli @@ -162,6 +162,14 @@ val get_contract_all_ticket_balances : val ticket_balances_encoding : (Ticket_token.unparsed_token * Z.t) list Data_encoding.t +(** Calls {!Tezos_protocol_alpha.Protocol.Delegate_services.val-frozen_deposits_limit}. *) +val get_frozen_deposits_limit : + #Protocol_client_context.rpc_context -> + chain:Shell_services.chain -> + block:Shell_services.block -> + Signature.Public_key_hash.t -> + Tez.t option tzresult Lwt.t + (** Calls {!Injection.prepare_manager_operation} with {!Alpha_context.Delegation} [delegate_opt] as operation. *) val build_delegate_operation : @@ -220,6 +228,25 @@ val drain_delegate : unit -> Kind.drain_delegate Injection.result tzresult Lwt.t +(** Calls {!Injection.inject_manager_operation} + with {!Annotated_manager_operation.Single_manager} {!Alpha_context.Set_deposits_limit} [limit_opt] + as operation. *) +val set_deposits_limit : + #Protocol_client_context.full -> + chain:Shell_services.chain -> + block:Shell_services.block -> + ?confirmations:int -> + ?dry_run:bool -> + ?verbose_signing:bool -> + ?simulation:bool -> + ?fee:Tez.tez -> + public_key_hash -> + src_pk:public_key -> + manager_sk:Client_keys.sk_uri -> + fee_parameter:Injection.fee_parameter -> + Tez.t option -> + Kind.set_deposits_limit Kind.manager Injection.result tzresult Lwt.t + (** Calls {!Injection.inject_manager_operation} with {!Annotated_manager_operation.Single_manager} {!Alpha_context.Increase_paid_storage} [{amount_in_bytes; destination}] as operation. *) diff --git a/src/proto_alpha/lib_client/injection.ml b/src/proto_alpha/lib_client/injection.ml index d82472539d74..0fac51be112a 100644 --- a/src/proto_alpha/lib_client/injection.ml +++ b/src/proto_alpha/lib_client/injection.ml @@ -345,6 +345,7 @@ let estimated_gas_single (type kind) | Reveal_result {consumed_gas} | Delegation_result {consumed_gas; _} | Register_global_constant_result {consumed_gas; _} + | Set_deposits_limit_result {consumed_gas} | Update_consensus_key_result {consumed_gas; _} | Increase_paid_storage_result {consumed_gas; _} | Transfer_ticket_result {consumed_gas; _} @@ -421,8 +422,9 @@ let estimated_storage_single (type kind) ~origination_size | Sc_rollup_originate_result {size; _} -> Ok size | Zk_rollup_origination_result {storage_size; _} -> Ok storage_size | Transaction_result (Transaction_to_sc_rollup_result _) - | Reveal_result _ | Delegation_result _ | Increase_paid_storage_result _ - | Dal_publish_slot_header_result _ | Sc_rollup_add_messages_result _ + | Reveal_result _ | Delegation_result _ | Set_deposits_limit_result _ + | Increase_paid_storage_result _ | Dal_publish_slot_header_result _ + | Sc_rollup_add_messages_result _ (* The following Sc_rollup operations have zero storage cost because we consider them to be paid in the stake deposit. @@ -502,12 +504,13 @@ let originated_contracts_single (type kind) ( Transaction_to_sc_rollup_result _ | Transaction_to_zk_rollup_result _ ) | Register_global_constant_result _ | Reveal_result _ - | Delegation_result _ | Update_consensus_key_result _ - | Increase_paid_storage_result _ | Transfer_ticket_result _ - | Dal_publish_slot_header_result _ | Sc_rollup_originate_result _ - | Sc_rollup_add_messages_result _ | Sc_rollup_cement_result _ - | Sc_rollup_publish_result _ | Sc_rollup_refute_result _ - | Sc_rollup_timeout_result _ | Sc_rollup_execute_outbox_message_result _ + | Delegation_result _ | Set_deposits_limit_result _ + | Update_consensus_key_result _ | Increase_paid_storage_result _ + | Transfer_ticket_result _ | Dal_publish_slot_header_result _ + | Sc_rollup_originate_result _ | Sc_rollup_add_messages_result _ + | Sc_rollup_cement_result _ | Sc_rollup_publish_result _ + | Sc_rollup_refute_result _ | Sc_rollup_timeout_result _ + | Sc_rollup_execute_outbox_message_result _ | Sc_rollup_recover_bond_result _ | Zk_rollup_origination_result _ | Zk_rollup_publish_result _ | Zk_rollup_update_result _ -> return_nil) @@ -866,7 +869,8 @@ let may_patch_limits (type kind) (cctxt : #Protocol_client_context.full) let safety_guard = match c.operation with | Transaction {destination = Implicit _; _} - | Reveal _ | Delegation _ | Increase_paid_storage _ -> + | Reveal _ | Delegation _ | Set_deposits_limit _ + | Increase_paid_storage _ -> Gas.Arith.zero | _ -> safety_guard in diff --git a/src/proto_alpha/lib_client/operation_result.ml b/src/proto_alpha/lib_client/operation_result.ml index d689cab7b137..06b2b4af6c6f 100644 --- a/src/proto_alpha/lib_client/operation_result.ml +++ b/src/proto_alpha/lib_client/operation_result.ml @@ -214,6 +214,15 @@ let pp_manager_operation_content (type kind) source ppf "Register Global:@,Value: %a" pp_micheline_from_lazy_expr value + | Set_deposits_limit limit_opt -> ( + Format.fprintf + ppf + "Set deposits limit:@,Delegate: %a@," + Contract.pp + source ; + match limit_opt with + | None -> Format.pp_print_string ppf "Unlimited deposits" + | Some limit -> Format.fprintf ppf "Limit: %a" Tez.pp limit) | Increase_paid_storage {amount_in_bytes; destination} -> Format.fprintf ppf @@ -806,6 +815,7 @@ let pp_manager_operation_contents_result ppf op_result = | Origination_result _ -> "origination" | Delegation_result _ -> "delegation" | Register_global_constant_result _ -> "global constant registration" + | Set_deposits_limit_result _ -> "deposits limit modification" | Update_consensus_key_result _ -> "consensus key update" | Increase_paid_storage_result _ -> "paid storage increase" | Transfer_ticket_result _ -> "tickets transfer" @@ -831,6 +841,8 @@ let pp_manager_operation_contents_result ppf op_result = | Delegation_result {consumed_gas; balance_updates} -> pp_consumed_gas ppf consumed_gas ; pp_balance_updates ppf balance_updates + | Set_deposits_limit_result {consumed_gas} -> + pp_consumed_gas ppf consumed_gas | Update_consensus_key_result {consumed_gas} -> pp_consumed_gas ppf consumed_gas | Transaction_result tx -> pp_transaction_result ppf tx diff --git a/src/proto_alpha/lib_client_commands/client_proto_context_commands.ml b/src/proto_alpha/lib_client_commands/client_proto_context_commands.ml index 93f8575e610d..4ea45fbc1bcd 100644 --- a/src/proto_alpha/lib_client_commands/client_proto_context_commands.ml +++ b/src/proto_alpha/lib_client_commands/client_proto_context_commands.ml @@ -821,6 +821,31 @@ let commands_ro () = .unsigned_encoding_with_legacy_attestation_name) in return_unit); + command + ~group + ~desc:"Get the frozen deposits limit of a delegate." + no_options + (prefixes ["get"; "deposits"; "limit"; "for"] + @@ Client_keys.Public_key_hash.source_param + ~name:"src" + ~desc:"source delegate" + @@ stop) + (fun () delegate (cctxt : Protocol_client_context.full) -> + let open Lwt_result_syntax in + let* deposit = + get_frozen_deposits_limit + cctxt + ~chain:cctxt#chain + ~block:cctxt#block + delegate + in + let*! () = + match deposit with + | None -> cctxt#answer "unlimited" + | Some limit -> + cctxt#answer "%a %s" Tez.pp limit Operation_result.tez_sym + in + return_unit); ] (* ----------------------------------------------------------------------------*) @@ -2726,6 +2751,83 @@ let commands_rw () = ballot in return_unit); + command + ~group + ~desc:"Set the deposits limit of a registered delegate." + (args5 + fee_arg + dry_run_switch + verbose_signing_switch + simulate_switch + fee_parameter_args) + (prefixes ["set"; "deposits"; "limit"; "for"] + @@ Client_keys.Public_key_hash.source_param + ~name:"src" + ~desc:"source contract" + @@ prefix "to" + @@ tez_param + ~name:"deposits limit" + ~desc:"the maximum amount of frozen deposits" + @@ stop) + (fun (fee, dry_run, verbose_signing, simulation, fee_parameter) + mgr + limit + (cctxt : Protocol_client_context.full) -> + let open Lwt_result_syntax in + let* _, src_pk, manager_sk = Client_keys.get_key cctxt mgr in + let* (_ : _ Injection.result) = + set_deposits_limit + cctxt + ~chain:cctxt#chain + ~block:cctxt#block + ?confirmations:cctxt#confirmations + ~dry_run + ~verbose_signing + ~simulation + ~fee_parameter + ?fee + mgr + ~src_pk + ~manager_sk + (Some limit) + in + return_unit); + command + ~group + ~desc:"Remove the deposits limit of a registered delegate." + (args5 + fee_arg + dry_run_switch + verbose_signing_switch + simulate_switch + fee_parameter_args) + (prefixes ["unset"; "deposits"; "limit"; "for"] + @@ Client_keys.Public_key_hash.source_param + ~name:"src" + ~desc:"source contract" + @@ stop) + (fun (fee, dry_run, verbose_signing, simulation, fee_parameter) + mgr + (cctxt : Protocol_client_context.full) -> + let open Lwt_result_syntax in + let* _, src_pk, manager_sk = Client_keys.get_key cctxt mgr in + let* (_ : _ Injection.result) = + set_deposits_limit + cctxt + ~chain:cctxt#chain + ~block:cctxt#block + ?confirmations:cctxt#confirmations + ~dry_run + ~verbose_signing + ~simulation + ~fee_parameter + ?fee + mgr + ~src_pk + ~manager_sk + None + in + return_unit); command ~group ~desc:"Increase the paid storage of a smart contract." diff --git a/src/proto_alpha/lib_sc_rollup_node/daemon_helpers.ml b/src/proto_alpha/lib_sc_rollup_node/daemon_helpers.ml index b7148b732a01..a1106f5a0b6e 100644 --- a/src/proto_alpha/lib_sc_rollup_node/daemon_helpers.ml +++ b/src/proto_alpha/lib_sc_rollup_node/daemon_helpers.ml @@ -323,7 +323,7 @@ let process_l1_operation (type kind) ~catching_up node_ctxt rollup = node_ctxt.Node_context.config.sc_rollup_address) | Dal_publish_slot_header _ -> true | Reveal _ | Transaction _ | Origination _ | Delegation _ - | Update_consensus_key _ | Register_global_constant _ + | Update_consensus_key _ | Register_global_constant _ | Set_deposits_limit _ | Increase_paid_storage _ | Transfer_ticket _ | Sc_rollup_originate _ | Zk_rollup_origination _ | Zk_rollup_publish _ | Zk_rollup_update _ -> false -- GitLab From 90de4bc5a3ea327c635ae511f6f48072b00f370f Mon Sep 17 00:00:00 2001 From: Julien Tesson Date: Thu, 12 Oct 2023 14:35:35 +0200 Subject: [PATCH 04/26] Revert "Tezt: remove unused regression tests" This reverts commit 78783b736f84d1ee6ba3d14b868e5b893a57c901. --- .../Alpha- set deposits limit.out | 26 +++++++++++++++++++ .../Alpha- unset deposits limit.out | 26 +++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 tezt/tests/expected/deposits_limit.ml/Alpha- set deposits limit.out create mode 100644 tezt/tests/expected/deposits_limit.ml/Alpha- unset deposits limit.out diff --git a/tezt/tests/expected/deposits_limit.ml/Alpha- set deposits limit.out b/tezt/tests/expected/deposits_limit.ml/Alpha- set deposits limit.out new file mode 100644 index 000000000000..8ac64a759156 --- /dev/null +++ b/tezt/tests/expected/deposits_limit.ml/Alpha- set deposits limit.out @@ -0,0 +1,26 @@ +Node is bootstrapped. +Estimated gas: 168.052 units (will add 0 for safety) +Estimated storage: no bytes added +Operation successfully injected in the node. +Operation hash is 'oogPg9fm3Wo8Fub69ehKvyhmefv442MSvvKME7awo3zvZVAsuTo' +NOT waiting for the operation to be included. +Use command + octez-client wait for oogPg9fm3Wo8Fub69ehKvyhmefv442MSvvKME7awo3zvZVAsuTo to be included --confirmations 1 --branch BLockGenesisGenesisGenesisGenesisGenesisf79b5d1CoW2 +and/or an external block explorer to make sure that it has been included. +This sequence of operations was run: + Manager signed operations: + From: tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx + Fee to the baker: ꜩ0.000247 + Expected counter: 1 + Gas limit: 169 + Storage limit: 0 bytes + Balance updates: + tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx ... -ꜩ0.000247 + payload fees(the block proposer) ....... +ꜩ0.000247 + Set deposits limit: + Delegate: tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx + Limit: 1000 + This deposits limit modification was successfully applied + Consumed gas: 167.986 + + diff --git a/tezt/tests/expected/deposits_limit.ml/Alpha- unset deposits limit.out b/tezt/tests/expected/deposits_limit.ml/Alpha- unset deposits limit.out new file mode 100644 index 000000000000..fc4436f54195 --- /dev/null +++ b/tezt/tests/expected/deposits_limit.ml/Alpha- unset deposits limit.out @@ -0,0 +1,26 @@ +Node is bootstrapped. +Estimated gas: 167.886 units (will add 0 for safety) +Estimated storage: no bytes added +Operation successfully injected in the node. +Operation hash is 'oo1PJ41t8u2VYdoeFVgREH9LKKtASE141H3wWRmKRbuBcshW3hR' +NOT waiting for the operation to be included. +Use command + octez-client wait for oo1PJ41t8u2VYdoeFVgREH9LKKtASE141H3wWRmKRbuBcshW3hR to be included --confirmations 1 --branch BLockGenesisGenesisGenesisGenesisGenesisf79b5d1CoW2 +and/or an external block explorer to make sure that it has been included. +This sequence of operations was run: + Manager signed operations: + From: tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx + Fee to the baker: ꜩ0.000242 + Expected counter: 1 + Gas limit: 168 + Storage limit: 0 bytes + Balance updates: + tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx ... -ꜩ0.000242 + payload fees(the block proposer) ....... +ꜩ0.000242 + Set deposits limit: + Delegate: tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx + Unlimited deposits + This deposits limit modification was successfully applied + Consumed gas: 167.820 + + -- GitLab From 3cc5aed096dfb09df22e1c5144bf905ce985dd08 Mon Sep 17 00:00:00 2001 From: Julien Tesson Date: Thu, 12 Oct 2023 14:42:15 +0200 Subject: [PATCH 05/26] Revert "Tezt: deactivate tests on Set_deposits_limit after Nairobi" This reverts commit 1846463b3c10a566c78a70d87e9478b13059f8d4. --- tezt/tests/deposits_limit.ml | 2 -- 1 file changed, 2 deletions(-) diff --git a/tezt/tests/deposits_limit.ml b/tezt/tests/deposits_limit.ml index a2e9eeb61a6c..630e90e90ef2 100644 --- a/tezt/tests/deposits_limit.ml +++ b/tezt/tests/deposits_limit.ml @@ -34,7 +34,6 @@ let test_set_deposits_limit = ~__FILE__ ~title:"set deposits limit" ~tags:["deposits_limit"] - ~supports:Protocol.(Until_protocol (number Nairobi)) @@ fun protocol -> let* _, client = Client.init_with_protocol ~protocol `Client () in let src = Constant.bootstrap1.alias in @@ -47,7 +46,6 @@ let test_unset_deposits_limit = ~__FILE__ ~title:"unset deposits limit" ~tags:["deposits_limit"] - ~supports:Protocol.(Until_protocol (number Nairobi)) @@ fun protocol -> let* _, client = Client.init_with_protocol ~protocol `Client () in let src = Constant.bootstrap1.alias in -- GitLab From 1beb12894f934cb5e354382876e1030ea6b0445a Mon Sep 17 00:00:00 2001 From: Julien Tesson Date: Thu, 12 Oct 2023 15:27:13 +0200 Subject: [PATCH 06/26] Revert "Proto/Tests: remove Set_deposits_limit" This reverts commit 0912dd09bf600201b0299cd1e7a5974b088a5694. --- .../lib_protocol/test/helpers/block.ml | 11 +- .../lib_protocol/test/helpers/context.ml | 4 + .../lib_protocol/test/helpers/context.mli | 4 + .../lib_protocol/test/helpers/op.ml | 16 +++ .../lib_protocol/test/helpers/op.mli | 11 ++ .../test/helpers/operation_generator.ml | 8 ++ .../consensus/test_frozen_deposits.ml | 114 +++++++++++++++++- .../validate/manager_operation_helpers.ml | 28 ++++- .../test/integration/validate/test_sanity.ml | 9 +- 9 files changed, 189 insertions(+), 16 deletions(-) diff --git a/src/proto_alpha/lib_protocol/test/helpers/block.ml b/src/proto_alpha/lib_protocol/test/helpers/block.ml index db8661f8154f..1da2ebf89d86 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/block.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/block.ml @@ -1082,11 +1082,11 @@ let balance_update_of_operation_result : | Protocol.Apply_results.Transaction_result (Transaction_to_sc_rollup_result _) | Reveal_result _ | Update_consensus_key_result _ - | Transfer_ticket_result _ | Dal_publish_slot_header_result _ - | Sc_rollup_originate_result _ | Sc_rollup_add_messages_result _ - | Sc_rollup_cement_result _ | Sc_rollup_publish_result _ - | Sc_rollup_refute_result _ | Sc_rollup_timeout_result _ - | Sc_rollup_execute_outbox_message_result _ + | Set_deposits_limit_result _ | Transfer_ticket_result _ + | Dal_publish_slot_header_result _ | Sc_rollup_originate_result _ + | Sc_rollup_add_messages_result _ | Sc_rollup_cement_result _ + | Sc_rollup_publish_result _ | Sc_rollup_refute_result _ + | Sc_rollup_timeout_result _ | Sc_rollup_execute_outbox_message_result _ | Sc_rollup_recover_bond_result _ | Zk_rollup_origination_result _ | Zk_rollup_publish_result _ | Zk_rollup_update_result _ -> [] @@ -1194,6 +1194,7 @@ let bake_n_with_origination_results ?baking_mode ?policy n b = | Successful_manager_result (Update_consensus_key_result _) | Successful_manager_result (Transaction_result _) | Successful_manager_result (Register_global_constant_result _) + | Successful_manager_result (Set_deposits_limit_result _) | Successful_manager_result (Increase_paid_storage_result _) | Successful_manager_result (Transfer_ticket_result _) | Successful_manager_result (Dal_publish_slot_header_result _) diff --git a/src/proto_alpha/lib_protocol/test/helpers/context.ml b/src/proto_alpha/lib_protocol/test/helpers/context.ml index 026e8249d1a1..1798ac389eba 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/context.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/context.ml @@ -456,6 +456,7 @@ module Delegate = struct current_frozen_deposits : Tez.t; frozen_deposits : Tez.t; staking_balance : Tez.t; + frozen_deposits_limit : Tez.t option; delegated_contracts : Alpha_context.Contract.t list; delegated_balance : Tez.t; total_delegated_stake : Tez.t; @@ -487,6 +488,9 @@ module Delegate = struct in Staking_pseudotoken.Internal_for_tests.to_z pseudotokens + let frozen_deposits_limit ctxt pkh = + Delegate_services.frozen_deposits_limit rpc_ctxt ctxt pkh + let deactivated ctxt pkh = Delegate_services.deactivated rpc_ctxt ctxt pkh let voting_info ctxt d = Alpha_services.Delegate.voting_info rpc_ctxt ctxt d diff --git a/src/proto_alpha/lib_protocol/test/helpers/context.mli b/src/proto_alpha/lib_protocol/test/helpers/context.mli index a726ac8f6c75..ac109ef496fb 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/context.mli +++ b/src/proto_alpha/lib_protocol/test/helpers/context.mli @@ -232,6 +232,7 @@ module Delegate : sig current_frozen_deposits : Tez.t; frozen_deposits : Tez.t; staking_balance : Tez.t; + frozen_deposits_limit : Tez.t option; delegated_contracts : Alpha_context.Contract.t list; delegated_balance : Tez.t; total_delegated_stake : Tez.t; @@ -257,6 +258,9 @@ module Delegate : sig val staking_denominator : t -> public_key_hash -> Z.t tzresult Lwt.t + val frozen_deposits_limit : + t -> public_key_hash -> Tez.t option tzresult Lwt.t + val deactivated : t -> public_key_hash -> bool tzresult Lwt.t val voting_info : t -> public_key_hash -> Vote.delegate_info tzresult Lwt.t diff --git a/src/proto_alpha/lib_protocol/test/helpers/op.ml b/src/proto_alpha/lib_protocol/test/helpers/op.ml index 751ba0581bf2..8f0b57bc1d39 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/op.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/op.ml @@ -635,6 +635,22 @@ let delegation ?force_reveal ?fee ?gas_limit ?counter ?storage_limit ctxt source let+ account = Context.Contract.manager ctxt source in sign account.sk (Context.branch ctxt) sop +let set_deposits_limit ?force_reveal ?fee ?gas_limit ?storage_limit ?counter + ctxt source limit = + let top = Set_deposits_limit limit in + manager_operation + ?force_reveal + ?fee + ?counter + ?storage_limit + ?gas_limit + ~source + ctxt + top + >>=? fun sop -> + Context.Contract.manager ctxt source >|=? fun account -> + sign account.sk (Context.branch ctxt) sop + let increase_paid_storage ?force_reveal ?counter ?fee ?gas_limit ?storage_limit ctxt ~source ~destination (amount : Z.t) = let open Lwt_result_syntax in diff --git a/src/proto_alpha/lib_protocol/test/helpers/op.mli b/src/proto_alpha/lib_protocol/test/helpers/op.mli index f51dd7318db3..cee0f9975ec9 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/op.mli +++ b/src/proto_alpha/lib_protocol/test/helpers/op.mli @@ -174,6 +174,17 @@ val delegation : public_key_hash option -> Operation.packed tzresult Lwt.t +val set_deposits_limit : + ?force_reveal:bool -> + ?fee:Tez.tez -> + ?gas_limit:gas_limit -> + ?storage_limit:Z.t -> + ?counter:Manager_counter.t -> + Context.t -> + Contract.t -> + Tez.tez option -> + Operation.packed tzresult Lwt.t + val increase_paid_storage : ?force_reveal:bool -> ?counter:Manager_counter.t -> diff --git a/src/proto_alpha/lib_protocol/test/helpers/operation_generator.ml b/src/proto_alpha/lib_protocol/test/helpers/operation_generator.ml index 43928b5360fa..447b5a9caf93 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/operation_generator.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/operation_generator.ml @@ -70,6 +70,7 @@ let manager_kinds = `KTransaction; `KOrigination; `KDelegation; + `KSet_deposits_limit; `KIncrease_paid_storage; `KRegister_global_constant; `KTransfer_ticket; @@ -501,6 +502,11 @@ let generate_increase_paid_storage = let+ destination = random_contract_hash in Increase_paid_storage {amount_in_bytes; destination} +let generate_set_deposits_limit = + let open QCheck2.Gen in + let+ amount_opt = option gen_amount in + Set_deposits_limit amount_opt + let generate_register_global_constant = let value = Script_repr.lazy_expr (Expr.from_string "Pair 1 2") in QCheck2.Gen.pure (Register_global_constant {value}) @@ -600,6 +606,8 @@ let generator_of ?source = function | `KReveal -> generate_manager_operation ?source generate_reveal | `KTransaction -> generate_manager_operation ?source generate_transaction | `KOrigination -> generate_manager_operation ?source generate_origination + | `KSet_deposits_limit -> + generate_manager_operation ?source generate_set_deposits_limit | `KIncrease_paid_storage -> generate_manager_operation ?source generate_increase_paid_storage | `KDelegation -> generate_manager_operation ?source generate_delegation diff --git a/src/proto_alpha/lib_protocol/test/integration/consensus/test_frozen_deposits.ml b/src/proto_alpha/lib_protocol/test/integration/consensus/test_frozen_deposits.ml index a5fc4550f214..2a58f26276f0 100644 --- a/src/proto_alpha/lib_protocol/test/integration/consensus/test_frozen_deposits.ml +++ b/src/proto_alpha/lib_protocol/test/integration/consensus/test_frozen_deposits.ml @@ -28,11 +28,12 @@ Component: Protocol (frozen_deposits) Invocation: dune exec src/proto_alpha/lib_protocol/test/integration/consensus/main.exe \ -- --file test_frozen_deposits.ml - Subject: consistency of frozen deposits + Subject: consistency of frozen deposits and the [set_deposits_limit] operation *) open Protocol open Alpha_context +open Test_tez let constants = { @@ -384,6 +385,106 @@ let test_may_not_bake_again_after_full_deposit_slash () = let* _ = Block.bake ~policy:(By_account slashed_account) b in return_unit +let test_set_limit balance_percentage () = + Context.init_with_constants2 constants >>=? fun (genesis, contracts) -> + let (contract1, account1), (_contract2, account2) = + get_first_2_accounts_contracts contracts + in + (Context.Delegate.frozen_deposits_limit (B genesis) account1 >>=? function + | Some _ -> Alcotest.fail "unexpected deposits limit" + | None -> return_unit) + >>=? fun () -> + (* Test deposit consistency before and after first cycle *) + Context.Delegate.full_balance (B genesis) account1 >>=? fun full_balance -> + Context.Delegate.current_frozen_deposits (B genesis) account1 + >>=? fun frozen_deposits -> + let expected_deposits = + full_balance /! Int64.of_int (constants.delegation_over_baking_limit + 1) + in + Assert.equal_tez ~loc:__LOC__ frozen_deposits expected_deposits >>=? fun () -> + (* Bake until end of first cycle *) + Block.bake_until_cycle_end genesis >>=? fun b -> + Context.Delegate.full_balance (B genesis) account1 >>=? fun full_balance -> + Context.Delegate.current_frozen_deposits (B genesis) account1 + >>=? fun frozen_deposits -> + let expected_deposits = + full_balance /! Int64.of_int (constants.delegation_over_baking_limit + 1) + in + Assert.equal_tez ~loc:__LOC__ frozen_deposits expected_deposits >>=? fun () -> + (* set deposits limit to balance_percentage out of the balance *) + let limit = + Test_tez.(full_balance *! Int64.of_int balance_percentage /! 100L) + in + Op.set_deposits_limit (B genesis) contract1 (Some limit) >>=? fun operation -> + Block.bake ~policy:(By_account account2) ~operation b >>=? fun b -> + (Context.Delegate.frozen_deposits_limit (B b) account1 >>=? function + | Some set_limit -> Assert.equal_tez ~loc:__LOC__ set_limit limit + | None -> Alcotest.fail "unexpected absence of deposits limit") + >>=? fun () -> + (* the frozen deposits limit affects the active stake for cycles starting with c + + preserved_cycles + 1; the new active stake is taken into account when + computing the frozen deposits for cycle c+1 already, however the user may see + an update to its frozen deposits at cycle c + preserved_cycles + + max_slashing_period at the latest (because up to that cycle the frozen + deposits also depend on the active stake at cycles before cycle c+1). *) + let expected_number_of_cycles_with_previous_deposit = + constants.preserved_cycles + Constants.max_slashing_period + in + Block.bake_until_n_cycle_end + ~policy:(By_account account2) + (expected_number_of_cycles_with_previous_deposit - 1) + b + >>=? fun b -> + Context.Delegate.current_frozen_deposits (B b) account1 + >>=? fun frozen_deposits -> + Assert.not_equal_tez ~loc:__LOC__ frozen_deposits Tez.zero >>=? fun () -> + Block.bake_until_cycle_end ~policy:(By_account account2) b >>=? fun b -> + Context.Delegate.current_frozen_deposits (B b) account1 + >>=? fun frozen_deposits -> + Assert.equal_tez ~loc:__LOC__ frozen_deposits limit + +let test_unset_limit () = + Context.init_with_constants2 constants >>=? fun (genesis, contracts) -> + let (contract1, account1), (_contract2, account2) = + get_first_2_accounts_contracts contracts + in + (* set the limit to 0 *) + Op.set_deposits_limit (B genesis) contract1 (Some Tez.zero) + >>=? fun operation -> + Block.bake ~policy:(By_account account2) ~operation genesis >>=? fun b -> + (Context.Delegate.frozen_deposits_limit (B b) account1 >>=? function + | Some set_limit -> Assert.equal_tez ~loc:__LOC__ set_limit Tez.zero + | None -> Alcotest.fail "unexpected absence of deposits limit") + >>=? fun () -> + let expected_number_of_cycles_with_previous_deposit = + constants.preserved_cycles + Constants.max_slashing_period + in + Block.bake_until_n_cycle_end + ~policy:(By_account account2) + expected_number_of_cycles_with_previous_deposit + b + >>=? fun b -> + Context.Delegate.current_frozen_deposits (B b) account1 + >>=? fun frozen_deposits_at_b -> + (* after [expected_number_of_cycles_with_previous_deposit] cycles + the 0 limit is reflected in the deposit which becomes 0 itself *) + Assert.equal_tez ~loc:__LOC__ frozen_deposits_at_b Tez.zero >>=? fun () -> + (* unset the 0 limit *) + Op.set_deposits_limit (B b) contract1 None >>=? fun operation -> + Block.bake ~policy:(By_account account2) ~operation b >>=? fun b -> + (Context.Delegate.frozen_deposits_limit (B b) account1 >>=? function + | Some _ -> Alcotest.fail "unexpected deposits limit" + | None -> return_unit) + >>=? fun () -> + (* removing the 0 limit is visible once the cycle ends *) + Block.bake_until_cycle_end ~policy:(By_account account2) b >>=? fun bfin -> + Context.Delegate.current_frozen_deposits (B bfin) account1 + >>=? fun frozen_deposits_at_bfin -> + (* without a limit and without manual staking, the new deposit is still zero; + note that account1 hasn't baked any block. *) + Assert.equal_tez ~loc:__LOC__ frozen_deposits_at_bfin Tez.zero >>=? fun () -> + return_unit + let test_deposits_after_stake_removal () = let open Lwt_result_syntax in let* genesis, contracts = Context.init_with_constants2 constants in @@ -782,6 +883,17 @@ let tests = Tztest. [ tztest "invariants" `Quick test_invariants; + tztest "set deposits limit to 0%" `Quick (test_set_limit 0); + tztest "set deposits limit to 5%" `Quick (test_set_limit 5); + tztest "unset deposits limit" `Quick test_unset_limit; + tztest + "cannot bake with zero deposits limit" + `Quick + test_cannot_bake_with_zero_deposits_limit; + tztest + "may not bake again after full slash" + `Quick + test_may_not_bake_again_after_full_deposit_slash; tztest "deposits after stake removal" `Quick diff --git a/src/proto_alpha/lib_protocol/test/integration/validate/manager_operation_helpers.ml b/src/proto_alpha/lib_protocol/test/integration/validate/manager_operation_helpers.ml index ef6d9ec573b0..7c6ba728193e 100644 --- a/src/proto_alpha/lib_protocol/test/integration/validate/manager_operation_helpers.ml +++ b/src/proto_alpha/lib_protocol/test/integration/validate/manager_operation_helpers.ml @@ -80,6 +80,7 @@ type manager_operation_kind = | K_Delegation | K_Undelegation | K_Self_delegation + | K_Set_deposits_limit | K_Update_consensus_key | K_Increase_paid_storage | K_Reveal @@ -170,6 +171,7 @@ let kind_to_string = function | K_Delegation -> "Delegation" | K_Undelegation -> "Undelegation" | K_Self_delegation -> "Self-delegation" + | K_Set_deposits_limit -> "Set deposits limit" | K_Update_consensus_key -> "Update consensus key" | K_Origination -> "Origination" | K_Register_global_constant -> "Register global constant" @@ -673,6 +675,17 @@ let mk_register_global_constant (oinfos : operation_req) (infos : infos) = ~source:(contract_of (get_source infos)) ~value:(Script_repr.lazy_expr (Expr.from_string "Pair 1 2")) +let mk_set_deposits_limit (oinfos : operation_req) (infos : infos) = + Op.set_deposits_limit + ?force_reveal:oinfos.force_reveal + ?fee:oinfos.fee + ?gas_limit:oinfos.gas_limit + ?storage_limit:oinfos.storage_limit + ?counter:oinfos.counter + (B infos.ctxt.block) + (contract_of (get_source infos)) + None + let mk_update_consensus_key (oinfos : operation_req) (infos : infos) = Op.update_consensus_key ?force_reveal:oinfos.force_reveal @@ -981,6 +994,7 @@ let select_op (op_req : operation_req) (infos : infos) = | K_Delegation -> mk_delegation | K_Undelegation -> mk_undelegation | K_Self_delegation -> mk_self_delegation + | K_Set_deposits_limit -> mk_set_deposits_limit | K_Update_consensus_key -> mk_update_consensus_key | K_Increase_paid_storage -> mk_increase_paid_storage | K_Reveal -> mk_reveal @@ -1346,6 +1360,7 @@ let subjects = K_Delegation; K_Undelegation; K_Self_delegation; + K_Set_deposits_limit; K_Update_consensus_key; K_Increase_paid_storage; K_Reveal; @@ -1365,10 +1380,10 @@ let subjects = ] let is_consumer = function - | K_Update_consensus_key | K_Increase_paid_storage | K_Reveal - | K_Self_delegation | K_Delegation | K_Undelegation | K_Sc_rollup_add_messages - | K_Sc_rollup_origination | K_Sc_rollup_refute | K_Sc_rollup_timeout - | K_Sc_rollup_cement | K_Sc_rollup_publish + | K_Set_deposits_limit | K_Update_consensus_key | K_Increase_paid_storage + | K_Reveal | K_Self_delegation | K_Delegation | K_Undelegation + | K_Sc_rollup_add_messages | K_Sc_rollup_origination | K_Sc_rollup_refute + | K_Sc_rollup_timeout | K_Sc_rollup_cement | K_Sc_rollup_publish | K_Sc_rollup_execute_outbox_message | K_Sc_rollup_recover_bond | K_Dal_publish_slot_header | K_Zk_rollup_origination | K_Zk_rollup_publish | K_Zk_rollup_update -> @@ -1385,8 +1400,9 @@ let revealed_subjects = let is_disabled flags = function | K_Transaction | K_Origination | K_Register_global_constant | K_Delegation - | K_Undelegation | K_Self_delegation | K_Update_consensus_key - | K_Increase_paid_storage | K_Reveal | K_Transfer_ticket -> + | K_Undelegation | K_Self_delegation | K_Set_deposits_limit + | K_Update_consensus_key | K_Increase_paid_storage | K_Reveal + | K_Transfer_ticket -> false | K_Sc_rollup_origination | K_Sc_rollup_publish | K_Sc_rollup_cement | K_Sc_rollup_add_messages | K_Sc_rollup_refute | K_Sc_rollup_timeout diff --git a/src/proto_alpha/lib_protocol/test/integration/validate/test_sanity.ml b/src/proto_alpha/lib_protocol/test/integration/validate/test_sanity.ml index 439db456ea92..98b47f13288f 100644 --- a/src/proto_alpha/lib_protocol/test/integration/validate/test_sanity.ml +++ b/src/proto_alpha/lib_protocol/test/integration/validate/test_sanity.ml @@ -63,6 +63,7 @@ let ensure_kind infos kind = | Delegation _, K_Undelegation | Delegation _, K_Self_delegation | Register_global_constant _, K_Register_global_constant + | Set_deposits_limit _, K_Set_deposits_limit | Update_consensus_key _, K_Update_consensus_key | Increase_paid_storage _, K_Increase_paid_storage | Transfer_ticket _, K_Transfer_ticket @@ -80,10 +81,10 @@ let ensure_kind infos kind = | Zk_rollup_update _, K_Zk_rollup_update -> return_unit | ( ( Transaction _ | Origination _ | Register_global_constant _ - | Delegation _ | Update_consensus_key _ | Increase_paid_storage _ - | Reveal _ | Transfer_ticket _ | Sc_rollup_originate _ - | Sc_rollup_publish _ | Sc_rollup_cement _ | Sc_rollup_add_messages _ - | Sc_rollup_refute _ | Sc_rollup_timeout _ + | Delegation _ | Set_deposits_limit _ | Update_consensus_key _ + | Increase_paid_storage _ | Reveal _ | Transfer_ticket _ + | Sc_rollup_originate _ | Sc_rollup_publish _ | Sc_rollup_cement _ + | Sc_rollup_add_messages _ | Sc_rollup_refute _ | Sc_rollup_timeout _ | Sc_rollup_execute_outbox_message _ | Sc_rollup_recover_bond _ | Dal_publish_slot_header _ | Zk_rollup_origination _ | Zk_rollup_publish _ | Zk_rollup_update _ ), -- GitLab From 8827ab6c4c5949e0472f7936537c07f1d7fe6e7c Mon Sep 17 00:00:00 2001 From: Julien Tesson Date: Thu, 12 Oct 2023 15:27:25 +0200 Subject: [PATCH 07/26] Proto/tests: use monadic-let in revived tests --- .../consensus/test_frozen_deposits.ml | 145 ++++++++++-------- 1 file changed, 85 insertions(+), 60 deletions(-) diff --git a/src/proto_alpha/lib_protocol/test/integration/consensus/test_frozen_deposits.ml b/src/proto_alpha/lib_protocol/test/integration/consensus/test_frozen_deposits.ml index 2a58f26276f0..f3c94f54fc73 100644 --- a/src/proto_alpha/lib_protocol/test/integration/consensus/test_frozen_deposits.ml +++ b/src/proto_alpha/lib_protocol/test/integration/consensus/test_frozen_deposits.ml @@ -386,103 +386,128 @@ let test_may_not_bake_again_after_full_deposit_slash () = return_unit let test_set_limit balance_percentage () = + let open Lwt_result_syntax in Context.init_with_constants2 constants >>=? fun (genesis, contracts) -> let (contract1, account1), (_contract2, account2) = get_first_2_accounts_contracts contracts in - (Context.Delegate.frozen_deposits_limit (B genesis) account1 >>=? function - | Some _ -> Alcotest.fail "unexpected deposits limit" - | None -> return_unit) - >>=? fun () -> + let* () = + let* res = Context.Delegate.frozen_deposits_limit (B genesis) account1 in + match res with + | Some _ -> Alcotest.fail "unexpected deposits limit" + | None -> return_unit + in (* Test deposit consistency before and after first cycle *) - Context.Delegate.full_balance (B genesis) account1 >>=? fun full_balance -> - Context.Delegate.current_frozen_deposits (B genesis) account1 - >>=? fun frozen_deposits -> + let* full_balance = Context.Delegate.full_balance (B genesis) account1 in + let* frozen_deposits = + Context.Delegate.current_frozen_deposits (B genesis) account1 + in let expected_deposits = full_balance /! Int64.of_int (constants.delegation_over_baking_limit + 1) in - Assert.equal_tez ~loc:__LOC__ frozen_deposits expected_deposits >>=? fun () -> + let* () = Assert.equal_tez ~loc:__LOC__ frozen_deposits expected_deposits in (* Bake until end of first cycle *) - Block.bake_until_cycle_end genesis >>=? fun b -> - Context.Delegate.full_balance (B genesis) account1 >>=? fun full_balance -> - Context.Delegate.current_frozen_deposits (B genesis) account1 - >>=? fun frozen_deposits -> + let* b = Block.bake_until_cycle_end genesis in + let* full_balance = Context.Delegate.full_balance (B genesis) account1 in + let* frozen_deposits = + Context.Delegate.current_frozen_deposits (B genesis) account1 + in let expected_deposits = full_balance /! Int64.of_int (constants.delegation_over_baking_limit + 1) in - Assert.equal_tez ~loc:__LOC__ frozen_deposits expected_deposits >>=? fun () -> + let* () = Assert.equal_tez ~loc:__LOC__ frozen_deposits expected_deposits in (* set deposits limit to balance_percentage out of the balance *) let limit = Test_tez.(full_balance *! Int64.of_int balance_percentage /! 100L) in - Op.set_deposits_limit (B genesis) contract1 (Some limit) >>=? fun operation -> - Block.bake ~policy:(By_account account2) ~operation b >>=? fun b -> - (Context.Delegate.frozen_deposits_limit (B b) account1 >>=? function - | Some set_limit -> Assert.equal_tez ~loc:__LOC__ set_limit limit - | None -> Alcotest.fail "unexpected absence of deposits limit") - >>=? fun () -> + let* operation = Op.set_deposits_limit (B genesis) contract1 (Some limit) in + let* b = Block.bake ~policy:(By_account account2) ~operation b in + let* () = + let* frozen_deposits_limit = + Context.Delegate.frozen_deposits_limit (B b) account1 + in + match frozen_deposits_limit with + | Some set_limit -> Assert.equal_tez ~loc:__LOC__ set_limit limit + | None -> Alcotest.fail "unexpected absence of deposits limit" + in (* the frozen deposits limit affects the active stake for cycles starting with c + - preserved_cycles + 1; the new active stake is taken into account when - computing the frozen deposits for cycle c+1 already, however the user may see - an update to its frozen deposits at cycle c + preserved_cycles + - max_slashing_period at the latest (because up to that cycle the frozen - deposits also depend on the active stake at cycles before cycle c+1). *) + preserved_cycles + 1; the new active stake is taken into account when + computing the frozen deposits for cycle c+1 already, however the user may see + an update to its frozen deposits at cycle c + preserved_cycles + + max_slashing_period at the latest (because up to that cycle the frozen + deposits also depend on the active stake at cycles before cycle c+1). *) let expected_number_of_cycles_with_previous_deposit = constants.preserved_cycles + Constants.max_slashing_period in - Block.bake_until_n_cycle_end - ~policy:(By_account account2) - (expected_number_of_cycles_with_previous_deposit - 1) - b - >>=? fun b -> - Context.Delegate.current_frozen_deposits (B b) account1 - >>=? fun frozen_deposits -> - Assert.not_equal_tez ~loc:__LOC__ frozen_deposits Tez.zero >>=? fun () -> - Block.bake_until_cycle_end ~policy:(By_account account2) b >>=? fun b -> - Context.Delegate.current_frozen_deposits (B b) account1 - >>=? fun frozen_deposits -> + let* b = + Block.bake_until_n_cycle_end + ~policy:(By_account account2) + (expected_number_of_cycles_with_previous_deposit - 1) + b + in + let* frozen_deposits = + Context.Delegate.current_frozen_deposits (B b) account1 + in + let* () = Assert.not_equal_tez ~loc:__LOC__ frozen_deposits Tez.zero in + let* b = Block.bake_until_cycle_end ~policy:(By_account account2) b in + let* frozen_deposits = + Context.Delegate.current_frozen_deposits (B b) account1 + in Assert.equal_tez ~loc:__LOC__ frozen_deposits limit let test_unset_limit () = + let open Lwt_result_syntax in Context.init_with_constants2 constants >>=? fun (genesis, contracts) -> let (contract1, account1), (_contract2, account2) = get_first_2_accounts_contracts contracts in (* set the limit to 0 *) - Op.set_deposits_limit (B genesis) contract1 (Some Tez.zero) - >>=? fun operation -> - Block.bake ~policy:(By_account account2) ~operation genesis >>=? fun b -> - (Context.Delegate.frozen_deposits_limit (B b) account1 >>=? function - | Some set_limit -> Assert.equal_tez ~loc:__LOC__ set_limit Tez.zero - | None -> Alcotest.fail "unexpected absence of deposits limit") - >>=? fun () -> + let* operation = + Op.set_deposits_limit (B genesis) contract1 (Some Tez.zero) + in + let* b = Block.bake ~policy:(By_account account2) ~operation genesis in + let* () = + let frozen_deposits_limit = + Context.Delegate.frozen_deposits_limit (B b) account1 + in + match frozen_deposits_limit with + | Some set_limit -> Assert.equal_tez ~loc:__LOC__ set_limit Tez.zero + | None -> Alcotest.fail "unexpected absence of deposits limit" + in let expected_number_of_cycles_with_previous_deposit = constants.preserved_cycles + Constants.max_slashing_period in - Block.bake_until_n_cycle_end - ~policy:(By_account account2) - expected_number_of_cycles_with_previous_deposit - b - >>=? fun b -> - Context.Delegate.current_frozen_deposits (B b) account1 - >>=? fun frozen_deposits_at_b -> + let* b = + Block.bake_until_n_cycle_end + ~policy:(By_account account2) + expected_number_of_cycles_with_previous_deposit + b + in + let frozen_deposits_at_b = + Context.Delegate.current_frozen_deposits (B b) account1 + in (* after [expected_number_of_cycles_with_previous_deposit] cycles the 0 limit is reflected in the deposit which becomes 0 itself *) - Assert.equal_tez ~loc:__LOC__ frozen_deposits_at_b Tez.zero >>=? fun () -> + let* () = Assert.equal_tez ~loc:__LOC__ frozen_deposits_at_b Tez.zero in (* unset the 0 limit *) - Op.set_deposits_limit (B b) contract1 None >>=? fun operation -> - Block.bake ~policy:(By_account account2) ~operation b >>=? fun b -> - (Context.Delegate.frozen_deposits_limit (B b) account1 >>=? function - | Some _ -> Alcotest.fail "unexpected deposits limit" - | None -> return_unit) - >>=? fun () -> + let* operation = Op.set_deposits_limit (B b) contract1 None in + let* b = Block.bake ~policy:(By_account account2) ~operation b in + let* () = + let* frozen_deposits_limit = + Context.Delegate.frozen_deposits_limit (B b) account1 + in + match frozen_deposits_limit with + | Some _ -> Alcotest.fail "unexpected deposits limit" + | None -> return_unit + in (* removing the 0 limit is visible once the cycle ends *) - Block.bake_until_cycle_end ~policy:(By_account account2) b >>=? fun bfin -> - Context.Delegate.current_frozen_deposits (B bfin) account1 - >>=? fun frozen_deposits_at_bfin -> + let* bfin = Block.bake_until_cycle_end ~policy:(By_account account2) b in + let* frozen_deposits_at_bfin = + Context.Delegate.current_frozen_deposits (B bfin) account1 + in (* without a limit and without manual staking, the new deposit is still zero; note that account1 hasn't baked any block. *) - Assert.equal_tez ~loc:__LOC__ frozen_deposits_at_bfin Tez.zero >>=? fun () -> + let* () = Assert.equal_tez ~loc:__LOC__ frozen_deposits_at_bfin Tez.zero in return_unit let test_deposits_after_stake_removal () = -- GitLab From eead8fae301e81e2ef15227186adb762f43ec9e9 Mon Sep 17 00:00:00 2001 From: Julien Tesson Date: Thu, 12 Oct 2023 16:01:32 +0200 Subject: [PATCH 08/26] Revert "Proto/Tests: remove tests that we may want to bring back" This reverts commit d7cc560bd3f6fa90ce35626dd83b16c91af37ccd. --- .../consensus/test_frozen_deposits.ml | 111 ++++++++++++++++++ 1 file changed, 111 insertions(+) diff --git a/src/proto_alpha/lib_protocol/test/integration/consensus/test_frozen_deposits.ml b/src/proto_alpha/lib_protocol/test/integration/consensus/test_frozen_deposits.ml index f3c94f54fc73..50ffdfd04242 100644 --- a/src/proto_alpha/lib_protocol/test/integration/consensus/test_frozen_deposits.ml +++ b/src/proto_alpha/lib_protocol/test/integration/consensus/test_frozen_deposits.ml @@ -510,6 +510,41 @@ let test_unset_limit () = let* () = Assert.equal_tez ~loc:__LOC__ frozen_deposits_at_bfin Tez.zero in return_unit +let test_cannot_bake_with_zero_deposits_limit () = + Context.init_with_constants2 constants >>=? fun (genesis, contracts) -> + let (contract1, account1), (_contract2, account2) = + get_first_2_accounts_contracts contracts + in + (* N.B. there is no non-zero frozen deposits value for which one cannot bake: + even with a small deposit one can still bake, though with a smaller probability + (because the frozen deposits value impacts the active stake and the active + stake is the one used to determine baking/endorsing rights. *) + Op.set_deposits_limit (B genesis) contract1 (Some Tez.zero) + >>=? fun operation -> + Block.bake ~policy:(By_account account2) ~operation genesis >>=? fun b -> + let expected_number_of_cycles_with_previous_deposit = + constants.preserved_cycles + Constants.max_slashing_period - 1 + in + Block.bake_until_n_cycle_end + ~policy:(By_account account2) + expected_number_of_cycles_with_previous_deposit + b + >>=? fun b -> + Block.bake ~policy:(By_account account1) b >>= fun b1 -> + (* by now, the active stake of account1 is 0 so it no longer has slots, thus it + cannot be a proposer, thus it cannot bake. Precisely, bake fails because + get_next_baker_by_account fails with "No slots found" *) + Assert.error ~loc:__LOC__ b1 (fun _ -> true) >>=? fun () -> + Block.bake_until_cycle_end ~policy:(By_account account2) b >>=? fun b -> + (* after one cycle is passed, the frozen deposit window has passed + and the frozen deposits should now be effectively 0. *) + Context.Delegate.current_frozen_deposits (B b) account1 >>=? fun fd -> + Assert.equal_tez ~loc:__LOC__ fd Tez.zero >>=? fun () -> + Block.bake ~policy:(By_account account1) b >>= fun b1 -> + (* don't know why the zero frozen deposits error is not caught here *) + (* Assert.proto_error_with_info ~loc:__LOC__ b1 "Zero frozen deposits" *) + Assert.error ~loc:__LOC__ b1 (fun _ -> true) + let test_deposits_after_stake_removal () = let open Lwt_result_syntax in let* genesis, contracts = Context.init_with_constants2 constants in @@ -849,6 +884,78 @@ let test_frozen_deposits_with_overdelegation () = let* (_ : Block.t) = loop b cycles_to_bake in return_unit +let test_set_limit_with_overdelegation () = + let constants = {constants with delegation_over_baking_limit = 9} in + Context.init_with_constants2 constants >>=? fun (genesis, contracts) -> + let (contract1, account1), (contract2, account2) = + get_first_2_accounts_contracts contracts + in + (* - [account1] and [account2] will give 80% of their balance to + [new_account] + - [new_account] will overdelegate to [account1] and [account1] will set + its frozen deposits limit to 15% of its stake *) + Context.Delegate.current_frozen_deposits (B genesis) account1 + >>=? fun initial_frozen_deposits -> + Context.Delegate.staking_balance (B genesis) account1 + >>=? fun initial_staking_balance -> + Context.Delegate.staking_balance (B genesis) account2 + >>=? fun initial_staking_balance' -> + let amount = Test_tez.(initial_staking_balance *! 8L /! 10L) in + let amount' = Test_tez.(initial_staking_balance' *! 8L /! 10L) in + let limit = Test_tez.(initial_staking_balance *! 15L /! 100L) in + let new_account = (Account.new_account ()).pkh in + let new_contract = Contract.Implicit new_account in + Op.transaction ~force_reveal:true (B genesis) contract1 new_contract amount + >>=? fun transfer1 -> + Op.transaction ~force_reveal:true (B genesis) contract2 new_contract amount' + >>=? fun transfer2 -> + Block.bake ~operations:[transfer1; transfer2] genesis >>=? fun b -> + Op.set_deposits_limit (B b) contract1 (Some limit) >>=? fun set_deposits -> + Block.bake ~operation:set_deposits b >>=? fun b -> + let expected_new_staking_balance = + Test_tez.(initial_staking_balance -! amount) + in + Context.Delegate.staking_balance (B b) account1 + >>=? fun new_staking_balance -> + Assert.equal_tez ~loc:__LOC__ new_staking_balance expected_new_staking_balance + >>=? fun () -> + let expected_new_staking_balance' = + Test_tez.(initial_staking_balance' -! amount') + in + Context.Delegate.staking_balance (B b) account2 + >>=? fun new_staking_balance' -> + Assert.equal_tez + ~loc:__LOC__ + new_staking_balance' + expected_new_staking_balance' + >>=? fun () -> + Op.delegation ~force_reveal:true (B b) new_contract (Some account1) + >>=? fun delegation -> + Block.bake ~operation:delegation b >>=? fun b -> + (* Finish the cycle. account1's frozen deposits aren't increased + automatically. *) + Block.bake_until_cycle_end b >>=? fun b -> + let expected_new_frozen_deposits = initial_frozen_deposits in + Context.Delegate.current_frozen_deposits (B b) account1 + >>=? fun frozen_deposits -> + Assert.equal_tez ~loc:__LOC__ frozen_deposits expected_new_frozen_deposits + >>=? fun () -> + let cycles_to_bake = + 2 * (constants.preserved_cycles + Constants.max_slashing_period) + in + let rec loop b n = + if n = 0 then return b + else + Block.bake_until_cycle_end ~policy:(By_account account1) b >>=? fun b -> + Context.Delegate.current_frozen_deposits (B b) account1 + >>=? fun frozen_deposits -> + Assert.equal_tez ~loc:__LOC__ frozen_deposits expected_new_frozen_deposits + >>=? fun () -> loop b (pred n) + in + (* Check that frozen deposits do not change for a sufficient period of + time *) + loop b cycles_to_bake >>=? fun (_b : Block.t) -> return_unit + (** This test fails when [to_cycle] in [Delegate.freeze_deposits] is smaller than [new_cycle + preserved_cycles]. *) let test_error_is_thrown_when_smaller_upper_bound_for_frozen_window () = @@ -949,6 +1056,10 @@ let tests = "frozen deposits with overdelegation" `Quick test_frozen_deposits_with_overdelegation; + tztest + "set limit with overdelegation" + `Quick + test_set_limit_with_overdelegation; tztest "error is thrown when the frozen window is smaller" `Quick -- GitLab From aee9892955d433f4f6b239fe3d3940edd2a1d5d6 Mon Sep 17 00:00:00 2001 From: Julien Tesson Date: Thu, 12 Oct 2023 16:14:54 +0200 Subject: [PATCH 09/26] Proto/test: adapt revived tests to new namings --- .../test/integration/consensus/test_frozen_deposits.ml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/proto_alpha/lib_protocol/test/integration/consensus/test_frozen_deposits.ml b/src/proto_alpha/lib_protocol/test/integration/consensus/test_frozen_deposits.ml index 50ffdfd04242..33e31f5fe7ce 100644 --- a/src/proto_alpha/lib_protocol/test/integration/consensus/test_frozen_deposits.ml +++ b/src/proto_alpha/lib_protocol/test/integration/consensus/test_frozen_deposits.ml @@ -403,7 +403,7 @@ let test_set_limit balance_percentage () = Context.Delegate.current_frozen_deposits (B genesis) account1 in let expected_deposits = - full_balance /! Int64.of_int (constants.delegation_over_baking_limit + 1) + full_balance /! Int64.of_int (constants.limit_of_delegation_over_baking + 1) in let* () = Assert.equal_tez ~loc:__LOC__ frozen_deposits expected_deposits in (* Bake until end of first cycle *) @@ -413,7 +413,7 @@ let test_set_limit balance_percentage () = Context.Delegate.current_frozen_deposits (B genesis) account1 in let expected_deposits = - full_balance /! Int64.of_int (constants.delegation_over_baking_limit + 1) + full_balance /! Int64.of_int (constants.limit_of_delegation_over_baking + 1) in let* () = Assert.equal_tez ~loc:__LOC__ frozen_deposits expected_deposits in (* set deposits limit to balance_percentage out of the balance *) @@ -885,7 +885,7 @@ let test_frozen_deposits_with_overdelegation () = return_unit let test_set_limit_with_overdelegation () = - let constants = {constants with delegation_over_baking_limit = 9} in + let constants = {constants with limit_of_delegation_over_baking = 9} in Context.init_with_constants2 constants >>=? fun (genesis, contracts) -> let (contract1, account1), (contract2, account2) = get_first_2_accounts_contracts contracts -- GitLab From 38f9911dde05a81528c36c58a2719d7b552049dc Mon Sep 17 00:00:00 2001 From: Julien Tesson Date: Thu, 12 Oct 2023 17:11:33 +0200 Subject: [PATCH 10/26] Proto/tests: use monadic-let in revived tests --- .../consensus/test_frozen_deposits.ml | 41 ++++++++++--------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/src/proto_alpha/lib_protocol/test/integration/consensus/test_frozen_deposits.ml b/src/proto_alpha/lib_protocol/test/integration/consensus/test_frozen_deposits.ml index 33e31f5fe7ce..07f537fa4246 100644 --- a/src/proto_alpha/lib_protocol/test/integration/consensus/test_frozen_deposits.ml +++ b/src/proto_alpha/lib_protocol/test/integration/consensus/test_frozen_deposits.ml @@ -387,7 +387,7 @@ let test_may_not_bake_again_after_full_deposit_slash () = let test_set_limit balance_percentage () = let open Lwt_result_syntax in - Context.init_with_constants2 constants >>=? fun (genesis, contracts) -> + let* genesis, contracts = Context.init_with_constants2 constants in let (contract1, account1), (_contract2, account2) = get_first_2_accounts_contracts contracts in @@ -457,7 +457,7 @@ let test_set_limit balance_percentage () = let test_unset_limit () = let open Lwt_result_syntax in - Context.init_with_constants2 constants >>=? fun (genesis, contracts) -> + let* genesis, contracts = Context.init_with_constants2 constants in let (contract1, account1), (_contract2, account2) = get_first_2_accounts_contracts contracts in @@ -467,7 +467,7 @@ let test_unset_limit () = in let* b = Block.bake ~policy:(By_account account2) ~operation genesis in let* () = - let frozen_deposits_limit = + let* frozen_deposits_limit = Context.Delegate.frozen_deposits_limit (B b) account1 in match frozen_deposits_limit with @@ -483,7 +483,7 @@ let test_unset_limit () = expected_number_of_cycles_with_previous_deposit b in - let frozen_deposits_at_b = + let* frozen_deposits_at_b = Context.Delegate.current_frozen_deposits (B b) account1 in (* after [expected_number_of_cycles_with_previous_deposit] cycles @@ -511,7 +511,8 @@ let test_unset_limit () = return_unit let test_cannot_bake_with_zero_deposits_limit () = - Context.init_with_constants2 constants >>=? fun (genesis, contracts) -> + let open Lwt_result_syntax in + let* genesis, contracts = Context.init_with_constants2 constants in let (contract1, account1), (_contract2, account2) = get_first_2_accounts_contracts contracts in @@ -519,28 +520,30 @@ let test_cannot_bake_with_zero_deposits_limit () = even with a small deposit one can still bake, though with a smaller probability (because the frozen deposits value impacts the active stake and the active stake is the one used to determine baking/endorsing rights. *) - Op.set_deposits_limit (B genesis) contract1 (Some Tez.zero) - >>=? fun operation -> - Block.bake ~policy:(By_account account2) ~operation genesis >>=? fun b -> + let* operation = + Op.set_deposits_limit (B genesis) contract1 (Some Tez.zero) + in + let* b = Block.bake ~policy:(By_account account2) ~operation genesis in let expected_number_of_cycles_with_previous_deposit = constants.preserved_cycles + Constants.max_slashing_period - 1 in - Block.bake_until_n_cycle_end - ~policy:(By_account account2) - expected_number_of_cycles_with_previous_deposit - b - >>=? fun b -> - Block.bake ~policy:(By_account account1) b >>= fun b1 -> + let* b = + Block.bake_until_n_cycle_end + ~policy:(By_account account2) + expected_number_of_cycles_with_previous_deposit + b + in + let*! b1 = Block.bake ~policy:(By_account account1) b in (* by now, the active stake of account1 is 0 so it no longer has slots, thus it cannot be a proposer, thus it cannot bake. Precisely, bake fails because get_next_baker_by_account fails with "No slots found" *) - Assert.error ~loc:__LOC__ b1 (fun _ -> true) >>=? fun () -> - Block.bake_until_cycle_end ~policy:(By_account account2) b >>=? fun b -> + let* () = Assert.error ~loc:__LOC__ b1 (fun _ -> true) in + let* b = Block.bake_until_cycle_end ~policy:(By_account account2) b in (* after one cycle is passed, the frozen deposit window has passed and the frozen deposits should now be effectively 0. *) - Context.Delegate.current_frozen_deposits (B b) account1 >>=? fun fd -> - Assert.equal_tez ~loc:__LOC__ fd Tez.zero >>=? fun () -> - Block.bake ~policy:(By_account account1) b >>= fun b1 -> + let* fd = Context.Delegate.current_frozen_deposits (B b) account1 in + let* () = Assert.equal_tez ~loc:__LOC__ fd Tez.zero in + let*! b1 = Block.bake ~policy:(By_account account1) b in (* don't know why the zero frozen deposits error is not caught here *) (* Assert.proto_error_with_info ~loc:__LOC__ b1 "Zero frozen deposits" *) Assert.error ~loc:__LOC__ b1 (fun _ -> true) -- GitLab From d07bf03e58b7f4a25ba44f756d40e290dc1053b7 Mon Sep 17 00:00:00 2001 From: Julien Tesson Date: Wed, 8 Nov 2023 18:16:33 +0100 Subject: [PATCH 11/26] proto/test/consensus/frozen_deposit: adapt to set-deposit-limit --- .../consensus/test_frozen_deposits.ml | 25 ++----------------- 1 file changed, 2 insertions(+), 23 deletions(-) diff --git a/src/proto_alpha/lib_protocol/test/integration/consensus/test_frozen_deposits.ml b/src/proto_alpha/lib_protocol/test/integration/consensus/test_frozen_deposits.ml index 07f537fa4246..4d716ef3e008 100644 --- a/src/proto_alpha/lib_protocol/test/integration/consensus/test_frozen_deposits.ml +++ b/src/proto_alpha/lib_protocol/test/integration/consensus/test_frozen_deposits.ml @@ -154,11 +154,8 @@ let test_cannot_bake_with_zero_deposits () = (because the frozen deposits value impacts the active stake and the active stake is the one used to determine baking/attesting rights. *) (* To make account1 have zero deposits, we unstake all its deposits. *) - let* frozen_deposits = - Context.Delegate.current_frozen_deposits (B genesis) account1 - in let* operation = - Adaptive_issuance_helpers.unstake (B genesis) contract1 frozen_deposits + Op.set_deposits_limit (B genesis) contract1 (Some Tez.zero) in let* b = Block.bake ~policy:(By_account account2) ~operation genesis in let expected_number_of_cycles_with_previous_deposit = @@ -430,25 +427,7 @@ let test_set_limit balance_percentage () = | Some set_limit -> Assert.equal_tez ~loc:__LOC__ set_limit limit | None -> Alcotest.fail "unexpected absence of deposits limit" in - (* the frozen deposits limit affects the active stake for cycles starting with c + - preserved_cycles + 1; the new active stake is taken into account when - computing the frozen deposits for cycle c+1 already, however the user may see - an update to its frozen deposits at cycle c + preserved_cycles + - max_slashing_period at the latest (because up to that cycle the frozen - deposits also depend on the active stake at cycles before cycle c+1). *) - let expected_number_of_cycles_with_previous_deposit = - constants.preserved_cycles + Constants.max_slashing_period - in - let* b = - Block.bake_until_n_cycle_end - ~policy:(By_account account2) - (expected_number_of_cycles_with_previous_deposit - 1) - b - in - let* frozen_deposits = - Context.Delegate.current_frozen_deposits (B b) account1 - in - let* () = Assert.not_equal_tez ~loc:__LOC__ frozen_deposits Tez.zero in + (* the frozen deposits limit affects the active stake at cycle end. *) let* b = Block.bake_until_cycle_end ~policy:(By_account account2) b in let* frozen_deposits = Context.Delegate.current_frozen_deposits (B b) account1 -- GitLab From 214936de33b8132945aafc56d50991994638e207 Mon Sep 17 00:00:00 2001 From: Julien Tesson Date: Wed, 8 Nov 2023 18:10:41 +0100 Subject: [PATCH 12/26] proto/test/consensus/frozen_deposit: resurrect simulation test with autostaking deactivated --- .../consensus/test_frozen_deposits.ml | 41 +++++++++++++------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/src/proto_alpha/lib_protocol/test/integration/consensus/test_frozen_deposits.ml b/src/proto_alpha/lib_protocol/test/integration/consensus/test_frozen_deposits.ml index 4d716ef3e008..1e6b8935c62e 100644 --- a/src/proto_alpha/lib_protocol/test/integration/consensus/test_frozen_deposits.ml +++ b/src/proto_alpha/lib_protocol/test/integration/consensus/test_frozen_deposits.ml @@ -215,7 +215,17 @@ let adjust_staking_towards_limit ~limit ~block ~account ~contract = let test_limit_with_overdelegation () = let open Lwt_result_syntax in - let constants = {constants with limit_of_delegation_over_baking = 9} in + let constants = + { + constants with + adaptive_issuance = + { + Default_parameters.constants_test.adaptive_issuance with + autostaking_enable = false; + }; + limit_of_delegation_over_baking = 9; + } + in let* genesis, contracts = Context.init_with_constants2 constants in let (contract1, account1), (contract2, account2) = get_first_2_accounts_contracts contracts @@ -266,7 +276,16 @@ let test_limit_with_overdelegation () = Op.delegation ~force_reveal:true (B b) new_contract (Some account1) in let* b = Block.bake ~operation:delegation b in - let* b = Block.bake_until_cycle_end ~policy:(By_account account1) b in + (* Overdelegation means that now there isn't enough staking, and the + baker who wants to have its stake close to its defined limit + should adjust it. *) + let* b = + adjust_staking_towards_limit + ~block:b + ~account:account1 + ~contract:contract1 + ~limit + in let expected_new_frozen_deposits = limit in let* frozen_deposits = Context.Delegate.current_frozen_deposits (B b) account1 @@ -1020,16 +1039,14 @@ let tests = "frozen deposits with delegation" `Quick test_frozen_deposits_with_delegation; - (* Reactivate with set deposit limit in !10449. *) - (*tztest *) - (* "test cannot bake with zero deposits" *) - (* `Quick *) - (* test_cannot_bake_with_zero_deposits; *) - (* Reactivated in !10449.. *) - (* tztest *) - (* "test simulation of limited staking with overdelegation" *) - (* `Quick *) - (* test_limit_with_overdelegation;*) + tztest + "test cannot bake with zero deposits" + `Quick + test_cannot_bake_with_zero_deposits; + tztest + "test simulation of limited staking with overdelegation" + `Quick + test_limit_with_overdelegation; tztest "test cannot bake again after full deposit slash" `Quick -- GitLab From 36ac33f4b02379cc132b0d7cf13dc892b7cbecf1 Mon Sep 17 00:00:00 2001 From: Julien Tesson Date: Wed, 8 Nov 2023 19:19:52 +0100 Subject: [PATCH 13/26] proto/test/consensus/frozen_deposit: change of error --- .../test/integration/consensus/test_frozen_deposits.ml | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/proto_alpha/lib_protocol/test/integration/consensus/test_frozen_deposits.ml b/src/proto_alpha/lib_protocol/test/integration/consensus/test_frozen_deposits.ml index 1e6b8935c62e..372a6f0f7006 100644 --- a/src/proto_alpha/lib_protocol/test/integration/consensus/test_frozen_deposits.ml +++ b/src/proto_alpha/lib_protocol/test/integration/consensus/test_frozen_deposits.ml @@ -169,13 +169,9 @@ let test_cannot_bake_with_zero_deposits () = in let*! b1 = Block.bake ~policy:(By_account account1) b in (* by now, the active stake of account1 is 0 so it no longer has slots, thus it - cannot be a proposer, thus it cannot bake. Precisely, bake fails because - get_next_baker_by_account fails with "No slots found" *) - let* () = - Assert.error ~loc:__LOC__ b1 (function - | Block.No_slots_found_for _ -> true - | _ -> false) - in + cannot be a proposer, thus it cannot bake. Precisely, bake fails with error + Validate_errors.Consensus.Zero_frozen_deposits*) + let* () = Assert.error ~loc:__LOC__ b1 (fun _ -> true) in let* b = Block.bake_until_cycle_end ~policy:(By_account account2) b in (* after one cycle is passed, the frozen deposit window has passed and the frozen deposits should now be effectively 0. *) -- GitLab From 63be055e0d53e502f1cde6bcdc5fc6d48a2a2ec5 Mon Sep 17 00:00:00 2001 From: Julien Tesson Date: Wed, 8 Nov 2023 19:21:17 +0100 Subject: [PATCH 14/26] proto/test/consensus/frozen_deposit: adapt to new staking mechanism --- .../consensus/test_frozen_deposits.ml | 32 +++++++------------ 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/src/proto_alpha/lib_protocol/test/integration/consensus/test_frozen_deposits.ml b/src/proto_alpha/lib_protocol/test/integration/consensus/test_frozen_deposits.ml index 372a6f0f7006..6676e0d3350d 100644 --- a/src/proto_alpha/lib_protocol/test/integration/consensus/test_frozen_deposits.ml +++ b/src/proto_alpha/lib_protocol/test/integration/consensus/test_frozen_deposits.ml @@ -468,20 +468,12 @@ let test_unset_limit () = | Some set_limit -> Assert.equal_tez ~loc:__LOC__ set_limit Tez.zero | None -> Alcotest.fail "unexpected absence of deposits limit" in - let expected_number_of_cycles_with_previous_deposit = - constants.preserved_cycles + Constants.max_slashing_period - in - let* b = - Block.bake_until_n_cycle_end - ~policy:(By_account account2) - expected_number_of_cycles_with_previous_deposit - b - in + let* b = Block.bake_until_cycle_end ~policy:(By_account account2) b in let* frozen_deposits_at_b = Context.Delegate.current_frozen_deposits (B b) account1 in - (* after [expected_number_of_cycles_with_previous_deposit] cycles - the 0 limit is reflected in the deposit which becomes 0 itself *) + (* after cycle end, the 0 limit is reflected in the deposit which becomes 0 + itself *) let* () = Assert.equal_tez ~loc:__LOC__ frozen_deposits_at_b Tez.zero in (* unset the 0 limit *) let* operation = Op.set_deposits_limit (B b) contract1 None in @@ -499,9 +491,11 @@ let test_unset_limit () = let* frozen_deposits_at_bfin = Context.Delegate.current_frozen_deposits (B bfin) account1 in - (* without a limit and without manual staking, the new deposit is still zero; - note that account1 hasn't baked any block. *) - let* () = Assert.equal_tez ~loc:__LOC__ frozen_deposits_at_bfin Tez.zero in + (* without a limit and with autostaking staking, the new deposit is no more + zero; note that account1 hasn't baked any block. *) + let* () = + Assert.not_equal_tez ~loc:__LOC__ frozen_deposits_at_bfin Tez.zero + in return_unit let test_cannot_bake_with_zero_deposits_limit () = @@ -518,13 +512,13 @@ let test_cannot_bake_with_zero_deposits_limit () = Op.set_deposits_limit (B genesis) contract1 (Some Tez.zero) in let* b = Block.bake ~policy:(By_account account2) ~operation genesis in - let expected_number_of_cycles_with_previous_deposit = + let expected_number_of_cycles_with_rights_from_previous_deposit = constants.preserved_cycles + Constants.max_slashing_period - 1 in let* b = Block.bake_until_n_cycle_end ~policy:(By_account account2) - expected_number_of_cycles_with_previous_deposit + expected_number_of_cycles_with_rights_from_previous_deposit b in let*! b1 = Block.bake ~policy:(By_account account1) b in @@ -891,8 +885,6 @@ let test_set_limit_with_overdelegation () = [new_account] - [new_account] will overdelegate to [account1] and [account1] will set its frozen deposits limit to 15% of its stake *) - Context.Delegate.current_frozen_deposits (B genesis) account1 - >>=? fun initial_frozen_deposits -> Context.Delegate.staking_balance (B genesis) account1 >>=? fun initial_staking_balance -> Context.Delegate.staking_balance (B genesis) account2 @@ -929,10 +921,10 @@ let test_set_limit_with_overdelegation () = Op.delegation ~force_reveal:true (B b) new_contract (Some account1) >>=? fun delegation -> Block.bake ~operation:delegation b >>=? fun b -> - (* Finish the cycle. account1's frozen deposits aren't increased + (* Finish the cycle. account1's frozen deposits are increased automatically. *) Block.bake_until_cycle_end b >>=? fun b -> - let expected_new_frozen_deposits = initial_frozen_deposits in + let expected_new_frozen_deposits = limit in Context.Delegate.current_frozen_deposits (B b) account1 >>=? fun frozen_deposits -> Assert.equal_tez ~loc:__LOC__ frozen_deposits expected_new_frozen_deposits -- GitLab From b62378d9148385d39cf1554e5d2cd2516de2ad5e Mon Sep 17 00:00:00 2001 From: Julien Tesson Date: Wed, 8 Nov 2023 19:42:52 +0100 Subject: [PATCH 15/26] Proto/tests: use monadic-let in revived tests --- .../consensus/test_frozen_deposits.ml | 92 +++++++++++-------- 1 file changed, 55 insertions(+), 37 deletions(-) diff --git a/src/proto_alpha/lib_protocol/test/integration/consensus/test_frozen_deposits.ml b/src/proto_alpha/lib_protocol/test/integration/consensus/test_frozen_deposits.ml index 6676e0d3350d..730d72d79faa 100644 --- a/src/proto_alpha/lib_protocol/test/integration/consensus/test_frozen_deposits.ml +++ b/src/proto_alpha/lib_protocol/test/integration/consensus/test_frozen_deposits.ml @@ -876,8 +876,9 @@ let test_frozen_deposits_with_overdelegation () = return_unit let test_set_limit_with_overdelegation () = + let open Lwt_result_syntax in let constants = {constants with limit_of_delegation_over_baking = 9} in - Context.init_with_constants2 constants >>=? fun (genesis, contracts) -> + let* genesis, contracts = Context.init_with_constants2 constants in let (contract1, account1), (contract2, account2) = get_first_2_accounts_contracts contracts in @@ -885,65 +886,82 @@ let test_set_limit_with_overdelegation () = [new_account] - [new_account] will overdelegate to [account1] and [account1] will set its frozen deposits limit to 15% of its stake *) - Context.Delegate.staking_balance (B genesis) account1 - >>=? fun initial_staking_balance -> - Context.Delegate.staking_balance (B genesis) account2 - >>=? fun initial_staking_balance' -> + let* initial_staking_balance = + Context.Delegate.staking_balance (B genesis) account1 + in + let* initial_staking_balance' = + Context.Delegate.staking_balance (B genesis) account2 + in let amount = Test_tez.(initial_staking_balance *! 8L /! 10L) in let amount' = Test_tez.(initial_staking_balance' *! 8L /! 10L) in let limit = Test_tez.(initial_staking_balance *! 15L /! 100L) in let new_account = (Account.new_account ()).pkh in let new_contract = Contract.Implicit new_account in - Op.transaction ~force_reveal:true (B genesis) contract1 new_contract amount - >>=? fun transfer1 -> - Op.transaction ~force_reveal:true (B genesis) contract2 new_contract amount' - >>=? fun transfer2 -> - Block.bake ~operations:[transfer1; transfer2] genesis >>=? fun b -> - Op.set_deposits_limit (B b) contract1 (Some limit) >>=? fun set_deposits -> - Block.bake ~operation:set_deposits b >>=? fun b -> + let* transfer1 = + Op.transaction ~force_reveal:true (B genesis) contract1 new_contract amount + in + let* transfer2 = + Op.transaction ~force_reveal:true (B genesis) contract2 new_contract amount' + in + let* b = Block.bake ~operations:[transfer1; transfer2] genesis in + let* set_deposits = Op.set_deposits_limit (B b) contract1 (Some limit) in + let* b = Block.bake ~operation:set_deposits b in let expected_new_staking_balance = Test_tez.(initial_staking_balance -! amount) in - Context.Delegate.staking_balance (B b) account1 - >>=? fun new_staking_balance -> - Assert.equal_tez ~loc:__LOC__ new_staking_balance expected_new_staking_balance - >>=? fun () -> + let* new_staking_balance = Context.Delegate.staking_balance (B b) account1 in + let* () = + Assert.equal_tez + ~loc:__LOC__ + new_staking_balance + expected_new_staking_balance + in let expected_new_staking_balance' = Test_tez.(initial_staking_balance' -! amount') in - Context.Delegate.staking_balance (B b) account2 - >>=? fun new_staking_balance' -> - Assert.equal_tez - ~loc:__LOC__ - new_staking_balance' - expected_new_staking_balance' - >>=? fun () -> - Op.delegation ~force_reveal:true (B b) new_contract (Some account1) - >>=? fun delegation -> - Block.bake ~operation:delegation b >>=? fun b -> + let* new_staking_balance' = Context.Delegate.staking_balance (B b) account2 in + let* () = + Assert.equal_tez + ~loc:__LOC__ + new_staking_balance' + expected_new_staking_balance' + in + let* delegation = + Op.delegation ~force_reveal:true (B b) new_contract (Some account1) + in + let* b = Block.bake ~operation:delegation b in (* Finish the cycle. account1's frozen deposits are increased automatically. *) - Block.bake_until_cycle_end b >>=? fun b -> + let* b = Block.bake_until_cycle_end b in let expected_new_frozen_deposits = limit in - Context.Delegate.current_frozen_deposits (B b) account1 - >>=? fun frozen_deposits -> - Assert.equal_tez ~loc:__LOC__ frozen_deposits expected_new_frozen_deposits - >>=? fun () -> + let* frozen_deposits = + Context.Delegate.current_frozen_deposits (B b) account1 + in + let* () = + Assert.equal_tez ~loc:__LOC__ frozen_deposits expected_new_frozen_deposits + in let cycles_to_bake = 2 * (constants.preserved_cycles + Constants.max_slashing_period) in let rec loop b n = if n = 0 then return b else - Block.bake_until_cycle_end ~policy:(By_account account1) b >>=? fun b -> - Context.Delegate.current_frozen_deposits (B b) account1 - >>=? fun frozen_deposits -> - Assert.equal_tez ~loc:__LOC__ frozen_deposits expected_new_frozen_deposits - >>=? fun () -> loop b (pred n) + let* b = Block.bake_until_cycle_end ~policy:(By_account account1) b in + let* frozen_deposits = + Context.Delegate.current_frozen_deposits (B b) account1 + in + let* () = + Assert.equal_tez + ~loc:__LOC__ + frozen_deposits + expected_new_frozen_deposits + in + loop b (pred n) in (* Check that frozen deposits do not change for a sufficient period of time *) - loop b cycles_to_bake >>=? fun (_b : Block.t) -> return_unit + let* (_b : Block.t) = loop b cycles_to_bake in + return_unit (** This test fails when [to_cycle] in [Delegate.freeze_deposits] is smaller than [new_cycle + preserved_cycles]. *) -- GitLab From 9074328817d70231470dc1214cf431f280c2e525 Mon Sep 17 00:00:00 2001 From: Julien Tesson Date: Thu, 9 Nov 2023 10:12:04 +0100 Subject: [PATCH 16/26] tezt/deposits_limit: deactivate on oxford Current tezt constraint do not allow to remove for just one protocol, so we deactivate the test for pre-alpha protocols. This should be reverted once Oxford is reverted. --- tezt/tests/deposits_limit.ml | 4 +++ .../Nairobi- set deposits limit.out | 26 ------------------- .../Nairobi- unset deposits limit.out | 26 ------------------- 3 files changed, 4 insertions(+), 52 deletions(-) delete mode 100644 tezt/tests/expected/deposits_limit.ml/Nairobi- set deposits limit.out delete mode 100644 tezt/tests/expected/deposits_limit.ml/Nairobi- unset deposits limit.out diff --git a/tezt/tests/deposits_limit.ml b/tezt/tests/deposits_limit.ml index 630e90e90ef2..52b06b56dcaa 100644 --- a/tezt/tests/deposits_limit.ml +++ b/tezt/tests/deposits_limit.ml @@ -33,6 +33,8 @@ let test_set_deposits_limit = Protocol.register_regression_test ~__FILE__ ~title:"set deposits limit" + (* TODO: Remove the constraint when oxford1 is removed *) + ~supports:(Protocol.From_protocol 019) ~tags:["deposits_limit"] @@ fun protocol -> let* _, client = Client.init_with_protocol ~protocol `Client () in @@ -45,6 +47,8 @@ let test_unset_deposits_limit = Protocol.register_regression_test ~__FILE__ ~title:"unset deposits limit" + (* TODO: Remove the constraint when oxford1 is removed *) + ~supports:(Protocol.From_protocol 019) ~tags:["deposits_limit"] @@ fun protocol -> let* _, client = Client.init_with_protocol ~protocol `Client () in diff --git a/tezt/tests/expected/deposits_limit.ml/Nairobi- set deposits limit.out b/tezt/tests/expected/deposits_limit.ml/Nairobi- set deposits limit.out deleted file mode 100644 index 8ac64a759156..000000000000 --- a/tezt/tests/expected/deposits_limit.ml/Nairobi- set deposits limit.out +++ /dev/null @@ -1,26 +0,0 @@ -Node is bootstrapped. -Estimated gas: 168.052 units (will add 0 for safety) -Estimated storage: no bytes added -Operation successfully injected in the node. -Operation hash is 'oogPg9fm3Wo8Fub69ehKvyhmefv442MSvvKME7awo3zvZVAsuTo' -NOT waiting for the operation to be included. -Use command - octez-client wait for oogPg9fm3Wo8Fub69ehKvyhmefv442MSvvKME7awo3zvZVAsuTo to be included --confirmations 1 --branch BLockGenesisGenesisGenesisGenesisGenesisf79b5d1CoW2 -and/or an external block explorer to make sure that it has been included. -This sequence of operations was run: - Manager signed operations: - From: tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx - Fee to the baker: ꜩ0.000247 - Expected counter: 1 - Gas limit: 169 - Storage limit: 0 bytes - Balance updates: - tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx ... -ꜩ0.000247 - payload fees(the block proposer) ....... +ꜩ0.000247 - Set deposits limit: - Delegate: tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx - Limit: 1000 - This deposits limit modification was successfully applied - Consumed gas: 167.986 - - diff --git a/tezt/tests/expected/deposits_limit.ml/Nairobi- unset deposits limit.out b/tezt/tests/expected/deposits_limit.ml/Nairobi- unset deposits limit.out deleted file mode 100644 index fc4436f54195..000000000000 --- a/tezt/tests/expected/deposits_limit.ml/Nairobi- unset deposits limit.out +++ /dev/null @@ -1,26 +0,0 @@ -Node is bootstrapped. -Estimated gas: 167.886 units (will add 0 for safety) -Estimated storage: no bytes added -Operation successfully injected in the node. -Operation hash is 'oo1PJ41t8u2VYdoeFVgREH9LKKtASE141H3wWRmKRbuBcshW3hR' -NOT waiting for the operation to be included. -Use command - octez-client wait for oo1PJ41t8u2VYdoeFVgREH9LKKtASE141H3wWRmKRbuBcshW3hR to be included --confirmations 1 --branch BLockGenesisGenesisGenesisGenesisGenesisf79b5d1CoW2 -and/or an external block explorer to make sure that it has been included. -This sequence of operations was run: - Manager signed operations: - From: tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx - Fee to the baker: ꜩ0.000242 - Expected counter: 1 - Gas limit: 168 - Storage limit: 0 bytes - Balance updates: - tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx ... -ꜩ0.000242 - payload fees(the block proposer) ....... +ꜩ0.000242 - Set deposits limit: - Delegate: tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx - Unlimited deposits - This deposits limit modification was successfully applied - Consumed gas: 167.820 - - -- GitLab From 656626d655dd508ad40cda511d9d5f272913b973 Mon Sep 17 00:00:00 2001 From: Julien Tesson Date: Thu, 9 Nov 2023 14:30:38 +0100 Subject: [PATCH 17/26] tezt/concensus_key/regresssion: reset traces --- ... - delegate - consensus - destination).out | 20 +++++++++---------- ...- delegate - consensus -- destination).out | 20 +++++++++---------- ...-- delegate - consensus - destination).out | 20 +++++++++---------- ...- delegate - consensus -- destination).out | 20 +++++++++---------- ...lpha- Test register with consensus key.out | 8 ++++---- ... set consensus key - baker is delegate.out | 12 +++++------ ... consensus key - baker is not delegate.out | 12 +++++------ 7 files changed, 56 insertions(+), 56 deletions(-) diff --git a/tezt/tests/expected/consensus_key.ml/Alpha- Test drain delegate with (baker - delegate - consensus - destination).out b/tezt/tests/expected/consensus_key.ml/Alpha- Test drain delegate with (baker - delegate - consensus - destination).out index ec31d3c9824e..f8c49a647634 100644 --- a/tezt/tests/expected/consensus_key.ml/Alpha- Test drain delegate with (baker - delegate - consensus - destination).out +++ b/tezt/tests/expected/consensus_key.ml/Alpha- Test drain delegate with (baker - delegate - consensus - destination).out @@ -29,8 +29,8 @@ This sequence of operations was run: { "full_balance": "4000000000000", "current_frozen_deposits": "200000000000", "frozen_deposits": "200000000000", "staking_balance": "4000000000000", "delegated_contracts": [ "[PUBLIC_KEY_HASH]" ], - "delegated_balance": "0", "total_delegated_stake": "0", - "staking_denominator": "0", "deactivated": false, "grace_period": 3, + "delegated_balance": "0", "deactivated": false, "grace_period": 3, + "total_delegated_stake": "0", "staking_denominator": "0", "voting_power": "4000000000000", "remaining_proposals": 20, "active_consensus_key": "[PUBLIC_KEY_HASH]" } @@ -38,8 +38,8 @@ This sequence of operations was run: { "full_balance": "4000000333333", "current_frozen_deposits": "200000000000", "frozen_deposits": "200000000000", "staking_balance": "4000000333333", "delegated_contracts": [ "[PUBLIC_KEY_HASH]" ], - "delegated_balance": "0", "total_delegated_stake": "0", - "staking_denominator": "0", "deactivated": false, "grace_period": 3, + "delegated_balance": "0", "deactivated": false, "grace_period": 3, + "total_delegated_stake": "0", "staking_denominator": "0", "voting_power": "4000000000000", "remaining_proposals": 20, "active_consensus_key": "[PUBLIC_KEY_HASH]", "pending_consensus_keys": @@ -49,8 +49,8 @@ This sequence of operations was run: { "full_balance": "4000003298113", "current_frozen_deposits": "200000164906", "frozen_deposits": "200000000000", "staking_balance": "4000003298113", "delegated_contracts": [ "[PUBLIC_KEY_HASH]" ], - "delegated_balance": "0", "total_delegated_stake": "0", - "staking_denominator": "0", "deactivated": false, "grace_period": 3, + "delegated_balance": "0", "deactivated": false, "grace_period": 3, + "total_delegated_stake": "0", "staking_denominator": "0", "voting_power": "4000000000000", "remaining_proposals": 20, "active_consensus_key": "[PUBLIC_KEY_HASH]" } @@ -58,8 +58,8 @@ This sequence of operations was run: { "full_balance": "4000003298113", "current_frozen_deposits": "200000164906", "frozen_deposits": "200000000000", "staking_balance": "4000003298113", "delegated_contracts": [ "[PUBLIC_KEY_HASH]" ], - "delegated_balance": "0", "total_delegated_stake": "0", - "staking_denominator": "0", "deactivated": false, "grace_period": 3, + "delegated_balance": "0", "deactivated": false, "grace_period": 3, + "total_delegated_stake": "0", "staking_denominator": "0", "voting_power": "4000000000000", "remaining_proposals": 20, "active_consensus_key": "[PUBLIC_KEY_HASH]" } @@ -98,8 +98,8 @@ This sequence of operations was run: { "full_balance": "238000528928", "current_frozen_deposits": "200000164906", "frozen_deposits": "200000000000", "staking_balance": "238000528928", "delegated_contracts": [ "[PUBLIC_KEY_HASH]" ], - "delegated_balance": "0", "total_delegated_stake": "0", - "staking_denominator": "0", "deactivated": false, "grace_period": 4, + "delegated_balance": "0", "deactivated": false, "grace_period": 4, + "total_delegated_stake": "0", "staking_denominator": "0", "voting_power": "4000000000000", "remaining_proposals": 20, "active_consensus_key": "[PUBLIC_KEY_HASH]" } diff --git a/tezt/tests/expected/consensus_key.ml/Alpha- Test drain delegate with (baker - delegate - consensus -- destination).out b/tezt/tests/expected/consensus_key.ml/Alpha- Test drain delegate with (baker - delegate - consensus -- destination).out index bd419bc192d1..0e5abdf4f22b 100644 --- a/tezt/tests/expected/consensus_key.ml/Alpha- Test drain delegate with (baker - delegate - consensus -- destination).out +++ b/tezt/tests/expected/consensus_key.ml/Alpha- Test drain delegate with (baker - delegate - consensus -- destination).out @@ -29,8 +29,8 @@ This sequence of operations was run: { "full_balance": "4000000000000", "current_frozen_deposits": "200000000000", "frozen_deposits": "200000000000", "staking_balance": "4000000000000", "delegated_contracts": [ "[PUBLIC_KEY_HASH]" ], - "delegated_balance": "0", "total_delegated_stake": "0", - "staking_denominator": "0", "deactivated": false, "grace_period": 3, + "delegated_balance": "0", "deactivated": false, "grace_period": 3, + "total_delegated_stake": "0", "staking_denominator": "0", "voting_power": "4000000000000", "remaining_proposals": 20, "active_consensus_key": "[PUBLIC_KEY_HASH]" } @@ -38,8 +38,8 @@ This sequence of operations was run: { "full_balance": "4000000333333", "current_frozen_deposits": "200000000000", "frozen_deposits": "200000000000", "staking_balance": "4000000333333", "delegated_contracts": [ "[PUBLIC_KEY_HASH]" ], - "delegated_balance": "0", "total_delegated_stake": "0", - "staking_denominator": "0", "deactivated": false, "grace_period": 3, + "delegated_balance": "0", "deactivated": false, "grace_period": 3, + "total_delegated_stake": "0", "staking_denominator": "0", "voting_power": "4000000000000", "remaining_proposals": 20, "active_consensus_key": "[PUBLIC_KEY_HASH]", "pending_consensus_keys": @@ -49,8 +49,8 @@ This sequence of operations was run: { "full_balance": "4000003298113", "current_frozen_deposits": "200000164906", "frozen_deposits": "200000000000", "staking_balance": "4000003298113", "delegated_contracts": [ "[PUBLIC_KEY_HASH]" ], - "delegated_balance": "0", "total_delegated_stake": "0", - "staking_denominator": "0", "deactivated": false, "grace_period": 3, + "delegated_balance": "0", "deactivated": false, "grace_period": 3, + "total_delegated_stake": "0", "staking_denominator": "0", "voting_power": "4000000000000", "remaining_proposals": 20, "active_consensus_key": "[PUBLIC_KEY_HASH]" } @@ -58,8 +58,8 @@ This sequence of operations was run: { "full_balance": "4000003298113", "current_frozen_deposits": "200000164906", "frozen_deposits": "200000000000", "staking_balance": "4000003298113", "delegated_contracts": [ "[PUBLIC_KEY_HASH]" ], - "delegated_balance": "0", "total_delegated_stake": "0", - "staking_denominator": "0", "deactivated": false, "grace_period": 3, + "delegated_balance": "0", "deactivated": false, "grace_period": 3, + "total_delegated_stake": "0", "staking_denominator": "0", "voting_power": "4000000000000", "remaining_proposals": 20, "active_consensus_key": "[PUBLIC_KEY_HASH]" } @@ -98,8 +98,8 @@ This sequence of operations was run: { "full_balance": "238000528928", "current_frozen_deposits": "200000164906", "frozen_deposits": "200000000000", "staking_balance": "238000528928", "delegated_contracts": [ "[PUBLIC_KEY_HASH]" ], - "delegated_balance": "0", "total_delegated_stake": "0", - "staking_denominator": "0", "deactivated": false, "grace_period": 4, + "delegated_balance": "0", "deactivated": false, "grace_period": 4, + "total_delegated_stake": "0", "staking_denominator": "0", "voting_power": "4000000000000", "remaining_proposals": 20, "active_consensus_key": "[PUBLIC_KEY_HASH]" } diff --git a/tezt/tests/expected/consensus_key.ml/Alpha- Test drain delegate with (baker -- delegate - consensus - destination).out b/tezt/tests/expected/consensus_key.ml/Alpha- Test drain delegate with (baker -- delegate - consensus - destination).out index e70f9077d949..9fd4b853d619 100644 --- a/tezt/tests/expected/consensus_key.ml/Alpha- Test drain delegate with (baker -- delegate - consensus - destination).out +++ b/tezt/tests/expected/consensus_key.ml/Alpha- Test drain delegate with (baker -- delegate - consensus - destination).out @@ -29,8 +29,8 @@ This sequence of operations was run: { "full_balance": "4000000000000", "current_frozen_deposits": "200000000000", "frozen_deposits": "200000000000", "staking_balance": "4000000000000", "delegated_contracts": [ "[PUBLIC_KEY_HASH]" ], - "delegated_balance": "0", "total_delegated_stake": "0", - "staking_denominator": "0", "deactivated": false, "grace_period": 3, + "delegated_balance": "0", "deactivated": false, "grace_period": 3, + "total_delegated_stake": "0", "staking_denominator": "0", "voting_power": "4000000000000", "remaining_proposals": 20, "active_consensus_key": "[PUBLIC_KEY_HASH]" } @@ -38,8 +38,8 @@ This sequence of operations was run: { "full_balance": "3999999999716", "current_frozen_deposits": "200000000000", "frozen_deposits": "200000000000", "staking_balance": "3999999999716", "delegated_contracts": [ "[PUBLIC_KEY_HASH]" ], - "delegated_balance": "0", "total_delegated_stake": "0", - "staking_denominator": "0", "deactivated": false, "grace_period": 3, + "delegated_balance": "0", "deactivated": false, "grace_period": 3, + "total_delegated_stake": "0", "staking_denominator": "0", "voting_power": "4000000000000", "remaining_proposals": 20, "active_consensus_key": "[PUBLIC_KEY_HASH]", "pending_consensus_keys": @@ -49,8 +49,8 @@ This sequence of operations was run: { "full_balance": "4000002964496", "current_frozen_deposits": "200000148225", "frozen_deposits": "200000000000", "staking_balance": "4000002964496", "delegated_contracts": [ "[PUBLIC_KEY_HASH]" ], - "delegated_balance": "0", "total_delegated_stake": "0", - "staking_denominator": "0", "deactivated": false, "grace_period": 3, + "delegated_balance": "0", "deactivated": false, "grace_period": 3, + "total_delegated_stake": "0", "staking_denominator": "0", "voting_power": "4000000000000", "remaining_proposals": 20, "active_consensus_key": "[PUBLIC_KEY_HASH]" } @@ -58,8 +58,8 @@ This sequence of operations was run: { "full_balance": "4000002964496", "current_frozen_deposits": "200000148225", "frozen_deposits": "200000000000", "staking_balance": "4000002964496", "delegated_contracts": [ "[PUBLIC_KEY_HASH]" ], - "delegated_balance": "0", "total_delegated_stake": "0", - "staking_denominator": "0", "deactivated": false, "grace_period": 3, + "delegated_balance": "0", "deactivated": false, "grace_period": 3, + "total_delegated_stake": "0", "staking_denominator": "0", "voting_power": "4000000000000", "remaining_proposals": 20, "active_consensus_key": "[PUBLIC_KEY_HASH]" } @@ -98,8 +98,8 @@ This sequence of operations was run: { "full_balance": "238000509078", "current_frozen_deposits": "200000148225", "frozen_deposits": "200000000000", "staking_balance": "238000509078", "delegated_contracts": [ "[PUBLIC_KEY_HASH]" ], - "delegated_balance": "0", "total_delegated_stake": "0", - "staking_denominator": "0", "deactivated": false, "grace_period": 4, + "delegated_balance": "0", "deactivated": false, "grace_period": 4, + "total_delegated_stake": "0", "staking_denominator": "0", "voting_power": "4000000000000", "remaining_proposals": 20, "active_consensus_key": "[PUBLIC_KEY_HASH]" } diff --git a/tezt/tests/expected/consensus_key.ml/Alpha- Test drain delegate with (baker -- delegate - consensus -- destination).out b/tezt/tests/expected/consensus_key.ml/Alpha- Test drain delegate with (baker -- delegate - consensus -- destination).out index 48749a41b404..dabfcdcd0321 100644 --- a/tezt/tests/expected/consensus_key.ml/Alpha- Test drain delegate with (baker -- delegate - consensus -- destination).out +++ b/tezt/tests/expected/consensus_key.ml/Alpha- Test drain delegate with (baker -- delegate - consensus -- destination).out @@ -29,8 +29,8 @@ This sequence of operations was run: { "full_balance": "4000000000000", "current_frozen_deposits": "200000000000", "frozen_deposits": "200000000000", "staking_balance": "4000000000000", "delegated_contracts": [ "[PUBLIC_KEY_HASH]" ], - "delegated_balance": "0", "total_delegated_stake": "0", - "staking_denominator": "0", "deactivated": false, "grace_period": 3, + "delegated_balance": "0", "deactivated": false, "grace_period": 3, + "total_delegated_stake": "0", "staking_denominator": "0", "voting_power": "4000000000000", "remaining_proposals": 20, "active_consensus_key": "[PUBLIC_KEY_HASH]" } @@ -38,8 +38,8 @@ This sequence of operations was run: { "full_balance": "3999999999716", "current_frozen_deposits": "200000000000", "frozen_deposits": "200000000000", "staking_balance": "3999999999716", "delegated_contracts": [ "[PUBLIC_KEY_HASH]" ], - "delegated_balance": "0", "total_delegated_stake": "0", - "staking_denominator": "0", "deactivated": false, "grace_period": 3, + "delegated_balance": "0", "deactivated": false, "grace_period": 3, + "total_delegated_stake": "0", "staking_denominator": "0", "voting_power": "4000000000000", "remaining_proposals": 20, "active_consensus_key": "[PUBLIC_KEY_HASH]", "pending_consensus_keys": @@ -49,8 +49,8 @@ This sequence of operations was run: { "full_balance": "4000002964496", "current_frozen_deposits": "200000148225", "frozen_deposits": "200000000000", "staking_balance": "4000002964496", "delegated_contracts": [ "[PUBLIC_KEY_HASH]" ], - "delegated_balance": "0", "total_delegated_stake": "0", - "staking_denominator": "0", "deactivated": false, "grace_period": 3, + "delegated_balance": "0", "deactivated": false, "grace_period": 3, + "total_delegated_stake": "0", "staking_denominator": "0", "voting_power": "4000000000000", "remaining_proposals": 20, "active_consensus_key": "[PUBLIC_KEY_HASH]" } @@ -58,8 +58,8 @@ This sequence of operations was run: { "full_balance": "4000002964496", "current_frozen_deposits": "200000148225", "frozen_deposits": "200000000000", "staking_balance": "4000002964496", "delegated_contracts": [ "[PUBLIC_KEY_HASH]" ], - "delegated_balance": "0", "total_delegated_stake": "0", - "staking_denominator": "0", "deactivated": false, "grace_period": 3, + "delegated_balance": "0", "deactivated": false, "grace_period": 3, + "total_delegated_stake": "0", "staking_denominator": "0", "voting_power": "4000000000000", "remaining_proposals": 20, "active_consensus_key": "[PUBLIC_KEY_HASH]" } @@ -98,8 +98,8 @@ This sequence of operations was run: { "full_balance": "238000509078", "current_frozen_deposits": "200000148225", "frozen_deposits": "200000000000", "staking_balance": "238000509078", "delegated_contracts": [ "[PUBLIC_KEY_HASH]" ], - "delegated_balance": "0", "total_delegated_stake": "0", - "staking_denominator": "0", "deactivated": false, "grace_period": 4, + "delegated_balance": "0", "deactivated": false, "grace_period": 4, + "total_delegated_stake": "0", "staking_denominator": "0", "voting_power": "4000000000000", "remaining_proposals": 20, "active_consensus_key": "[PUBLIC_KEY_HASH]" } diff --git a/tezt/tests/expected/consensus_key.ml/Alpha- Test register with consensus key.out b/tezt/tests/expected/consensus_key.ml/Alpha- Test register with consensus key.out index 54d39cd5d875..3259d73cdb5f 100644 --- a/tezt/tests/expected/consensus_key.ml/Alpha- Test register with consensus key.out +++ b/tezt/tests/expected/consensus_key.ml/Alpha- Test register with consensus key.out @@ -64,8 +64,8 @@ This sequence of operations was run: { "full_balance": "999999999385", "current_frozen_deposits": "0", "frozen_deposits": "0", "staking_balance": "999999999385", "delegated_contracts": [ "[PUBLIC_KEY_HASH]" ], - "delegated_balance": "0", "total_delegated_stake": "0", - "staking_denominator": "0", "deactivated": false, "grace_period": 3, + "delegated_balance": "0", "deactivated": false, "grace_period": 3, + "total_delegated_stake": "0", "staking_denominator": "0", "active_consensus_key": "[PUBLIC_KEY_HASH]", "pending_consensus_keys": [ { "cycle": 2, "pkh": "[PUBLIC_KEY_HASH]" } ] } @@ -77,6 +77,6 @@ This sequence of operations was run: { "full_balance": "999999999385", "current_frozen_deposits": "49999999970", "frozen_deposits": "0", "staking_balance": "999999999385", "delegated_contracts": [ "[PUBLIC_KEY_HASH]" ], - "delegated_balance": "0", "total_delegated_stake": "0", - "staking_denominator": "0", "deactivated": false, "grace_period": 3, + "delegated_balance": "0", "deactivated": false, "grace_period": 3, + "total_delegated_stake": "0", "staking_denominator": "0", "active_consensus_key": "[PUBLIC_KEY_HASH]" } diff --git a/tezt/tests/expected/consensus_key.ml/Alpha- Test set consensus key - baker is delegate.out b/tezt/tests/expected/consensus_key.ml/Alpha- Test set consensus key - baker is delegate.out index bd1aafcc1ff8..3a0d2ea180d0 100644 --- a/tezt/tests/expected/consensus_key.ml/Alpha- Test set consensus key - baker is delegate.out +++ b/tezt/tests/expected/consensus_key.ml/Alpha- Test set consensus key - baker is delegate.out @@ -29,8 +29,8 @@ This sequence of operations was run: { "full_balance": "4000000000000", "current_frozen_deposits": "200000000000", "frozen_deposits": "200000000000", "staking_balance": "4000000000000", "delegated_contracts": [ "[PUBLIC_KEY_HASH]" ], - "delegated_balance": "0", "total_delegated_stake": "0", - "staking_denominator": "0", "deactivated": false, "grace_period": 3, + "delegated_balance": "0", "deactivated": false, "grace_period": 3, + "total_delegated_stake": "0", "staking_denominator": "0", "voting_power": "4000000000000", "remaining_proposals": 20, "active_consensus_key": "[PUBLIC_KEY_HASH]" } @@ -38,8 +38,8 @@ This sequence of operations was run: { "full_balance": "4000000333333", "current_frozen_deposits": "200000000000", "frozen_deposits": "200000000000", "staking_balance": "4000000333333", "delegated_contracts": [ "[PUBLIC_KEY_HASH]" ], - "delegated_balance": "0", "total_delegated_stake": "0", - "staking_denominator": "0", "deactivated": false, "grace_period": 3, + "delegated_balance": "0", "deactivated": false, "grace_period": 3, + "total_delegated_stake": "0", "staking_denominator": "0", "voting_power": "4000000000000", "remaining_proposals": 20, "active_consensus_key": "[PUBLIC_KEY_HASH]", "pending_consensus_keys": @@ -49,7 +49,7 @@ This sequence of operations was run: { "full_balance": "4000003298113", "current_frozen_deposits": "200000164906", "frozen_deposits": "200000000000", "staking_balance": "4000003298113", "delegated_contracts": [ "[PUBLIC_KEY_HASH]" ], - "delegated_balance": "0", "total_delegated_stake": "0", - "staking_denominator": "0", "deactivated": false, "grace_period": 3, + "delegated_balance": "0", "deactivated": false, "grace_period": 3, + "total_delegated_stake": "0", "staking_denominator": "0", "voting_power": "4000000000000", "remaining_proposals": 20, "active_consensus_key": "[PUBLIC_KEY_HASH]" } diff --git a/tezt/tests/expected/consensus_key.ml/Alpha- Test set consensus key - baker is not delegate.out b/tezt/tests/expected/consensus_key.ml/Alpha- Test set consensus key - baker is not delegate.out index fcf568c02721..c4a3c8433dfe 100644 --- a/tezt/tests/expected/consensus_key.ml/Alpha- Test set consensus key - baker is not delegate.out +++ b/tezt/tests/expected/consensus_key.ml/Alpha- Test set consensus key - baker is not delegate.out @@ -29,8 +29,8 @@ This sequence of operations was run: { "full_balance": "4000000000000", "current_frozen_deposits": "200000000000", "frozen_deposits": "200000000000", "staking_balance": "4000000000000", "delegated_contracts": [ "[PUBLIC_KEY_HASH]" ], - "delegated_balance": "0", "total_delegated_stake": "0", - "staking_denominator": "0", "deactivated": false, "grace_period": 3, + "delegated_balance": "0", "deactivated": false, "grace_period": 3, + "total_delegated_stake": "0", "staking_denominator": "0", "voting_power": "4000000000000", "remaining_proposals": 20, "active_consensus_key": "[PUBLIC_KEY_HASH]" } @@ -38,8 +38,8 @@ This sequence of operations was run: { "full_balance": "3999999999716", "current_frozen_deposits": "200000000000", "frozen_deposits": "200000000000", "staking_balance": "3999999999716", "delegated_contracts": [ "[PUBLIC_KEY_HASH]" ], - "delegated_balance": "0", "total_delegated_stake": "0", - "staking_denominator": "0", "deactivated": false, "grace_period": 3, + "delegated_balance": "0", "deactivated": false, "grace_period": 3, + "total_delegated_stake": "0", "staking_denominator": "0", "voting_power": "4000000000000", "remaining_proposals": 20, "active_consensus_key": "[PUBLIC_KEY_HASH]", "pending_consensus_keys": @@ -49,7 +49,7 @@ This sequence of operations was run: { "full_balance": "4000002964496", "current_frozen_deposits": "200000148225", "frozen_deposits": "200000000000", "staking_balance": "4000002964496", "delegated_contracts": [ "[PUBLIC_KEY_HASH]" ], - "delegated_balance": "0", "total_delegated_stake": "0", - "staking_denominator": "0", "deactivated": false, "grace_period": 3, + "delegated_balance": "0", "deactivated": false, "grace_period": 3, + "total_delegated_stake": "0", "staking_denominator": "0", "voting_power": "4000000000000", "remaining_proposals": 20, "active_consensus_key": "[PUBLIC_KEY_HASH]" } -- GitLab From 0e5fe18d803bcab77d594921ec4a9c691274a008 Mon Sep 17 00:00:00 2001 From: Julien Tesson Date: Thu, 9 Nov 2023 15:08:36 +0100 Subject: [PATCH 18/26] tezt/test/RPC: reset regression --- .../Alpha- (mode client) RPC regression tests- delegates.out | 4 ++-- .../Alpha- (mode light) RPC regression tests- delegates.out | 4 ++-- .../Alpha- (mode proxy) RPC regression tests- delegates.out | 4 ++-- ...proxy_server_data_dir) RPC regression tests- delegates.out | 4 ++-- ...mode proxy_server_rpc) RPC regression tests- delegates.out | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/tezt/tests/expected/RPC_test.ml/Alpha- (mode client) RPC regression tests- delegates.out b/tezt/tests/expected/RPC_test.ml/Alpha- (mode client) RPC regression tests- delegates.out index d2c658c332c7..9528e8733acd 100644 --- a/tezt/tests/expected/RPC_test.ml/Alpha- (mode client) RPC regression tests- delegates.out +++ b/tezt/tests/expected/RPC_test.ml/Alpha- (mode client) RPC regression tests- delegates.out @@ -20,8 +20,8 @@ { "full_balance": "4000000000000", "current_frozen_deposits": "200000000000", "frozen_deposits": "200000000000", "staking_balance": "4000000000000", "delegated_contracts": [ "[PUBLIC_KEY_HASH]" ], - "delegated_balance": "0", "total_delegated_stake": "0", - "staking_denominator": "0", "deactivated": false, "grace_period": 5, + "delegated_balance": "0", "deactivated": false, "grace_period": 5, + "total_delegated_stake": "0", "staking_denominator": "0", "voting_power": "4000000000000", "remaining_proposals": 20, "active_consensus_key": "[PUBLIC_KEY_HASH]" } diff --git a/tezt/tests/expected/RPC_test.ml/Alpha- (mode light) RPC regression tests- delegates.out b/tezt/tests/expected/RPC_test.ml/Alpha- (mode light) RPC regression tests- delegates.out index 305ebe8b50b7..06e165d99971 100644 --- a/tezt/tests/expected/RPC_test.ml/Alpha- (mode light) RPC regression tests- delegates.out +++ b/tezt/tests/expected/RPC_test.ml/Alpha- (mode light) RPC regression tests- delegates.out @@ -20,8 +20,8 @@ { "full_balance": "4000000000000", "current_frozen_deposits": "200000000000", "frozen_deposits": "200000000000", "staking_balance": "4000000000000", "delegated_contracts": [ "[PUBLIC_KEY_HASH]" ], - "delegated_balance": "0", "total_delegated_stake": "0", - "staking_denominator": "0", "deactivated": false, "grace_period": 5, + "delegated_balance": "0", "deactivated": false, "grace_period": 5, + "total_delegated_stake": "0", "staking_denominator": "0", "voting_power": "4000000000000", "remaining_proposals": 20, "active_consensus_key": "[PUBLIC_KEY_HASH]" } diff --git a/tezt/tests/expected/RPC_test.ml/Alpha- (mode proxy) RPC regression tests- delegates.out b/tezt/tests/expected/RPC_test.ml/Alpha- (mode proxy) RPC regression tests- delegates.out index 141835afb4c4..0e3cf04b9dda 100644 --- a/tezt/tests/expected/RPC_test.ml/Alpha- (mode proxy) RPC regression tests- delegates.out +++ b/tezt/tests/expected/RPC_test.ml/Alpha- (mode proxy) RPC regression tests- delegates.out @@ -20,8 +20,8 @@ { "full_balance": "4000000000000", "current_frozen_deposits": "200000000000", "frozen_deposits": "200000000000", "staking_balance": "4000000000000", "delegated_contracts": [ "[PUBLIC_KEY_HASH]" ], - "delegated_balance": "0", "total_delegated_stake": "0", - "staking_denominator": "0", "deactivated": false, "grace_period": 5, + "delegated_balance": "0", "deactivated": false, "grace_period": 5, + "total_delegated_stake": "0", "staking_denominator": "0", "voting_power": "4000000000000", "remaining_proposals": 20, "active_consensus_key": "[PUBLIC_KEY_HASH]" } diff --git a/tezt/tests/expected/RPC_test.ml/Alpha- (mode proxy_server_data_dir) RPC regression tests- delegates.out b/tezt/tests/expected/RPC_test.ml/Alpha- (mode proxy_server_data_dir) RPC regression tests- delegates.out index d2c658c332c7..9528e8733acd 100644 --- a/tezt/tests/expected/RPC_test.ml/Alpha- (mode proxy_server_data_dir) RPC regression tests- delegates.out +++ b/tezt/tests/expected/RPC_test.ml/Alpha- (mode proxy_server_data_dir) RPC regression tests- delegates.out @@ -20,8 +20,8 @@ { "full_balance": "4000000000000", "current_frozen_deposits": "200000000000", "frozen_deposits": "200000000000", "staking_balance": "4000000000000", "delegated_contracts": [ "[PUBLIC_KEY_HASH]" ], - "delegated_balance": "0", "total_delegated_stake": "0", - "staking_denominator": "0", "deactivated": false, "grace_period": 5, + "delegated_balance": "0", "deactivated": false, "grace_period": 5, + "total_delegated_stake": "0", "staking_denominator": "0", "voting_power": "4000000000000", "remaining_proposals": 20, "active_consensus_key": "[PUBLIC_KEY_HASH]" } diff --git a/tezt/tests/expected/RPC_test.ml/Alpha- (mode proxy_server_rpc) RPC regression tests- delegates.out b/tezt/tests/expected/RPC_test.ml/Alpha- (mode proxy_server_rpc) RPC regression tests- delegates.out index d2c658c332c7..9528e8733acd 100644 --- a/tezt/tests/expected/RPC_test.ml/Alpha- (mode proxy_server_rpc) RPC regression tests- delegates.out +++ b/tezt/tests/expected/RPC_test.ml/Alpha- (mode proxy_server_rpc) RPC regression tests- delegates.out @@ -20,8 +20,8 @@ { "full_balance": "4000000000000", "current_frozen_deposits": "200000000000", "frozen_deposits": "200000000000", "staking_balance": "4000000000000", "delegated_contracts": [ "[PUBLIC_KEY_HASH]" ], - "delegated_balance": "0", "total_delegated_stake": "0", - "staking_denominator": "0", "deactivated": false, "grace_period": 5, + "delegated_balance": "0", "deactivated": false, "grace_period": 5, + "total_delegated_stake": "0", "staking_denominator": "0", "voting_power": "4000000000000", "remaining_proposals": 20, "active_consensus_key": "[PUBLIC_KEY_HASH]" } -- GitLab From 867cf51b593168a09b6e156912abfe9e901c76a0 Mon Sep 17 00:00:00 2001 From: Julien Tesson Date: Thu, 9 Nov 2023 21:07:14 +0100 Subject: [PATCH 19/26] proto/staking: add strictness parameter --- src/proto_alpha/lib_protocol/alpha_context.mli | 1 + src/proto_alpha/lib_protocol/apply.ml | 2 +- src/proto_alpha/lib_protocol/delegate_cycles.ml | 7 ++++++- src/proto_alpha/lib_protocol/staking.ml | 3 ++- src/proto_alpha/lib_protocol/staking.mli | 1 + 5 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/proto_alpha/lib_protocol/alpha_context.mli b/src/proto_alpha/lib_protocol/alpha_context.mli index c3dcb3429971..a4046ab24443 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/alpha_context.mli @@ -2425,6 +2425,7 @@ module Staking : sig to [delegate]. *) val stake : context -> + amount_strictness:[`Exact] -> sender:public_key_hash -> delegate:public_key_hash -> Tez.t -> diff --git a/src/proto_alpha/lib_protocol/apply.ml b/src/proto_alpha/lib_protocol/apply.ml index db088b6ea89e..9d6268621229 100644 --- a/src/proto_alpha/lib_protocol/apply.ml +++ b/src/proto_alpha/lib_protocol/apply.ml @@ -407,7 +407,7 @@ let apply_stake ~ctxt ~sender ~amount ~destination ~before_operation = error_when forbidden Staking_to_delegate_that_refuses_external_staking in let* ctxt, balance_updates = - Staking.stake ctxt ~sender ~delegate amount + Staking.stake ctxt ~amount_strictness:`Exact ~sender ~delegate amount in (* Since [delegate] is an already existing delegate, it is already allocated. *) let allocated_destination_contract = false in diff --git a/src/proto_alpha/lib_protocol/delegate_cycles.ml b/src/proto_alpha/lib_protocol/delegate_cycles.ml index 4c665fb95b7b..c3b5d9faad49 100644 --- a/src/proto_alpha/lib_protocol/delegate_cycles.ml +++ b/src/proto_alpha/lib_protocol/delegate_cycles.ml @@ -149,7 +149,12 @@ let adjust_frozen_stakes ctxt : maximal_available_for_staking ctxt ~delegate in let to_stake = Tez_repr.min optimal_to_stake available_to_stake in - Staking.stake ctxt ~sender:delegate ~delegate to_stake + Staking.stake + ctxt + ~amount_strictness:`Exact + ~sender:delegate + ~delegate + to_stake else if Tez_repr.(optimal_frozen < own_frozen) then let*? to_unstake = Tez_repr.(own_frozen -? optimal_frozen) in Staking.request_unstake diff --git a/src/proto_alpha/lib_protocol/staking.ml b/src/proto_alpha/lib_protocol/staking.ml index 84f076a373cc..93db1ae0a0ac 100644 --- a/src/proto_alpha/lib_protocol/staking.ml +++ b/src/proto_alpha/lib_protocol/staking.ml @@ -242,7 +242,7 @@ let stake_from_unstake_for_delegate ctxt ~delegate ~unfinalizable_requests_opt in return (ctxt, balance_updates, remaining_amount_to_transfer) -let stake ctxt ~sender ~delegate amount = +let stake ctxt ~amount_strictness ~sender ~delegate amount = let open Lwt_result_syntax in let check_unfinalizable ctxt Unstake_requests_storage.{delegate = unstake_delegate; requests} = @@ -258,6 +258,7 @@ let stake ctxt ~sender ~delegate amount = let* ctxt, finalize_balance_updates, unfinalizable_requests_opt = finalize_unstake_and_check ~check_unfinalizable ctxt sender_contract in + let amount = match amount_strictness with `Exact -> amount in let* ctxt, stake_balance_updates1, amount_from_liquid = if Signature.Public_key_hash.(sender <> delegate) then let* ctxt, stake_balance_updates_pseudotoken = diff --git a/src/proto_alpha/lib_protocol/staking.mli b/src/proto_alpha/lib_protocol/staking.mli index a84e022782a6..ef42a141c2d0 100644 --- a/src/proto_alpha/lib_protocol/staking.mli +++ b/src/proto_alpha/lib_protocol/staking.mli @@ -27,6 +27,7 @@ to [delegate]. *) val stake : Raw_context.t -> + amount_strictness:[`Exact] -> sender:Signature.Public_key_hash.t -> delegate:Signature.public_key_hash -> Tez_repr.t -> -- GitLab From 4190c7059131398da681cb99728d442774dfcfc8 Mon Sep 17 00:00:00 2001 From: Julien Tesson Date: Thu, 9 Nov 2023 21:38:23 +0100 Subject: [PATCH 20/26] proto/staking: add best_effort staking and use for autostaking --- .../lib_protocol/alpha_context.mli | 2 +- .../lib_protocol/delegate_cycles.ml | 11 +-- src/proto_alpha/lib_protocol/staking.ml | 70 ++++++++++++++----- src/proto_alpha/lib_protocol/staking.mli | 2 +- 4 files changed, 55 insertions(+), 30 deletions(-) diff --git a/src/proto_alpha/lib_protocol/alpha_context.mli b/src/proto_alpha/lib_protocol/alpha_context.mli index a4046ab24443..3ca5f6c34ae9 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/alpha_context.mli @@ -2425,7 +2425,7 @@ module Staking : sig to [delegate]. *) val stake : context -> - amount_strictness:[`Exact] -> + amount_strictness:[`Best_effort | `Exact] -> sender:public_key_hash -> delegate:public_key_hash -> Tez.t -> diff --git a/src/proto_alpha/lib_protocol/delegate_cycles.ml b/src/proto_alpha/lib_protocol/delegate_cycles.ml index c3b5d9faad49..bff194fdf79d 100644 --- a/src/proto_alpha/lib_protocol/delegate_cycles.ml +++ b/src/proto_alpha/lib_protocol/delegate_cycles.ml @@ -114,9 +114,6 @@ let distribute_attesting_rewards ctxt last_cycle unrevealed_nonces = (ctxt, []) delegates -let maximal_available_for_staking ctxt ~delegate = - Contract_storage.get_balance ctxt (Implicit delegate) - let adjust_frozen_stakes ctxt : (Raw_context.t * Receipt_repr.balance_updates) tzresult Lwt.t = let open Lwt_result_syntax in @@ -145,16 +142,12 @@ let adjust_frozen_stakes ctxt : let* ctxt, new_balance_updates = if Tez_repr.(optimal_frozen > own_frozen) then let*? optimal_to_stake = Tez_repr.(optimal_frozen -? own_frozen) in - let* available_to_stake = - maximal_available_for_staking ctxt ~delegate - in - let to_stake = Tez_repr.min optimal_to_stake available_to_stake in Staking.stake ctxt - ~amount_strictness:`Exact + ~amount_strictness:`Best_effort ~sender:delegate ~delegate - to_stake + optimal_to_stake else if Tez_repr.(optimal_frozen < own_frozen) then let*? to_unstake = Tez_repr.(own_frozen -? optimal_frozen) in Staking.request_unstake diff --git a/src/proto_alpha/lib_protocol/staking.ml b/src/proto_alpha/lib_protocol/staking.ml index 93db1ae0a0ac..faf41627422a 100644 --- a/src/proto_alpha/lib_protocol/staking.ml +++ b/src/proto_alpha/lib_protocol/staking.ml @@ -109,6 +109,26 @@ let finalize_unstake ctxt contract = in return (ctxt, balance_updates) +let can_stake_from_unstake ctxt ~delegate = + let open Lwt_result_syntax in + let* slashing_history_opt = + Storage.Contract.Slashed_deposits.find + ctxt + (Contract_repr.Implicit delegate) + in + let slashing_history = Option.value slashing_history_opt ~default:[] in + let current_cycle = (Raw_context.current_level ctxt).cycle in + let preserved_cycles = Constants_storage.preserved_cycles ctxt in + let oldest_slashable_cycle = + Cycle_repr.sub current_cycle (preserved_cycles + 1) + |> Option.value ~default:Cycle_repr.root + in + return + @@ not + (List.exists + (fun (x, _) -> Cycle_repr.(x >= oldest_slashable_cycle)) + slashing_history) + let stake_from_unstake_for_delegate ctxt ~delegate ~unfinalizable_requests_opt amount = let open Lwt_result_syntax in @@ -139,23 +159,8 @@ let stake_from_unstake_for_delegate ctxt ~delegate ~unfinalizable_requests_opt if Signature.Public_key_hash.(delegate <> delegate_requests) then (* impossible *) return (ctxt, [], amount) else - let* slashing_history_opt = - Storage.Contract.Slashed_deposits.find - ctxt - (Contract_repr.Implicit delegate) - in - let slashing_history = Option.value slashing_history_opt ~default:[] in - let current_cycle = (Raw_context.current_level ctxt).cycle in - let preserved_cycles = Constants_storage.preserved_cycles ctxt in - let oldest_slashable_cycle = - Cycle_repr.sub current_cycle (preserved_cycles + 1) - |> Option.value ~default:Cycle_repr.root - in - if - List.exists - (fun (x, _) -> Cycle_repr.(x >= oldest_slashable_cycle)) - slashing_history - then + let* allowed = can_stake_from_unstake ctxt ~delegate in + if not allowed then (* a slash could have modified the unstaked frozen deposits: cannot stake from unstake *) return (ctxt, [], amount) else @@ -242,7 +247,8 @@ let stake_from_unstake_for_delegate ctxt ~delegate ~unfinalizable_requests_opt in return (ctxt, balance_updates, remaining_amount_to_transfer) -let stake ctxt ~amount_strictness ~sender ~delegate amount = +let stake ctxt ~(amount_strictness : [`Best_effort | `Exact]) ~sender ~delegate + amount = let open Lwt_result_syntax in let check_unfinalizable ctxt Unstake_requests_storage.{delegate = unstake_delegate; requests} = @@ -258,7 +264,33 @@ let stake ctxt ~amount_strictness ~sender ~delegate amount = let* ctxt, finalize_balance_updates, unfinalizable_requests_opt = finalize_unstake_and_check ~check_unfinalizable ctxt sender_contract in - let amount = match amount_strictness with `Exact -> amount in + let* amount = + let unfinalizable_requests = + Option.fold + ~none:[] + ~some:(fun x -> x.Unstake_requests_storage.requests) + unfinalizable_requests_opt + in + match amount_strictness with + | `Exact -> return amount + | `Best_effort -> + let*? unstake_frozen_balance = + List.fold_left_e + (fun acc (_, t) -> Tez_repr.(acc +? t)) + Tez_repr.zero + unfinalizable_requests + in + let* spendable = + Contract_storage.get_balance ctxt (Implicit delegate) + in + let* stake_from_unstake = can_stake_from_unstake ctxt ~delegate in + let*? max_spendable = + if stake_from_unstake then + Tez_repr.(spendable +? unstake_frozen_balance) + else ok spendable + in + return Tez_repr.(min amount max_spendable) + in let* ctxt, stake_balance_updates1, amount_from_liquid = if Signature.Public_key_hash.(sender <> delegate) then let* ctxt, stake_balance_updates_pseudotoken = diff --git a/src/proto_alpha/lib_protocol/staking.mli b/src/proto_alpha/lib_protocol/staking.mli index ef42a141c2d0..0e7e6399e8ce 100644 --- a/src/proto_alpha/lib_protocol/staking.mli +++ b/src/proto_alpha/lib_protocol/staking.mli @@ -27,7 +27,7 @@ to [delegate]. *) val stake : Raw_context.t -> - amount_strictness:[`Exact] -> + amount_strictness:[`Best_effort | `Exact] -> sender:Signature.Public_key_hash.t -> delegate:Signature.public_key_hash -> Tez_repr.t -> -- GitLab From 8b74a3735f4aaf20f9a69018269ae6477c572ccf Mon Sep 17 00:00:00 2001 From: Lucas Randazzo Date: Mon, 13 Nov 2023 10:03:54 +0100 Subject: [PATCH 21/26] Proto/stake: applying suggestions --- .../lib_client/client_proto_context.ml | 36 ++++++++++--------- .../consensus/test_frozen_deposits.ml | 4 ++- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/proto_alpha/lib_client/client_proto_context.ml b/src/proto_alpha/lib_client/client_proto_context.ml index 410dcfb9f80e..e5a1d39c2a41 100644 --- a/src/proto_alpha/lib_client/client_proto_context.ml +++ b/src/proto_alpha/lib_client/client_proto_context.ml @@ -459,6 +459,7 @@ let drain_delegate cctxt ~chain ~block ?confirmations ?dry_run ?verbose_signing let set_deposits_limit cctxt ~chain ~block ?confirmations ?dry_run ?verbose_signing ?simulation ?fee contract ~src_pk ~manager_sk ~fee_parameter limit_opt = + let open Lwt_result_syntax in let operation = Set_deposits_limit limit_opt in let operation = Injection.prepare_manager_operation @@ -468,23 +469,24 @@ let set_deposits_limit cctxt ~chain ~block ?confirmations ?dry_run operation in let operation = Annotated_manager_operation.Single_manager operation in - Injection.inject_manager_operation - cctxt - ~chain - ~block - ?confirmations - ?dry_run - ?verbose_signing - ?simulation - ~source:contract - ~fee:(Limit.of_option fee) - ~gas_limit:Limit.unknown - ~storage_limit:Limit.unknown - ~src_pk - ~src_sk:manager_sk - ~fee_parameter - operation - >>=? fun (oph, _, op, result) -> + let* oph, _, op, result = + Injection.inject_manager_operation + cctxt + ~chain + ~block + ?confirmations + ?dry_run + ?verbose_signing + ?simulation + ~source:contract + ~fee:(Limit.of_option fee) + ~gas_limit:Limit.unknown + ~storage_limit:Limit.unknown + ~src_pk + ~src_sk:manager_sk + ~fee_parameter + operation + in match Apply_results.pack_contents_list op result with | Apply_results.Single_and_result ((Manager_operation _ as op), result) -> return (oph, op, result) diff --git a/src/proto_alpha/lib_protocol/test/integration/consensus/test_frozen_deposits.ml b/src/proto_alpha/lib_protocol/test/integration/consensus/test_frozen_deposits.ml index 730d72d79faa..a089cbe67f8e 100644 --- a/src/proto_alpha/lib_protocol/test/integration/consensus/test_frozen_deposits.ml +++ b/src/proto_alpha/lib_protocol/test/integration/consensus/test_frozen_deposits.ml @@ -171,7 +171,9 @@ let test_cannot_bake_with_zero_deposits () = (* by now, the active stake of account1 is 0 so it no longer has slots, thus it cannot be a proposer, thus it cannot bake. Precisely, bake fails with error Validate_errors.Consensus.Zero_frozen_deposits*) - let* () = Assert.error ~loc:__LOC__ b1 (fun _ -> true) in + let* () = + Assert.proto_error_with_info ~loc:__LOC__ b1 "Zero frozen deposits" + in let* b = Block.bake_until_cycle_end ~policy:(By_account account2) b in (* after one cycle is passed, the frozen deposit window has passed and the frozen deposits should now be effectively 0. *) -- GitLab From 8b26695d81f0af5e33e3ab665060e38eb64e7e1a Mon Sep 17 00:00:00 2001 From: Lucas Randazzo Date: Mon, 13 Nov 2023 10:08:47 +0100 Subject: [PATCH 22/26] Proto/stake: put amount in amount_strictness --- src/proto_alpha/lib_protocol/alpha_context.mli | 3 +-- src/proto_alpha/lib_protocol/apply.ml | 2 +- src/proto_alpha/lib_protocol/delegate_cycles.ml | 3 +-- src/proto_alpha/lib_protocol/staking.ml | 11 ++++++----- src/proto_alpha/lib_protocol/staking.mli | 3 +-- 5 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/proto_alpha/lib_protocol/alpha_context.mli b/src/proto_alpha/lib_protocol/alpha_context.mli index 3ca5f6c34ae9..104af605fd68 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/alpha_context.mli @@ -2425,10 +2425,9 @@ module Staking : sig to [delegate]. *) val stake : context -> - amount_strictness:[`Best_effort | `Exact] -> + amount_strictness:[`Best_effort of Tez.t | `Exact of Tez.t] -> sender:public_key_hash -> delegate:public_key_hash -> - Tez.t -> (context * Receipt.balance_updates) tzresult Lwt.t (** [request_unstake ctxt ~sender_contract ~delegate amount] records a request diff --git a/src/proto_alpha/lib_protocol/apply.ml b/src/proto_alpha/lib_protocol/apply.ml index 9d6268621229..d901f8aab4d1 100644 --- a/src/proto_alpha/lib_protocol/apply.ml +++ b/src/proto_alpha/lib_protocol/apply.ml @@ -407,7 +407,7 @@ let apply_stake ~ctxt ~sender ~amount ~destination ~before_operation = error_when forbidden Staking_to_delegate_that_refuses_external_staking in let* ctxt, balance_updates = - Staking.stake ctxt ~amount_strictness:`Exact ~sender ~delegate amount + Staking.stake ctxt ~amount_strictness:(`Exact amount) ~sender ~delegate in (* Since [delegate] is an already existing delegate, it is already allocated. *) let allocated_destination_contract = false in diff --git a/src/proto_alpha/lib_protocol/delegate_cycles.ml b/src/proto_alpha/lib_protocol/delegate_cycles.ml index bff194fdf79d..e47ec2e1b1e5 100644 --- a/src/proto_alpha/lib_protocol/delegate_cycles.ml +++ b/src/proto_alpha/lib_protocol/delegate_cycles.ml @@ -144,10 +144,9 @@ let adjust_frozen_stakes ctxt : let*? optimal_to_stake = Tez_repr.(optimal_frozen -? own_frozen) in Staking.stake ctxt - ~amount_strictness:`Best_effort + ~amount_strictness:(`Best_effort optimal_to_stake) ~sender:delegate ~delegate - optimal_to_stake else if Tez_repr.(optimal_frozen < own_frozen) then let*? to_unstake = Tez_repr.(own_frozen -? optimal_frozen) in Staking.request_unstake diff --git a/src/proto_alpha/lib_protocol/staking.ml b/src/proto_alpha/lib_protocol/staking.ml index faf41627422a..1d7b019e0ffb 100644 --- a/src/proto_alpha/lib_protocol/staking.ml +++ b/src/proto_alpha/lib_protocol/staking.ml @@ -247,8 +247,9 @@ let stake_from_unstake_for_delegate ctxt ~delegate ~unfinalizable_requests_opt in return (ctxt, balance_updates, remaining_amount_to_transfer) -let stake ctxt ~(amount_strictness : [`Best_effort | `Exact]) ~sender ~delegate - amount = +let stake ctxt + ~(amount_strictness : [`Best_effort of Tez_repr.t | `Exact of Tez_repr.t]) + ~sender ~delegate = let open Lwt_result_syntax in let check_unfinalizable ctxt Unstake_requests_storage.{delegate = unstake_delegate; requests} = @@ -272,8 +273,8 @@ let stake ctxt ~(amount_strictness : [`Best_effort | `Exact]) ~sender ~delegate unfinalizable_requests_opt in match amount_strictness with - | `Exact -> return amount - | `Best_effort -> + | `Exact amount -> return amount + | `Best_effort max_amount -> let*? unstake_frozen_balance = List.fold_left_e (fun acc (_, t) -> Tez_repr.(acc +? t)) @@ -289,7 +290,7 @@ let stake ctxt ~(amount_strictness : [`Best_effort | `Exact]) ~sender ~delegate Tez_repr.(spendable +? unstake_frozen_balance) else ok spendable in - return Tez_repr.(min amount max_spendable) + return Tez_repr.(min max_amount max_spendable) in let* ctxt, stake_balance_updates1, amount_from_liquid = if Signature.Public_key_hash.(sender <> delegate) then diff --git a/src/proto_alpha/lib_protocol/staking.mli b/src/proto_alpha/lib_protocol/staking.mli index 0e7e6399e8ce..56eb23bfff8b 100644 --- a/src/proto_alpha/lib_protocol/staking.mli +++ b/src/proto_alpha/lib_protocol/staking.mli @@ -27,10 +27,9 @@ to [delegate]. *) val stake : Raw_context.t -> - amount_strictness:[`Best_effort | `Exact] -> + amount_strictness:[`Best_effort of Tez_repr.t | `Exact of Tez_repr.t] -> sender:Signature.Public_key_hash.t -> delegate:Signature.public_key_hash -> - Tez_repr.t -> (Raw_context.t * Receipt_repr.balance_updates) tzresult Lwt.t (** [request_unstake ctxt ~sender_contract ~delegate amount] records a request -- GitLab From 7ff1c0fbd85f085ffb746e5888244cfaf8497db3 Mon Sep 17 00:00:00 2001 From: Lucas Randazzo Date: Tue, 14 Nov 2023 10:11:55 +0100 Subject: [PATCH 23/26] Proto/stake/tests: reenable tezt tests --- tezt/tests/deposits_limit.ml | 4 --- .../Nairobi- set deposits limit.out | 26 +++++++++++++++++++ .../Nairobi- unset deposits limit.out | 26 +++++++++++++++++++ 3 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 tezt/tests/expected/deposits_limit.ml/Nairobi- set deposits limit.out create mode 100644 tezt/tests/expected/deposits_limit.ml/Nairobi- unset deposits limit.out diff --git a/tezt/tests/deposits_limit.ml b/tezt/tests/deposits_limit.ml index 52b06b56dcaa..630e90e90ef2 100644 --- a/tezt/tests/deposits_limit.ml +++ b/tezt/tests/deposits_limit.ml @@ -33,8 +33,6 @@ let test_set_deposits_limit = Protocol.register_regression_test ~__FILE__ ~title:"set deposits limit" - (* TODO: Remove the constraint when oxford1 is removed *) - ~supports:(Protocol.From_protocol 019) ~tags:["deposits_limit"] @@ fun protocol -> let* _, client = Client.init_with_protocol ~protocol `Client () in @@ -47,8 +45,6 @@ let test_unset_deposits_limit = Protocol.register_regression_test ~__FILE__ ~title:"unset deposits limit" - (* TODO: Remove the constraint when oxford1 is removed *) - ~supports:(Protocol.From_protocol 019) ~tags:["deposits_limit"] @@ fun protocol -> let* _, client = Client.init_with_protocol ~protocol `Client () in diff --git a/tezt/tests/expected/deposits_limit.ml/Nairobi- set deposits limit.out b/tezt/tests/expected/deposits_limit.ml/Nairobi- set deposits limit.out new file mode 100644 index 000000000000..8ac64a759156 --- /dev/null +++ b/tezt/tests/expected/deposits_limit.ml/Nairobi- set deposits limit.out @@ -0,0 +1,26 @@ +Node is bootstrapped. +Estimated gas: 168.052 units (will add 0 for safety) +Estimated storage: no bytes added +Operation successfully injected in the node. +Operation hash is 'oogPg9fm3Wo8Fub69ehKvyhmefv442MSvvKME7awo3zvZVAsuTo' +NOT waiting for the operation to be included. +Use command + octez-client wait for oogPg9fm3Wo8Fub69ehKvyhmefv442MSvvKME7awo3zvZVAsuTo to be included --confirmations 1 --branch BLockGenesisGenesisGenesisGenesisGenesisf79b5d1CoW2 +and/or an external block explorer to make sure that it has been included. +This sequence of operations was run: + Manager signed operations: + From: tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx + Fee to the baker: ꜩ0.000247 + Expected counter: 1 + Gas limit: 169 + Storage limit: 0 bytes + Balance updates: + tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx ... -ꜩ0.000247 + payload fees(the block proposer) ....... +ꜩ0.000247 + Set deposits limit: + Delegate: tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx + Limit: 1000 + This deposits limit modification was successfully applied + Consumed gas: 167.986 + + diff --git a/tezt/tests/expected/deposits_limit.ml/Nairobi- unset deposits limit.out b/tezt/tests/expected/deposits_limit.ml/Nairobi- unset deposits limit.out new file mode 100644 index 000000000000..fc4436f54195 --- /dev/null +++ b/tezt/tests/expected/deposits_limit.ml/Nairobi- unset deposits limit.out @@ -0,0 +1,26 @@ +Node is bootstrapped. +Estimated gas: 167.886 units (will add 0 for safety) +Estimated storage: no bytes added +Operation successfully injected in the node. +Operation hash is 'oo1PJ41t8u2VYdoeFVgREH9LKKtASE141H3wWRmKRbuBcshW3hR' +NOT waiting for the operation to be included. +Use command + octez-client wait for oo1PJ41t8u2VYdoeFVgREH9LKKtASE141H3wWRmKRbuBcshW3hR to be included --confirmations 1 --branch BLockGenesisGenesisGenesisGenesisGenesisf79b5d1CoW2 +and/or an external block explorer to make sure that it has been included. +This sequence of operations was run: + Manager signed operations: + From: tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx + Fee to the baker: ꜩ0.000242 + Expected counter: 1 + Gas limit: 168 + Storage limit: 0 bytes + Balance updates: + tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx ... -ꜩ0.000242 + payload fees(the block proposer) ....... +ꜩ0.000242 + Set deposits limit: + Delegate: tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx + Unlimited deposits + This deposits limit modification was successfully applied + Consumed gas: 167.820 + + -- GitLab From 7429ff5d63d841c21ac8f5b051e9f0930c8e921e Mon Sep 17 00:00:00 2001 From: Lucas Randazzo Date: Tue, 14 Nov 2023 17:04:13 +0100 Subject: [PATCH 24/26] Proto/stake: rename parameters for stake amount --- src/proto_alpha/lib_protocol/alpha_context.mli | 2 +- src/proto_alpha/lib_protocol/apply.ml | 2 +- src/proto_alpha/lib_protocol/delegate_cycles.ml | 2 +- src/proto_alpha/lib_protocol/staking.ml | 9 ++++----- src/proto_alpha/lib_protocol/staking.mli | 2 +- 5 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/proto_alpha/lib_protocol/alpha_context.mli b/src/proto_alpha/lib_protocol/alpha_context.mli index 104af605fd68..b9d51fd3e62b 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/alpha_context.mli @@ -2425,7 +2425,7 @@ module Staking : sig to [delegate]. *) val stake : context -> - amount_strictness:[`Best_effort of Tez.t | `Exact of Tez.t] -> + amount:[`At_most of Tez.t | `Exactly of Tez.t] -> sender:public_key_hash -> delegate:public_key_hash -> (context * Receipt.balance_updates) tzresult Lwt.t diff --git a/src/proto_alpha/lib_protocol/apply.ml b/src/proto_alpha/lib_protocol/apply.ml index d901f8aab4d1..780f22f84c52 100644 --- a/src/proto_alpha/lib_protocol/apply.ml +++ b/src/proto_alpha/lib_protocol/apply.ml @@ -407,7 +407,7 @@ let apply_stake ~ctxt ~sender ~amount ~destination ~before_operation = error_when forbidden Staking_to_delegate_that_refuses_external_staking in let* ctxt, balance_updates = - Staking.stake ctxt ~amount_strictness:(`Exact amount) ~sender ~delegate + Staking.stake ctxt ~amount:(`Exactly amount) ~sender ~delegate in (* Since [delegate] is an already existing delegate, it is already allocated. *) let allocated_destination_contract = false in diff --git a/src/proto_alpha/lib_protocol/delegate_cycles.ml b/src/proto_alpha/lib_protocol/delegate_cycles.ml index e47ec2e1b1e5..0bb7262c5866 100644 --- a/src/proto_alpha/lib_protocol/delegate_cycles.ml +++ b/src/proto_alpha/lib_protocol/delegate_cycles.ml @@ -144,7 +144,7 @@ let adjust_frozen_stakes ctxt : let*? optimal_to_stake = Tez_repr.(optimal_frozen -? own_frozen) in Staking.stake ctxt - ~amount_strictness:(`Best_effort optimal_to_stake) + ~amount:(`At_most optimal_to_stake) ~sender:delegate ~delegate else if Tez_repr.(optimal_frozen < own_frozen) then diff --git a/src/proto_alpha/lib_protocol/staking.ml b/src/proto_alpha/lib_protocol/staking.ml index 1d7b019e0ffb..6beb56eb5709 100644 --- a/src/proto_alpha/lib_protocol/staking.ml +++ b/src/proto_alpha/lib_protocol/staking.ml @@ -247,8 +247,7 @@ let stake_from_unstake_for_delegate ctxt ~delegate ~unfinalizable_requests_opt in return (ctxt, balance_updates, remaining_amount_to_transfer) -let stake ctxt - ~(amount_strictness : [`Best_effort of Tez_repr.t | `Exact of Tez_repr.t]) +let stake ctxt ~(amount : [`At_most of Tez_repr.t | `Exactly of Tez_repr.t]) ~sender ~delegate = let open Lwt_result_syntax in let check_unfinalizable ctxt @@ -272,9 +271,9 @@ let stake ctxt ~some:(fun x -> x.Unstake_requests_storage.requests) unfinalizable_requests_opt in - match amount_strictness with - | `Exact amount -> return amount - | `Best_effort max_amount -> + match amount with + | `Exactly amount -> return amount + | `At_most max_amount -> let*? unstake_frozen_balance = List.fold_left_e (fun acc (_, t) -> Tez_repr.(acc +? t)) diff --git a/src/proto_alpha/lib_protocol/staking.mli b/src/proto_alpha/lib_protocol/staking.mli index 56eb23bfff8b..a7b0409f4d41 100644 --- a/src/proto_alpha/lib_protocol/staking.mli +++ b/src/proto_alpha/lib_protocol/staking.mli @@ -27,7 +27,7 @@ to [delegate]. *) val stake : Raw_context.t -> - amount_strictness:[`Best_effort of Tez_repr.t | `Exact of Tez_repr.t] -> + amount:[`At_most of Tez_repr.t | `Exactly of Tez_repr.t] -> sender:Signature.Public_key_hash.t -> delegate:Signature.public_key_hash -> (Raw_context.t * Receipt_repr.balance_updates) tzresult Lwt.t -- GitLab From 8dfe5940ccffc5687dbd25a68d29c37e75d6ea4d Mon Sep 17 00:00:00 2001 From: Lucas Randazzo Date: Tue, 14 Nov 2023 17:15:28 +0100 Subject: [PATCH 25/26] Proto/stake: refactor stake function --- src/proto_alpha/lib_protocol/staking.ml | 66 +++++++++++-------------- 1 file changed, 28 insertions(+), 38 deletions(-) diff --git a/src/proto_alpha/lib_protocol/staking.ml b/src/proto_alpha/lib_protocol/staking.ml index 6beb56eb5709..de94a9a6931f 100644 --- a/src/proto_alpha/lib_protocol/staking.ml +++ b/src/proto_alpha/lib_protocol/staking.ml @@ -264,51 +264,41 @@ let stake ctxt ~(amount : [`At_most of Tez_repr.t | `Exactly of Tez_repr.t]) let* ctxt, finalize_balance_updates, unfinalizable_requests_opt = finalize_unstake_and_check ~check_unfinalizable ctxt sender_contract in - let* amount = - let unfinalizable_requests = - Option.fold - ~none:[] - ~some:(fun x -> x.Unstake_requests_storage.requests) - unfinalizable_requests_opt - in - match amount with - | `Exactly amount -> return amount - | `At_most max_amount -> - let*? unstake_frozen_balance = - List.fold_left_e - (fun acc (_, t) -> Tez_repr.(acc +? t)) - Tez_repr.zero - unfinalizable_requests - in - let* spendable = - Contract_storage.get_balance ctxt (Implicit delegate) - in - let* stake_from_unstake = can_stake_from_unstake ctxt ~delegate in - let*? max_spendable = - if stake_from_unstake then - Tez_repr.(spendable +? unstake_frozen_balance) - else ok spendable - in - return Tez_repr.(min max_amount max_spendable) + let tez_amount = + match amount with `Exactly amount | `At_most amount -> amount in + (* stake from unstake for eligible delegates *) let* ctxt, stake_balance_updates1, amount_from_liquid = if Signature.Public_key_hash.(sender <> delegate) then - let* ctxt, stake_balance_updates_pseudotoken = - Staking_pseudotokens_storage.stake - ctxt - ~contract:sender_contract - ~delegate - amount - in - return (ctxt, stake_balance_updates_pseudotoken, amount) + return (ctxt, [], tez_amount) else stake_from_unstake_for_delegate ctxt ~delegate ~unfinalizable_requests_opt - amount + tez_amount + in + (* Get amount to transfer from liquid wrt mode *) + let* amount_from_liquid = + match amount with + | `Exactly _ -> return amount_from_liquid + | `At_most _ -> + let* spendable = + Contract_storage.get_balance ctxt (Implicit delegate) + in + return Tez_repr.(min amount_from_liquid spendable) + in + (* Issue pseudotokens for delegators *) + let* ctxt, stake_balance_updates2 = + if Signature.Public_key_hash.(sender <> delegate) then + Staking_pseudotokens_storage.stake + ctxt + ~contract:sender_contract + ~delegate + amount_from_liquid + else return (ctxt, []) in - let+ ctxt, stake_balance_updates2 = + let+ ctxt, stake_balance_updates3 = Token.transfer ctxt (`Contract sender_contract) @@ -317,8 +307,8 @@ let stake ctxt ~(amount : [`At_most of Tez_repr.t | `Exactly of Tez_repr.t]) amount_from_liquid in ( ctxt, - stake_balance_updates1 @ stake_balance_updates2 @ finalize_balance_updates - ) + stake_balance_updates1 @ stake_balance_updates2 @ stake_balance_updates3 + @ finalize_balance_updates ) let request_unstake ctxt ~sender_contract ~delegate requested_amount = let open Lwt_result_syntax in -- GitLab From 2edfae00cf490630252589a8612cbe76f3841cce Mon Sep 17 00:00:00 2001 From: Lucas Randazzo Date: Wed, 15 Nov 2023 10:14:31 +0100 Subject: [PATCH 26/26] Proto/stake: avoid fail case when autostaking --- src/proto_alpha/lib_protocol/staking.ml | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/proto_alpha/lib_protocol/staking.ml b/src/proto_alpha/lib_protocol/staking.ml index de94a9a6931f..e07253d27669 100644 --- a/src/proto_alpha/lib_protocol/staking.ml +++ b/src/proto_alpha/lib_protocol/staking.ml @@ -157,7 +157,9 @@ let stake_from_unstake_for_delegate ctxt ~delegate ~unfinalizable_requests_opt | None -> return (ctxt, [], amount) | Some Unstake_requests_storage.{delegate = delegate_requests; requests} -> if Signature.Public_key_hash.(delegate <> delegate_requests) then - (* impossible *) return (ctxt, [], amount) + (* Possible. If reached, stake should not do anything, + so we also set the amount to stake from the liquid part to zero. *) + return (ctxt, [], Tez_repr.zero) else let* allowed = can_stake_from_unstake ctxt ~delegate in if not allowed then @@ -252,9 +254,9 @@ let stake ctxt ~(amount : [`At_most of Tez_repr.t | `Exactly of Tez_repr.t]) let open Lwt_result_syntax in let check_unfinalizable ctxt Unstake_requests_storage.{delegate = unstake_delegate; requests} = - match requests with - | [] -> return ctxt - | _ :: _ -> + match (requests, amount) with + | [], _ | _ :: _, `At_most _ -> return ctxt + | _ :: _, `Exactly _ -> if Signature.Public_key_hash.(delegate <> unstake_delegate) then tzfail Cannot_stake_with_unfinalizable_unstake_requests_to_another_delegate -- GitLab