package tezos-protocol-017-PtNairob
Tezos protocol 017-PtNairob package
Install
Dune Dependency
Authors
Maintainers
Sources
tezos-octez-v20.1.tag.bz2
sha256=ddfb5076eeb0b32ac21c1eed44e8fc86a6743ef18ab23fff02d36e365bb73d61
sha512=d22a827df5146e0aa274df48bc2150b098177ff7e5eab52c6109e867eb0a1f0ec63e6bfbb0e3645a6c2112de3877c91a17df32ccbff301891ce4ba630c997a65
doc/src/tezos_raw_protocol_017_PtNairob/fitness_repr.ml.html
Source file fitness_repr.ml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
(*****************************************************************************) (* *) (* Open Source License *) (* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.com> *) (* *) (* Permission is hereby granted, free of charge, to any person obtaining a *) (* copy of this software and associated documentation files (the "Software"),*) (* to deal in the Software without restriction, including without limitation *) (* the rights to use, copy, modify, merge, publish, distribute, sublicense, *) (* and/or sell copies of the Software, and to permit persons to whom the *) (* Software is furnished to do so, subject to the following conditions: *) (* *) (* The above copyright notice and this permission notice shall be included *) (* in all copies or substantial portions of the Software. *) (* *) (* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*) (* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *) (* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *) (* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*) (* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *) (* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *) (* DEALINGS IN THE SOFTWARE. *) (* *) (*****************************************************************************) type t = { level : Raw_level_repr.t; locked_round : Round_repr.t option; predecessor_round : Round_repr.t; (* by convention, predecessor_round is 0 in case of protocol migration *) round : Round_repr.t; } let encoding = let open Data_encoding in def "fitness" (conv_with_guard (fun {level; locked_round; predecessor_round; round} -> (level, locked_round, predecessor_round, round)) (fun (level, locked_round, predecessor_round, round) -> match locked_round with | None -> ok {level; locked_round; predecessor_round; round} | Some locked_round_val -> if Round_repr.(round <= locked_round_val) then Error "Locked round must be smaller than round." else ok {level; locked_round; predecessor_round; round}) (obj4 (req "level" Raw_level_repr.encoding) (req "locked_round" (option Round_repr.encoding)) (req "predecessor_round" Round_repr.encoding) (req "round" Round_repr.encoding))) let pp ppf f = let minus_sign = if Round_repr.(f.predecessor_round = Round_repr.zero) then "" else "-" in let locked_round ppf locked_round = match locked_round with | None -> Format.pp_print_string ppf "unlocked" | Some round -> Format.fprintf ppf "locked: %a" Round_repr.pp round in Format.fprintf ppf "(%a, %a, %s%a, %a)" Raw_level_repr.pp f.level locked_round f.locked_round minus_sign Round_repr.pp f.predecessor_round Round_repr.pp f.round type error += | (* `Permanent *) Invalid_fitness | (* `Permanent *) Wrong_fitness | (* `Permanent *) Outdated_fitness | (* `Permanent *) Locked_round_not_less_than_round of { round : Round_repr.t; locked_round : Round_repr.t; } let () = register_error_kind `Permanent ~id:"invalid_fitness" ~title:"Invalid fitness" ~description: "Fitness representation should be exactly 4 times 4 bytes long." ~pp:(fun ppf () -> Format.fprintf ppf "Invalid fitness") Data_encoding.empty (function Invalid_fitness -> Some () | _ -> None) (fun () -> Invalid_fitness) ; register_error_kind `Permanent ~id:"wrong_fitness" ~title:"Wrong fitness" ~description:"Wrong fitness." ~pp:(fun ppf () -> Format.fprintf ppf "Wrong fitness.") Data_encoding.empty (function Wrong_fitness -> Some () | _ -> None) (fun () -> Wrong_fitness) ; register_error_kind `Permanent ~id:"outdated_fitness" ~title:"Outdated fitness" ~description:"Outdated fitness: referring to a previous version" ~pp:(fun ppf () -> Format.fprintf ppf "Outdated fitness: referring to a previous version.") Data_encoding.empty (function Outdated_fitness -> Some () | _ -> None) (fun () -> Outdated_fitness) ; register_error_kind `Permanent ~id:"locked_round_not_less_than_round" ~title:"Locked round not smaller than round" ~description:"The round is smaller than or equal to the locked round." ~pp:(fun ppf (round, locked_round) -> Format.fprintf ppf "Incorrect fitness: round %a is less than or equal to locked round %a." Round_repr.pp round Round_repr.pp locked_round) Data_encoding.( obj2 (req "round" Round_repr.encoding) (req "locked_round" Round_repr.encoding)) (function | Locked_round_not_less_than_round {round; locked_round} -> Some (round, locked_round) | _ -> None) (fun (round, locked_round) -> Locked_round_not_less_than_round {round; locked_round}) let create_without_locked_round ~level ~predecessor_round ~round = {level; locked_round = None; predecessor_round; round} let create ~level ~locked_round ~predecessor_round ~round = match locked_round with | None -> ok {level; locked_round; predecessor_round; round} | Some locked_round_val -> error_when Round_repr.(round <= locked_round_val) (Locked_round_not_less_than_round {round; locked_round = locked_round_val}) >>? fun () -> ok {level; locked_round; predecessor_round; round} let int32_to_bytes i = let b = Bytes.make 4 '\000' in TzEndian.set_int32 b 0 i ; b let int32_of_bytes b = if Compare.Int.(Bytes.length b <> 4) then error Invalid_fitness else ok (TzEndian.get_int32 b 0) (* Locked round is an option. And we want None to be smaller than any other value. The way the shell handles the order makes the empty Bytes smaller than any other *) let locked_round_to_bytes = function | None -> Bytes.empty | Some locked_round -> int32_to_bytes (Round_repr.to_int32 locked_round) let locked_round_of_bytes b = match Bytes.length b with | 0 -> ok None | 4 -> Round_repr.of_int32 (TzEndian.get_int32 b 0) >>? fun r -> ok (Some r) | _ -> error Invalid_fitness let predecessor_round_of_bytes neg_predecessor_round = int32_of_bytes neg_predecessor_round >>? fun neg_predecessor_round -> Round_repr.of_int32 @@ Int32.pred (Int32.neg neg_predecessor_round) let round_of_bytes round = int32_of_bytes round >>? Round_repr.of_int32 let to_raw {level; locked_round; predecessor_round; round} = [ Bytes.of_string Constants_repr.fitness_version_number; int32_to_bytes (Raw_level_repr.to_int32 level); locked_round_to_bytes locked_round; int32_to_bytes (Int32.pred (Int32.neg (Round_repr.to_int32 predecessor_round))); int32_to_bytes (Round_repr.to_int32 round); ] let from_raw = function | [version; level; locked_round; neg_predecessor_round; round] when Compare.String.( Bytes.to_string version = Constants_repr.fitness_version_number) -> int32_of_bytes level >>? Raw_level_repr.of_int32 >>? fun level -> locked_round_of_bytes locked_round >>? fun locked_round -> predecessor_round_of_bytes neg_predecessor_round >>? fun predecessor_round -> round_of_bytes round >>? fun round -> create ~level ~locked_round ~predecessor_round ~round | [version; _] when Compare.String.( Bytes.to_string version < Constants_repr.fitness_version_number) -> error Outdated_fitness | [] (* genesis fitness *) -> error Outdated_fitness | _ -> error Invalid_fitness let round_from_raw = function | [version; _level; _locked_round; _neg_predecessor_round; round] when Compare.String.( Bytes.to_string version = Constants_repr.fitness_version_number) -> round_of_bytes round | [version; _] when Compare.String.( Bytes.to_string version < Constants_repr.fitness_version_number) -> ok Round_repr.zero | [] (* genesis fitness *) -> ok Round_repr.zero | _ -> error Invalid_fitness let predecessor_round_from_raw = function | [version; _level; _locked_round; neg_predecessor_round; _round] when Compare.String.( Bytes.to_string version = Constants_repr.fitness_version_number) -> predecessor_round_of_bytes neg_predecessor_round | [version; _] when Compare.String.( Bytes.to_string version < Constants_repr.fitness_version_number) -> ok Round_repr.zero | [] (* genesis fitness *) -> ok Round_repr.zero | _ -> error Invalid_fitness let locked_round_from_raw = function | [version; _level; locked_round; _neg_predecessor_round; _round] when Compare.String.( Bytes.to_string version = Constants_repr.fitness_version_number) -> locked_round_of_bytes locked_round | [version; _] when Compare.String.( Bytes.to_string version < Constants_repr.fitness_version_number) -> ok None | [] (* former genesis fitness *) -> ok None | _ -> error Invalid_fitness let check_except_locked_round fitness ~level ~predecessor_round = let { level = expected_level; locked_round = _; predecessor_round = expected_predecessor_round; round = _; } = fitness in let correct = Raw_level_repr.(level = expected_level) && Round_repr.(predecessor_round = expected_predecessor_round) in error_unless correct Wrong_fitness let level fitness = fitness.level let round fitness = fitness.round let locked_round fitness = fitness.locked_round let predecessor_round fitness = fitness.predecessor_round module Internal_for_tests = struct module ListInt32Compare = Compare.List (Compare.Int32) let compare f ff = let unopt l = match l with Some l -> Round_repr.to_int32 l | None -> -1l in let to_list {level; locked_round; predecessor_round; round} = Int32. [ Raw_level_repr.to_int32 level; unopt locked_round; neg (Round_repr.to_int32 predecessor_round); Round_repr.to_int32 round; ] in ListInt32Compare.compare (to_list f) (to_list ff) end
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>