diff --git a/src/bin_validation/validator.ml b/src/bin_validation/validator.ml index ecf996fd66f3a291b49220fbd7a3adc1e68ec4d9..9227d0a7efd231c172e88a83f27244ea0ba078ff 100644 --- a/src/bin_validation/validator.ml +++ b/src/bin_validation/validator.ml @@ -439,7 +439,7 @@ let run input output = External_validation.send output (Error_monad.result_encoding Data_encoding.empty) - (error + (Tzresult_syntax.fail (Block_validator_errors.Failed_to_checkout_context context_hash)) in diff --git a/src/lib_base/p2p_rejection.ml b/src/lib_base/p2p_rejection.ml index 46e30c4d76e457e6599786d38a0b3e6334014ca1..76b8692d63fd1b58fb83b4c45bc2878c0f76a998 100644 --- a/src/lib_base/p2p_rejection.ml +++ b/src/lib_base/p2p_rejection.ml @@ -97,4 +97,4 @@ let () = (function Rejecting {motive} -> Some motive | _ -> None) (fun motive -> Rejecting {motive}) -let rejecting motive = error (Rejecting {motive}) +let rejecting motive = Tzresult_syntax.fail (Rejecting {motive}) diff --git a/src/lib_client_base/client_keys.ml b/src/lib_client_base/client_keys.ml index f6a65209e95d68b63bfe13a661c5a5fd0306f7b4..d6d033c62b85353b7c8adc5648b92b0fff101ed6 100644 --- a/src/lib_client_base/client_keys.ml +++ b/src/lib_client_base/client_keys.ml @@ -87,10 +87,11 @@ module Pk_uri_hashtbl = Hashtbl.Make (struct end) let make_pk_uri (x : Uri.t) : pk_uri tzresult = + let open Tzresult_syntax in match Uri.scheme x with | None -> - error (Exn (Failure "Error while parsing URI: PK_URI needs a scheme")) - | Some _ -> ok x + fail (Exn (Failure "Error while parsing URI: PK_URI needs a scheme")) + | Some _ -> return x type sk_uri = Uri.t @@ -101,25 +102,28 @@ module CompareUri = Compare.Make (struct end) let make_sk_uri (x : Uri.t) : sk_uri tzresult = + let open Tzresult_syntax in match Uri.scheme x with | None -> - error (Exn (Failure "Error while parsing URI: SK_URI needs a scheme")) - | Some _ -> ok x + fail (Exn (Failure "Error while parsing URI: SK_URI needs a scheme")) + | Some _ -> return x type sapling_uri = Uri.t let make_sapling_uri (x : Uri.t) : sapling_uri tzresult = + let open Tzresult_syntax in match Uri.scheme x with - | None -> error (Exn (Failure "SAPLING_URI needs a scheme")) - | Some _ -> ok x + | None -> fail (Exn (Failure "SAPLING_URI needs a scheme")) + | Some _ -> return x type pvss_sk_uri = Uri.t let make_pvss_sk_uri (x : Uri.t) : pvss_sk_uri tzresult = + let open Tzresult_syntax in match Uri.scheme x with | None -> - error (Exn (Failure "Error while parsing URI: PVSS_URI needs a scheme")) - | Some _ -> ok x + fail (Exn (Failure "Error while parsing URI: PVSS_URI needs a scheme")) + | Some _ -> return x let pk_uri_parameter () = Clic.parameter (fun _ s -> Lwt.return @@ make_pk_uri (Uri.of_string s)) diff --git a/src/lib_crypto/helpers.ml b/src/lib_crypto/helpers.ml index 1a6bdf0f184a8c044e2390d56a2855306d138929..b85712639c92dd09c3799ab8efc77107a739ffb5 100644 --- a/src/lib_crypto/helpers.ml +++ b/src/lib_crypto/helpers.ml @@ -69,7 +69,7 @@ struct let of_hex s = match of_hex_opt s with | None -> error_with "of_hex (%s)" H.name - | Some pk -> ok pk + | Some pk -> Ok pk end module MakeB58 (H : sig diff --git a/src/lib_micheline/micheline_parser.ml b/src/lib_micheline/micheline_parser.ml index 1416d4e529128d76086117454baa71f5c04031b7..d10059ecad6d480174c006bc163dcb21c83bdbbb 100644 --- a/src/lib_micheline/micheline_parser.ml +++ b/src/lib_micheline/micheline_parser.ml @@ -758,7 +758,7 @@ let print_location ppf loc = loc.stop.column let no_parsing_error (ast, errors) = - match errors with [] -> ok ast | errors -> Error errors + match errors with [] -> Ok ast | errors -> Error errors let () = register_error_kind diff --git a/src/lib_p2p/p2p_buffer_reader.ml b/src/lib_p2p/p2p_buffer_reader.ml index 43f529e8ae3ef29bb93f53a596f8fc21ae400a9c..cb5862743c91fc3b118e5c4859cad977e130d0f2 100644 --- a/src/lib_p2p/p2p_buffer_reader.ml +++ b/src/lib_p2p/p2p_buffer_reader.ml @@ -131,7 +131,7 @@ let read_from readable {pos = offset; length_to_copy; buf} data = ~into:buf ~offset) ; Ok read_len - | Error _ -> error P2p_errors.Connection_closed + | Error _ -> Tzresult_syntax.fail P2p_errors.Connection_closed let read ?canceler readable buffer = let open Lwt_syntax in diff --git a/src/lib_p2p/p2p_socket.ml b/src/lib_p2p/p2p_socket.ml index c787a3497b8270e4161f1ecfaba4e06d92d6a97b..8263f1944e891df48fa0b55601519ab3ad511f56 100644 --- a/src/lib_p2p/p2p_socket.ml +++ b/src/lib_p2p/p2p_socket.ml @@ -560,8 +560,10 @@ module Writer = struct let encode_message st msg = match Data_encoding.Binary.to_bytes st.encoding msg with - | Error we -> error (Tezos_base.Data_encoding_wrapper.Encoding_error we) - | Ok bytes -> ok (Utils.cut st.binary_chunks_size bytes) + | Error we -> + Tzresult_syntax.fail + (Tezos_base.Data_encoding_wrapper.Encoding_error we) + | Ok bytes -> Ok (Utils.cut st.binary_chunks_size bytes) let rec worker_loop st = let open Lwt_syntax in @@ -584,7 +586,10 @@ module Writer = struct worker_loop st | Error err -> ( Option.iter - (fun u -> Lwt.wakeup_later u (error P2p_errors.Connection_closed)) + (fun u -> + Lwt.wakeup_later + u + (Tzresult_syntax.fail P2p_errors.Connection_closed)) wakener ; match err with | (Canceled | Exn Lwt_pipe.Closed) :: _ -> @@ -791,7 +796,8 @@ let read {reader; _} = let read_now {reader; _} = try Lwt_pipe.Maybe_bounded.pop_now reader.messages - with Lwt_pipe.Closed -> Some (error P2p_errors.Connection_closed) + with Lwt_pipe.Closed -> + Some (Tzresult_syntax.fail P2p_errors.Connection_closed) let stat {conn = {scheduled_conn; _}; _} = P2p_io_scheduler.stat scheduled_conn diff --git a/src/lib_requester/requester.ml b/src/lib_requester/requester.ml index 498b5a21f71aa35b77bb860dacfcba1d7a5c821d..4720338ba3dddb2dffab8830fe86873d1594b67f 100644 --- a/src/lib_requester/requester.ml +++ b/src/lib_requester/requester.ml @@ -532,7 +532,7 @@ module Make if data.waiters = 0 then ( Memory_table.remove s.memory k ; Scheduler.notify_cancellation s.scheduler k ; - Lwt.wakeup_later w (error (Canceled k)))) ; + Lwt.wakeup_later w (Tzresult_syntax.fail (Canceled k)))) ; match timeout with | None -> t | Some delay -> @@ -631,7 +631,7 @@ module Make | Some (Pending {wakener = w; _}) -> Scheduler.notify_cancellation s.scheduler k ; Memory_table.remove s.memory k ; - Lwt.wakeup_later w (error (Canceled k)) + Lwt.wakeup_later w (Tzresult_syntax.fail (Canceled k)) | Some (Found _) -> Memory_table.remove s.memory k let watch s = Lwt_watcher.create_stream s.input diff --git a/src/lib_shell/prevalidation.ml b/src/lib_shell/prevalidation.ml index 8f263bd51003f8f7dac63837455a0081fee2c0b0..a45177eaa1ff8edab44f2c5793b66d4384e375aa 100644 --- a/src/lib_shell/prevalidation.ml +++ b/src/lib_shell/prevalidation.ml @@ -104,9 +104,10 @@ end (** Doesn't depend on heavy [Registered_protocol.T] for testability. *) let safe_binary_of_bytes (encoding : 'a Data_encoding.t) (bytes : bytes) : 'a tzresult = + let open Tzresult_syntax in match Data_encoding.Binary.of_bytes_opt encoding bytes with - | None -> error Parse_error - | Some protocol_data -> ok protocol_data + | None -> fail Parse_error + | Some protocol_data -> return protocol_data module MakeAbstract (Chain_store : CHAIN_STORE) diff --git a/src/lib_shell/prevalidator.ml b/src/lib_shell/prevalidator.ml index b7dbf8f29b3f8137632185e6785a086b0c77aaa6..024a4d932636617ccbcd315ee8f3b89d335b6585 100644 --- a/src/lib_shell/prevalidator.ml +++ b/src/lib_shell/prevalidator.ml @@ -812,7 +812,7 @@ module Make_s op in List.iter (handle_classification ~notifier shell) to_handle ; - ok (new_filter_state, new_validation_state, new_mempool, limit - 1))) + Ok (new_filter_state, new_validation_state, new_mempool, limit - 1))) shell.pending ( filter_state, state, @@ -949,11 +949,11 @@ module Make_s else match Prevalidation_t.parse oph op with | Error _ -> - let+ () = Event.(emit unparsable_operation) oph in + let* () = Event.(emit unparsable_operation) oph in Prevalidator_classification.add_unparsable oph pv.shell.classification ; - ok () + return_ok_unit | Ok parsed_op -> ( let* v = pre_filter diff --git a/src/lib_shell/protocol_validator.ml b/src/lib_shell/protocol_validator.ml index 9f88383be074c2715e3d9e7d45f0a7f598af1387..c0785f37b4c8e5b424db3742f5200ac73ed90d29 100644 --- a/src/lib_shell/protocol_validator.ml +++ b/src/lib_shell/protocol_validator.ml @@ -58,14 +58,16 @@ let rec worker_loop bv = | None -> Lwt.wakeup_later wakener - (error (Invalid_protocol {hash; error = Dynlinking_failed}))) ; + (Tzresult_syntax.fail + (Invalid_protocol {hash; error = Dynlinking_failed}))) ; return_unit) else ( (* no need to tag 'invalid' protocol on disk, the economic protocol prevents us from being spammed with protocol validation. *) Lwt.wakeup_later wakener - (error (Invalid_protocol {hash; error = Compilation_failed})) ; + (Tzresult_syntax.fail + (Invalid_protocol {hash; error = Compilation_failed})) ; return_unit) in match r with diff --git a/src/lib_shell/test/test_prevalidation.ml b/src/lib_shell/test/test_prevalidation.ml index c3777afa8752b87fab27b7192e4924c2893c2283..204738d09e0cdd831838a78e0db952e9d59adc5e 100644 --- a/src/lib_shell/test/test_prevalidation.ml +++ b/src/lib_shell/test/test_prevalidation.ml @@ -47,7 +47,7 @@ let test_safe_decode () = check bool "A broken encoding should return None" - (actual = error Validation_errors.Parse_error) + (actual = Tzresult_syntax.fail Validation_errors.Parse_error) true) open Tezos_requester diff --git a/src/lib_shell/test/test_validator.ml b/src/lib_shell/test/test_validator.ml index 7937ec06dfdd95482d6c5c356efe8ec45933931c..a7ce3755966e67bcc296a1b496125a0d3d691cf4 100644 --- a/src/lib_shell/test/test_validator.ml +++ b/src/lib_shell/test/test_validator.ml @@ -72,7 +72,7 @@ let init_validator Node.default_chain_validator_limits ~start_testchain:false in - Lwt.return (ok (block_validator, validator, Store.main_chain_store store)) + Lwt.return_ok (block_validator, validator, Store.main_chain_store store) in match r with | Ok (block_validator, validator, chain) -> diff --git a/src/lib_shell/validator.ml b/src/lib_shell/validator.ml index cc3231f912f649346cbcebc364eed6368c8b2efe..424166b2c3e732875878f4b9608d4caa369d2a79 100644 --- a/src/lib_shell/validator.ml +++ b/src/lib_shell/validator.ml @@ -88,9 +88,10 @@ let activate v ~start_prevalidator ~validator_process chain_store = v.chain_validator_limits let get {active_chains; _} chain_id = + let open Tzresult_syntax in match Chain_id.Table.find active_chains chain_id with - | Some nv -> Ok nv - | None -> error (Validation_errors.Inactive_chain chain_id) + | Some nv -> return nv + | None -> fail (Validation_errors.Inactive_chain chain_id) let get_active_chains {active_chains; _} = let l = Chain_id.Table.fold (fun c _ acc -> c :: acc) active_chains [] in diff --git a/src/lib_validation/block_validation.ml b/src/lib_validation/block_validation.ml index dc0962c4b0bea1c953bc2d4dc3bdfa612f9ceb0e..15ad14d60e995a8d918869b287c76b1fdddd5867 100644 --- a/src/lib_validation/block_validation.ml +++ b/src/lib_validation/block_validation.ml @@ -94,9 +94,10 @@ type result = { type apply_result = {result : result; cache : Environment_context.Context.cache} let check_proto_environment_version_increasing block_hash before after = - if Protocol.compare_version before after <= 0 then Result.return_unit + let open Tzresult_syntax in + if Protocol.compare_version before after <= 0 then return_unit else - error + fail (invalid_block block_hash (Invalid_protocol_environment_transition (before, after))) @@ -656,9 +657,10 @@ module Make (Proto : Registered_protocol.T) = struct (** Doesn't depend on heavy [Registered_protocol.T] for testability. *) let safe_binary_of_bytes (encoding : 'a Data_encoding.t) (bytes : bytes) : 'a tzresult = + let open Tzresult_syntax in match Data_encoding.Binary.of_bytes_opt encoding bytes with - | None -> error Parse_error - | Some protocol_data -> ok protocol_data + | None -> fail Parse_error + | Some protocol_data -> return protocol_data let parse_unsafe (proto : bytes) : Proto.operation_data tzresult = safe_binary_of_bytes Proto.operation_data_encoding proto @@ -1021,9 +1023,10 @@ module Make (Proto : Registered_protocol.T) = struct end let assert_no_duplicate_operations block_hash live_operations operations = + let open Tzresult_syntax in let exception Duplicate of block_error in try - ok + return (List.fold_left (List.fold_left (fun live_operations op -> let oph = Operation.hash op in @@ -1032,12 +1035,13 @@ let assert_no_duplicate_operations block_hash live_operations operations = else Operation_hash.Set.add oph live_operations)) live_operations operations) - with Duplicate err -> error (invalid_block block_hash err) + with Duplicate err -> fail (invalid_block block_hash err) let assert_operation_liveness block_hash live_blocks operations = + let open Tzresult_syntax in let exception Outdated of block_error in try - ok + return (List.iter (List.iter (fun op -> if not (Block_hash.Set.mem op.Operation.shell.branch live_blocks) @@ -1051,7 +1055,7 @@ let assert_operation_liveness block_hash live_blocks operations = in raise (Outdated error))) operations) - with Outdated err -> error (invalid_block block_hash err) + with Outdated err -> fail (invalid_block block_hash err) (* Maybe this function should be moved somewhere else since it used once by [Block_validator_process] *) diff --git a/src/lib_workers/worker.ml b/src/lib_workers/worker.ml index f9461699ae95689bce0a58732aa1aa8de5d7af5d..7e535a64f64dca80f1c5d7cc99631dd787243535 100644 --- a/src/lib_workers/worker.ml +++ b/src/lib_workers/worker.ml @@ -426,7 +426,9 @@ struct let wakeup = function | (_, Message (_, Some u)) -> let name = Format.asprintf "%a" Name.pp w.name in - Lwt.wakeup_later u (error (Closed {base = base_name; name})) + Lwt.wakeup_later + u + (Tzresult_syntax.fail (Closed {base = base_name; name})) | (_, Message (_, None)) -> () in let close_queue message_queue =