From b1afab937a3fca0d15b671b33a049b8f7214ab09 Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Fri, 13 Oct 2023 13:55:35 +0200 Subject: [PATCH 1/4] Proto/Delegate_sampler: remove dead compute_snapshot_index --- src/proto_alpha/lib_protocol/alpha_context.ml | 2 -- src/proto_alpha/lib_protocol/alpha_context.mli | 3 --- src/proto_alpha/lib_protocol/delegate_sampler.ml | 11 ----------- src/proto_alpha/lib_protocol/delegate_sampler.mli | 7 ------- 4 files changed, 23 deletions(-) diff --git a/src/proto_alpha/lib_protocol/alpha_context.ml b/src/proto_alpha/lib_protocol/alpha_context.ml index 96ce66298b39..392d5284e3a9 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.ml +++ b/src/proto_alpha/lib_protocol/alpha_context.ml @@ -572,8 +572,6 @@ module Delegate = struct end module Stake_distribution = struct - let compute_snapshot_index = Delegate_sampler.compute_snapshot_index - let baking_rights_owner = Delegate_sampler.baking_rights_owner let slot_owner = Delegate_sampler.slot_owner diff --git a/src/proto_alpha/lib_protocol/alpha_context.mli b/src/proto_alpha/lib_protocol/alpha_context.mli index 2edd6be8fdd7..52792f335668 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/alpha_context.mli @@ -4920,9 +4920,6 @@ end (** This module re-exports definitions from {!Stake_storage}, {!Delegate_storage} and {!Delegate}. *) module Stake_distribution : sig - val compute_snapshot_index : - context -> Cycle.t -> max_snapshot_index:int -> int tzresult Lwt.t - val baking_rights_owner : context -> Level.t -> diff --git a/src/proto_alpha/lib_protocol/delegate_sampler.ml b/src/proto_alpha/lib_protocol/delegate_sampler.ml index 5608b1e5ce88..82a31b405835 100644 --- a/src/proto_alpha/lib_protocol/delegate_sampler.ml +++ b/src/proto_alpha/lib_protocol/delegate_sampler.ml @@ -200,17 +200,6 @@ let get_stakes ctxt = else return acc) ~init:([], Stake_repr.zero) -let compute_snapshot_index_for_seed ~max_snapshot_index seed = - let rd = Seed_repr.initialize_new seed [Bytes.of_string "stake_snapshot"] in - let seq = Seed_repr.sequence rd 0l in - Seed_repr.take_int32 seq (Int32.of_int max_snapshot_index) - |> fst |> Int32.to_int |> return - -let compute_snapshot_index ctxt cycle ~max_snapshot_index = - let open Lwt_result_syntax in - let* seed = Seed_storage.for_cycle ctxt cycle in - compute_snapshot_index_for_seed ~max_snapshot_index seed - let select_distribution_for_cycle ctxt cycle = let open Lwt_result_syntax in let* seed = Seed_storage.raw_for_cycle ctxt cycle in diff --git a/src/proto_alpha/lib_protocol/delegate_sampler.mli b/src/proto_alpha/lib_protocol/delegate_sampler.mli index d3477865821d..8e1f9cd6426d 100644 --- a/src/proto_alpha/lib_protocol/delegate_sampler.mli +++ b/src/proto_alpha/lib_protocol/delegate_sampler.mli @@ -61,13 +61,6 @@ val baking_rights_owner : val load_sampler_for_cycle : Raw_context.t -> Cycle_repr.t -> Raw_context.t tzresult Lwt.t -(** [compute_snapshot_index ctxt cycle max_snapshot_index] Returns the index of - the selected snapshot for the [cycle] passed as argument, and for the max - index of snapshots taken so far, [max_snapshot_index] (see - [Stake_storage.max_snapshot_index]. *) -val compute_snapshot_index : - Raw_context.t -> Cycle_repr.t -> max_snapshot_index:int -> int tzresult Lwt.t - val select_new_distribution_at_cycle_end : Raw_context.t -> new_cycle:Cycle_repr.t -> Raw_context.t tzresult Lwt.t -- GitLab From c679ddd3b8ac647cb1342ee23136e169a4afdb68 Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Fri, 13 Oct 2023 13:56:27 +0200 Subject: [PATCH 2/4] Proto/Stake_storage: remove dead max_snapshot_index --- src/proto_alpha/lib_protocol/stake_storage.ml | 2 -- src/proto_alpha/lib_protocol/stake_storage.mli | 4 ---- 2 files changed, 6 deletions(-) diff --git a/src/proto_alpha/lib_protocol/stake_storage.ml b/src/proto_alpha/lib_protocol/stake_storage.ml index 0d1e84ab716b..b2995c36b7b7 100644 --- a/src/proto_alpha/lib_protocol/stake_storage.ml +++ b/src/proto_alpha/lib_protocol/stake_storage.ml @@ -198,8 +198,6 @@ let set_active ctxt delegate = return ctxt else return ctxt -let max_snapshot_index = Storage.Stake.Last_snapshot.get - let set_selected_distribution_for_cycle ctxt cycle stakes total_stake = let open Lwt_result_syntax in let stakes = List.sort (fun (_, x) (_, y) -> Stake_repr.compare y x) stakes in diff --git a/src/proto_alpha/lib_protocol/stake_storage.mli b/src/proto_alpha/lib_protocol/stake_storage.mli index 3b51335efb73..5dbf028bbc7b 100644 --- a/src/proto_alpha/lib_protocol/stake_storage.mli +++ b/src/proto_alpha/lib_protocol/stake_storage.mli @@ -84,10 +84,6 @@ val fold_on_active_delegates_with_minimal_stake_es : init:'a -> 'a tzresult Lwt.t -(** [max_snapshot_index ctxt] returns the index of the last snapshot taken of - staking balances and active delegates. *) -val max_snapshot_index : Raw_context.t -> int tzresult Lwt.t - (** [set_selected_distribution_for_cycle ctxt cycle distrib total_stake] saves the selected distribution [distrib] of the [total_stake] for the given [cycle]. *) -- GitLab From 6fd34e65dde341e79823105d1c8aa5c669227b0a Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Thu, 12 Oct 2023 11:16:51 +0200 Subject: [PATCH 3/4] Proto/Level_storage: remove dead may_snapshot_stake_distribution --- src/proto_alpha/lib_protocol/alpha_context.mli | 2 -- src/proto_alpha/lib_protocol/level_storage.ml | 9 --------- src/proto_alpha/lib_protocol/level_storage.mli | 4 ---- 3 files changed, 15 deletions(-) diff --git a/src/proto_alpha/lib_protocol/alpha_context.mli b/src/proto_alpha/lib_protocol/alpha_context.mli index 52792f335668..a8bf747061d0 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/alpha_context.mli @@ -1227,8 +1227,6 @@ module Level : sig val dawn_of_a_new_cycle : context -> Cycle.t option - val may_snapshot_stake_distribution : context -> bool - val may_compute_randao : context -> bool end diff --git a/src/proto_alpha/lib_protocol/level_storage.ml b/src/proto_alpha/lib_protocol/level_storage.ml index 20be68068e99..fcf9cd89eb09 100644 --- a/src/proto_alpha/lib_protocol/level_storage.ml +++ b/src/proto_alpha/lib_protocol/level_storage.ml @@ -123,15 +123,6 @@ let dawn_of_a_new_cycle ctxt = let level = current ctxt in if last_of_a_cycle ctxt level then Some level.cycle else None -let may_snapshot_stake_distribution ctxt = - let level = current ctxt in - let blocks_per_stake_snapshot = - Constants_storage.blocks_per_stake_snapshot ctxt - in - Compare.Int32.equal - (Int32.rem level.cycle_position blocks_per_stake_snapshot) - (Int32.pred blocks_per_stake_snapshot) - let may_compute_randao ctxt = let level = current ctxt in let nonce_reveal_cutoff = Constants_storage.nonce_revelation_threshold ctxt in diff --git a/src/proto_alpha/lib_protocol/level_storage.mli b/src/proto_alpha/lib_protocol/level_storage.mli index bd6b45606073..64e8bc45e12f 100644 --- a/src/proto_alpha/lib_protocol/level_storage.mli +++ b/src/proto_alpha/lib_protocol/level_storage.mli @@ -71,10 +71,6 @@ val last_finalized_block_level : Raw_context.t -> Raw_level_repr.t cycle. *) val dawn_of_a_new_cycle : Raw_context.t -> Cycle_repr.t option -(** Returns [true] if the stake distribution should be snapshot at the current - level. *) -val may_snapshot_stake_distribution : Raw_context.t -> bool - (** Returns [true] if RANDAO should be computed at the current level, that is if the current level, relative to the cycle's start, equals the nonce revelation period cut-off. *) -- GitLab From 3929f40445f83f356b735afcce193cdff75549e4 Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Fri, 13 Oct 2023 14:11:16 +0200 Subject: [PATCH 4/4] Proto/Seed_repr: remove dead values --- src/proto_alpha/lib_protocol/seed_repr.ml | 68 ---------------------- src/proto_alpha/lib_protocol/seed_repr.mli | 29 --------- 2 files changed, 97 deletions(-) diff --git a/src/proto_alpha/lib_protocol/seed_repr.ml b/src/proto_alpha/lib_protocol/seed_repr.ml index 531b7129004f..ab00efece29d 100644 --- a/src/proto_alpha/lib_protocol/seed_repr.ml +++ b/src/proto_alpha/lib_protocol/seed_repr.ml @@ -27,10 +27,6 @@ type seed = B of State_hash.t -type t = T of State_hash.t - -type sequence = S of State_hash.t - type nonce = bytes type vdf_setup = Vdf.discriminant * Vdf.challenge @@ -117,70 +113,6 @@ let seed_encoding = let update_seed (B state) nonce = B (State_hash.hash_bytes [State_hash.to_bytes state; nonce]) -let initialize_new (B state) append = - T (State_hash.hash_bytes (State_hash.to_bytes state :: zero_bytes :: append)) - -let xor_higher_bits i b = - let higher = TzEndian.get_int32 b 0 in - let r = Int32.logxor higher i in - let res = Bytes.copy b in - TzEndian.set_int32 res 0 r ; - res - -let sequence (T state) n = - State_hash.to_bytes state |> xor_higher_bits n |> fun b -> - S (State_hash.hash_bytes [b]) - -let take (S state) = - let b = State_hash.to_bytes state in - let h = State_hash.hash_bytes [b] in - (State_hash.to_bytes h, S h) - -let take_int32 s bound = - if Compare.Int32.(bound <= 0l) then invalid_arg "Seed_repr.take_int32" - (* FIXME *) - else - let drop_if_over = - Int32.sub Int32.max_int (Int32.rem Int32.max_int bound) - in - let rec loop s = - let bytes, s = take s in - let r = TzEndian.get_int32 bytes 0 in - (* The absolute value of min_int is min_int. Also, every - positive integer is represented twice (positive and negative), - but zero is only represented once. We fix both problems at - once. *) - let r = if Compare.Int32.(r = Int32.min_int) then 0l else Int32.abs r in - if Compare.Int32.(r >= drop_if_over) then loop s - else - let v = Int32.rem r bound in - (v, s) - in - loop s - -let take_int64 s bound = - if Compare.Int64.(bound <= 0L) then invalid_arg "Seed_repr.take_int64" - (* FIXME *) - else - let drop_if_over = - Int64.sub Int64.max_int (Int64.rem Int64.max_int bound) - in - - let rec loop s = - let bytes, s = take s in - let r = TzEndian.get_int64 bytes 0 in - (* The absolute value of min_int is min_int. Also, every - positive integer is represented twice (positive and negative), - but zero is only represented once. We fix both problems at - once. *) - let r = if Compare.Int64.(r = Int64.min_int) then 0L else Int64.abs r in - if Compare.Int64.(r >= drop_if_over) then loop s - else - let v = Int64.rem r bound in - (v, s) - in - loop s - type error += Unexpected_nonce_length (* `Permanent *) let () = diff --git a/src/proto_alpha/lib_protocol/seed_repr.mli b/src/proto_alpha/lib_protocol/seed_repr.mli index 86b4f15c7f96..e15c31b99a83 100644 --- a/src/proto_alpha/lib_protocol/seed_repr.mli +++ b/src/proto_alpha/lib_protocol/seed_repr.mli @@ -34,15 +34,9 @@ (** {2 Random Generation} *) -(** The state of the random number generator *) -type t - (** A random seed, to derive random sequences from *) type seed -(** A random sequence, to derive random values from *) -type sequence - (** A VDF discriminant and challenge *) type vdf_setup = Vdf.discriminant * Vdf.challenge @@ -62,29 +56,6 @@ val verify : vdf_setup -> Int64.t -> vdf_solution -> bool option val vdf_to_seed : seed -> vdf_solution -> seed -(** [initialize_new state ident] returns a new generator *) -val initialize_new : seed -> bytes list -> t - -(** [sequence state n] prepares the n-th sequence of a state *) -val sequence : t -> int32 -> sequence - -(** Generates the next random value in the sequence *) -val take : sequence -> bytes * sequence - -(** [take_int32 s bound] generates the next random value as a bounded [int32] - - @param bound must be a positive integer - @raise Invalid_argument "Seed_repr.take_int32" if [bound] <= 0 - *) -val take_int32 : sequence -> int32 -> int32 * sequence - -(** [take_int64 s bound] generates the next random value as a bounded [int64] - - @param bound must be a positive integer - @raise Invalid_argument "Seed_repr.take_int64" if [bound] <= 0 - *) -val take_int64 : sequence -> int64 -> int64 * sequence - (** {2 Entropy} *) (** A nonce for adding entropy to the generator *) -- GitLab