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.validation/external_validator.ml.html
Source file external_validator.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
(*****************************************************************************) (* *) (* Open Source License *) (* Copyright (c) 2018 Nomadic Labs. <contact@nomadic-labs.com> *) (* Copyright (c) 2020 Metastate AG <hello@metastate.dev> *) (* *) (* 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. *) (* *) (*****************************************************************************) module Events = struct open Internal_event.Simple let section = ["external_validator"] let initialized = declare_0 ~section ~level:Info ~name:"initialized" ~msg:"validator initialized and listening" () let terminated = declare_0 ~section ~level:Info ~name:"terminated_request" ~msg:"validator terminated" () let dynload_protocol = declare_1 ~section ~level:Info ~name:"dynload_protocol" ~msg:"dynamic loading of protocol {protocol}" ~pp1:Protocol_hash.pp ("protocol", Protocol_hash.encoding) let validation_request = declare_1 ~section ~level:Info ~name:"validation_request" ~msg:"prechecking and applying block {block}" ~pp1:(fun fmt header -> Block_hash.pp fmt (Block_header.hash header)) ("block", Block_header.encoding) let application_request = declare_1 ~section ~level:Info ~name:"application_request" ~msg:"applying block {block}" ~pp1:(fun fmt header -> Block_hash.pp fmt (Block_header.hash header)) ("block", Block_header.encoding) let preapply_request = declare_1 ~section ~level:Info ~name:"preapply_request" ~msg:"preapplying on top of block {hash}" ~pp1:Block_hash.pp ("hash", Block_hash.encoding) let precheck_request = declare_1 ~section ~level:Info ~name:"precheck_request" ~msg:"prechecking block {hash}" ~pp1:Block_hash.pp ("hash", Block_hash.encoding) let commit_genesis_request = declare_1 ~section ~level:Info ~name:"commit_genesis_request" ~msg:"committing genesis block {genesis}" ~pp1:Block_hash.pp ("genesis", Block_hash.encoding) let initialization_request = declare_0 ~section ~level:Info ~name:"initialization_request" ~msg:"initializing validator's environment" () let fork_test_chain_request = declare_1 ~section ~level:Info ~name:"fork_testchain_request" ~msg:"forking test chain at block {block}" ~pp1:Block_header.pp ("block", Block_header.encoding) let context_gc_request = declare_1 ~section ~level:Info ~name:"context_gc_request" ~msg:"garbage collecting context below {context_hash}" ~pp1:Context_hash.pp ("context_hash", Context_hash.encoding) let context_split_request = declare_0 ~section ~level:Info ~name:"context_split_request" ~msg:"spliting context" () let reconfigure_event_logging_request = declare_0 ~section ~level:Info ~name:"reconfigure_event_logging_request" ~msg:"reconfiguring event logging" () let termination_request = declare_0 ~section ~level:Info ~name:"termination_request" ~msg:"validator terminated" () let emit = Internal_event.Simple.emit end open Filename.Infix let load_protocol proto protocol_root = let open Lwt_result_syntax in if Registered_protocol.mem proto then return_unit else let cmxs_file = protocol_root // Protocol_hash.to_short_b58check proto // Format.asprintf "protocol_%a.cmxs" Protocol_hash.pp proto in let*! () = Events.(emit dynload_protocol proto) in match Dynlink.loadfile_private cmxs_file with | () -> return_unit | exception Dynlink.Error err -> Format.ksprintf (fun msg -> tzfail Block_validator_errors.( Validation_process_failed (Protocol_dynlink_failure msg))) "Cannot load file: %s. (Expected location: %s.)" (Dynlink.error_message err) cmxs_file let with_retry_to_load_protocol protocol_root f = let open Lwt_syntax in let* r = f () in match r with | Error [Block_validator_errors.Unavailable_protocol {protocol; _}] as original_error -> ( (* If `next_protocol` is missing, try to load it *) let* r = load_protocol protocol protocol_root in match r with Error _ -> Lwt.return original_error | Ok () -> f ()) | _ -> Lwt.return r let inconsistent_handshake msg = Block_validator_errors.( Validation_process_failed (Inconsistent_handshake msg)) (* Handshake with the node. See [Block_validator_process.process_handshake] for the handshake scenario. *) let handshake input output = let open Lwt_syntax in let* () = External_validation.send output Data_encoding.Variable.bytes External_validation.magic in let* magic = External_validation.recv input Data_encoding.Variable.bytes in fail_when (not (Bytes.equal magic External_validation.magic)) (inconsistent_handshake "bad magic") (* Initialization of the external process thanks to the parameters sent by the node. This is expected to be run after the [handshake]. See [Block_validator_process.process_init] for the init scenario. *) let init input output = let open Lwt_result_syntax in let*! () = Events.(emit initialization_request ()) in let*! ({context_root; genesis; readonly; sandbox_parameters; dal_config; _} as parameters) = External_validation.recv input External_validation.parameters_encoding in let*? () = Tezos_crypto_dal.Cryptobox.Config.init_verifier_dal dal_config in let sandbox_parameters = Option.map (fun p -> ("sandbox_parameter", p)) sandbox_parameters in let*! context_index = Context.init ~patch_context:(fun ctxt -> let open Lwt_result_syntax in let ctxt = Shell_context.wrap_disk_context ctxt in let+ ctxt = Patch_context.patch_context genesis sandbox_parameters ctxt in Shell_context.unwrap_disk_context ctxt) ~readonly context_root in (* It is necessary to send the ok result, as a blocking promise for the node (see [Block_validator_process.process_init]), after a complete initialization as the node relies on the external validator for the context's initialization. *) let*! () = External_validation.send output (Error_monad.result_encoding Data_encoding.empty) (Ok ()) in return (parameters, context_index) let handle_request : type a. External_validation.parameters -> Context.index -> Context_ops.Environment_context.block_cache option -> (Block_validation.apply_result * Context_ops.Environment_context.t) option -> a External_validation.request -> [ `Continue of a tzresult * Context_ops.Environment_context.block_cache option * (Block_validation.apply_result * Context_ops.Environment_context.t) option | `Stop ] Lwt.t = fun { protocol_root; genesis; user_activated_upgrades; user_activated_protocol_overrides; operation_metadata_size_limit; _; } context_index cache cached_result -> let open Lwt_result_syntax in let continue res cache cached_result = Lwt.return (`Continue (res, cache, cached_result)) in function | Commit_genesis {chain_id} -> let*! () = Events.(emit commit_genesis_request genesis.Genesis.block) in let*! commit = Error_monad.catch_es (fun () -> Context.commit_genesis context_index ~chain_id ~time:genesis.time ~protocol:genesis.protocol) in continue commit cache None | Apply { chain_id; block_header; predecessor_block_header; predecessor_block_metadata_hash; predecessor_ops_metadata_hash; predecessor_resulting_context_hash; operations; max_operations_ttl; should_precheck; simulate; } -> let*! () = if should_precheck then Events.(emit validation_request block_header) else Events.(emit application_request block_header) in let*! block_application_result = let* predecessor_context = Error_monad.catch_es (fun () -> let*! o = Context.checkout context_index predecessor_resulting_context_hash in match o with | Some c -> return (Shell_context.wrap_disk_context c) | None -> tzfail (Block_validator_errors.Failed_to_checkout_context predecessor_resulting_context_hash)) in let*! protocol_hash = Context_ops.get_protocol predecessor_context in let* () = load_protocol protocol_hash protocol_root in let env = { Block_validation.chain_id; user_activated_upgrades; user_activated_protocol_overrides; operation_metadata_size_limit; max_operations_ttl; predecessor_block_header; predecessor_block_metadata_hash; predecessor_ops_metadata_hash; predecessor_context; predecessor_resulting_context_hash; } in let cache = match cache with | None -> `Load | Some cache -> `Inherited (cache, predecessor_resulting_context_hash) in with_retry_to_load_protocol protocol_root (fun () -> Block_validation.apply ~simulate ?cached_result ~should_precheck env block_header operations ~cache) in let block_application_result, cache = match block_application_result with | Error _ as err -> (err, cache) | Ok {result; cache} -> ( Ok result, Some { context_hash = result.validation_store.resulting_context_hash; cache; } ) in continue block_application_result cache None | Preapply { chain_id; timestamp; protocol_data; live_blocks; live_operations; predecessor_shell_header; predecessor_hash; predecessor_max_operations_ttl; predecessor_block_metadata_hash; predecessor_ops_metadata_hash; predecessor_resulting_context_hash; operations; } -> let*! () = Events.(emit preapply_request predecessor_hash) in let*! block_preapplication_result = let* predecessor_context = Error_monad.catch_es (fun () -> let*! context = Context.checkout context_index predecessor_resulting_context_hash in match context with | Some context -> return (Shell_context.wrap_disk_context context) | None -> tzfail (Block_validator_errors.Failed_to_checkout_context predecessor_resulting_context_hash)) in let*! protocol_hash = Context_ops.get_protocol predecessor_context in let* () = load_protocol protocol_hash protocol_root in let cache = match cache with | None -> `Load | Some cache -> `Inherited (cache, predecessor_resulting_context_hash) in with_retry_to_load_protocol protocol_root (fun () -> Block_validation.preapply ~chain_id ~user_activated_upgrades ~user_activated_protocol_overrides ~operation_metadata_size_limit ~timestamp ~protocol_data ~live_blocks ~live_operations ~predecessor_context ~predecessor_shell_header ~predecessor_hash ~predecessor_max_operations_ttl ~predecessor_block_metadata_hash ~predecessor_ops_metadata_hash ~predecessor_resulting_context_hash ~cache operations) in let*! res, cachable_result = match block_preapplication_result with | Ok (res, last_preapplied_context) -> Lwt.return (Ok res, Some last_preapplied_context) | Error _ as err -> Lwt.return (err, None) in continue res cache cachable_result | External_validation.Precheck { chain_id; predecessor_block_header; predecessor_block_hash; predecessor_resulting_context_hash; header; operations; hash; } -> let*! () = Events.(emit precheck_request hash) in let*! block_precheck_result = let* predecessor_context = Error_monad.catch_es (fun () -> let*! o = Context.checkout context_index predecessor_resulting_context_hash in match o with | Some context -> return (Shell_context.wrap_disk_context context) | None -> tzfail (Block_validator_errors.Failed_to_checkout_context predecessor_resulting_context_hash)) in let cache = match cache with | None -> `Lazy | Some cache -> `Inherited (cache, predecessor_resulting_context_hash) in let*! protocol_hash = Context_ops.get_protocol predecessor_context in let* () = load_protocol protocol_hash protocol_root in with_retry_to_load_protocol protocol_root (fun () -> Block_validation.precheck ~chain_id ~predecessor_block_header ~predecessor_block_hash ~predecessor_context ~predecessor_resulting_context_hash ~cache header operations) in continue block_precheck_result cache cached_result | External_validation.Fork_test_chain {chain_id; context_hash; forked_header} -> let*! () = Events.(emit fork_test_chain_request forked_header) in let*! context_opt = Context.checkout context_index context_hash in let*! res = match context_opt with | Some ctxt -> let ctxt = Shell_context.wrap_disk_context ctxt in with_retry_to_load_protocol protocol_root (fun () -> Block_validation.init_test_chain chain_id ctxt forked_header) | None -> tzfail (Block_validator_errors.Failed_to_checkout_context context_hash) in continue res cache cached_result | External_validation.Context_garbage_collection {context_hash; gc_lockfile_path} -> let*! () = Events.(emit context_gc_request context_hash) in let*! () = Context.gc context_index context_hash in let*! lockfile = Lwt_unix.openfile gc_lockfile_path [Unix.O_CREAT; O_RDWR; O_CLOEXEC; O_SYNC] 0o644 in let*! () = Lwt.catch (fun () -> Lwt_unix.lockf lockfile Unix.F_LOCK 0) (fun exn -> let*! () = Lwt_unix.close lockfile in Lwt.fail exn) in let gc_waiter () = Lwt.finalize (fun () -> let*! () = Context.wait_gc_completion context_index in let*! () = Lwt_unix.lockf lockfile Unix.F_ULOCK 0 in Lwt.return_unit) (fun () -> Lwt_unix.close lockfile) in let () = Lwt.dont_wait gc_waiter (fun _exn -> ()) in continue (Ok ()) cache cached_result | External_validation.Context_split -> let*! () = Events.(emit context_split_request) () in let*! () = Context.split context_index in continue (Ok ()) cache cached_result | External_validation.Terminate -> let*! () = Lwt_io.flush_all () in let*! () = Events.(emit termination_request ()) in Lwt.return `Stop | External_validation.Reconfigure_event_logging config -> let*! () = Events.(emit reconfigure_event_logging_request ()) in let*! res = Tezos_base_unix.Internal_event_unix.Configuration.reapply config in continue res cache cached_result let run ~using_std_channel input output = let open Lwt_result_syntax in let* () = handshake input output in let* parameters, context_index = init input output in let*! () = (* if the external validator is spawned in a standalone way and communicates with the node through stdin/stdoud, we do no start the logging system. *) if using_std_channel then Lwt.return_unit else Tezos_base_unix.Internal_event_unix.init ~config:parameters.internal_events () in (* Main loop waiting for request to be processed, forever, until the [Terminate] request is received. *) let rec loop (cache : Tezos_protocol_environment.Context.block_cache option) cached_result = let*! (External_validation.Erequest recved) = External_validation.recv input External_validation.request_encoding in let*! action = handle_request parameters context_index cache cached_result recved in match action with | `Continue (res, cache, cached_result) -> let*! () = External_validation.send output (Error_monad.result_encoding (External_validation.result_encoding recved)) res in loop cache cached_result | `Stop -> return_unit in loop None None let main ?socket_dir () = let open Lwt_result_syntax in let canceler = Lwt_canceler.create () in let* in_channel, out_channel, using_std_channel = match socket_dir with | Some socket_dir -> let pid = Unix.getpid () in let socket_path = External_validation.socket_path ~socket_dir ~pid in let* socket_process = External_validation.create_socket_connect ~canceler ~socket_path in let socket_in = Lwt_io.of_fd ~mode:Input socket_process in let socket_out = Lwt_io.of_fd ~mode:Output socket_process in return (socket_in, socket_out, false) | None -> return (Lwt_io.stdin, Lwt_io.stdout, true) in let*! () = Events.(emit initialized ()) in let*! r = Error_monad.catch_es (fun () -> let* () = run ~using_std_channel in_channel out_channel in let*! r = Lwt_canceler.cancel canceler in match r with | Ok () | Error [] -> return_unit | Error (exc :: excs) -> let texc = TzTrace.make (Error_monad.Exn exc) in let texcs = List.map (fun exc -> TzTrace.make (Error_monad.Exn exc)) excs in let t = TzTrace.conp_list texc texcs in Lwt.return (Error t)) in match r with | Ok () -> let*! () = Events.(emit terminated ()) in return_unit | Error _ as errs -> let*! () = External_validation.send out_channel (Error_monad.result_encoding Data_encoding.unit) errs in Lwt.return errs
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>