package octez-shell-libs
Octez shell libraries
Install
Dune Dependency
Authors
Maintainers
Sources
tezos-octez-v20.1.tag.bz2
sha256=ddfb5076eeb0b32ac21c1eed44e8fc86a6743ef18ab23fff02d36e365bb73d61
sha512=d22a827df5146e0aa274df48bc2150b098177ff7e5eab52c6109e867eb0a1f0ec63e6bfbb0e3645a6c2112de3877c91a17df32ccbff301891ce4ba630c997a65
doc/src/octez-shell-libs.shell/bootstrap_pipeline.ml.html
Source file bootstrap_pipeline.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 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
(*****************************************************************************) (* *) (* Open Source License *) (* Copyright (c) 2020-2021 Nomadic Labs. <contact@nomadic-labs.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. *) (* *) (*****************************************************************************) open Validation_errors (** Workflow of the bootstrap pipeline. +-------+ |Locator| +---+---+ | | +---------v---------+ +----------------------+ | promise <-----------------+ | | fetching headers | | distributed_db | | +-----------------> | +---------+---------+ +----------------------+ | | +---v---+ | pipe | +---+---+ | | +---------v---------+ +----------------------+ | promise <-----------------+ | |fetching operations| | distributed_db | | +-----------------> | +---------+---------+ +----------------------+ | | +---v---+ | pipe | +---+---+ | | +---------v---------+ +----------------------+ | promsie <-----------------+ block | | validating blocks | | validator | | +-----------------> | +-------------------+ +----------------------+ *) (** Overview: The [bootstrap_pipeline] is a promise which is fulfilled when all block hashes of a locator has been valided. It is canceled if one of the three premises above fails. The promise "fetching headers" fetches headers step by step (a locator being a list of steps). [steps] are processed bottom to top. A [step] is a subchain delimited by two block hashes. A subchain being a list of block [[b1;...;bn]] such that [bi.pred] = hash([bj]) where i = j + 1. Headers are fetched from the [distributed_db] top to bottom but are enqueued in the [pipe] bottom to top. The promise is fulfilled if every hashes contain in the locator steps were successfuly enqueued in the [pipe]. The promise is canceled if an error from the [distrubted_db] is raised, or if the [locator] was invalid. The promise "fetching operations" dequeue block headers and for each block header fetches the operations contained in the block. Once all the operations are fetched, it enqueues the headers and the operations in a [pipe] used by the promise validating blocks. This promise is fulfilled when it fetches all the operations for all the blocks that were in the input [pipe]. It is canceled if the [distributed_db] raised an error. The promise "validating blocks" dequeue full blocks and give them to the [Block_validator]. The promise is fulfilled is all blocks were validated successfuly. It is canceled otherwise. *) (** An event is trigerred when the node is fetching large steps of a [Block_locator] from the network. A large step is defined by [big_step_size]. In that case an event is made every [big_step_size_announced]. *) let big_step_size, big_step_size_announce = (2000, 1000) (** The promises which fetches headers and operations communicate through a [Lwt_pipe.Bounded]. This pipe stores headers by batch. The size of the batch is defined by [header_batch_size]. *) let header_batch_size = 20 (** Size of the [Lwt_pipe.Bounded] containing the fetched headers. If this size is reached, the promise which fetches headers holds and wait that the promise which fetches operations to dequeue some headers. This means that the maximum number of headers the queue can contain is [fetched_headers_queue_size] * [batch_header_size]. *) let fetched_headers_queue_size = 1024 (** Size of the queue containing a full blocks (block + operations) before they are processed by the [Block_validator]. *) let fetched_blocks_queue_size = 128 type t = { canceler : Lwt_canceler.t; block_header_timeout : Time.System.Span.t; block_operations_timeout : Time.System.Span.t; mutable headers_fetch_worker : unit Lwt.t; mutable operations_fetch_worker : unit Lwt.t; mutable validation_worker : unit Lwt.t; peer_id : P2p_peer.Id.t; chain_db : Distributed_db.chain_db; locator : Block_locator.t; block_validator : Block_validator.t; notify_new_block : Block_validator.new_block -> unit; fetched_headers : (Block_hash.t * Block_header.t) list Lwt_pipe.Bounded.t; fetched_blocks : (Block_hash.t * Block_header.t * Operation.t list list tzresult Lwt.t) Lwt_pipe.Bounded.t; (* HACK, a worker should be able to return the 'error'. *) mutable errors : Error_monad.error list; } (* FIXME: this function may be called many times by different bootstrap pipelines on the same hash (and therefore same header). This can be fixed by having only one bootstrap_pipeline. *) (** A block is NOT acceptable if one of the following holds: - The timestamp of the block is more than s seconds in the future, where s is specified by the module Clock_drift. - The block is at the same level as the checkpoint, but they are different. - The checkpoint has been reached (that is, the head of the chain is past the checkpoint) but the block is not yet in the chain. *) let assert_acceptable_header pipeline hash (header : Block_header.t) = let open Lwt_result_syntax in let chain_store = Distributed_db.chain_store pipeline.chain_db in let time_now = Time.System.now () in let* () = fail_unless (Clock_drift.is_not_too_far_in_the_future header.shell.timestamp) (Future_block_header {block = hash; time = time_now; block_time = header.shell.timestamp}) in let*! checkpoint_hash, checkpoint_level = Store.Chain.checkpoint chain_store in let* () = fail_when (Compare.Int32.(header.shell.level = checkpoint_level) && not (Block_hash.equal hash checkpoint_hash)) (Checkpoint_error (hash, Some pipeline.peer_id)) in let*! current_head = Store.Chain.current_head chain_store in let checkpoint_reached = Compare.Int32.(Store.Block.level current_head >= checkpoint_level) in if checkpoint_reached then (* If the checkpoint is reached, every block before the checkpoint must be part of the chain. *) if header.shell.level <= checkpoint_level then let*! in_chain = Store.Chain.is_in_chain chain_store (hash, header.shell.level) in fail_unless in_chain (Checkpoint_error (hash, Some pipeline.peer_id)) else return_unit else return_unit (** [fetch_step] fetches block headers given a [Block_locator.step] and returns them as a list. It fetches headers iteratively starting from the top block down to the bottom block. Blocks are returned in the reverse order. At each iteration, the function does the following: 1. First, it does some sanity check to ensure that the locator is valid. 2. Then it asks to the [Distributed_db] for the block header associated to the hash of the block. 3. It checks whether the received header is acceptable. 4. It loops on the predecessor of the current block. *) let fetch_step pipeline (step : Block_locator.step) = let open Lwt_result_syntax in let rec fetch_loop acc hash cpt = let*! () = Lwt.pause () in let*! () = if step.step > big_step_size && 0 <> cpt && cpt mod big_step_size_announce = 0 then Bootstrap_pipeline_event.(emit still_fetching_large_step_from_peer) (pipeline.peer_id, cpt, step.step) else Lwt.return_unit in if cpt > step.step then let*! () = Bootstrap_pipeline_event.(emit step_too_long) pipeline.peer_id in tzfail (Invalid_locator (pipeline.peer_id, pipeline.locator)) else if Block_hash.equal hash step.predecessor then if step.strict_step && cpt <> step.step then let*! () = Bootstrap_pipeline_event.(emit step_too_short) pipeline.peer_id in tzfail (Invalid_locator (pipeline.peer_id, pipeline.locator)) else return acc else let chain_store = Distributed_db.chain_store pipeline.chain_db in let*! in_chain = let*! o = Store.Block.read_block_opt chain_store hash in match o with | Some b -> Store.Chain.is_in_chain chain_store (hash, Store.Block.level b) | None -> Lwt.return_false in if in_chain then return acc else let* header = protect ~canceler:pipeline.canceler (fun () -> Distributed_db.Block_header.fetch ~timeout:pipeline.block_header_timeout pipeline.chain_db ~peer:pipeline.peer_id hash ()) in let* () = assert_acceptable_header pipeline hash header in let*! () = Bootstrap_pipeline_event.(emit fetching_block_header_from_peer) (hash, pipeline.peer_id, cpt, step.step) in fetch_loop ((hash, header) :: acc) header.shell.predecessor (cpt + 1) in fetch_loop [] step.block 0 (** [headers_fetch_work_loop] is a promise which fetches headers locator step by locator step and store them in a queue. Each locator step is processed bottom to top by the [fetch_step] function. This promise is fulfilled if it fetches all the locators and store them successfuly in the queue. It is canceled the first time it was unable to fetch a header or if the [locator] was invalid. A step may be truncated in [rolling] or in [full] mode if the blocks are below the [savepoint].*) let headers_fetch_worker_loop pipeline = let open Lwt_result_syntax in let*! r = let sender_id = Distributed_db.my_peer_id pipeline.chain_db in (* sender and receiver are inverted here because they are from the point of view of the node sending the locator *) let seed = {Block_locator.sender_id = pipeline.peer_id; receiver_id = sender_id} in let chain_store = Distributed_db.chain_store pipeline.chain_db in let*! savepoint = match Store.Chain.history_mode chain_store with | History_mode.Archive -> Lwt.return_none | Full _ | Rolling _ -> let*! v = Store.Chain.savepoint chain_store in Lwt.return_some v in (* In Full and Rolling mode, we do not want to receive blocks that are past our savepoint's level, otherwise we would start validating them again. *) let steps = match savepoint with | None -> Block_locator.to_steps seed pipeline.locator | Some (savepoint_hash, savepoint_level) -> let head_level = pipeline.locator.head_header.shell.level in let truncate_limit = Int32.(sub head_level savepoint_level) in Block_locator.to_steps_truncate ~limit:(Int32.to_int truncate_limit) ~save_point:savepoint_hash seed pipeline.locator in let locator_length = Block_locator.estimated_length seed pipeline.locator in let number_of_steps = List.length steps in let*! () = Bootstrap_pipeline_event.(emit fetching_locator) (locator_length, pipeline.peer_id) in match steps with | [] -> tzfail (Too_short_locator (sender_id, pipeline.locator)) | {Block_locator.predecessor; _} :: _ -> let*! predecessor_known = Store.Block.is_known chain_store predecessor in (* Check that the locator is anchored in a block locally known. *) let* () = fail_unless predecessor_known (Too_short_locator (sender_id, pipeline.locator)) in (* We add the headers by batch to the fetched_headers queue. If the queue is full, the [Lwt_pipe.Bounded.push] promise is pending until some headers are popped from the queue. *) let rec process_headers headers = let batch, remaining_headers = List.split_n header_batch_size headers in let* () = protect ~canceler:pipeline.canceler (fun () -> let*! () = Lwt_pipe.Bounded.push pipeline.fetched_headers batch in return_unit) in match remaining_headers with | [] -> return_unit | _ -> process_headers remaining_headers in let rec loop counter steps = match steps with | [] -> return_unit | current :: rest -> let open Block_locator in let*! () = Bootstrap_pipeline_event.(emit fetching_step_from_peer) ( counter, number_of_steps, current.step, current.block, current.predecessor, pipeline.peer_id ) in let* v = fetch_step pipeline current in let* () = process_headers v in loop (succ counter) rest in loop 1 steps in match r with | Ok () -> let*! () = Bootstrap_pipeline_event.(emit fetching_all_steps_from_peer) pipeline.peer_id in Lwt_pipe.Bounded.close pipeline.fetched_headers ; Lwt.return_unit | Error (Exn Lwt.Canceled :: _) | Error (Canceled :: _) | Error (Exn Lwt_pipe.Closed :: _) -> Lwt.return_unit | Error (Distributed_db.Block_header.Timeout bh :: _) -> let*! () = Bootstrap_pipeline_event.(emit header_request_timeout) (bh, pipeline.peer_id) in Error_monad.cancel_with_exceptions pipeline.canceler | Error (Future_block_header {block; block_time; time} :: _) -> let*! () = Bootstrap_pipeline_event.(emit locator_contains_future_block) (block, pipeline.peer_id, time, block_time) in Error_monad.cancel_with_exceptions pipeline.canceler | Error (Too_short_locator _ :: _ as err) -> pipeline.errors <- pipeline.errors @ err ; let*! () = Bootstrap_pipeline_event.(emit locator_too_short) () in Error_monad.cancel_with_exceptions pipeline.canceler | Error err -> pipeline.errors <- pipeline.errors @ err ; let*! () = Bootstrap_pipeline_event.(emit unexpected_error_while_fetching_headers) err in Error_monad.cancel_with_exceptions pipeline.canceler (** [operations_fetch_worker_loop] is a promise which fethches operations and store them with the corresponding header to a queue. Operations are fetched block by block bottom to top. The promise is fulfilled if every operation was fetched and stored successfuly in the queue. It is canceled if one operation could not be fetched. *) let rec operations_fetch_worker_loop pipeline = let open Lwt_result_syntax in let*! r = let*! () = Lwt.pause () in let* batch = protect ~canceler:pipeline.canceler (fun () -> let*! v = Lwt_pipe.Bounded.pop pipeline.fetched_headers in return v) in let* operationss = List.map_ep (fun (hash, header) -> let*! () = Bootstrap_pipeline_event.(emit fetching_operations) (hash, pipeline.peer_id) in let operations = let* operations = List.map_ep (fun i -> protect ~canceler:pipeline.canceler (fun () -> let*! res = Distributed_db.Operations.fetch ~timeout:pipeline.block_operations_timeout pipeline.chain_db ~peer:pipeline.peer_id (hash, i) header.Block_header.shell.operations_hash in Lwt.return res)) (0 -- (header.shell.validation_passes - 1)) in let*! () = Bootstrap_pipeline_event.(emit fetched_operations) (hash, pipeline.peer_id) in return operations in return (hash, header, operations)) batch in List.iter_es (fun (hash, header, operations) -> protect ~canceler:pipeline.canceler (fun () -> let*! () = Lwt_pipe.Bounded.push pipeline.fetched_blocks (hash, header, operations) in return_unit)) operationss in match r with | Ok () -> operations_fetch_worker_loop pipeline | Error (Exn Lwt.Canceled :: _) | Error (Canceled :: _) | Error (Exn Lwt_pipe.Closed :: _) -> Lwt_pipe.Bounded.close pipeline.fetched_blocks ; Lwt.return_unit | Error (Distributed_db.Operations.Timeout (bh, n) :: _) -> let*! () = Bootstrap_pipeline_event.(emit request_operations_timeout) (bh, n, pipeline.peer_id) in Error_monad.cancel_with_exceptions pipeline.canceler | Error err -> pipeline.errors <- pipeline.errors @ err ; let*! () = Bootstrap_pipeline_event.(emit unexpected_error_while_fetching_headers) err in Error_monad.cancel_with_exceptions pipeline.canceler (** [validation_work_loop] is a promise which validates blocks one by one using the [Block_validator.validate] function. Each validated block calls the [notify_new_block] callback. The promise is fulfilled if every block from the locator was validated. It is canceled if the validation of one block fails. *) let rec validation_worker_loop pipeline = let open Lwt_result_syntax in let*! r = let*! () = Lwt.pause () in let* hash, header, operations = protect ~canceler:pipeline.canceler (fun () -> let*! v = Lwt_pipe.Bounded.pop pipeline.fetched_blocks in return v) in let*! () = Bootstrap_pipeline_event.(emit requesting_validation) (hash, pipeline.peer_id) in let* operations in let* () = protect ~canceler:pipeline.canceler (fun () -> let*! r = Block_validator.precheck_and_apply ~canceler:pipeline.canceler ~notify_new_block:pipeline.notify_new_block ~precheck_and_notify:false pipeline.block_validator pipeline.chain_db hash header operations in match r with | Block_validator.Invalid errs | Unapplicable_after_precheck errs -> (* Cancel the pipeline if a block is invalid *) Lwt.return_error errs | Valid -> return_unit) in let*! () = Bootstrap_pipeline_event.(emit validated_block) (hash, pipeline.peer_id) in return_unit in match r with | Ok () -> validation_worker_loop pipeline | Error ((Exn Lwt.Canceled | Canceled | Exn Lwt_pipe.Closed) :: _) -> Lwt.return_unit | Error (( Block_validator_errors.Invalid_block _ | Block_validator_errors.Unavailable_protocol _ | Block_validator_errors.System_error _ | Timeout ) :: _ as err) -> (* Propagate the error to the peer validator. *) pipeline.errors <- pipeline.errors @ err ; Error_monad.cancel_with_exceptions pipeline.canceler | Error err -> pipeline.errors <- pipeline.errors @ err ; let*! () = Bootstrap_pipeline_event.(emit unexpected_error_while_fetching_headers) err in Error_monad.cancel_with_exceptions pipeline.canceler (** The creation of the bootstrap starts three promises: - One to fetch block headers - One to fetch block operations - One which validates operations It intializes two pipes so that promises can communicate each others (see diagram at the begining of the file). *) let create ?(notify_new_block = fun _ -> ()) ~block_header_timeout ~block_operations_timeout block_validator peer_id chain_db locator = let canceler = Lwt_canceler.create () in let fetched_headers = Lwt_pipe.Bounded.create ~max_size:fetched_headers_queue_size ~compute_size:(fun _ -> 1) () in let fetched_blocks = Lwt_pipe.Bounded.create ~max_size:fetched_blocks_queue_size ~compute_size:(fun _ -> 1) () in let pipeline = { canceler; block_header_timeout; block_operations_timeout; headers_fetch_worker = Lwt.return_unit; operations_fetch_worker = Lwt.return_unit; validation_worker = Lwt.return_unit; notify_new_block; peer_id; chain_db; locator; block_validator; fetched_headers; fetched_blocks; errors = []; } in Lwt_canceler.on_cancel pipeline.canceler (fun () -> Lwt_pipe.Bounded.close fetched_blocks ; Lwt_pipe.Bounded.close fetched_headers ; (* TODO proper cleanup of resources... *) Lwt.return_unit) ; pipeline.headers_fetch_worker <- Lwt_utils.worker (Format.asprintf "bootstrap_pipeline-headers_fetch.%a.%a" P2p_peer.Id.pp_short peer_id Block_hash.pp_short locator.Block_locator.head_hash) ~on_event:Internal_event.Lwt_worker_logger.on_event ~run:(fun () -> headers_fetch_worker_loop pipeline) ~cancel:(fun () -> Error_monad.cancel_with_exceptions pipeline.canceler) ; pipeline.operations_fetch_worker <- Lwt_utils.worker (Format.asprintf "bootstrap_pipeline-operations_fetch.%a.%a" P2p_peer.Id.pp_short peer_id Block_hash.pp_short locator.head_hash) ~on_event:Internal_event.Lwt_worker_logger.on_event ~run:(fun () -> operations_fetch_worker_loop pipeline) ~cancel:(fun () -> Error_monad.cancel_with_exceptions pipeline.canceler) ; pipeline.validation_worker <- Lwt_utils.worker (Format.asprintf "bootstrap_pipeline-validation.%a.%a" P2p_peer.Id.pp_short peer_id Block_hash.pp_short locator.head_hash) ~on_event:Internal_event.Lwt_worker_logger.on_event ~run:(fun () -> validation_worker_loop pipeline) ~cancel:(fun () -> Error_monad.cancel_with_exceptions pipeline.canceler) ; pipeline let wait_workers pipeline = let open Lwt_syntax in let* () = pipeline.headers_fetch_worker in let* () = pipeline.operations_fetch_worker in pipeline.validation_worker let wait pipeline = let open Lwt_syntax in let* () = wait_workers pipeline in match pipeline.errors with | [] -> return_ok_unit | errors -> Lwt.return_error errors let cancel pipeline = let open Lwt_syntax in let* _res = Lwt_canceler.cancel pipeline.canceler in wait_workers pipeline let length pipeline = Peer_validator_worker_state. { fetched_header_length = Lwt_pipe.Bounded.length pipeline.fetched_headers; fetched_block_length = Lwt_pipe.Bounded.length pipeline.fetched_blocks; } let length_zero = Peer_validator_worker_state. {fetched_header_length = 0; fetched_block_length = 0}
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>