(*****************************************************************************) (* *) (* Open Source License *) (* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. *) (* *) (* 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 Block_validator_worker_state open Block_validator_errors let invalid_block block error = Invalid_block { block ; error } type limits = { protocol_timeout: float ; worker_limits : Worker_types.limits ; } module Name = struct type t = unit let encoding = Data_encoding.empty let base = [ "validator.block" ] let pp _ () = () end module Types = struct include Worker_state type state = { protocol_validator: Protocol_validator.t ; validation_process: Validator_process.t ; limits : limits ; } type parameters = limits * Distributed_db.t * Validator_process.t let view _state _parameters = () end module Request = struct include Request type 'a t = | Request_validation : { chain_db: Distributed_db.chain_db ; notify_new_block: State.Block.t -> unit ; canceler: Lwt_canceler.t option ; peer: P2p_peer.Id.t option ; hash: Block_hash.t ; header: Block_header.t ; operations: Operation.t list list ; } -> State.Block.t option tzresult t let view : type a. a t -> view = fun (Request_validation { chain_db ; peer ; hash }) -> let chain_id = chain_db |> Distributed_db.chain_state |> State.Chain.id in { chain_id ; block = hash ; peer = peer } end module Worker = Worker.Make (Name) (Event) (Request) (Types) type t = Worker.infinite Worker.queue Worker.t type error += Closed = Worker.Closed let debug w = Format.kasprintf (fun msg -> Worker.record_event w (Debug msg)) let check_header (pred: State.Block.t) validation_passes hash (header: Block_header.t) = let pred_header = State.Block.header pred in fail_unless (Int32.succ pred_header.shell.level = header.shell.level) (invalid_block hash @@ Invalid_level { expected = Int32.succ pred_header.shell.level ; found = header.shell.level }) >>=? fun () -> fail_unless Time.(pred_header.shell.timestamp < header.shell.timestamp) (invalid_block hash Non_increasing_timestamp) >>=? fun () -> fail_unless Fitness.(pred_header.shell.fitness < header.shell.fitness) (invalid_block hash Non_increasing_fitness) >>=? fun () -> fail_unless (header.shell.validation_passes = validation_passes) (invalid_block hash (Unexpected_number_of_validation_passes header.shell.validation_passes) ) >>=? fun () -> return_unit let assert_no_duplicate_operations block live_operations operation_hashes = fold_left_s (fold_left_s (fun live_operations oph -> fail_when (Operation_hash.Set.mem oph live_operations) (invalid_block block @@ Replayed_operation oph) >>=? fun () -> return (Operation_hash.Set.add oph live_operations))) live_operations operation_hashes >>=? fun _ -> return_unit let assert_operation_liveness block live_blocks operations = iter_s (iter_s (fun op -> fail_unless (Block_hash.Set.mem op.Operation.shell.branch live_blocks) (invalid_block block @@ Outdated_operation { operation = Operation.hash op ; originating_block = op.shell.branch }))) operations let check_liveness chain_state pred hash operations_hashes operations = begin Chain.data chain_state >>= fun chain_data -> if State.Block.equal chain_data.current_head pred then Lwt.return (chain_data.live_blocks, chain_data.live_operations) else Chain_traversal.live_blocks pred (State.Block.max_operations_ttl pred) end >>= fun (live_blocks, live_operations) -> assert_no_duplicate_operations hash live_operations operations_hashes >>=? fun () -> assert_operation_liveness hash live_blocks operations >>=? fun () -> return_unit let may_patch_protocol ~level (validation_result : Tezos_protocol_environment_shell.validation_result) = match Block_header.get_forced_protocol_upgrade ~level with | None -> return validation_result | Some hash -> Context.set_protocol validation_result.context hash >>= fun context -> return { validation_result with context } let apply_block chain_state validation_process pred (module Proto : Registered_protocol.T) hash (header: Block_header.t) operations = check_header pred (List.length Proto.validation_passes) hash header >>=? fun () -> iteri2_p (fun i ops quota -> fail_unless (Option.unopt_map ~default:true ~f:(fun max -> List.length ops <= max) quota.Tezos_protocol_environment_shell.max_op) (let max = Option.unopt ~default:~-1 quota.max_op in invalid_block hash @@ Too_many_operations { pass = i + 1 ; found = List.length ops ; max }) >>=? fun () -> iter_p (fun op -> let size = Data_encoding.Binary.length Operation.encoding op in fail_unless (size <= Proto.max_operation_data_length) (invalid_block hash @@ Oversized_operation { operation = Operation.hash op ; size ; max = Proto.max_operation_data_length })) ops >>=? fun () -> return_unit) operations Proto.validation_passes >>=? fun () -> let operation_hashes = List.map (List.map Operation.hash) operations in check_liveness chain_state pred hash operation_hashes operations >>=? fun () -> begin match Data_encoding.Binary.of_bytes Proto.block_header_data_encoding header.protocol_data with | None -> fail (invalid_block hash Cannot_parse_block_header) | Some protocol_data -> return ({ shell = header.shell ; protocol_data } : Proto.block_header) end >>=? fun _header -> Validator_process.apply_block validation_process header operations chain_state >>=? fun { validation_result ; block_data ; ops_metadata ; context_hash } -> let validation_store = ({ context_hash ; message = validation_result.message ; max_operations_ttl = validation_result.max_operations_ttl ; last_allowed_fork_level = validation_result.last_allowed_fork_level} : State.Block.validation_store) in return (validation_store, block_data, ops_metadata) let check_chain_liveness chain_db hash (header: Block_header.t) = let chain_state = Distributed_db.chain_state chain_db in match State.Chain.expiration chain_state with | Some eol when Time.(eol <= header.shell.timestamp) -> fail @@ invalid_block hash @@ Expired_chain { chain_id = State.Chain.id chain_state ; expiration = eol ; timestamp = header.shell.timestamp } | None | Some _ -> return_unit let get_proto pred hash = State.Block.context pred >>= fun pred_context -> Context.get_protocol pred_context >>= fun pred_protocol_hash -> match Registered_protocol.get pred_protocol_hash with | None -> fail (Unavailable_protocol { block = hash ; protocol = pred_protocol_hash }) | Some p -> return p let on_request : type r. t -> r Request.t -> r tzresult Lwt.t = fun w (Request.Request_validation { chain_db ; notify_new_block ; canceler ; peer ; hash ; header ; operations }) -> let bv = Worker.state w in let chain_state = Distributed_db.chain_state chain_db in State.Block.read_opt chain_state hash >>= function | Some block -> debug w "previously validated block %a (after pipe)" Block_hash.pp_short hash ; Protocol_validator.prefetch_and_compile_protocols bv.protocol_validator ?peer ~timeout:bv.limits.protocol_timeout block ; return (Ok None) | None -> State.Block.read_invalid chain_state hash >>= function | Some { errors } -> return (Error errors) | None -> begin debug w "validating block %a" Block_hash.pp_short hash ; State.Block.read chain_state header.shell.predecessor >>=? fun pred -> get_proto pred hash >>=? fun proto -> (* TODO also protect with [Worker.canceler w]. *) protect ?canceler begin fun () -> apply_block (Distributed_db.chain_state chain_db) bv.validation_process pred proto hash header operations >>=? fun (result, header_data, operations_data) -> Distributed_db.commit_block chain_db hash header header_data operations operations_data result >>=? function | None -> assert false (* should not happen *) | Some block -> return block end end >>= function | Ok block -> Protocol_validator.prefetch_and_compile_protocols bv.protocol_validator ?peer ~timeout:bv.limits.protocol_timeout block ; notify_new_block block ; return (Ok (Some block)) (* TODO catch other temporary error (e.g. system errors) and do not 'commit' them on disk... *) | Error [Canceled | Unavailable_protocol _] as err -> (* FIXME: Canceled can escape. Canceled is not registered. BOOM! *) return err | Error errors -> Worker.protect w begin fun () -> Distributed_db.commit_invalid_block chain_db hash header errors end >>=? fun commited -> assert commited ; return (Error errors) let on_launch _ _ (limits, db, validation_process) = let protocol_validator = Protocol_validator.create db in Lwt.return { Types.protocol_validator ; validation_process ; limits } let on_error w r st errs = Worker.record_event w (Validation_failure (r, st, errs)) ; Lwt.return (Error errs) let on_completion : type a. t -> a Request.t -> a -> Worker_types.request_status -> unit Lwt.t = fun w (Request.Request_validation _ as r) v st -> match v with | Ok (Some _) -> Worker.record_event w (Event.Validation_success (Request.view r, st)) ; Lwt.return_unit | Ok None -> Lwt.return_unit | Error errs -> Worker.record_event w (Event.Validation_failure (Request.view r, st, errs)) ; Lwt.return_unit let on_close w = let bv = Worker.state w in Validator_process.close bv.validation_process let table = Worker.create_table Queue let create limits db validation_process_kind = let module Handlers = struct type self = t let on_launch = on_launch let on_request = on_request let on_close = on_close let on_error = on_error let on_completion = on_completion let on_no_request _ = return_unit end in Worker.launch table limits.worker_limits () (limits, db, validation_process_kind) (module Handlers) let shutdown = Worker.shutdown let validate w ?canceler ?peer ?(notify_new_block = fun _ -> ()) chain_db hash (header : Block_header.t) operations = let bv = Worker.state w in let chain_state = Distributed_db.chain_state chain_db in State.Block.read_opt chain_state hash >>= function | Some block -> debug w "previously validated block %a (before pipe)" Block_hash.pp_short hash ; Protocol_validator.prefetch_and_compile_protocols bv.protocol_validator ?peer ~timeout:bv.limits.protocol_timeout block ; return_none | None -> map_p (map_p (fun op -> let op_hash = Operation.hash op in return op_hash)) operations >>=? fun hashes -> let computed_hash = Operation_list_list_hash.compute (List.map Operation_list_hash.compute hashes) in fail_when (Operation_list_list_hash.compare computed_hash header.shell.operations_hash <> 0) (Inconsistent_operations_hash { block = hash ; expected = header.shell.operations_hash ; found = computed_hash ; }) >>=? fun () -> check_chain_liveness chain_db hash header >>=? fun () -> Worker.push_request_and_wait w (Request_validation { chain_db ; notify_new_block ; canceler ; peer ; hash ; header ; operations }) >>=? fun result -> Lwt.return result let fetch_and_compile_protocol w = let bv = Worker.state w in Protocol_validator.fetch_and_compile_protocol bv.protocol_validator let status = Worker.status let running_worker () = match Worker.list table with | (_, single) :: _ -> single | [] -> raise Not_found let pending_requests t = Worker.pending_requests t let current_request t = Worker.current_request t let last_events = Worker.last_events