diff --git a/docs/protocols/alpha.rst b/docs/protocols/alpha.rst index be92fd1226baceda3e6fd72468d76c9b3f3c43eb..86121420f9eb933a44809146423d3cb32f7be5d6 100644 --- a/docs/protocols/alpha.rst +++ b/docs/protocols/alpha.rst @@ -68,6 +68,9 @@ Data Availability Layer (ongoing) Adaptive Issuance (ongoing) ---------------------------- +- Active-delegates update and autostaking are now done before staking-rights computation + at cycle end. (MR :gl:`!11972`) + - The staking balance is now explicitly initialized when a delegate is registered. (MR :gl:`!11197`) - The issuance reward coeff is now computed only once. diff --git a/src/proto_alpha/lib_protocol/delegate_cycles.ml b/src/proto_alpha/lib_protocol/delegate_cycles.ml index 4bd8974f7cc2e04e4703b2ee02289002733c5157..29813716743275776055df15bb5fae86d075f704 100644 --- a/src/proto_alpha/lib_protocol/delegate_cycles.ml +++ b/src/proto_alpha/lib_protocol/delegate_cycles.ml @@ -196,33 +196,45 @@ let adjust_frozen_stakes ctxt ~deactivated_delegates : let cycle_end ctxt last_cycle = let open Lwt_result_syntax in + (* attributing attesting rewards *) let* ctxt, unrevealed_nonces = Seed_storage.cycle_end ctxt last_cycle in let* ctxt, attesting_balance_updates = distribute_attesting_rewards ctxt last_cycle unrevealed_nonces in + (* Applying slashing related to expiring denunciations *) let* ctxt, slashing_balance_updates = Delegate_slashed_deposits_storage.apply_and_clear_denunciations ctxt in let new_cycle = Cycle_repr.add last_cycle 1 in - let* ctxt = - Delegate_sampler.select_new_distribution_at_cycle_end ctxt ~new_cycle - in - let*! ctxt = Delegate_consensus_key.activate ctxt ~new_cycle in let*! ctxt = Already_denounced_storage.clear_outdated_cycle ctxt ~new_cycle in + (* Deactivating delegates which didn't participate to consensus for too long *) let* ctxt, deactivated_delegates = update_activity ctxt last_cycle in + (* Applying autostaking. Do not move before slashing. Keep before rights + computation for optimising rights*) let* ctxt, autostake_balance_updates = match Staking.staking_automation ctxt with | Manual_staking -> return (ctxt, []) | Auto_staking -> adjust_frozen_stakes ctxt ~deactivated_delegates in + (* Computing future staking rights *) + let* ctxt = + Delegate_sampler.select_new_distribution_at_cycle_end ctxt ~new_cycle + in + (* Activating consensus key for the cycle to come *) + let*! ctxt = Delegate_consensus_key.activate ctxt ~new_cycle in + (* trying to unforbid delegates for the cycle to come. *) let* ctxt = Forbidden_delegates_storage.update_at_cycle_end_after_slashing ctxt ~new_cycle in + (* clear deprecated cycles data. *) let* ctxt = Stake_storage.clear_at_cycle_end ctxt ~new_cycle in let* ctxt = Delegate_sampler.clear_outdated_sampling_data ctxt ~new_cycle in + (* activate delegate parameters for the cycle to come. *) let*! ctxt = Delegate_staking_parameters.activate ctxt ~new_cycle in + (* updating AI coefficient. It should remain after all balance changes of the + cycle-end operations *) let* ctxt = Adaptive_issuance_storage.update_stored_rewards_at_cycle_end ctxt ~new_cycle in 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 6dec06831d79bbdbf59ddb42c9abc4c6a25f2f9c..594b17134a0729acaaf813d4f0c0eb85cb9f95fd 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 @@ -483,8 +483,10 @@ 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 + (* autostaking happens before staking right computations, + so the limit will apply in constants.consensus_rights_delay+1 *) let expected_number_of_cycles_with_rights_from_previous_deposit = - constants.consensus_rights_delay + Constants.max_slashing_period - 1 + constants.consensus_rights_delay in let* b = Block.bake_until_n_cycle_end @@ -513,8 +515,8 @@ let test_cannot_bake_with_zero_deposits_limit () = | Block.No_slots_found_for _ -> true | _ -> false) in - (* Unstaked frozen deposits are released one cycle later. *) - let* b = Block.bake_until_cycle_end b in + (* Unstaked frozen deposits are released two cycles later. *) + let* b = Block.bake_until_n_cycle_end 2 b in let* ufd = Context.Contract.unstaked_frozen_balance (B b) (Implicit account1) in diff --git a/tezt/tests/adaptive_issuance.ml b/tezt/tests/adaptive_issuance.ml index f6732fb1781efafa15592f7c13bb7910aeb0302e..19d8d03cef7f9d0e2e2ff2ca618dd7902ce3e61d 100644 --- a/tezt/tests/adaptive_issuance.ml +++ b/tezt/tests/adaptive_issuance.ml @@ -35,6 +35,8 @@ let blocks_per_cycle = 4 let nonce_revelation_threshold = 2 +let bootstrap_accounts = Constant.all_secret_keys + module Helpers = struct let level_type : RPC.level Check.typ = Check.convert @@ -52,6 +54,17 @@ module Helpers = struct ~error_msg:"expected current_period = %R, got %L" ; unit + let bake ?ai_vote + ?(keys = List.map (fun x -> x.Account.alias) bootstrap_accounts) ~endpoint + ~protocol client = + Client.bake_for + ~endpoint + ~minimal_timestamp:true + ~protocol + ~keys + client + ?ai_vote + let bake_n_cycles bake ?keys n client = let* current_level = get_current_level client in let current_level = current_level.level in @@ -135,8 +148,6 @@ let default_overrides = (["adaptive_issuance_launch_ema_threshold"], `Int 1); ] -let bootstrap_accounts = Constant.all_secret_keys - let launch_ema_threshold client = let* json = Client.RPC.call client @@ RPC.get_chain_block_context_constants () @@ -171,13 +182,7 @@ let activate_ai protocol sandbox_client sandbox_endpoint = assert (JSON.is_null launch_cycle) ; (* Make delegate vote for AI activation*) let bake ?keys client = - Client.bake_for - ~endpoint:sandbox_endpoint - ~minimal_timestamp:true - ~protocol - ?keys - client - ~ai_vote:On + Helpers.bake ~ai_vote:On ~endpoint:sandbox_endpoint ~protocol ?keys client in (* The vote should have passed during the first cycle *) let* () = @@ -404,12 +409,21 @@ type bu_check = { msg : string; } -(* some values might be slightly different (+-1 mutez) because of roundings and +(* some values might be slightly different (+-[margin] mutez) because of roundings and randomness in baking rights that may affect the overall rewards coming from previous blocks, to avoid flakiness we test the "rounded range" of those values *) -let check_with_roundings got expected = - got >= expected - 1 && got <= expected + 1 +let check_with_roundings ?(margin = 1) got expected = + got >= expected - margin && got <= expected + margin + +let assert_with_roundings ~__LOC__ ?margin got expected = + if not (check_with_roundings ?margin got expected) then + Test.fail + "@[%s: Asserted equality (up to rounding) failed. got %d, expected \ + %d.@]@." + __LOC__ + got + expected let check_balance_updates balance_updates (predicates : bu_check list) = List.iter @@ -426,11 +440,13 @@ let check_balance_updates balance_updates (predicates : bu_check list) = pre_filtered) then Test.fail - "Inconsistant balance update: %s, could it be a regression, got: %s" + "@[Inconsistant balance update, could it be a regression.@. \ + Expected:@ @[%s, change amount: %d@]@.Got:@ @[%s@]@]" msg + change (List.fold_left (fun acc x -> - acc ^ "\n" ^ Operation_receipt.Balance_updates.to_string x) + acc ^ "@ " ^ Operation_receipt.Balance_updates.to_string x) "" pre_filtered)) predicates @@ -739,10 +755,7 @@ let test_staking = Constant.bootstrap2.public_key_hash in Log.info "Numerator/denominator before: %d/%d " numerator denominator ; - - let bake ?keys client = - Client.bake_for ~endpoint ~minimal_timestamp:true ~protocol ?keys client - in + let bake = Helpers.bake ~ai_vote:Pass ~endpoint ~protocol in let* () = Helpers.bake_n_cycles bake 1 client_1 in let stake0 = @@ -1074,50 +1087,49 @@ let test_staking = (* check slashed and rewarded amounts *) (* total amounts *) - let total_amount_rewarded = 1450001824 in - let total_amount_slashed = 8700010946 in + let total_amount_rewarded = 1450001868 in + let total_amount_slashed = 8700011216 in (* slashed stakers (including baker) unstake deposit *) let amount_rewarded_from_unstake_stakers_deposits = 7142857 in - let amount_slashed_from_unstake_stakers_deposits = 42857144 in + let amount_slashed_from_unstake_stakers_deposits = 42857145 in (* slashed stake *) let amount_rewarded_from_stakers_deposits = 7178393 in let amount_slashed_from_stakers_deposits = 43070362 in (* slashing baker (bootstrap2) stake*) - let amount_rewarded_from_baker_deposits = 1435680574 in - let amount_slashed_from_baker_deposits = 8614083440 in - - assert ( - check_with_roundings - amount_rewarded_from_unstake_stakers_deposits - (int_of_float (float amount_slashed_from_unstake_stakers_deposits /. 6.))) ; - - assert ( - check_with_roundings - amount_rewarded_from_stakers_deposits - (int_of_float (float amount_slashed_from_stakers_deposits /. 6.))) ; - - assert ( - check_with_roundings - amount_rewarded_from_baker_deposits - (int_of_float (float amount_slashed_from_baker_deposits /. 6.))) ; - - assert ( - check_with_roundings - (amount_rewarded_from_unstake_stakers_deposits - + amount_rewarded_from_stakers_deposits - + amount_rewarded_from_baker_deposits) - total_amount_rewarded) ; - - assert ( - check_with_roundings - (amount_slashed_from_unstake_stakers_deposits - + amount_slashed_from_stakers_deposits + amount_slashed_from_baker_deposits - ) - total_amount_slashed) ; - + let amount_rewarded_from_baker_deposits = 1435680618 in + let amount_slashed_from_baker_deposits = 8614083709 in + + assert_with_roundings + ~__LOC__ + amount_rewarded_from_unstake_stakers_deposits + (int_of_float (float amount_slashed_from_unstake_stakers_deposits /. 6.)) ; + + assert_with_roundings + ~__LOC__ + amount_rewarded_from_stakers_deposits + (int_of_float (float amount_slashed_from_stakers_deposits /. 6.)) ; + + assert_with_roundings + ~__LOC__ + amount_rewarded_from_baker_deposits + (int_of_float (float amount_slashed_from_baker_deposits /. 6.)) ; + + assert_with_roundings + ~margin:3 (* as we sum 3 amounts *) + ~__LOC__ + (amount_rewarded_from_unstake_stakers_deposits + + amount_rewarded_from_stakers_deposits + amount_rewarded_from_baker_deposits + ) + total_amount_rewarded ; + assert_with_roundings + ~margin:3 (* as we sum 3 amounts *) + ~__LOC__ + (amount_slashed_from_unstake_stakers_deposits + + amount_slashed_from_stakers_deposits + amount_slashed_from_baker_deposits) + total_amount_slashed ; let check_opr ~kind ~category ~change ~staker ~msg ~delayed_operation_hash = let open Operation_receipt.Balance_updates in { 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 cb0219480fd431e4e399037fd94ea8d5a0e7908d..5cdd1a09377af71b0f5465d23d7cd7bae0158d91 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 @@ -104,7 +104,7 @@ This sequence of operations was run: ./octez-client rpc get '/chains/main/blocks/head/context/delegates/[PUBLIC_KEY_HASH]' { "full_balance": "238000528932", "current_frozen_deposits": "200000181573", - "frozen_deposits": "200000083984", "staking_balance": "238000528932", + "frozen_deposits": "200000083983", "staking_balance": "238000528932", "delegated_contracts": [ "[PUBLIC_KEY_HASH]" ], "delegated_balance": "0", "min_delegated_in_current_cycle": "4", "deactivated": false, "grace_period": 4, "pending_denunciations": false, 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 2f3c81352c2accb4148fbf0949a05f44cfc8c416..945261d8da9ecd83c182990e665d3d03798dcc56 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 @@ -104,7 +104,7 @@ This sequence of operations was run: ./octez-client rpc get '/chains/main/blocks/head/context/delegates/[PUBLIC_KEY_HASH]' { "full_balance": "238000528932", "current_frozen_deposits": "200000181573", - "frozen_deposits": "200000083984", "staking_balance": "238000528932", + "frozen_deposits": "200000083983", "staking_balance": "238000528932", "delegated_contracts": [ "[PUBLIC_KEY_HASH]" ], "delegated_balance": "0", "min_delegated_in_current_cycle": "4", "deactivated": false, "grace_period": 4, "pending_denunciations": false, 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 7efa88268e391bb12548c2fee620c3f78d8b456b..e86e3964537fdbfd963b51d9d12b96cf3ae3db7e 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 @@ -104,7 +104,7 @@ This sequence of operations was run: ./octez-client rpc get '/chains/main/blocks/head/context/delegates/[PUBLIC_KEY_HASH]' { "full_balance": "238000509096", "current_frozen_deposits": "200000164892", - "frozen_deposits": "200000067317", "staking_balance": "238000509096", + "frozen_deposits": "200000067302", "staking_balance": "238000509096", "delegated_contracts": [ "[PUBLIC_KEY_HASH]" ], "delegated_balance": "0", "min_delegated_in_current_cycle": "18", "deactivated": false, "grace_period": 4, "pending_denunciations": false, 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 44c9111831859d4d2fa8b29728bbf40f36b952bd..f845c4c2c4eed7c1bf12fb364ae71425833fc893 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 @@ -104,7 +104,7 @@ This sequence of operations was run: ./octez-client rpc get '/chains/main/blocks/head/context/delegates/[PUBLIC_KEY_HASH]' { "full_balance": "238000509096", "current_frozen_deposits": "200000164892", - "frozen_deposits": "200000067317", "staking_balance": "238000509096", + "frozen_deposits": "200000067302", "staking_balance": "238000509096", "delegated_contracts": [ "[PUBLIC_KEY_HASH]" ], "delegated_balance": "0", "min_delegated_in_current_cycle": "18", "deactivated": false, "grace_period": 4, "pending_denunciations": false,