diff --git a/etherlink/bin_node/lib_dev/encodings/ethereum_types.ml b/etherlink/bin_node/lib_dev/encodings/ethereum_types.ml index ff52ee60b3c5b5712f0aa6532610d91db3958600..9abad215be0ab8c17097d07e1e74219e349a1bb3 100644 --- a/etherlink/bin_node/lib_dev/encodings/ethereum_types.ml +++ b/etherlink/bin_node/lib_dev/encodings/ethereum_types.ml @@ -62,6 +62,18 @@ 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 + +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] @@ -1238,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] -> @@ -1246,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 @@ -1254,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 @@ -1273,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 02ce75b903ce551fd6ad11615bf1bf21b478da48..5e043b1c5dd0714e5c3062c6c764296529fa328c 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 ef01d095c24acb8699f1cf44cdbadde3e5032437..21ec96474dc3cc9e12aba400df97cf88ebda48b7 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 diff --git a/etherlink/bin_node/lib_dev/helpers.ml b/etherlink/bin_node/lib_dev/helpers.ml index e8cd7c674efdd6a5581ca751f712ceed5adb493e..34d73eb2a48ab3decb15bb538aaf8467305fe456 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 468b2281a100834a6ea7739c47f42729001eadd3..0cf2fd0dfac94ea281b4b14a407c1a5e8edb9a70 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 965513539ebe5f7ca21a00a4e8d8810e436e4909..ec684b0f234f1c7426d42e88ccc67f8a19d4dc53 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 cf9f122ace6d885564aabf8bc1199c4f4f4fdeef..dbe0f2e1f162c51eb8b217db60baa4e4985501ce 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 = - Evm_node_lib_dev.Helpers.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) @@ -1051,7 +1046,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