diff --git a/scripts/profile_alpha.patch b/scripts/profile_alpha.patch index 45c2857bbff3ca83e5906e6e9253b6b4945c7ecb..e6018b28c21c2b57332aefb9aaebc702a5111e0c 100644 --- a/scripts/profile_alpha.patch +++ b/scripts/profile_alpha.patch @@ -1,7 +1,7 @@ -From dee7499f02f6a562c6d2e4669b5304b0933e6d35 Mon Sep 17 00:00:00 2001 -From: Pierrick Couderc -Date: Mon, 15 Dec 2025 14:51:38 +0100 -Subject: [PATCH 1/1] Proto/Alpha: profiler patch +From f38494c05b0b2672c470163a1bffe0269271e133 Mon Sep 17 00:00:00 2001 +From: Lucas Randazzo +Date: Thu, 18 Dec 2025 11:52:50 +0100 +Subject: [PATCH 1/1] Patch alpha --- src/lib_protocol_environment/sigs/v16.in.ml | 2 + @@ -9,37 +9,37 @@ Subject: [PATCH 1/1] Proto/Alpha: profiler patch .../sigs/v16/profiler.mli | 39 +++++++++ src/proto_alpha/lib_protocol/apply.ml | 17 ++-- src/proto_alpha/lib_protocol/baking.ml | 4 +- - .../lib_protocol/delegate_cycles.ml | 78 ++++++++++++++--- + .../lib_protocol/delegate_cycles.ml | 79 ++++++++++++++--- src/proto_alpha/lib_protocol/dune | 2 + src/proto_alpha/lib_protocol/init_storage.ml | 26 +++++- src/proto_alpha/lib_protocol/raw_context.ml | 4 +- src/proto_alpha/lib_protocol/script_cache.ml | 19 +++-- .../lib_protocol/script_interpreter.ml | 15 ++++ .../lib_protocol/script_ir_translator.ml | 26 +++--- - 12 files changed, 250 insertions(+), 67 deletions(-) + 12 files changed, 251 insertions(+), 67 deletions(-) create mode 100644 src/lib_protocol_environment/sigs/v16/profiler.mli diff --git a/src/lib_protocol_environment/sigs/v16.in.ml b/src/lib_protocol_environment/sigs/v16.in.ml -index 50c910a229..035e47c5bf 100644 +index 50c910a2290..035e47c5bff 100644 --- a/src/lib_protocol_environment/sigs/v16.in.ml +++ b/src/lib_protocol_environment/sigs/v16.in.ml @@ -105,6 +105,8 @@ module type T = sig - + module Operation_list_list_hash : [%sig "v16/operation_list_list_hash.mli"] - + + module Profiler : [%sig "v16/profiler.mli"] + module Protocol_hash : [%sig "v16/protocol_hash.mli"] - + module Context_hash : [%sig "v16/context_hash.mli"] diff --git a/src/lib_protocol_environment/sigs/v16.ml b/src/lib_protocol_environment/sigs/v16.ml -index 31ba26fc7f..302929ee88 100644 +index 31ba26fc7f1..302929ee889 100644 --- a/src/lib_protocol_environment/sigs/v16.ml +++ b/src/lib_protocol_environment/sigs/v16.ml @@ -10060,6 +10060,51 @@ end # 106 "v16.in.ml" - - + + + module Profiler : sig +# 1 "v16/profiler.mli" +(*****************************************************************************) @@ -94,17 +94,17 @@ index 31ba26fc7f..302929ee88 100644 end -# 108 "v16.in.ml" +# 110 "v16.in.ml" - - + + module Context_hash : sig @@ -10143,7 +10188,7 @@ end - + type version = Version.t end -# 110 "v16.in.ml" +# 112 "v16.in.ml" - - + + module Sapling : sig @@ -10291,7 +10336,7 @@ module Verification : sig val final_check : t -> UTXO.transaction -> string -> bool @@ -112,8 +112,8 @@ index 31ba26fc7f..302929ee88 100644 end -# 112 "v16.in.ml" +# 114 "v16.in.ml" - - + + module Timelock : sig @@ -10348,7 +10393,7 @@ val open_chest : chest -> chest_key -> time:int -> opening_result Used for gas accounting*) @@ -121,8 +121,8 @@ index 31ba26fc7f..302929ee88 100644 end -# 114 "v16.in.ml" +# 116 "v16.in.ml" - - + + module Vdf : sig @@ -10436,7 +10481,7 @@ val prove : discriminant -> challenge -> difficulty -> result * proof @raise Invalid_argument when inputs are invalid *) @@ -130,26 +130,26 @@ index 31ba26fc7f..302929ee88 100644 end -# 116 "v16.in.ml" +# 118 "v16.in.ml" - - + + module Micheline : sig @@ -10496,7 +10541,7 @@ val annotations : ('l, 'p) node -> string list - + val strip_locations : (_, 'p) node -> 'p canonical end -# 118 "v16.in.ml" +# 120 "v16.in.ml" - - + + module Block_header : sig @@ -10553,7 +10598,7 @@ type t = {shell : shell_header; protocol_data : bytes} - + include S.HASHABLE with type t := t and type hash := Block_hash.t end -# 120 "v16.in.ml" +# 122 "v16.in.ml" - - + + module Bounded : sig @@ -10702,7 +10747,7 @@ module Int8 (B : BOUNDS with type ocaml_type := int) : module Uint8 (B : BOUNDS with type ocaml_type := int) : @@ -157,8 +157,8 @@ index 31ba26fc7f..302929ee88 100644 end -# 122 "v16.in.ml" +# 124 "v16.in.ml" - - + + module Fitness : sig @@ -10736,7 +10781,7 @@ end compared in a lexicographical order (longer list are greater). *) @@ -166,17 +166,17 @@ index 31ba26fc7f..302929ee88 100644 end -# 124 "v16.in.ml" +# 126 "v16.in.ml" - - + + module Operation : sig @@ -10780,7 +10825,7 @@ type t = {shell : shell_header; proto : bytes} - + include S.HASHABLE with type t := t and type hash := Operation_hash.t end -# 126 "v16.in.ml" +# 128 "v16.in.ml" - - + + module Context : sig @@ -11417,7 +11462,7 @@ module Cache : and type key = cache_key @@ -184,8 +184,8 @@ index 31ba26fc7f..302929ee88 100644 end -# 128 "v16.in.ml" +# 130 "v16.in.ml" - - + + module Updater : sig @@ -11953,7 +11998,7 @@ end not complete until [init] in invoked. *) @@ -193,8 +193,8 @@ index 31ba26fc7f..302929ee88 100644 end -# 130 "v16.in.ml" +# 132 "v16.in.ml" - - + + module RPC_context : sig @@ -12107,7 +12152,7 @@ val make_opt_call3 : 'i -> @@ -202,17 +202,17 @@ index 31ba26fc7f..302929ee88 100644 end -# 132 "v16.in.ml" +# 134 "v16.in.ml" - - + + module Context_binary : sig @@ -12150,7 +12195,7 @@ module Tree : - + val make_empty_context : ?root:string -> unit -> t end -# 134 "v16.in.ml" +# 136 "v16.in.ml" - - + + module Wasm_2_0_0 : sig @@ -12224,7 +12269,7 @@ module Make val get_info : Tree.tree -> info Lwt.t @@ -220,8 +220,8 @@ index 31ba26fc7f..302929ee88 100644 end -# 136 "v16.in.ml" +# 138 "v16.in.ml" - - + + module Plonk : sig @@ -12343,7 +12388,7 @@ val scalar_array_encoding : scalar array Data_encoding.t on the given [inputs] according to the [public_parameters]. *) @@ -229,8 +229,8 @@ index 31ba26fc7f..302929ee88 100644 end -# 138 "v16.in.ml" +# 140 "v16.in.ml" - - + + module Dal : sig @@ -12498,7 +12543,7 @@ val share_is_trap : traps_fraction:Q.t -> @@ -238,8 +238,8 @@ index 31ba26fc7f..302929ee88 100644 end -# 140 "v16.in.ml" +# 142 "v16.in.ml" - - + + module Skip_list : sig @@ -12730,7 +12775,7 @@ module Make (_ : sig val basis : int @@ -247,8 +247,8 @@ index 31ba26fc7f..302929ee88 100644 end -# 142 "v16.in.ml" +# 144 "v16.in.ml" - - + + module Smart_rollup : sig @@ -12787,7 +12832,7 @@ module Inbox_hash : S.HASH (** Smart rollup merkelized payload hashes' hash *) @@ -256,20 +256,20 @@ index 31ba26fc7f..302929ee88 100644 end -# 144 "v16.in.ml" +# 146 "v16.in.ml" - - + + module Riscv : sig @@ -12850,6 +12895,6 @@ val bytes_to_output_proof : bytes -> (output_proof, string) result - + val get_current_level : state -> int32 option Lwt.t end -# 146 "v16.in.ml" +# 148 "v16.in.ml" - + end diff --git a/src/lib_protocol_environment/sigs/v16/profiler.mli b/src/lib_protocol_environment/sigs/v16/profiler.mli new file mode 100644 -index 0000000000..efbc52c38a +index 00000000000..efbc52c38ab --- /dev/null +++ b/src/lib_protocol_environment/sigs/v16/profiler.mli @@ -0,0 +1,39 @@ @@ -313,7 +313,7 @@ index 0000000000..efbc52c38a + +val mark : verbosity -> ids -> unit diff --git a/src/proto_alpha/lib_protocol/apply.ml b/src/proto_alpha/lib_protocol/apply.ml -index b412a56dcb..587de1c9f1 100644 +index 18aaf484e9e..95cd9a30044 100644 --- a/src/proto_alpha/lib_protocol/apply.ml +++ b/src/proto_alpha/lib_protocol/apply.ml @@ -2560,7 +2560,11 @@ let apply_manager_operations ctxt ~payload_producer chain_id ~mempool_mode @@ -344,11 +344,11 @@ index b412a56dcb..587de1c9f1 100644 + let+ ctxt = Bootstrap.cycle_end ctxt last_cycle in + (ctxt, balance_updates, deactivated)) + [@profiler.record_s {verbosity = Notice} "delegate end cycle"] - + let apply_liquidity_baking_subsidy ctxt ~per_block_vote = let open Lwt_result_syntax in diff --git a/src/proto_alpha/lib_protocol/baking.ml b/src/proto_alpha/lib_protocol/baking.ml -index 8c060118f0..d763f1fdbe 100644 +index 8c060118f0e..d763f1fdbed 100644 --- a/src/proto_alpha/lib_protocol/baking.ml +++ b/src/proto_alpha/lib_protocol/baking.ml @@ -114,7 +114,7 @@ type ordered_slots = { @@ -366,14 +366,14 @@ index 8c060118f0..d763f1fdbe 100644 in - return (ctxt, map) + return (ctxt, map)) [@profiler.record_s {verbosity = Notice} "attesting_rights"] - + let incr_slot att_rights = let one = Attesting_power.make ~slots:1 ~baking_power:0L in diff --git a/src/proto_alpha/lib_protocol/delegate_cycles.ml b/src/proto_alpha/lib_protocol/delegate_cycles.ml -index a666a47b9b..6c87495e8a 100644 +index 2d31093dd88..8241240ad43 100644 --- a/src/proto_alpha/lib_protocol/delegate_cycles.ml +++ b/src/proto_alpha/lib_protocol/delegate_cycles.ml -@@ -225,42 +225,92 @@ let distribute_attesting_rewards ctxt last_cycle unrevealed_nonces = +@@ -225,50 +225,101 @@ let distribute_attesting_rewards ctxt last_cycle unrevealed_nonces = let cycle_end ctxt last_cycle = let open Lwt_result_syntax in (* attributing attesting rewards *) @@ -417,6 +417,15 @@ index a666a47b9b..6c87495e8a 100644 + ctxt + last_cycle [@profiler.record_s {verbosity = Notice} "update activity"]) + in + (* Reset SWRR credits for delegates that have been deactivated *) + let* ctxt = + if Constants_storage.swrr_new_baker_lottery_enable ctxt then + Swrr_sampler.reset_credit_for_deactivated_delegates + ctxt + deactivated_delegates ++ [@profiler.record_s {verbosity = Notice} "reset SWRR credit"] + else return ctxt + in (* Computing future staking rights *) let* ctxt = - Delegate_sampler.select_new_distribution_at_cycle_end ctxt ~new_cycle @@ -481,7 +490,7 @@ index a666a47b9b..6c87495e8a 100644 let balance_updates = slashing_balance_updates @ attesting_balance_updates in return (ctxt, balance_updates, deactivated_delegates) diff --git a/src/proto_alpha/lib_protocol/dune b/src/proto_alpha/lib_protocol/dune -index e57a9089a6..96126b26b5 100644 +index 25be8b874c6..3bce2e5fa7a 100644 --- a/src/proto_alpha/lib_protocol/dune +++ b/src/proto_alpha/lib_protocol/dune @@ -23,6 +23,8 @@ @@ -494,7 +503,7 @@ index e57a9089a6..96126b26b5 100644 (flags (:standard) diff --git a/src/proto_alpha/lib_protocol/init_storage.ml b/src/proto_alpha/lib_protocol/init_storage.ml -index 20be8e1481..9ac5fdfd81 100644 +index 20be8e1481c..9ac5fdfd81b 100644 --- a/src/proto_alpha/lib_protocol/init_storage.ml +++ b/src/proto_alpha/lib_protocol/init_storage.ml @@ -259,10 +259,20 @@ let prepare_first_block chain_id ctxt ~typecheck_smart_contract @@ -543,7 +552,7 @@ index 20be8e1481..9ac5fdfd81 100644 return (ctxt, []) (* End of alpha predecessor stitching. Comment used for automatic snapshot *) diff --git a/src/proto_alpha/lib_protocol/raw_context.ml b/src/proto_alpha/lib_protocol/raw_context.ml -index a567971a70..870b098ec0 100644 +index a567971a705..870b098ec07 100644 --- a/src/proto_alpha/lib_protocol/raw_context.ml +++ b/src/proto_alpha/lib_protocol/raw_context.ml @@ -1641,12 +1641,12 @@ let prepare_first_block ~level ~timestamp chain_id ctxt = @@ -560,9 +569,9 @@ index a567971a70..870b098ec0 100644 + ~all_bakers_attest_first_level:None [@profiler.record_s {verbosity = Notice} "Prepare"]) in (previous_proto, previous_proto_constants, ctxt) - + diff --git a/src/proto_alpha/lib_protocol/script_cache.ml b/src/proto_alpha/lib_protocol/script_cache.ml -index 35cc6b5178..5c679ad606 100644 +index 35cc6b51781..5c679ad606b 100644 --- a/src/proto_alpha/lib_protocol/script_cache.ml +++ b/src/proto_alpha/lib_protocol/script_cache.ml @@ -98,15 +98,16 @@ let find ctxt addr = @@ -588,11 +597,11 @@ index 35cc6b5178..5c679ad606 100644 + in + return (ctxt, identifier, Some (unparsed_script, script_ir))) + [@profiler.record_s {verbosity = Notice} "cache_miss"]) - + let update ctxt identifier updated_script approx_size = Cache.update ctxt identifier (Some (updated_script, approx_size)) diff --git a/src/proto_alpha/lib_protocol/script_interpreter.ml b/src/proto_alpha/lib_protocol/script_interpreter.ml -index 182af698fa..23620cd13c 100644 +index 182af698fae..23620cd13c7 100644 --- a/src/proto_alpha/lib_protocol/script_interpreter.ml +++ b/src/proto_alpha/lib_protocol/script_interpreter.ml @@ -1878,10 +1878,12 @@ let execute_any_arg logger ctxt mode step_constants ~entrypoint ~internal @@ -611,7 +620,7 @@ index 182af698fa..23620cd13c 100644 @@ -1959,6 +1961,19 @@ let execute_any_arg logger ctxt mode step_constants ~entrypoint ~internal }, ctxt ) - + +let execute_any_arg logger ctxt mode step_constants ~entrypoint ~internal + unparsed_script cached_script arg = + execute_any_arg @@ -629,7 +638,7 @@ index 182af698fa..23620cd13c 100644 ~script ~entrypoint ~parameter_ty ~location ~parameter ~internal = execute_any_arg diff --git a/src/proto_alpha/lib_protocol/script_ir_translator.ml b/src/proto_alpha/lib_protocol/script_ir_translator.ml -index 1fb6a40176..1e168c553b 100644 +index 1fb6a401765..1e168c553b0 100644 --- a/src/proto_alpha/lib_protocol/script_ir_translator.ml +++ b/src/proto_alpha/lib_protocol/script_ir_translator.ml @@ -5319,21 +5319,27 @@ let parse_script : @@ -675,9 +684,8 @@ index 1fb6a40176..1e168c553b 100644 ty - t + t [@profiler.record_s {verbosity = Notice} "parse_data"] - + let parse_view ~elab_conf ctxt ty view = parse_view ~unparse_code_rec ~elab_conf ctxt ty view --- -2.52.0 - +-- +2.51.0 diff --git a/src/proto_alpha/lib_plugin/RPC.ml b/src/proto_alpha/lib_plugin/RPC.ml index 09b5c218bbfdf99dbe2289351e09f8dc24fc5c61..289b1d08919f1fd82cd6f9269823e4daf486c74e 100644 --- a/src/proto_alpha/lib_plugin/RPC.ml +++ b/src/proto_alpha/lib_plugin/RPC.ml @@ -4259,6 +4259,15 @@ let get_blocks_preservation_cycles ~get_context = in return constants_parametrics.blocks_preservation_cycles +type swrr_credit_entry = {delegate : Signature.Public_key_hash.t; credit : Z.t} + +let swrr_credit_entry_encoding = + let open Data_encoding in + conv + (fun {delegate; credit} -> (delegate, credit)) + (fun (delegate, credit) -> {delegate; credit}) + (obj2 (req "delegate" Signature.Public_key_hash.encoding) (req "credit" z)) + module S = struct open Data_encoding @@ -4330,6 +4339,24 @@ module S = struct let open RPC_query in query Fun.id |+ opt_field "cycle" Cycle.rpc_arg Fun.id |> seal + let swrr_selected_bakers = + RPC_service.get_service + ~description: + "Returns the selected bakers for the round 0 of the cycle, selected \ + via the SWRR algorithm." + ~query:cycle_query + ~output:Data_encoding.(option (list Signature.Public_key_hash.encoding)) + RPC_path.(path / "swrr_selected_bakers") + + let swrr_credits = + RPC_service.get_service + ~description: + "Returns the current SWRR state which contains the remaining credits \ + for all the active delegates." + ~query:RPC_query.empty + ~output:Data_encoding.(list swrr_credit_entry_encoding) + RPC_path.(path / "swrr_credits") + let tz4_baker_number_ratio = RPC_service.get_service ~description:"Returns the ratio of active bakers using a tz4." @@ -4419,6 +4446,25 @@ let register () = List.map (fun (x, y) -> (Consensus_key.pkh x, y)) stake_info_list in return (total_stake, stake_info_list)) ; + Registration.register0 ~chunked:false S.swrr_selected_bakers (fun ctxt q () -> + let cycle = + Option.value ~default:(Cycle.current ctxt) q + |> Cycle.to_int32 |> Cycle_repr.of_int32_exn + in + let ctxt = Alpha_context.Internal_for_tests.to_raw ctxt in + Storage.Stake.Selected_bakers.find ctxt cycle) ; + Registration.register0 ~chunked:false S.swrr_credits (fun ctxt () () -> + let ctxt = Alpha_context.Internal_for_tests.to_raw ctxt in + Stake_storage.fold_on_active_delegates_with_minimal_stake_es + ctxt + ~order:`Undefined + ~init:[] + ~f:(fun pkh acc -> + let* credit = + Storage.Contract.SWRR_credit.get ctxt (Contract_repr.Implicit pkh) + in + let entry = {delegate = pkh; credit} in + return (entry :: acc))) ; Registration.register0 ~chunked:false S.tz4_baker_number_ratio diff --git a/src/proto_alpha/lib_protocol/constants_storage.ml b/src/proto_alpha/lib_protocol/constants_storage.ml index a3fb00818a1da0ecab91f704005357dacf3bf837..8da532fcb49aa240efc714533e51e5b1cde2493f 100644 --- a/src/proto_alpha/lib_protocol/constants_storage.ml +++ b/src/proto_alpha/lib_protocol/constants_storage.ml @@ -298,6 +298,9 @@ let round_durations ctxt = Raw_context.round_durations ctxt let native_contracts_enable c = (Raw_context.constants c).native_contracts_enable +let swrr_new_baker_lottery_enable c = + (Raw_context.constants c).swrr_new_baker_lottery_enable + let all ctxt = Constants_repr.all_of_parametric (parametric ctxt) (* Derived pseudo-constants *) diff --git a/src/proto_alpha/lib_protocol/constants_storage.mli b/src/proto_alpha/lib_protocol/constants_storage.mli index e2474f00d26d74268cc544fe6a60d2fccea3726d..9f69793a8d856d24fee797a0283f281858196205 100644 --- a/src/proto_alpha/lib_protocol/constants_storage.mli +++ b/src/proto_alpha/lib_protocol/constants_storage.mli @@ -178,6 +178,9 @@ val round_durations : Raw_context.t -> Round_repr.Durations.t (** Native contracts feature flag *) val native_contracts_enable : Raw_context.t -> bool +(** SWRR new baker lottery feature flag *) +val swrr_new_baker_lottery_enable : Raw_context.t -> bool + (** Builds a representation of all constants (fixed and parametric) from the context. *) val all : Raw_context.t -> Constants_repr.t diff --git a/src/proto_alpha/lib_protocol/delegate_cycles.ml b/src/proto_alpha/lib_protocol/delegate_cycles.ml index a666a47b9b4036946cc3ec1e02a5db8174bd03e7..2d31093dd88fda1cf35027166817d67e6ccfb527 100644 --- a/src/proto_alpha/lib_protocol/delegate_cycles.ml +++ b/src/proto_alpha/lib_protocol/delegate_cycles.ml @@ -240,6 +240,14 @@ let cycle_end ctxt last_cycle = in (* Deactivating delegates which didn't participate to consensus for too long *) let* ctxt, deactivated_delegates = update_activity ctxt last_cycle in + (* Reset SWRR credits for delegates that have been deactivated *) + let* ctxt = + if Constants_storage.swrr_new_baker_lottery_enable ctxt then + Swrr_sampler.reset_credit_for_deactivated_delegates + ctxt + deactivated_delegates + else return ctxt + in (* Computing future staking rights *) let* ctxt = Delegate_sampler.select_new_distribution_at_cycle_end ctxt ~new_cycle diff --git a/src/proto_alpha/lib_protocol/delegate_sampler.ml b/src/proto_alpha/lib_protocol/delegate_sampler.ml index 89551ba3675e3b9ccaed5203570be675e3e1b264..d5c42f1f57d2916ed71984e740664688c426337f 100644 --- a/src/proto_alpha/lib_protocol/delegate_sampler.ml +++ b/src/proto_alpha/lib_protocol/delegate_sampler.ml @@ -84,11 +84,12 @@ module Delegate_sampler_state = struct return (ctxt, v) | Some v -> return (ctxt, v) - let remove_existing ctxt cycle = + let remove ctxt cycle = let open Lwt_result_syntax in let id = identifier_of_cycle cycle in let*? ctxt = Cache.update ctxt id None in - Storage.Delegate_sampler_state.remove_existing ctxt cycle + let*! ctxt = Storage.Delegate_sampler_state.remove ctxt cycle in + return ctxt end module Random = struct @@ -167,14 +168,19 @@ module Random = struct return (c, pk) end -let baking_rights_owner c (level : Level_repr.t) ~round = +let baking_rights_owner ctxt (level : Level_repr.t) ~round = let open Lwt_result_syntax in (* This committee is used for rounds *) - let committee_size = Constants_storage.consensus_committee_size c in + let committee_size = Constants_storage.consensus_committee_size ctxt in (* We use [Round.to_slot] to have a limited number of unique rounds (it should loop after some time) *) let*? slot = Round_repr.to_slot ~committee_size round in - let* ctxt, pk = Random.owner c level (Slot_repr.to_int slot) in + let* ctxt, pk = + let* ctxt, baker_opt = Swrr_sampler.get_baker ctxt level round in + match baker_opt with + | Some pk -> return (ctxt, pk) + | None -> Random.owner ctxt level (Slot_repr.to_int slot) + in return (ctxt, slot, pk) let load_sampler_for_cycle ctxt cycle = @@ -301,10 +307,16 @@ let select_distribution_for_cycle ctxt cycle = stakes in let total_stake = Stake_repr.staking_weight total_stake in - let state = Sampler.create stakes_pk in - let* ctxt = Delegate_sampler_state.init ctxt cycle state in - (* pre-allocate the sampler *) - let*? ctxt = Raw_context.init_sampler_for_cycle ctxt cycle seed state in + let* ctxt = + if Constants_storage.swrr_new_baker_lottery_enable ctxt then + Swrr_sampler.select_bakers_at_cycle_end ctxt ~target_cycle:cycle + else + let state = Sampler.create stakes_pk in + let* ctxt = Delegate_sampler_state.init ctxt cycle state in + (* pre-allocate the sampler *) + let*? ctxt = Raw_context.init_sampler_for_cycle ctxt cycle seed state in + return ctxt + in (* pre-allocate the raw stake distribution info *) let*? ctxt = Raw_context.init_stake_info_for_cycle ctxt cycle ~total_stake stakes_pk @@ -331,8 +343,10 @@ let clear_outdated_sampling_data ctxt ~new_cycle = match Cycle_storage.cycle_to_clear_of_sampling_data ~new_cycle with | None -> return ctxt | Some outdated_cycle -> - let* ctxt = Delegate_sampler_state.remove_existing ctxt outdated_cycle in - Seed_storage.remove_for_cycle ctxt outdated_cycle + let* ctxt = Delegate_sampler_state.remove ctxt outdated_cycle in + let* ctxt = Seed_storage.remove_for_cycle ctxt outdated_cycle in + let*! ctxt = Swrr_sampler.remove_outdated_cycle ctxt outdated_cycle in + return ctxt let attesting_power ~all_bakers_attest_enabled ctxt level = let open Lwt_result_syntax in diff --git a/src/proto_alpha/lib_protocol/swrr_sampler.ml b/src/proto_alpha/lib_protocol/swrr_sampler.ml index adddb2ebd1998e3835aaf0c1100d2a7254fd5e1e..20cad7b9330206b3701e089d6dfae0ab1d1bca61 100644 --- a/src/proto_alpha/lib_protocol/swrr_sampler.ml +++ b/src/proto_alpha/lib_protocol/swrr_sampler.ml @@ -78,10 +78,10 @@ let select_bakers_at_cycle_end ctxt ~target_cycle = let*! ctxt = Storage.Stake.Selected_bakers.add ctxt target_cycle selected_bakers in - let* ctxt = - List.fold_left_es + let*! ctxt = + List.fold_left_s (fun ctxt {pkh; credit; _} -> - Storage.Contract.SWRR_credit.update + Storage.Contract.SWRR_credit.add ctxt (Contract_repr.Implicit pkh) credit) @@ -96,14 +96,33 @@ let get_baker ctxt level round = let pos = level.Level_repr.cycle_position in let*? round_int = Round_repr.to_int round in - let* selected_bakers = Storage.Stake.Selected_bakers.get ctxt cycle in - let len = List.length selected_bakers in - let idx = (Int32.to_int pos + (3 * round_int)) mod len in + let* selected_bakers = Storage.Stake.Selected_bakers.find ctxt cycle in + match selected_bakers with + | None -> return (ctxt, None) + | Some selected_bakers -> ( + let len = List.length selected_bakers in + let idx = (Int32.to_int pos + (3 * round_int)) mod len in + + match List.nth_opt selected_bakers idx with + | None -> + assert false + (* should not happen if select_bakers_at_cycle_end is correct *) + | Some pkh -> + let* pk = Delegate_consensus_key.active_pubkey ctxt pkh in + return (ctxt, Some pk)) + +let reset_credit_for_deactivated_delegates ctxt deactivated_delegates = + let open Lwt_result_syntax in + let*! ctxt = + List.fold_left_s + (fun ctxt pkh -> + Storage.Contract.SWRR_credit.add + ctxt + (Contract_repr.Implicit pkh) + Z.zero) + ctxt + deactivated_delegates + in + return ctxt - match List.nth_opt selected_bakers idx with - | None -> - assert - false (* should not happen if select_bakers_at_cycle_end is correct *) - | Some pkh -> - let* pk = Delegate_consensus_key.active_pubkey ctxt pkh in - return (ctxt, pk) +let remove_outdated_cycle = Storage.Stake.Selected_bakers.remove diff --git a/src/proto_alpha/lib_protocol/swrr_sampler.mli b/src/proto_alpha/lib_protocol/swrr_sampler.mli index 93284f82a74f21721ce42a98d4325b1896078352..1ef3bb332b5f57658098830764996eba0e51e07d 100644 --- a/src/proto_alpha/lib_protocol/swrr_sampler.mli +++ b/src/proto_alpha/lib_protocol/swrr_sampler.mli @@ -5,7 +5,7 @@ (* *) (*****************************************************************************) -(** [select_bakers_at_cycle_end ctxt ~target_cycle] called to select the bakers at the end of a cycle +(** [select_bakers_at_cycle_end ctxt ~target_cycle] called to select the bakers at the end of a cycle and it will precompute the list of round 0 bakers for the [target_cycle]. *) val select_bakers_at_cycle_end : @@ -15,4 +15,11 @@ val get_baker : Raw_context.t -> Level_repr.t -> Round_repr.round -> - (Raw_context.t * Delegate_consensus_key.pk) tzresult Lwt.t + (Raw_context.t * Delegate_consensus_key.pk option) tzresult Lwt.t + +val reset_credit_for_deactivated_delegates : + Raw_context.t -> + Signature.Public_key_hash.t list -> + Raw_context.t tzresult Lwt.t + +val remove_outdated_cycle : Raw_context.t -> Cycle_repr.t -> Raw_context.t Lwt.t diff --git a/tezt/lib_tezos/RPC.ml b/tezt/lib_tezos/RPC.ml index db9e72eabbcc7622079aaad7ea7e31471cbdf558..efce243dbfd8bcad34a997ba9f1f5d281a263f97 100644 --- a/tezt/lib_tezos/RPC.ml +++ b/tezt/lib_tezos/RPC.ml @@ -2085,3 +2085,16 @@ let get_chain_block_context_destination_index ?(chain = "main") "index"; ] @@ JSON.as_int_opt + +let get_chain_block_helpers_swrr_credits ?(chain = "main") ?(block = "head") () + = + make GET ["chains"; chain; "blocks"; block; "helpers"; "swrr_credits"] Fun.id + +let get_chain_block_helpers_swrr_selected_bakers ?(chain = "main") + ?(block = "head") ?cycle () = + let query_string = Option.map (fun c -> [("cycle", Int.to_string c)]) cycle in + make + ?query_string + GET + ["chains"; chain; "blocks"; block; "helpers"; "swrr_selected_bakers"] + Fun.id diff --git a/tezt/lib_tezos/RPC.mli b/tezt/lib_tezos/RPC.mli index cb05ea6426031f3015a337cdd74b353a02ace9ca..71314fceae264e2bcd9bbd6051ad37c3acb4c687 100644 --- a/tezt/lib_tezos/RPC.mli +++ b/tezt/lib_tezos/RPC.mli @@ -1514,3 +1514,18 @@ val get_abaab_activation_level : [block] defaults to ["head"]. *) val get_chain_block_context_destination_index : ?chain:string -> ?block:string -> string -> int option t + +(** RPC: [GET /chains//blocks//helpers/swrr_credits] + + [chain] defaults to ["main"]. + [block] defaults to ["head"]. *) +val get_chain_block_helpers_swrr_credits : + ?chain:string -> ?block:string -> unit -> JSON.t t + +(** RPC: [GET /chains//blocks//helpers/swrr_selected_bakers?cycle=] + + [chain] defaults to ["main"]. + [block] defaults to ["head"]. + [cycle] doesn't have to be specified (defaults to current cycle). *) +val get_chain_block_helpers_swrr_selected_bakers : + ?chain:string -> ?block:string -> ?cycle:int -> unit -> JSON.t t diff --git a/tezt/tests/RPC_test.ml b/tezt/tests/RPC_test.ml index 55005fbf12cd3501ed0b9ce1ec128cbe16bee18c..f932a2067d5cb22b1271e345526f04581d1a95f8 100644 --- a/tezt/tests/RPC_test.ml +++ b/tezt/tests/RPC_test.ml @@ -759,6 +759,19 @@ let test_misc_protocol _test_mode_tag protocol ?endpoint client = unit else unit in + let* () = + if Protocol.(number protocol >= 025) then + let* _ = + Client.RPC.call ?endpoint ~hooks client + @@ RPC.get_chain_block_helpers_swrr_credits () + in + let* _ = + Client.RPC.call ?endpoint ~hooks client + @@ RPC.get_chain_block_helpers_swrr_selected_bakers () + in + unit + else unit + in unit let mempool_hooks = @@ -1680,6 +1693,11 @@ let register protocols = `Int 0 ); ] in + let swrr_flag protocol = + if Protocol.(number protocol >= 025) then + [(["swrr_new_baker_lottery_enable"], `Bool true)] + else [] + in check_rpc_regression "contracts" ~test_function:test_contracts @@ -1707,7 +1725,8 @@ let register protocols = check_rpc_regression "misc_protocol" ~test_function:test_misc_protocol - ~parameter_overrides:consensus_threshold ; + ~parameter_overrides:(fun protocol -> + consensus_threshold protocol @ swrr_flag protocol) ; check_rpc_regression "misc_protocol_abaab" ~test_function:test_misc_protocol @@ -1718,7 +1737,8 @@ let register protocols = ( ["all_bakers_attest_activation_threshold"], `O [("numerator", `Float 0.); ("denominator", `Float 1.)] ); ] - @ consensus_threshold protocol) ; + @ consensus_threshold protocol + @ swrr_flag protocol) ; (match test_mode_tag with | `Light -> () | _ -> diff --git a/tezt/tests/expected/RPC_test.ml/Alpha- (mode client) RPC regression tests- misc_protocol.out b/tezt/tests/expected/RPC_test.ml/Alpha- (mode client) RPC regression tests- misc_protocol.out index af9ce682b130264d6f83932eaf35d6c5d7c32a95..312456bec9edf20b5df9656f1d4ec2afc5cfbfd4 100644 --- a/tezt/tests/expected/RPC_test.ml/Alpha- (mode client) RPC regression tests- misc_protocol.out +++ b/tezt/tests/expected/RPC_test.ml/Alpha- (mode client) RPC regression tests- misc_protocol.out @@ -84,7 +84,7 @@ "allow_tz4_delegate_enable": true, "all_bakers_attest_activation_threshold": { "numerator": 1, "denominator": 2 }, "native_contracts_enable": true, - "swrr_new_baker_lottery_enable": false, "issuance_modification_delay": 2, + "swrr_new_baker_lottery_enable": true, "issuance_modification_delay": 2, "consensus_key_activation_delay": 2, "unstake_finalization_delay": 3 } ./octez-client rpc get /chains/main/blocks/head/helpers/baking_rights @@ -101,12 +101,12 @@ "round": 3, "estimated_time": "[TIMESTAMP]", "consensus_key": "[PUBLIC_KEY_HASH]" }, { "level": 2, "delegate": "[PUBLIC_KEY_HASH]", - "round": 10, "estimated_time": "[TIMESTAMP]", + "round": 6, "estimated_time": "[TIMESTAMP]", "consensus_key": "[PUBLIC_KEY_HASH]" } ] ./octez-client rpc get '/chains/main/blocks/head/helpers/baking_rights?delegate=[PUBLIC_KEY_HASH]' [ { "level": 2, "delegate": "[PUBLIC_KEY_HASH]", - "round": 2, "estimated_time": "[TIMESTAMP]", + "round": 1, "estimated_time": "[TIMESTAMP]", "consensus_key": "[PUBLIC_KEY_HASH]" } ] ./octez-client rpc get '/chains/main/blocks/head/helpers/current_level?offset=0' @@ -120,26 +120,26 @@ [ { "level": 1, "delegates": [ { "delegate": "[PUBLIC_KEY_HASH]", - "first_slot": 11, "attesting_power": 50, + "first_slot": 6, "attesting_power": 32, "consensus_key": "[PUBLIC_KEY_HASH]" }, { "delegate": "[PUBLIC_KEY_HASH]", - "first_slot": 4, "attesting_power": 47, + "first_slot": 4, "attesting_power": 64, "consensus_key": "[PUBLIC_KEY_HASH]" }, { "delegate": "[PUBLIC_KEY_HASH]", - "first_slot": 2, "attesting_power": 46, + "first_slot": 2, "attesting_power": 64, "consensus_key": "[PUBLIC_KEY_HASH]" }, { "delegate": "[PUBLIC_KEY_HASH]", - "first_slot": 1, "attesting_power": 55, + "first_slot": 1, "attesting_power": 32, "consensus_key": "[PUBLIC_KEY_HASH]" }, { "delegate": "[PUBLIC_KEY_HASH]", - "first_slot": 0, "attesting_power": 58, + "first_slot": 0, "attesting_power": 64, "consensus_key": "[PUBLIC_KEY_HASH]" } ] } ] ./octez-client rpc get '/chains/main/blocks/head/helpers/attestation_rights?delegate=[PUBLIC_KEY_HASH]' [ { "level": 1, "delegates": [ { "delegate": "[PUBLIC_KEY_HASH]", - "first_slot": 11, "attesting_power": 50, + "first_slot": 6, "attesting_power": 32, "consensus_key": "[PUBLIC_KEY_HASH]" } ] } ] ./octez-client rpc get /chains/main/blocks/head/helpers/levels_in_current_cycle @@ -153,3 +153,25 @@ ./octez-client rpc get /chains/main/blocks/head/helpers/all_bakers_attest_activation_level null + +./octez-client rpc get /chains/main/blocks/head/helpers/swrr_credits +[ { "delegate": "[PUBLIC_KEY_HASH]", + "credit": "-1466666666666" }, + { "delegate": "[PUBLIC_KEY_HASH]", + "credit": "-1466666666666" }, + { "delegate": "[PUBLIC_KEY_HASH]", + "credit": "5866666666664" }, + { "delegate": "[PUBLIC_KEY_HASH]", + "credit": "-1466666666666" }, + { "delegate": "[PUBLIC_KEY_HASH]", + "credit": "-1466666666666" } ] + +./octez-client rpc get /chains/main/blocks/head/helpers/swrr_selected_bakers +[ "[PUBLIC_KEY_HASH]", + "[PUBLIC_KEY_HASH]", + "[PUBLIC_KEY_HASH]", + "[PUBLIC_KEY_HASH]", + "[PUBLIC_KEY_HASH]", + "[PUBLIC_KEY_HASH]", + "[PUBLIC_KEY_HASH]", + "[PUBLIC_KEY_HASH]" ] diff --git a/tezt/tests/expected/RPC_test.ml/Alpha- (mode client) RPC regression tests- misc_protocol_abaab.out b/tezt/tests/expected/RPC_test.ml/Alpha- (mode client) RPC regression tests- misc_protocol_abaab.out index b776c8532d5b50779db428b7f7b63a91b6eca3bd..2093bb857391e1a0e275a60d4c13c928839f1ce8 100644 --- a/tezt/tests/expected/RPC_test.ml/Alpha- (mode client) RPC regression tests- misc_protocol_abaab.out +++ b/tezt/tests/expected/RPC_test.ml/Alpha- (mode client) RPC regression tests- misc_protocol_abaab.out @@ -84,7 +84,7 @@ "allow_tz4_delegate_enable": true, "all_bakers_attest_activation_threshold": { "numerator": 0, "denominator": 1 }, "native_contracts_enable": true, - "swrr_new_baker_lottery_enable": false, "issuance_modification_delay": 2, + "swrr_new_baker_lottery_enable": true, "issuance_modification_delay": 2, "consensus_key_activation_delay": 2, "unstake_finalization_delay": 3 } ./octez-client rpc get /chains/main/blocks/head/helpers/baking_rights @@ -101,12 +101,12 @@ "round": 3, "estimated_time": "[TIMESTAMP]", "consensus_key": "[PUBLIC_KEY_HASH]" }, { "level": 2, "delegate": "[PUBLIC_KEY_HASH]", - "round": 10, "estimated_time": "[TIMESTAMP]", + "round": 6, "estimated_time": "[TIMESTAMP]", "consensus_key": "[PUBLIC_KEY_HASH]" } ] ./octez-client rpc get '/chains/main/blocks/head/helpers/baking_rights?delegate=[PUBLIC_KEY_HASH]' [ { "level": 2, "delegate": "[PUBLIC_KEY_HASH]", - "round": 2, "estimated_time": "[TIMESTAMP]", + "round": 1, "estimated_time": "[TIMESTAMP]", "consensus_key": "[PUBLIC_KEY_HASH]" } ] ./octez-client rpc get '/chains/main/blocks/head/helpers/current_level?offset=0' @@ -120,26 +120,26 @@ [ { "level": 1, "delegates": [ { "delegate": "[PUBLIC_KEY_HASH]", - "first_slot": 4, "attesting_power": 55, + "first_slot": 4, "attesting_power": 64, "consensus_key": "[PUBLIC_KEY_HASH]" }, { "delegate": "[PUBLIC_KEY_HASH]", - "first_slot": 3, "attesting_power": 47, + "first_slot": 3, "attesting_power": 32, "consensus_key": "[PUBLIC_KEY_HASH]" }, { "delegate": "[PUBLIC_KEY_HASH]", - "first_slot": 2, "attesting_power": 46, + "first_slot": 2, "attesting_power": 64, "consensus_key": "[PUBLIC_KEY_HASH]" }, { "delegate": "[PUBLIC_KEY_HASH]", - "first_slot": 1, "attesting_power": 50, + "first_slot": 1, "attesting_power": 32, "consensus_key": "[PUBLIC_KEY_HASH]" }, { "delegate": "[PUBLIC_KEY_HASH]", - "first_slot": 0, "attesting_power": 58, + "first_slot": 0, "attesting_power": 64, "consensus_key": "[PUBLIC_KEY_HASH]" } ] } ] ./octez-client rpc get '/chains/main/blocks/head/helpers/attestation_rights?delegate=[PUBLIC_KEY_HASH]' [ { "level": 1, "delegates": [ { "delegate": "[PUBLIC_KEY_HASH]", - "first_slot": 1, "attesting_power": 50, + "first_slot": 1, "attesting_power": 32, "consensus_key": "[PUBLIC_KEY_HASH]" } ] } ] ./octez-client rpc get /chains/main/blocks/head/helpers/levels_in_current_cycle @@ -154,3 +154,25 @@ ./octez-client rpc get /chains/main/blocks/head/helpers/all_bakers_attest_activation_level { "level": 1, "level_position": 0, "cycle": 0, "cycle_position": 0, "expected_commitment": false } + +./octez-client rpc get /chains/main/blocks/head/helpers/swrr_credits +[ { "delegate": "[PUBLIC_KEY_HASH]", + "credit": "-1466666666666" }, + { "delegate": "[PUBLIC_KEY_HASH]", + "credit": "-1466666666666" }, + { "delegate": "[PUBLIC_KEY_HASH]", + "credit": "5866666666664" }, + { "delegate": "[PUBLIC_KEY_HASH]", + "credit": "-1466666666666" }, + { "delegate": "[PUBLIC_KEY_HASH]", + "credit": "-1466666666666" } ] + +./octez-client rpc get /chains/main/blocks/head/helpers/swrr_selected_bakers +[ "[PUBLIC_KEY_HASH]", + "[PUBLIC_KEY_HASH]", + "[PUBLIC_KEY_HASH]", + "[PUBLIC_KEY_HASH]", + "[PUBLIC_KEY_HASH]", + "[PUBLIC_KEY_HASH]", + "[PUBLIC_KEY_HASH]", + "[PUBLIC_KEY_HASH]" ]