Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Page
Library
Module
Module type
Parameter
Class
Class type
Source
external_validation.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
(*****************************************************************************) (* *) (* 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. *) (* *) (*****************************************************************************) type parameters = { context_root : string; protocol_root : string; genesis : Genesis.t; sandbox_parameters : Data_encoding.json option; user_activated_upgrades : User_activated.upgrades; user_activated_protocol_overrides : User_activated.protocol_overrides; operation_metadata_size_limit : Shell_limits.operation_metadata_size_limit; dal_config : Tezos_crypto_dal.Cryptobox.Config.t; } type request = | Init | Validate of { chain_id : Chain_id.t; block_header : Block_header.t; predecessor_block_header : Block_header.t; predecessor_block_metadata_hash : Block_metadata_hash.t option; predecessor_ops_metadata_hash : Operation_metadata_list_list_hash.t option; predecessor_resulting_context_hash : Context_hash.t; operations : Operation.t list list; max_operations_ttl : int; should_precheck : bool; simulate : bool; } | Preapply of { chain_id : Chain_id.t; timestamp : Time.Protocol.t; protocol_data : bytes; live_blocks : Block_hash.Set.t; live_operations : Operation_hash.Set.t; predecessor_shell_header : Block_header.shell_header; predecessor_hash : Block_hash.t; predecessor_max_operations_ttl : int; predecessor_block_metadata_hash : Block_metadata_hash.t option; predecessor_ops_metadata_hash : Operation_metadata_list_list_hash.t option; predecessor_resulting_context_hash : Context_hash.t; operations : Operation.t list list; } | Precheck of { chain_id : Chain_id.t; predecessor_block_header : Block_header.t; predecessor_block_hash : Block_hash.t; predecessor_resulting_context_hash : Context_hash.t; header : Block_header.t; operations : Operation.t list list; hash : Block_hash.t; } | Commit_genesis of {chain_id : Chain_id.t} | Fork_test_chain of { chain_id : Chain_id.t; context_hash : Context_hash.t; forked_header : Block_header.t; } | Context_garbage_collection of { context_hash : Context_hash.t; gc_lockfile_path : string; } | Context_split | Terminate | Reconfigure_event_logging of Tezos_base_unix.Internal_event_unix.Configuration.t let request_pp ppf = function | Init -> Format.fprintf ppf "process handshake" | Validate {block_header; chain_id; _} -> Format.fprintf ppf "block validation %a for chain %a" Block_hash.pp_short (Block_header.hash block_header) Chain_id.pp_short chain_id | Preapply {predecessor_hash; chain_id; _} -> Format.fprintf ppf "preapply block ontop of %a for chain %a" Block_hash.pp_short predecessor_hash Chain_id.pp_short chain_id | Precheck {hash; _} -> Format.fprintf ppf "precheck block %a" Block_hash.pp_short hash | Commit_genesis {chain_id} -> Format.fprintf ppf "commit genesis block for chain %a" Chain_id.pp_short chain_id | Fork_test_chain {forked_header; _} -> Format.fprintf ppf "test chain fork on block %a" Block_hash.pp_short (Block_header.hash forked_header) | Terminate -> Format.fprintf ppf "terminate validation process" | Context_garbage_collection {context_hash; gc_lockfile_path = _} -> Format.fprintf ppf "garbage collecting context below %a" Context_hash.pp context_hash | Context_split -> Format.fprintf ppf "splitting context" | Reconfigure_event_logging _ -> Format.fprintf ppf "reconfigure event logging" let magic = Bytes.of_string "TEZOS_FORK_VALIDATOR_MAGIC_0" let parameters_encoding = let open Data_encoding in conv (fun { context_root; protocol_root; genesis; user_activated_upgrades; user_activated_protocol_overrides; operation_metadata_size_limit; sandbox_parameters; dal_config; } -> ( context_root, protocol_root, genesis, user_activated_upgrades, user_activated_protocol_overrides, operation_metadata_size_limit, sandbox_parameters, dal_config )) (fun ( context_root, protocol_root, genesis, user_activated_upgrades, user_activated_protocol_overrides, operation_metadata_size_limit, sandbox_parameters, dal_config ) -> { context_root; protocol_root; genesis; user_activated_upgrades; user_activated_protocol_overrides; operation_metadata_size_limit; sandbox_parameters; dal_config; }) (obj8 (req "context_root" string) (req "protocol_root" string) (req "genesis" Genesis.encoding) (req "user_activated_upgrades" User_activated.upgrades_encoding) (req "user_activated_protocol_overrides" User_activated.protocol_overrides_encoding) (req "operation_metadata_size_limit" Shell_limits.operation_metadata_size_limit_encoding) (opt "sandbox_parameters" json) (req "dal_config" Tezos_crypto_dal.Cryptobox.Config.encoding)) let case_validate tag = let open Data_encoding in case tag ~title:"validate" (obj10 (req "chain_id" Chain_id.encoding) (req "block_header" (dynamic_size Block_header.encoding)) (req "pred_header" (dynamic_size Block_header.encoding)) (opt "pred_block_metadata_hash" Block_metadata_hash.encoding) (opt "pred_ops_metadata_hash" Operation_metadata_list_list_hash.encoding) (req "predecessor_resulting_context_hash" Context_hash.encoding) (req "max_operations_ttl" int31) (req "operations" (list (list (dynamic_size Operation.encoding)))) (req "should_precheck" bool) (req "simulate" bool)) (function | Validate { chain_id; block_header; predecessor_block_header; predecessor_block_metadata_hash; predecessor_ops_metadata_hash; predecessor_resulting_context_hash; max_operations_ttl; operations; should_precheck; simulate; } -> Some ( chain_id, block_header, predecessor_block_header, predecessor_block_metadata_hash, predecessor_ops_metadata_hash, predecessor_resulting_context_hash, max_operations_ttl, operations, should_precheck, simulate ) | _ -> None) (fun ( chain_id, block_header, predecessor_block_header, predecessor_block_metadata_hash, predecessor_ops_metadata_hash, predecessor_resulting_context_hash, max_operations_ttl, operations, should_precheck, simulate ) -> Validate { chain_id; block_header; predecessor_block_header; predecessor_block_metadata_hash; predecessor_ops_metadata_hash; predecessor_resulting_context_hash; max_operations_ttl; operations; should_precheck; simulate; }) let case_preapply tag = let open Data_encoding in case tag ~title:"preapply" (merge_objs (obj10 (req "chain_id" Chain_id.encoding) (req "timestamp" Time.Protocol.encoding) (req "protocol_data" bytes) (req "live_blocks" Block_hash.Set.encoding) (req "live_operations" Operation_hash.Set.encoding) (req "predecessor_shell_header" Block_header.shell_header_encoding) (req "predecessor_hash" Block_hash.encoding) (req "predecessor_max_operations_ttl" int31) (opt "predecessor_block_metadata_hash" Block_metadata_hash.encoding) (opt "predecessor_ops_metadata_hash" Operation_metadata_list_list_hash.encoding)) (obj2 (req "predecessor_resulting_context_hash" Context_hash.encoding) (req "operations" (list (list (dynamic_size Operation.encoding)))))) (function | 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; } -> Some ( ( 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) ) | _ -> None) (fun ( ( 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) ) -> 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 case_precheck tag = let open Data_encoding in case tag ~title:"precheck" (obj7 (req "chain_id" Chain_id.encoding) (req "predecessor_block_header" (dynamic_size Block_header.encoding)) (req "predecessor_block_hash" Block_hash.encoding) (req "predecessor_resulting_context_hash" Context_hash.encoding) (req "header" (dynamic_size Block_header.encoding)) (req "hash" Block_hash.encoding) (req "operations" (list (list (dynamic_size Operation.encoding))))) (function | Precheck { chain_id; predecessor_block_header; predecessor_block_hash; predecessor_resulting_context_hash; header; operations; hash; } -> Some ( chain_id, predecessor_block_header, predecessor_block_hash, predecessor_resulting_context_hash, header, hash, operations ) | _ -> None) (fun ( chain_id, predecessor_block_header, predecessor_block_hash, predecessor_resulting_context_hash, header, hash, operations ) -> Precheck { chain_id; predecessor_block_header; predecessor_block_hash; predecessor_resulting_context_hash; header; operations; hash; }) let case_context_gc tag = let open Data_encoding in case tag ~title:"context_gc" (obj2 (req "context_hash" Context_hash.encoding) (req "gc_lockfile_path" string)) (function | Context_garbage_collection {context_hash; gc_lockfile_path} -> Some (context_hash, gc_lockfile_path) | _ -> None) (fun (context_hash, gc_lockfile_path) -> Context_garbage_collection {context_hash; gc_lockfile_path}) let case_context_split tag = let open Data_encoding in case tag ~title:"context_split" unit (function Context_split -> Some () | _ -> None) (fun () -> Context_split) let request_encoding = let open Data_encoding in union [ case (Tag 0) ~title:"init" empty (function Init -> Some () | _ -> None) (fun () -> Init); case_validate (Tag 1); case (Tag 2) ~title:"commit_genesis" (obj1 (req "chain_id" Chain_id.encoding)) (function Commit_genesis {chain_id} -> Some chain_id | _ -> None) (fun chain_id -> Commit_genesis {chain_id}); case (Tag 3) ~title:"fork_test_chain" (obj3 (req "chain_id" Chain_id.encoding) (req "context_hash" Context_hash.encoding) (req "forked_header" Block_header.encoding)) (function | Fork_test_chain {chain_id; context_hash; forked_header} -> Some (chain_id, context_hash, forked_header) | _ -> None) (fun (chain_id, context_hash, forked_header) -> Fork_test_chain {chain_id; context_hash; forked_header}); case (Tag 4) ~title:"terminate" unit (function Terminate -> Some () | _ -> None) (fun () -> Terminate); (* Tag 5 was ["restore_integrity"]. *) case (Tag 6) ~title:"reconfigure_event_logging" Tezos_base_unix.Internal_event_unix.Configuration.encoding (function Reconfigure_event_logging c -> Some c | _ -> None) (fun c -> Reconfigure_event_logging c); case_preapply (Tag 7); case_precheck (Tag 8); case_context_gc (Tag 9); case_context_split (Tag 10); ] let send pin encoding data = let open Lwt_syntax in let msg = Data_encoding.Binary.to_bytes_exn encoding data in let* () = Lwt_io.write_int pin (Bytes.length msg) in let* () = Lwt_io.write pin (Bytes.to_string msg) in Lwt_io.flush pin let recv_result pout encoding = let open Lwt_syntax in let* count = Lwt_io.read_int pout in let buf = Bytes.create count in let* () = Lwt_io.read_into_exactly pout buf 0 count in return (Data_encoding.Binary.of_bytes_exn (Error_monad.result_encoding encoding) buf) let recv pout encoding = let open Lwt_syntax in let* count = Lwt_io.read_int pout in let buf = Bytes.create count in let* () = Lwt_io.read_into_exactly pout buf 0 count in Lwt.return (Data_encoding.Binary.of_bytes_exn encoding buf) let socket_path_prefix = "tezos-validation-socket-" let socket_path ~socket_dir ~pid = let filename = Format.sprintf "%s%d" socket_path_prefix pid in Filename.concat socket_dir filename (* To get optimized socket communication of processes on the same machine, we use Unix domain sockets: ADDR_UNIX. *) let make_socket socket_path = Unix.ADDR_UNIX socket_path let create_socket ~canceler = let open Lwt_syntax in let socket = Lwt_unix.socket PF_UNIX SOCK_STREAM 0o000 in Lwt_unix.set_close_on_exec socket ; Lwt_canceler.on_cancel canceler (fun () -> let* (_ : unit tzresult) = Lwt_utils_unix.safe_close socket in return_unit) ; Lwt_unix.setsockopt socket SO_REUSEADDR true ; Lwt.return socket let create_socket_listen ~canceler ~max_requests ~socket_path = let open Lwt_result_syntax in let*! socket = create_socket ~canceler in let* () = Lwt.catch (fun () -> let*! () = Lwt_unix.bind socket (make_socket socket_path) in return_unit) (function | Unix.Unix_error (ENAMETOOLONG, _, _) -> (* Unix.ENAMETOOLONG (Filename too long (POSIX.1-2001)) can be thrown if the given directory has a too long path. *) tzfail Block_validator_errors.( Validation_process_failed (Socket_path_too_long socket_path)) | Unix.Unix_error (EACCES, _, _) -> (* Unix.EACCES (Permission denied (POSIX.1-2001)) can be thrown when the given directory has wrong access rights. Unix.EPERM (Operation not permitted (POSIX.1-2001)) should not be thrown in this case. *) tzfail Block_validator_errors.( Validation_process_failed (Socket_path_wrong_permission socket_path)) | exn -> tzfail (Block_validator_errors.Validation_process_failed (Cannot_run_external_validator (Printexc.to_string exn)))) in Lwt_unix.listen socket max_requests ; return socket let create_socket_connect ~canceler ~socket_path = let open Lwt_syntax in let* socket = create_socket ~canceler in let* () = Lwt_unix.connect socket (make_socket socket_path) in Lwt.return socket