diff --git a/src/proto_alpha/lib_protocol/delegate_cycles.ml b/src/proto_alpha/lib_protocol/delegate_cycles.ml index a3c979c34763e7ad8337f63f9cbdcf28e74e0b4e..9ce53661b790f340b1bcf70bc53d049ebb783dfe 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 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] *) @@ -110,7 +109,7 @@ let unfreeze_exceeding_deposits ?(origin = Receipt_repr.Block_application) ctxt 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 @@ -119,51 +118,24 @@ 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 -> - return (ctxt, balance_updates) - else return (ctxt, balance_updates)) + return ctxt + else return 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. *) + 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 -> - 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)) + 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) @@ -243,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) -> + 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) -> @@ -256,7 +227,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 -> @@ -269,4 +240,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 + update_initial_frozen_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 1b7f8a87bad6cc85ca7982ef207c33d5e6fb0ae0..82e96bae71f0466f78f876e8e7d050424c160ad2 100644 --- a/src/proto_alpha/lib_protocol/delegate_cycles.mli +++ b/src/proto_alpha/lib_protocol/delegate_cycles.mli @@ -40,12 +40,9 @@ 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 +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 8e97b0ceef4ef9f4af34f719b20ff0fd5c1aa912..260d12c870887ca8235374e1cf4ddd23d672746f 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 ~origin:Protocol_migration - >>=? 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. *) -> 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 b078130d01ddfc713fe70d3f330bba009ba91f23..33732011e9d3d7c76269e3da69e62a101e5a3d76 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