From 4a48f649c0be0f98e3b026a03c755a1dc9071e50 Mon Sep 17 00:00:00 2001 From: Gauthier SEBILLE Date: Fri, 23 Dec 2022 16:39:03 +0100 Subject: [PATCH 1/3] init benchmark with core_bench --- deku-c/wasm-vm-ocaml/dune | 5 +- deku-c/wasm-vm-ocaml/operation.ml | 14 ++ deku-c/wasm-vm-ocaml/operation.mli | 2 +- deku-c/wasm-vm-ocaml/operation_payload.ml | 2 +- deku-p/src/benchmarking/benchmarking.ml | 208 ++++++++++++++++++ deku-p/src/benchmarking/dune | 6 + deku-p/src/benchmarking/readme.md | 51 +++++ deku-p/src/core/concepts/amount.ml | 2 +- deku-p/src/core/concepts/amount.mli | 2 +- deku-p/src/core/concepts/dune | 4 +- deku-p/src/core/concepts/level.ml | 2 +- deku-p/src/core/concepts/level.mli | 2 +- deku-p/src/core/consensus/block_hash.ml | 2 +- deku-p/src/core/crypto/BLAKE2b.ml | 16 +- deku-p/src/core/crypto/BLAKE2b.mli | 2 + deku-p/src/core/crypto/alg_intf.mli | 8 +- deku-p/src/core/crypto/dune | 5 +- deku-p/src/core/crypto/ed25519.ml | 10 +- deku-p/src/core/crypto/hash_intf.mli | 4 +- deku-p/src/core/crypto/key.ml | 7 + deku-p/src/core/crypto/key.mli | 2 +- deku-p/src/core/crypto/key_hash.ml | 13 +- deku-p/src/core/crypto/key_hash.mli | 2 +- deku-p/src/core/crypto/p256.ml | 8 +- deku-p/src/core/crypto/secp256k1.ml | 8 +- deku-p/src/core/crypto/signature.ml | 7 + deku-p/src/core/crypto/signature.mli | 2 +- deku-p/src/core/gossip/message_hash.ml | 2 +- deku-p/src/core/gossip/request_hash.ml | 2 +- deku-p/src/core/ledger/address.ml | 2 +- deku-p/src/core/ledger/address.mli | 2 +- deku-p/src/core/ledger/contract_address.ml | 2 +- deku-p/src/core/ledger/contract_address.mli | 2 +- deku-p/src/core/ledger/dune | 4 +- deku-p/src/core/ledger/ledger.ml | 2 +- deku-p/src/core/ledger/ticket_id.ml | 4 +- deku-p/src/core/ledger/ticket_id.mli | 2 +- deku-p/src/core/network/network_handshake.ml | 2 +- deku-p/src/core/protocol/dune | 6 +- deku-p/src/core/protocol/nonce.ml | 2 +- deku-p/src/core/protocol/nonce.mli | 2 +- deku-p/src/core/protocol/operation.ml | 6 +- deku-p/src/core/protocol/operation.mli | 4 +- deku-p/src/core/protocol/operation_hash.ml | 2 +- deku-p/src/core/protocol/operation_hash.mli | 2 +- deku-p/src/core/repr/deku_repr.ml | 35 ++- deku-p/src/core/repr/deku_repr.mli | 18 +- deku-p/src/core/repr/dune | 4 +- deku-p/src/core/stdlib/deku_stdlib.ml | 11 + deku-p/src/core/stdlib/deku_stdlib.mli | 11 + deku-p/src/core/stdlib/dune | 4 +- deku-p/src/core/stdlib/n.ml | 2 +- deku-p/src/core/stdlib/n.mli | 2 +- deku-p/src/core/stdlib/z_ext.ml | 11 + deku-p/src/core/stdlib/z_ext.mli | 6 + deku-p/src/core/tezos/address.ml | 2 +- deku-p/src/core/tezos/address.mli | 2 +- deku-p/src/core/tezos/contract_hash.ml | 2 +- deku-p/src/core/tezos/contract_hash.mli | 2 +- deku-p/src/core/tezos/dune | 4 +- deku-p/src/core/tezos/tezos_operation_hash.ml | 2 +- nix/deku-p/deku.nix | 5 +- 62 files changed, 489 insertions(+), 80 deletions(-) create mode 100644 deku-p/src/benchmarking/benchmarking.ml create mode 100644 deku-p/src/benchmarking/dune create mode 100644 deku-p/src/benchmarking/readme.md diff --git a/deku-c/wasm-vm-ocaml/dune b/deku-c/wasm-vm-ocaml/dune index 9a727e542a..b271c95b84 100644 --- a/deku-c/wasm-vm-ocaml/dune +++ b/deku-c/wasm-vm-ocaml/dune @@ -8,6 +8,7 @@ deku_concepts digestif data-encoding - cstruct) + cstruct + yojson) (preprocess - (pps ppx_deriving.ord ppx_deriving.eq ppx_deriving.show ppx_let_binding))) + (pps ppx_deriving.ord ppx_deriving.eq ppx_deriving.show ppx_let_binding ppx_yojson_conv))) diff --git a/deku-c/wasm-vm-ocaml/operation.ml b/deku-c/wasm-vm-ocaml/operation.ml index c7c8c2f3b0..49da884e66 100644 --- a/deku-c/wasm-vm-ocaml/operation.ml +++ b/deku-c/wasm-vm-ocaml/operation.ml @@ -78,3 +78,17 @@ let encoding = (fun (initial_storage, module_, constants, entrypoints) -> Originate { initial_storage; module_; constants; entrypoints }); ] + +let yojson_of_t t = + `String + (Data_encoding.Json.construct encoding t |> Data_encoding.Json.to_string) + +let t_of_yojson t = + match t with + | `String string -> ( + try + Data_encoding.Json.from_string string + |> Result.map (fun x -> Data_encoding.Json.destruct encoding x) + |> Result.get_ok + with Invalid_argument _ -> raise Not_an_operation) + | _ -> raise Not_an_operation \ No newline at end of file diff --git a/deku-c/wasm-vm-ocaml/operation.mli b/deku-c/wasm-vm-ocaml/operation.mli index 7ff0e7a409..ce3c86e463 100644 --- a/deku-c/wasm-vm-ocaml/operation.mli +++ b/deku-c/wasm-vm-ocaml/operation.mli @@ -11,7 +11,7 @@ type t = entrypoints : Entrypoints.t; constants : (int * Value.t) array; } -[@@deriving show] +[@@deriving show, yojson] exception Not_an_operation diff --git a/deku-c/wasm-vm-ocaml/operation_payload.ml b/deku-c/wasm-vm-ocaml/operation_payload.ml index 06da3162d8..b3cdb0b6d0 100644 --- a/deku-c/wasm-vm-ocaml/operation_payload.ml +++ b/deku-c/wasm-vm-ocaml/operation_payload.ml @@ -5,7 +5,7 @@ type t = { operation : Operation.t; tickets : (Ticket_id.t * Amount.t) list; [@opaque] } -[@@deriving show] +[@@deriving show, yojson] let encoding = let open Data_encoding in diff --git a/deku-p/src/benchmarking/benchmarking.ml b/deku-p/src/benchmarking/benchmarking.ml new file mode 100644 index 0000000000..4bbc62806c --- /dev/null +++ b/deku-p/src/benchmarking/benchmarking.ml @@ -0,0 +1,208 @@ +open Core_bench +open Deku_concepts +open Deku_protocol +open Deku_crypto + +module Get_level = struct + type path = unit + type response = { level : Level.t } [@@deriving yojson] + + let response_encoding = + let open Data_encoding in + conv + (fun { level } -> level) + (fun level -> { level }) + (obj1 (req "level" Level.encoding)) +end + +module Encode_operation = struct + type body = Operation.Initial.hash_repr [@@deriving yojson] + + let body_encoding = Operation.Initial.hash_encoding +end + +module Operations = struct + type t = { + key : Key.t; + signature : Signature.t; + initial : Operation.Initial.t; + } + [@@deriving yojson] + + let obj_encoding = + let open Data_encoding in + conv + (fun { key; signature; initial } -> (key, signature, initial)) + (fun (key, signature, initial) -> { key; signature; initial }) + (obj3 (req "key" Key.encoding) + (req "signature" Signature.encoding) + (req "initial" Operation.Initial.encoding)) + + let tup_encoding = + let open Data_encoding in + conv + (fun { key; signature; initial } -> (key, signature, initial)) + (fun (key, signature, initial) -> { key; signature; initial }) + (tup3 Key.encoding Signature.encoding Operation.Initial.encoding) + + let to_signed repr = + let { key; signature; initial } = repr in + Operation.Signed.make_with_signature ~key ~signature ~initial +end + +let () = + let level : Get_level.response = + { + level = + Level.next @@ Level.next @@ Level.next @@ Level.next @@ Level.next + @@ Level.next @@ Level.next @@ Level.next @@ Level.next @@ Level.next + @@ Level.next @@ Level.next @@ Level.next @@ Level.next @@ Level.next + @@ Level.next @@ Level.next @@ Level.next Level.zero; + } + in + let json_level_yojson = {| { "level" : "18" } |} in + let json_level_encoding = {| { "level" : 18 } |} in + let json_encode_operation_encoding = + {| {"nonce":"495761182","level":289099,"operation":{"type":"vm_transaction","sender":"tz1KufAGaM2EM49bikm5VQfLNWT9rWsAWEHy","operation":{"operation":{"address":"DK1DpUMB44Ex3WXEUXPjp9DDkjiQ5cyvVwoU","argument":["Union",["Right",["Union",["Left",["Pair",[["Pair",[["Int","136331"],["String","tz1KufAGaM2EM49bikm5VQfLNWT9rWsAWEHy"]]],["Union",["Left",["Union",["Left",["Union",["Right",["Unit"]]]]]]]]]]]]]},"tickets":[]}}}|} + in + let encode_operation = + Data_encoding.Json.from_string json_encode_operation_encoding + |> Result.get_ok + |> Data_encoding.Json.destruct Encode_operation.body_encoding + in + let json_encode_operation_yojson = + Yojson.Safe.to_string (Encode_operation.yojson_of_body encode_operation) + in + let json_operation_encoding = + {| {"key":"edpkuUADDrRjMHLq9ehcWJev6Gd9YLTX38vgXSizzghJ9NS5ceNg8v","signature":"edsigu48RihHyRN7XRm8YjdLzCcF8rMRwF6WWTzGcU3jQYguQhCCv1thksnMSrbEwEhHnRUovfKoSYLaacT6ek3iJLuBgfaARL2","initial":{"nonce":"735632897","level":291078,"operation":{"type":"vm_transaction","sender":"tz1KufAGaM2EM49bikm5VQfLNWT9rWsAWEHy","operation":{"operation":{"address":"DK1DpUMB44Ex3WXEUXPjp9DDkjiQ5cyvVwoU","argument":["Union",["Right",["Union",["Left",["Pair",[["Pair",[["Int","136331"],["String","tz1KufAGaM2EM49bikm5VQfLNWT9rWsAWEHy"]]],["Union",["Left",["Union",["Left",["Union",["Right",["Unit"]]]]]]]]]]]]]},"tickets":[]}}}}|} + in + let operation = + Data_encoding.Json.from_string json_operation_encoding + |> Result.get_ok + |> Data_encoding.Json.destruct Operations.obj_encoding + in + let binary_operation : string = + Data_encoding.Binary.to_string_opt Operations.obj_encoding operation + |> Option.get + in + let json_operation_yojson = + Operations.yojson_of_t operation |> Yojson.Safe.to_string + in + let json_operation_tup = + Data_encoding.Json.to_string + (Data_encoding.Json.construct Operations.tup_encoding operation) + in + let config : Core_bench_internals.Run_config.t = + Bench.Run_config.create + ?verbosity:(Some Core_bench_internals.Verbosity.High) + ?quota:(Some (Bench.Quota.of_string "10")) + ?no_compactions:None ?sampling_type:None ?stabilize_gc_between_runs:None + ?fork_each_benchmark:None ?thin_overhead:None () + in + let measures = + Bench.measure ?run_config:(Some config) + [ + Bench.Test.create_group ~name:"level to JSON" + [ + Bench.Test.create ~name:"yojson" (fun () -> + ignore (Get_level.yojson_of_response level)); + Bench.Test.create ~name:"encoding" (fun () -> + ignore + (Data_encoding.Json.construct Get_level.response_encoding + level)); + ]; + Bench.Test.create_group ~name:"Encode_operation to JSON" + [ + Bench.Test.create ~name:"yojson" (fun () -> + ignore (Encode_operation.yojson_of_body encode_operation)); + Bench.Test.create ~name:"encoding" (fun () -> + ignore + (Data_encoding.Json.construct Encode_operation.body_encoding + encode_operation)); + ]; + Bench.Test.create_group ~name:"Operations to JSON" + [ + Bench.Test.create ~name:"yojson" (fun () -> + ignore (Operations.yojson_of_t operation)); + Bench.Test.create ~name:"encoding" (fun () -> + ignore + (Data_encoding.Json.construct Operations.obj_encoding + operation)); + ]; + Bench.Test.create_group ~name:"level from JSON" + [ + Bench.Test.create ~name:"yojson" (fun () -> + ignore + (Get_level.response_of_yojson + @@ Yojson.Safe.from_string json_level_yojson)); + Bench.Test.create ~name:"encoding" (fun () -> + ignore + (Data_encoding.Json.from_string json_level_encoding + |> Result.get_ok + |> Data_encoding.Json.destruct Get_level.response_encoding)); + ]; + Bench.Test.create_group ~name:"Encode_operation from JSON" + [ + Bench.Test.create ~name:"yojson" (fun () -> + ignore + (Encode_operation.body_of_yojson + @@ Yojson.Safe.from_string json_encode_operation_yojson)); + Bench.Test.create ~name:"encoding" (fun () -> + ignore + (Data_encoding.Json.from_string json_encode_operation_encoding + |> Result.get_ok + |> Data_encoding.Json.destruct Encode_operation.body_encoding + )); + ]; + Bench.Test.create_group ~name:"Operations from JSON" + [ + Bench.Test.create ~name:"yojson" (fun () -> + ignore + (Operations.t_of_yojson + @@ Yojson.Safe.from_string json_operation_yojson)); + Bench.Test.create ~name:"encoding" (fun () -> + ignore + (Data_encoding.Json.from_string json_operation_encoding + |> Result.get_ok + |> Data_encoding.Json.destruct Operations.obj_encoding)); + ]; + Bench.Test.create_group ~name:"Operations from JSON + to_SIGNED" + [ + Bench.Test.create ~name:"yojson" (fun () -> + ignore + (Operations.to_signed @@ Operations.t_of_yojson + @@ Yojson.Safe.from_string json_operation_yojson)); + Bench.Test.create ~name:"encoding" (fun () -> + ignore + (Data_encoding.Json.from_string json_operation_encoding + |> Result.get_ok + |> Data_encoding.Json.destruct Operations.obj_encoding + |> Operations.to_signed)); + ]; + Bench.Test.create_group ~name:"Operations from JSON + tup" + [ + Bench.Test.create ~name:"encoding" (fun () -> + ignore + (Data_encoding.Json.from_string json_operation_tup + |> Result.get_ok + |> Data_encoding.Json.destruct Operations.tup_encoding)); + ]; + Bench.Test.create_group ~name:"Operations from binary" + [ + Bench.Test.create ~name:"encoding" (fun () -> + ignore + (Data_encoding.Binary.of_string_exn Operations.obj_encoding + binary_operation)); + ]; + ] + in + let analysis_results : Core_bench.Bench.Analysis_result.t Core.Or_error.t list + = + List.map (fun m -> Bench.analyze m) measures + in + let result = + List.map + (fun r -> match r with Ok r -> r | Error _e -> failwith "error") + analysis_results + in + Bench.display result diff --git a/deku-p/src/benchmarking/dune b/deku-p/src/benchmarking/dune new file mode 100644 index 0000000000..19d03ed67a --- /dev/null +++ b/deku-p/src/benchmarking/dune @@ -0,0 +1,6 @@ +(executable + (public_name benchmarking) + (name benchmarking) + (libraries deku_concepts deku_protocol fmt.tty core core_bench core_unix.command_unix yojson) + (preprocess + (pps ppx_yojson_conv))) diff --git a/deku-p/src/benchmarking/readme.md b/deku-p/src/benchmarking/readme.md new file mode 100644 index 0000000000..9f4d423e81 --- /dev/null +++ b/deku-p/src/benchmarking/readme.md @@ -0,0 +1,51 @@ +# Run the benchmarks + +```bash +$ dune exec benchmarking + +Estimated testing time 2m40s (16 benchmarks x 10s). Change using '-quota'. +level to JSON/yojson: Total time taken 10.032530069351196s (1143 samples, max runs 1822631). +level to JSON/encoding: Total time taken 10.098333120346069s (1171 samples, max runs 2408211). +Encode_operation to JSON/yojson: Total time taken 10.000631093978882s (527 samples, max runs 4021). +Encode_operation to JSON/encoding: Total time taken 10.075474977493286s (648 samples, max runs 13285). +Operations to JSON/yojson: Total time taken 10.07265305519104s (469 samples, max runs 2279). +Operations to JSON/encoding: Total time taken 10.0344078540802s (538 samples, max runs 4481). +level from JSON/yojson: Total time taken 10.066639184951782s (976 samples, max runs 346003). +level from JSON/encoding: Total time taken 10.045109033584595s (909 samples, max runs 177666). +Encode_operation from JSON/yojson: Total time taken 10.079186201095581s (124 samples, max runs 124). +Encode_operation from JSON/encoding: Total time taken 10.10786509513855s (125 samples, max runs 125). +Operations from JSON/yojson: Total time taken 10.132002115249634s (124 samples, max runs 124). +Operations from JSON/encoding: Total time taken 10.004235744476318s (124 samples, max runs 124). +Operations from JSON + to_SIGNED/yojson: Total time taken 10.158401012420654s (122 samples, max runs 122). +Operations from JSON + to_SIGNED/encoding: Total time taken 10.160748958587646s (123 samples, max runs 123). +Operations from JSON + tup/encoding: Total time taken 10.052727937698364s (124 samples, max runs 124). +Operations from binary/encoding: Total time taken 10.023288249969482s (655 samples, max runs 14239). +┌───────────────────────────────────────────┬────────────────┬───────────────┬────────────┬────────────┬─────────────┬──────────┐ +│ Name │ Time/Run │ mWd/Run │ mjWd/Run │ Prom/Run │ mGC/Run │ mjGC/Run │ +├───────────────────────────────────────────┼────────────────┼───────────────┼────────────┼────────────┼─────────────┼──────────┤ +│ level to JSON/yojson │ 54.92ns │ 14.00w │ │ │ 0.05e-3 │ │ +│ level to JSON/encoding │ 41.93ns │ 49.00w │ │ │ 0.19e-3 │ │ +│ Encode_operation to JSON/yojson │ 23_961.44ns │ 15_285.29w │ 150.77w │ 150.77w │ 58.94e-3 │ 0.63e-3 │ +│ Encode_operation to JSON/encoding │ 7_480.26ns │ 2_869.19w │ 44.74w │ 44.74w │ 11.10e-3 │ 0.15e-3 │ +│ Operations to JSON/yojson │ 41_533.79ns │ 18_500.14w │ 219.22w │ 219.22w │ 71.27e-3 │ 0.70e-3 │ +│ Operations to JSON/encoding │ 21_624.05ns │ 5_912.69w │ 88.59w │ 88.59w │ 22.81e-3 │ 0.25e-3 │ +│ level from JSON/yojson │ 290.44ns │ 96.00w │ │ │ 0.37e-3 │ │ +│ level from JSON/encoding │ 563.94ns │ 486.00w │ 0.17w │ 0.17w │ 1.86e-3 │ │ +│ Encode_operation from JSON/yojson │ 1_322_918.03ns │ 1_548_192.73w │ 14_095.65w │ 14_095.65w │ 5_937.22e-3 │ 31.44e-3 │ +│ Encode_operation from JSON/encoding │ 1_306_432.95ns │ 1_547_962.53w │ 13_982.59w │ 13_982.59w │ 5_934.08e-3 │ 29.13e-3 │ +│ Operations from JSON/yojson │ 1_327_051.61ns │ 1_552_779.10w │ 14_413.98w │ 14_413.98w │ 5_951.41e-3 │ 28.18e-3 │ +│ Operations from JSON/encoding │ 1_309_895.89ns │ 1_553_628.50w │ 14_203.58w │ 14_203.58w │ 5_952.61e-3 │ 26.10e-3 │ +│ Operations from JSON + to_SIGNED/yojson │ 1_379_044.45ns │ 1_552_898.45w │ 14_461.49w │ 14_461.49w │ 5_948.82e-3 │ 25.05e-3 │ +│ Operations from JSON + to_SIGNED/encoding │ 1_352_071.62ns │ 1_553_819.52w │ 14_252.51w │ 14_252.51w │ 5_950.50e-3 │ 23.32e-3 │ +│ Operations from JSON + tup/encoding │ 1_316_792.64ns │ 1_557_600.61w │ 14_198.52w │ 14_198.52w │ 5_963.62e-3 │ 22.02e-3 │ +│ Operations from binary/encoding │ 6_952.12ns │ 1_155.88w │ 14.43w │ 14.43w │ 4.43e-3 │ 0.02e-3 │ +└───────────────────────────────────────────┴────────────────┴───────────────┴────────────┴────────────┴─────────────┴──────────┘ +``` + +# What had been tested? + +TODO + +# Understading the results + +TODO diff --git a/deku-p/src/core/concepts/amount.ml b/deku-p/src/core/concepts/amount.ml index 2fa6051e93..af01b63f41 100644 --- a/deku-p/src/core/concepts/amount.ml +++ b/deku-p/src/core/concepts/amount.ml @@ -2,7 +2,7 @@ open Deku_stdlib open N type amount = N.t -and t = amount [@@deriving show, eq, ord] +and t = amount [@@deriving show, eq, ord, yojson] let zero = zero let one = one diff --git a/deku-p/src/core/concepts/amount.mli b/deku-p/src/core/concepts/amount.mli index 5a6859f84d..f2db82b557 100644 --- a/deku-p/src/core/concepts/amount.mli +++ b/deku-p/src/core/concepts/amount.mli @@ -1,7 +1,7 @@ open Deku_stdlib type amount -type t = amount [@@deriving show, eq, ord] +type t = amount [@@deriving show, eq, ord, yojson] val zero : amount val one : amount diff --git a/deku-p/src/core/concepts/dune b/deku-p/src/core/concepts/dune index 611c199019..13b0e405a8 100644 --- a/deku-p/src/core/concepts/dune +++ b/deku-p/src/core/concepts/dune @@ -1,5 +1,5 @@ (library (name deku_concepts) - (libraries deku_stdlib deku_crypto) + (libraries deku_stdlib deku_crypto yojson) (preprocess - (pps ppx_deriving.show ppx_deriving.eq ppx_deriving.ord))) + (pps ppx_deriving.show ppx_deriving.eq ppx_deriving.ord ppx_yojson_conv))) diff --git a/deku-p/src/core/concepts/level.ml b/deku-p/src/core/concepts/level.ml index 36efce8cfb..775348c058 100644 --- a/deku-p/src/core/concepts/level.ml +++ b/deku-p/src/core/concepts/level.ml @@ -3,7 +3,7 @@ open N (* TODO: should we prefix level in b58? *) type level = N.t -and t = level [@@deriving show, eq, ord] +and t = level [@@deriving show, eq, ord, yojson] let zero = zero let next x = x + one diff --git a/deku-p/src/core/concepts/level.mli b/deku-p/src/core/concepts/level.mli index a04bcfc816..cd5b2cba86 100644 --- a/deku-p/src/core/concepts/level.mli +++ b/deku-p/src/core/concepts/level.mli @@ -1,7 +1,7 @@ open Deku_stdlib type level -type t = level [@@deriving show, eq, ord] +type t = level [@@deriving show, eq, ord, yojson] val zero : level val next : level -> level diff --git a/deku-p/src/core/consensus/block_hash.ml b/deku-p/src/core/consensus/block_hash.ml index 99843ed770..eb980c04f5 100644 --- a/deku-p/src/core/consensus/block_hash.ml +++ b/deku-p/src/core/consensus/block_hash.ml @@ -8,7 +8,7 @@ and t = block_hash [@@deriving eq, ord] let of_blake2b hash = hash let to_blake2b operation_hash = operation_hash -include With_b58_and_encoding (struct +include With_b58_and_encoding_and_yojson (struct let name = "Deku_consensus.Block_hash" let prefix = Prefix.deku_block_hash end) diff --git a/deku-p/src/core/crypto/BLAKE2b.ml b/deku-p/src/core/crypto/BLAKE2b.ml index 327054804d..eeb2eab07b 100644 --- a/deku-p/src/core/crypto/BLAKE2b.ml +++ b/deku-p/src/core/crypto/BLAKE2b.ml @@ -41,12 +41,12 @@ struct Alg.verify key signature (to_raw_string hash) end - module With_b58_and_encoding (P : sig + module With_b58_and_encoding_and_yojson (P : sig val name : string val prefix : Prefix.t end) = struct - include With_b58_and_encoding (struct + include With_b58_and_encoding_and_yojson (struct include P type t = hash @@ -91,3 +91,15 @@ let encoding = | Some hash -> hash | None -> failwith "impossible to decode") string + +let yojson_of_t t = `String (to_hex t) + +let t_of_yojson json = + (* TODO: Is this warning correct? *) + Logs.warn (fun m -> + m "error, an 'untyped' hash is circulating on the network"); + let json_str = Yojson.Safe.Util.to_string json in + match json_str |> of_hex with + | Some t -> t + | None -> + failwith (Format.sprintf "cannot deserialize %s to blake2b" json_str) \ No newline at end of file diff --git a/deku-p/src/core/crypto/BLAKE2b.mli b/deku-p/src/core/crypto/BLAKE2b.mli index 8809788023..7f06c3e6f3 100644 --- a/deku-p/src/core/crypto/BLAKE2b.mli +++ b/deku-p/src/core/crypto/BLAKE2b.mli @@ -3,3 +3,5 @@ module BLAKE2b_256 : Hash_intf.S include Hash_intf.S with type hash = BLAKE2b_256.hash val encoding : t Data_encoding.t +val t_of_yojson : Yojson.Safe.t -> t +val yojson_of_t : t -> Yojson.Safe.t \ No newline at end of file diff --git a/deku-p/src/core/crypto/alg_intf.mli b/deku-p/src/core/crypto/alg_intf.mli index c2285d8486..40e2aa970e 100644 --- a/deku-p/src/core/crypto/alg_intf.mli +++ b/deku-p/src/core/crypto/alg_intf.mli @@ -1,7 +1,7 @@ module type S = sig module Secret : sig type secret - type t = secret [@@deriving eq, ord] + type t = secret [@@deriving eq, ord, yojson] (* repr *) val of_b58 : string -> secret option @@ -18,7 +18,7 @@ module type S = sig module Key : sig type key - type t = key [@@deriving eq, ord] + type t = key [@@deriving eq, ord, yojson] (* repr *) val of_b58 : string -> key option @@ -35,7 +35,7 @@ module type S = sig module Key_hash : sig type key_hash - type t = key_hash [@@deriving eq, ord] + type t = key_hash [@@deriving eq, ord, yojson] (* repr *) val of_b58 : string -> key_hash option @@ -48,7 +48,7 @@ module type S = sig module Signature : sig type signature - type t = signature [@@deriving eq, ord] + type t = signature [@@deriving eq, ord, yojson] (* repr *) val of_b58 : string -> signature option diff --git a/deku-p/src/core/crypto/dune b/deku-p/src/core/crypto/dune index 8c248daa80..a5d1ac7a58 100644 --- a/deku-p/src/core/crypto/dune +++ b/deku-p/src/core/crypto/dune @@ -9,7 +9,8 @@ mirage-crypto-rng.unix digestif mirage-crypto-ec - secp256k1-internal) + secp256k1-internal + yojson) (modules_without_implementation alg_intf hash_intf) (preprocess - (pps ppx_deriving.show ppx_deriving.eq ppx_deriving.ord))) + (pps ppx_deriving.show ppx_deriving.eq ppx_deriving.ord ppx_yojson_conv))) diff --git a/deku-p/src/core/crypto/ed25519.ml b/deku-p/src/core/crypto/ed25519.ml index 4529d191c7..8663cd2e8e 100644 --- a/deku-p/src/core/crypto/ed25519.ml +++ b/deku-p/src/core/crypto/ed25519.ml @@ -12,7 +12,7 @@ module Secret = struct let compare a b = Cstruct.compare (priv_to_cstruct a) (priv_to_cstruct b) - include With_b58_and_encoding (struct + include With_b58_and_encoding_and_yojson (struct type t = secret let name = "Ed25519.Secret_key" @@ -50,7 +50,7 @@ module Key = struct let of_secret secret = pub_of_priv secret let to_raw key = Cstruct.to_string (pub_to_cstruct key) - include With_b58_and_encoding (struct + include With_b58_and_encoding_and_yojson (struct type t = key let name = "Ed25519.Public_key" @@ -83,7 +83,7 @@ module Key_hash = struct let compare = compare let of_key key = hash (Key.to_raw key) - include With_b58_and_encoding (struct + include With_b58_and_encoding_and_yojson (struct let name = "Ed25519.Public_key_hash" let prefix = Prefix.ed25519_public_key_hash end) @@ -98,7 +98,7 @@ module Signature = struct let size = 64 let zero = String.make size '\x00' - include With_b58_and_encoding (struct + include With_b58_and_encoding_and_yojson (struct type t = signature let name = "Ed25519.Signature" @@ -127,4 +127,4 @@ module Signature = struct let signature = Cstruct.of_string signature in verify ~key ~msg:hash signature end) -end +end \ No newline at end of file diff --git a/deku-p/src/core/crypto/hash_intf.mli b/deku-p/src/core/crypto/hash_intf.mli index 631c7c1451..45facf7eb7 100644 --- a/deku-p/src/core/crypto/hash_intf.mli +++ b/deku-p/src/core/crypto/hash_intf.mli @@ -35,13 +35,15 @@ module type S = sig val verify : key -> signature -> hash -> bool end - module With_b58_and_encoding (_ : sig + module With_b58_and_encoding_and_yojson (_ : sig val name : string val prefix : Prefix.t end) : sig val of_b58 : string -> hash option val to_b58 : hash -> string val encoding : hash Data_encoding.t + val t_of_yojson : [> `String of string ] -> hash + val yojson_of_t : hash -> [> `String of string ] module Set : Set.S with type elt = hash module Map : Map.S with type key = hash diff --git a/deku-p/src/core/crypto/key.ml b/deku-p/src/core/crypto/key.ml index f9d2a64fcd..baf5e82861 100644 --- a/deku-p/src/core/crypto/key.ml +++ b/deku-p/src/core/crypto/key.ml @@ -37,6 +37,13 @@ let of_secret secret = | Secret.Secp256k1 secret -> Secp256k1 (Secp256k1.Key.of_secret secret) | Secret.P256 secret -> P256 (P256.Key.of_secret secret) +include With_yojson_of_b58 (struct + type t = key + + let of_b58 = of_b58 + let to_b58 = to_b58 +end) + let encoding = let open Data_encoding in let name = "Signature.Public_key" in diff --git a/deku-p/src/core/crypto/key.mli b/deku-p/src/core/crypto/key.mli index 955a6c5c17..b9ef198f1b 100644 --- a/deku-p/src/core/crypto/key.mli +++ b/deku-p/src/core/crypto/key.mli @@ -4,7 +4,7 @@ type key = | Secp256k1 of Secp256k1.Key.t | P256 of P256.Key.t -type t = key [@@deriving eq, ord, show] +type t = key [@@deriving eq, ord, show, yojson] (* repr *) val of_b58 : string -> key option diff --git a/deku-p/src/core/crypto/key_hash.ml b/deku-p/src/core/crypto/key_hash.ml index 30a2bb7305..2eada6feb0 100644 --- a/deku-p/src/core/crypto/key_hash.ml +++ b/deku-p/src/core/crypto/key_hash.ml @@ -6,7 +6,7 @@ type key_hash = | Secp256k1 of Secp256k1.Key_hash.t | P256 of P256.Key_hash.t -and t = key_hash [@@deriving eq, ord] +and t = key_hash [@@deriving eq, ord, yojson] let of_b58 = let ed25519 string = @@ -37,6 +37,13 @@ let of_key = function | Key.Secp256k1 key -> Secp256k1 (Secp256k1.Key_hash.of_key key) | Key.P256 key -> P256 (P256.Key_hash.of_key key) +include With_yojson_of_b58 (struct + type t = key_hash + + let of_b58 = of_b58 + let to_b58 = to_b58 +end) + let encoding = let open Data_encoding in let name = "Signature.Public_key_hash" in @@ -57,13 +64,13 @@ let encoding = make_encoding ~name ~to_string:to_b58 ~of_string:of_b58 ~raw_encoding module Map = Map.Make (struct - type t = key_hash [@@deriving ord] + type t = key_hash [@@deriving ord, yojson] let encoding = encoding end) module Set = Set.Make (struct - type t = key_hash [@@deriving ord] + type t = key_hash [@@deriving ord, yojson] let encoding = encoding end) diff --git a/deku-p/src/core/crypto/key_hash.mli b/deku-p/src/core/crypto/key_hash.mli index ecfe82dc65..2924c146f9 100644 --- a/deku-p/src/core/crypto/key_hash.mli +++ b/deku-p/src/core/crypto/key_hash.mli @@ -6,7 +6,7 @@ type key_hash = | Secp256k1 of Secp256k1.Key_hash.t | P256 of P256.Key_hash.t -type t = key_hash [@@deriving eq, ord, show] +type t = key_hash [@@deriving eq, ord, show, yojson] (* repr *) val of_b58 : string -> key_hash option diff --git a/deku-p/src/core/crypto/p256.ml b/deku-p/src/core/crypto/p256.ml index 0eed129e1d..36d1e79726 100644 --- a/deku-p/src/core/crypto/p256.ml +++ b/deku-p/src/core/crypto/p256.ml @@ -13,7 +13,7 @@ module Secret = struct let compare a b = Cstruct.compare (priv_to_cstruct a) (priv_to_cstruct b) - include With_b58_and_encoding (struct + include With_b58_and_encoding_and_yojson (struct type t = secret let name = "P256.Secret_key" @@ -52,7 +52,7 @@ module Key = struct let compare a b = Cstruct.compare (pub_to_cstruct a) (pub_to_cstruct b) let to_raw key = Cstruct.to_string (pub_to_cstruct ~compress:true key) - include With_b58_and_encoding (struct + include With_b58_and_encoding_and_yojson (struct type t = key let name = "P256.Public_key" @@ -84,7 +84,7 @@ module Key_hash = struct let compare = compare let of_key key = hash (Key.to_raw key) - include With_b58_and_encoding (struct + include With_b58_and_encoding_and_yojson (struct let name = "P256.Public_key_hash" let prefix = Prefix.p256_public_key_hash end) @@ -99,7 +99,7 @@ module Signature = struct let size = 64 let zero = String.make size '\x00' - include With_b58_and_encoding (struct + include With_b58_and_encoding_and_yojson (struct type t = signature let name = "P256.Signature" diff --git a/deku-p/src/core/crypto/secp256k1.ml b/deku-p/src/core/crypto/secp256k1.ml index b6e091cd5b..d15869d9ac 100644 --- a/deku-p/src/core/crypto/secp256k1.ml +++ b/deku-p/src/core/crypto/secp256k1.ml @@ -24,7 +24,7 @@ module Secret = struct let to_raw secret = Bigstring.to_string (Libsecp256k1.Key.to_bytes context secret) - include With_b58_and_encoding (struct + include With_b58_and_encoding_and_yojson (struct type t = secret let name = "Secp256k1.Secret_key" @@ -63,7 +63,7 @@ module Key = struct let of_secret secret = Libsecp256k1.Key.neuterize_exn context secret let to_raw key = Bigstring.to_string (Libsecp256k1.Key.to_bytes context key) - include With_b58_and_encoding (struct + include With_b58_and_encoding_and_yojson (struct type t = key let name = "Secp256k1.Public_key" @@ -97,7 +97,7 @@ module Key_hash = struct let compare = compare let of_key key = hash (Key.to_raw key) - include With_b58_and_encoding (struct + include With_b58_and_encoding_and_yojson (struct let name = "Secp256k1.Public_key_hash" let prefix = Prefix.secp256k1_public_key_hash end) @@ -116,7 +116,7 @@ module Signature = struct let size = Sign.plain_bytes let zero = of_raw (String.make size '\x00') |> Option.get - include With_b58_and_encoding (struct + include With_b58_and_encoding_and_yojson (struct type t = signature let name = "Secp256k1.Secret" diff --git a/deku-p/src/core/crypto/signature.ml b/deku-p/src/core/crypto/signature.ml index 2540cc5ac2..864be1748f 100644 --- a/deku-p/src/core/crypto/signature.ml +++ b/deku-p/src/core/crypto/signature.ml @@ -45,6 +45,13 @@ let to_b58 = function | Secp256k1 signature -> Secp256k1.Signature.to_b58 signature | P256 signature -> P256.Signature.to_b58 signature +include With_yojson_of_b58 (struct + type t = signature + + let of_b58 = of_b58 + let to_b58 = to_b58 +end) + let size = assert ( Ed25519.Signature.size = Secp256k1.Signature.size diff --git a/deku-p/src/core/crypto/signature.mli b/deku-p/src/core/crypto/signature.mli index 250df57208..1266ca941f 100644 --- a/deku-p/src/core/crypto/signature.mli +++ b/deku-p/src/core/crypto/signature.mli @@ -4,7 +4,7 @@ type signature = | Secp256k1 of Secp256k1.Signature.t | P256 of P256.Signature.t -type t = signature [@@deriving eq, ord, show] +type t = signature [@@deriving eq, ord, show, yojson] val encoding : t Data_encoding.t diff --git a/deku-p/src/core/gossip/message_hash.ml b/deku-p/src/core/gossip/message_hash.ml index 4dc7af8da4..aab7346db7 100644 --- a/deku-p/src/core/gossip/message_hash.ml +++ b/deku-p/src/core/gossip/message_hash.ml @@ -8,7 +8,7 @@ and t = message_hash [@@deriving eq, ord] let to_blake2b message_hash = message_hash let of_blake2b message_hash = message_hash -include With_b58_and_encoding (struct +include With_b58_and_encoding_and_yojson (struct let name = "Deku_gossip.Message_hash" let prefix = Prefix.deku_message_hash end) diff --git a/deku-p/src/core/gossip/request_hash.ml b/deku-p/src/core/gossip/request_hash.ml index fb2bff92b1..ad7f83bb5c 100644 --- a/deku-p/src/core/gossip/request_hash.ml +++ b/deku-p/src/core/gossip/request_hash.ml @@ -7,7 +7,7 @@ and t = request_hash [@@deriving eq, ord] let to_blake2b request_hash = request_hash -include With_b58_and_encoding (struct +include With_b58_and_encoding_and_yojson (struct let name = "Deku_gossip.Request_hash" let prefix = Prefix.deku_request_hash end) diff --git a/deku-p/src/core/ledger/address.ml b/deku-p/src/core/ledger/address.ml index 88968b5d57..edb398386b 100644 --- a/deku-p/src/core/ledger/address.ml +++ b/deku-p/src/core/ledger/address.ml @@ -5,7 +5,7 @@ type address = | Implicit of Key_hash.t | Originated of { address : Contract_address.t; entrypoint : string option } -and t = address [@@deriving eq, ord, show] +and t = address [@@deriving eq, ord, show, yojson] let of_key_hash key_hash = Implicit key_hash let to_key_hash = function Implicit x -> Some x | Originated _ -> None diff --git a/deku-p/src/core/ledger/address.mli b/deku-p/src/core/ledger/address.mli index 3f794e67a9..9ac0b139db 100644 --- a/deku-p/src/core/ledger/address.mli +++ b/deku-p/src/core/ledger/address.mli @@ -2,7 +2,7 @@ open Deku_stdlib open Deku_crypto type address -type t = address [@@deriving eq, ord, show] +type t = address [@@deriving eq, ord, show, yojson] (* repr *) val of_key_hash : Key_hash.t -> address diff --git a/deku-p/src/core/ledger/contract_address.ml b/deku-p/src/core/ledger/contract_address.ml index c2d2335610..37647228e7 100644 --- a/deku-p/src/core/ledger/contract_address.ml +++ b/deku-p/src/core/ledger/contract_address.ml @@ -7,7 +7,7 @@ type t = BLAKE2b_160.t [@@deriving eq, ord, show] let of_user_operation_hash t = BLAKE2b_256.to_hex t |> BLAKE2b_160.hash -include With_b58_and_encoding (struct +include With_b58_and_encoding_and_yojson (struct let name = "Deku_contract_hash" let prefix = Prefix.deku_contract_hash end) diff --git a/deku-p/src/core/ledger/contract_address.mli b/deku-p/src/core/ledger/contract_address.mli index 6a4f1be6c1..eb03eea3b1 100644 --- a/deku-p/src/core/ledger/contract_address.mli +++ b/deku-p/src/core/ledger/contract_address.mli @@ -1,7 +1,7 @@ open Deku_crypto open BLAKE2b -type t [@@deriving ord, eq, show] +type t [@@deriving ord, eq, show, yojson] val of_user_operation_hash : BLAKE2b_256.t -> t val to_b58 : t -> string diff --git a/deku-p/src/core/ledger/dune b/deku-p/src/core/ledger/dune index addafcff0f..1378af7016 100644 --- a/deku-p/src/core/ledger/dune +++ b/deku-p/src/core/ledger/dune @@ -1,5 +1,5 @@ (library (name deku_ledger) - (libraries deku_stdlib deku_crypto deku_concepts deku_tezos data-encoding) + (libraries deku_stdlib deku_crypto deku_concepts deku_tezos data-encoding yojson) (preprocess - (pps ppx_deriving.show ppx_deriving.eq ppx_deriving.ord ppx_let_binding))) + (pps ppx_deriving.show ppx_deriving.eq ppx_deriving.ord ppx_let_binding ppx_yojson_conv))) diff --git a/deku-p/src/core/ledger/ledger.ml b/deku-p/src/core/ledger/ledger.ml index db6e366234..48777ae516 100644 --- a/deku-p/src/core/ledger/ledger.ml +++ b/deku-p/src/core/ledger/ledger.ml @@ -15,7 +15,7 @@ module Withdrawal_handle = struct type t = BLAKE2b.t [@@deriving show] - include With_b58_and_encoding (struct + include With_b58_and_encoding_and_yojson (struct let name = "Ledger.Withdrawal_handle_hash" let prefix = Deku_repr.Prefix.deku_withdrawal_hash end) diff --git a/deku-p/src/core/ledger/ticket_id.ml b/deku-p/src/core/ledger/ticket_id.ml index 6cab0f7a70..ff9c20f073 100644 --- a/deku-p/src/core/ledger/ticket_id.ml +++ b/deku-p/src/core/ledger/ticket_id.ml @@ -1,7 +1,7 @@ type ticketer = | Tezos of Deku_tezos.Contract_hash.t | Deku of Contract_address.t -[@@deriving ord, eq, show] +[@@deriving ord, eq, show, yojson] let ticketer_encoding = let tezos_tag = 0 in @@ -30,7 +30,7 @@ let ticketer_encoding = ] type ticket_id = Ticket_id of { ticketer : ticketer; data : bytes } -[@@deriving ord, show, eq] +[@@deriving ord, show, eq, yojson] and t = ticket_id [@@deriving eq, ord, show] diff --git a/deku-p/src/core/ledger/ticket_id.mli b/deku-p/src/core/ledger/ticket_id.mli index 9cae8c5ccb..c74d77a9e5 100644 --- a/deku-p/src/core/ledger/ticket_id.mli +++ b/deku-p/src/core/ledger/ticket_id.mli @@ -4,7 +4,7 @@ type ticketer = [@@deriving ord, show] type ticket_id = private Ticket_id of { ticketer : ticketer; data : bytes } -and t = ticket_id [@@deriving ord, show, eq] +and t = ticket_id [@@deriving ord, show, eq, yojson] val make : ticketer -> bytes -> t val encoding : ticket_id Data_encoding.t diff --git a/deku-p/src/core/network/network_handshake.ml b/deku-p/src/core/network/network_handshake.ml index e892293a88..593509d9b7 100644 --- a/deku-p/src/core/network/network_handshake.ml +++ b/deku-p/src/core/network/network_handshake.ml @@ -5,7 +5,7 @@ module Challenge = struct type challenge = Challenge of { hash : BLAKE2b.t } type t = challenge - include BLAKE2b.With_b58_and_encoding (struct + include BLAKE2b.With_b58_and_encoding_and_yojson (struct let name = "Network_handshake.Challenge" let prefix = Deku_repr.Prefix.deku_handshake_challenge_hash end) diff --git a/deku-p/src/core/protocol/dune b/deku-p/src/core/protocol/dune index 5fbce8a8c9..460705c174 100644 --- a/deku-p/src/core/protocol/dune +++ b/deku-p/src/core/protocol/dune @@ -10,11 +10,13 @@ deku_tezos deku_ledger deku_metrics - ocaml_wasm_vm) + ocaml_wasm_vm + yojson) (preprocess (pps ppx_let_binding ppx_deriving.eq ppx_deriving.show ppx_deriving.ord - ppx_expect))) + ppx_expect + ppx_yojson_conv))) diff --git a/deku-p/src/core/protocol/nonce.ml b/deku-p/src/core/protocol/nonce.ml index 71318f987e..6c21cd6a8c 100644 --- a/deku-p/src/core/protocol/nonce.ml +++ b/deku-p/src/core/protocol/nonce.ml @@ -2,7 +2,7 @@ open Deku_stdlib (* TODO: should we prefix level in b58? *) type nonce = N.t -and t = nonce [@@deriving eq, ord, show] +and t = nonce [@@deriving eq, ord, show, yojson] (* repr *) let of_n n = n diff --git a/deku-p/src/core/protocol/nonce.mli b/deku-p/src/core/protocol/nonce.mli index f764435652..675c0f7af2 100644 --- a/deku-p/src/core/protocol/nonce.mli +++ b/deku-p/src/core/protocol/nonce.mli @@ -1,7 +1,7 @@ open Deku_stdlib type nonce -type t = nonce [@@deriving eq, ord, show] +type t = nonce [@@deriving eq, ord, show, yojson] (* repr *) val of_n : N.t -> nonce diff --git a/deku-p/src/core/protocol/operation.ml b/deku-p/src/core/protocol/operation.ml index 1b80d429b3..bd1a0d9cb2 100644 --- a/deku-p/src/core/protocol/operation.ml +++ b/deku-p/src/core/protocol/operation.ml @@ -25,7 +25,7 @@ type operation = } | Operation_noop of { sender : Address.t } -and t = operation [@@deriving show] +and t = operation [@@deriving show, yojson] let encoding = (* TODO: bench Data_encoding.union vs Data_encoding.matching*) @@ -154,9 +154,9 @@ module Initial = struct operation : operation; } - and t = initial_operation [@@deriving show] + and t = initial_operation [@@deriving show, yojson] - type hash_repr = Nonce.t * Level.t * operation + type hash_repr = Nonce.t * Level.t * operation [@@deriving yojson] let hash_encoding : hash_repr Data_encoding.t = let open Data_encoding in diff --git a/deku-p/src/core/protocol/operation.mli b/deku-p/src/core/protocol/operation.mli index eda6bfcfcd..71507b88c5 100644 --- a/deku-p/src/core/protocol/operation.mli +++ b/deku-p/src/core/protocol/operation.mli @@ -37,8 +37,8 @@ module Initial : sig operation : operation; } - type t = initial_operation [@@deriving show] - type hash_repr = Nonce.t * Level.t * operation + type t = initial_operation [@@deriving show, yojson] + type hash_repr = Nonce.t * Level.t * operation [@@deriving yojson] val hash_encoding : (Nonce.t * Level.t * operation) Data_encoding.t val encoding : t Data_encoding.t diff --git a/deku-p/src/core/protocol/operation_hash.ml b/deku-p/src/core/protocol/operation_hash.ml index 85a438518a..a778d4fdf2 100644 --- a/deku-p/src/core/protocol/operation_hash.ml +++ b/deku-p/src/core/protocol/operation_hash.ml @@ -7,7 +7,7 @@ and t = operation_hash [@@deriving eq, ord] let to_blake2b operation_hash = operation_hash -include With_b58_and_encoding (struct +include With_b58_and_encoding_and_yojson (struct let name = "Deku_protocol.Operation_hash" let prefix = Prefix.deku_operation_hash end) diff --git a/deku-p/src/core/protocol/operation_hash.mli b/deku-p/src/core/protocol/operation_hash.mli index d3d749782f..95df30427d 100644 --- a/deku-p/src/core/protocol/operation_hash.mli +++ b/deku-p/src/core/protocol/operation_hash.mli @@ -2,7 +2,7 @@ open Deku_stdlib open Deku_crypto type operation_hash -type t = operation_hash [@@deriving eq, ord, show] +type t = operation_hash [@@deriving eq, ord, show, yojson] (* repr *) val to_blake2b : operation_hash -> BLAKE2b.t diff --git a/deku-p/src/core/repr/deku_repr.ml b/deku-p/src/core/repr/deku_repr.ml index 513e220743..bd704af43b 100644 --- a/deku-p/src/core/repr/deku_repr.ml +++ b/deku-p/src/core/repr/deku_repr.ml @@ -19,6 +19,29 @@ struct let to_b58 t = Base58.simple_encode ~prefix ~to_raw t end +(* TODO: exceptions???*) +exception Not_a_string +exception Not_a_b58 + +(* TODO: this is clearly not in the right file *) +module With_yojson_of_b58 (P : sig + type t + + val of_b58 : string -> t option + val to_b58 : t -> string +end) = +struct + open P + + let t_of_yojson json = + match json with + | `String string -> ( + match of_b58 string with Some t -> t | None -> raise Not_a_b58) + | _ -> raise Not_a_string + + let yojson_of_t t = `String (to_b58 t) +end + (* TODO: this is dumb *) let rec decode_variant l string = match l with @@ -68,7 +91,7 @@ struct conv to_raw of_raw_exn (Fixed.string size)) end -module With_b58_and_encoding (P : sig +module With_b58_and_encoding_and_yojson (P : sig type t val name : string @@ -79,5 +102,13 @@ module With_b58_and_encoding (P : sig end) = struct include With_b58 (P) + + include With_yojson_of_b58 (struct + type t = P.t + + let of_b58 = of_b58 + let to_b58 = to_b58 + end) + include With_encoding (P) -end +end \ No newline at end of file diff --git a/deku-p/src/core/repr/deku_repr.mli b/deku-p/src/core/repr/deku_repr.mli index 7a0cfe5076..ee839b655b 100644 --- a/deku-p/src/core/repr/deku_repr.mli +++ b/deku-p/src/core/repr/deku_repr.mli @@ -39,7 +39,21 @@ end) : sig val to_b58 : P.t -> string end -module With_b58_and_encoding (P : sig +(* yojson exceptions *) +exception Not_a_string +exception Not_a_b58 + +module With_yojson_of_b58 (P : sig + type t + + val of_b58 : string -> t option + val to_b58 : t -> string +end) : sig + val t_of_yojson : [> `String of string ] -> P.t + val yojson_of_t : P.t -> [> `String of string ] +end + +module With_b58_and_encoding_and_yojson (P : sig type t val name : string @@ -51,6 +65,8 @@ end) : sig val encoding : P.t Data_encoding.t val of_b58 : string -> P.t option val to_b58 : P.t -> string + val t_of_yojson : [> `String of string ] -> P.t + val yojson_of_t : P.t -> [> `String of string ] end val decode_variant : (string -> 'a option) list -> string -> 'a option diff --git a/deku-p/src/core/repr/dune b/deku-p/src/core/repr/dune index a8c6f5d777..df6c2c85d9 100644 --- a/deku-p/src/core/repr/dune +++ b/deku-p/src/core/repr/dune @@ -1,5 +1,5 @@ (library (name deku_repr) - (libraries zarith mirage-crypto data-encoding) + (libraries zarith mirage-crypto data-encoding yojson) (preprocess - (pps ppx_deriving.eq ppx_deriving.ord))) + (pps ppx_deriving.eq ppx_deriving.ord ppx_yojson_conv))) diff --git a/deku-p/src/core/stdlib/deku_stdlib.ml b/deku-p/src/core/stdlib/deku_stdlib.ml index 46cc6b0eb1..eb3a1a77b6 100644 --- a/deku-p/src/core/stdlib/deku_stdlib.ml +++ b/deku-p/src/core/stdlib/deku_stdlib.ml @@ -6,3 +6,14 @@ module IO = Io include Let_syntax module Parallel = Parallel module List = List_ext + +module Yojson = struct + include Yojson + + module Safe = struct + include Safe + + let t_of_yojson t = t + let yojson_of_t t = t + end +end \ No newline at end of file diff --git a/deku-p/src/core/stdlib/deku_stdlib.mli b/deku-p/src/core/stdlib/deku_stdlib.mli index 183928a66a..a7a4364c50 100644 --- a/deku-p/src/core/stdlib/deku_stdlib.mli +++ b/deku-p/src/core/stdlib/deku_stdlib.mli @@ -11,3 +11,14 @@ module IO = Io module Parallel = Parallel module List = List_ext + +module Yojson : sig + include module type of Yojson + + module Safe : sig + include module type of Safe + + val t_of_yojson : t -> t + val yojson_of_t : t -> t + end +end \ No newline at end of file diff --git a/deku-p/src/core/stdlib/dune b/deku-p/src/core/stdlib/dune index daa17097e5..e52bd593f8 100644 --- a/deku-p/src/core/stdlib/dune +++ b/deku-p/src/core/stdlib/dune @@ -1,5 +1,5 @@ (library (name deku_stdlib) - (libraries deku_repr zarith uri base eio eio_main) + (libraries deku_repr zarith uri base eio eio_main yojson) (preprocess - (pps ppx_let_binding ppx_deriving.show ppx_deriving.eq ppx_deriving.ord))) + (pps ppx_let_binding ppx_deriving.show ppx_deriving.eq ppx_deriving.ord ppx_yojson_conv))) diff --git a/deku-p/src/core/stdlib/n.ml b/deku-p/src/core/stdlib/n.ml index 97e254d5eb..946ccb2c9b 100644 --- a/deku-p/src/core/stdlib/n.ml +++ b/deku-p/src/core/stdlib/n.ml @@ -1,7 +1,7 @@ open Z_ext type nat = Z_ext.t -and t = nat [@@deriving eq, ord] +and t = nat [@@deriving eq, ord, yojson] let show n = Format.asprintf "%a" pp_print n let pp = pp_print diff --git a/deku-p/src/core/stdlib/n.mli b/deku-p/src/core/stdlib/n.mli index 1923989a76..881c67f6c1 100644 --- a/deku-p/src/core/stdlib/n.mli +++ b/deku-p/src/core/stdlib/n.mli @@ -1,5 +1,5 @@ type nat -type t = nat [@@deriving show, eq, ord] +type t = nat [@@deriving show, eq, ord, yojson] exception Not_a_natural diff --git a/deku-p/src/core/stdlib/z_ext.ml b/deku-p/src/core/stdlib/z_ext.ml index 728e715403..df04d91dbf 100644 --- a/deku-p/src/core/stdlib/z_ext.ml +++ b/deku-p/src/core/stdlib/z_ext.ml @@ -1 +1,12 @@ include Z + +exception Not_a_string +exception Not_an_integer + +let t_of_yojson json = + match json with + | `String string -> ( + try Z.of_string string with Invalid_argument _ -> raise Not_an_integer) + | _ -> raise Not_a_string + +let yojson_of_t t = `String (Z.to_string t) \ No newline at end of file diff --git a/deku-p/src/core/stdlib/z_ext.mli b/deku-p/src/core/stdlib/z_ext.mli index 5bacadafc4..ac8adfbc17 100644 --- a/deku-p/src/core/stdlib/z_ext.mli +++ b/deku-p/src/core/stdlib/z_ext.mli @@ -1,3 +1,9 @@ include module type of struct include Z end + +exception Not_a_string +exception Not_an_integer + +val t_of_yojson : Yojson.Safe.t -> t +val yojson_of_t : t -> Yojson.Safe.t \ No newline at end of file diff --git a/deku-p/src/core/tezos/address.ml b/deku-p/src/core/tezos/address.ml index 87d39854a8..108402b904 100644 --- a/deku-p/src/core/tezos/address.ml +++ b/deku-p/src/core/tezos/address.ml @@ -4,7 +4,7 @@ open Deku_stdlib type t = | Implicit of Key_hash.t | Originated of { contract : Contract_hash.t; entrypoint : string option } -[@@deriving eq, ord, show] +[@@deriving eq, ord, show, yojson] let to_string = function | Implicit key_hash -> Key_hash.to_b58 key_hash diff --git a/deku-p/src/core/tezos/address.mli b/deku-p/src/core/tezos/address.mli index 45c3e36e9a..8d0dc53a11 100644 --- a/deku-p/src/core/tezos/address.mli +++ b/deku-p/src/core/tezos/address.mli @@ -3,7 +3,7 @@ open Deku_crypto type t = | Implicit of Key_hash.t | Originated of { contract : Contract_hash.t; entrypoint : string option } -[@@deriving eq, ord, show] +[@@deriving eq, ord, show, yojson] (* TODO: explain why this encoding? TLDR fixed size and no entrypoint *) val contract_encoding : t Data_encoding.t diff --git a/deku-p/src/core/tezos/contract_hash.ml b/deku-p/src/core/tezos/contract_hash.ml index be0784c411..eee281efc2 100644 --- a/deku-p/src/core/tezos/contract_hash.ml +++ b/deku-p/src/core/tezos/contract_hash.ml @@ -4,7 +4,7 @@ open BLAKE2b_160 type t = BLAKE2b_160.t [@@deriving eq, ord, show] -include With_b58_and_encoding (struct +include With_b58_and_encoding_and_yojson (struct let name = "Contract_hash" let prefix = Deku_repr.Prefix.contract_hash end) diff --git a/deku-p/src/core/tezos/contract_hash.mli b/deku-p/src/core/tezos/contract_hash.mli index 7f10a66ecb..d424b835e0 100644 --- a/deku-p/src/core/tezos/contract_hash.mli +++ b/deku-p/src/core/tezos/contract_hash.mli @@ -1,7 +1,7 @@ open Deku_crypto open BLAKE2b -type t = BLAKE2b_160.t [@@deriving eq, ord, show] +type t = BLAKE2b_160.t [@@deriving eq, ord, show, yojson] val encoding : t Data_encoding.t val to_b58 : t -> string diff --git a/deku-p/src/core/tezos/dune b/deku-p/src/core/tezos/dune index 44a40e6e6f..22e9b3948d 100644 --- a/deku-p/src/core/tezos/dune +++ b/deku-p/src/core/tezos/dune @@ -1,5 +1,5 @@ (library (name deku_tezos) - (libraries deku_stdlib deku_crypto tezos-micheline deku_concepts) + (libraries deku_stdlib deku_crypto tezos-micheline deku_concepts yojson) (preprocess - (pps ppx_deriving.eq ppx_deriving.ord ppx_let_binding ppx_deriving.show))) + (pps ppx_deriving.eq ppx_deriving.ord ppx_let_binding ppx_deriving.show ppx_yojson_conv))) diff --git a/deku-p/src/core/tezos/tezos_operation_hash.ml b/deku-p/src/core/tezos/tezos_operation_hash.ml index e48276fc55..1e6f05812d 100644 --- a/deku-p/src/core/tezos/tezos_operation_hash.ml +++ b/deku-p/src/core/tezos/tezos_operation_hash.ml @@ -5,7 +5,7 @@ open BLAKE2b type tezos_operation_hash = BLAKE2b.t [@@deriving eq, ord] type t = BLAKE2b.t [@@deriving eq, ord] -include With_b58_and_encoding (struct +include With_b58_and_encoding_and_yojson (struct let name = "Operation_hash" let prefix = Prefix.operation_hash end) diff --git a/nix/deku-p/deku.nix b/nix/deku-p/deku.nix index 12f31fd6bb..d8dcd0f33a 100644 --- a/nix/deku-p/deku.nix +++ b/nix/deku-p/deku.nix @@ -55,7 +55,7 @@ in runHook postBuild ''; - nativeBuildInputs = [nodejs removeReferencesTo] ++ npmPackages; + nativeBuildInputs = [nodejs removeReferencesTo core_bench] ++ npmPackages; checkPhase = '' runHook preInstall @@ -97,6 +97,9 @@ in core_unix ppx_expect ppx_deriving_encoding + core_bench + ppx_yojson_conv + yojson ] # checkInputs are here because when cross compiling dune needs test dependencies # but they are not available for the build phase. The issue can be seen by adding strictDeps = true;. From 824c2ecf941743afb8e0ca038c91d193c20c2e69 Mon Sep 17 00:00:00 2001 From: Gauthier SEBILLE Date: Mon, 26 Dec 2022 12:35:51 +0100 Subject: [PATCH 2/3] revert deriving yojson --- deku-c/wasm-vm-ocaml/dune | 5 +- deku-c/wasm-vm-ocaml/operation.ml | 14 - deku-c/wasm-vm-ocaml/operation.mli | 2 +- deku-c/wasm-vm-ocaml/operation_payload.ml | 2 +- deku-p/src/benchmarking/ADR.md | 506 ++++++++++++++++++ deku-p/src/benchmarking/benchmarking.ml | 59 +- deku-p/src/benchmarking/dune | 4 +- deku-p/src/benchmarking/readme.md | 51 -- deku-p/src/core/concepts/amount.ml | 2 +- deku-p/src/core/concepts/amount.mli | 2 +- deku-p/src/core/concepts/dune | 4 +- deku-p/src/core/concepts/level.ml | 2 +- deku-p/src/core/concepts/level.mli | 2 +- deku-p/src/core/consensus/block_hash.ml | 2 +- deku-p/src/core/crypto/BLAKE2b.ml | 16 +- deku-p/src/core/crypto/BLAKE2b.mli | 2 - deku-p/src/core/crypto/alg_intf.mli | 8 +- deku-p/src/core/crypto/dune | 5 +- deku-p/src/core/crypto/ed25519.ml | 10 +- deku-p/src/core/crypto/hash_intf.mli | 4 +- deku-p/src/core/crypto/key.ml | 7 - deku-p/src/core/crypto/key.mli | 2 +- deku-p/src/core/crypto/key_hash.ml | 13 +- deku-p/src/core/crypto/key_hash.mli | 2 +- deku-p/src/core/crypto/p256.ml | 8 +- deku-p/src/core/crypto/secp256k1.ml | 8 +- deku-p/src/core/crypto/signature.ml | 7 - deku-p/src/core/crypto/signature.mli | 2 +- deku-p/src/core/gossip/message_hash.ml | 2 +- deku-p/src/core/gossip/request_hash.ml | 2 +- deku-p/src/core/ledger/address.ml | 2 +- deku-p/src/core/ledger/address.mli | 2 +- deku-p/src/core/ledger/contract_address.ml | 2 +- deku-p/src/core/ledger/contract_address.mli | 2 +- deku-p/src/core/ledger/dune | 4 +- deku-p/src/core/ledger/ledger.ml | 2 +- deku-p/src/core/ledger/ticket_id.ml | 4 +- deku-p/src/core/ledger/ticket_id.mli | 2 +- deku-p/src/core/network/network_handshake.ml | 2 +- deku-p/src/core/protocol/dune | 6 +- deku-p/src/core/protocol/nonce.ml | 2 +- deku-p/src/core/protocol/nonce.mli | 2 +- deku-p/src/core/protocol/operation.ml | 6 +- deku-p/src/core/protocol/operation.mli | 4 +- deku-p/src/core/protocol/operation_hash.ml | 2 +- deku-p/src/core/protocol/operation_hash.mli | 2 +- deku-p/src/core/repr/deku_repr.ml | 35 +- deku-p/src/core/repr/deku_repr.mli | 18 +- deku-p/src/core/repr/dune | 4 +- deku-p/src/core/stdlib/deku_stdlib.ml | 11 - deku-p/src/core/stdlib/deku_stdlib.mli | 11 - deku-p/src/core/stdlib/dune | 4 +- deku-p/src/core/stdlib/n.ml | 2 +- deku-p/src/core/stdlib/n.mli | 2 +- deku-p/src/core/stdlib/z_ext.ml | 11 - deku-p/src/core/stdlib/z_ext.mli | 6 - deku-p/src/core/tezos/address.ml | 2 +- deku-p/src/core/tezos/address.mli | 2 +- deku-p/src/core/tezos/contract_hash.ml | 2 +- deku-p/src/core/tezos/contract_hash.mli | 2 +- deku-p/src/core/tezos/dune | 4 +- deku-p/src/core/tezos/tezos_operation_hash.ml | 2 +- nix/deku-p/deku.nix | 2 - 63 files changed, 613 insertions(+), 308 deletions(-) create mode 100644 deku-p/src/benchmarking/ADR.md delete mode 100644 deku-p/src/benchmarking/readme.md diff --git a/deku-c/wasm-vm-ocaml/dune b/deku-c/wasm-vm-ocaml/dune index b271c95b84..9a727e542a 100644 --- a/deku-c/wasm-vm-ocaml/dune +++ b/deku-c/wasm-vm-ocaml/dune @@ -8,7 +8,6 @@ deku_concepts digestif data-encoding - cstruct - yojson) + cstruct) (preprocess - (pps ppx_deriving.ord ppx_deriving.eq ppx_deriving.show ppx_let_binding ppx_yojson_conv))) + (pps ppx_deriving.ord ppx_deriving.eq ppx_deriving.show ppx_let_binding))) diff --git a/deku-c/wasm-vm-ocaml/operation.ml b/deku-c/wasm-vm-ocaml/operation.ml index 49da884e66..c7c8c2f3b0 100644 --- a/deku-c/wasm-vm-ocaml/operation.ml +++ b/deku-c/wasm-vm-ocaml/operation.ml @@ -78,17 +78,3 @@ let encoding = (fun (initial_storage, module_, constants, entrypoints) -> Originate { initial_storage; module_; constants; entrypoints }); ] - -let yojson_of_t t = - `String - (Data_encoding.Json.construct encoding t |> Data_encoding.Json.to_string) - -let t_of_yojson t = - match t with - | `String string -> ( - try - Data_encoding.Json.from_string string - |> Result.map (fun x -> Data_encoding.Json.destruct encoding x) - |> Result.get_ok - with Invalid_argument _ -> raise Not_an_operation) - | _ -> raise Not_an_operation \ No newline at end of file diff --git a/deku-c/wasm-vm-ocaml/operation.mli b/deku-c/wasm-vm-ocaml/operation.mli index ce3c86e463..7ff0e7a409 100644 --- a/deku-c/wasm-vm-ocaml/operation.mli +++ b/deku-c/wasm-vm-ocaml/operation.mli @@ -11,7 +11,7 @@ type t = entrypoints : Entrypoints.t; constants : (int * Value.t) array; } -[@@deriving show, yojson] +[@@deriving show] exception Not_an_operation diff --git a/deku-c/wasm-vm-ocaml/operation_payload.ml b/deku-c/wasm-vm-ocaml/operation_payload.ml index b3cdb0b6d0..06da3162d8 100644 --- a/deku-c/wasm-vm-ocaml/operation_payload.ml +++ b/deku-c/wasm-vm-ocaml/operation_payload.ml @@ -5,7 +5,7 @@ type t = { operation : Operation.t; tickets : (Ticket_id.t * Amount.t) list; [@opaque] } -[@@deriving show, yojson] +[@@deriving show] let encoding = let open Data_encoding in diff --git a/deku-p/src/benchmarking/ADR.md b/deku-p/src/benchmarking/ADR.md new file mode 100644 index 0000000000..bdfd1b6e79 --- /dev/null +++ b/deku-p/src/benchmarking/ADR.md @@ -0,0 +1,506 @@ +# Architecture Design Record + +The goal of the benchmarks is to validate that `Data_encoding` is a better choice than `Yojson` for encoding and decoding. + +# Run the benchmarks + +```bash +$ dune exec benchmarking + +Estimated testing time 2m40s (16 benchmarks x 10s). Change using '-quota'. +level to JSON/yojson: Total time taken 10.050047159194946s (1133 samples, max runs 1650007). +level to JSON/encoding: Total time taken 10.015674114227295s (1164 samples, max runs 2246186). +Encode_operation to JSON/yojson: Total time taken 10.008656024932861s (528 samples, max runs 4061). +Encode_operation to JSON/encoding: Total time taken 10.010571002960205s (648 samples, max runs 13285). +Operations to JSON/yojson: Total time taken 10.093136072158813s (472 samples, max runs 2347). +Operations to JSON/encoding: Total time taken 10.017661809921265s (538 samples, max runs 4481). +level from JSON/yojson: Total time taken 10.00353479385376s (968 samples, max runs 319531). +level from JSON/encoding: Total time taken 10.012985706329346s (899 samples, max runs 160843). +Encode_operation from JSON/yojson: Total time taken 10.066035985946655s (126 samples, max runs 126). +Encode_operation from JSON/encoding: Total time taken 10.131572008132935s (127 samples, max runs 127). +Operations from JSON/yojson: Total time taken 10.051375150680542s (125 samples, max runs 125). +Operations from JSON/encoding: Total time taken 10.132168769836426s (126 samples, max runs 126). +Operations from JSON + to_SIGNED/yojson: Total time taken 10.119817018508911s (124 samples, max runs 124). +Operations from JSON + to_SIGNED/encoding: Total time taken 10.039397239685059s (123 samples, max runs 123). +Operations from JSON + tup/encoding: Total time taken 10.01436185836792s (125 samples, max runs 125). +Operations from binary/encoding: Total time taken 10.070946216583252s (656 samples, max runs 14381). +┌───────────────────────────────────────────┬────────────────┬───────────────┬────────────┬────────────┬─────────────┬──────────┐ +│ Name │ Time/Run │ mWd/Run │ mjWd/Run │ Prom/Run │ mGC/Run │ mjGC/Run │ +├───────────────────────────────────────────┼────────────────┼───────────────┼────────────┼────────────┼─────────────┼──────────┤ +│ level to JSON/yojson │ 60.72ns │ 14.00w │ │ │ 0.05e-3 │ │ +│ level to JSON/encoding │ 44.62ns │ 49.00w │ │ │ 0.19e-3 │ │ +│ Encode_operation to JSON/yojson │ 23_642.19ns │ 15_284.51w │ 150.76w │ 150.76w │ 58.93e-3 │ 0.62e-3 │ +│ Encode_operation to JSON/encoding │ 7_413.65ns │ 2_869.19w │ 44.74w │ 44.74w │ 11.10e-3 │ 0.15e-3 │ +│ Operations to JSON/yojson │ 40_651.36ns │ 18_502.42w │ 219.23w │ 219.23w │ 71.28e-3 │ 0.70e-3 │ +│ Operations to JSON/encoding │ 21_527.34ns │ 5_912.69w │ 88.59w │ 88.59w │ 22.80e-3 │ 0.25e-3 │ +│ level from JSON/yojson │ 312.34ns │ 97.01w │ │ │ 0.37e-3 │ │ +│ level from JSON/encoding │ 620.68ns │ 514.98w │ │ │ 1.96e-3 │ │ +│ Encode_operation from JSON/yojson │ 1_275_840.60ns │ 1_548_138.67w │ 14_092.03w │ 14_092.03w │ 5_937.15e-3 │ 31.47e-3 │ +│ Encode_operation from JSON/encoding │ 1_266_042.20ns │ 1_547_947.82w │ 13_986.71w │ 13_986.71w │ 5_934.20e-3 │ 29.27e-3 │ +│ Operations from JSON/yojson │ 1_297_230.59ns │ 1_552_730.09w │ 14_409.96w │ 14_409.96w │ 5_951.25e-3 │ 28.11e-3 │ +│ Operations from JSON/encoding │ 1_285_833.38ns │ 1_553_656.25w │ 14_203.17w │ 14_203.17w │ 5_952.58e-3 │ 26.02e-3 │ +│ Operations from JSON + to_SIGNED/yojson │ 1_323_388.29ns │ 1_552_965.38w │ 14_464.86w │ 14_464.86w │ 5_948.93e-3 │ 25.02e-3 │ +│ Operations from JSON + to_SIGNED/encoding │ 1_339_803.92ns │ 1_553_819.52w │ 14_252.51w │ 14_252.51w │ 5_950.49e-3 │ 23.31e-3 │ +│ Operations from JSON + tup/encoding │ 1_286_578.90ns │ 1_557_523.78w │ 14_198.25w │ 14_198.25w │ 5_963.54e-3 │ 22.08e-3 │ +│ Operations from binary/encoding │ 6_904.63ns │ 1_155.98w │ 14.43w │ 14.43w │ 4.43e-3 │ 0.02e-3 │ +└───────────────────────────────────────────┴────────────────┴───────────────┴────────────┴────────────┴─────────────┴──────────┘ +``` + +# What had been tested? + +The main goal, is to have some data around `Yojson` VS `Data_encoding`, and also between `Data_encoding.Json` and `Data_encoding.Binary`. + +We haven't tested all the record types we defined in Deku, but only the record types, managed by the HTTP API, and which will be called a lot: + +- `/level`: a basic JSON like `{"level":"30"}` but it is called by the toolkit each time we want to submit an operation. +- `/encode-operation`: basically, adding the `0x80` prefix for Deku operations, to your submitted operation translated into hexadecimal. +- `/operations`: finally submit the operation to Deku + +## /level + +### Creating the types + +In order to avoid to expose `handlers.ml`, we redefined, inside `benchmarking.ml` the needed types from the HTTP API. + +We started by redefining the `Get_level` module, which is using the exposed `Level.t`: + +```OCaml +module Get_level = struct + type path = unit + type response = { level : Level.t } [@@deriving yojson] + + let response_encoding = + let open Data_encoding in + conv + (fun { level } -> level) + (fun level -> { level }) + (obj1 (req "level" Level.encoding)) +end +``` + +Since we have added `[@@deriving yojson]` to `response` type, we will need to add several `[@@deriving yojson]` in our code base. + +To do a real benchmark, we could have created some `generators` to generate a random `Level.t`, this can be done in a future if needed. For now, we will assume that encoding/decoding any value will have "similar" results. + +Hence, we create two different constants, which are representative to what we are going to do inside Deku code: + +```OCaml + let json_level_encoding = {| { "level" : 548430 } |} in + let level : Get_level.response = + Data_encoding.Json.from_string json_level_encoding + |> Result.get_ok + |> Data_encoding.Json.destruct Get_level.response_encoding + in +``` + +`json_level_encoding` is a quoted string representing the `Get_level.response` encoded into a JSON. We "cheat" and decode it to a `Get_level.reponse` using `Data_encoding`, our goal is not to test that the encoding is correct, but how fast it is. + +```OCaml + let json_level_yojson = {| { "level" : "18" } |} in +``` + +Finally, we defined the exact same `Get_level.response` but from a `yojson` version (TODO: this is only because the current branch doesn't contain a rc-6 commit, which is fixing this breaking change on the HTTP API, once this branch is rebased, remove the previous line). + +### Creating the tests + +As we want to compare `Yojson` VS `Data_encoding` we defined the two instances, by `[@@deriving yojson]` for `Yojson` and by defining a `response_encoding` for `Data_encoding`. + +We then want to test encoding a `Get_level.response` to a JSON, and decoding a JSON to a `Get_level.response`. To do that, using `Core_bench` we simply write the following: + +```OCaml +Bench.Test.create_group ~name:"level to JSON" + [ + Bench.Test.create ~name:"yojson" (fun () -> + ignore (Get_level.yojson_of_response level)); + Bench.Test.create ~name:"encoding" (fun () -> + ignore + (Data_encoding.Json.construct Get_level.response_encoding + level)); + ]; +Bench.Test.create_group ~name:"level from JSON" + [ + Bench.Test.create ~name:"yojson" (fun () -> + ignore + (Get_level.response_of_yojson + @@ Yojson.Safe.from_string json_level_yojson)); + Bench.Test.create ~name:"encoding" (fun () -> + ignore + (Data_encoding.Json.from_string json_level_encoding + |> Result.get_ok + |> Data_encoding.Json.destruct Get_level.response_encoding)); + ]; +``` + +We now have two groups test from `core_bench`: + +1. `Level to JSON` which contains two tests, creating a JSON from a `Get_level.response`: + + 1. ```OCaml + Get_level.yojson_of_response level + (* Creating a JSON from a Get_level.response with Yojson *) + ``` + + 2. ```OCaml + Data_encoding.Json.construct Get_level.response_encoding level + (* Creating a JSON from a Get_level.response with Data_encoding *) + ``` + +2. `Level from JSON` which also contains two tests, encoding a `Get_level.response` into a JSON: + + 1. ```OCaml + Get_level.response_of_yojson @@ Yojson.Safe.from_string json_level_yojson + (* Creating a Get_level.response from a JSON with Yojson *) + ``` + + 2. ```OCaml + Data_encoding.Json.from_string json_level_encoding + |> Result.get_ok + |> Data_encoding.Json.destruct Get_level.response_encoding + (* Creating a Get_level.response from a JSON with Data_encoding *) + ``` + +### Results of `to JSON` + +```bash +$ dune exec benchmarking + +level to JSON/yojson: Total time taken 10.050047159194946s (1133 samples, max runs 1650007). +level to JSON/encoding: Total time taken 10.015674114227295s (1164 samples, max runs 2246186). +┌──────────────────────────┬────────────┬───────────┬──────────┐ +│ Name │ Time/Run │ mWd/Run │ mGC/Run │ +├──────────────────────────┼────────────┼───────────┼──────────┤ +│ level to JSON/yojson │ 60.72ns │ 14.00w │ 0.05e-3 │ +│ level to JSON/encoding │ 44.62ns │ 49.00w │ 0.19e-3 │ +└──────────────────────────┴────────────┴───────────┴──────────┘ +``` + +Every figures are interesing. We now know that on a 10s run, we have encoded `1650007` at best from `Get_level.response` to the corresponding JSON using `Yojson`. + +But, in the same time, we can encode `2246186` at best using `Data_encoding`. + +Which leads to an average `time per run` lower by around 25% in favor of `Data_encoding`: 60.72ns for Yojson VS 44.62ns for Data_encoding. + +`mWd/Run` is the amount of words allocated on the minor heap, while `mGC/Run` is the number of `minor collections`. + +On our example, we see that `Data_encoding` is 25% faster than `Yojson`, but in the mean, time, it is using 3.5 times more words in the minor heap, and so, needing 3.8 times more minor collections. + +### Results of `from JSON` + +```bash +$ dune exec benchmarking + +level from JSON/yojson: Total time taken 10.00353479385376s (968 samples, max runs 319531). +level from JSON/encoding: Total time taken 10.012985706329346s (899 samples, max runs 160843). +┌──────────────────────────┬────────────┬───────────┬──────────┐ +│ Name │ Time/Run │ mWd/Run │ mGC/Run │ +├──────────────────────────┼────────────┼───────────┼──────────┤ +│ level to JSON/yojson │ 312.34ns │ 97.01w │ 0.37e-3 │ +│ level to JSON/encoding │ 620.68ns │ 514.98w │ 1.96e-3 │ +└──────────────────────────┴────────────┴───────────┴──────────┘ +``` + +Unlike encoding, decoding a `Get_level.response` is faster using `Yojson`: 312.34ns VS 620.68ns. Moreover, the other figures are also in favor of `Yojson`. + +### Conclusion + +**If we are only looking for performance, we should use `Data_encoding` for encoding into JSON, and `Yojson` to decode.** + +Let's continue with other types, to verify the results, or not! + +## /encode-operation + +```OCaml +module Encode_operation = struct + type body = Operation.Initial.hash_repr [@@deriving yojson] + + let body_encoding = Operation.Initial.hash_encoding +end +``` + +As we did for `Level` we simply added a `[@@deriving yojson]` on the `Operation.Initial.hash_repr` and everywhere it is needed. + +The corresponding `encoding` is already defined in `Operation.Initial`, so we are simply going to use it. + +### Creating the tests + +Once again, because the encoding of `level` is different between `Yojson` and `Data_encoding` (TODO: remove), we need to define two different sample: + +```OCaml +let json_encode_operation_encoding = + {| {"nonce":"495761182","level":289099,"operation":{"type":"vm_transaction","sender":"tz1KufAGaM2EM49bikm5VQfLNWT9rWsAWEHy","operation":{"operation":{"address":"DK1DpUMB44Ex3WXEUXPjp9DDkjiQ5cyvVwoU","argument":["Union",["Right",["Union",["Left",["Pair",[["Pair",[["Int","136331"],["String","tz1KufAGaM2EM49bikm5VQfLNWT9rWsAWEHy"]]],["Union",["Left",["Union",["Left",["Union",["Right",["Unit"]]]]]]]]]]]]]},"tickets":[]}}}|} +in +let encode_operation = + Data_encoding.Json.from_string json_encode_operation_encoding + |> Result.get_ok + |> Data_encoding.Json.destruct Encode_operation.body_encoding +in +let json_encode_operation_yojson = + Yojson.Safe.to_string (Encode_operation.yojson_of_body encode_operation) +in +``` + +Then, we now benchmark the time to encode and decode this `encode-operation` body: + +```OCaml +Bench.Test.create_group ~name:"Encode_operation to JSON" + [ + Bench.Test.create ~name:"yojson" (fun () -> + ignore (Encode_operation.yojson_of_body encode_operation)); + Bench.Test.create ~name:"encoding" (fun () -> + ignore + (Data_encoding.Json.construct Encode_operation.body_encoding + encode_operation)); + ]; +Bench.Test.create_group ~name:"Operations to JSON" + [ + Bench.Test.create ~name:"yojson" (fun () -> + ignore (Operations.yojson_of_t operation)); + Bench.Test.create ~name:"encoding" (fun () -> + ignore + (Data_encoding.Json.construct Operations.obj_encoding + operation)); + ]; +``` + +### Results of `to JSON` + +```bash +Encode_operation to JSON/yojson: Total time taken 10.008656024932861s (528 samples, max runs 4061). +Encode_operation to JSON/encoding: Total time taken 10.010571002960205s (648 samples, max runs 13285). +┌───────────────────────────────────────────┬────────────────┬───────────────┬────────────┬────────────┬─────────────┬──────────┐ +│ Name │ Time/Run │ mWd/Run │ mjWd/Run │ Prom/Run │ mGC/Run │ mjGC/Run │ +├───────────────────────────────────────────┼────────────────┼───────────────┼────────────┼────────────┼─────────────┼──────────┤ +│ Encode_operation to JSON/yojson │ 23_642.19ns │ 15_284.51w │ 150.76w │ 150.76w │ 58.93e-3 │ 0.62e-3 │ +│ Encode_operation to JSON/encoding │ 7_413.65ns │ 2_869.19w │ 44.74w │ 44.74w │ 11.10e-3 │ 0.15e-3 │ +└───────────────────────────────────────────┴────────────────┴───────────────┴────────────┴────────────┴─────────────┴──────────┘ + +``` + +Some new colums are displayed, because these two tests make significant changes on these values, which was not the case for the `Get_level.response`. + +As already seen in the previous tests, `Data_encoding` is around 3.2 times faster to encode such a type, but unlike `Get_level.response`, it is also allocating less `minor words` into the heap and ~5.3 times less and needing ~5.3 times less minor collections. + +The new colums are: + +- `mjWd/Run`: amount of words allocated in the major heap +- `Prom/Run`: +- `mjGC/Run`: amount of major collections + +For all these columns, the figures are also in favor of `Data_encoding. + +### Results of `from JSON` + +```bash +Encode_operation from JSON/yojson: Total time taken 10.066035985946655s (126 samples, max runs 126). +Encode_operation from JSON/encoding: Total time taken 10.131572008132935s (127 samples, max runs 127). +┌───────────────────────────────────────────┬────────────────┬───────────────┬────────────┬────────────┬─────────────┬──────────┐ +│ Name │ Time/Run │ mWd/Run │ mjWd/Run │ Prom/Run │ mGC/Run │ mjGC/Run │ +├───────────────────────────────────────────┼────────────────┼───────────────┼────────────┼────────────┼─────────────┼──────────┤ +│ Encode_operation from JSON/yojson │ 1_275_840.60ns │ 1_548_138.67w │ 14_092.03w │ 14_092.03w │ 5_937.15e-3 │ 31.47e-3 │ +│ Encode_operation from JSON/encoding │ 1_266_042.20ns │ 1_547_947.82w │ 13_986.71w │ 13_986.71w │ 5_934.20e-3 │ 29.27e-3 │ +└───────────────────────────────────────────┴────────────────┴───────────────┴────────────┴────────────┴─────────────┴──────────┘ +``` + +Unlike the previous figures, there is not significant "winner" in this match. `Data_encoding` is globally a bit better, but it is really closed. + +### Conclusion + +Benchmarking this "complex" record type had lead to display new columns of `core_bench` result because of use of the major heap. + +**Looking for performance, `Data_encoding` is better than `Yojson`** + +## /operations + +This endpoint is used to submit an operation to Deku. Hence, it is called a lot, and since it is carrying a lot of data, it is interesting to benchmark its body. + +On this body, we also want to benchmark: + +- `obj` and `tup` from `Data_encoding`. +- If the verification of the signature is taking a lot of time or of resources. +- `Data_encoding.Json` VS `Data_encoding.Binary` + +### Creating the types + +```OCaml +module Operations = struct + type t = { + key : Key.t; + signature : Signature.t; + initial : Operation.Initial.t; + } + [@@deriving yojson] + + let obj_encoding = + let open Data_encoding in + conv + (fun { key; signature; initial } -> (key, signature, initial)) + (fun (key, signature, initial) -> { key; signature; initial }) + (obj3 (req "key" Key.encoding) + (req "signature" Signature.encoding) + (req "initial" Operation.Initial.encoding)) + + let tup_encoding = + let open Data_encoding in + conv + (fun { key; signature; initial } -> (key, signature, initial)) + (fun (key, signature, initial) -> { key; signature; initial }) + (tup3 Key.encoding Signature.encoding Operation.Initial.encoding) + + let to_signed repr = + let { key; signature; initial } = repr in + Operation.Signed.make_with_signature ~key ~signature ~initial +end +``` + +Once again, we duplicate the body type from `operations` endpoint in `Handlers.ml`, then adding `[@@deriving yojson]` everywhere it is needed in our Deku code base. + +Then we define two different encoding functions: + +- `obj_encoding` using `obj` from `Data_encoding` +- `tup_encoding` using `tup` from `Data_encoding` + +And finally, we define the exact same `to_signed` function, which verifiy the signature. + +### Creating the tests + +Then, like for the other tests, we need to define the sample data we are going to encode and decode: + +```OCaml +let json_operation_encoding = + {| {"key":"edpkuUADDrRjMHLq9ehcWJev6Gd9YLTX38vgXSizzghJ9NS5ceNg8v","signature":"edsigu48RihHyRN7XRm8YjdLzCcF8rMRwF6WWTzGcU3jQYguQhCCv1thksnMSrbEwEhHnRUovfKoSYLaacT6ek3iJLuBgfaARL2","initial":{"nonce":"735632897","level":291078,"operation":{"type":"vm_transaction","sender":"tz1KufAGaM2EM49bikm5VQfLNWT9rWsAWEHy","operation":{"operation":{"address":"DK1DpUMB44Ex3WXEUXPjp9DDkjiQ5cyvVwoU","argument":["Union",["Right",["Union",["Left",["Pair",[["Pair",[["Int","136331"],["String","tz1KufAGaM2EM49bikm5VQfLNWT9rWsAWEHy"]]],["Union",["Left",["Union",["Left",["Union",["Right",["Unit"]]]]]]]]]]]]]},"tickets":[]}}}}|} +in +let operation = + Data_encoding.Json.from_string json_operation_encoding + |> Result.get_ok + |> Data_encoding.Json.destruct Operations.obj_encoding +in +let binary_operation : string = + Data_encoding.Binary.to_string_opt Operations.obj_encoding operation + |> Option.get +in +let json_operation_yojson = + Operations.yojson_of_t operation |> Yojson.Safe.to_string +in +let json_operation_tup = + Data_encoding.Json.to_string + (Data_encoding.Json.construct Operations.tup_encoding operation) +in +``` + +And measure everything with `core_bench`: + +```OCaml +Bench.Test.create_group ~name:"Operations to JSON" + [ + Bench.Test.create ~name:"yojson" (fun () -> + ignore (Operations.yojson_of_t operation)); + Bench.Test.create ~name:"encoding" (fun () -> + ignore + (Data_encoding.Json.construct Operations.obj_encoding + operation)); + ]; +Bench.Test.create_group ~name:"Operations from JSON" + [ + Bench.Test.create ~name:"yojson" (fun () -> + ignore + (Operations.t_of_yojson + @@ Yojson.Safe.from_string json_operation_yojson)); + Bench.Test.create ~name:"encoding" (fun () -> + ignore + (Data_encoding.Json.from_string json_operation_encoding + |> Result.get_ok + |> Data_encoding.Json.destruct Operations.obj_encoding)); + ]; +Bench.Test.create_group ~name:"Operations from JSON + to_SIGNED" + [ + Bench.Test.create ~name:"yojson" (fun () -> + ignore + (Operations.to_signed @@ Operations.t_of_yojson + @@ Yojson.Safe.from_string json_operation_yojson)); + Bench.Test.create ~name:"encoding" (fun () -> + ignore + (Data_encoding.Json.from_string json_operation_encoding + |> Result.get_ok + |> Data_encoding.Json.destruct Operations.obj_encoding + |> Operations.to_signed)); + ]; +Bench.Test.create_group ~name:"Operations from JSON + tup" + [ + Bench.Test.create ~name:"encoding" (fun () -> + ignore + (Data_encoding.Json.from_string json_operation_tup + |> Result.get_ok + |> Data_encoding.Json.destruct Operations.tup_encoding)); + ]; +Bench.Test.create_group ~name:"Operations from binary" + [ + Bench.Test.create ~name:"encoding" (fun () -> + ignore + (Data_encoding.Binary.of_string_exn Operations.obj_encoding + binary_operation)); + ]; +``` + +### Results of `to JSON` + +```bash +Operations to JSON/yojson: Total time taken 10.093136072158813s (472 samples, max runs 2347). +Operations to JSON/encoding: Total time taken 10.017661809921265s (538 samples, max runs 4481). +┌───────────────────────────────────────────┬────────────────┬───────────────┬────────────┬────────────┬─────────────┬──────────┐ +│ Name │ Time/Run │ mWd/Run │ mjWd/Run │ Prom/Run │ mGC/Run │ mjGC/Run │ +├───────────────────────────────────────────┼────────────────┼───────────────┼────────────┼────────────┼─────────────┼──────────┤ +│ Operations to JSON/yojson │ 40_651.36ns │ 18_502.42w │ 219.23w │ 219.23w │ 71.28e-3 │ 0.70e-3 │ +│ Operations to JSON/encoding │ 21_527.34ns │ 5_912.69w │ 88.59w │ 88.59w │ 22.80e-3 │ 0.25e-3 │ +└───────────────────────────────────────────┴────────────────┴───────────────┴────────────┴────────────┴─────────────┴──────────┘ +``` + +Once again, this is a victory for `Data_encoding`, wich is twice ~2 times faster, and also needing less words in minor and major heap, and so, less collections. + +### Results of `from JSON` + +```bash +Operations from JSON/yojson: Total time taken 10.051375150680542s (125 samples, max runs 125). +Operations from JSON/encoding: Total time taken 10.132168769836426s (126 samples, max runs 126). +Operations from JSON + to_SIGNED/yojson: Total time taken 10.119817018508911s (124 samples, max runs 124). +Operations from JSON + to_SIGNED/encoding: Total time taken 10.039397239685059s (123 samples, max runs 123). +Operations from JSON + tup/encoding: Total time taken 10.01436185836792s (125 samples, max runs 125). +Operations from binary/encoding: Total time taken 10.070946216583252s (656 samples, max runs 14381). +┌───────────────────────────────────────────┬────────────────┬───────────────┬────────────┬────────────┬─────────────┬──────────┐ +│ Name │ Time/Run │ mWd/Run │ mjWd/Run │ Prom/Run │ mGC/Run │ mjGC/Run │ +├───────────────────────────────────────────┼────────────────┼───────────────┼────────────┼────────────┼─────────────┼──────────┤ +│ Operations from JSON/yojson │ 1_297_230.59ns │ 1_552_730.09w │ 14_409.96w │ 14_409.96w │ 5_951.25e-3 │ 28.11e-3 │ +│ Operations from JSON/encoding │ 1_285_833.38ns │ 1_553_656.25w │ 14_203.17w │ 14_203.17w │ 5_952.58e-3 │ 26.02e-3 │ +│ Operations from JSON + to_SIGNED/yojson │ 1_323_388.29ns │ 1_552_965.38w │ 14_464.86w │ 14_464.86w │ 5_948.93e-3 │ 25.02e-3 │ +│ Operations from JSON + to_SIGNED/encoding │ 1_339_803.92ns │ 1_553_819.52w │ 14_252.51w │ 14_252.51w │ 5_950.49e-3 │ 23.31e-3 │ +│ Operations from JSON + tup/encoding │ 1_286_578.90ns │ 1_557_523.78w │ 14_198.25w │ 14_198.25w │ 5_963.54e-3 │ 22.08e-3 │ +│ Operations from binary/encoding │ 6_904.63ns │ 1_155.98w │ 14.43w │ 14.43w │ 4.43e-3 │ 0.02e-3 │ +└───────────────────────────────────────────┴────────────────┴───────────────┴────────────┴────────────┴─────────────┴──────────┘ +``` + +#### Yojson VS Data_encoding + +There is no significant winner, but `Data_encoding` is faster than `Yojson` + +#### With `to_signed` VS without `to_signed` + +Unlike previously, when calling `to_signed` function, `Yojson` is faster than `Data_encoding`. In the other hand, there is not a significant win by avoinding to call `to_signed`, 1_285_833.38ns with `Data_encoding` without `to_signed` VS 1_339_803.92ns with `Data_encoding` with `to_signed` + +#### `obj` VS `tup` + +1_285_833.38ns for `obj_encoding` and 1_286_578.90ns for `tup_encoding`, and other figures are also in favor of `obj`. + +#### `Data_encoding.Json` VS `Data_encoding.Binary` + +This is a clear win for `Data_encoding.Binary`, where the average `Time/Run` is 6_904.63ns, when it is 1_285_833.38ns for the `Data_encoding.Json`! + +### Conclusion + +Once again, `Data_encoding.Json` was globally better than `Yojson`. + +Inside `Data_encoding`, there is no win to switch from `obj` to `tup. + +As expected, decoding with calling the `to_signed` function needs more time and resources than decoding without it. But the win is not significant to remove it. + +**The most interesting figure, is about `Data_encoding.Json` VS `Data_encoding.Binary`. This is a clear and neat win for `Data_encoding.Binary`.** + +**If we want to improve performances on our HTTP API, we could/should switch the `/operations` endpoint to `Data_encoding.Binary`, however, this would imply to provide a decode/encode function to `Data_encoding.Binary` in our toolkit and without calling our API (because if we simply move it to an other endpoint, we simply move the results).** diff --git a/deku-p/src/benchmarking/benchmarking.ml b/deku-p/src/benchmarking/benchmarking.ml index 4bbc62806c..8019875152 100644 --- a/deku-p/src/benchmarking/benchmarking.ml +++ b/deku-p/src/benchmarking/benchmarking.ml @@ -5,7 +5,7 @@ open Deku_crypto module Get_level = struct type path = unit - type response = { level : Level.t } [@@deriving yojson] + type response = { level : Level.t } let response_encoding = let open Data_encoding in @@ -16,7 +16,7 @@ module Get_level = struct end module Encode_operation = struct - type body = Operation.Initial.hash_repr [@@deriving yojson] + type body = Operation.Initial.hash_repr let body_encoding = Operation.Initial.hash_encoding end @@ -27,7 +27,6 @@ module Operations = struct signature : Signature.t; initial : Operation.Initial.t; } - [@@deriving yojson] let obj_encoding = let open Data_encoding in @@ -51,17 +50,13 @@ module Operations = struct end let () = + let json_level_encoding = {| { "level" : 548430 } |} in let level : Get_level.response = - { - level = - Level.next @@ Level.next @@ Level.next @@ Level.next @@ Level.next - @@ Level.next @@ Level.next @@ Level.next @@ Level.next @@ Level.next - @@ Level.next @@ Level.next @@ Level.next @@ Level.next @@ Level.next - @@ Level.next @@ Level.next @@ Level.next Level.zero; - } + Data_encoding.Json.from_string json_level_encoding + |> Result.get_ok + |> Data_encoding.Json.destruct Get_level.response_encoding in - let json_level_yojson = {| { "level" : "18" } |} in - let json_level_encoding = {| { "level" : 18 } |} in + (* let json_level_yojson = {| { "level" : "548430" } |} in *) let json_encode_operation_encoding = {| {"nonce":"495761182","level":289099,"operation":{"type":"vm_transaction","sender":"tz1KufAGaM2EM49bikm5VQfLNWT9rWsAWEHy","operation":{"operation":{"address":"DK1DpUMB44Ex3WXEUXPjp9DDkjiQ5cyvVwoU","argument":["Union",["Right",["Union",["Left",["Pair",[["Pair",[["Int","136331"],["String","tz1KufAGaM2EM49bikm5VQfLNWT9rWsAWEHy"]]],["Union",["Left",["Union",["Left",["Union",["Right",["Unit"]]]]]]]]]]]]]},"tickets":[]}}}|} in @@ -70,9 +65,9 @@ let () = |> Result.get_ok |> Data_encoding.Json.destruct Encode_operation.body_encoding in - let json_encode_operation_yojson = - Yojson.Safe.to_string (Encode_operation.yojson_of_body encode_operation) - in + (* let json_encode_operation_yojson = + Yojson.Safe.to_string (Encode_operation.yojson_of_body encode_operation) + in *) let json_operation_encoding = {| {"key":"edpkuUADDrRjMHLq9ehcWJev6Gd9YLTX38vgXSizzghJ9NS5ceNg8v","signature":"edsigu48RihHyRN7XRm8YjdLzCcF8rMRwF6WWTzGcU3jQYguQhCCv1thksnMSrbEwEhHnRUovfKoSYLaacT6ek3iJLuBgfaARL2","initial":{"nonce":"735632897","level":291078,"operation":{"type":"vm_transaction","sender":"tz1KufAGaM2EM49bikm5VQfLNWT9rWsAWEHy","operation":{"operation":{"address":"DK1DpUMB44Ex3WXEUXPjp9DDkjiQ5cyvVwoU","argument":["Union",["Right",["Union",["Left",["Pair",[["Pair",[["Int","136331"],["String","tz1KufAGaM2EM49bikm5VQfLNWT9rWsAWEHy"]]],["Union",["Left",["Union",["Left",["Union",["Right",["Unit"]]]]]]]]]]]]]},"tickets":[]}}}}|} in @@ -85,9 +80,9 @@ let () = Data_encoding.Binary.to_string_opt Operations.obj_encoding operation |> Option.get in - let json_operation_yojson = - Operations.yojson_of_t operation |> Yojson.Safe.to_string - in + (* let json_operation_yojson = + Operations.yojson_of_t operation |> Yojson.Safe.to_string + in *) let json_operation_tup = Data_encoding.Json.to_string (Data_encoding.Json.construct Operations.tup_encoding operation) @@ -104,8 +99,8 @@ let () = [ Bench.Test.create_group ~name:"level to JSON" [ - Bench.Test.create ~name:"yojson" (fun () -> - ignore (Get_level.yojson_of_response level)); + (* Bench.Test.create ~name:"yojson" (fun () -> + ignore (Get_level.yojson_of_response level)); *) Bench.Test.create ~name:"encoding" (fun () -> ignore (Data_encoding.Json.construct Get_level.response_encoding @@ -113,8 +108,8 @@ let () = ]; Bench.Test.create_group ~name:"Encode_operation to JSON" [ - Bench.Test.create ~name:"yojson" (fun () -> - ignore (Encode_operation.yojson_of_body encode_operation)); + (* Bench.Test.create ~name:"yojson" (fun () -> + ignore (Encode_operation.yojson_of_body encode_operation)); *) Bench.Test.create ~name:"encoding" (fun () -> ignore (Data_encoding.Json.construct Encode_operation.body_encoding @@ -122,8 +117,8 @@ let () = ]; Bench.Test.create_group ~name:"Operations to JSON" [ - Bench.Test.create ~name:"yojson" (fun () -> - ignore (Operations.yojson_of_t operation)); + (* Bench.Test.create ~name:"yojson" (fun () -> + ignore (Operations.yojson_of_t operation)); *) Bench.Test.create ~name:"encoding" (fun () -> ignore (Data_encoding.Json.construct Operations.obj_encoding @@ -131,10 +126,10 @@ let () = ]; Bench.Test.create_group ~name:"level from JSON" [ - Bench.Test.create ~name:"yojson" (fun () -> + (* Bench.Test.create ~name:"yojson" (fun () -> ignore (Get_level.response_of_yojson - @@ Yojson.Safe.from_string json_level_yojson)); + @@ Yojson.Safe.from_string json_level_yojson)); *) Bench.Test.create ~name:"encoding" (fun () -> ignore (Data_encoding.Json.from_string json_level_encoding @@ -143,10 +138,10 @@ let () = ]; Bench.Test.create_group ~name:"Encode_operation from JSON" [ - Bench.Test.create ~name:"yojson" (fun () -> + (* Bench.Test.create ~name:"yojson" (fun () -> ignore (Encode_operation.body_of_yojson - @@ Yojson.Safe.from_string json_encode_operation_yojson)); + @@ Yojson.Safe.from_string json_encode_operation_yojson)); *) Bench.Test.create ~name:"encoding" (fun () -> ignore (Data_encoding.Json.from_string json_encode_operation_encoding @@ -156,10 +151,10 @@ let () = ]; Bench.Test.create_group ~name:"Operations from JSON" [ - Bench.Test.create ~name:"yojson" (fun () -> + (* Bench.Test.create ~name:"yojson" (fun () -> ignore (Operations.t_of_yojson - @@ Yojson.Safe.from_string json_operation_yojson)); + @@ Yojson.Safe.from_string json_operation_yojson)); *) Bench.Test.create ~name:"encoding" (fun () -> ignore (Data_encoding.Json.from_string json_operation_encoding @@ -168,10 +163,10 @@ let () = ]; Bench.Test.create_group ~name:"Operations from JSON + to_SIGNED" [ - Bench.Test.create ~name:"yojson" (fun () -> + (* Bench.Test.create ~name:"yojson" (fun () -> ignore (Operations.to_signed @@ Operations.t_of_yojson - @@ Yojson.Safe.from_string json_operation_yojson)); + @@ Yojson.Safe.from_string json_operation_yojson)); *) Bench.Test.create ~name:"encoding" (fun () -> ignore (Data_encoding.Json.from_string json_operation_encoding diff --git a/deku-p/src/benchmarking/dune b/deku-p/src/benchmarking/dune index 19d03ed67a..86d4a247eb 100644 --- a/deku-p/src/benchmarking/dune +++ b/deku-p/src/benchmarking/dune @@ -1,6 +1,4 @@ (executable (public_name benchmarking) (name benchmarking) - (libraries deku_concepts deku_protocol fmt.tty core core_bench core_unix.command_unix yojson) - (preprocess - (pps ppx_yojson_conv))) + (libraries deku_concepts deku_protocol fmt.tty core core_bench core_unix.command_unix)) diff --git a/deku-p/src/benchmarking/readme.md b/deku-p/src/benchmarking/readme.md deleted file mode 100644 index 9f4d423e81..0000000000 --- a/deku-p/src/benchmarking/readme.md +++ /dev/null @@ -1,51 +0,0 @@ -# Run the benchmarks - -```bash -$ dune exec benchmarking - -Estimated testing time 2m40s (16 benchmarks x 10s). Change using '-quota'. -level to JSON/yojson: Total time taken 10.032530069351196s (1143 samples, max runs 1822631). -level to JSON/encoding: Total time taken 10.098333120346069s (1171 samples, max runs 2408211). -Encode_operation to JSON/yojson: Total time taken 10.000631093978882s (527 samples, max runs 4021). -Encode_operation to JSON/encoding: Total time taken 10.075474977493286s (648 samples, max runs 13285). -Operations to JSON/yojson: Total time taken 10.07265305519104s (469 samples, max runs 2279). -Operations to JSON/encoding: Total time taken 10.0344078540802s (538 samples, max runs 4481). -level from JSON/yojson: Total time taken 10.066639184951782s (976 samples, max runs 346003). -level from JSON/encoding: Total time taken 10.045109033584595s (909 samples, max runs 177666). -Encode_operation from JSON/yojson: Total time taken 10.079186201095581s (124 samples, max runs 124). -Encode_operation from JSON/encoding: Total time taken 10.10786509513855s (125 samples, max runs 125). -Operations from JSON/yojson: Total time taken 10.132002115249634s (124 samples, max runs 124). -Operations from JSON/encoding: Total time taken 10.004235744476318s (124 samples, max runs 124). -Operations from JSON + to_SIGNED/yojson: Total time taken 10.158401012420654s (122 samples, max runs 122). -Operations from JSON + to_SIGNED/encoding: Total time taken 10.160748958587646s (123 samples, max runs 123). -Operations from JSON + tup/encoding: Total time taken 10.052727937698364s (124 samples, max runs 124). -Operations from binary/encoding: Total time taken 10.023288249969482s (655 samples, max runs 14239). -┌───────────────────────────────────────────┬────────────────┬───────────────┬────────────┬────────────┬─────────────┬──────────┐ -│ Name │ Time/Run │ mWd/Run │ mjWd/Run │ Prom/Run │ mGC/Run │ mjGC/Run │ -├───────────────────────────────────────────┼────────────────┼───────────────┼────────────┼────────────┼─────────────┼──────────┤ -│ level to JSON/yojson │ 54.92ns │ 14.00w │ │ │ 0.05e-3 │ │ -│ level to JSON/encoding │ 41.93ns │ 49.00w │ │ │ 0.19e-3 │ │ -│ Encode_operation to JSON/yojson │ 23_961.44ns │ 15_285.29w │ 150.77w │ 150.77w │ 58.94e-3 │ 0.63e-3 │ -│ Encode_operation to JSON/encoding │ 7_480.26ns │ 2_869.19w │ 44.74w │ 44.74w │ 11.10e-3 │ 0.15e-3 │ -│ Operations to JSON/yojson │ 41_533.79ns │ 18_500.14w │ 219.22w │ 219.22w │ 71.27e-3 │ 0.70e-3 │ -│ Operations to JSON/encoding │ 21_624.05ns │ 5_912.69w │ 88.59w │ 88.59w │ 22.81e-3 │ 0.25e-3 │ -│ level from JSON/yojson │ 290.44ns │ 96.00w │ │ │ 0.37e-3 │ │ -│ level from JSON/encoding │ 563.94ns │ 486.00w │ 0.17w │ 0.17w │ 1.86e-3 │ │ -│ Encode_operation from JSON/yojson │ 1_322_918.03ns │ 1_548_192.73w │ 14_095.65w │ 14_095.65w │ 5_937.22e-3 │ 31.44e-3 │ -│ Encode_operation from JSON/encoding │ 1_306_432.95ns │ 1_547_962.53w │ 13_982.59w │ 13_982.59w │ 5_934.08e-3 │ 29.13e-3 │ -│ Operations from JSON/yojson │ 1_327_051.61ns │ 1_552_779.10w │ 14_413.98w │ 14_413.98w │ 5_951.41e-3 │ 28.18e-3 │ -│ Operations from JSON/encoding │ 1_309_895.89ns │ 1_553_628.50w │ 14_203.58w │ 14_203.58w │ 5_952.61e-3 │ 26.10e-3 │ -│ Operations from JSON + to_SIGNED/yojson │ 1_379_044.45ns │ 1_552_898.45w │ 14_461.49w │ 14_461.49w │ 5_948.82e-3 │ 25.05e-3 │ -│ Operations from JSON + to_SIGNED/encoding │ 1_352_071.62ns │ 1_553_819.52w │ 14_252.51w │ 14_252.51w │ 5_950.50e-3 │ 23.32e-3 │ -│ Operations from JSON + tup/encoding │ 1_316_792.64ns │ 1_557_600.61w │ 14_198.52w │ 14_198.52w │ 5_963.62e-3 │ 22.02e-3 │ -│ Operations from binary/encoding │ 6_952.12ns │ 1_155.88w │ 14.43w │ 14.43w │ 4.43e-3 │ 0.02e-3 │ -└───────────────────────────────────────────┴────────────────┴───────────────┴────────────┴────────────┴─────────────┴──────────┘ -``` - -# What had been tested? - -TODO - -# Understading the results - -TODO diff --git a/deku-p/src/core/concepts/amount.ml b/deku-p/src/core/concepts/amount.ml index af01b63f41..2fa6051e93 100644 --- a/deku-p/src/core/concepts/amount.ml +++ b/deku-p/src/core/concepts/amount.ml @@ -2,7 +2,7 @@ open Deku_stdlib open N type amount = N.t -and t = amount [@@deriving show, eq, ord, yojson] +and t = amount [@@deriving show, eq, ord] let zero = zero let one = one diff --git a/deku-p/src/core/concepts/amount.mli b/deku-p/src/core/concepts/amount.mli index f2db82b557..5a6859f84d 100644 --- a/deku-p/src/core/concepts/amount.mli +++ b/deku-p/src/core/concepts/amount.mli @@ -1,7 +1,7 @@ open Deku_stdlib type amount -type t = amount [@@deriving show, eq, ord, yojson] +type t = amount [@@deriving show, eq, ord] val zero : amount val one : amount diff --git a/deku-p/src/core/concepts/dune b/deku-p/src/core/concepts/dune index 13b0e405a8..611c199019 100644 --- a/deku-p/src/core/concepts/dune +++ b/deku-p/src/core/concepts/dune @@ -1,5 +1,5 @@ (library (name deku_concepts) - (libraries deku_stdlib deku_crypto yojson) + (libraries deku_stdlib deku_crypto) (preprocess - (pps ppx_deriving.show ppx_deriving.eq ppx_deriving.ord ppx_yojson_conv))) + (pps ppx_deriving.show ppx_deriving.eq ppx_deriving.ord))) diff --git a/deku-p/src/core/concepts/level.ml b/deku-p/src/core/concepts/level.ml index 775348c058..36efce8cfb 100644 --- a/deku-p/src/core/concepts/level.ml +++ b/deku-p/src/core/concepts/level.ml @@ -3,7 +3,7 @@ open N (* TODO: should we prefix level in b58? *) type level = N.t -and t = level [@@deriving show, eq, ord, yojson] +and t = level [@@deriving show, eq, ord] let zero = zero let next x = x + one diff --git a/deku-p/src/core/concepts/level.mli b/deku-p/src/core/concepts/level.mli index cd5b2cba86..a04bcfc816 100644 --- a/deku-p/src/core/concepts/level.mli +++ b/deku-p/src/core/concepts/level.mli @@ -1,7 +1,7 @@ open Deku_stdlib type level -type t = level [@@deriving show, eq, ord, yojson] +type t = level [@@deriving show, eq, ord] val zero : level val next : level -> level diff --git a/deku-p/src/core/consensus/block_hash.ml b/deku-p/src/core/consensus/block_hash.ml index eb980c04f5..99843ed770 100644 --- a/deku-p/src/core/consensus/block_hash.ml +++ b/deku-p/src/core/consensus/block_hash.ml @@ -8,7 +8,7 @@ and t = block_hash [@@deriving eq, ord] let of_blake2b hash = hash let to_blake2b operation_hash = operation_hash -include With_b58_and_encoding_and_yojson (struct +include With_b58_and_encoding (struct let name = "Deku_consensus.Block_hash" let prefix = Prefix.deku_block_hash end) diff --git a/deku-p/src/core/crypto/BLAKE2b.ml b/deku-p/src/core/crypto/BLAKE2b.ml index eeb2eab07b..327054804d 100644 --- a/deku-p/src/core/crypto/BLAKE2b.ml +++ b/deku-p/src/core/crypto/BLAKE2b.ml @@ -41,12 +41,12 @@ struct Alg.verify key signature (to_raw_string hash) end - module With_b58_and_encoding_and_yojson (P : sig + module With_b58_and_encoding (P : sig val name : string val prefix : Prefix.t end) = struct - include With_b58_and_encoding_and_yojson (struct + include With_b58_and_encoding (struct include P type t = hash @@ -91,15 +91,3 @@ let encoding = | Some hash -> hash | None -> failwith "impossible to decode") string - -let yojson_of_t t = `String (to_hex t) - -let t_of_yojson json = - (* TODO: Is this warning correct? *) - Logs.warn (fun m -> - m "error, an 'untyped' hash is circulating on the network"); - let json_str = Yojson.Safe.Util.to_string json in - match json_str |> of_hex with - | Some t -> t - | None -> - failwith (Format.sprintf "cannot deserialize %s to blake2b" json_str) \ No newline at end of file diff --git a/deku-p/src/core/crypto/BLAKE2b.mli b/deku-p/src/core/crypto/BLAKE2b.mli index 7f06c3e6f3..8809788023 100644 --- a/deku-p/src/core/crypto/BLAKE2b.mli +++ b/deku-p/src/core/crypto/BLAKE2b.mli @@ -3,5 +3,3 @@ module BLAKE2b_256 : Hash_intf.S include Hash_intf.S with type hash = BLAKE2b_256.hash val encoding : t Data_encoding.t -val t_of_yojson : Yojson.Safe.t -> t -val yojson_of_t : t -> Yojson.Safe.t \ No newline at end of file diff --git a/deku-p/src/core/crypto/alg_intf.mli b/deku-p/src/core/crypto/alg_intf.mli index 40e2aa970e..c2285d8486 100644 --- a/deku-p/src/core/crypto/alg_intf.mli +++ b/deku-p/src/core/crypto/alg_intf.mli @@ -1,7 +1,7 @@ module type S = sig module Secret : sig type secret - type t = secret [@@deriving eq, ord, yojson] + type t = secret [@@deriving eq, ord] (* repr *) val of_b58 : string -> secret option @@ -18,7 +18,7 @@ module type S = sig module Key : sig type key - type t = key [@@deriving eq, ord, yojson] + type t = key [@@deriving eq, ord] (* repr *) val of_b58 : string -> key option @@ -35,7 +35,7 @@ module type S = sig module Key_hash : sig type key_hash - type t = key_hash [@@deriving eq, ord, yojson] + type t = key_hash [@@deriving eq, ord] (* repr *) val of_b58 : string -> key_hash option @@ -48,7 +48,7 @@ module type S = sig module Signature : sig type signature - type t = signature [@@deriving eq, ord, yojson] + type t = signature [@@deriving eq, ord] (* repr *) val of_b58 : string -> signature option diff --git a/deku-p/src/core/crypto/dune b/deku-p/src/core/crypto/dune index a5d1ac7a58..8c248daa80 100644 --- a/deku-p/src/core/crypto/dune +++ b/deku-p/src/core/crypto/dune @@ -9,8 +9,7 @@ mirage-crypto-rng.unix digestif mirage-crypto-ec - secp256k1-internal - yojson) + secp256k1-internal) (modules_without_implementation alg_intf hash_intf) (preprocess - (pps ppx_deriving.show ppx_deriving.eq ppx_deriving.ord ppx_yojson_conv))) + (pps ppx_deriving.show ppx_deriving.eq ppx_deriving.ord))) diff --git a/deku-p/src/core/crypto/ed25519.ml b/deku-p/src/core/crypto/ed25519.ml index 8663cd2e8e..4529d191c7 100644 --- a/deku-p/src/core/crypto/ed25519.ml +++ b/deku-p/src/core/crypto/ed25519.ml @@ -12,7 +12,7 @@ module Secret = struct let compare a b = Cstruct.compare (priv_to_cstruct a) (priv_to_cstruct b) - include With_b58_and_encoding_and_yojson (struct + include With_b58_and_encoding (struct type t = secret let name = "Ed25519.Secret_key" @@ -50,7 +50,7 @@ module Key = struct let of_secret secret = pub_of_priv secret let to_raw key = Cstruct.to_string (pub_to_cstruct key) - include With_b58_and_encoding_and_yojson (struct + include With_b58_and_encoding (struct type t = key let name = "Ed25519.Public_key" @@ -83,7 +83,7 @@ module Key_hash = struct let compare = compare let of_key key = hash (Key.to_raw key) - include With_b58_and_encoding_and_yojson (struct + include With_b58_and_encoding (struct let name = "Ed25519.Public_key_hash" let prefix = Prefix.ed25519_public_key_hash end) @@ -98,7 +98,7 @@ module Signature = struct let size = 64 let zero = String.make size '\x00' - include With_b58_and_encoding_and_yojson (struct + include With_b58_and_encoding (struct type t = signature let name = "Ed25519.Signature" @@ -127,4 +127,4 @@ module Signature = struct let signature = Cstruct.of_string signature in verify ~key ~msg:hash signature end) -end \ No newline at end of file +end diff --git a/deku-p/src/core/crypto/hash_intf.mli b/deku-p/src/core/crypto/hash_intf.mli index 45facf7eb7..631c7c1451 100644 --- a/deku-p/src/core/crypto/hash_intf.mli +++ b/deku-p/src/core/crypto/hash_intf.mli @@ -35,15 +35,13 @@ module type S = sig val verify : key -> signature -> hash -> bool end - module With_b58_and_encoding_and_yojson (_ : sig + module With_b58_and_encoding (_ : sig val name : string val prefix : Prefix.t end) : sig val of_b58 : string -> hash option val to_b58 : hash -> string val encoding : hash Data_encoding.t - val t_of_yojson : [> `String of string ] -> hash - val yojson_of_t : hash -> [> `String of string ] module Set : Set.S with type elt = hash module Map : Map.S with type key = hash diff --git a/deku-p/src/core/crypto/key.ml b/deku-p/src/core/crypto/key.ml index baf5e82861..f9d2a64fcd 100644 --- a/deku-p/src/core/crypto/key.ml +++ b/deku-p/src/core/crypto/key.ml @@ -37,13 +37,6 @@ let of_secret secret = | Secret.Secp256k1 secret -> Secp256k1 (Secp256k1.Key.of_secret secret) | Secret.P256 secret -> P256 (P256.Key.of_secret secret) -include With_yojson_of_b58 (struct - type t = key - - let of_b58 = of_b58 - let to_b58 = to_b58 -end) - let encoding = let open Data_encoding in let name = "Signature.Public_key" in diff --git a/deku-p/src/core/crypto/key.mli b/deku-p/src/core/crypto/key.mli index b9ef198f1b..955a6c5c17 100644 --- a/deku-p/src/core/crypto/key.mli +++ b/deku-p/src/core/crypto/key.mli @@ -4,7 +4,7 @@ type key = | Secp256k1 of Secp256k1.Key.t | P256 of P256.Key.t -type t = key [@@deriving eq, ord, show, yojson] +type t = key [@@deriving eq, ord, show] (* repr *) val of_b58 : string -> key option diff --git a/deku-p/src/core/crypto/key_hash.ml b/deku-p/src/core/crypto/key_hash.ml index 2eada6feb0..30a2bb7305 100644 --- a/deku-p/src/core/crypto/key_hash.ml +++ b/deku-p/src/core/crypto/key_hash.ml @@ -6,7 +6,7 @@ type key_hash = | Secp256k1 of Secp256k1.Key_hash.t | P256 of P256.Key_hash.t -and t = key_hash [@@deriving eq, ord, yojson] +and t = key_hash [@@deriving eq, ord] let of_b58 = let ed25519 string = @@ -37,13 +37,6 @@ let of_key = function | Key.Secp256k1 key -> Secp256k1 (Secp256k1.Key_hash.of_key key) | Key.P256 key -> P256 (P256.Key_hash.of_key key) -include With_yojson_of_b58 (struct - type t = key_hash - - let of_b58 = of_b58 - let to_b58 = to_b58 -end) - let encoding = let open Data_encoding in let name = "Signature.Public_key_hash" in @@ -64,13 +57,13 @@ let encoding = make_encoding ~name ~to_string:to_b58 ~of_string:of_b58 ~raw_encoding module Map = Map.Make (struct - type t = key_hash [@@deriving ord, yojson] + type t = key_hash [@@deriving ord] let encoding = encoding end) module Set = Set.Make (struct - type t = key_hash [@@deriving ord, yojson] + type t = key_hash [@@deriving ord] let encoding = encoding end) diff --git a/deku-p/src/core/crypto/key_hash.mli b/deku-p/src/core/crypto/key_hash.mli index 2924c146f9..ecfe82dc65 100644 --- a/deku-p/src/core/crypto/key_hash.mli +++ b/deku-p/src/core/crypto/key_hash.mli @@ -6,7 +6,7 @@ type key_hash = | Secp256k1 of Secp256k1.Key_hash.t | P256 of P256.Key_hash.t -type t = key_hash [@@deriving eq, ord, show, yojson] +type t = key_hash [@@deriving eq, ord, show] (* repr *) val of_b58 : string -> key_hash option diff --git a/deku-p/src/core/crypto/p256.ml b/deku-p/src/core/crypto/p256.ml index 36d1e79726..0eed129e1d 100644 --- a/deku-p/src/core/crypto/p256.ml +++ b/deku-p/src/core/crypto/p256.ml @@ -13,7 +13,7 @@ module Secret = struct let compare a b = Cstruct.compare (priv_to_cstruct a) (priv_to_cstruct b) - include With_b58_and_encoding_and_yojson (struct + include With_b58_and_encoding (struct type t = secret let name = "P256.Secret_key" @@ -52,7 +52,7 @@ module Key = struct let compare a b = Cstruct.compare (pub_to_cstruct a) (pub_to_cstruct b) let to_raw key = Cstruct.to_string (pub_to_cstruct ~compress:true key) - include With_b58_and_encoding_and_yojson (struct + include With_b58_and_encoding (struct type t = key let name = "P256.Public_key" @@ -84,7 +84,7 @@ module Key_hash = struct let compare = compare let of_key key = hash (Key.to_raw key) - include With_b58_and_encoding_and_yojson (struct + include With_b58_and_encoding (struct let name = "P256.Public_key_hash" let prefix = Prefix.p256_public_key_hash end) @@ -99,7 +99,7 @@ module Signature = struct let size = 64 let zero = String.make size '\x00' - include With_b58_and_encoding_and_yojson (struct + include With_b58_and_encoding (struct type t = signature let name = "P256.Signature" diff --git a/deku-p/src/core/crypto/secp256k1.ml b/deku-p/src/core/crypto/secp256k1.ml index d15869d9ac..b6e091cd5b 100644 --- a/deku-p/src/core/crypto/secp256k1.ml +++ b/deku-p/src/core/crypto/secp256k1.ml @@ -24,7 +24,7 @@ module Secret = struct let to_raw secret = Bigstring.to_string (Libsecp256k1.Key.to_bytes context secret) - include With_b58_and_encoding_and_yojson (struct + include With_b58_and_encoding (struct type t = secret let name = "Secp256k1.Secret_key" @@ -63,7 +63,7 @@ module Key = struct let of_secret secret = Libsecp256k1.Key.neuterize_exn context secret let to_raw key = Bigstring.to_string (Libsecp256k1.Key.to_bytes context key) - include With_b58_and_encoding_and_yojson (struct + include With_b58_and_encoding (struct type t = key let name = "Secp256k1.Public_key" @@ -97,7 +97,7 @@ module Key_hash = struct let compare = compare let of_key key = hash (Key.to_raw key) - include With_b58_and_encoding_and_yojson (struct + include With_b58_and_encoding (struct let name = "Secp256k1.Public_key_hash" let prefix = Prefix.secp256k1_public_key_hash end) @@ -116,7 +116,7 @@ module Signature = struct let size = Sign.plain_bytes let zero = of_raw (String.make size '\x00') |> Option.get - include With_b58_and_encoding_and_yojson (struct + include With_b58_and_encoding (struct type t = signature let name = "Secp256k1.Secret" diff --git a/deku-p/src/core/crypto/signature.ml b/deku-p/src/core/crypto/signature.ml index 864be1748f..2540cc5ac2 100644 --- a/deku-p/src/core/crypto/signature.ml +++ b/deku-p/src/core/crypto/signature.ml @@ -45,13 +45,6 @@ let to_b58 = function | Secp256k1 signature -> Secp256k1.Signature.to_b58 signature | P256 signature -> P256.Signature.to_b58 signature -include With_yojson_of_b58 (struct - type t = signature - - let of_b58 = of_b58 - let to_b58 = to_b58 -end) - let size = assert ( Ed25519.Signature.size = Secp256k1.Signature.size diff --git a/deku-p/src/core/crypto/signature.mli b/deku-p/src/core/crypto/signature.mli index 1266ca941f..250df57208 100644 --- a/deku-p/src/core/crypto/signature.mli +++ b/deku-p/src/core/crypto/signature.mli @@ -4,7 +4,7 @@ type signature = | Secp256k1 of Secp256k1.Signature.t | P256 of P256.Signature.t -type t = signature [@@deriving eq, ord, show, yojson] +type t = signature [@@deriving eq, ord, show] val encoding : t Data_encoding.t diff --git a/deku-p/src/core/gossip/message_hash.ml b/deku-p/src/core/gossip/message_hash.ml index aab7346db7..4dc7af8da4 100644 --- a/deku-p/src/core/gossip/message_hash.ml +++ b/deku-p/src/core/gossip/message_hash.ml @@ -8,7 +8,7 @@ and t = message_hash [@@deriving eq, ord] let to_blake2b message_hash = message_hash let of_blake2b message_hash = message_hash -include With_b58_and_encoding_and_yojson (struct +include With_b58_and_encoding (struct let name = "Deku_gossip.Message_hash" let prefix = Prefix.deku_message_hash end) diff --git a/deku-p/src/core/gossip/request_hash.ml b/deku-p/src/core/gossip/request_hash.ml index ad7f83bb5c..fb2bff92b1 100644 --- a/deku-p/src/core/gossip/request_hash.ml +++ b/deku-p/src/core/gossip/request_hash.ml @@ -7,7 +7,7 @@ and t = request_hash [@@deriving eq, ord] let to_blake2b request_hash = request_hash -include With_b58_and_encoding_and_yojson (struct +include With_b58_and_encoding (struct let name = "Deku_gossip.Request_hash" let prefix = Prefix.deku_request_hash end) diff --git a/deku-p/src/core/ledger/address.ml b/deku-p/src/core/ledger/address.ml index edb398386b..88968b5d57 100644 --- a/deku-p/src/core/ledger/address.ml +++ b/deku-p/src/core/ledger/address.ml @@ -5,7 +5,7 @@ type address = | Implicit of Key_hash.t | Originated of { address : Contract_address.t; entrypoint : string option } -and t = address [@@deriving eq, ord, show, yojson] +and t = address [@@deriving eq, ord, show] let of_key_hash key_hash = Implicit key_hash let to_key_hash = function Implicit x -> Some x | Originated _ -> None diff --git a/deku-p/src/core/ledger/address.mli b/deku-p/src/core/ledger/address.mli index 9ac0b139db..3f794e67a9 100644 --- a/deku-p/src/core/ledger/address.mli +++ b/deku-p/src/core/ledger/address.mli @@ -2,7 +2,7 @@ open Deku_stdlib open Deku_crypto type address -type t = address [@@deriving eq, ord, show, yojson] +type t = address [@@deriving eq, ord, show] (* repr *) val of_key_hash : Key_hash.t -> address diff --git a/deku-p/src/core/ledger/contract_address.ml b/deku-p/src/core/ledger/contract_address.ml index 37647228e7..c2d2335610 100644 --- a/deku-p/src/core/ledger/contract_address.ml +++ b/deku-p/src/core/ledger/contract_address.ml @@ -7,7 +7,7 @@ type t = BLAKE2b_160.t [@@deriving eq, ord, show] let of_user_operation_hash t = BLAKE2b_256.to_hex t |> BLAKE2b_160.hash -include With_b58_and_encoding_and_yojson (struct +include With_b58_and_encoding (struct let name = "Deku_contract_hash" let prefix = Prefix.deku_contract_hash end) diff --git a/deku-p/src/core/ledger/contract_address.mli b/deku-p/src/core/ledger/contract_address.mli index eb03eea3b1..6a4f1be6c1 100644 --- a/deku-p/src/core/ledger/contract_address.mli +++ b/deku-p/src/core/ledger/contract_address.mli @@ -1,7 +1,7 @@ open Deku_crypto open BLAKE2b -type t [@@deriving ord, eq, show, yojson] +type t [@@deriving ord, eq, show] val of_user_operation_hash : BLAKE2b_256.t -> t val to_b58 : t -> string diff --git a/deku-p/src/core/ledger/dune b/deku-p/src/core/ledger/dune index 1378af7016..addafcff0f 100644 --- a/deku-p/src/core/ledger/dune +++ b/deku-p/src/core/ledger/dune @@ -1,5 +1,5 @@ (library (name deku_ledger) - (libraries deku_stdlib deku_crypto deku_concepts deku_tezos data-encoding yojson) + (libraries deku_stdlib deku_crypto deku_concepts deku_tezos data-encoding) (preprocess - (pps ppx_deriving.show ppx_deriving.eq ppx_deriving.ord ppx_let_binding ppx_yojson_conv))) + (pps ppx_deriving.show ppx_deriving.eq ppx_deriving.ord ppx_let_binding))) diff --git a/deku-p/src/core/ledger/ledger.ml b/deku-p/src/core/ledger/ledger.ml index 48777ae516..db6e366234 100644 --- a/deku-p/src/core/ledger/ledger.ml +++ b/deku-p/src/core/ledger/ledger.ml @@ -15,7 +15,7 @@ module Withdrawal_handle = struct type t = BLAKE2b.t [@@deriving show] - include With_b58_and_encoding_and_yojson (struct + include With_b58_and_encoding (struct let name = "Ledger.Withdrawal_handle_hash" let prefix = Deku_repr.Prefix.deku_withdrawal_hash end) diff --git a/deku-p/src/core/ledger/ticket_id.ml b/deku-p/src/core/ledger/ticket_id.ml index ff9c20f073..6cab0f7a70 100644 --- a/deku-p/src/core/ledger/ticket_id.ml +++ b/deku-p/src/core/ledger/ticket_id.ml @@ -1,7 +1,7 @@ type ticketer = | Tezos of Deku_tezos.Contract_hash.t | Deku of Contract_address.t -[@@deriving ord, eq, show, yojson] +[@@deriving ord, eq, show] let ticketer_encoding = let tezos_tag = 0 in @@ -30,7 +30,7 @@ let ticketer_encoding = ] type ticket_id = Ticket_id of { ticketer : ticketer; data : bytes } -[@@deriving ord, show, eq, yojson] +[@@deriving ord, show, eq] and t = ticket_id [@@deriving eq, ord, show] diff --git a/deku-p/src/core/ledger/ticket_id.mli b/deku-p/src/core/ledger/ticket_id.mli index c74d77a9e5..9cae8c5ccb 100644 --- a/deku-p/src/core/ledger/ticket_id.mli +++ b/deku-p/src/core/ledger/ticket_id.mli @@ -4,7 +4,7 @@ type ticketer = [@@deriving ord, show] type ticket_id = private Ticket_id of { ticketer : ticketer; data : bytes } -and t = ticket_id [@@deriving ord, show, eq, yojson] +and t = ticket_id [@@deriving ord, show, eq] val make : ticketer -> bytes -> t val encoding : ticket_id Data_encoding.t diff --git a/deku-p/src/core/network/network_handshake.ml b/deku-p/src/core/network/network_handshake.ml index 593509d9b7..e892293a88 100644 --- a/deku-p/src/core/network/network_handshake.ml +++ b/deku-p/src/core/network/network_handshake.ml @@ -5,7 +5,7 @@ module Challenge = struct type challenge = Challenge of { hash : BLAKE2b.t } type t = challenge - include BLAKE2b.With_b58_and_encoding_and_yojson (struct + include BLAKE2b.With_b58_and_encoding (struct let name = "Network_handshake.Challenge" let prefix = Deku_repr.Prefix.deku_handshake_challenge_hash end) diff --git a/deku-p/src/core/protocol/dune b/deku-p/src/core/protocol/dune index 460705c174..5fbce8a8c9 100644 --- a/deku-p/src/core/protocol/dune +++ b/deku-p/src/core/protocol/dune @@ -10,13 +10,11 @@ deku_tezos deku_ledger deku_metrics - ocaml_wasm_vm - yojson) + ocaml_wasm_vm) (preprocess (pps ppx_let_binding ppx_deriving.eq ppx_deriving.show ppx_deriving.ord - ppx_expect - ppx_yojson_conv))) + ppx_expect))) diff --git a/deku-p/src/core/protocol/nonce.ml b/deku-p/src/core/protocol/nonce.ml index 6c21cd6a8c..71318f987e 100644 --- a/deku-p/src/core/protocol/nonce.ml +++ b/deku-p/src/core/protocol/nonce.ml @@ -2,7 +2,7 @@ open Deku_stdlib (* TODO: should we prefix level in b58? *) type nonce = N.t -and t = nonce [@@deriving eq, ord, show, yojson] +and t = nonce [@@deriving eq, ord, show] (* repr *) let of_n n = n diff --git a/deku-p/src/core/protocol/nonce.mli b/deku-p/src/core/protocol/nonce.mli index 675c0f7af2..f764435652 100644 --- a/deku-p/src/core/protocol/nonce.mli +++ b/deku-p/src/core/protocol/nonce.mli @@ -1,7 +1,7 @@ open Deku_stdlib type nonce -type t = nonce [@@deriving eq, ord, show, yojson] +type t = nonce [@@deriving eq, ord, show] (* repr *) val of_n : N.t -> nonce diff --git a/deku-p/src/core/protocol/operation.ml b/deku-p/src/core/protocol/operation.ml index bd1a0d9cb2..1b80d429b3 100644 --- a/deku-p/src/core/protocol/operation.ml +++ b/deku-p/src/core/protocol/operation.ml @@ -25,7 +25,7 @@ type operation = } | Operation_noop of { sender : Address.t } -and t = operation [@@deriving show, yojson] +and t = operation [@@deriving show] let encoding = (* TODO: bench Data_encoding.union vs Data_encoding.matching*) @@ -154,9 +154,9 @@ module Initial = struct operation : operation; } - and t = initial_operation [@@deriving show, yojson] + and t = initial_operation [@@deriving show] - type hash_repr = Nonce.t * Level.t * operation [@@deriving yojson] + type hash_repr = Nonce.t * Level.t * operation let hash_encoding : hash_repr Data_encoding.t = let open Data_encoding in diff --git a/deku-p/src/core/protocol/operation.mli b/deku-p/src/core/protocol/operation.mli index 71507b88c5..eda6bfcfcd 100644 --- a/deku-p/src/core/protocol/operation.mli +++ b/deku-p/src/core/protocol/operation.mli @@ -37,8 +37,8 @@ module Initial : sig operation : operation; } - type t = initial_operation [@@deriving show, yojson] - type hash_repr = Nonce.t * Level.t * operation [@@deriving yojson] + type t = initial_operation [@@deriving show] + type hash_repr = Nonce.t * Level.t * operation val hash_encoding : (Nonce.t * Level.t * operation) Data_encoding.t val encoding : t Data_encoding.t diff --git a/deku-p/src/core/protocol/operation_hash.ml b/deku-p/src/core/protocol/operation_hash.ml index a778d4fdf2..85a438518a 100644 --- a/deku-p/src/core/protocol/operation_hash.ml +++ b/deku-p/src/core/protocol/operation_hash.ml @@ -7,7 +7,7 @@ and t = operation_hash [@@deriving eq, ord] let to_blake2b operation_hash = operation_hash -include With_b58_and_encoding_and_yojson (struct +include With_b58_and_encoding (struct let name = "Deku_protocol.Operation_hash" let prefix = Prefix.deku_operation_hash end) diff --git a/deku-p/src/core/protocol/operation_hash.mli b/deku-p/src/core/protocol/operation_hash.mli index 95df30427d..d3d749782f 100644 --- a/deku-p/src/core/protocol/operation_hash.mli +++ b/deku-p/src/core/protocol/operation_hash.mli @@ -2,7 +2,7 @@ open Deku_stdlib open Deku_crypto type operation_hash -type t = operation_hash [@@deriving eq, ord, show, yojson] +type t = operation_hash [@@deriving eq, ord, show] (* repr *) val to_blake2b : operation_hash -> BLAKE2b.t diff --git a/deku-p/src/core/repr/deku_repr.ml b/deku-p/src/core/repr/deku_repr.ml index bd704af43b..513e220743 100644 --- a/deku-p/src/core/repr/deku_repr.ml +++ b/deku-p/src/core/repr/deku_repr.ml @@ -19,29 +19,6 @@ struct let to_b58 t = Base58.simple_encode ~prefix ~to_raw t end -(* TODO: exceptions???*) -exception Not_a_string -exception Not_a_b58 - -(* TODO: this is clearly not in the right file *) -module With_yojson_of_b58 (P : sig - type t - - val of_b58 : string -> t option - val to_b58 : t -> string -end) = -struct - open P - - let t_of_yojson json = - match json with - | `String string -> ( - match of_b58 string with Some t -> t | None -> raise Not_a_b58) - | _ -> raise Not_a_string - - let yojson_of_t t = `String (to_b58 t) -end - (* TODO: this is dumb *) let rec decode_variant l string = match l with @@ -91,7 +68,7 @@ struct conv to_raw of_raw_exn (Fixed.string size)) end -module With_b58_and_encoding_and_yojson (P : sig +module With_b58_and_encoding (P : sig type t val name : string @@ -102,13 +79,5 @@ module With_b58_and_encoding_and_yojson (P : sig end) = struct include With_b58 (P) - - include With_yojson_of_b58 (struct - type t = P.t - - let of_b58 = of_b58 - let to_b58 = to_b58 - end) - include With_encoding (P) -end \ No newline at end of file +end diff --git a/deku-p/src/core/repr/deku_repr.mli b/deku-p/src/core/repr/deku_repr.mli index ee839b655b..7a0cfe5076 100644 --- a/deku-p/src/core/repr/deku_repr.mli +++ b/deku-p/src/core/repr/deku_repr.mli @@ -39,21 +39,7 @@ end) : sig val to_b58 : P.t -> string end -(* yojson exceptions *) -exception Not_a_string -exception Not_a_b58 - -module With_yojson_of_b58 (P : sig - type t - - val of_b58 : string -> t option - val to_b58 : t -> string -end) : sig - val t_of_yojson : [> `String of string ] -> P.t - val yojson_of_t : P.t -> [> `String of string ] -end - -module With_b58_and_encoding_and_yojson (P : sig +module With_b58_and_encoding (P : sig type t val name : string @@ -65,8 +51,6 @@ end) : sig val encoding : P.t Data_encoding.t val of_b58 : string -> P.t option val to_b58 : P.t -> string - val t_of_yojson : [> `String of string ] -> P.t - val yojson_of_t : P.t -> [> `String of string ] end val decode_variant : (string -> 'a option) list -> string -> 'a option diff --git a/deku-p/src/core/repr/dune b/deku-p/src/core/repr/dune index df6c2c85d9..a8c6f5d777 100644 --- a/deku-p/src/core/repr/dune +++ b/deku-p/src/core/repr/dune @@ -1,5 +1,5 @@ (library (name deku_repr) - (libraries zarith mirage-crypto data-encoding yojson) + (libraries zarith mirage-crypto data-encoding) (preprocess - (pps ppx_deriving.eq ppx_deriving.ord ppx_yojson_conv))) + (pps ppx_deriving.eq ppx_deriving.ord))) diff --git a/deku-p/src/core/stdlib/deku_stdlib.ml b/deku-p/src/core/stdlib/deku_stdlib.ml index eb3a1a77b6..46cc6b0eb1 100644 --- a/deku-p/src/core/stdlib/deku_stdlib.ml +++ b/deku-p/src/core/stdlib/deku_stdlib.ml @@ -6,14 +6,3 @@ module IO = Io include Let_syntax module Parallel = Parallel module List = List_ext - -module Yojson = struct - include Yojson - - module Safe = struct - include Safe - - let t_of_yojson t = t - let yojson_of_t t = t - end -end \ No newline at end of file diff --git a/deku-p/src/core/stdlib/deku_stdlib.mli b/deku-p/src/core/stdlib/deku_stdlib.mli index a7a4364c50..183928a66a 100644 --- a/deku-p/src/core/stdlib/deku_stdlib.mli +++ b/deku-p/src/core/stdlib/deku_stdlib.mli @@ -11,14 +11,3 @@ module IO = Io module Parallel = Parallel module List = List_ext - -module Yojson : sig - include module type of Yojson - - module Safe : sig - include module type of Safe - - val t_of_yojson : t -> t - val yojson_of_t : t -> t - end -end \ No newline at end of file diff --git a/deku-p/src/core/stdlib/dune b/deku-p/src/core/stdlib/dune index e52bd593f8..daa17097e5 100644 --- a/deku-p/src/core/stdlib/dune +++ b/deku-p/src/core/stdlib/dune @@ -1,5 +1,5 @@ (library (name deku_stdlib) - (libraries deku_repr zarith uri base eio eio_main yojson) + (libraries deku_repr zarith uri base eio eio_main) (preprocess - (pps ppx_let_binding ppx_deriving.show ppx_deriving.eq ppx_deriving.ord ppx_yojson_conv))) + (pps ppx_let_binding ppx_deriving.show ppx_deriving.eq ppx_deriving.ord))) diff --git a/deku-p/src/core/stdlib/n.ml b/deku-p/src/core/stdlib/n.ml index 946ccb2c9b..97e254d5eb 100644 --- a/deku-p/src/core/stdlib/n.ml +++ b/deku-p/src/core/stdlib/n.ml @@ -1,7 +1,7 @@ open Z_ext type nat = Z_ext.t -and t = nat [@@deriving eq, ord, yojson] +and t = nat [@@deriving eq, ord] let show n = Format.asprintf "%a" pp_print n let pp = pp_print diff --git a/deku-p/src/core/stdlib/n.mli b/deku-p/src/core/stdlib/n.mli index 881c67f6c1..1923989a76 100644 --- a/deku-p/src/core/stdlib/n.mli +++ b/deku-p/src/core/stdlib/n.mli @@ -1,5 +1,5 @@ type nat -type t = nat [@@deriving show, eq, ord, yojson] +type t = nat [@@deriving show, eq, ord] exception Not_a_natural diff --git a/deku-p/src/core/stdlib/z_ext.ml b/deku-p/src/core/stdlib/z_ext.ml index df04d91dbf..728e715403 100644 --- a/deku-p/src/core/stdlib/z_ext.ml +++ b/deku-p/src/core/stdlib/z_ext.ml @@ -1,12 +1 @@ include Z - -exception Not_a_string -exception Not_an_integer - -let t_of_yojson json = - match json with - | `String string -> ( - try Z.of_string string with Invalid_argument _ -> raise Not_an_integer) - | _ -> raise Not_a_string - -let yojson_of_t t = `String (Z.to_string t) \ No newline at end of file diff --git a/deku-p/src/core/stdlib/z_ext.mli b/deku-p/src/core/stdlib/z_ext.mli index ac8adfbc17..5bacadafc4 100644 --- a/deku-p/src/core/stdlib/z_ext.mli +++ b/deku-p/src/core/stdlib/z_ext.mli @@ -1,9 +1,3 @@ include module type of struct include Z end - -exception Not_a_string -exception Not_an_integer - -val t_of_yojson : Yojson.Safe.t -> t -val yojson_of_t : t -> Yojson.Safe.t \ No newline at end of file diff --git a/deku-p/src/core/tezos/address.ml b/deku-p/src/core/tezos/address.ml index 108402b904..87d39854a8 100644 --- a/deku-p/src/core/tezos/address.ml +++ b/deku-p/src/core/tezos/address.ml @@ -4,7 +4,7 @@ open Deku_stdlib type t = | Implicit of Key_hash.t | Originated of { contract : Contract_hash.t; entrypoint : string option } -[@@deriving eq, ord, show, yojson] +[@@deriving eq, ord, show] let to_string = function | Implicit key_hash -> Key_hash.to_b58 key_hash diff --git a/deku-p/src/core/tezos/address.mli b/deku-p/src/core/tezos/address.mli index 8d0dc53a11..45c3e36e9a 100644 --- a/deku-p/src/core/tezos/address.mli +++ b/deku-p/src/core/tezos/address.mli @@ -3,7 +3,7 @@ open Deku_crypto type t = | Implicit of Key_hash.t | Originated of { contract : Contract_hash.t; entrypoint : string option } -[@@deriving eq, ord, show, yojson] +[@@deriving eq, ord, show] (* TODO: explain why this encoding? TLDR fixed size and no entrypoint *) val contract_encoding : t Data_encoding.t diff --git a/deku-p/src/core/tezos/contract_hash.ml b/deku-p/src/core/tezos/contract_hash.ml index eee281efc2..be0784c411 100644 --- a/deku-p/src/core/tezos/contract_hash.ml +++ b/deku-p/src/core/tezos/contract_hash.ml @@ -4,7 +4,7 @@ open BLAKE2b_160 type t = BLAKE2b_160.t [@@deriving eq, ord, show] -include With_b58_and_encoding_and_yojson (struct +include With_b58_and_encoding (struct let name = "Contract_hash" let prefix = Deku_repr.Prefix.contract_hash end) diff --git a/deku-p/src/core/tezos/contract_hash.mli b/deku-p/src/core/tezos/contract_hash.mli index d424b835e0..7f10a66ecb 100644 --- a/deku-p/src/core/tezos/contract_hash.mli +++ b/deku-p/src/core/tezos/contract_hash.mli @@ -1,7 +1,7 @@ open Deku_crypto open BLAKE2b -type t = BLAKE2b_160.t [@@deriving eq, ord, show, yojson] +type t = BLAKE2b_160.t [@@deriving eq, ord, show] val encoding : t Data_encoding.t val to_b58 : t -> string diff --git a/deku-p/src/core/tezos/dune b/deku-p/src/core/tezos/dune index 22e9b3948d..44a40e6e6f 100644 --- a/deku-p/src/core/tezos/dune +++ b/deku-p/src/core/tezos/dune @@ -1,5 +1,5 @@ (library (name deku_tezos) - (libraries deku_stdlib deku_crypto tezos-micheline deku_concepts yojson) + (libraries deku_stdlib deku_crypto tezos-micheline deku_concepts) (preprocess - (pps ppx_deriving.eq ppx_deriving.ord ppx_let_binding ppx_deriving.show ppx_yojson_conv))) + (pps ppx_deriving.eq ppx_deriving.ord ppx_let_binding ppx_deriving.show))) diff --git a/deku-p/src/core/tezos/tezos_operation_hash.ml b/deku-p/src/core/tezos/tezos_operation_hash.ml index 1e6f05812d..e48276fc55 100644 --- a/deku-p/src/core/tezos/tezos_operation_hash.ml +++ b/deku-p/src/core/tezos/tezos_operation_hash.ml @@ -5,7 +5,7 @@ open BLAKE2b type tezos_operation_hash = BLAKE2b.t [@@deriving eq, ord] type t = BLAKE2b.t [@@deriving eq, ord] -include With_b58_and_encoding_and_yojson (struct +include With_b58_and_encoding (struct let name = "Operation_hash" let prefix = Prefix.operation_hash end) diff --git a/nix/deku-p/deku.nix b/nix/deku-p/deku.nix index d8dcd0f33a..89a3ed6086 100644 --- a/nix/deku-p/deku.nix +++ b/nix/deku-p/deku.nix @@ -98,8 +98,6 @@ in ppx_expect ppx_deriving_encoding core_bench - ppx_yojson_conv - yojson ] # checkInputs are here because when cross compiling dune needs test dependencies # but they are not available for the build phase. The issue can be seen by adding strictDeps = true;. From e0eb49f3a46e4fa76bd10a0d3cb4bfdc09b4cd5f Mon Sep 17 00:00:00 2001 From: Gauthier SEBILLE Date: Wed, 28 Dec 2022 10:20:44 +0100 Subject: [PATCH 3/3] review ADR --- deku-p/src/benchmarking/ADR.md | 81 ++++++++++++++++++---------------- 1 file changed, 43 insertions(+), 38 deletions(-) diff --git a/deku-p/src/benchmarking/ADR.md b/deku-p/src/benchmarking/ADR.md index bdfd1b6e79..7fbcaf3797 100644 --- a/deku-p/src/benchmarking/ADR.md +++ b/deku-p/src/benchmarking/ADR.md @@ -50,10 +50,10 @@ Operations from binary/encoding: Total time taken 10.070946216583252s (656 sampl The main goal, is to have some data around `Yojson` VS `Data_encoding`, and also between `Data_encoding.Json` and `Data_encoding.Binary`. -We haven't tested all the record types we defined in Deku, but only the record types, managed by the HTTP API, and which will be called a lot: +We haven't tested all the record types we defined in Deku, but only the record types managed by the HTTP API, and which will be called a lot: - `/level`: a basic JSON like `{"level":"30"}` but it is called by the toolkit each time we want to submit an operation. -- `/encode-operation`: basically, adding the `0x80` prefix for Deku operations, to your submitted operation translated into hexadecimal. +- `/encode-operation`: basically, adding the `0x80` prefix for Deku operations, to the operation you want to submit, then returns its hexadecimal value. - `/operations`: finally submit the operation to Deku ## /level @@ -96,10 +96,10 @@ Hence, we create two different constants, which are representative to what we ar `json_level_encoding` is a quoted string representing the `Get_level.response` encoded into a JSON. We "cheat" and decode it to a `Get_level.reponse` using `Data_encoding`, our goal is not to test that the encoding is correct, but how fast it is. ```OCaml - let json_level_yojson = {| { "level" : "18" } |} in + let json_level_yojson = {| { "level" : "548430" } |} in ``` -Finally, we defined the exact same `Get_level.response` but from a `yojson` version (TODO: this is only because the current branch doesn't contain a rc-6 commit, which is fixing this breaking change on the HTTP API, once this branch is rebased, remove the previous line). +Finally, we defined the exact same `Get_level.response` but from a `yojson` version (TODO: this is only because the current branch doesn't contain a rc-6 commit, which is fixing this breaking change on the HTTP API, once this branch is rebased, remove the previous constant). ### Creating the tests @@ -180,7 +180,7 @@ But, in the same time, we can encode `2246186` at best using `Data_encoding`. Which leads to an average `time per run` lower by around 25% in favor of `Data_encoding`: 60.72ns for Yojson VS 44.62ns for Data_encoding. -`mWd/Run` is the amount of words allocated on the minor heap, while `mGC/Run` is the number of `minor collections`. +`mWd/Run` is the amount of words allocated on the minor heap, while `mGC/Run` is the number of minor collections. On our example, we see that `Data_encoding` is 25% faster than `Yojson`, but in the mean, time, it is using 3.5 times more words in the minor heap, and so, needing 3.8 times more minor collections. @@ -223,7 +223,7 @@ The corresponding `encoding` is already defined in `Operation.Initial`, so we ar ### Creating the tests -Once again, because the encoding of `level` is different between `Yojson` and `Data_encoding` (TODO: remove), we need to define two different sample: +Once again, because the encoding of `level` is different between `Yojson` and `Data_encoding` (TODO: remove, due to previous missing commit of rc-6), we need to define two different samples: ```OCaml let json_encode_operation_encoding = @@ -267,57 +267,56 @@ Bench.Test.create_group ~name:"Operations to JSON" ```bash Encode_operation to JSON/yojson: Total time taken 10.008656024932861s (528 samples, max runs 4061). Encode_operation to JSON/encoding: Total time taken 10.010571002960205s (648 samples, max runs 13285). -┌───────────────────────────────────────────┬────────────────┬───────────────┬────────────┬────────────┬─────────────┬──────────┐ -│ Name │ Time/Run │ mWd/Run │ mjWd/Run │ Prom/Run │ mGC/Run │ mjGC/Run │ -├───────────────────────────────────────────┼────────────────┼───────────────┼────────────┼────────────┼─────────────┼──────────┤ -│ Encode_operation to JSON/yojson │ 23_642.19ns │ 15_284.51w │ 150.76w │ 150.76w │ 58.93e-3 │ 0.62e-3 │ -│ Encode_operation to JSON/encoding │ 7_413.65ns │ 2_869.19w │ 44.74w │ 44.74w │ 11.10e-3 │ 0.15e-3 │ -└───────────────────────────────────────────┴────────────────┴───────────────┴────────────┴────────────┴─────────────┴──────────┘ - +┌─────────────────────────────────────┬──────────────┬─────────────┬───────────┬───────────┬──────────┬──────────┐ +│ Name │ Time/Run │ mWd/Run │ mjWd/Run │ Prom/Run │ mGC/Run │ mjGC/Run │ +├─────────────────────────────────────┼──────────────┼─────────────┼───────────┼───────────┼──────────┼──────────┤ +│ Encode_operation to JSON/yojson │ 23_642.19ns │ 15_284.51w │ 150.76w │ 150.76w │ 58.93e-3 │ 0.62e-3 │ +│ Encode_operation to JSON/encoding │ 7_413.65ns │ 2_869.19w │ 44.74w │ 44.74w │ 11.10e-3 │ 0.15e-3 │ +└─────────────────────────────────────┴──────────────┴─────────────┴───────────┴───────────┴──────────┴──────────┘ ``` Some new colums are displayed, because these two tests make significant changes on these values, which was not the case for the `Get_level.response`. -As already seen in the previous tests, `Data_encoding` is around 3.2 times faster to encode such a type, but unlike `Get_level.response`, it is also allocating less `minor words` into the heap and ~5.3 times less and needing ~5.3 times less minor collections. +As already seen in the previous tests, `Data_encoding` is around 3.2 times faster to encode such a type, but unlike `Get_level.response`, it is also allocating less `minor words` into the heap around ~5.3 times less and needing around ~5.3 less times minor collections. The new colums are: -- `mjWd/Run`: amount of words allocated in the major heap -- `Prom/Run`: -- `mjGC/Run`: amount of major collections +- `mjWd/Run`: amount of words allocated in the major heap per run +- `Prom/Run`: amount of words promoted from minor to major heap per run +- `mjGC/Run`: amount of major collections per run -For all these columns, the figures are also in favor of `Data_encoding. +For all these columns, the figures are also in favor of `Data_encoding`. ### Results of `from JSON` ```bash Encode_operation from JSON/yojson: Total time taken 10.066035985946655s (126 samples, max runs 126). Encode_operation from JSON/encoding: Total time taken 10.131572008132935s (127 samples, max runs 127). -┌───────────────────────────────────────────┬────────────────┬───────────────┬────────────┬────────────┬─────────────┬──────────┐ -│ Name │ Time/Run │ mWd/Run │ mjWd/Run │ Prom/Run │ mGC/Run │ mjGC/Run │ -├───────────────────────────────────────────┼────────────────┼───────────────┼────────────┼────────────┼─────────────┼──────────┤ -│ Encode_operation from JSON/yojson │ 1_275_840.60ns │ 1_548_138.67w │ 14_092.03w │ 14_092.03w │ 5_937.15e-3 │ 31.47e-3 │ -│ Encode_operation from JSON/encoding │ 1_266_042.20ns │ 1_547_947.82w │ 13_986.71w │ 13_986.71w │ 5_934.20e-3 │ 29.27e-3 │ -└───────────────────────────────────────────┴────────────────┴───────────────┴────────────┴────────────┴─────────────┴──────────┘ +┌──────────────────────────────────────┬────────────────┬───────────────┬────────────┬────────────┬─────────────┬──────────┐ +│ Name │ Time/Run │ mWd/Run │ mjWd/Run │ Prom/Run │ mGC/Run │ mjGC/Run │ +├──────────────────────────────────────┼────────────────┼───────────────┼────────────┼────────────┼─────────────┼──────────┤ +│ Encode_operation from JSON/yojson │ 1_275_840.60ns │ 1_548_138.67w │ 14_092.03w │ 14_092.03w │ 5_937.15e-3 │ 31.47e-3 │ +│ Encode_operation from JSON/encoding │ 1_266_042.20ns │ 1_547_947.82w │ 13_986.71w │ 13_986.71w │ 5_934.20e-3 │ 29.27e-3 │ +└──────────────────────────────────────┴────────────────┴───────────────┴────────────┴────────────┴─────────────┴──────────┘ ``` -Unlike the previous figures, there is not significant "winner" in this match. `Data_encoding` is globally a bit better, but it is really closed. +Unlike the previous figures, there is not significant "winner" in this match. `Data_encoding` is globally a bit better, but it is really tight. ### Conclusion Benchmarking this "complex" record type had lead to display new columns of `core_bench` result because of use of the major heap. -**Looking for performance, `Data_encoding` is better than `Yojson`** +**Globally, `Data_encoding` is better than `Yojson` for this more complex record type.** ## /operations -This endpoint is used to submit an operation to Deku. Hence, it is called a lot, and since it is carrying a lot of data, it is interesting to benchmark its body. +This endpoint is used to submit an operation to Deku. Hence, it is called a lot and since it is carrying a lot of data, it is interesting to benchmark its body. On this body, we also want to benchmark: - `obj` and `tup` from `Data_encoding`. -- If the verification of the signature is taking a lot of time or of resources. -- `Data_encoding.Json` VS `Data_encoding.Binary` +- If the verification of the signature is taking a lot of time or of resources(`to_signed` function). +- `Data_encoding.Json` VS `Data_encoding.Binary` to see if it could be interesting to submit bytes instead of plain JSON. ### Creating the types @@ -356,8 +355,8 @@ Once again, we duplicate the body type from `operations` endpoint in `Handlers.m Then we define two different encoding functions: -- `obj_encoding` using `obj` from `Data_encoding` -- `tup_encoding` using `tup` from `Data_encoding` +- `obj_encoding` using `obj` +- `tup_encoding` using `tup` And finally, we define the exact same `to_signed` function, which verifiy the signature. @@ -454,7 +453,7 @@ Operations to JSON/encoding: Total time taken 10.017661809921265s (538 samples, └───────────────────────────────────────────┴────────────────┴───────────────┴────────────┴────────────┴─────────────┴──────────┘ ``` -Once again, this is a victory for `Data_encoding`, wich is twice ~2 times faster, and also needing less words in minor and major heap, and so, less collections. +Once again, it is a victory for `Data_encoding`, wich is around ~2 times faster, and also needing less words in minor and major heap, and so, less collections. ### Results of `from JSON` @@ -479,23 +478,29 @@ Operations from binary/encoding: Total time taken 10.070946216583252s (656 sampl #### Yojson VS Data_encoding -There is no significant winner, but `Data_encoding` is faster than `Yojson` +**There is no significant winner, but `Data_encoding` is globally faster than `Yojson`.** #### With `to_signed` VS without `to_signed` -Unlike previously, when calling `to_signed` function, `Yojson` is faster than `Data_encoding`. In the other hand, there is not a significant win by avoinding to call `to_signed`, 1_285_833.38ns with `Data_encoding` without `to_signed` VS 1_339_803.92ns with `Data_encoding` with `to_signed` +Unlike previously, when calling `to_signed` function, `Yojson` is faster than `Data_encoding`. In the other hand, there is not a significant win by avoinding to call `to_signed` (around 4%), 1_285_833.38ns with `Data_encoding` without `to_signed` VS 1_339_803.92ns with `Data_encoding` with `to_signed`. #### `obj` VS `tup` -1_285_833.38ns for `obj_encoding` and 1_286_578.90ns for `tup_encoding`, and other figures are also in favor of `obj`. +1_285_833.38ns for `obj_encoding` and 1_286_578.90ns for `tup_encoding`, and other figures are also in favor of `obj`. But the difference is really small. #### `Data_encoding.Json` VS `Data_encoding.Binary` -This is a clear win for `Data_encoding.Binary`, where the average `Time/Run` is 6_904.63ns, when it is 1_285_833.38ns for the `Data_encoding.Json`! +This is a clear win for `Data_encoding.Binary`, where the average `Time/Run` is 6_904.63ns, when it is 1_285_833.38ns for the `Data_encoding.Json`, which means `Data_encoding.Binary` is around 186 times faster! + +### Other inputs + +- Unlike `Yojson`, `Data_encoding` allows us to use encode and decode to `Binary`. It is not only using JSON. + +- `Yojson` is part of the `OCaml-community` repository, while `Data_encoding` is maintained by Nomadic Labs. ### Conclusion -Once again, `Data_encoding.Json` was globally better than `Yojson`. +Once again, `Data_encoding.Json` was globally better than `Yojson`. Even if when calling `to_signed` function, `Yojson` is a bit better. Inside `Data_encoding`, there is no win to switch from `obj` to `tup. @@ -503,4 +508,4 @@ As expected, decoding with calling the `to_signed` function needs more time and **The most interesting figure, is about `Data_encoding.Json` VS `Data_encoding.Binary`. This is a clear and neat win for `Data_encoding.Binary`.** -**If we want to improve performances on our HTTP API, we could/should switch the `/operations` endpoint to `Data_encoding.Binary`, however, this would imply to provide a decode/encode function to `Data_encoding.Binary` in our toolkit and without calling our API (because if we simply move it to an other endpoint, we simply move the results).** +**If we want to improve performances on our HTTP API, we could/should switch the `/operations` endpoint to `Data_encoding.Binary`, however, this would imply to submit bytes with the deku-prefix.**