From 289c8db7f5c11d4e073ccd70e47510bfba6bbc97 Mon Sep 17 00:00:00 2001 From: Felix Puscasu Date: Fri, 31 Jan 2025 15:04:55 +0000 Subject: [PATCH 1/2] RISC-V/PROTO: Update riscv backend types --- src/lib_riscv/pvm/backend.ml | 40 +++++++++++++---- src/lib_riscv/pvm/backend.mli | 10 +++-- .../lib_sc_rollup_node/riscv_pvm.ml | 10 +---- .../lib_sc_rollup_node/riscv_pvm.ml | 10 +---- .../lib_sc_rollup_node/riscv_pvm.ml | 16 +++---- src/riscv/lib/octez_riscv_api.ml | 5 +-- src/riscv/lib/octez_riscv_api.mli | 5 +-- src/riscv/lib/src/ocaml_api.rs | 44 +++++++------------ 8 files changed, 70 insertions(+), 70 deletions(-) diff --git a/src/lib_riscv/pvm/backend.ml b/src/lib_riscv/pvm/backend.ml index 1da8aef28638..5b52da9802e0 100644 --- a/src/lib_riscv/pvm/backend.ml +++ b/src/lib_riscv/pvm/backend.ml @@ -18,11 +18,13 @@ type state = Api.state type status = Api.status -type reveal_data = Api.reveal_data +type input = Inbox_message of int32 * int64 * string | Reveal of string -type input = Api.input - -type input_request = Api.input_request +type input_request = + | No_input_required + | Initial + | First_after of int32 * int64 + | Needs_reveal of string type proof = Api.proof @@ -55,6 +57,22 @@ let from_api_output : Api.output -> output = encoded_message = String.of_bytes encoded_message; } +let to_api_input : input -> Api.input = + fun input -> + match input with + | Inbox_message (outbox_level, message_index, payload) -> + InboxMessage (outbox_level, message_index, Bytes.of_string payload) + | Reveal raw_reveal -> Reveal (Bytes.of_string raw_reveal) + +let from_api_input_request : Api.input_request -> input_request = + fun input_request -> + match input_request with + | Api.NoInputRequired -> No_input_required + | Api.Initial -> Initial + | Api.FirstAfter (outbox_level, message_index) -> + First_after (outbox_level, message_index) + | Api.NeedsReveal raw_reveal -> Needs_reveal (String.of_bytes raw_reveal) + (* 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 @@ -101,7 +119,7 @@ module Mutable_state = struct riscv_hash_to_rollup_state_hash @@ Api.octez_riscv_mut_state_hash state let set_input state input = - Lwt.return (Api.octez_riscv_mut_set_input state input) + Lwt.return (Api.octez_riscv_mut_set_input state (to_api_input input)) let get_reveal_request state = Lwt.return (String.of_bytes @@ Api.octez_riscv_mut_get_reveal_request state) @@ -142,7 +160,8 @@ let get_current_level state = Lwt.return (Api.octez_riscv_get_level state) let state_hash state = riscv_hash_to_rollup_state_hash @@ Api.octez_riscv_state_hash state -let set_input state input = Lwt.return (Api.octez_riscv_set_input state input) +let set_input state input = + Lwt.return (Api.octez_riscv_set_input state (to_api_input input)) let proof_start_state proof = riscv_hash_to_rollup_state_hash @@ Api.octez_riscv_proof_start_state proof @@ -150,9 +169,14 @@ let proof_start_state proof = let proof_stop_state proof = riscv_hash_to_rollup_state_hash @@ Api.octez_riscv_proof_stop_state proof -let verify_proof input proof = Api.octez_riscv_verify_proof input proof +let verify_proof input proof = + let open Option_syntax in + let input = Option.map to_api_input input in + let* input_request = Api.octez_riscv_verify_proof input proof in + return @@ from_api_input_request input_request -let produce_proof input state = Api.octez_riscv_produce_proof input state +let produce_proof input state = + Api.octez_riscv_produce_proof (Option.map to_api_input input) state let serialise_proof proof = Api.octez_riscv_serialise_proof proof diff --git a/src/lib_riscv/pvm/backend.mli b/src/lib_riscv/pvm/backend.mli index e26ff01df752..64a1781c97b2 100644 --- a/src/lib_riscv/pvm/backend.mli +++ b/src/lib_riscv/pvm/backend.mli @@ -18,11 +18,13 @@ type state = Api.state type status = Api.status -type reveal_data = Api.reveal_data +type input = Inbox_message of int32 * int64 * string | Reveal of string -type input = Api.input - -type input_request = Api.input_request +type input_request = + | No_input_required + | Initial + | First_after of int32 * int64 + | Needs_reveal of string type proof = Api.proof 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 a1accfb5183f..bed91a58cf88 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 @@ -80,17 +80,11 @@ module PVM : let to_pvm_input (input : Sc_rollup.input) : Backend.input = match input with | Sc_rollup.Inbox_message {inbox_level; message_counter; payload} -> - InboxMessage + Inbox_message ( 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 + | Sc_rollup.(Reveal _reveal_data) -> assert false let set_input input state = Backend.set_input state (to_pvm_input input) diff --git a/src/proto_022_PsRiotum/lib_sc_rollup_node/riscv_pvm.ml b/src/proto_022_PsRiotum/lib_sc_rollup_node/riscv_pvm.ml index 75bbb1f1ff57..25811c211ec9 100644 --- a/src/proto_022_PsRiotum/lib_sc_rollup_node/riscv_pvm.ml +++ b/src/proto_022_PsRiotum/lib_sc_rollup_node/riscv_pvm.ml @@ -22,17 +22,11 @@ module Ctxt_wrapper = Context_wrapper.Riscv let to_pvm_input (input : Sc_rollup.input) : Backend.input = match input with | Sc_rollup.Inbox_message {inbox_level; message_counter; payload} -> - InboxMessage + Inbox_message ( 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 + | Sc_rollup.(Reveal _reveal_data) -> assert false let of_pvm_input_request (_input_request : Backend.input_request) : Sc_rollup.input_request = 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 80a0f982ef91..b01b87d0d685 100644 --- a/src/proto_alpha/lib_sc_rollup_node/riscv_pvm.ml +++ b/src/proto_alpha/lib_sc_rollup_node/riscv_pvm.ml @@ -22,17 +22,17 @@ module Ctxt_wrapper = Context_wrapper.Riscv let to_pvm_input (input : Sc_rollup.input) : Backend.input = match input with | Sc_rollup.Inbox_message {inbox_level; message_counter; payload} -> - InboxMessage + Inbox_message ( 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 + | Sc_rollup.(Reveal (Metadata data)) -> + let bytes = + Data_encoding.Binary.to_string_exn Sc_rollup.Metadata.encoding data + in + Reveal bytes + (* TODO: RV-504 Encode remaining types of reveal *) + | Sc_rollup.(Reveal _) -> assert false let of_pvm_input_request (_input_request : Backend.input_request) : Sc_rollup.input_request = diff --git a/src/riscv/lib/octez_riscv_api.ml b/src/riscv/lib/octez_riscv_api.ml index 451529ea65a7..fc053421a582 100644 --- a/src/riscv/lib/octez_riscv_api.ml +++ b/src/riscv/lib/octez_riscv_api.ml @@ -9,9 +9,8 @@ type state type mut_state type id type status = Evaluating | WaitingForInput | WaitingForReveal -type reveal_data = RawData of string | Metadata of bytes * int32 -type input = InboxMessage of int32 * int64 * string | Reveal of reveal_data -type input_request +type input = InboxMessage of int32 * int64 * bytes | Reveal of bytes +type input_request = NoInputRequired | Initial | FirstAfter of int32 * int64 | NeedsReveal of bytes type output_info = {message_index : int64; outbox_level : int32} type output = {info : output_info; encoded_message : bytes} type output_proof diff --git a/src/riscv/lib/octez_riscv_api.mli b/src/riscv/lib/octez_riscv_api.mli index 451529ea65a7..fc053421a582 100644 --- a/src/riscv/lib/octez_riscv_api.mli +++ b/src/riscv/lib/octez_riscv_api.mli @@ -9,9 +9,8 @@ type state type mut_state type id type status = Evaluating | WaitingForInput | WaitingForReveal -type reveal_data = RawData of string | Metadata of bytes * int32 -type input = InboxMessage of int32 * int64 * string | Reveal of reveal_data -type input_request +type input = InboxMessage of int32 * int64 * bytes | Reveal of bytes +type input_request = NoInputRequired | Initial | FirstAfter of int32 * int64 | NeedsReveal of bytes type output_info = {message_index : int64; outbox_level : int32} type output = {info : output_info; encoded_message : bytes} type output_proof diff --git a/src/riscv/lib/src/ocaml_api.rs b/src/riscv/lib/src/ocaml_api.rs index a67e6081f293..3331f734ea55 100644 --- a/src/riscv/lib/src/ocaml_api.rs +++ b/src/riscv/lib/src/ocaml_api.rs @@ -14,7 +14,6 @@ use num_enum::{IntoPrimitive, TryFromPrimitive}; use ocaml::{Pointer, ToValue}; use pointer_apply::{ApplyReadOnly, apply_imm, apply_mut}; use sha2::{Digest, Sha256}; -use tezos_smart_rollup_constants::core::{METADATA_LENGTH, ROLLUP_ADDRESS_LENGTH}; use crate::{ pvm::{ @@ -101,13 +100,6 @@ impl From for PvmStatus { } } -#[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), -} - /// Wrapper to convert a Rust allocated byte-array to an OCaml allocated Bytes type pub struct BytesWrapper(Box<[u8]>); @@ -116,19 +108,23 @@ unsafe impl ocaml::ToValue for BytesWrapper { unsafe { ocaml::Value::bytes(&self.0) } } } - -#[derive(ocaml::FromValue, ocaml::ToValue)] -#[ocaml::sig("InboxMessage of int32 * int64 * string | Reveal of reveal_data")] +/// Input values are passed into the PVM after an input request has been made. +#[derive(ocaml::FromValue)] +#[ocaml::sig("InboxMessage of int32 * int64 * bytes | Reveal of bytes")] pub enum Input<'a> { InboxMessage(u32, u64, &'a [u8]), - Reveal(RevealData<'a>), + Reveal(&'a [u8]), } -// TODO RV-336 Implement `InputRequest` type -/// A value of this type could only be returned as part of successfully verifying -/// a proof, which is not yet implemented. It is therefore only mocked for now. -#[ocaml::sig] -pub struct InputRequest; +/// A value of this type could only be returned as part of successfully verifying a proof. +#[derive(ocaml::ToValue)] +#[ocaml::sig("NoInputRequired | Initial | FirstAfter of int32 * int64 | NeedsReveal of bytes")] +pub enum InputRequest { + NoInputRequired, + Initial, + FirstAfter(u32, u64), + NeedsReveal(BytesWrapper), +} ocaml::custom!(InputRequest); @@ -441,16 +437,8 @@ fn apply_set_input(pvm: &mut NodePvm, input: Input) { Input::InboxMessage(level, message_counter, payload) => { pvm.set_input_message(level, message_counter, payload.to_vec()) } - Input::Reveal(RevealData::Metadata(address, origination_level)) => { - let mut metadata_response_buffer = [0u8; METADATA_LENGTH]; - metadata_response_buffer[..ROLLUP_ADDRESS_LENGTH].copy_from_slice(address); - metadata_response_buffer[ROLLUP_ADDRESS_LENGTH..] - .copy_from_slice(&origination_level.to_be_bytes()); - pvm.set_reveal_response(&metadata_response_buffer) - } - _ => { - // TODO: RV-110. Support all revelations in set_input method - todo!() + Input::Reveal(reveal_data) => { + pvm.set_reveal_response(reveal_data); } } } @@ -534,7 +522,7 @@ pub unsafe fn octez_riscv_verify_proof( } // TODO: RV-336: Need to construct InputRequest - Some(Pointer::from(InputRequest)) + Some(todo!()) } #[ocaml::func] -- GitLab From e9c3f2c1b8cd5e0fb89f12447ce25d1a359dd230 Mon Sep 17 00:00:00 2001 From: Felix Puscasu Date: Thu, 27 Feb 2025 11:18:02 +0000 Subject: [PATCH 2/2] RISC-V: Use OCaml naming convention for riscv OCaml bindings --- src/lib_riscv/pvm/backend.ml | 13 ++++++------- src/lib_riscv/pvm/backend.mli | 2 ++ .../lib_sc_rollup_node/riscv_pvm.ml | 4 ++-- .../lib_sc_rollup_node/riscv_pvm.ml | 4 ++-- src/proto_alpha/lib_sc_rollup_node/riscv_pvm.ml | 4 ++-- src/riscv/lib/octez_riscv_api.ml | 6 +++--- src/riscv/lib/octez_riscv_api.mli | 6 +++--- src/riscv/lib/src/ocaml_api.rs | 11 ++++++----- 8 files changed, 26 insertions(+), 24 deletions(-) diff --git a/src/lib_riscv/pvm/backend.ml b/src/lib_riscv/pvm/backend.ml index 5b52da9802e0..7605f135d53f 100644 --- a/src/lib_riscv/pvm/backend.ml +++ b/src/lib_riscv/pvm/backend.ml @@ -61,17 +61,17 @@ let to_api_input : input -> Api.input = fun input -> match input with | Inbox_message (outbox_level, message_index, payload) -> - InboxMessage (outbox_level, message_index, Bytes.of_string payload) + Inbox_message (outbox_level, message_index, Bytes.of_string payload) | Reveal raw_reveal -> Reveal (Bytes.of_string raw_reveal) let from_api_input_request : Api.input_request -> input_request = fun input_request -> match input_request with - | Api.NoInputRequired -> No_input_required + | Api.No_input_required -> No_input_required | Api.Initial -> Initial - | Api.FirstAfter (outbox_level, message_index) -> + | Api.First_after (outbox_level, message_index) -> First_after (outbox_level, message_index) - | Api.NeedsReveal raw_reveal -> Needs_reveal (String.of_bytes raw_reveal) + | Api.Needs_reveal raw_reveal -> Needs_reveal (String.of_bytes raw_reveal) (* The kernel debug logging function (`string -> unit Lwt.t`) passed by the node * to [compute_step] and [compute_step_many] cannot be passed directly @@ -170,10 +170,9 @@ let proof_stop_state proof = riscv_hash_to_rollup_state_hash @@ Api.octez_riscv_proof_stop_state proof let verify_proof input proof = - let open Option_syntax in let input = Option.map to_api_input input in - let* input_request = Api.octez_riscv_verify_proof input proof in - return @@ from_api_input_request input_request + let input_request = Api.octez_riscv_verify_proof input proof in + Option.map from_api_input_request input_request let produce_proof input state = Api.octez_riscv_produce_proof (Option.map to_api_input input) state diff --git a/src/lib_riscv/pvm/backend.mli b/src/lib_riscv/pvm/backend.mli index 64a1781c97b2..922b1b10927a 100644 --- a/src/lib_riscv/pvm/backend.mli +++ b/src/lib_riscv/pvm/backend.mli @@ -18,8 +18,10 @@ type state = Api.state type status = Api.status +(* Mirrors Api.input but requires manual conversion *) type input = Inbox_message of int32 * int64 * string | Reveal of string +(* Mirrors Api.input_request but requires manual conversion *) type input_request = | No_input_required | Initial 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 bed91a58cf88..635314e3b66c 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 @@ -64,7 +64,7 @@ module PVM : let* status = Backend.get_status state in match status with | Evaluating -> return Sc_rollup.No_input_required - | WaitingForInput -> ( + | Waiting_for_input -> ( let* level = Backend.get_current_level state in match level with | None -> return Sc_rollup.Initial @@ -73,7 +73,7 @@ module PVM : return (Sc_rollup.First_after (Raw_level.of_int32_exn level, Z.of_int64 message_counter))) - | WaitingForReveal -> + | Waiting_for_reveal -> (* TODO: RV-407: Rollup node handles reveal request from riscv pvm *) assert false diff --git a/src/proto_022_PsRiotum/lib_sc_rollup_node/riscv_pvm.ml b/src/proto_022_PsRiotum/lib_sc_rollup_node/riscv_pvm.ml index 25811c211ec9..3056f8c47f47 100644 --- a/src/proto_022_PsRiotum/lib_sc_rollup_node/riscv_pvm.ml +++ b/src/proto_022_PsRiotum/lib_sc_rollup_node/riscv_pvm.ml @@ -39,7 +39,7 @@ let make_is_input_state (get_status : 'a -> Backend.status Lwt.t) let* status = get_status state in match status with | Evaluating -> return Sc_rollup.No_input_required - | WaitingForInput -> ( + | Waiting_for_input -> ( let* level = get_current_level state in match level with | None -> return Sc_rollup.Initial @@ -48,7 +48,7 @@ let make_is_input_state (get_status : 'a -> Backend.status Lwt.t) return (Sc_rollup.First_after (Raw_level.of_int32_exn level, Z.of_int64 message_counter))) - | WaitingForReveal -> + | Waiting_for_reveal -> (* TODO: RV-407: Rollup node handles reveal request from riscv pvm *) assert false 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 b01b87d0d685..c710d3a016de 100644 --- a/src/proto_alpha/lib_sc_rollup_node/riscv_pvm.ml +++ b/src/proto_alpha/lib_sc_rollup_node/riscv_pvm.ml @@ -46,7 +46,7 @@ let make_is_input_state (get_status : 'a -> Backend.status Lwt.t) let* status = get_status state in match status with | Evaluating -> return Sc_rollup.No_input_required - | WaitingForInput -> ( + | Waiting_for_input -> ( let* level = get_current_level state in match level with | None -> return Sc_rollup.Initial @@ -55,7 +55,7 @@ let make_is_input_state (get_status : 'a -> Backend.status Lwt.t) return (Sc_rollup.First_after (Raw_level.of_int32_exn level, Z.of_int64 message_counter))) - | WaitingForReveal -> + | Waiting_for_reveal -> let* reveal_request_string = get_reveal_request state in let reveal_request = (* TODO: RV-501: Errors during decode reveal request should not be fatal *) diff --git a/src/riscv/lib/octez_riscv_api.ml b/src/riscv/lib/octez_riscv_api.ml index fc053421a582..399eda0d36d7 100644 --- a/src/riscv/lib/octez_riscv_api.ml +++ b/src/riscv/lib/octez_riscv_api.ml @@ -8,9 +8,9 @@ type repo type state type mut_state type id -type status = Evaluating | WaitingForInput | WaitingForReveal -type input = InboxMessage of int32 * int64 * bytes | Reveal of bytes -type input_request = NoInputRequired | Initial | FirstAfter of int32 * int64 | NeedsReveal of bytes +type status = Evaluating | Waiting_for_input | Waiting_for_reveal +type input = Inbox_message of int32 * int64 * bytes | Reveal of bytes +type input_request = No_input_required | Initial | First_after of int32 * int64 | Needs_reveal of bytes type output_info = {message_index : int64; outbox_level : int32} type output = {info : output_info; encoded_message : bytes} type output_proof diff --git a/src/riscv/lib/octez_riscv_api.mli b/src/riscv/lib/octez_riscv_api.mli index fc053421a582..399eda0d36d7 100644 --- a/src/riscv/lib/octez_riscv_api.mli +++ b/src/riscv/lib/octez_riscv_api.mli @@ -8,9 +8,9 @@ type repo type state type mut_state type id -type status = Evaluating | WaitingForInput | WaitingForReveal -type input = InboxMessage of int32 * int64 * bytes | Reveal of bytes -type input_request = NoInputRequired | Initial | FirstAfter of int32 * int64 | NeedsReveal of bytes +type status = Evaluating | Waiting_for_input | Waiting_for_reveal +type input = Inbox_message of int32 * int64 * bytes | Reveal of bytes +type input_request = No_input_required | Initial | First_after of int32 * int64 | Needs_reveal of bytes type output_info = {message_index : int64; outbox_level : int32} type output = {info : output_info; encoded_message : bytes} type output_proof diff --git a/src/riscv/lib/src/ocaml_api.rs b/src/riscv/lib/src/ocaml_api.rs index 3331f734ea55..514dbeb98c2b 100644 --- a/src/riscv/lib/src/ocaml_api.rs +++ b/src/riscv/lib/src/ocaml_api.rs @@ -49,7 +49,7 @@ ocaml::custom!(Repo); ocaml::custom!(Id); #[derive(ocaml::FromValue, ocaml::ToValue, IntoPrimitive, TryFromPrimitive, strum::EnumCount)] -#[ocaml::sig("Evaluating | WaitingForInput | WaitingForReveal")] +#[ocaml::sig("Evaluating | Waiting_for_input | Waiting_for_reveal")] #[repr(u8)] pub enum Status { Evaluating, @@ -108,17 +108,18 @@ unsafe impl ocaml::ToValue for BytesWrapper { unsafe { ocaml::Value::bytes(&self.0) } } } + /// Input values are passed into the PVM after an input request has been made. #[derive(ocaml::FromValue)] -#[ocaml::sig("InboxMessage of int32 * int64 * bytes | Reveal of bytes")] +#[ocaml::sig("Inbox_message of int32 * int64 * bytes | Reveal of bytes")] pub enum Input<'a> { InboxMessage(u32, u64, &'a [u8]), Reveal(&'a [u8]), } -/// A value of this type could only be returned as part of successfully verifying a proof. +/// Describes possible input requests the PVM may ask for during execution. #[derive(ocaml::ToValue)] -#[ocaml::sig("NoInputRequired | Initial | FirstAfter of int32 * int64 | NeedsReveal of bytes")] +#[ocaml::sig("No_input_required | Initial | First_after of int32 * int64 | Needs_reveal of bytes")] pub enum InputRequest { NoInputRequired, Initial, @@ -152,7 +153,7 @@ pub struct OutputInfo { ocaml::custom!(OutputInfo); -/// A value of this type could only be returned as part of successfully verifying an output proof. +/// A value of this type is generated as part of successfully verifying an output proof. #[derive(ocaml::ToValue)] #[ocaml::sig("{info : output_info; encoded_message : bytes}")] pub struct Output { -- GitLab