package tezos-protocol-015-PtLimaPt
Tezos protocol 015-PtLimaPt package
Install
Dune Dependency
Authors
Maintainers
Sources
tezos-octez-v20.1.tag.bz2
sha256=ddfb5076eeb0b32ac21c1eed44e8fc86a6743ef18ab23fff02d36e365bb73d61
sha512=d22a827df5146e0aa274df48bc2150b098177ff7e5eab52c6109e867eb0a1f0ec63e6bfbb0e3645a6c2112de3877c91a17df32ccbff301891ce4ba630c997a65
doc/src/tezos_raw_protocol_015_PtLimaPt/sc_rollup_stake_storage.ml.html
Source file sc_rollup_stake_storage.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 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
(*****************************************************************************) (* *) (* Open Source License *) (* Copyright (c) 2022 Nomadic Labs <contact@nomadic-labs.com> *) (* Copyright (c) 2022 TriliTech <contact@trili.tech> *) (* *) (* 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. *) (* *) (*****************************************************************************) open Sc_rollup_errors module Store = Storage.Sc_rollup module Commitment_storage = Sc_rollup_commitment_storage module Commitment = Sc_rollup_commitment_repr module Commitment_hash = Commitment.Hash let find_staker_unsafe ctxt rollup staker = let open Lwt_tzresult_syntax in let* ctxt, res = Store.Stakers.find (ctxt, rollup) staker in match res with | None -> fail Sc_rollup_not_staked | Some branch -> return (branch, ctxt) let find_staker ctxt rollup staker = let open Lwt_tzresult_syntax in let* ctxt, res = Store.Last_cemented_commitment.mem ctxt rollup in if not res then fail (Sc_rollup_does_not_exist rollup) else find_staker_unsafe ctxt rollup staker let modify_staker_count ctxt rollup f = let open Lwt_tzresult_syntax in let* ctxt, maybe_count = Store.Staker_count.find ctxt rollup in let count = Option.value ~default:0l maybe_count in let* ctxt, size_diff, _was_bound = Store.Staker_count.add ctxt rollup (f count) in assert (Compare.Int.(size_diff = 0)) ; return ctxt let get_contract_and_stake ctxt staker = let staker_contract = Contract_repr.Implicit staker in let stake = Constants_storage.sc_rollup_stake_amount ctxt in (staker_contract, stake) (** Warning: must be called only if [rollup] exists and [staker] is not to be found in {!Store.Stakers.} *) let deposit_stake ctxt rollup staker = let open Lwt_tzresult_syntax in let* lcc, ctxt = Commitment_storage.last_cemented_commitment ctxt rollup in let staker_contract, stake = get_contract_and_stake ctxt staker in let* ctxt, staker_balance = Token.balance ctxt (`Contract staker_contract) in let* () = fail_when Tez_repr.(staker_balance < stake) (Sc_rollup_staker_funds_too_low { staker; sc_rollup = rollup; staker_balance; min_expected_balance = stake; }) in let bond_id = Bond_id_repr.Sc_rollup_bond_id rollup in let* ctxt, balance_updates = Token.transfer ctxt (`Contract staker_contract) (`Frozen_bonds (staker_contract, bond_id)) stake in let* ctxt, _size = Store.Stakers.init (ctxt, rollup) staker lcc in let* ctxt = modify_staker_count ctxt rollup Int32.succ in return (ctxt, balance_updates, lcc) let withdraw_stake ctxt rollup staker = let open Lwt_tzresult_syntax in let* lcc, ctxt = Commitment_storage.last_cemented_commitment ctxt rollup in let* ctxt, res = Store.Stakers.find (ctxt, rollup) staker in match res with | None -> fail Sc_rollup_not_staked | Some staked_on_commitment -> let* () = fail_unless Commitment_hash.(staked_on_commitment = lcc) Sc_rollup_not_staked_on_lcc in let staker_contract, stake = get_contract_and_stake ctxt staker in let bond_id = Bond_id_repr.Sc_rollup_bond_id rollup in let* ctxt, balance_updates = Token.transfer ctxt (`Frozen_bonds (staker_contract, bond_id)) (`Contract staker_contract) stake in let* ctxt, _size_freed = Store.Stakers.remove_existing (ctxt, rollup) staker in let+ ctxt = modify_staker_count ctxt rollup Int32.pred in (ctxt, balance_updates) let assert_commitment_not_too_far_ahead ctxt rollup lcc commitment = let open Lwt_tzresult_syntax in let* lcc, ctxt = Commitment_storage.get_commitment_unsafe ctxt rollup lcc in let min_level = Commitment.(lcc.inbox_level) in let max_level = Commitment.(commitment.inbox_level) in let* () = fail_when (let sc_rollup_max_lookahead = Constants_storage.sc_rollup_max_lookahead_in_blocks ctxt in Compare.Int32.( sc_rollup_max_lookahead < Raw_level_repr.diff max_level min_level)) Sc_rollup_too_far_ahead in return ctxt (** Enfore that a commitment's inbox level increases by an exact fixed amount over its predecessor. This property is used in several places - not obeying it causes severe breakage. *) let assert_commitment_period ctxt rollup commitment = let open Lwt_tzresult_syntax in let pred_hash = Commitment.(commitment.predecessor) in let* pred, ctxt = Commitment_storage.get_commitment_unsafe ctxt rollup pred_hash in let pred_level = Commitment.(pred.inbox_level) in (* We want to check the following inequalities on [commitment.inbox_level], [commitment.predecessor.inbox_level] and the constant [sc_rollup_commitment_period]. - Greater-than-or-equal (>=), to ensure inbox_levels are monotonically increasing along each branch of commitments. Together with [assert_commitment_not_too_far_ahead] this is sufficient to limit the depth of the commitment tree, which is also the number of commitments stored per staker. This constraint must be enforced at submission time. - Equality (=), so that L2 blocks are produced at a regular rate. This ensures that there is only ever one branch of correct commitments, simplifying refutation logic. This could also be enforced at refutation time rather than submission time, but doing it here works too. Because [a >= b && a = b] is equivalent to [a = b], we can just keep the latter as an optimization. *) let sc_rollup_commitment_period = Constants_storage.sc_rollup_commitment_period_in_blocks ctxt in let* () = fail_unless Raw_level_repr.( commitment.inbox_level = add pred_level sc_rollup_commitment_period) Sc_rollup_bad_inbox_level in return ctxt let assert_same_hash_as_predecessor ctxt rollup (commitment : Commitment.t) = let open Lwt_tzresult_syntax in let* pred, ctxt = Commitment_storage.get_commitment_unsafe ctxt rollup commitment.predecessor in if Sc_rollup_repr.State_hash.equal pred.compressed_state commitment.compressed_state then return ctxt else fail Sc_rollup_state_change_on_zero_tick_commitment (** Check invariants on [inbox_level], enforcing overallocation of storage and regularity of block production. The constants used by [assert_refine_conditions_met] must be chosen such that the maximum cost of storage allocated by each staker is at most the size of their deposit. *) let assert_refine_conditions_met ctxt rollup lcc commitment = let open Lwt_tzresult_syntax in let* ctxt = assert_commitment_not_too_far_ahead ctxt rollup lcc commitment in let* ctxt = assert_commitment_period ctxt rollup commitment in if Sc_rollup_repr.Number_of_ticks.equal Commitment.(commitment.number_of_ticks) Sc_rollup_repr.Number_of_ticks.zero then assert_same_hash_as_predecessor ctxt rollup commitment else return ctxt let get_commitment_stake_count ctxt rollup node = let open Lwt_tzresult_syntax in let* ctxt, maybe_staked_on_commitment = Store.Commitment_stake_count.find (ctxt, rollup) node in return (Option.value ~default:0l maybe_staked_on_commitment, ctxt) let modify_commitment_stake_count ctxt rollup node f = let open Lwt_tzresult_syntax in let* count, ctxt = get_commitment_stake_count ctxt rollup node in let new_count = f count in let* ctxt, size_diff, _was_bound = Store.Commitment_stake_count.add (ctxt, rollup) node new_count in return (new_count, size_diff, ctxt) let deallocate_commitment ctxt rollup node = let open Lwt_tzresult_syntax in if Commitment_hash.(node = zero) then return ctxt else let* ctxt, _size_freed = Store.Commitments.remove_existing (ctxt, rollup) node in return ctxt let deallocate_commitment_metadata ctxt rollup node = let open Lwt_tzresult_syntax in if Commitment_hash.(node = zero) then return ctxt else let* ctxt, _size_freed = Store.Commitment_added.remove_existing (ctxt, rollup) node in let* ctxt, _size_freed = Store.Commitment_stake_count.remove_existing (ctxt, rollup) node in return ctxt let deallocate ctxt rollup node = let open Lwt_tzresult_syntax in let* ctxt = deallocate_commitment_metadata ctxt rollup node in deallocate_commitment ctxt rollup node let find_commitment_to_deallocate ctxt rollup commitment_hash ~num_commitments_to_keep = let open Lwt_result_syntax in let rec aux ctxt commitment_hash n = if Compare.Int.(n = 0) then return (Some commitment_hash, ctxt) else let* pred_hash, ctxt = Commitment_storage.get_predecessor_opt_unsafe ctxt rollup commitment_hash in match pred_hash with | None -> return (None, ctxt) | Some pred_hash -> (aux [@ocaml.tailcall]) ctxt pred_hash (n - 1) in aux ctxt commitment_hash num_commitments_to_keep let decrease_commitment_stake_count ctxt rollup node = let open Lwt_tzresult_syntax in let* new_count, _size_diff, ctxt = modify_commitment_stake_count ctxt rollup node Int32.pred in if Compare.Int32.(new_count <= 0l) then deallocate ctxt rollup node else return ctxt let increase_commitment_stake_count ctxt rollup node = let open Lwt_tzresult_syntax in let* _new_count, size_diff, ctxt = modify_commitment_stake_count ctxt rollup node Int32.succ in return (size_diff, ctxt) (* 77 for Commitments entry + 4 for Commitment_stake_count entry + 4 for Commitment_added entry + 0 for Staker_count_update entry *) let commitment_storage_size_in_bytes = 85 let refine_stake ctxt rollup staker staked_on commitment = let open Lwt_tzresult_syntax in let* lcc, ctxt = Commitment_storage.last_cemented_commitment ctxt rollup in let* ctxt = assert_refine_conditions_met ctxt rollup lcc commitment in let*? ctxt, new_hash = Sc_rollup_commitment_storage.hash ctxt commitment in (* TODO: https://gitlab.com/tezos/tezos/-/issues/2559 Add a test checking that L2 nodes can catch up after going offline. *) let rec go node ctxt = (* WARNING: Do NOT reorder this sequence of ifs. we must check for staked_on before LCC, since refining from the LCC to another commit is a valid operation. *) if Commitment_hash.(node = staked_on) then ( (* Previously staked commit found: Insert new commitment if not existing *) let* ctxt, commitment_size_diff, _was_bound = Store.Commitments.add (ctxt, rollup) new_hash commitment in let level = (Raw_context.current_level ctxt).level in let* commitment_added_size_diff, commitment_added_level, ctxt = Commitment_storage.set_commitment_added ctxt rollup new_hash level in let* ctxt, staker_count_diff = Store.Stakers.update (ctxt, rollup) staker new_hash in let* stake_count_size_diff, ctxt = increase_commitment_stake_count ctxt rollup new_hash in (* WARNING: [commitment_storage_size] is a defined constant, and used to set a bound on the relationship between [max_lookahead], [commitment_period] and [stake_amount]. Be careful changing this calculation. *) let size_diff = commitment_size_diff + commitment_added_size_diff + stake_count_size_diff + staker_count_diff in let expected_size_diff = commitment_storage_size_in_bytes in (* First submission adds [commitment_storage_size_in_bytes] to storage. Later submission adds 0 due to content-addressing. *) assert (Compare.Int.(size_diff = 0 || size_diff = expected_size_diff)) ; return (new_hash, commitment_added_level, ctxt) (* See WARNING above. *)) else let* () = (* We reached the LCC, but [staker] is not staked directly on it. Thus, we backtracked. Note that everyone is staked indirectly on the LCC. *) fail_when Commitment_hash.(node = lcc) Sc_rollup_staker_backtracked in let* pred, ctxt = Commitment_storage.get_predecessor_unsafe ctxt rollup node in let* _size, ctxt = increase_commitment_stake_count ctxt rollup node in (go [@ocaml.tailcall]) pred ctxt in go Commitment.(commitment.predecessor) ctxt let publish_commitment ctxt rollup staker commitment = let open Lwt_tzresult_syntax in let* ctxt, staked_on_opt = Store.Stakers.find (ctxt, rollup) staker in let* ctxt, balance_updates, staked_on = match staked_on_opt with | Some staked_on -> return (ctxt, [], staked_on) | None -> deposit_stake ctxt rollup staker in let+ commitment_hash, ctxt, level = refine_stake ctxt rollup staker staked_on commitment in (commitment_hash, ctxt, level, balance_updates) let cement_commitment ctxt rollup new_lcc = let open Lwt_tzresult_syntax in let refutation_deadline_blocks = Constants_storage.sc_rollup_challenge_window_in_blocks ctxt in (* Calling [last_final_commitment] first to trigger failure in case of non-existing rollup. *) let* old_lcc, ctxt = Commitment_storage.last_cemented_commitment ctxt rollup in (* Get is safe, as [Stakers_size] is initialized on origination. *) let* ctxt, total_staker_count = Store.Staker_count.get ctxt rollup in let* () = fail_when Compare.Int32.(total_staker_count <= 0l) Sc_rollup_no_stakers in let* new_lcc_commitment, ctxt = Commitment_storage.get_commitment_unsafe ctxt rollup new_lcc in let* () = fail_when Commitment_hash.(new_lcc_commitment.predecessor <> old_lcc) Sc_rollup_parent_not_lcc in let* new_lcc_stake_count, ctxt = get_commitment_stake_count ctxt rollup new_lcc in let* () = fail_when Compare.Int32.(total_staker_count <> new_lcc_stake_count) Sc_rollup_disputed in let* ctxt, new_lcc_added = Store.Commitment_added.get (ctxt, rollup) new_lcc in let* () = let current_level = (Raw_context.current_level ctxt).level in let min_level = Raw_level_repr.add new_lcc_added refutation_deadline_blocks in fail_when Raw_level_repr.(current_level < min_level) (Sc_rollup_commitment_too_recent {current_level; min_level}) in (* update LCC *) let* ctxt, lcc_size_diff = Store.Last_cemented_commitment.update ctxt rollup new_lcc in assert (Compare.Int.(lcc_size_diff = 0)) ; (* At this point we know that all stakers are implicitly staked on the new LCC, and no one is directly staked on the old LCC. Therefore we can safely deallocate the metadata ([Commitment_added] and [Commitment_stake_count]) of the old LCC. However, we must not remove the commitment itself as we need it to allow executing outbox messages for a limited period. The maximum number of active cemented commitments available for execution is specified in [ctxt.sc_rollup.max_number_of_stored_cemented_commitments]. Instead, we remove the oldest cemented commitment that would exceed [max_number_of_cemented_commitments], if such exist. *) let* ctxt = deallocate_commitment_metadata ctxt rollup old_lcc in (* Decrease max_number_of_stored_cemented_commitments by one because we start counting commitments from old_lcc, rather than from new_lcc. *) let num_commitments_to_keep = (Raw_context.constants ctxt).sc_rollup .max_number_of_stored_cemented_commitments - 1 in let* commitment_to_deallocate, ctxt = find_commitment_to_deallocate ~num_commitments_to_keep ctxt rollup old_lcc in match commitment_to_deallocate with | None -> return (ctxt, new_lcc_commitment) | Some old_lcc -> let+ ctxt = deallocate_commitment ctxt rollup old_lcc in (ctxt, new_lcc_commitment) let remove_staker ctxt rollup staker = let open Lwt_tzresult_syntax in let* lcc, ctxt = Commitment_storage.last_cemented_commitment ctxt rollup in let* ctxt, res = Store.Stakers.find (ctxt, rollup) staker in match res with | None -> fail Sc_rollup_not_staked | Some staked_on -> let* () = fail_when Commitment_hash.(staked_on = lcc) Sc_rollup_remove_lcc in let staker_contract, stake = get_contract_and_stake ctxt staker in let bond_id = Bond_id_repr.Sc_rollup_bond_id rollup in let* ctxt, balance_updates = Token.transfer ctxt (`Frozen_bonds (staker_contract, bond_id)) `Sc_rollup_refutation_punishments stake in let* ctxt, _size_diff = Store.Stakers.remove_existing (ctxt, rollup) staker in let* ctxt = modify_staker_count ctxt rollup Int32.pred in let rec go node ctxt = if Commitment_hash.(node = lcc) then return ctxt else let* pred, ctxt = Commitment_storage.get_predecessor_unsafe ctxt rollup node in let* ctxt = decrease_commitment_stake_count ctxt rollup node in (go [@ocaml.tailcall]) pred ctxt in let+ ctxt = go staked_on ctxt in (ctxt, balance_updates) module Internal_for_tests = struct let deposit_stake = deposit_stake let refine_stake ctxt rollup staker ?staked_on commitment = let open Lwt_tzresult_syntax in match staked_on with | Some staked_on -> refine_stake ctxt rollup staker staked_on commitment | None -> (* This allows to call {!refine_stake} without explicitely passing the staked_on parameter, it's more convenient for tests. However, it still enforce that {!deposit_stake} was called before. *) let* _ctxt, staked_on = Store.Stakers.get (ctxt, rollup) staker in refine_stake ctxt rollup staker staked_on commitment end
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>