From e21592ecc0dd801d90b403714fc21798579af83e Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Thu, 8 Jun 2023 17:02:33 +0200 Subject: [PATCH 1/6] Proto/Delegate_cycles: do not reimburse over-frozen delegates at cycle ends --- src/proto_alpha/lib_protocol/delegate_cycles.ml | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/src/proto_alpha/lib_protocol/delegate_cycles.ml b/src/proto_alpha/lib_protocol/delegate_cycles.ml index a3c979c34763..f374bf9b469f 100644 --- a/src/proto_alpha/lib_protocol/delegate_cycles.ml +++ b/src/proto_alpha/lib_protocol/delegate_cycles.ml @@ -119,17 +119,7 @@ let unfreeze_exceeding_deposits ?(origin = Receipt_repr.Block_application) ctxt >>=? fun ctxt -> Frozen_deposits_storage.get ctxt delegate_contract >>=? fun deposits -> let current_amount = deposits.current_amount in - if Tez_repr.(current_amount > maximum_stake_to_be_deposited) then - Tez_repr.(current_amount -? maximum_stake_to_be_deposited) - >>?= fun to_reimburse -> - Token.transfer - ~origin - ctxt - (`Frozen_deposits delegate) - (`Contract delegate_contract) - to_reimburse - >|=? fun (ctxt, bupds) -> (ctxt, bupds @ balance_updates) - else if Tez_repr.(current_amount = zero) then + if Tez_repr.(current_amount = zero) then (* If the delegate's current deposit remains at zero then we add it to the forbidden set. *) Delegate_storage.forbid_delegate ctxt delegate >>= fun ctxt -> -- GitLab From 95aebc6e8cacc9c8fedd142ddc03027c9a667899 Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Thu, 8 Jun 2023 17:07:59 +0200 Subject: [PATCH 2/6] Proto/Delegate_cycles: do not reimburse inactive delegates at cycle ends --- .../lib_protocol/delegate_cycles.ml | 21 ++----------------- 1 file changed, 2 insertions(+), 19 deletions(-) diff --git a/src/proto_alpha/lib_protocol/delegate_cycles.ml b/src/proto_alpha/lib_protocol/delegate_cycles.ml index f374bf9b469f..752c2ca0abdb 100644 --- a/src/proto_alpha/lib_protocol/delegate_cycles.ml +++ b/src/proto_alpha/lib_protocol/delegate_cycles.ml @@ -89,8 +89,7 @@ let max_frozen_deposits_and_delegates_to_remove ctxt ~from_cycle ~to_cycle = (Signature.Public_key_hash.Map.empty, cleared_cycle_delegates) cycles -let unfreeze_exceeding_deposits ?(origin = Receipt_repr.Block_application) ctxt - ~new_cycle ~balance_updates = +let unfreeze_exceeding_deposits ?origin:_ ctxt ~new_cycle ~balance_updates = Delegate_storage.reset_forbidden_delegates ctxt >>= fun ctxt -> let max_slashable_period = Constants_storage.max_slashing_period ctxt in (* We want to be able to slash for at most [max_slashable_period] *) @@ -128,11 +127,6 @@ let unfreeze_exceeding_deposits ?(origin = Receipt_repr.Block_application) ctxt maxima (ctxt, balance_updates) >>=? fun (ctxt, balance_updates) -> - (* Unfreeze deposits (that is, set them to zero) for delegates that - were previously in the relevant window (and therefore had some - frozen deposits) but are not in the new window; because that means - that such a delegate had no active stake in the relevant cycles, - and therefore it should have no frozen deposits. *) Signature.Public_key_hash.Set.fold_es (fun delegate (ctxt, balance_updates) -> let delegate_contract = Contract_repr.Implicit delegate in @@ -140,18 +134,7 @@ let unfreeze_exceeding_deposits ?(origin = Receipt_repr.Block_application) ctxt ctxt delegate_contract Tez_repr.zero - >>=? fun ctxt -> - Frozen_deposits_storage.get ctxt delegate_contract - >>=? fun frozen_deposits -> - if Tez_repr.(frozen_deposits.current_amount > zero) then - Token.transfer - ~origin - ctxt - (`Frozen_deposits delegate) - (`Contract delegate_contract) - frozen_deposits.current_amount - >|=? fun (ctxt, bupds) -> (ctxt, bupds @ balance_updates) - else return (ctxt, balance_updates)) + >>=? fun ctxt -> return (ctxt, balance_updates)) delegates_to_remove (ctxt, balance_updates) -- GitLab From 4d3bc5d551ecb2f15151f7abe80a06777f7df3cc Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Thu, 8 Jun 2023 17:09:57 +0200 Subject: [PATCH 3/6] Proto/Delegate_cycles: remove unused parameter origin --- src/proto_alpha/lib_protocol/delegate_cycles.ml | 6 +++--- src/proto_alpha/lib_protocol/delegate_cycles.mli | 6 ++---- src/proto_alpha/lib_protocol/init_storage.ml | 2 +- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/proto_alpha/lib_protocol/delegate_cycles.ml b/src/proto_alpha/lib_protocol/delegate_cycles.ml index 752c2ca0abdb..89f28afd8a70 100644 --- a/src/proto_alpha/lib_protocol/delegate_cycles.ml +++ b/src/proto_alpha/lib_protocol/delegate_cycles.ml @@ -89,7 +89,7 @@ let max_frozen_deposits_and_delegates_to_remove ctxt ~from_cycle ~to_cycle = (Signature.Public_key_hash.Map.empty, cleared_cycle_delegates) cycles -let unfreeze_exceeding_deposits ?origin:_ ctxt ~new_cycle ~balance_updates = +let unfreeze_exceeding_deposits ctxt ~new_cycle ~balance_updates = Delegate_storage.reset_forbidden_delegates ctxt >>= fun ctxt -> let max_slashable_period = Constants_storage.max_slashing_period ctxt in (* We want to be able to slash for at most [max_slashable_period] *) @@ -229,7 +229,7 @@ let cycle_end ctxt last_cycle = ~last_cycle >>=? fun ctxt -> return (ctxt, balance_updates, deactivated_delegates) -let init_first_cycles ctxt ~origin = +let init_first_cycles ctxt = let preserved = Constants_storage.preserved_cycles ctxt in List.fold_left_es (fun ctxt c -> @@ -242,4 +242,4 @@ let init_first_cycles ctxt ~origin = Misc.(0 --> preserved) >>=? fun ctxt -> let cycle = (Raw_context.current_level ctxt).cycle in - unfreeze_exceeding_deposits ~origin ~new_cycle:cycle ~balance_updates:[] ctxt + unfreeze_exceeding_deposits ~new_cycle:cycle ~balance_updates:[] ctxt diff --git a/src/proto_alpha/lib_protocol/delegate_cycles.mli b/src/proto_alpha/lib_protocol/delegate_cycles.mli index 1b7f8a87bad6..21bb964c9b06 100644 --- a/src/proto_alpha/lib_protocol/delegate_cycles.mli +++ b/src/proto_alpha/lib_protocol/delegate_cycles.mli @@ -40,12 +40,10 @@ val cycle_end : tzresult Lwt.t -(** [init_first_cycles ctxt ~origin] computes and records the distribution of +(** [init_first_cycles ctxt] computes and records the distribution of the total active stake among active delegates. This concerns the total active stake involved in the calculation of baking rights for all cycles in the range [0, preserved_cycles]. It also freezes the deposits for all the active delegates. *) val init_first_cycles : - Raw_context.t -> - origin:Receipt_repr.update_origin -> - (Raw_context.t * Receipt_repr.balance_updates) tzresult Lwt.t + Raw_context.t -> (Raw_context.t * Receipt_repr.balance_updates) tzresult Lwt.t diff --git a/src/proto_alpha/lib_protocol/init_storage.ml b/src/proto_alpha/lib_protocol/init_storage.ml index 8e97b0ceef4e..7b76532d0a41 100644 --- a/src/proto_alpha/lib_protocol/init_storage.ml +++ b/src/proto_alpha/lib_protocol/init_storage.ml @@ -250,7 +250,7 @@ let prepare_first_block _chain_id ctxt ~typecheck_smart_contract param.bootstrap_contracts param.bootstrap_smart_rollups >>=? fun (ctxt, bootstrap_balance_updates) -> - Delegate_cycles.init_first_cycles ctxt ~origin:Protocol_migration + Delegate_cycles.init_first_cycles ctxt >>=? fun (ctxt, deposits_balance_updates) -> Vote_storage.init ctxt -- GitLab From 06993cb56251fd52991a59a289d50fdce69f0ab8 Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Thu, 8 Jun 2023 17:12:16 +0200 Subject: [PATCH 4/6] Proto/Delegate_cycles: no balance updates anymore --- .../lib_protocol/delegate_cycles.ml | 24 +++++++++---------- .../lib_protocol/delegate_cycles.mli | 3 +-- src/proto_alpha/lib_protocol/init_storage.ml | 8 ++----- 3 files changed, 14 insertions(+), 21 deletions(-) diff --git a/src/proto_alpha/lib_protocol/delegate_cycles.ml b/src/proto_alpha/lib_protocol/delegate_cycles.ml index 89f28afd8a70..f057e4868ee6 100644 --- a/src/proto_alpha/lib_protocol/delegate_cycles.ml +++ b/src/proto_alpha/lib_protocol/delegate_cycles.ml @@ -89,7 +89,7 @@ let max_frozen_deposits_and_delegates_to_remove ctxt ~from_cycle ~to_cycle = (Signature.Public_key_hash.Map.empty, cleared_cycle_delegates) cycles -let unfreeze_exceeding_deposits ctxt ~new_cycle ~balance_updates = +let unfreeze_exceeding_deposits ctxt ~new_cycle = Delegate_storage.reset_forbidden_delegates ctxt >>= fun ctxt -> let max_slashable_period = Constants_storage.max_slashing_period ctxt in (* We want to be able to slash for at most [max_slashable_period] *) @@ -109,7 +109,7 @@ let unfreeze_exceeding_deposits ctxt ~new_cycle ~balance_updates = max_frozen_deposits_and_delegates_to_remove ctxt ~from_cycle ~to_cycle >>=? fun (maxima, delegates_to_remove) -> Signature.Public_key_hash.Map.fold_es - (fun delegate maximum_stake_to_be_deposited (ctxt, balance_updates) -> + (fun delegate maximum_stake_to_be_deposited ctxt -> let delegate_contract = Contract_repr.Implicit delegate in Frozen_deposits_storage.update_initial_amount ctxt @@ -122,21 +122,20 @@ let unfreeze_exceeding_deposits ctxt ~new_cycle ~balance_updates = (* If the delegate's current deposit remains at zero then we add it to the forbidden set. *) Delegate_storage.forbid_delegate ctxt delegate >>= fun ctxt -> - return (ctxt, balance_updates) - else return (ctxt, balance_updates)) + return ctxt + else return ctxt) maxima - (ctxt, balance_updates) - >>=? fun (ctxt, balance_updates) -> + ctxt + >>=? fun ctxt -> Signature.Public_key_hash.Set.fold_es - (fun delegate (ctxt, balance_updates) -> + (fun delegate ctxt -> let delegate_contract = Contract_repr.Implicit delegate in Frozen_deposits_storage.update_initial_amount ctxt delegate_contract - Tez_repr.zero - >>=? fun ctxt -> return (ctxt, balance_updates)) + Tez_repr.zero) delegates_to_remove - (ctxt, balance_updates) + ctxt let delegate_has_revealed_nonces delegate unrevelead_nonces_set = not (Signature.Public_key_hash.Set.mem delegate unrevelead_nonces_set) @@ -216,8 +215,7 @@ let cycle_end ctxt last_cycle = >>= fun ctxt -> distribute_endorsing_rewards ctxt last_cycle unrevealed_nonces >>=? fun (ctxt, balance_updates) -> - unfreeze_exceeding_deposits ctxt ~new_cycle ~balance_updates - >>=? fun (ctxt, balance_updates) -> + unfreeze_exceeding_deposits ctxt ~new_cycle >>=? fun ctxt -> Stake_storage.clear_at_cycle_end ctxt ~new_cycle >>=? fun ctxt -> Delegate_sampler.clear_outdated_sampling_data ctxt ~new_cycle >>=? fun ctxt -> update_activity ctxt last_cycle >>=? fun (ctxt, deactivated_delegates) -> @@ -242,4 +240,4 @@ let init_first_cycles ctxt = Misc.(0 --> preserved) >>=? fun ctxt -> let cycle = (Raw_context.current_level ctxt).cycle in - unfreeze_exceeding_deposits ~new_cycle:cycle ~balance_updates:[] ctxt + unfreeze_exceeding_deposits ~new_cycle:cycle ctxt diff --git a/src/proto_alpha/lib_protocol/delegate_cycles.mli b/src/proto_alpha/lib_protocol/delegate_cycles.mli index 21bb964c9b06..82e96bae71f0 100644 --- a/src/proto_alpha/lib_protocol/delegate_cycles.mli +++ b/src/proto_alpha/lib_protocol/delegate_cycles.mli @@ -45,5 +45,4 @@ val cycle_end : active stake involved in the calculation of baking rights for all cycles in the range [0, preserved_cycles]. It also freezes the deposits for all the active delegates. *) -val init_first_cycles : - Raw_context.t -> (Raw_context.t * Receipt_repr.balance_updates) tzresult Lwt.t +val init_first_cycles : Raw_context.t -> Raw_context.t tzresult Lwt.t diff --git a/src/proto_alpha/lib_protocol/init_storage.ml b/src/proto_alpha/lib_protocol/init_storage.ml index 7b76532d0a41..260d12c87088 100644 --- a/src/proto_alpha/lib_protocol/init_storage.ml +++ b/src/proto_alpha/lib_protocol/init_storage.ml @@ -250,8 +250,7 @@ let prepare_first_block _chain_id ctxt ~typecheck_smart_contract param.bootstrap_contracts param.bootstrap_smart_rollups >>=? fun (ctxt, bootstrap_balance_updates) -> - Delegate_cycles.init_first_cycles ctxt - >>=? fun (ctxt, deposits_balance_updates) -> + Delegate_cycles.init_first_cycles ctxt >>=? fun ctxt -> Vote_storage.init ctxt ~start_position:(Level_storage.current ctxt).level_position @@ -264,10 +263,7 @@ let prepare_first_block _chain_id ctxt ~typecheck_smart_contract >>=? fun ctxt -> Sc_rollup_inbox_storage.init_inbox ~predecessor ctxt >>=? fun ctxt -> Adaptive_inflation_storage.init_ema ctxt >>=? fun ctxt -> - return - ( ctxt, - commitments_balance_updates @ bootstrap_balance_updates - @ deposits_balance_updates ) + return (ctxt, commitments_balance_updates @ bootstrap_balance_updates) | Nairobi_017 (* Please update [next_protocol] and [previous_protocol] in [tezt/lib_tezos/protocol.ml] when you update this value. *) -> -- GitLab From 8b7eacd43cc90470aae98b9f71952dcefd6432ce Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Thu, 8 Jun 2023 17:23:22 +0200 Subject: [PATCH 5/6] Proto/Delegate_cycles: rename unfreeze_exceeding_deposits --- src/proto_alpha/lib_protocol/delegate_cycles.ml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/proto_alpha/lib_protocol/delegate_cycles.ml b/src/proto_alpha/lib_protocol/delegate_cycles.ml index f057e4868ee6..9ce53661b790 100644 --- a/src/proto_alpha/lib_protocol/delegate_cycles.ml +++ b/src/proto_alpha/lib_protocol/delegate_cycles.ml @@ -89,7 +89,7 @@ let max_frozen_deposits_and_delegates_to_remove ctxt ~from_cycle ~to_cycle = (Signature.Public_key_hash.Map.empty, cleared_cycle_delegates) cycles -let unfreeze_exceeding_deposits ctxt ~new_cycle = +let update_initial_frozen_deposits ctxt ~new_cycle = Delegate_storage.reset_forbidden_delegates ctxt >>= fun ctxt -> let max_slashable_period = Constants_storage.max_slashing_period ctxt in (* We want to be able to slash for at most [max_slashable_period] *) @@ -215,7 +215,7 @@ let cycle_end ctxt last_cycle = >>= fun ctxt -> distribute_endorsing_rewards ctxt last_cycle unrevealed_nonces >>=? fun (ctxt, balance_updates) -> - unfreeze_exceeding_deposits ctxt ~new_cycle >>=? fun ctxt -> + update_initial_frozen_deposits ctxt ~new_cycle >>=? fun ctxt -> Stake_storage.clear_at_cycle_end ctxt ~new_cycle >>=? fun ctxt -> Delegate_sampler.clear_outdated_sampling_data ctxt ~new_cycle >>=? fun ctxt -> update_activity ctxt last_cycle >>=? fun (ctxt, deactivated_delegates) -> @@ -240,4 +240,4 @@ let init_first_cycles ctxt = Misc.(0 --> preserved) >>=? fun ctxt -> let cycle = (Raw_context.current_level ctxt).cycle in - unfreeze_exceeding_deposits ~new_cycle:cycle ctxt + update_initial_frozen_deposits ~new_cycle:cycle ctxt -- GitLab From 7afc7c545a6fb84c8fcb3e4ee14c244d46d91fc8 Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Fri, 9 Jun 2023 12:38:05 +0200 Subject: [PATCH 6/6] Proto/Tests: update deactivation test --- .../consensus/test_frozen_deposits.ml | 23 +++++++------------ 1 file changed, 8 insertions(+), 15 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 b078130d01dd..33732011e9d3 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 @@ -182,12 +182,13 @@ let test_deposits_after_stake_removal () = >>=? fun frozen_deposits_2 -> Assert.equal_tez ~loc:__LOC__ frozen_deposits_2 initial_frozen_deposits_2 -let test_unfreeze_deposits_after_deactivation () = +let test_deposits_not_unfrozen_after_deactivation () = Context.init_with_constants2 constants >>=? fun (genesis, contracts) -> - let (contract1, account1), (_contract2, account2) = + let (_contract1, account1), (_contract2, account2) = get_first_2_accounts_contracts contracts in - Context.Delegate.full_balance (B genesis) account1 >>=? fun initial_balance -> + Context.Delegate.current_frozen_deposits (B genesis) account1 + >>=? fun initial_frozen_deposits -> (* [account1] will not participate (ie bake/endorse); we set the expected last cycles at which it is considered active and at which it has non-zero deposits *) @@ -208,22 +209,14 @@ let test_unfreeze_deposits_after_deactivation () = Context.Delegate.deactivated (B b) account1 >>=? fun is_deactivated -> Context.Delegate.current_frozen_deposits (B b) account1 >>=? fun frozen_deposits -> - (* the spendable balance *) - Context.Contract.balance (B b) contract1 >>=? fun balance -> let new_cycle = cycles_to_bake - n + 1 in Assert.equal_bool ~loc:__LOC__ is_deactivated (new_cycle > last_active_cycle) >>=? fun () -> - Assert.equal_bool - ~loc:__LOC__ - (new_cycle > last_cycle_with_deposits) - (* as soon as frozen_deposits are set to zero from a non-zero value v, v is - returned to the spendable balance of account1; in this particular - case, the spendable balance [balance] updated with v is precisely the - initial_balance.*) - (Tez.(frozen_deposits = zero) && Tez.(balance = initial_balance)) + (* deposits are not automatically unfrozen *) + Assert.equal_tez ~loc:__LOC__ frozen_deposits initial_frozen_deposits >>=? fun () -> loop b (pred n) in loop genesis cycles_to_bake >>=? fun (_b : Block.t) -> return_unit @@ -428,9 +421,9 @@ let tests = `Quick test_deposits_after_stake_removal; tztest - "unfreeze deposits after deactivation" + "deposits are not unfrozen after deactivation" `Quick - test_unfreeze_deposits_after_deactivation; + test_deposits_not_unfrozen_after_deactivation; tztest "frozen deposits with delegation" `Quick -- GitLab