From 5023e1a261340f45aa29cde7551650067f26114c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Henry?= Date: Sat, 12 May 2018 19:54:57 +0200 Subject: [PATCH] Data_encoding: rename `Binary.to_bytes` into `to_bytes_exn` --- src/bin_node/node_run_command.ml | 2 +- src/lib_base/block_header.ml | 2 +- src/lib_base/fitness.ml | 2 +- src/lib_base/operation.ml | 2 +- src/lib_base/protocol.ml | 2 +- .../client_signer_encrypted.ml | 2 +- src/lib_crypto/signature.ml | 2 +- src/lib_data_encoding/binary_writer.ml | 6 +++++- src/lib_data_encoding/binary_writer.mli | 3 ++- src/lib_data_encoding/data_encoding.mli | 13 ++----------- src/lib_data_encoding/encoding.ml | 18 +++++------------- src/lib_data_encoding/encoding.mli | 12 ------------ src/lib_data_encoding/test/read_failure.ml | 2 +- src/lib_data_encoding/test/success.ml | 4 ++-- src/lib_data_encoding/test/write_failure.ml | 2 +- src/lib_p2p/p2p_socket.ml | 5 +++-- .../sigs/v1/data_encoding.mli | 12 +++++------- src/lib_rpc_http/media_type.ml | 2 +- src/lib_shell/node.ml | 2 +- src/lib_shell/node_rpc.ml | 2 +- src/lib_shell/test/test_locator.ml | 4 ++-- src/lib_shell/test/test_state.ml | 4 ++-- src/lib_storage/context.ml | 2 +- src/lib_storage/store_helpers.ml | 7 ++++--- .../lib_baking/test/proto_alpha_helpers.ml | 4 ++-- .../lib_protocol/src/block_header_repr.ml | 6 +++--- .../lib_protocol/src/contract_repr.ml | 2 +- .../lib_protocol/src/operation_repr.ml | 4 ++-- .../lib_protocol/src/raw_context.ml | 4 ++-- .../lib_protocol/src/script_ir_translator.ml | 4 ++-- .../lib_protocol/src/storage_functors.ml | 5 +++-- .../lib_protocol/test/helpers/helpers_init.ml | 6 +++--- .../test/helpers/helpers_operation.ml | 2 +- .../lib_client/client_proto_main.ml | 6 +++--- src/proto_genesis/lib_protocol/src/data.ml | 4 ++-- 35 files changed, 69 insertions(+), 92 deletions(-) diff --git a/src/bin_node/node_run_command.ml b/src/bin_node/node_run_command.ml index 791e1f1f7..855806b46 100644 --- a/src/bin_node/node_run_command.ml +++ b/src/bin_node/node_run_command.ml @@ -98,7 +98,7 @@ let init_node ?sandbox (config : Node_config_file.t) = | Some json -> Tezos_storage.Context.set ctxt ["sandbox_parameter"] - (Data_encoding.Binary.to_bytes Data_encoding.json json) + (Data_encoding.Binary.to_bytes_exn Data_encoding.json json) end >>= fun ctxt -> let module Proto = (val Registered_protocol.get_exn genesis.protocol) in Proto.init ctxt { diff --git a/src/lib_base/block_header.ml b/src/lib_base/block_header.ml index d021e83d4..4de8e87ff 100644 --- a/src/lib_base/block_header.ml +++ b/src/lib_base/block_header.ml @@ -80,7 +80,7 @@ let pp ppf op = Data_encoding.Json.pp ppf (Data_encoding.Json.construct encoding op) -let to_bytes v = Data_encoding.Binary.to_bytes encoding v +let to_bytes v = Data_encoding.Binary.to_bytes_exn encoding v let of_bytes b = Data_encoding.Binary.of_bytes encoding b let of_bytes_exn b = Data_encoding.Binary.of_bytes_exn encoding b diff --git a/src/lib_base/fitness.ml b/src/lib_base/fitness.ml index 40cee4d71..f47174620 100644 --- a/src/lib_base/fitness.ml +++ b/src/lib_base/fitness.ml @@ -55,5 +55,5 @@ let encoding = describe ~title: "Tezos block fitness" (list bytes) -let to_bytes v = Data_encoding.Binary.to_bytes encoding v +let to_bytes v = Data_encoding.Binary.to_bytes_exn encoding v let of_bytes b = Data_encoding.Binary.of_bytes encoding b diff --git a/src/lib_base/operation.ml b/src/lib_base/operation.ml index bd7ebc34f..15ddeddac 100644 --- a/src/lib_base/operation.ml +++ b/src/lib_base/operation.ml @@ -44,7 +44,7 @@ let pp fmt op = Data_encoding.Json.pp fmt (Data_encoding.Json.construct encoding op) -let to_bytes v = Data_encoding.Binary.to_bytes encoding v +let to_bytes v = Data_encoding.Binary.to_bytes_exn encoding v let of_bytes b = Data_encoding.Binary.of_bytes encoding b let of_bytes_exn b = Data_encoding.Binary.of_bytes_exn encoding b diff --git a/src/lib_base/protocol.ml b/src/lib_base/protocol.ml index 879b43bbd..602dc81bc 100644 --- a/src/lib_base/protocol.ml +++ b/src/lib_base/protocol.ml @@ -79,7 +79,7 @@ let pp_ocaml ppf { expected_env ; components } = pp_ocaml_component) components -let to_bytes v = Data_encoding.Binary.to_bytes encoding v +let to_bytes v = Data_encoding.Binary.to_bytes_exn encoding v let of_bytes b = Data_encoding.Binary.of_bytes encoding b let of_bytes_exn b = Data_encoding.Binary.of_bytes_exn encoding b let hash proto = Protocol_hash.hash_bytes [to_bytes proto] diff --git a/src/lib_client_base_unix/client_signer_encrypted.ml b/src/lib_client_base_unix/client_signer_encrypted.ml index db98a0db2..219b1d170 100644 --- a/src/lib_client_base_unix/client_signer_encrypted.ml +++ b/src/lib_client_base_unix/client_signer_encrypted.ml @@ -112,7 +112,7 @@ module Encrypted_signer : SIGNER = struct let password = MBytes.of_string password in let salt = Rand.generate salt_len in let key = Crypto_box.Secretbox.unsafe_of_bytes (pbkdf ~password ~salt) in - let msg = Data_encoding.Binary.to_bytes Signature.Secret_key.encoding sk in + let msg = Data_encoding.Binary.to_bytes_exn Signature.Secret_key.encoding sk in let encrypted_passwd = Crypto_box.Secretbox.box key msg nonce in let payload = MBytes.(to_string (concat "" [salt; encrypted_passwd])) in let location = Base58.safe_encode payload in diff --git a/src/lib_crypto/signature.ml b/src/lib_crypto/signature.ml index 5fbeee731..31d488204 100644 --- a/src/lib_crypto/signature.ml +++ b/src/lib_crypto/signature.ml @@ -51,7 +51,7 @@ module Public_key_hash = struct ] let to_bytes s = - Data_encoding.Binary.to_bytes raw_encoding s + Data_encoding.Binary.to_bytes_exn raw_encoding s let of_bytes_opt s = Data_encoding.Binary.of_bytes raw_encoding s let to_string s = MBytes.to_string (to_bytes s) diff --git a/src/lib_data_encoding/binary_writer.ml b/src/lib_data_encoding/binary_writer.ml index f9524095a..c06835d8e 100644 --- a/src/lib_data_encoding/binary_writer.ml +++ b/src/lib_data_encoding/binary_writer.ml @@ -287,7 +287,7 @@ let write e v buffer offset len = Some state.offset with Write_error _ -> None -let to_bytes e v = +let to_bytes_exn e v = match Encoding.classify e with | `Fixed n -> begin (* Preallocate the complete buffer *) @@ -303,3 +303,7 @@ let to_bytes e v = offset = 0 ; allowed_bytes = None } in write_rec e state v ; MBytes.sub state.buffer 0 state.offset + +let to_bytes e v = + try Some (to_bytes_exn e v) + with Write_error _ -> None diff --git a/src/lib_data_encoding/binary_writer.mli b/src/lib_data_encoding/binary_writer.mli index e0a1c5723..ede6b8e7b 100644 --- a/src/lib_data_encoding/binary_writer.mli +++ b/src/lib_data_encoding/binary_writer.mli @@ -11,4 +11,5 @@ use the corresponding module intended for use: {Data_encoding.Binary}. *) val write : 'a Encoding.t -> 'a -> MBytes.t -> int -> int -> int option -val to_bytes : 'a Encoding.t -> 'a -> MBytes.t +val to_bytes_exn : 'a Encoding.t -> 'a -> MBytes.t +val to_bytes : 'a Encoding.t -> 'a -> MBytes.t option diff --git a/src/lib_data_encoding/data_encoding.mli b/src/lib_data_encoding/data_encoding.mli index a7578c531..72ea2b715 100644 --- a/src/lib_data_encoding/data_encoding.mli +++ b/src/lib_data_encoding/data_encoding.mli @@ -64,13 +64,6 @@ module Encoding: sig type 'a t type 'a encoding = 'a t - (** Exceptions that can be raised by the functions of this library. *) - exception No_case_matched - exception Unexpected_tag of int - exception Duplicated_tag of int - exception Invalid_tag of int * [ `Uint8 | `Uint16 ] - exception Unexpected_enum of string * string list - (** {3 Ground descriptors} *) (** Special value [null] in JSON, nothing in binary. *) @@ -568,8 +561,9 @@ module Binary: sig val pp_write_error : Format.formatter -> write_error -> unit exception Write_error of write_error + val to_bytes : 'a Encoding.t -> 'a -> MBytes.t option - val to_bytes : 'a Encoding.t -> 'a -> MBytes.t + val to_bytes_exn : 'a Encoding.t -> 'a -> MBytes.t end @@ -578,6 +572,3 @@ val json: json Encoding.t type json_schema = Json.schema val json_schema: json_schema Encoding.t type bson = Bson.t - -exception Float_out_of_range of float * float * float -exception Int_out_of_range of int * int * int diff --git a/src/lib_data_encoding/encoding.ml b/src/lib_data_encoding/encoding.ml index 6f11adef6..ef50854dc 100644 --- a/src/lib_data_encoding/encoding.ml +++ b/src/lib_data_encoding/encoding.ml @@ -7,17 +7,6 @@ (* *) (**************************************************************************) -exception No_case_matched -exception Unexpected_tag of int -exception Duplicated_tag of int -exception Invalid_tag of int * [ `Uint8 | `Uint16 ] -exception Unexpected_enum of string * string list -exception Invalid_size of int -exception Int_out_of_range of int * int * int -exception Float_out_of_range of float * float * float -exception Parse_error of string - - module Kind = struct type t = @@ -453,9 +442,12 @@ let check_cases tag_size cases = match tag with | Json_only -> others | Tag tag -> - if List.mem tag others then raise (Duplicated_tag tag) ; + if List.mem tag others then + Format.kasprintf invalid_arg + "The tag %d appears twice in an union." + tag ; if tag < 0 || max_tag <= tag then - raise (Invalid_tag (tag, tag_size)) ; + Format.kasprintf invalid_arg "The tag %d is invalid." tag ; tag :: others ) [] cases diff --git a/src/lib_data_encoding/encoding.mli b/src/lib_data_encoding/encoding.mli index 18e16552d..e431838b3 100644 --- a/src/lib_data_encoding/encoding.mli +++ b/src/lib_data_encoding/encoding.mli @@ -87,18 +87,6 @@ type 'a encoding = 'a t val make: ?json_encoding: 'a Json_encoding.encoding -> 'a desc -> 'a t - -exception No_case_matched -exception Unexpected_tag of int -exception Duplicated_tag of int -exception Invalid_tag of int * [ `Uint8 | `Uint16 ] -exception Unexpected_enum of string * string list -exception Parse_error of string -exception Float_out_of_range of float * float * float -exception Int_out_of_range of int * int * int -exception Invalid_size of int - - val null : unit encoding val empty : unit encoding val unit : unit encoding diff --git a/src/lib_data_encoding/test/read_failure.ml b/src/lib_data_encoding/test/read_failure.ml index f4fc2b6f2..e999abbac 100644 --- a/src/lib_data_encoding/test/read_failure.ml +++ b/src/lib_data_encoding/test/read_failure.ml @@ -84,7 +84,7 @@ let stream ?(expected = fun _ -> true) read_encoding bytes () = let all ?expected name write_encoding read_encoding value = let json_value = Json.construct write_encoding value in let bson_value = Bson.construct write_encoding value in - let bytes_value = Binary.to_bytes write_encoding value in + let bytes_value = Binary.to_bytes_exn write_encoding value in [ name ^ ".json", `Quick, json ?expected read_encoding json_value ; name ^ ".bson", `Quick, bson ?expected read_encoding bson_value ; name ^ ".bytes", `Quick, binary ?expected read_encoding bytes_value ; diff --git a/src/lib_data_encoding/test/success.ml b/src/lib_data_encoding/test/success.ml index f03960f04..23d67f1c1 100644 --- a/src/lib_data_encoding/test/success.ml +++ b/src/lib_data_encoding/test/success.ml @@ -36,14 +36,14 @@ let bson ty encoding value () = let binary ty encoding value () = no_exception begin fun () -> - let bytes = Binary.to_bytes encoding value in + let bytes = Binary.to_bytes_exn encoding value in let result = Binary.of_bytes_exn encoding bytes in Alcotest.check ty "binary" value result end let stream ty encoding value () = no_exception begin fun () -> - let bytes = Binary.to_bytes encoding value in + let bytes = Binary.to_bytes_exn encoding value in let len_data = MBytes.length bytes in for sz = 1 to max 1 len_data do let name = Format.asprintf "stream (%d)" sz in diff --git a/src/lib_data_encoding/test/write_failure.ml b/src/lib_data_encoding/test/write_failure.ml index 0d0b2197f..80e25481e 100644 --- a/src/lib_data_encoding/test/write_failure.ml +++ b/src/lib_data_encoding/test/write_failure.ml @@ -31,7 +31,7 @@ let bson ?(expected = fun _ -> true) encoding value () = let binary ?(expected = fun _ -> true) encoding value () = check_raises expected begin fun () -> - ignore (Binary.to_bytes encoding value : MBytes.t) ; + ignore (Binary.to_bytes_exn encoding value : MBytes.t) ; end let all name encoding value = diff --git a/src/lib_p2p/p2p_socket.ml b/src/lib_p2p/p2p_socket.ml index 542e8b9e2..1f75c4334 100644 --- a/src/lib_p2p/p2p_socket.ml +++ b/src/lib_p2p/p2p_socket.ml @@ -336,8 +336,9 @@ module Writer = struct let encode_message st msg = try ok (MBytes.cut st.binary_chunks_size - (Data_encoding.Binary.to_bytes st.encoding msg)) - with _ -> error P2p_errors.Encoding_error + (Data_encoding.Binary.to_bytes_exn st.encoding msg)) + with Data_encoding.Binary.Write_error _ -> + error P2p_errors.Encoding_error let rec worker_loop st = Lwt_unix.yield () >>= fun () -> diff --git a/src/lib_protocol_environment/sigs/v1/data_encoding.mli b/src/lib_protocol_environment/sigs/v1/data_encoding.mli index 0441b3d4b..c7fb45053 100644 --- a/src/lib_protocol_environment/sigs/v1/data_encoding.mli +++ b/src/lib_protocol_environment/sigs/v1/data_encoding.mli @@ -18,12 +18,6 @@ type json = type json_schema -exception No_case_matched -exception Unexpected_tag of int -exception Duplicated_tag of int -exception Invalid_tag of int * [ `Uint8 | `Uint16 ] -exception Unexpected_enum of string * string list - type 'a t type 'a encoding = 'a t @@ -237,8 +231,12 @@ module Binary : sig val length : 'a encoding -> 'a -> int val fixed_length : 'a encoding -> int option val read : 'a encoding -> MBytes.t -> int -> int -> (int * 'a) option - val to_bytes : 'a encoding -> 'a -> MBytes.t val write : 'a encoding -> 'a -> MBytes.t -> int -> int -> int option + val to_bytes : 'a encoding -> 'a -> MBytes.t option + val to_bytes_exn : 'a encoding -> 'a -> MBytes.t val of_bytes : 'a encoding -> MBytes.t -> 'a option + type write_error + exception Write_error of write_error + end diff --git a/src/lib_rpc_http/media_type.ml b/src/lib_rpc_http/media_type.ml index cb0bf6b7c..f850e0aa2 100644 --- a/src/lib_rpc_http/media_type.ml +++ b/src/lib_rpc_http/media_type.ml @@ -90,7 +90,7 @@ let octet_stream = { end ; construct = begin fun enc v -> MBytes.to_string @@ - Data_encoding.Binary.to_bytes enc v + Data_encoding.Binary.to_bytes_exn enc v end ; destruct = begin fun enc s -> match Data_encoding.Binary.of_bytes enc (MBytes.of_string s) with diff --git a/src/lib_shell/node.ml b/src/lib_shell/node.ml index 1a19ce319..e397446f9 100644 --- a/src/lib_shell/node.ml +++ b/src/lib_shell/node.ml @@ -22,7 +22,7 @@ let inject_operation validator ?chain_id bytes = let inject_protocol state ?force:_ proto = let proto_bytes = - Data_encoding.Binary.to_bytes Protocol.encoding proto in + Data_encoding.Binary.to_bytes_exn Protocol.encoding proto in let hash = Protocol_hash.hash_bytes [proto_bytes] in let validation = Updater.compile hash proto >>= function diff --git a/src/lib_shell/node_rpc.ml b/src/lib_shell/node_rpc.ml index 587f4f482..d7de3d008 100644 --- a/src/lib_shell/node_rpc.ml +++ b/src/lib_shell/node_rpc.ml @@ -360,7 +360,7 @@ let build_rpc_directory node = let dir = let implementation () header = let res = - Data_encoding.Binary.to_bytes Block_header.encoding header in + Data_encoding.Binary.to_bytes_exn Block_header.encoding header in RPC_answer.return res in RPC_directory.gen_register0 dir Shell_services.S.forge_block_header implementation in diff --git a/src/lib_shell/test/test_locator.ml b/src/lib_shell/test/test_locator.ml index f9d0cb19c..6137b5891 100644 --- a/src/lib_shell/test/test_locator.ml +++ b/src/lib_shell/test/test_locator.ml @@ -43,9 +43,9 @@ let incr_fitness fitness = Data_encoding.Binary.of_bytes Data_encoding.int64 fitness |> Option.unopt ~default:0L |> Int64.succ - |> Data_encoding.Binary.to_bytes Data_encoding.int64 + |> Data_encoding.Binary.to_bytes_exn Data_encoding.int64 ) - | _ -> Data_encoding.Binary.to_bytes Data_encoding.int64 1L + | _ -> Data_encoding.Binary.to_bytes_exn Data_encoding.int64 1L in [ new_fitness ] diff --git a/src/lib_shell/test/test_state.ml b/src/lib_shell/test/test_state.ml index 0e36da8c0..429d30589 100644 --- a/src/lib_shell/test/test_state.ml +++ b/src/lib_shell/test/test_state.ml @@ -40,9 +40,9 @@ let incr_fitness fitness = Data_encoding.Binary.of_bytes Data_encoding.int64 fitness |> Option.unopt ~default:0L |> Int64.succ - |> Data_encoding.Binary.to_bytes Data_encoding.int64 + |> Data_encoding.Binary.to_bytes_exn Data_encoding.int64 ) - | _ -> Data_encoding.Binary.to_bytes Data_encoding.int64 1L + | _ -> Data_encoding.Binary.to_bytes_exn Data_encoding.int64 1L in [ new_fitness ] diff --git a/src/lib_storage/context.ml b/src/lib_storage/context.ml index 8ff69bfbb..a053a71f2 100644 --- a/src/lib_storage/context.ml +++ b/src/lib_storage/context.ml @@ -194,7 +194,7 @@ let get_test_chain v = let set_test_chain v id = raw_set v current_test_chain_key - (Data_encoding.Binary.to_bytes Test_chain_status.encoding id) + (Data_encoding.Binary.to_bytes_exn Test_chain_status.encoding id) let del_test_chain v = raw_del v current_test_chain_key let fork_test_chain v ~protocol ~expiration = diff --git a/src/lib_storage/store_helpers.ml b/src/lib_storage/store_helpers.ml index 361a4967c..82c88cc69 100644 --- a/src/lib_storage/store_helpers.ml +++ b/src/lib_storage/store_helpers.ml @@ -16,10 +16,11 @@ module Make_value (V : ENCODED_VALUE) = struct | None -> generic_error "Cannot parse data" (* TODO personalize *) | Some v -> ok v let to_bytes v = - try Data_encoding.Binary.to_bytes V.encoding v - with exn -> + try Data_encoding.Binary.to_bytes_exn V.encoding v + with Data_encoding.Binary.Write_error error -> Logging.Node.State.log_error - "Exception while serializing value %a" pp_exn exn ; + "Exception while serializing value %a" + Data_encoding.Binary.pp_write_error error ; MBytes.create 0 end diff --git a/src/proto_alpha/lib_baking/test/proto_alpha_helpers.ml b/src/proto_alpha/lib_baking/test/proto_alpha_helpers.ml index f1a8a4e2f..06a6e7be9 100644 --- a/src/proto_alpha/lib_baking/test/proto_alpha_helpers.ml +++ b/src/proto_alpha/lib_baking/test/proto_alpha_helpers.ml @@ -86,7 +86,7 @@ let protocol_parameters = match json_result with | Error err -> raise (Failure err) | Ok json -> - Data_encoding.Binary.to_bytes Data_encoding.json json + Data_encoding.Binary.to_bytes_exn Data_encoding.json json let vote_protocol_parameters = let json_result = @@ -110,7 +110,7 @@ let vote_protocol_parameters = match json_result with | Error err -> raise (Failure err) | Ok json -> - Data_encoding.Binary.to_bytes Data_encoding.json json + Data_encoding.Binary.to_bytes_exn Data_encoding.json json let activate_alpha ?(vote = false) () = let fitness = Fitness_repr.from_int64 0L in diff --git a/src/proto_alpha/lib_protocol/src/block_header_repr.ml b/src/proto_alpha/lib_protocol/src/block_header_repr.ml index 0ae98e025..797d08f5b 100644 --- a/src/proto_alpha/lib_protocol/src/block_header_repr.ml +++ b/src/proto_alpha/lib_protocol/src/block_header_repr.ml @@ -105,16 +105,16 @@ let parse_unsigned_protocol_data bytes = | Some proto -> Ok proto let forge_unsigned shell proto = - Data_encoding.Binary.to_bytes unsigned_header_encoding (shell, proto) + Data_encoding.Binary.to_bytes_exn unsigned_header_encoding (shell, proto) let forge_unsigned_protocol_data proto = - Data_encoding.Binary.to_bytes protocol_data_encoding proto + Data_encoding.Binary.to_bytes_exn protocol_data_encoding proto let hash_raw = Block_header.hash let hash { shell ; protocol_data ; signature } = Block_header.hash { shell ; protocol_data = - Data_encoding.Binary.to_bytes + Data_encoding.Binary.to_bytes_exn signed_protocol_data_encoding (protocol_data, signature ) } diff --git a/src/proto_alpha/lib_protocol/src/contract_repr.ml b/src/proto_alpha/lib_protocol/src/contract_repr.ml index 8125f8855..b3065526e 100644 --- a/src/proto_alpha/lib_protocol/src/contract_repr.ml +++ b/src/proto_alpha/lib_protocol/src/contract_repr.ml @@ -114,7 +114,7 @@ let origination_nonce_encoding = let originated_contract nonce = let data = - Data_encoding.Binary.to_bytes origination_nonce_encoding nonce in + Data_encoding.Binary.to_bytes_exn origination_nonce_encoding nonce in Originated (Contract_hash.hash_bytes [data]) let originated_contracts ({ origination_index } as origination_nonce) = diff --git a/src/proto_alpha/lib_protocol/src/operation_repr.ml b/src/proto_alpha/lib_protocol/src/operation_repr.ml index b9eda6f81..f74fe1640 100644 --- a/src/proto_alpha/lib_protocol/src/operation_repr.ml +++ b/src/proto_alpha/lib_protocol/src/operation_repr.ml @@ -474,7 +474,7 @@ let () = (fun () -> Missing_signature) let forge shell proto = - Data_encoding.Binary.to_bytes + Data_encoding.Binary.to_bytes_exn Encoding.unsigned_operation_encoding (shell, proto) let check_signature key { shell ; contents ; signature } = @@ -499,7 +499,7 @@ let parse_proto bytes = let hash_raw = Operation.hash let hash o = let proto = - Data_encoding.Binary.to_bytes + Data_encoding.Binary.to_bytes_exn Encoding.signed_proto_operation_encoding (o.contents, o.signature) in Operation.hash { shell = o.shell ; proto } diff --git a/src/proto_alpha/lib_protocol/src/raw_context.ml b/src/proto_alpha/lib_protocol/src/raw_context.ml index 8fc42d6a8..6548b0a5f 100644 --- a/src/proto_alpha/lib_protocol/src/raw_context.ml +++ b/src/proto_alpha/lib_protocol/src/raw_context.ml @@ -150,7 +150,7 @@ let get_first_level ctxt = let set_first_level ctxt level = let bytes = - Data_encoding.Binary.to_bytes Raw_level_repr.encoding level in + Data_encoding.Binary.to_bytes_exn Raw_level_repr.encoding level in Context.set ctxt first_level_key bytes >>= fun ctxt -> return ctxt @@ -212,7 +212,7 @@ let get_proto_param ctxt = let set_constants ctxt constants = let bytes = - Data_encoding.Binary.to_bytes + Data_encoding.Binary.to_bytes_exn Parameters_repr.constants_encoding constants in Context.set ctxt constants_key bytes diff --git a/src/proto_alpha/lib_protocol/src/script_ir_translator.ml b/src/proto_alpha/lib_protocol/src/script_ir_translator.ml index f8dd89ea8..73c0b9b1d 100644 --- a/src/proto_alpha/lib_protocol/src/script_ir_translator.ml +++ b/src/proto_alpha/lib_protocol/src/script_ir_translator.ml @@ -576,7 +576,7 @@ let rec unparse_data | Signature_t, s -> let `Hex text = MBytes.to_hex - (Data_encoding.Binary.to_bytes Signature.encoding s) in + (Data_encoding.Binary.to_bytes_exn Signature.encoding s) in String (-1, text) | Tez_t, v -> String (-1, Tez.to_string v) @@ -2129,7 +2129,7 @@ let typecheck_data let hash_data typ data = let unparsed = strip_annotations @@ unparse_data typ data in - let bytes = Data_encoding.Binary.to_bytes expr_encoding (Micheline.strip_locations unparsed) in + let bytes = Data_encoding.Binary.to_bytes_exn expr_encoding (Micheline.strip_locations unparsed) in Script_expr_hash.(hash_bytes [ bytes ] |> to_b58check) (* ---------------- Big map -------------------------------------------------*) diff --git a/src/proto_alpha/lib_protocol/src/storage_functors.ml b/src/proto_alpha/lib_protocol/src/storage_functors.ml index 7b0b58cab..4611530db 100644 --- a/src/proto_alpha/lib_protocol/src/storage_functors.ml +++ b/src/proto_alpha/lib_protocol/src/storage_functors.ml @@ -21,8 +21,9 @@ module Make_value (V : ENCODED_VALUE) = struct | None -> Error [Raw_context.Storage_error (Corrupted_data [(* FIXME??*)])] | Some v -> Ok v let to_bytes v = - try Data_encoding.Binary.to_bytes V.encoding v - with _ -> MBytes.create 0 + match Data_encoding.Binary.to_bytes V.encoding v with + | Some b -> b + | None -> MBytes.create 0 end module Raw_value = struct diff --git a/src/proto_alpha/lib_protocol/test/helpers/helpers_init.ml b/src/proto_alpha/lib_protocol/test/helpers/helpers_init.ml index 23c3a0c4a..0d7072231 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/helpers_init.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/helpers_init.ml @@ -19,7 +19,7 @@ let sandbox_parameters = |json} with | Error err -> raise (Failure err) | Ok json -> - Data_encoding.Binary.to_bytes Data_encoding.json json + Data_encoding.Binary.to_bytes_exn Data_encoding.json json let protocol_parameters = let json_result = @@ -52,7 +52,7 @@ let protocol_parameters = match json_result with | Error err -> raise (Failure err) | Ok json -> - Data_encoding.Binary.to_bytes Data_encoding.json json + Data_encoding.Binary.to_bytes_exn Data_encoding.json json let main () = @@ -75,7 +75,7 @@ let main () = context = Context_hash.zero ; (* don't care *) } in let protocol_data = - Data_encoding.Binary.to_bytes + Data_encoding.Binary.to_bytes_exn Alpha_context.Block_header.protocol_data_encoding (Helpers_block.get_protocol_data 0 true) in let tezos_header = { Block_header.shell = header ; protocol_data } in diff --git a/src/proto_alpha/lib_protocol/test/helpers/helpers_operation.ml b/src/proto_alpha/lib_protocol/test/helpers/helpers_operation.ml index 6f5d5aa27..25ac7ccbf 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/helpers_operation.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/helpers_operation.ml @@ -107,7 +107,7 @@ let sign src oph protop = Operation.proto_operation_encoding (obj1 @@ varopt "signature" Signature.encoding) in let proto_bytes = - Data_encoding.Binary.to_bytes + Data_encoding.Binary.to_bytes_exn signed_proto_operation_encoding (protop, signature) in (proto_bytes, signature) diff --git a/src/proto_genesis/lib_client/client_proto_main.ml b/src/proto_genesis/lib_client/client_proto_main.ml index 5a11276d6..8fe661343 100644 --- a/src/proto_genesis/lib_client/client_proto_main.ml +++ b/src/proto_genesis/lib_client/client_proto_main.ml @@ -14,12 +14,12 @@ let protocol = "ProtoGenesisGenesisGenesisGenesisGenesisGenesk612im" let bake cctxt ?(timestamp = Time.now ()) block command sk = - let protocol_data = Data_encoding.Binary.to_bytes Data.Command.encoding command in + let protocol_data = Data_encoding.Binary.to_bytes_exn Data.Command.encoding command in Block_services.preapply cctxt block ~timestamp ~protocol_data [] >>=? fun { shell_header } -> let blk = - Data_encoding.Binary.to_bytes Block_header.encoding + Data_encoding.Binary.to_bytes_exn Block_header.encoding { shell = shell_header ; protocol_data } in Client_keys.append cctxt sk blk >>=? fun signed_blk -> Shell_services.inject_block cctxt signed_blk [] @@ -74,7 +74,7 @@ let commands () = begin fun timestamp hash fitness sk param_json_file (cctxt : Client_context.full) -> let fitness = Proto_alpha.Fitness_repr.from_int64 fitness in Tezos_stdlib_unix.Lwt_utils_unix.Json.read_file param_json_file >>=? fun json -> - let protocol_parameters = Data_encoding.Binary.to_bytes Data_encoding.json json in + let protocol_parameters = Data_encoding.Binary.to_bytes_exn Data_encoding.json json in bake cctxt ?timestamp cctxt#block (Activate { protocol = hash ; fitness ; protocol_parameters }) sk >>=? fun hash -> diff --git a/src/proto_genesis/lib_protocol/src/data.ml b/src/proto_genesis/lib_protocol/src/data.ml index 3e04310d3..26fce347d 100644 --- a/src/proto_genesis/lib_protocol/src/data.ml +++ b/src/proto_genesis/lib_protocol/src/data.ml @@ -70,7 +70,7 @@ module Command = struct (req "signature" Signature.encoding) let forge shell command = - Data_encoding.Binary.to_bytes + Data_encoding.Binary.to_bytes_exn (Data_encoding.tup2 Block_header.shell_header_encoding encoding) (shell, command) @@ -94,7 +94,7 @@ module Pubkey = struct let set_pubkey ctxt v = Context.set ctxt pubkey_key @@ - Data_encoding.Binary.to_bytes Signature.Public_key.encoding v + Data_encoding.Binary.to_bytes_exn Signature.Public_key.encoding v let sandbox_encoding = let open Data_encoding in