Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "trevm"
version = "0.27.0"
version = "0.27.1"
rust-version = "1.83.0"
edition = "2021"
authors = ["init4"]
Expand Down
5 changes: 3 additions & 2 deletions src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2134,11 +2134,12 @@ where
let Self { mut inner, state: TransactedState { result } } = self;

trevm_try!(inner.db_mut().try_commit(result.state), Trevm { inner, state: NeedsTx::new() });

Ok((result.result, Trevm { inner, state: NeedsTx::new() }))
}

/// Accept the state changes, commiting them to the database. Do not return
/// the [`ExecutionResult.`]
/// Accept the state changes, commiting them to the database, dropping the
/// [`ExecutionResult`].
pub fn accept_state(self) -> EvmNeedsTx<Db, Insp>
where
Db: DatabaseCommit,
Expand Down
31 changes: 31 additions & 0 deletions src/journal/coder.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::journal::{AcctDiff, BundleStateIndex, InfoOutcome};
use alloy::{
consensus::Header,
primitives::{Address, Bytes, B256, U256},
rlp::{Buf, BufMut},
};
Expand Down Expand Up @@ -69,6 +70,9 @@ pub enum JournalDecodeError {

/// Error decoding an EIP-7702 bytecode.
Eip7702Decode(Eip7702DecodeError),

/// RLP decoding error.
Rlp(alloy::rlp::Error),
}

impl core::fmt::Display for JournalDecodeError {
Expand All @@ -89,6 +93,9 @@ impl core::fmt::Display for JournalDecodeError {
Self::Eip7702Decode(e) => {
write!(f, "error decoding EIP-7702 bytecode: {e}")
}
Self::Rlp(e) => {
write!(f, "error decoding RLP: {e}")
}
}
}
}
Expand All @@ -97,6 +104,7 @@ impl core::error::Error for JournalDecodeError {
fn cause(&self) -> Option<&dyn core::error::Error> {
match self {
Self::Eip7702Decode(e) => Some(e),
Self::Rlp(e) => Some(e),
_ => None,
}
}
Expand All @@ -108,6 +116,7 @@ impl core::error::Error for JournalDecodeError {
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
match self {
Self::Eip7702Decode(e) => Some(e),
Self::Rlp(e) => Some(e),
_ => None,
}
}
Expand All @@ -119,6 +128,12 @@ impl From<Eip7702DecodeError> for JournalDecodeError {
}
}

impl From<alloy::rlp::Error> for JournalDecodeError {
fn from(err: alloy::rlp::Error) -> Self {
Self::Rlp(err)
}
}

macro_rules! check_len {
($buf:ident, $ty_name:literal, $len:expr) => {
let rem = $buf.remaining();
Expand Down Expand Up @@ -601,6 +616,22 @@ impl JournalDecode for BundleState {
}
}

impl JournalEncode for Header {
fn serialized_size(&self) -> usize {
alloy::rlp::Encodable::length(&self)
}

fn encode(&self, buf: &mut dyn BufMut) {
alloy::rlp::Encodable::encode(self, buf);
}
}

impl JournalDecode for Header {
fn decode(buf: &mut &[u8]) -> Result<Self> {
alloy::rlp::Decodable::decode(buf).map_err(Into::into)
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
3 changes: 3 additions & 0 deletions src/journal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,6 @@ pub use coder::{JournalDecode, JournalDecodeError, JournalEncode};

mod index;
pub use index::{AcctDiff, BundleStateIndex, InfoOutcome};

mod update;
pub use update::BlockUpdate;
85 changes: 85 additions & 0 deletions src/journal/update.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
use crate::journal::{BundleStateIndex, JournalDecode, JournalDecodeError, JournalEncode};
use alloy::primitives::{keccak256, B256};
use std::sync::OnceLock;

/// Journal associated with a block
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BlockUpdate<'a> {
/// The height of the block.
height: u64,

/// The previous journal hash.
prev_journal_hash: B256,

/// The indexed changes.
journal: BundleStateIndex<'a>,

/// The serialized journal
serialized: OnceLock<Vec<u8>>,

/// The hash of the serialized journal
hash: OnceLock<B256>,
}

impl<'a> BlockUpdate<'a> {
/// Create a new block update.
pub const fn new(height: u64, prev_journal_hash: B256, journal: BundleStateIndex<'a>) -> Self {
Self {
height,
prev_journal_hash,
journal,
serialized: OnceLock::new(),
hash: OnceLock::new(),
}
}

/// Get the height of the block.
pub const fn height(&self) -> u64 {
self.height
}

/// Get the previous journal hash.
pub const fn prev_journal_hash(&self) -> B256 {
self.prev_journal_hash
}

/// Get the journal index.
pub const fn journal(&self) -> &BundleStateIndex<'a> {
&self.journal
}

/// Serialize the block update.
pub fn serialized(&self) -> &[u8] {
self.serialized.get_or_init(|| JournalEncode::encoded(self)).as_slice()
}

/// Serialize and hash the block update.
pub fn journal_hash(&self) -> B256 {
*self.hash.get_or_init(|| keccak256(self.serialized()))
}
}

impl JournalEncode for BlockUpdate<'_> {
fn serialized_size(&self) -> usize {
8 + 32 + self.journal.serialized_size()
}

fn encode(&self, buf: &mut dyn alloy::rlp::BufMut) {
self.height.encode(buf);
self.prev_journal_hash.encode(buf);
self.journal.encode(buf);
}
}

impl JournalDecode for BlockUpdate<'static> {
fn decode(buf: &mut &[u8]) -> Result<Self, JournalDecodeError> {
let original = *buf;
Ok(Self {
height: JournalDecode::decode(buf)?,
prev_journal_hash: JournalDecode::decode(buf)?,
journal: JournalDecode::decode(buf)?,
serialized: OnceLock::from(original.to_vec()),
hash: OnceLock::from(keccak256(original)),
})
}
}
2 changes: 1 addition & 1 deletion src/states.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ pub(crate) mod sealed {
///
/// ## Basic usage:
///
/// Invoking with just a DB type will use [`()`] for the ext
/// Invoking with just a DB type will use `()` for the ext
///
/// ```
/// use trevm::trevm_aliases;
Expand Down
4 changes: 2 additions & 2 deletions src/system/eip2935.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ mod test {
trevm.apply_eip2935().unwrap();

assert_eq!(
trevm.try_read_storage(HISTORY_STORAGE_ADDRESS, slot).unwrap(),
prev_hash.into()
trevm.try_read_storage(HISTORY_STORAGE_ADDRESS, slot).unwrap().to_be_bytes(),
prev_hash
);
assert_eq!(
trevm.try_read_code(HISTORY_STORAGE_ADDRESS).unwrap().unwrap(),
Expand Down
4 changes: 2 additions & 2 deletions src/system/eip4788.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ mod test {
U256::from(timestamp)
);
assert_eq!(
trevm.try_read_storage(BEACON_ROOTS_ADDRESS, root_slot).unwrap(),
parent_beacon_root.into()
trevm.try_read_storage(BEACON_ROOTS_ADDRESS, root_slot).unwrap().to_be_bytes(),
parent_beacon_root
);
assert_eq!(
trevm.try_read_code(BEACON_ROOTS_ADDRESS).unwrap().unwrap(),
Expand Down