From d85d0bd9059d11823f2b9be0776a861d99bd4b98 Mon Sep 17 00:00:00 2001 From: clabby Date: Sat, 20 Jul 2024 16:41:34 -0400 Subject: [PATCH] chore: even more cleanups --- bin/src/subcommands/load_elf.rs | 5 +- bin/src/subcommands/witness.rs | 2 +- crates/cannon/src/builder.rs | 17 ++--- crates/cannon/src/kernel.rs | 12 ++-- crates/cannon/src/types.rs | 4 +- crates/cannon/src/utils.rs | 20 ++---- crates/fpvm/benches/execution.rs | 2 +- crates/fpvm/benches/memory.rs | 16 ++--- crates/fpvm/src/evm/mod.rs | 71 ++++++------------ crates/fpvm/src/lib.rs | 12 +--- crates/fpvm/src/memory/map_memory.rs | 103 +++++++-------------------- crates/fpvm/src/memory/page.rs | 36 +++------- crates/fpvm/src/mips/instrumented.rs | 41 ++++------- crates/fpvm/src/mips/mips_vm.rs | 20 ++---- crates/fpvm/src/test_utils/mod.rs | 36 ++-------- crates/fpvm/src/types/state.rs | 9 ++- crates/fpvm/src/types/witness.rs | 5 +- crates/fpvm/src/utils/patch.rs | 30 ++++---- crates/fpvm/src/utils/ser.rs | 14 ++-- rustfmt.toml | 11 +++ 20 files changed, 154 insertions(+), 312 deletions(-) create mode 100644 rustfmt.toml diff --git a/bin/src/subcommands/load_elf.rs b/bin/src/subcommands/load_elf.rs index 9c87479..2c905d0 100644 --- a/bin/src/subcommands/load_elf.rs +++ b/bin/src/subcommands/load_elf.rs @@ -4,7 +4,10 @@ use super::CannonSubcommandDispatcher; use alloy_primitives::B256; use anyhow::Result; use cannon::gz::compress_bytes; -use cannon_fpvm::{load_elf, patch_go, patch_stack, state_hash}; +use cannon_fpvm::{ + types::state_hash, + utils::patch::{load_elf, patch_go, patch_stack}, +}; use clap::Args; use std::{ fmt::Display, diff --git a/bin/src/subcommands/witness.rs b/bin/src/subcommands/witness.rs index d47d483..8d411bf 100644 --- a/bin/src/subcommands/witness.rs +++ b/bin/src/subcommands/witness.rs @@ -4,7 +4,7 @@ use super::CannonSubcommandDispatcher; use alloy_primitives::B256; use anyhow::Result; use cannon::gz::decompress_bytes; -use cannon_fpvm::{state_hash, State}; +use cannon_fpvm::types::{state_hash, State}; use clap::Args; use std::{fs, path::PathBuf}; diff --git a/crates/cannon/src/builder.rs b/crates/cannon/src/builder.rs index b0dcdbd..220f415 100644 --- a/crates/cannon/src/builder.rs +++ b/crates/cannon/src/builder.rs @@ -2,7 +2,7 @@ use crate::{gz, Kernel, ProcessPreimageOracle}; use anyhow::{anyhow, Result}; -use cannon_fpvm::{InstrumentedState, State}; +use cannon_fpvm::{types::State, InstrumentedState}; use std::{ fs::{self, File}, io::{self, BufReader, Read, Stderr, Stdout}, @@ -42,8 +42,8 @@ impl KernelBuilder { let f = File::open(&self.input)?; let f_sz = f.metadata()?.len(); let mut reader = BufReader::new(f); - // Give a reasonable capacity to the vector to avoid too many reallocations. The size of the file - // is the size of the compressed state dump, so we will still reallocate. + // Give a reasonable capacity to the vector to avoid too many reallocations. The size of the + // file is the size of the compressed state dump, so we will still reallocate. let mut raw_state = Vec::with_capacity(f_sz as usize); reader.read_to_end(&mut raw_state)?; let raw_state = fs::read(&self.input)?; @@ -52,16 +52,9 @@ impl KernelBuilder { let (preimage_pipe, hint_pipe, pipe_fds) = crate::utils::create_native_pipes()?; // TODO(clabby): Allow for the preimage server to be configurable. - let cmd = self - .preimage_server - .split(' ') - .map(String::from) - .collect::>(); + let cmd = self.preimage_server.split(' ').map(String::from).collect::>(); let (oracle, server_proc) = ProcessPreimageOracle::start( - PathBuf::from( - cmd.first() - .ok_or(anyhow!("Missing preimage server binary path"))?, - ), + PathBuf::from(cmd.first().ok_or(anyhow!("Missing preimage server binary path"))?), &cmd[1..], preimage_pipe, hint_pipe, diff --git a/crates/cannon/src/kernel.rs b/crates/cannon/src/kernel.rs index fbd094a..8dc650c 100644 --- a/crates/cannon/src/kernel.rs +++ b/crates/cannon/src/kernel.rs @@ -2,13 +2,13 @@ use crate::{gz::compress_bytes, types::Proof}; use anyhow::{anyhow, Result}; -use cannon_fpvm::{state_hash, InstrumentedState}; +use cannon_fpvm::{types::state_hash, InstrumentedState}; use kona_preimage::{HintRouter, PreimageFetcher}; -use std::time::Instant; use std::{ fs::File, io::{BufWriter, Write}, process::Child, + time::Instant, }; use tokio::{runtime::Runtime, task::JoinHandle}; @@ -23,10 +23,10 @@ where { /// The instrumented state that the kernel will run. ins_state: InstrumentedState, - /// The server's process coupled with the preimage server's IO. We hold on to these so that they - /// are not dropped until the kernel is dropped, preventing a broken pipe before the kernel is - /// dropped. The other side of the bidirectional channel is owned by the [InstrumentedState], - /// which is also dropped when the kernel is dropped. + /// The server's process coupled with the preimage server's IO. We hold on to these so that + /// they are not dropped until the kernel is dropped, preventing a broken pipe before the + /// kernel is dropped. The other side of the bidirectional channel is owned by the + /// [InstrumentedState], which is also dropped when the kernel is dropped. server_proc: Option, /// The path to the input JSON state. input: String, diff --git a/crates/cannon/src/types.rs b/crates/cannon/src/types.rs index d28ea5c..5b132d6 100644 --- a/crates/cannon/src/types.rs +++ b/crates/cannon/src/types.rs @@ -1,6 +1,6 @@ //! This module contains the types for the `cannon` interface. -use cannon_fpvm::StateWitness; +use cannon_fpvm::types::StateWitness; use serde::{Deserialize, Serialize}; /// The [Proof] struct contains the data for a Cannon proof at a given instruction. @@ -10,7 +10,7 @@ pub struct Proof { pub step: u64, pub pre: [u8; 32], pub post: [u8; 32], - #[serde(with = "cannon_fpvm::ser::state_witness_hex")] + #[serde(with = "cannon_fpvm::utils::ser::state_witness_hex")] pub state_data: StateWitness, pub proof_data: Vec, pub step_input: Vec, diff --git a/crates/cannon/src/utils.rs b/crates/cannon/src/utils.rs index abf2b14..93f7e30 100644 --- a/crates/cannon/src/utils.rs +++ b/crates/cannon/src/utils.rs @@ -33,30 +33,18 @@ pub(crate) fn create_native_pipes() -> Result<(PipeHandle, PipeHandle, NativePip let (hint_reader, hint_writer) = create_temp_files()?; let preimage_pipe = PipeHandle::new( FileDescriptor::Wildcard( - po_reader - .as_raw_fd() - .try_into() - .map_err(|e| anyhow!("Failed to get raw FD: {e}"))?, + po_reader.as_raw_fd().try_into().map_err(|e| anyhow!("Failed to get raw FD: {e}"))?, ), FileDescriptor::Wildcard( - po_writer - .as_raw_fd() - .try_into() - .map_err(|e| anyhow!("Failed to get raw FD: {e}"))?, + po_writer.as_raw_fd().try_into().map_err(|e| anyhow!("Failed to get raw FD: {e}"))?, ), ); let hint_pipe = PipeHandle::new( FileDescriptor::Wildcard( - hint_reader - .as_raw_fd() - .try_into() - .map_err(|e| anyhow!("Failed to get raw FD: {e}"))?, + hint_reader.as_raw_fd().try_into().map_err(|e| anyhow!("Failed to get raw FD: {e}"))?, ), FileDescriptor::Wildcard( - hint_writer - .as_raw_fd() - .try_into() - .map_err(|e| anyhow!("Failed to get raw FD: {e}"))?, + hint_writer.as_raw_fd().try_into().map_err(|e| anyhow!("Failed to get raw FD: {e}"))?, ), ); diff --git a/crates/fpvm/benches/execution.rs b/crates/fpvm/benches/execution.rs index afa10bb..08b5773 100644 --- a/crates/fpvm/benches/execution.rs +++ b/crates/fpvm/benches/execution.rs @@ -1,6 +1,6 @@ use cannon_fpvm::{ - load_elf, patch_go, patch_stack, test_utils::{ClaimTestOracle, StaticOracle}, + utils::patch::{load_elf, patch_go, patch_stack}, InstrumentedState, }; use criterion::{criterion_group, criterion_main, Bencher, Criterion}; diff --git a/crates/fpvm/benches/memory.rs b/crates/fpvm/benches/memory.rs index 1eedf40..b7e5f45 100644 --- a/crates/fpvm/benches/memory.rs +++ b/crates/fpvm/benches/memory.rs @@ -11,9 +11,7 @@ fn merkle_root(c: &mut Criterion) { let mut memory = Memory::default(); let mut data = vec![0u8; 25_000_000]; rand::thread_rng().fill_bytes(&mut data[..]); - memory - .set_memory_range(0, &data[..]) - .expect("Should not error"); + memory.set_memory_range(0, &data[..]).expect("Should not error"); b.iter(|| { memory.merkle_root().unwrap(); }); @@ -23,9 +21,7 @@ fn merkle_root(c: &mut Criterion) { let mut memory = Memory::default(); let mut data = vec![0u8; 50_000_000]; rand::thread_rng().fill_bytes(&mut data[..]); - memory - .set_memory_range(0, &data[..]) - .expect("Should not error"); + memory.set_memory_range(0, &data[..]).expect("Should not error"); b.iter(|| { memory.merkle_root().unwrap(); }); @@ -35,9 +31,7 @@ fn merkle_root(c: &mut Criterion) { let mut memory = Memory::default(); let mut data = vec![0u8; 100_000_000]; rand::thread_rng().fill_bytes(&mut data[..]); - memory - .set_memory_range(0, &data[..]) - .expect("Should not error"); + memory.set_memory_range(0, &data[..]).expect("Should not error"); b.iter(|| { memory.merkle_root().unwrap(); }); @@ -47,9 +41,7 @@ fn merkle_root(c: &mut Criterion) { let mut memory = Memory::default(); let mut data = vec![0u8; 200_000_000]; rand::thread_rng().fill_bytes(&mut data[..]); - memory - .set_memory_range(0, &data[..]) - .expect("Should not error"); + memory.set_memory_range(0, &data[..]).expect("Should not error"); b.iter(|| { memory.merkle_root().unwrap(); }); diff --git a/crates/fpvm/src/evm/mod.rs b/crates/fpvm/src/evm/mod.rs index f4721ef..7b41fcd 100644 --- a/crates/fpvm/src/evm/mod.rs +++ b/crates/fpvm/src/evm/mod.rs @@ -1,7 +1,7 @@ //! This module contains a wrapper around a [revm] inspector with an in-memory backend //! that has the MIPS & PreimageOracle smart contracts deployed at deterministic addresses. -use crate::{state_hash, StateWitness, StepWitness}; +use crate::types::{state_hash, StateWitness, StepWitness}; use anyhow::Result; use revm::{ db::{CacheDB, EmptyDB}, @@ -23,9 +23,10 @@ pub const MIPS_CREATION_CODE: &str = include_str!("../../bindings/mips_creation. pub const PREIMAGE_ORACLE_DEPLOYED_CODE: &str = include_str!("../../bindings/preimage_creation.bin"); -/// A wrapper around a [revm] interpreter with an in-memory backend that has the MIPS & PreimageOracle -/// smart contracts deployed at deterministic addresses. This is used for differential testing the -/// implementation of the MIPS VM in this crate against the smart contract implementations. +/// A wrapper around a [revm] interpreter with an in-memory backend that has the MIPS & +/// PreimageOracle smart contracts deployed at deterministic addresses. This is used for +/// differential testing the implementation of the MIPS VM in this crate against the smart contract +/// implementations. pub struct MipsEVM<'a, DB: Database> { pub inner: Evm<'a, (), DB>, } @@ -131,16 +132,10 @@ impl<'a> MipsEVM<'a, CacheDB> { witness.preimage_offset ); - let preimage_oracle_input = - witness - .encode_preimage_oracle_input() - .ok_or(anyhow::anyhow!( - "Failed to ABI encode preimage oracle input." - ))?; - self.fill_tx_env( - TransactTo::Call(PREIMAGE_ORACLE_ADDR.into()), - preimage_oracle_input, - ); + let preimage_oracle_input = witness + .encode_preimage_oracle_input() + .ok_or(anyhow::anyhow!("Failed to ABI encode preimage oracle input."))?; + self.fill_tx_env(TransactTo::Call(PREIMAGE_ORACLE_ADDR.into()), preimage_oracle_input); self.inner.transact_commit().map_err(|_| { anyhow::anyhow!("Failed to commit preimage to PreimageOracle contract") })?; @@ -235,9 +230,10 @@ impl<'a> MipsEVM<'a, CacheDB> { mod test { use super::*; use crate::{ - load_elf, patch_go, patch_stack, + utils::patch::{load_elf, patch_go, patch_stack}, test_utils::{ClaimTestOracle, StaticOracle, BASE_ADDR_END, END_ADDR}, - Address, InstrumentedState, Memory, State, + types::{Address, State}, + InstrumentedState, Memory }; use std::{ fs, @@ -273,10 +269,7 @@ mod test { state.memory = Memory::default(); state }; - state - .memory - .set_memory_range(0, BufReader::new(program_mem.as_slice())) - .unwrap(); + state.memory.set_memory_range(0, BufReader::new(program_mem.as_slice())).unwrap(); // Set the return address ($ra) to jump into when the test completes. state.registers[31] = END_ADDR; @@ -343,19 +336,9 @@ mod test { let cases = [ ("j MSB set target", 0, 4, 0x0A_00_00_02), - ( - "j non-zero PC region", - 0x10_00_00_00, - 0x10_00_00_04, - 0x08_00_00_02, - ), + ("j non-zero PC region", 0x10_00_00_00, 0x10_00_00_04, 0x08_00_00_02), ("jal MSB set target", 0, 4, 0x0E_00_00_02), - ( - "jal non-zero PC region", - 0x10_00_00_00, - 0x10_00_00_04, - 0x0C_00_00_02, - ), + ("jal non-zero PC region", 0x10_00_00_00, 0x10_00_00_04, 0x0C_00_00_02), ]; for (name, pc, next_pc, instruction) in cases { @@ -395,10 +378,7 @@ mod test { for (name, next_pc, instruction) in cases { println!(" -> Running test: {name}"); - let mut state = State { - next_pc: next_pc as Address, - ..Default::default() - }; + let mut state = State { next_pc: next_pc as Address, ..Default::default() }; state.memory.set_memory(0, instruction).unwrap(); // Set the return address ($ra) to jump to when the test completes. @@ -448,11 +428,8 @@ mod test { } if i % 1000 == 0 { - let instruction = instrumented - .state - .memory - .get_memory(instrumented.state.pc as Address) - .unwrap(); + let instruction = + instrumented.state.memory.get_memory(instrumented.state.pc as Address).unwrap(); println!( "step: {} pc: 0x{:08x} instruction: {:08x}", instrumented.state.step, instrumented.state.pc, instruction @@ -492,11 +469,8 @@ mod test { } if i % 1000 == 0 { - let instruction = instrumented - .state - .memory - .get_memory(instrumented.state.pc as Address) - .unwrap(); + let instruction = + instrumented.state.memory.get_memory(instrumented.state.pc as Address).unwrap(); println!( "step: {} pc: 0x{:08x} instruction: {:08x}", instrumented.state.step, instrumented.state.pc, instruction @@ -523,9 +497,6 @@ mod test { ClaimTestOracle::S * ClaimTestOracle::A + ClaimTestOracle::B ) ); - assert_eq!( - String::from_utf8(instrumented.std_err.buffer().to_vec()).unwrap(), - "started!" - ); + assert_eq!(String::from_utf8(instrumented.std_err.buffer().to_vec()).unwrap(), "started!"); } } diff --git a/crates/fpvm/src/lib.rs b/crates/fpvm/src/lib.rs index a272255..3917ae3 100644 --- a/crates/fpvm/src/lib.rs +++ b/crates/fpvm/src/lib.rs @@ -6,17 +6,9 @@ pub use mips::InstrumentedState; mod memory; pub use memory::{CachedPage, Memory, MemoryReader}; -mod utils; -pub use utils::{ - patch::{load_elf, patch_go, patch_stack, MultiReader}, - ser, -}; +pub mod utils; -mod types; -pub use types::{ - state_hash, Address, Fd, Gindex, Page, PageIndex, State, StateWitness, StepWitness, VMStatus, - STATE_WITNESS_SIZE, -}; +pub mod types; #[cfg(any(feature = "test-utils", test))] pub mod test_utils; diff --git a/crates/fpvm/src/memory/map_memory.rs b/crates/fpvm/src/memory/map_memory.rs index 4c70b41..017ba43 100644 --- a/crates/fpvm/src/memory/map_memory.rs +++ b/crates/fpvm/src/memory/map_memory.rs @@ -1,8 +1,9 @@ //! The memory module contains the [Memory] data structure and its functionality for the emulator. use crate::{ - memory::page, types::SharedCachedPage, utils::keccak_concat_hashes, Address, Gindex, Page, - PageIndex, + memory::page, + types::{Address, Gindex, Page, PageIndex, SharedCachedPage}, + utils::keccak_concat_hashes, }; use anyhow::Result; use rustc_hash::FxHashMap; @@ -218,7 +219,8 @@ impl Memory { } /// Set a 32 bit value in the [Memory] at a given address. - /// This will invalidate the page at the given address, or allocate a new page if it does not exist. + /// This will invalidate the page at the given address, or allocate a new page if it does not + /// exist. /// /// ### Takes /// - `address`: The address to set the value at. @@ -339,8 +341,8 @@ impl Memory { /// Returns a human-readable string describing the size of the [Memory]. /// /// ### Returns - /// - A human-readable string describing the size of the [Memory] in B, KiB, - /// MiB, GiB, TiB, PiB, or EiB. + /// - A human-readable string describing the size of the [Memory] in B, KiB, MiB, GiB, TiB, PiB, + /// or EiB. pub fn usage(&self) -> String { let total = (self.pages.len() * page::PAGE_SIZE) as u64; const UNIT: u64 = 1024; @@ -355,27 +357,20 @@ impl Memory { exp += 1; n /= UNIT; } - format!( - "{:.1} {}iB", - (total as f64) / (div as f64), - ['K', 'M', 'G', 'T', 'P', 'E'][exp] - ) + format!("{:.1} {}iB", (total as f64) / (div as f64), ['K', 'M', 'G', 'T', 'P', 'E'][exp]) } } #[derive(Serialize, Deserialize, Debug)] struct PageEntry { index: PageIndex, - #[serde(with = "crate::ser::page_hex")] + #[serde(with = "crate::utils::ser::page_hex")] data: Page, } impl Default for PageEntry { fn default() -> Self { - Self { - index: Default::default(), - data: [0u8; page::PAGE_SIZE], - } + Self { index: Default::default(), data: [0u8; page::PAGE_SIZE] } } } @@ -387,10 +382,7 @@ impl Serialize for Memory { let mut page_entries: Vec = self .pages .iter() - .map(|(&k, p)| PageEntry { - index: k, - data: p.borrow().data, - }) + .map(|(&k, p)| PageEntry { index: k, data: p.borrow().data }) .collect(); page_entries.sort_by(|a, b| a.index.cmp(&b.index)); @@ -434,11 +426,7 @@ pub struct MemoryReader<'a> { impl<'a> MemoryReader<'a> { pub fn new(memory: &'a mut Memory, address: Address, count: u32) -> Self { - Self { - memory, - address, - count, - } + Self { memory, address, count } } } @@ -559,34 +547,18 @@ mod test { memory.set_memory(0xF000, 0).unwrap(); memory.set_memory(0xF004, 0).unwrap(); let root = memory.merkle_root().unwrap(); - assert_eq!( - page::ZERO_HASHES[32 - 5], - root, - "Still should have expected zero hash" - ); + assert_eq!(page::ZERO_HASHES[32 - 5], root, "Still should have expected zero hash"); } #[test] fn random_few_pages() { let mut memory = Memory::default(); - memory - .set_memory(page::PAGE_SIZE as Address * 3, 1) - .unwrap(); - memory - .set_memory(page::PAGE_SIZE as Address * 5, 42) - .unwrap(); - memory - .set_memory(page::PAGE_SIZE as Address * 6, 123) - .unwrap(); - let p3 = memory - .merkleize_subtree((1 << page::PAGE_KEY_SIZE) | 3) - .unwrap(); - let p5 = memory - .merkleize_subtree((1 << page::PAGE_KEY_SIZE) | 5) - .unwrap(); - let p6 = memory - .merkleize_subtree((1 << page::PAGE_KEY_SIZE) | 6) - .unwrap(); + memory.set_memory(page::PAGE_SIZE as Address * 3, 1).unwrap(); + memory.set_memory(page::PAGE_SIZE as Address * 5, 42).unwrap(); + memory.set_memory(page::PAGE_SIZE as Address * 6, 123).unwrap(); + let p3 = memory.merkleize_subtree((1 << page::PAGE_KEY_SIZE) | 3).unwrap(); + let p5 = memory.merkleize_subtree((1 << page::PAGE_KEY_SIZE) | 5).unwrap(); + let p6 = memory.merkleize_subtree((1 << page::PAGE_KEY_SIZE) | 6).unwrap(); let z = page::ZERO_HASHES[page::PAGE_ADDRESS_SIZE - 5]; let r1 = keccak_concat_hashes( keccak_concat_hashes( @@ -600,36 +572,19 @@ mod test { ) .into(), ); - let r2 = memory - .merkleize_subtree(1 << (page::PAGE_KEY_SIZE - 3)) - .unwrap(); - assert_eq!( - r1, r2, - "Expecting manual page combination to match subtree merkle func" - ); + let r2 = memory.merkleize_subtree(1 << (page::PAGE_KEY_SIZE - 3)).unwrap(); + assert_eq!(r1, r2, "Expecting manual page combination to match subtree merkle func"); } #[test] fn invalidate_page() { let mut memory = Memory::default(); memory.set_memory(0xF000, 0).unwrap(); - assert_eq!( - page::ZERO_HASHES[32 - 5], - memory.merkle_root().unwrap(), - "Zero at first" - ); + assert_eq!(page::ZERO_HASHES[32 - 5], memory.merkle_root().unwrap(), "Zero at first"); memory.set_memory(0xF004, 1).unwrap(); - assert_ne!( - page::ZERO_HASHES[32 - 5], - memory.merkle_root().unwrap(), - "Non-zero" - ); + assert_ne!(page::ZERO_HASHES[32 - 5], memory.merkle_root().unwrap(), "Non-zero"); memory.set_memory(0xF004, 0).unwrap(); - assert_eq!( - page::ZERO_HASHES[32 - 5], - memory.merkle_root().unwrap(), - "Zero again" - ); + assert_eq!(page::ZERO_HASHES[32 - 5], memory.merkle_root().unwrap(), "Zero again"); } } @@ -644,9 +599,7 @@ mod test { let mut memory = Memory::default(); let mut data = [0u8; 20_000]; rand::thread_rng().fill_bytes(&mut data[..]); - memory - .set_memory_range(0, &data[..]) - .expect("Should not error"); + memory.set_memory_range(0, &data[..]).expect("Should not error"); for i in [0, 4, 1000, 20_000 - 4] { let value = memory.get_memory(i).expect("Should not error"); let expected = @@ -659,9 +612,7 @@ mod test { fn repeat_range() { let mut memory = Memory::default(); let data = b"under the big bright yellow sun".repeat(40); - memory - .set_memory_range(0x1337, &data[..]) - .expect("Should not error"); + memory.set_memory_range(0x1337, &data[..]).expect("Should not error"); let mut reader = MemoryReader::new(&mut memory, 0x1337 - 10, data.len() as u32 + 20); let mut buf = Vec::with_capacity(1260); @@ -707,7 +658,7 @@ mod test { mod serialize { use super::*; - use crate::{types::SharedCachedPage, Gindex, PageIndex}; + use crate::types::{Gindex, PageIndex, SharedCachedPage}; use proptest::{ prelude::{any, Arbitrary}, proptest, diff --git a/crates/fpvm/src/memory/page.rs b/crates/fpvm/src/memory/page.rs index e8bd125..f68c912 100644 --- a/crates/fpvm/src/memory/page.rs +++ b/crates/fpvm/src/memory/page.rs @@ -1,6 +1,9 @@ //! This module contains the data structure for a [Page] within the MIPS emulator's [Memory]. -use crate::{utils::keccak_concat_hashes, Address, Gindex, Page}; +use crate::{ + types::{Address, Gindex, Page}, + utils::keccak_concat_hashes, +}; use anyhow::Result; use once_cell::sync::Lazy; @@ -47,11 +50,7 @@ pub struct CachedPage { impl Default for CachedPage { fn default() -> Self { - Self { - data: [0; PAGE_SIZE], - cache: *DEFAULT_CACHE, - valid: [true; PAGE_SIZE / 32], - } + Self { data: [0; PAGE_SIZE], cache: *DEFAULT_CACHE, valid: [true; PAGE_SIZE / 32] } } } @@ -177,39 +176,24 @@ mod test { let pre = page.merkle_root().unwrap(); page.data[42] = 0xcd; let post = page.merkle_root().unwrap(); - assert_eq!( - pre, post, - "Pre and post state should be equal until the cache is invalidated" - ); + assert_eq!(pre, post, "Pre and post state should be equal until the cache is invalidated"); page.invalidate(42).unwrap(); let post_b = page.merkle_root().unwrap(); - assert_ne!( - post, post_b, - "Pre and post state should be different after cache invalidation" - ); + assert_ne!(post, post_b, "Pre and post state should be different after cache invalidation"); page.data[2000] = 0xef; page.invalidate(42).unwrap(); let post_c = page.merkle_root().unwrap(); - assert_eq!( - post_b, post_c, - "Local invalidation is not global invalidation." - ); + assert_eq!(post_b, post_c, "Local invalidation is not global invalidation."); page.invalidate(2000).unwrap(); let post_d = page.merkle_root().unwrap(); - assert_ne!( - post_c, post_d, - "Multiple invalidations should change the root." - ); + assert_ne!(post_c, post_d, "Multiple invalidations should change the root."); page.data[1000] = 0xff; page.invalidate_full(); let post_e = page.merkle_root().unwrap(); - assert_ne!( - post_d, post_e, - "Full invalidation should always change the root." - ); + assert_ne!(post_d, post_e, "Full invalidation should always change the root."); } } diff --git a/crates/fpvm/src/mips/instrumented.rs b/crates/fpvm/src/mips/instrumented.rs index 925eb40..0a45a6f 100644 --- a/crates/fpvm/src/mips/instrumented.rs +++ b/crates/fpvm/src/mips/instrumented.rs @@ -1,6 +1,6 @@ //! This module contains the [InstrumentedState] definition. -use crate::{Address, State, StepWitness}; +use crate::types::{Address, State, StepWitness}; use anyhow::Result; use kona_preimage::{HintRouter, PreimageFetcher}; use std::io::{BufWriter, Write}; @@ -120,14 +120,15 @@ where mod test { use alloy_primitives::keccak256; - use crate::test_utils::{ClaimTestOracle, BASE_ADDR_END, END_ADDR}; - use crate::STATE_WITNESS_SIZE; - use crate::{load_elf, patch_go, patch_stack, state_hash}; - use crate::{test_utils::StaticOracle, Address, InstrumentedState, Memory, State}; - use std::io::BufWriter; + use crate::{ + test_utils::{ClaimTestOracle, StaticOracle, BASE_ADDR_END, END_ADDR}, + types::{state_hash, Address, State, STATE_WITNESS_SIZE}, + utils::patch::{load_elf, patch_go, patch_stack}, + InstrumentedState, Memory, + }; use std::{ fs, - io::{self, BufReader}, + io::{self, BufReader, BufWriter}, path::PathBuf, }; @@ -216,11 +217,7 @@ mod test { ]; for (exited, exit_code) in cases.into_iter() { - let mut state = State { - exited, - exit_code, - ..Default::default() - }; + let mut state = State { exited, exit_code, ..Default::default() }; let actual_witness = state.encode_witness().unwrap(); let actual_state_hash = state_hash(actual_witness); @@ -236,10 +233,7 @@ mod test { let mut expected_state_hash = keccak256(&expected_witness); expected_state_hash[0] = State::vm_status(exited, exit_code) as u8; - assert_eq!( - actual_state_hash, expected_state_hash, - "Incorrect state hash" - ); + assert_eq!(actual_state_hash, expected_state_hash, "Incorrect state hash"); } } @@ -265,14 +259,8 @@ mod test { assert!(ins.state.exited, "must exit"); assert_eq!(ins.state.exit_code, 0, "must exit with 0"); - assert_eq!( - String::from_utf8(ins.std_out.buffer().to_vec()).unwrap(), - "hello world!\n" - ); - assert_eq!( - String::from_utf8(ins.std_err.buffer().to_vec()).unwrap(), - "" - ); + assert_eq!(String::from_utf8(ins.std_out.buffer().to_vec()).unwrap(), "hello world!\n"); + assert_eq!(String::from_utf8(ins.std_err.buffer().to_vec()).unwrap(), ""); } #[tokio::test] @@ -306,9 +294,6 @@ mod test { ClaimTestOracle::S * ClaimTestOracle::A + ClaimTestOracle::B ) ); - assert_eq!( - String::from_utf8(ins.std_err.buffer().to_vec()).unwrap(), - "started!" - ); + assert_eq!(String::from_utf8(ins.std_err.buffer().to_vec()).unwrap(), "started!"); } } diff --git a/crates/fpvm/src/mips/mips_vm.rs b/crates/fpvm/src/mips/mips_vm.rs index 51c6805..2ea159b 100644 --- a/crates/fpvm/src/mips/mips_vm.rs +++ b/crates/fpvm/src/mips/mips_vm.rs @@ -3,8 +3,8 @@ use crate::{ memory::{page, MemoryReader}, mips::instrumented::{MIPS_EBADF, MIPS_EINVAL}, - types::Syscall, - Address, Fd, InstrumentedState, + types::{Address, Fd, Syscall}, + InstrumentedState, }; use anyhow::Result; use kona_preimage::{HintRouter, PreimageFetcher}; @@ -185,9 +185,7 @@ where // Write memory if store_address != 0xFFFFFFFF { self.track_mem_access(store_address as Address)?; - self.state - .memory - .set_memory(store_address as Address, val)?; + self.state.memory.set_memory(store_address as Address, val)?; } // Write back the value to the destination register @@ -203,11 +201,8 @@ where let mut v0 = 0; let mut v1 = 0; - let (a0, a1, mut a2) = ( - self.state.registers[4], - self.state.registers[5], - self.state.registers[6], - ); + let (a0, a1, mut a2) = + (self.state.registers[4], self.state.registers[5], self.state.registers[6]); if let Ok(syscall) = Syscall::try_from(self.state.registers[2]) { match syscall { @@ -337,10 +332,7 @@ where } let key_copy = key; - io::copy( - &mut key_copy[a2 as usize..].as_ref(), - &mut key.as_mut_slice(), - )?; + io::copy(&mut key_copy[a2 as usize..].as_ref(), &mut key.as_mut_slice())?; let _ = memory.to_be_bytes()[alignment as usize..] .as_ref() diff --git a/crates/fpvm/src/test_utils/mod.rs b/crates/fpvm/src/test_utils/mod.rs index f03481a..9d270af 100644 --- a/crates/fpvm/src/test_utils/mod.rs +++ b/crates/fpvm/src/test_utils/mod.rs @@ -1,6 +1,6 @@ //! Testing utilities. -use crate::{utils::concat_fixed, utils::keccak256}; +use crate::utils::{concat_fixed, keccak256}; use alloy_primitives::hex; use anyhow::Result; use async_trait::async_trait; @@ -79,9 +79,7 @@ impl Default for ClaimTestOracle { (Self::S * Self::A + Self::B).to_be_bytes().to_vec(), ); - Self { - images: Arc::new(Mutex::new(images)), - } + Self { images: Arc::new(Mutex::new(images)) } } } @@ -98,11 +96,7 @@ impl HintRouter for ClaimTestOracle { match parts[0] { "fetch-state" => { - assert_eq!( - hash, - Self::pre_hash(), - "Expecting request for pre-state preimage" - ); + assert_eq!(hash, Self::pre_hash(), "Expecting request for pre-state preimage"); self.images.lock().await.insert( PreimageKey::new(Self::pre_hash(), PreimageKeyType::Keccak256), @@ -110,11 +104,7 @@ impl HintRouter for ClaimTestOracle { ); } "fetch-diff" => { - assert_eq!( - hash, - Self::diff_hash(), - "Expecting request for diff preimage" - ); + assert_eq!(hash, Self::diff_hash(), "Expecting request for diff preimage"); let mut images_lock = self.images.lock().await; images_lock.insert( @@ -122,17 +112,11 @@ impl HintRouter for ClaimTestOracle { Self::diff().to_vec(), ); images_lock.insert( - PreimageKey::new( - *keccak256(Self::A.to_be_bytes()), - PreimageKeyType::Keccak256, - ), + PreimageKey::new(*keccak256(Self::A.to_be_bytes()), PreimageKeyType::Keccak256), Self::A.to_be_bytes().to_vec(), ); images_lock.insert( - PreimageKey::new( - *keccak256(Self::B.to_be_bytes()), - PreimageKeyType::Keccak256, - ), + PreimageKey::new(*keccak256(Self::B.to_be_bytes()), PreimageKeyType::Keccak256), Self::B.to_be_bytes().to_vec(), ); } @@ -146,12 +130,6 @@ impl HintRouter for ClaimTestOracle { #[async_trait] impl PreimageFetcher for ClaimTestOracle { async fn get_preimage(&self, key: PreimageKey) -> Result> { - Ok(self - .images - .lock() - .await - .get(&key) - .ok_or(anyhow::anyhow!("No image for key"))? - .to_vec()) + Ok(self.images.lock().await.get(&key).ok_or(anyhow::anyhow!("No image for key"))?.to_vec()) } } diff --git a/crates/fpvm/src/types/state.rs b/crates/fpvm/src/types/state.rs index 0cb07a4..a0eed62 100644 --- a/crates/fpvm/src/types/state.rs +++ b/crates/fpvm/src/types/state.rs @@ -1,6 +1,9 @@ //! This module contains the data structure for the state of the MIPS emulator. -use crate::{Memory, StateWitness, VMStatus, STATE_WITNESS_SIZE}; +use crate::{ + types::{StateWitness, VMStatus, STATE_WITNESS_SIZE}, + Memory, +}; use anyhow::Result; use serde::{Deserialize, Serialize}; @@ -14,7 +17,7 @@ pub struct State { /// The [Memory] of the emulated MIPS thread context. pub memory: Memory, /// The preimage key for the given state. - #[serde(with = "crate::ser::fixed_32_hex")] + #[serde(with = "crate::utils::ser::fixed_32_hex")] pub preimage_key: [u8; 32], /// The preimage offset. pub preimage_offset: u32, @@ -37,7 +40,7 @@ pub struct State { /// The MIPS emulator's registers. pub registers: [u32; 32], /// The last hint sent to the host. - #[serde(with = "crate::ser::vec_u8_hex")] + #[serde(with = "crate::utils::ser::vec_u8_hex")] pub last_hint: Vec, } diff --git a/crates/fpvm/src/types/witness.rs b/crates/fpvm/src/types/witness.rs index 498f0f5..8c3ab56 100644 --- a/crates/fpvm/src/types/witness.rs +++ b/crates/fpvm/src/types/witness.rs @@ -1,6 +1,9 @@ //! This module contains the various witness types. -use crate::{utils::keccak256, State, StateWitness}; +use crate::{ + types::{State, StateWitness}, + utils::keccak256, +}; use alloy_primitives::{Bytes, B256, U256}; use alloy_sol_types::{sol, SolCall}; use kona_preimage::PreimageKeyType; diff --git a/crates/fpvm/src/utils/patch.rs b/crates/fpvm/src/utils/patch.rs index 0489b5c..922f7f9 100644 --- a/crates/fpvm/src/utils/patch.rs +++ b/crates/fpvm/src/utils/patch.rs @@ -1,6 +1,9 @@ //! This module contains utilities for loading ELF files into [State] objects. -use crate::{memory::page, Address, State}; +use crate::{ + memory::page, + types::{Address, State}, +}; use anyhow::Result; use elf::{abi::PT_LOAD, endian::AnyEndian, ElfBytes}; use std::io::{self, Cursor, Read}; @@ -20,7 +23,8 @@ pub(crate) const GO_SYMBOLS: [&str; 14] = [ "github.com/prometheus/client_model/go.init.0", "github.com/prometheus/client_model/go.init.1", "flag.init", // skip flag pkg init, we need to debug arg-processing more to see why this fails - "runtime.check", // We need to patch this out, we don't pass float64nan because we don't support floats + "runtime.check", /* We need to patch this out, we don't pass float64nan because we don't support + * floats */ ]; /// Load a raw ELF file into a [State] object. @@ -41,9 +45,7 @@ pub fn load_elf(raw: &[u8]) -> Result { ..Default::default() }; - let headers = elf - .segments() - .ok_or(anyhow::anyhow!("Failed to load section headers"))?; + let headers = elf.segments().ok_or(anyhow::anyhow!("Failed to load section headers"))?; for (i, header) in headers.iter().enumerate() { if header.p_type == 0x70000003 { @@ -88,9 +90,7 @@ pub fn load_elf(raw: &[u8]) -> Result { ); } - state - .memory - .set_memory_range(header.p_vaddr as u32, reader)?; + state.memory.set_memory_range(header.p_vaddr as u32, reader)?; } Ok(state) @@ -107,9 +107,8 @@ pub fn load_elf(raw: &[u8]) -> Result { /// - `Err(_)` if the patch failed pub fn patch_go(raw: &[u8], state: &mut State) -> Result<()> { let elf = ElfBytes::::minimal_parse(raw)?; - let (parsing_table, string_table) = elf - .symbol_table()? - .ok_or(anyhow::anyhow!("Failed to load ELF symbol table"))?; + let (parsing_table, string_table) = + elf.symbol_table()?.ok_or(anyhow::anyhow!("Failed to load ELF symbol table"))?; for symbol in parsing_table { let symbol_idx = symbol.st_name; @@ -166,15 +165,14 @@ pub fn patch_stack(state: &mut State) -> Result<()> { store_mem(state, ptr + 4 * 8, 0)?; // auxv[term] = 0 // 16 bytes of "randomness" - state - .memory - .set_memory_range(ptr + 4 * 9, b"4;byfairdiceroll".as_slice())?; + state.memory.set_memory_range(ptr + 4 * 9, b"4;byfairdiceroll".as_slice())?; Ok(()) } -/// A multi reader is a reader that reads from the first reader until it returns 0, then reads from the second reader. -pub struct MultiReader(R1, R2); +/// A multi reader is a reader that reads from the first reader until it returns 0, then reads from +/// the second reader. +struct MultiReader(R1, R2); impl Read for MultiReader { fn read(&mut self, buf: &mut [u8]) -> io::Result { diff --git a/crates/fpvm/src/utils/ser.rs b/crates/fpvm/src/utils/ser.rs index 0f2cb0a..c9a4d0c 100644 --- a/crates/fpvm/src/utils/ser.rs +++ b/crates/fpvm/src/utils/ser.rs @@ -19,13 +19,11 @@ macro_rules! fixed_hex_ser { D: Deserializer<'de>, { let s = String::deserialize(deserializer)?; - hex::decode(s) - .map_err(serde::de::Error::custom) - .map(|bytes| { - let mut array = [0u8; $size]; - array.copy_from_slice(&bytes); - array - }) + hex::decode(s).map_err(serde::de::Error::custom).map(|bytes| { + let mut array = [0u8; $size]; + array.copy_from_slice(&bytes); + array + }) } } }; @@ -33,7 +31,7 @@ macro_rules! fixed_hex_ser { fixed_hex_ser!(fixed_32_hex, 32); fixed_hex_ser!(page_hex, crate::memory::page::PAGE_SIZE); -fixed_hex_ser!(state_witness_hex, crate::STATE_WITNESS_SIZE); +fixed_hex_ser!(state_witness_hex, crate::types::STATE_WITNESS_SIZE); pub mod vec_u8_hex { use alloy_primitives::hex; diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000..68c3c93 --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1,11 @@ +reorder_imports = true +imports_granularity = "Crate" +use_small_heuristics = "Max" +comment_width = 100 +wrap_comments = true +binop_separator = "Back" +trailing_comma = "Vertical" +trailing_semicolon = false +use_field_init_shorthand = true +format_code_in_doc_comments = true +doc_comment_code_block_width = 100