From 9e08e59d60dce842285e11c725dc05e4a005c9ce Mon Sep 17 00:00:00 2001 From: Sylvain Ribstein Date: Thu, 22 Feb 2024 13:57:11 +0100 Subject: [PATCH 1/3] etherlink/node: move timestamp_to_bytes to ethereum_type helpers.ml is not accessible from ethereum_types and I need to use it in in next commit --- etherlink/bin_node/lib_dev/encodings/ethereum_types.ml | 8 ++++++++ etherlink/bin_node/lib_dev/helpers.ml | 6 ------ etherlink/bin_node/lib_dev/helpers.mli | 4 ---- etherlink/bin_node/lib_dev/sequencer_blueprint.ml | 2 +- etherlink/bin_node/main.ml | 4 ++-- 5 files changed, 11 insertions(+), 13 deletions(-) diff --git a/etherlink/bin_node/lib_dev/encodings/ethereum_types.ml b/etherlink/bin_node/lib_dev/encodings/ethereum_types.ml index ff52ee60b3c5..f9a2d1f7d363 100644 --- a/etherlink/bin_node/lib_dev/encodings/ethereum_types.ml +++ b/etherlink/bin_node/lib_dev/encodings/ethereum_types.ml @@ -62,6 +62,14 @@ let address_to_string (Address a) = hex_to_string a let address_encoding = Data_encoding.(conv address_to_string address_of_string string) +(** [timestamp_to_bytes timestamp] transforms the timestamp to bytes + compatible with the kernel. *) +let timestamp_to_bytes timestamp = + let seconds = Time.Protocol.to_seconds timestamp in + let buffer = Bytes.make 8 '\000' in + Bytes.set_int64_le buffer 0 seconds ; + buffer + (** Ethereum generic quantity, always encoded in hexadecimal. *) type quantity = Qty of Z.t [@@ocaml.unboxed] diff --git a/etherlink/bin_node/lib_dev/helpers.ml b/etherlink/bin_node/lib_dev/helpers.ml index e8cd7c674efd..34d73eb2a48a 100644 --- a/etherlink/bin_node/lib_dev/helpers.ml +++ b/etherlink/bin_node/lib_dev/helpers.ml @@ -9,9 +9,3 @@ let now () = let now = Ptime_clock.now () in let now = Ptime.to_rfc3339 now in Time.Protocol.of_notation_exn now - -let timestamp_to_bytes timestamp = - let seconds = Time.Protocol.to_seconds timestamp in - let buffer = Bytes.make 8 '\000' in - Bytes.set_int64_le buffer 0 seconds ; - buffer diff --git a/etherlink/bin_node/lib_dev/helpers.mli b/etherlink/bin_node/lib_dev/helpers.mli index 468b2281a100..0cf2fd0dfac9 100644 --- a/etherlink/bin_node/lib_dev/helpers.mli +++ b/etherlink/bin_node/lib_dev/helpers.mli @@ -7,7 +7,3 @@ (** [now ()] returns the current time. *) val now : unit -> Time.Protocol.t - -(** [timestamp_to_bytes timestamp] transforms the timestamp to bytes - compatible with the kernel. *) -val timestamp_to_bytes : Time.Protocol.t -> bytes diff --git a/etherlink/bin_node/lib_dev/sequencer_blueprint.ml b/etherlink/bin_node/lib_dev/sequencer_blueprint.ml index 965513539ebe..ec684b0f234f 100644 --- a/etherlink/bin_node/lib_dev/sequencer_blueprint.ml +++ b/etherlink/bin_node/lib_dev/sequencer_blueprint.ml @@ -85,7 +85,7 @@ let make_blueprint_chunks ~timestamp ~transactions ~delayed_transactions in (List m, List (delayed_transactions @ m)) in - let timestamp = Value (Helpers.timestamp_to_bytes timestamp) in + let timestamp = Value (Ethereum_types.timestamp_to_bytes timestamp) in let parent_hash = Value (block_hash_to_bytes parent_hash |> Bytes.of_string) in diff --git a/etherlink/bin_node/main.ml b/etherlink/bin_node/main.ml index cf9f122ace6d..409c7d08b91f 100644 --- a/etherlink/bin_node/main.ml +++ b/etherlink/bin_node/main.ml @@ -1012,7 +1012,7 @@ let make_upgrade_command = (fun () root_hash activation_timestamp () -> let open Rlp in let activation_timestamp = - Evm_node_lib_dev.Helpers.timestamp_to_bytes activation_timestamp + Ethereum_types.timestamp_to_bytes activation_timestamp in let root_hash_bytes = Hex.to_bytes_exn (`Hex root_hash) in let kernel_upgrade = @@ -1051,7 +1051,7 @@ let make_sequencer_upgrade_command = Client_keys.Public_key.parse_source_string wallet_ctxt sequencer_str in let activation_timestamp = - Evm_node_lib_dev.Helpers.timestamp_to_bytes activation_timestamp + Ethereum_types.timestamp_to_bytes activation_timestamp in let*? sequencer_pk = Option.to_result -- GitLab From ca8bb11d0626f1c10a6e8b0dae06a4e7dd0bf7ae Mon Sep 17 00:00:00 2001 From: Sylvain Ribstein Date: Mon, 19 Feb 2024 16:29:46 +0100 Subject: [PATCH 2/3] eherlink/node: add event for new evm event --- .../lib_dev/encodings/ethereum_types.ml | 41 +++++++++++++++---- .../bin_node/lib_dev/evm_events_follower.ml | 2 + .../lib_dev/evm_events_follower_events.ml | 11 +++++ 3 files changed, 46 insertions(+), 8 deletions(-) diff --git a/etherlink/bin_node/lib_dev/encodings/ethereum_types.ml b/etherlink/bin_node/lib_dev/encodings/ethereum_types.ml index f9a2d1f7d363..9abad215be0a 100644 --- a/etherlink/bin_node/lib_dev/encodings/ethereum_types.ml +++ b/etherlink/bin_node/lib_dev/encodings/ethereum_types.ml @@ -70,6 +70,10 @@ let timestamp_to_bytes timestamp = Bytes.set_int64_le buffer 0 seconds ; buffer +let timestamp_of_bytes timestamp_bytes = + let timestamp_64 = Bytes.get_int64_le timestamp_bytes 0 in + Time.Protocol.of_seconds timestamp_64 + (** Ethereum generic quantity, always encoded in hexadecimal. *) type quantity = Qty of Z.t [@@ocaml.unboxed] @@ -1246,7 +1250,7 @@ module Delayed_transaction = struct end module Upgrade = struct - type t = {hash : hash; timestamp : quantity} + type t = {hash : hash; timestamp : Time.Protocol.t} let of_rlp = function | Rlp.List [Value hash_bytes; Value timestamp] -> @@ -1254,7 +1258,7 @@ module Upgrade = struct hash_bytes |> Bytes.to_string |> Hex.of_string |> Hex.show |> hash_of_string in - let timestamp = decode_number timestamp in + let timestamp = timestamp_of_bytes timestamp in Some {hash; timestamp} | _ -> None @@ -1262,9 +1266,16 @@ module Upgrade = struct match bytes |> Rlp.decode with Ok rlp -> of_rlp rlp | _ -> None let to_bytes {hash; timestamp} = - let hash_bytes = hash_to_bytes hash |> String.to_bytes in - let timestamp_bytes = encode_number timestamp in - Rlp.(encode (List [Value hash_bytes; Value timestamp_bytes])) + let hash = hash_to_bytes hash |> String.to_bytes in + let timestamp = timestamp_to_bytes timestamp in + Rlp.(encode (List [Value hash; Value timestamp])) + + let encoding = + let open Data_encoding in + conv + (fun {hash = Hash (Hex hash); timestamp} -> (hash, timestamp)) + (fun (hash, timestamp) -> {hash = Hash (Hex hash); timestamp}) + (tup2 string Time.Protocol.encoding) end module Evm_events = struct @@ -1281,12 +1292,26 @@ module Evm_events = struct | _ -> None let pp fmt = function - | Upgrade_event {hash; timestamp = Qty timestamp} -> + | Upgrade_event {hash; timestamp} -> Format.fprintf fmt - "upgrade:@ hash %a,@ timestamp %a" + "upgrade:@ hash %a,@ timestamp: %a" pp_hash hash - Z.pp_print + Time.Protocol.pp_hum timestamp + + let encoding = + let open Data_encoding in + union + [ + (let tag = "kernel_upgrade" in + case + ~title:tag + (Tag 0) + (obj2 (req "kind" string) (req "event" Upgrade.encoding)) + (function + | Upgrade_event upgrade -> Some ("kernel_upgrade", upgrade)) + (fun (_, upgrade) -> Upgrade_event upgrade)); + ] end diff --git a/etherlink/bin_node/lib_dev/evm_events_follower.ml b/etherlink/bin_node/lib_dev/evm_events_follower.ml index 02ce75b903ce..5e043b1c5dd0 100644 --- a/etherlink/bin_node/lib_dev/evm_events_follower.ml +++ b/etherlink/bin_node/lib_dev/evm_events_follower.ml @@ -73,8 +73,10 @@ let read_from_rollup_node path level rollup_node_endpoint = () let on_new_event ({backend; _} : Types.state) event = + let open Lwt_syntax in let open Ethereum_types in let (module Backend) = backend in + let* () = Evm_events_follower_events.new_event event in match event with | Evm_events.Upgrade_event upgrade -> let payload = Upgrade.to_bytes upgrade |> String.of_bytes in diff --git a/etherlink/bin_node/lib_dev/evm_events_follower_events.ml b/etherlink/bin_node/lib_dev/evm_events_follower_events.ml index ef01d095c24a..21ec96474dc3 100644 --- a/etherlink/bin_node/lib_dev/evm_events_follower_events.ml +++ b/etherlink/bin_node/lib_dev/evm_events_follower_events.ml @@ -27,6 +27,15 @@ module Event = struct ("index", Data_encoding.int31) ("level", Data_encoding.int32) + let new_event = + declare_1 + ~section + ~name:"evm_events_new_event" + ~msg:"Evm events follower: applying {event}" + ~level:Notice + ~pp1:Ethereum_types.Evm_events.pp + ("event", Ethereum_types.Evm_events.encoding) + let pp_int32 fmt i = Format.fprintf fmt "%ld" i let shutdown = @@ -44,3 +53,5 @@ let shutdown = Internal_event.Simple.emit Event.shutdown let unreadable_event (index, level) = Internal_event.Simple.emit Event.unreadable_event (index, level) + +let new_event event = Internal_event.Simple.emit Event.new_event event -- GitLab From 115bb19beff132348206ed06b925cf71616b5e13 Mon Sep 17 00:00:00 2001 From: Sylvain Ribstein Date: Mon, 26 Feb 2024 15:52:14 +0100 Subject: [PATCH 3/3] etherlink/node: refactor make upgrade cmd --- etherlink/bin_node/main.ml | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/etherlink/bin_node/main.ml b/etherlink/bin_node/main.ml index 409c7d08b91f..dbe0f2e1f162 100644 --- a/etherlink/bin_node/main.ml +++ b/etherlink/bin_node/main.ml @@ -1009,16 +1009,11 @@ let make_upgrade_command = "After activation timestamp, the kernel will upgrade to this value" Params.timestamp @@ stop) - (fun () root_hash activation_timestamp () -> - let open Rlp in - let activation_timestamp = - Ethereum_types.timestamp_to_bytes activation_timestamp + (fun () root_hash timestamp () -> + let payload = + Ethereum_types.Upgrade.( + to_bytes @@ {hash = Hash (Hex root_hash); timestamp}) in - let root_hash_bytes = Hex.to_bytes_exn (`Hex root_hash) in - let kernel_upgrade = - List [Value root_hash_bytes; Value activation_timestamp] - in - let payload = encode kernel_upgrade in Printf.printf "%s%!" Hex.(of_bytes payload |> show) ; return_unit) -- GitLab