Skip to content

Commit

Permalink
chore: even more cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
clabby committed Jul 20, 2024
1 parent b802f1a commit d85d0bd
Show file tree
Hide file tree
Showing 20 changed files with 154 additions and 312 deletions.
5 changes: 4 additions & 1 deletion bin/src/subcommands/load_elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion bin/src/subcommands/witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down
17 changes: 5 additions & 12 deletions crates/cannon/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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)?;
Expand All @@ -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::<Vec<_>>();
let cmd = self.preimage_server.split(' ').map(String::from).collect::<Vec<_>>();
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,
Expand Down
12 changes: 6 additions & 6 deletions crates/cannon/src/kernel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -23,10 +23,10 @@ where
{
/// The instrumented state that the kernel will run.
ins_state: InstrumentedState<O, E, P>,
/// 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<Child>,
/// The path to the input JSON state.
input: String,
Expand Down
4 changes: 2 additions & 2 deletions crates/cannon/src/types.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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<u8>,
pub step_input: Vec<u8>,
Expand Down
20 changes: 4 additions & 16 deletions crates/cannon/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}"))?,
),
);

Expand Down
2 changes: 1 addition & 1 deletion crates/fpvm/benches/execution.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down
16 changes: 4 additions & 12 deletions crates/fpvm/benches/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand All @@ -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();
});
Expand All @@ -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();
});
Expand All @@ -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();
});
Expand Down
71 changes: 21 additions & 50 deletions crates/fpvm/src/evm/mod.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand All @@ -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>,
}
Expand Down Expand Up @@ -131,16 +132,10 @@ impl<'a> MipsEVM<'a, CacheDB<EmptyDB>> {
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")
})?;
Expand Down Expand Up @@ -235,9 +230,10 @@ impl<'a> MipsEVM<'a, CacheDB<EmptyDB>> {
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,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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!");
}
}
12 changes: 2 additions & 10 deletions crates/fpvm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading

0 comments on commit d85d0bd

Please sign in to comment.