From 2be58abdb15c7497ecdca9d1fc39bfecf138df53 Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Thu, 8 Jun 2023 10:23:26 +0200 Subject: [PATCH 1/3] Proto/Operation_result: add balance_updates to external delegation result --- src/proto_alpha/lib_client/injection.ml | 2 +- src/proto_alpha/lib_client/operation_result.ml | 6 ++++-- src/proto_alpha/lib_protocol/apply.ml | 2 +- .../lib_protocol/apply_internal_results.ml | 12 +++++++++--- .../lib_protocol/apply_internal_results.mli | 1 + 5 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/proto_alpha/lib_client/injection.ml b/src/proto_alpha/lib_client/injection.ml index bb125faf698e..a05d6e7a5b84 100644 --- a/src/proto_alpha/lib_client/injection.ml +++ b/src/proto_alpha/lib_client/injection.ml @@ -358,7 +358,7 @@ let estimated_gas_single (type kind) | Transaction_to_sc_rollup_result {consumed_gas; _} | Transaction_to_zk_rollup_result {consumed_gas; _} ) | IOrigination_result {consumed_gas; _} - | IDelegation_result {consumed_gas} + | IDelegation_result {consumed_gas; _} | IEvent_result {consumed_gas} -> Ok consumed_gas) | Skipped _ -> diff --git a/src/proto_alpha/lib_client/operation_result.ml b/src/proto_alpha/lib_client/operation_result.ml index 00e266a74e2a..32dc65307d1f 100644 --- a/src/proto_alpha/lib_client/operation_result.ml +++ b/src/proto_alpha/lib_client/operation_result.ml @@ -782,8 +782,10 @@ let pp_internal_operation_and_result ppf (Internal_operation_result (op, res)) = match result with | ITransaction_result tx -> pp_transaction_result ppf tx | IOrigination_result op_res -> pp_origination_result ppf op_res - | IDelegation_result {consumed_gas} | IEvent_result {consumed_gas} -> - pp_consumed_gas ppf consumed_gas + | IDelegation_result {consumed_gas; balance_updates} -> + pp_consumed_gas ppf consumed_gas ; + pp_balance_updates ppf balance_updates + | IEvent_result {consumed_gas} -> pp_consumed_gas ppf consumed_gas in Format.fprintf ppf diff --git a/src/proto_alpha/lib_protocol/apply.ml b/src/proto_alpha/lib_protocol/apply.ml index 6224b6fd9893..e11b3e598f7a 100644 --- a/src/proto_alpha/lib_protocol/apply.ml +++ b/src/proto_alpha/lib_protocol/apply.ml @@ -902,7 +902,7 @@ let apply_internal_operation_contents : assert_sender_is_contract sender >>?= fun sender -> apply_delegation ~ctxt ~sender ~delegate ~before_operation:ctxt_before_op >|=? fun (ctxt, consumed_gas, ops) -> - (ctxt, IDelegation_result {consumed_gas}, ops) + (ctxt, IDelegation_result {consumed_gas; balance_updates = []}, ops) let apply_manager_operation : type kind. diff --git a/src/proto_alpha/lib_protocol/apply_internal_results.ml b/src/proto_alpha/lib_protocol/apply_internal_results.ml index 6fc7f20048bc..ca82d86267f2 100644 --- a/src/proto_alpha/lib_protocol/apply_internal_results.ml +++ b/src/proto_alpha/lib_protocol/apply_internal_results.ml @@ -173,6 +173,7 @@ type _ successful_internal_operation_result = -> Kind.origination successful_internal_operation_result | IDelegation_result : { consumed_gas : Gas.Arith.fp; + balance_updates : Receipt.balance_updates; } -> Kind.delegation successful_internal_operation_result | IEvent_result : { @@ -621,14 +622,19 @@ module Internal_operation_result = struct ~op_case:Internal_operation.delegation_case ~encoding: Data_encoding.( - obj1 (dft "consumed_milligas" Gas.Arith.n_fp_encoding Gas.Arith.zero)) + obj2 + (dft "consumed_milligas" Gas.Arith.n_fp_encoding Gas.Arith.zero) + (dft "balance_updates" Receipt.balance_updates_encoding [])) ~select:(function | Successful_internal_operation_result (IDelegation_result _ as op) -> Some op | _ -> None) ~kind:Kind.Delegation_manager_kind - ~proj:(function IDelegation_result {consumed_gas} -> consumed_gas) - ~inj:(fun consumed_gas -> IDelegation_result {consumed_gas}) + ~proj:(function + | IDelegation_result {consumed_gas; balance_updates} -> + (consumed_gas, balance_updates)) + ~inj:(fun (consumed_gas, balance_updates) -> + IDelegation_result {consumed_gas; balance_updates}) let event_case = make diff --git a/src/proto_alpha/lib_protocol/apply_internal_results.mli b/src/proto_alpha/lib_protocol/apply_internal_results.mli index e2ec98399e11..8a2f2b5ccb13 100644 --- a/src/proto_alpha/lib_protocol/apply_internal_results.mli +++ b/src/proto_alpha/lib_protocol/apply_internal_results.mli @@ -119,6 +119,7 @@ type _ successful_internal_operation_result = -> Kind.origination successful_internal_operation_result | IDelegation_result : { consumed_gas : Gas.Arith.fp; + balance_updates : Receipt.balance_updates; } -> Kind.delegation successful_internal_operation_result | IEvent_result : { -- GitLab From e6da5854631e2c2f8782873d1740c51ab2c3f414 Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Thu, 8 Jun 2023 10:29:13 +0200 Subject: [PATCH 2/3] Proto/Operation_result: add balance_updates to internal delegation result --- src/proto_alpha/lib_client/injection.ml | 2 +- src/proto_alpha/lib_client/operation_result.ml | 4 +++- src/proto_alpha/lib_protocol/apply.ml | 10 +++++----- src/proto_alpha/lib_protocol/apply_results.ml | 12 +++++++++--- src/proto_alpha/lib_protocol/apply_results.mli | 1 + src/proto_alpha/lib_protocol/test/helpers/block.ml | 12 ++++++------ 6 files changed, 25 insertions(+), 16 deletions(-) diff --git a/src/proto_alpha/lib_client/injection.ml b/src/proto_alpha/lib_client/injection.ml index a05d6e7a5b84..a6beb84f6b31 100644 --- a/src/proto_alpha/lib_client/injection.ml +++ b/src/proto_alpha/lib_client/injection.ml @@ -324,7 +324,7 @@ let estimated_gas_single (type kind) | Transaction_to_zk_rollup_result {consumed_gas; _} ) | Origination_result {consumed_gas; _} | Reveal_result {consumed_gas} - | Delegation_result {consumed_gas} + | Delegation_result {consumed_gas; _} | Register_global_constant_result {consumed_gas; _} | Update_consensus_key_result {consumed_gas; _} | Increase_paid_storage_result {consumed_gas; _} diff --git a/src/proto_alpha/lib_client/operation_result.ml b/src/proto_alpha/lib_client/operation_result.ml index 32dc65307d1f..6c8a04c2dd07 100644 --- a/src/proto_alpha/lib_client/operation_result.ml +++ b/src/proto_alpha/lib_client/operation_result.ml @@ -737,7 +737,9 @@ let pp_manager_operation_contents_result ppf op_result = (result : kind successful_manager_operation_result) = match result with | Reveal_result {consumed_gas} -> pp_consumed_gas ppf consumed_gas - | Delegation_result {consumed_gas} -> pp_consumed_gas ppf consumed_gas + | Delegation_result {consumed_gas; balance_updates} -> + pp_consumed_gas ppf consumed_gas ; + pp_balance_updates ppf balance_updates | 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_protocol/apply.ml b/src/proto_alpha/lib_protocol/apply.ml index e11b3e598f7a..fc7dcbb8f315 100644 --- a/src/proto_alpha/lib_protocol/apply.ml +++ b/src/proto_alpha/lib_protocol/apply.ml @@ -319,7 +319,7 @@ let update_script_storage_and_ticket_balances ctxt ~self_contract storage let apply_delegation ~ctxt ~sender ~delegate ~before_operation = Contract.Delegate.set ctxt sender delegate >|=? fun ctxt -> - (ctxt, Gas.consumed ~since:before_operation ~until:ctxt, []) + (ctxt, Gas.consumed ~since:before_operation ~until:ctxt, [], []) type 'loc execution_arg = | Typed_arg : 'loc * ('a, _) Script_typed_ir.ty * 'a -> 'loc execution_arg @@ -901,8 +901,8 @@ let apply_internal_operation_contents : | Delegation delegate -> assert_sender_is_contract sender >>?= fun sender -> apply_delegation ~ctxt ~sender ~delegate ~before_operation:ctxt_before_op - >|=? fun (ctxt, consumed_gas, ops) -> - (ctxt, IDelegation_result {consumed_gas; balance_updates = []}, ops) + >|=? fun (ctxt, consumed_gas, balance_updates, ops) -> + (ctxt, IDelegation_result {consumed_gas; balance_updates}, ops) let apply_manager_operation : type kind. @@ -1201,8 +1201,8 @@ let apply_manager_operation : ~sender:source_contract ~delegate ~before_operation:ctxt_before_op - >|=? fun (ctxt, consumed_gas, ops) -> - (ctxt, Delegation_result {consumed_gas}, ops) + >|=? fun (ctxt, consumed_gas, balance_updates, ops) -> + (ctxt, Delegation_result {consumed_gas; balance_updates}, ops) | Register_global_constant {value} -> (* Decode the value and consume gas appropriately *) Script.force_decode_in_context ~consume_deserialization_gas ctxt value diff --git a/src/proto_alpha/lib_protocol/apply_results.ml b/src/proto_alpha/lib_protocol/apply_results.ml index a458578dbcc2..ddac4f68d246 100644 --- a/src/proto_alpha/lib_protocol/apply_results.ml +++ b/src/proto_alpha/lib_protocol/apply_results.ml @@ -48,6 +48,7 @@ type _ successful_manager_operation_result = -> Kind.origination successful_manager_operation_result | Delegation_result : { consumed_gas : Gas.Arith.fp; + balance_updates : Receipt.balance_updates; } -> Kind.delegation successful_manager_operation_result | Register_global_constant_result : { @@ -443,13 +444,18 @@ module Manager_result = struct ~op_case:Operation.Encoding.Manager_operations.delegation_case ~encoding: Data_encoding.( - obj1 (dft "consumed_milligas" Gas.Arith.n_fp_encoding Gas.Arith.zero)) + obj2 + (dft "consumed_milligas" Gas.Arith.n_fp_encoding Gas.Arith.zero) + (dft "balance_updates" Receipt.balance_updates_encoding [])) ~select:(function | Successful_manager_result (Delegation_result _ as op) -> Some op | _ -> None) ~kind:Kind.Delegation_manager_kind - ~proj:(function Delegation_result {consumed_gas} -> consumed_gas) - ~inj:(fun consumed_gas -> Delegation_result {consumed_gas}) + ~proj:(function + | Delegation_result {consumed_gas; balance_updates} -> + (consumed_gas, balance_updates)) + ~inj:(fun (consumed_gas, balance_updates) -> + Delegation_result {consumed_gas; balance_updates}) let update_consensus_key_case = make diff --git a/src/proto_alpha/lib_protocol/apply_results.mli b/src/proto_alpha/lib_protocol/apply_results.mli index 86695bcfcb6f..0b823a54b5e8 100644 --- a/src/proto_alpha/lib_protocol/apply_results.mli +++ b/src/proto_alpha/lib_protocol/apply_results.mli @@ -139,6 +139,7 @@ and _ successful_manager_operation_result = -> Kind.origination successful_manager_operation_result | Delegation_result : { consumed_gas : Gas.Arith.fp; + balance_updates : Receipt.balance_updates; } -> Kind.delegation successful_manager_operation_result | Register_global_constant_result : { diff --git a/src/proto_alpha/lib_protocol/test/helpers/block.ml b/src/proto_alpha/lib_protocol/test/helpers/block.ml index dcf80e50fd46..bce05cb0aad0 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/block.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/block.ml @@ -928,16 +928,16 @@ let bake_n_with_all_balance_updates ?(baking_mode = Application) ?policy fun (Successful_manager_result r) -> match r with | Transaction_result (Transaction_to_sc_rollup_result _) - | Reveal_result _ | Delegation_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 _ + | 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 _ | Sc_rollup_recover_bond_result _ | Zk_rollup_origination_result _ | Zk_rollup_publish_result _ | Zk_rollup_update_result _ -> balance_updates_rev + | Delegation_result {balance_updates; _} | Transaction_result ( Transaction_to_contract_result {balance_updates; _} | Transaction_to_zk_rollup_result {balance_updates; _} ) -- GitLab From 1c71488d7b565c9f4889f227e69c93d47783c671 Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Thu, 8 Jun 2023 11:18:51 +0200 Subject: [PATCH 3/3] Proto: request full unstake on set_delegate --- src/proto_alpha/lib_protocol/alpha_context.mli | 2 ++ src/proto_alpha/lib_protocol/apply.ml | 8 ++++++-- src/proto_alpha/lib_protocol/staking.ml | 11 +++++++++++ src/proto_alpha/lib_protocol/staking.mli | 7 +++++++ 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/proto_alpha/lib_protocol/alpha_context.mli b/src/proto_alpha/lib_protocol/alpha_context.mli index f1944074c462..1bb9a3f539ff 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/alpha_context.mli @@ -126,6 +126,8 @@ module Tez : sig val one : tez + val max_mutez : tez + val ( -? ) : tez -> tez -> tez tzresult val sub_opt : tez -> tez -> tez option diff --git a/src/proto_alpha/lib_protocol/apply.ml b/src/proto_alpha/lib_protocol/apply.ml index fc7dcbb8f315..2831cc9d7055 100644 --- a/src/proto_alpha/lib_protocol/apply.ml +++ b/src/proto_alpha/lib_protocol/apply.ml @@ -318,8 +318,12 @@ let update_script_storage_and_ticket_balances ctxt ~self_contract storage operations let apply_delegation ~ctxt ~sender ~delegate ~before_operation = - Contract.Delegate.set ctxt sender delegate >|=? fun ctxt -> - (ctxt, Gas.consumed ~since:before_operation ~until:ctxt, [], []) + let open Lwt_result_syntax in + let* ctxt, balance_updates = + Staking.request_full_unstake ctxt ~sender_contract:sender + in + let+ ctxt = Contract.Delegate.set ctxt sender delegate in + (ctxt, Gas.consumed ~since:before_operation ~until:ctxt, balance_updates, []) type 'loc execution_arg = | Typed_arg : 'loc * ('a, _) Script_typed_ir.ty * 'a -> 'loc execution_arg diff --git a/src/proto_alpha/lib_protocol/staking.ml b/src/proto_alpha/lib_protocol/staking.ml index b3cc7e1a0bbe..13146b034e0f 100644 --- a/src/proto_alpha/lib_protocol/staking.ml +++ b/src/proto_alpha/lib_protocol/staking.ml @@ -199,3 +199,14 @@ let request_unstake ctxt ~sender_contract ~delegate requested_amount = record_request_unstake ctxt ~sender_contract ~delegate requested_amount in (ctxt, unstake_balance_updates @ finalize_balance_updates) + +let request_full_unstake ctxt ~sender_contract = + let open Lwt_result_syntax in + let* delegate_opt = Contract.Delegate.find ctxt sender_contract in + match delegate_opt with + | None -> + (* No delegates, nothing to unstake but maybe some unstake request to finalize. *) + finalize_unstake ctxt sender_contract + | Some delegate -> + (* [request_unstake] bounds to the actual stake. *) + request_unstake ctxt ~sender_contract ~delegate Tez.max_mutez diff --git a/src/proto_alpha/lib_protocol/staking.mli b/src/proto_alpha/lib_protocol/staking.mli index 14612f3e20b9..a128d47f9b6b 100644 --- a/src/proto_alpha/lib_protocol/staking.mli +++ b/src/proto_alpha/lib_protocol/staking.mli @@ -43,6 +43,13 @@ val request_unstake : Tez.t -> (context * Receipt.balance_updates) tzresult Lwt.t +(** [request_full_unstake ctxt ~sender_contract] records a request from + [sender_contract] to unstake everything from their delegate. *) +val request_full_unstake : + context -> + sender_contract:Contract.t -> + (context * Receipt.balance_updates) tzresult Lwt.t + (** [finalize_unstake ctxt contract] performs the finalization of all unstake requests from [contract] that can be finalized. An unstake request can be finalized if it is old enough, specifically the -- GitLab