-
Notifications
You must be signed in to change notification settings - Fork 714
Move wavmio logic to caller-env #4472
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
+250
−108
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0f5d304
Move wavmio logic to caller-env
pmikolajczyk41 234469b
changelog
pmikolajczyk41 e1d073a
Restore log about unknown preimage type
pmikolajczyk41 8ad52e7
Extract common logic for reading messages
pmikolajczyk41 c12e18a
import
pmikolajczyk41 f453fd3
Merge branch 'master' into pmikolajczyk/nit-4614-port-wavmio
pmikolajczyk41 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| ### Internal | ||
| - Move wavmio logic from JIT crate to caller-env (to be reused soon by SP1 validator) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| // Copyright 2026, Offchain Labs, Inc. | ||
| // For license information, see https://github.com/OffchainLabs/nitro/blob/master/LICENSE.md | ||
|
|
||
| use crate::{GuestPtr, MemAccess}; | ||
| use alloc::format; | ||
| use alloc::string::String; | ||
| use core::cmp::min; | ||
|
|
||
| /// Read validation inputs and set outputs for the `wavmio` host functions. | ||
| pub trait WavmIo { | ||
| fn get_u64_global(&self, idx: usize) -> Option<u64>; | ||
| fn set_u64_global(&mut self, idx: usize, val: u64) -> bool; | ||
| fn get_bytes32_global(&self, idx: usize) -> Option<&[u8; 32]>; | ||
| fn set_bytes32_global(&mut self, idx: usize, val: [u8; 32]) -> bool; | ||
| fn get_sequencer_message(&self, num: u64) -> Option<&[u8]>; | ||
| fn get_delayed_message(&self, num: u64) -> Option<&[u8]>; | ||
| fn get_preimage(&self, preimage_type: u8, hash: &[u8; 32]) -> Option<&[u8]>; | ||
| } | ||
|
|
||
| /// Reads 32-bytes of global state and writes to guest memory. | ||
| pub fn get_global_state_bytes32( | ||
| mem: &mut impl MemAccess, | ||
| io: &impl WavmIo, | ||
| idx: u32, | ||
| out_ptr: GuestPtr, | ||
| ) -> Result<(), String> { | ||
| let Some(global) = io.get_bytes32_global(idx as usize) else { | ||
| return Err("global read out of bounds in wavmio.getGlobalStateBytes32".into()); | ||
| }; | ||
| mem.write_slice(out_ptr, &global[..]); | ||
| Ok(()) | ||
| } | ||
|
|
||
| /// Reads 32-bytes from guest memory and writes to global state. | ||
| pub fn set_global_state_bytes32( | ||
| mem: &impl MemAccess, | ||
| io: &mut impl WavmIo, | ||
| idx: u32, | ||
| src_ptr: GuestPtr, | ||
| ) -> Result<(), String> { | ||
| let val = mem.read_fixed(src_ptr); | ||
| if !io.set_bytes32_global(idx as usize, val) { | ||
| return Err("global write oob in wavmio.setGlobalStateBytes32".into()); | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| /// Reads 8-bytes of global state. | ||
| pub fn get_global_state_u64(io: &impl WavmIo, idx: u32) -> Result<u64, String> { | ||
| match io.get_u64_global(idx as usize) { | ||
| Some(val) => Ok(val), | ||
| None => Err("global read out of bounds in wavmio.getGlobalStateU64".into()), | ||
| } | ||
| } | ||
|
|
||
| /// Writes 8-bytes of global state. | ||
| pub fn set_global_state_u64(io: &mut impl WavmIo, idx: u32, val: u64) -> Result<(), String> { | ||
| if !io.set_u64_global(idx as usize, val) { | ||
| return Err("global write out of bounds in wavmio.setGlobalStateU64".into()); | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| /// Reads up to 32 bytes of a sequencer inbox message at the given offset. | ||
| pub fn read_inbox_message( | ||
| mem: &mut impl MemAccess, | ||
| io: &impl WavmIo, | ||
| msg_num: u64, | ||
| offset: u32, | ||
| out_ptr: GuestPtr, | ||
| ) -> Result<u32, String> { | ||
| let message = io | ||
| .get_sequencer_message(msg_num) | ||
| .ok_or(format!("missing sequencer inbox message {msg_num}"))?; | ||
| read_message(mem, message, offset, out_ptr) | ||
| } | ||
|
|
||
| /// Reads up to 32 bytes of a delayed inbox message at the given offset. | ||
| pub fn read_delayed_inbox_message( | ||
| mem: &mut impl MemAccess, | ||
| io: &impl WavmIo, | ||
| msg_num: u64, | ||
| offset: u32, | ||
| out_ptr: GuestPtr, | ||
| ) -> Result<u32, String> { | ||
| let message = io | ||
| .get_delayed_message(msg_num) | ||
| .ok_or(format!("missing delayed inbox message {msg_num}"))?; | ||
| read_message(mem, message, offset, out_ptr) | ||
| } | ||
|
|
||
| fn read_message( | ||
| mem: &mut impl MemAccess, | ||
| message: &[u8], | ||
| offset: u32, | ||
| out_ptr: GuestPtr, | ||
| ) -> Result<u32, String> { | ||
| let offset = offset as usize; | ||
| let len = min(32, message.len().saturating_sub(offset)); | ||
| let read = message.get(offset..(offset + len)).unwrap_or_default(); | ||
| mem.write_slice(out_ptr, read); | ||
| Ok(read.len() as u32) | ||
| } | ||
|
|
||
| /// Looks up a preimage by type and hash, reads up to 32 bytes at an aligned offset. | ||
| pub fn resolve_preimage( | ||
| mem: &mut impl MemAccess, | ||
| io: &impl WavmIo, | ||
| preimage_type: u8, | ||
| hash_ptr: GuestPtr, | ||
| offset: u32, | ||
| out_ptr: GuestPtr, | ||
| name: &str, | ||
| ) -> Result<u32, String> { | ||
| let hash = mem.read_fixed(hash_ptr); | ||
| let offset = offset as usize; | ||
|
|
||
| let Some(preimage) = io.get_preimage(preimage_type, &hash) else { | ||
| let hash_hex = hex::encode(hash); | ||
| return Err(format!( | ||
| "Missing requested preimage for hash {hash_hex} in {name}" | ||
| )); | ||
| }; | ||
|
|
||
| if offset % 32 != 0 { | ||
| return Err(format!("bad offset {offset} in {name}")); | ||
| } | ||
|
|
||
| let len = min(32, preimage.len().saturating_sub(offset)); | ||
| let read = preimage.get(offset..(offset + len)).unwrap_or_default(); | ||
| mem.write_slice(out_ptr, read); | ||
| Ok(read.len() as u32) | ||
| } | ||
|
|
||
| /// Returns 1 if a preimage exists for the given type and hash, 0 otherwise. | ||
| pub fn validate_certificate( | ||
| mem: &impl MemAccess, | ||
| io: &impl WavmIo, | ||
| preimage_type: u8, | ||
| hash_ptr: GuestPtr, | ||
| ) -> u8 { | ||
| let hash = mem.read_fixed(hash_ptr); | ||
| match io.get_preimage(preimage_type, &hash) { | ||
| Some(_) => 1, | ||
| None => 0, | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.