Data_encoding: rename Binary.to_bytes into to_bytes_exn

This commit is contained in:
Grégoire Henry 2018-05-12 19:54:57 +02:00 committed by Benjamin Canou
parent 72fc71be67
commit 5023e1a261
35 changed files with 69 additions and 92 deletions

View File

@ -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 {

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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]

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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 ;

View File

@ -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

View File

@ -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 =

View File

@ -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 () ->

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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 ]

View File

@ -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 ]

View File

@ -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 =

View File

@ -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

View File

@ -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

View File

@ -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 ) }

View File

@ -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) =

View File

@ -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 }

View File

@ -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

View File

@ -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 -------------------------------------------------*)

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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 ->

View File

@ -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