From 10035da1e7ae30b92b62778fcbafa97ec0f220b8 Mon Sep 17 00:00:00 2001 From: Victor Dumitrescu Date: Wed, 11 Sep 2024 11:53:51 +0200 Subject: [PATCH 1/2] RISC-V PVM: Define PVM input type in Rust --- src/lib_riscv/pvm/backend.ml | 18 ++--- src/lib_riscv/pvm/backend.mli | 10 +-- .../lib_sc_rollup_node/riscv_pvm.ml | 23 +++---- src/riscv/lib/octez_riscv_api.ml | 6 +- src/riscv/lib/octez_riscv_api.mli | 6 +- src/riscv/lib/src/ocaml_api.rs | 65 ++++++++++--------- 6 files changed, 61 insertions(+), 67 deletions(-) diff --git a/src/lib_riscv/pvm/backend.ml b/src/lib_riscv/pvm/backend.ml index db1c8b9ef25c..414f5b7aeffc 100644 --- a/src/lib_riscv/pvm/backend.ml +++ b/src/lib_riscv/pvm/backend.ml @@ -18,6 +18,10 @@ type state = Storage.State.t type status = Api.status +type reveal_data = Api.reveal_data + +type input = Api.input + (* The kernel debug logging function (`string -> unit Lwt.t`) passed by the node * to [compute_step] and [compute_step_many] cannot be passed directly * to the Rust backend, which expects a `u8 -> ()` function and cannot run Lwt @@ -68,16 +72,4 @@ let get_current_level state = Lwt.return (Api.octez_riscv_get_level state) let state_hash state = Api.octez_riscv_state_hash state -let set_input state level message_counter payload = - Lwt.return - (Api.octez_riscv_set_input_message - state - level - message_counter - (Bytes.of_string payload)) - -let set_metadata state address origination_level = - Lwt.return (Api.octez_riscv_set_metadata state address origination_level) - -let reveal_raw_data state raw_data = - Lwt.return (Api.octez_riscv_reveal_raw_data state raw_data) +let set_input state input = Lwt.return (Api.octez_riscv_set_input state input) diff --git a/src/lib_riscv/pvm/backend.mli b/src/lib_riscv/pvm/backend.mli index 00e49957e2fb..491aa758a785 100644 --- a/src/lib_riscv/pvm/backend.mli +++ b/src/lib_riscv/pvm/backend.mli @@ -16,6 +16,10 @@ type state = Storage.State.t type status = Octez_riscv_api.status +type reveal_data = Octez_riscv_api.reveal_data + +type input = Octez_riscv_api.input + val compute_step_many : ?reveal_builtins:reveals -> ?write_debug:write_debug -> @@ -42,8 +46,4 @@ val get_current_level : state -> int32 option Lwt.t val state_hash : state -> bytes -val set_input : state -> int32 -> int64 -> string -> state Lwt.t - -val set_metadata : state -> bytes -> int32 -> state Lwt.t - -val reveal_raw_data : state -> string -> state Lwt.t +val set_input : state -> input -> state Lwt.t diff --git a/src/proto_alpha/lib_sc_rollup_node/riscv_pvm.ml b/src/proto_alpha/lib_sc_rollup_node/riscv_pvm.ml index d30d91e1ef4e..aced8941e598 100644 --- a/src/proto_alpha/lib_sc_rollup_node/riscv_pvm.ml +++ b/src/proto_alpha/lib_sc_rollup_node/riscv_pvm.ml @@ -76,22 +76,23 @@ module PVM : (Raw_level.of_int32_exn level, Z.of_int64 message_counter))) | WaitingForMetadata -> return Sc_rollup.(Needs_reveal Reveal_metadata) - let set_input input state = + let to_pvm_input (input : Sc_rollup.input) : Backend.input = match input with | Sc_rollup.Inbox_message {inbox_level; message_counter; payload} -> - Backend.set_input - state - (Raw_level.to_int32 inbox_level) - (Z.to_int64 message_counter) - (Sc_rollup.Inbox_message.unsafe_to_string payload) + InboxMessage + ( Raw_level.to_int32 inbox_level, + Z.to_int64 message_counter, + Sc_rollup.Inbox_message.unsafe_to_string payload ) | Sc_rollup.(Reveal (Metadata {address; origination_level})) -> - Backend.set_metadata - state - (Sc_rollup.Address.to_bytes address) - (Raw_level.to_int32 origination_level) - | Sc_rollup.(Reveal (Raw_data data)) -> Backend.reveal_raw_data state data + Reveal + (Metadata + ( Sc_rollup.Address.to_bytes address, + Raw_level.to_int32 origination_level )) + | Sc_rollup.(Reveal (Raw_data data)) -> Reveal (RawData data) | _ -> assert false + let set_input input state = Backend.set_input state (to_pvm_input input) + let eval state = Backend.compute_step state let verify_proof ~is_reveal_enabled:_ _input = function (_ : proof) -> . diff --git a/src/riscv/lib/octez_riscv_api.ml b/src/riscv/lib/octez_riscv_api.ml index e0d6237ba738..1a25ac9e5a61 100644 --- a/src/riscv/lib/octez_riscv_api.ml +++ b/src/riscv/lib/octez_riscv_api.ml @@ -8,6 +8,8 @@ type repo type state type id type status = Evaluating | WaitingForInput | WaitingForMetadata +type reveal_data = RawData of string | Metadata of bytes * int32 +type input = InboxMessage of int32 * int64 * string | Reveal of reveal_data external octez_riscv_id_unsafe_of_raw_bytes: bytes -> id = "octez_riscv_id_unsafe_of_raw_bytes" external octez_riscv_storage_id_to_raw_bytes: id -> bytes = "octez_riscv_storage_id_to_raw_bytes" external octez_riscv_storage_id_equal: id -> id -> bool = "octez_riscv_storage_id_equal" @@ -27,8 +29,6 @@ external octez_riscv_get_tick: state -> int64 = "octez_riscv_get_tick" external octez_riscv_get_level: state -> int32 option = "octez_riscv_get_level" external octez_riscv_install_boot_sector: state -> bytes -> state = "octez_riscv_install_boot_sector" external octez_riscv_state_hash: state -> bytes = "octez_riscv_state_hash" -external octez_riscv_set_input_message: state -> int32 -> int64 -> bytes -> state = "octez_riscv_set_input_message" -external octez_riscv_set_metadata: state -> bytes -> int32 -> state = "octez_riscv_set_metadata" -external octez_riscv_reveal_raw_data: state -> string -> state = "octez_riscv_reveal_raw_data" +external octez_riscv_set_input: state -> input -> state = "octez_riscv_set_input" external octez_riscv_get_message_counter: state -> int64 = "octez_riscv_get_message_counter" external octez_riscv_storage_export_snapshot: repo -> id -> string -> (unit, [`Msg of string]) result = "octez_riscv_storage_export_snapshot" diff --git a/src/riscv/lib/octez_riscv_api.mli b/src/riscv/lib/octez_riscv_api.mli index e0d6237ba738..1a25ac9e5a61 100644 --- a/src/riscv/lib/octez_riscv_api.mli +++ b/src/riscv/lib/octez_riscv_api.mli @@ -8,6 +8,8 @@ type repo type state type id type status = Evaluating | WaitingForInput | WaitingForMetadata +type reveal_data = RawData of string | Metadata of bytes * int32 +type input = InboxMessage of int32 * int64 * string | Reveal of reveal_data external octez_riscv_id_unsafe_of_raw_bytes: bytes -> id = "octez_riscv_id_unsafe_of_raw_bytes" external octez_riscv_storage_id_to_raw_bytes: id -> bytes = "octez_riscv_storage_id_to_raw_bytes" external octez_riscv_storage_id_equal: id -> id -> bool = "octez_riscv_storage_id_equal" @@ -27,8 +29,6 @@ external octez_riscv_get_tick: state -> int64 = "octez_riscv_get_tick" external octez_riscv_get_level: state -> int32 option = "octez_riscv_get_level" external octez_riscv_install_boot_sector: state -> bytes -> state = "octez_riscv_install_boot_sector" external octez_riscv_state_hash: state -> bytes = "octez_riscv_state_hash" -external octez_riscv_set_input_message: state -> int32 -> int64 -> bytes -> state = "octez_riscv_set_input_message" -external octez_riscv_set_metadata: state -> bytes -> int32 -> state = "octez_riscv_set_metadata" -external octez_riscv_reveal_raw_data: state -> string -> state = "octez_riscv_reveal_raw_data" +external octez_riscv_set_input: state -> input -> state = "octez_riscv_set_input" external octez_riscv_get_message_counter: state -> int64 = "octez_riscv_get_message_counter" external octez_riscv_storage_export_snapshot: repo -> id -> string -> (unit, [`Msg of string]) result = "octez_riscv_storage_export_snapshot" diff --git a/src/riscv/lib/src/ocaml_api.rs b/src/riscv/lib/src/ocaml_api.rs index 643530e2a1cb..283a1f3855db 100644 --- a/src/riscv/lib/src/ocaml_api.rs +++ b/src/riscv/lib/src/ocaml_api.rs @@ -37,6 +37,20 @@ pub enum Status { WaitingForMetadata, } +#[derive(ocaml::FromValue, ocaml::ToValue)] +#[ocaml::sig("RawData of string | Metadata of bytes * int32")] +pub enum RevealData<'a> { + RawData(&'a [u8]), + Metadata(&'a [u8], u32), +} + +#[derive(ocaml::FromValue, ocaml::ToValue)] +#[ocaml::sig("InboxMessage of int32 * int64 * string | Reveal of reveal_data")] +pub enum Input<'a> { + InboxMessage(u32, u64, &'a [u8]), + Reveal(RevealData<'a>), +} + impl From for Status { fn from(item: PvmStatus) -> Self { Status::try_from(item as u8).expect("Invalid conversion") @@ -255,38 +269,25 @@ pub fn octez_riscv_state_hash(state: Pointer) -> [u8; 32] { } #[ocaml::func] -#[ocaml::sig("state -> int32 -> int64 -> bytes -> state")] -pub fn octez_riscv_set_input_message( - state: Pointer, - level: u32, - message_counter: u64, - input: &[u8], -) -> Pointer { - State( - state - .as_ref() - .0 - .set_input_message(level, message_counter, input.to_vec()), - ) - .into() -} - -#[ocaml::func] -#[ocaml::sig("state -> bytes -> int32 -> state")] -pub fn octez_riscv_set_metadata( - state: Pointer, - address: &[u8], - origination_level: u32, -) -> Pointer { - let address: &[u8; 20] = address.try_into().expect("Unexpected rollup address size"); - State(state.as_ref().0.set_metadata(address, origination_level)).into() -} - -#[ocaml::func] -#[ocaml::sig("state -> string -> state")] -pub fn octez_riscv_reveal_raw_data(_state: Pointer, _data: &[u8]) -> Pointer { - // TODO: RV-110. Support all revelations in set_input method - todo!() +#[ocaml::sig("state -> input -> state")] +pub fn octez_riscv_set_input(state: Pointer, input: Input) -> Pointer { + match input { + Input::InboxMessage(level, message_counter, payload) => State( + state + .as_ref() + .0 + .set_input_message(level, message_counter, payload.to_vec()), + ) + .into(), + Input::Reveal(RevealData::Metadata(address, origination_level)) => { + let address: &[u8; 20] = address.try_into().expect("Unexpected rollup address size"); + State(state.as_ref().0.set_metadata(address, origination_level)).into() + } + _ => { + // TODO: RV-110. Support all revelations in set_input method + todo!() + } + } } #[ocaml::func] -- GitLab From 67b8d6580ed5ed9a5f66a3a9d43aa6d1848d7309 Mon Sep 17 00:00:00 2001 From: Victor Dumitrescu Date: Tue, 17 Sep 2024 10:18:46 +0200 Subject: [PATCH 2/2] Proto_021/rollup node: use input type in RISC-V PVM API --- .../lib_sc_rollup_node/riscv_pvm.ml | 44 ++++++++++++------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/src/proto_021_PsquebeC/lib_sc_rollup_node/riscv_pvm.ml b/src/proto_021_PsquebeC/lib_sc_rollup_node/riscv_pvm.ml index a37e11add049..aced8941e598 100644 --- a/src/proto_021_PsquebeC/lib_sc_rollup_node/riscv_pvm.ml +++ b/src/proto_021_PsquebeC/lib_sc_rollup_node/riscv_pvm.ml @@ -62,24 +62,36 @@ module PVM : let is_input_state ~is_reveal_enabled:_ state = let open Lwt_syntax in - let* level = Backend.get_current_level state in - match level with - | None -> return Sc_rollup.Initial - | Some level -> - let* message_counter = Backend.get_message_counter state in - return - (Sc_rollup.First_after - (Raw_level.of_int32_exn level, Z.of_int64 message_counter)) - - let set_input input state = + let* status = Backend.get_status state in + match status with + | Evaluating -> return Sc_rollup.No_input_required + | WaitingForInput -> ( + let* level = Backend.get_current_level state in + match level with + | None -> return Sc_rollup.Initial + | Some level -> + let* message_counter = Backend.get_message_counter state in + return + (Sc_rollup.First_after + (Raw_level.of_int32_exn level, Z.of_int64 message_counter))) + | WaitingForMetadata -> return Sc_rollup.(Needs_reveal Reveal_metadata) + + let to_pvm_input (input : Sc_rollup.input) : Backend.input = match input with | Sc_rollup.Inbox_message {inbox_level; message_counter; payload} -> - Backend.set_input - state - (Raw_level.to_int32 inbox_level) - (Z.to_int64 message_counter) - (Sc_rollup.Inbox_message.unsafe_to_string payload) - | Sc_rollup.Reveal _ -> assert false + InboxMessage + ( Raw_level.to_int32 inbox_level, + Z.to_int64 message_counter, + Sc_rollup.Inbox_message.unsafe_to_string payload ) + | Sc_rollup.(Reveal (Metadata {address; origination_level})) -> + Reveal + (Metadata + ( Sc_rollup.Address.to_bytes address, + Raw_level.to_int32 origination_level )) + | Sc_rollup.(Reveal (Raw_data data)) -> Reveal (RawData data) + | _ -> assert false + + let set_input input state = Backend.set_input state (to_pvm_input input) let eval state = Backend.compute_step state -- GitLab