From b819e45edff9c1fb6fc421721c62704f39d3c408 Mon Sep 17 00:00:00 2001 From: clabby Date: Sat, 17 Feb 2024 15:54:14 -0500 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=20Move=20pipe=20reading=20logic=20int?= =?UTF-8?q?o=20pipe,=20so=20that=20it's=20shared?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/preimage/src/hint.rs | 33 ++------------------ crates/preimage/src/oracle.rs | 36 +++------------------- crates/preimage/src/pipe.rs | 25 +++++++++++++-- fpvm-tests/cannon-go-tests/minimal_test.go | 2 +- 4 files changed, 31 insertions(+), 65 deletions(-) diff --git a/crates/preimage/src/hint.rs b/crates/preimage/src/hint.rs index 4fc1df324..7d613aa7f 100644 --- a/crates/preimage/src/hint.rs +++ b/crates/preimage/src/hint.rs @@ -1,6 +1,6 @@ use crate::PipeHandle; use alloc::vec; -use anyhow::{bail, Result}; +use anyhow::Result; /// A [HintWriter] is a high-level interface to the hint pipe. It provides a way to write hints to the host. #[derive(Debug, Clone, Copy)] @@ -24,38 +24,11 @@ impl HintWriter { hint_bytes[4..].copy_from_slice(hint.as_bytes()); // Write the hint to the host. - let mut written = 0; - loop { - match self.pipe_handle.write(&hint_bytes[written..]) { - Ok(0) => break, - Ok(n) => { - written += n as usize; - continue; - } - Err(e) => bail!("Failed to write preimage key: {}", e), - } - } + self.pipe_handle.write(&hint_bytes)?; // Read the hint acknowledgement from the host. let mut hint_ack = [0u8; 1]; - self.read_exact(&mut hint_ack)?; - - Ok(()) - } - - /// Reads bytes into `buf` and returns the number of bytes read. - fn read(&self, buf: &mut [u8]) -> Result { - let read = self.pipe_handle.read(buf)?; - Ok(read as usize) - } - - /// Reads exactly `buf.len()` bytes into `buf`, blocking until all bytes are read. - fn read_exact(&self, buf: &mut [u8]) -> Result<()> { - let mut read = 0; - while read < buf.len() { - let chunk_read = self.read(&mut buf[read..])?; - read += chunk_read; - } + self.pipe_handle.read_exact(&mut hint_ack)?; Ok(()) } diff --git a/crates/preimage/src/oracle.rs b/crates/preimage/src/oracle.rs index 288b1ea50..ccb9cc002 100644 --- a/crates/preimage/src/oracle.rs +++ b/crates/preimage/src/oracle.rs @@ -46,7 +46,7 @@ impl OracleReader { let mut data_buffer = alloc::vec![0; self.length]; // Grab a read lock on the preimage pipe to read the data. - self.read_exact(&mut data_buffer)?; + self.cursor += self.pipe_handle.read_exact(&mut data_buffer)? as usize; Ok(data_buffer) } @@ -68,7 +68,7 @@ impl OracleReader { } // Grab a read lock on the preimage pipe to read the data. - self.read_exact(buf)?; + self.cursor += self.pipe_handle.read_exact(buf)? as usize; Ok(()) } @@ -87,43 +87,15 @@ impl OracleReader { // Write the key to the host so that it can prepare the preimage. let key_bytes: [u8; 32] = key.into(); - let mut written = 0; - loop { - match self.pipe_handle.write(&key_bytes[written..]) { - Ok(0) => break, - Ok(n) => { - written += n as usize; - continue; - } - Err(e) => bail!("Failed to write preimage key: {}", e), - } - } + self.pipe_handle.write(&key_bytes)?; // Read the length prefix and reset the cursor. let mut length_buffer = [0u8; 8]; - self.read_exact(&mut length_buffer)?; + self.pipe_handle.read_exact(&mut length_buffer)?; self.length = u64::from_be_bytes(length_buffer) as usize; self.cursor = 0; Ok(()) } - - /// Reads bytes into `buf` and returns the number of bytes read. - fn read(&mut self, buf: &mut [u8]) -> Result { - let read = self.pipe_handle.read(buf)?; - self.cursor += read as usize; - Ok(read as usize) - } - - /// Reads exactly `buf.len()` bytes into `buf`, blocking until all bytes are read. - fn read_exact(&mut self, buf: &mut [u8]) -> Result<()> { - let mut read = 0; - while read < buf.len() { - let chunk_read = self.read(&mut buf[read..])?; - read += chunk_read; - } - - Ok(()) - } } #[cfg(test)] diff --git a/crates/preimage/src/pipe.rs b/crates/preimage/src/pipe.rs index 7cf83fbf1..42194a69f 100644 --- a/crates/preimage/src/pipe.rs +++ b/crates/preimage/src/pipe.rs @@ -1,7 +1,7 @@ //! This module contains a rudamentary pipe between two file descriptors, using [kona_common::io] for //! reading and writing from the file descriptors. -use anyhow::Result; +use anyhow::{bail, Result}; use kona_common::{io, FileDescriptor, RegisterSize}; /// [PipeHandle] is a handle for one end of a bidirectional pipe. @@ -27,8 +27,29 @@ impl PipeHandle { io::read(self.read_handle, buf) } + /// Reads exactly `buf.len()` bytes into `buf`, blocking until all bytes are read. + pub fn read_exact(&self, buf: &mut [u8]) -> Result { + let mut read = 0; + while read < buf.len() { + let chunk_read = self.read(&mut buf[read..])?; + read += chunk_read as usize; + } + Ok(read as RegisterSize) + } + /// Write the given buffer to the pipe. pub fn write(&self, buf: &[u8]) -> Result { - io::write(self.write_handle, buf) + let mut written = 0; + loop { + match io::write(self.write_handle, &buf[written..]) { + Ok(0) => break, + Ok(n) => { + written += n as usize; + continue; + } + Err(e) => bail!("Failed to write preimage key: {}", e), + } + } + Ok(written as RegisterSize) } } diff --git a/fpvm-tests/cannon-go-tests/minimal_test.go b/fpvm-tests/cannon-go-tests/minimal_test.go index 89ee32c14..b59a52cc3 100644 --- a/fpvm-tests/cannon-go-tests/minimal_test.go +++ b/fpvm-tests/cannon-go-tests/minimal_test.go @@ -25,7 +25,7 @@ func TestMinimal(t *testing.T) { us := mipsevm.NewInstrumentedState(state, oracle, io.MultiWriter(&stdOutBuf, os.Stdout), io.MultiWriter(&stdErrBuf, os.Stderr)) for i := 0; i < 200_000; i++ { - _, err := us.Step(true) + _, err := us.Step(false) require.NoError(t, err) if state.Exited { fmt.Printf("exited @ step #%d\n", state.Step)