From 6db325f7830595b1397511c25085bdf6dc24d672 Mon Sep 17 00:00:00 2001 From: Alain Mebsout Date: Mon, 21 Nov 2022 14:27:55 +0100 Subject: [PATCH 1/4] Proto/SCORU: use hexadecimal encoding for binary data --- .../lib_protocol/operation_repr.ml | 23 ++++++------------- .../lib_protocol/sc_rollup_PVM_sig.ml | 10 ++++---- .../sc_rollup_inbox_message_repr.ml | 5 +--- .../lib_protocol/sc_rollup_inbox_repr.ml | 10 ++++---- .../lib_protocol/sc_rollup_proof_repr.ml | 8 ++++--- src/proto_alpha/lib_sc_rollup/l2_message.ml | 2 +- 6 files changed, 25 insertions(+), 33 deletions(-) diff --git a/src/proto_alpha/lib_protocol/operation_repr.ml b/src/proto_alpha/lib_protocol/operation_repr.ml index dd9c7b34b21a..8e2ab343f300 100644 --- a/src/proto_alpha/lib_protocol/operation_repr.ml +++ b/src/proto_alpha/lib_protocol/operation_repr.ml @@ -804,12 +804,6 @@ module Encoding = struct inj = (fun () -> Tx_rollup_origination); } - let tx_rollup_batch_content = - (* The content of batches is a string, but stands for an immutable byte - sequence. JSON only allows unicode strings so we use the [bytes] - encoding which is in hexadecimal for JSON. *) - conv Bytes.of_string Bytes.to_string bytes - let tx_rollup_submit_batch_case = MCase { @@ -818,7 +812,7 @@ module Encoding = struct encoding = obj3 (req "rollup" Tx_rollup_repr.encoding) - (req "content" tx_rollup_batch_content) + (req "content" (string' Hex)) (opt "burn_limit" Tez_repr.encoding); select = (function @@ -1126,9 +1120,6 @@ module Encoding = struct (fun (zk_rollup, update) -> Zk_rollup_update {zk_rollup; update}); } - let string_to_bytes_encoding = - Data_encoding.conv Bytes.of_string Bytes.to_string Data_encoding.bytes - let sc_rollup_originate_case = MCase { @@ -1137,8 +1128,8 @@ module Encoding = struct encoding = obj4 (req "pvm_kind" Sc_rollups.Kind.encoding) - (req "boot_sector" string_to_bytes_encoding) - (req "origination_proof" string_to_bytes_encoding) + (req "boot_sector" (string' Hex)) + (req "origination_proof" (string' Hex)) (req "parameters_ty" Script_repr.lazy_expr_encoding); select = (function @@ -1173,7 +1164,7 @@ module Encoding = struct { tag = sc_rollup_operation_add_message_tag; name = "sc_rollup_add_messages"; - encoding = obj1 (req "message" (list string)); + encoding = obj1 (req "message" (list (string' Hex))); select = (function | Manager (Sc_rollup_add_messages _ as op) -> Some op | _ -> None); @@ -1270,7 +1261,7 @@ module Encoding = struct (req "cemented_commitment" Sc_rollup_commitment_repr.Hash.encoding) - (req "output_proof" Data_encoding.string); + (req "output_proof" (string' Hex)); select = (function | Manager (Sc_rollup_execute_outbox_message _ as op) -> Some op @@ -1589,7 +1580,7 @@ module Encoding = struct { tag = 17; name = "failing_noop"; - encoding = obj1 (req "arbitrary" Data_encoding.string); + encoding = obj1 (req "arbitrary" string); select = (function Contents (Failing_noop _ as op) -> Some op | _ -> None); proj = (function Failing_noop message -> message); @@ -1948,7 +1939,7 @@ let () = ppf "An operation contents list has an unexpected shape: %s" s) - Data_encoding.(obj1 (req "message" string)) + Data_encoding.(obj1 (req "message" (string' Hex))) (function Contents_list_error s -> Some s | _ -> None) (fun s -> Contents_list_error s) diff --git a/src/proto_alpha/lib_protocol/sc_rollup_PVM_sig.ml b/src/proto_alpha/lib_protocol/sc_rollup_PVM_sig.ml index c4a4d6ca4d7a..8b8b3a623219 100644 --- a/src/proto_alpha/lib_protocol/sc_rollup_PVM_sig.ml +++ b/src/proto_alpha/lib_protocol/sc_rollup_PVM_sig.ml @@ -102,7 +102,7 @@ let inbox_message_encoding = (obj3 (req "inbox_level" Raw_level_repr.encoding) (req "message_counter" n) - (req "payload" string)) + (req "payload" (string' Hex))) let reveal_data_encoding = let open Data_encoding in @@ -114,9 +114,11 @@ let reveal_data_encoding = (req "reveal_data_kind" (constant "raw_data")) (req "raw_data" - (check_size Constants_repr.sc_rollup_message_size_limit bytes))) - (function Raw_data m -> Some ((), Bytes.of_string m) | _ -> None) - (fun ((), m) -> Raw_data (Bytes.to_string m)) + (check_size + Constants_repr.sc_rollup_message_size_limit + (string' Hex)))) + (function Raw_data m -> Some ((), m) | _ -> None) + (fun ((), m) -> Raw_data m) and case_metadata = case ~title:"metadata" diff --git a/src/proto_alpha/lib_protocol/sc_rollup_inbox_message_repr.ml b/src/proto_alpha/lib_protocol/sc_rollup_inbox_message_repr.ml index 62effb1292f0..54e2c73c2b1d 100644 --- a/src/proto_alpha/lib_protocol/sc_rollup_inbox_message_repr.ml +++ b/src/proto_alpha/lib_protocol/sc_rollup_inbox_message_repr.ml @@ -98,9 +98,6 @@ let internal_inbox_message_encoding = (fun () -> End_of_level); ] -(* TODO: https://gitlab.com/tezos/tezos/-/issues/4027 - We should change the payload of [External] from [bytes] to [string]. *) - type t = Internal of internal_inbox_message | External of string let encoding = @@ -120,7 +117,7 @@ let encoding = case (Tag 1) ~title:"External" - Variable.string + Variable.(string' Hex) (function External msg -> Some msg | Internal _ -> None) (fun msg -> External msg); ]) diff --git a/src/proto_alpha/lib_protocol/sc_rollup_inbox_repr.ml b/src/proto_alpha/lib_protocol/sc_rollup_inbox_repr.ml index f10849036693..855088a64605 100644 --- a/src/proto_alpha/lib_protocol/sc_rollup_inbox_repr.ml +++ b/src/proto_alpha/lib_protocol/sc_rollup_inbox_repr.ml @@ -352,9 +352,9 @@ let key_of_message ix = let number_of_messages_key = ["number_of_messages"] -type serialized_proof = bytes +type serialized_proof = string -let serialized_proof_encoding = Data_encoding.bytes +let serialized_proof_encoding = Data_encoding.(string' Hex) module type Merkelized_operations = sig type inbox_context @@ -758,9 +758,9 @@ struct Next_level {lower_message_proof; inc}); ] - let of_serialized_proof = Data_encoding.Binary.of_bytes_opt proof_encoding + let of_serialized_proof = Data_encoding.Binary.of_string_opt proof_encoding - let to_serialized_proof = Data_encoding.Binary.to_bytes_exn proof_encoding + let to_serialized_proof = Data_encoding.Binary.to_string_exn proof_encoding let proof_error reason = let open Lwt_result_syntax in @@ -935,7 +935,7 @@ struct |> Option.map (lift_ptr_path deref) |> Option.join |> return - let serialized_proof_of_string x = Bytes.of_string x + let serialized_proof_of_string x = x let inbox_message_counter = inbox_message_counter end diff --git a/src/proto_alpha/lib_protocol/sc_rollup_proof_repr.ml b/src/proto_alpha/lib_protocol/sc_rollup_proof_repr.ml index bdcf201620c5..2dfe7e8c3cb6 100644 --- a/src/proto_alpha/lib_protocol/sc_rollup_proof_repr.ml +++ b/src/proto_alpha/lib_protocol/sc_rollup_proof_repr.ml @@ -67,9 +67,11 @@ let reveal_proof_encoding = (req "reveal_proof_kind" (constant "raw_data_proof")) (req "raw_data" - (check_size Constants_repr.sc_rollup_message_size_limit bytes))) - (function Raw_data_proof s -> Some ((), Bytes.of_string s) | _ -> None) - (fun ((), s) -> Raw_data_proof (Bytes.to_string s)) + (check_size + Constants_repr.sc_rollup_message_size_limit + (string' Hex)))) + (function Raw_data_proof s -> Some ((), s) | _ -> None) + (fun ((), s) -> Raw_data_proof s) and case_metadata_proof = case ~title:"metadata proof" diff --git a/src/proto_alpha/lib_sc_rollup/l2_message.ml b/src/proto_alpha/lib_sc_rollup/l2_message.ml index 9b669451cd09..71987dc50b8d 100644 --- a/src/proto_alpha/lib_sc_rollup/l2_message.ml +++ b/src/proto_alpha/lib_sc_rollup/l2_message.ml @@ -33,7 +33,7 @@ type t = { let content_encoding = let open Data_encoding in def "sc_l2_message" ~description:"A hex encoded smart contact rollup message" - @@ conv String.to_bytes String.of_bytes bytes + @@ string' Hex let encoding = let open Data_encoding in -- GitLab From 0e42623b918746eba461b27b808081e84a5a6a05 Mon Sep 17 00:00:00 2001 From: Alain Mebsout Date: Mon, 21 Nov 2022 14:29:45 +0100 Subject: [PATCH 2/4] Proto: Failing noop uses hexadecimal string in JSON --- src/proto_alpha/lib_protocol/operation_repr.ml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/proto_alpha/lib_protocol/operation_repr.ml b/src/proto_alpha/lib_protocol/operation_repr.ml index 8e2ab343f300..c524f5951e0e 100644 --- a/src/proto_alpha/lib_protocol/operation_repr.ml +++ b/src/proto_alpha/lib_protocol/operation_repr.ml @@ -1580,7 +1580,7 @@ module Encoding = struct { tag = 17; name = "failing_noop"; - encoding = obj1 (req "arbitrary" string); + encoding = obj1 (req "arbitrary" (string' Hex)); select = (function Contents (Failing_noop _ as op) -> Some op | _ -> None); proj = (function Failing_noop message -> message); -- GitLab From 985047becdfe4ea832c2837bec1538f59d8bd32e Mon Sep 17 00:00:00 2001 From: Alain Mebsout Date: Mon, 21 Nov 2022 14:51:23 +0100 Subject: [PATCH 3/4] Proto/Tx_rollup: remove unneeded encoding --- src/proto_alpha/lib_protocol/tx_rollup_l2_proof.ml | 6 +----- src/proto_alpha/lib_protocol/tx_rollup_message_repr.ml | 7 +------ 2 files changed, 2 insertions(+), 11 deletions(-) diff --git a/src/proto_alpha/lib_protocol/tx_rollup_l2_proof.ml b/src/proto_alpha/lib_protocol/tx_rollup_l2_proof.ml index 2b0010fe8530..806ab8da15ee 100644 --- a/src/proto_alpha/lib_protocol/tx_rollup_l2_proof.ml +++ b/src/proto_alpha/lib_protocol/tx_rollup_l2_proof.ml @@ -32,11 +32,7 @@ type serialized = string let length = String.length -let serialized_encoding = - let open Data_encoding in - (* Deal with unprintable string *) - let json = conv Bytes.of_string Bytes.to_string bytes in - splitted ~json ~binary:string +let serialized_encoding = Data_encoding.(string' Hex) let proof_of_serialized_opt = Data_encoding.Binary.of_string_opt encoding diff --git a/src/proto_alpha/lib_protocol/tx_rollup_message_repr.ml b/src/proto_alpha/lib_protocol/tx_rollup_message_repr.ml index a393b22676f9..c86c770d68dc 100644 --- a/src/proto_alpha/lib_protocol/tx_rollup_message_repr.ml +++ b/src/proto_alpha/lib_protocol/tx_rollup_message_repr.ml @@ -45,11 +45,6 @@ let deposit_encoding = (req "ticket_hash" Ticket_hash_repr.encoding) (req "amount" Tx_rollup_l2_qty.encoding) -let batch_encoding = - let open Data_encoding in - let json = conv Bytes.of_string Bytes.to_string bytes in - splitted ~json ~binary:string - type t = Batch of string | Deposit of deposit let encoding = @@ -60,7 +55,7 @@ let encoding = case (Tag 0) ~title:"Batch" - (obj1 (req "batch" batch_encoding)) + (obj1 (req "batch" (string' Hex))) (function Batch batch -> Some batch | _ -> None) (fun batch -> Batch batch); case -- GitLab From c87b88b143111a4f1849c324df69fcf5d904825f Mon Sep 17 00:00:00 2001 From: Alain Mebsout Date: Mon, 21 Nov 2022 15:40:17 +0100 Subject: [PATCH 4/4] Test/Python: fix test for failing noop --- tests_python/tests_alpha/test_basic.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tests_python/tests_alpha/test_basic.py b/tests_python/tests_alpha/test_basic.py index 3a0a97f40cb5..b5702c307f92 100644 --- a/tests_python/tests_alpha/test_basic.py +++ b/tests_python/tests_alpha/test_basic.py @@ -143,7 +143,12 @@ class TestRawContext: run_json = { 'operation': { "branch": head_hash, - "contents": [{"kind": "failing_noop", "arbitrary": message}], + "contents": [ + { + "kind": "failing_noop", + "arbitrary": bytes(message, 'utf-8').hex(), + } + ], 'signature': signature, }, 'chain_id': chain_id, -- GitLab