From a663a4bf703c199853d44c9a887f6829ff7793a5 Mon Sep 17 00:00:00 2001 From: James Date: Wed, 3 Sep 2025 16:27:23 -0400 Subject: [PATCH] refactor: use signet-journal better in signet-evm --- Cargo.toml | 2 +- crates/evm/Cargo.toml | 1 + crates/evm/src/driver.rs | 2 ++ crates/evm/src/result.rs | 50 ++++++++++++++++++++++++---------- crates/journal/src/host.rs | 14 +++++----- crates/journal/src/meta.rs | 24 ++++++++-------- crates/journal/src/set.rs | 14 ++++++++-- crates/journal/src/versions.rs | 7 +++-- 8 files changed, 74 insertions(+), 40 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 25124d14..129a22dc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ members = ["crates/*"] resolver = "2" [workspace.package] -version = "0.10.0" +version = "0.10.1" edition = "2021" rust-version = "1.81" authors = ["init4"] diff --git a/crates/evm/Cargo.toml b/crates/evm/Cargo.toml index 4c322580..2c05c4f7 100644 --- a/crates/evm/Cargo.toml +++ b/crates/evm/Cargo.toml @@ -11,6 +11,7 @@ repository.workspace = true [dependencies] signet-extract.workspace = true +signet-journal.workspace = true signet-types.workspace = true signet-zenith.workspace = true diff --git a/crates/evm/src/driver.rs b/crates/evm/src/driver.rs index c307d778..c5c9ae8f 100644 --- a/crates/evm/src/driver.rs +++ b/crates/evm/src/driver.rs @@ -320,8 +320,10 @@ impl<'a, 'b, C: Extractable> SignetDriver<'a, 'b, C> { Insp: Inspector>>, { let ru_height = self.extracts.ru_height; + let host_height = self.extracts.host_block.number(); let (sealed_block, receipts) = self.finish(); BlockResult { + host_height, sealed_block, execution_outcome: ExecutionOutcome::new(trevm.finish(), vec![receipts], ru_height), } diff --git a/crates/evm/src/result.rs b/crates/evm/src/result.rs index 4ecc0b36..7d154266 100644 --- a/crates/evm/src/result.rs +++ b/crates/evm/src/result.rs @@ -1,33 +1,45 @@ use crate::ExecutionOutcome; use alloy::{consensus::Header, primitives::B256}; +use signet_journal::{HostJournal, JournalMeta}; use signet_types::primitives::{RecoveredBlock, TransactionSigned}; -use trevm::journal::{BlockUpdate, BundleStateIndex}; +use std::borrow::Cow; +use trevm::journal::BundleStateIndex; /// Output of a block execution. /// /// This is a convenience struct that combines the consensus block object with /// the result of its execution. #[derive(Debug, Default)] -pub struct BlockResult { +pub struct BlockResult { + /// The host height. + pub host_height: u64, + /// A reth [`RecoveredBlock`], containing the sealed block and a vec of - /// transaction sender. - pub sealed_block: RecoveredBlock, + /// transaction senders. + pub sealed_block: RecoveredBlock, + /// The reth [`ExecutionOutcome`] containing the net state changes and /// receipts. pub execution_outcome: ExecutionOutcome, } -impl BlockResult { +impl BlockResult { /// Create a new block result. pub const fn new( - sealed_block: RecoveredBlock, + host_height: u64, + sealed_block: RecoveredBlock, execution_outcome: ExecutionOutcome, ) -> Self { - Self { sealed_block, execution_outcome } + Self { host_height, sealed_block, execution_outcome } + } + + /// Get the rollup block header. + pub const fn header(&self) -> &Header { + self.sealed_block.block.header.header() } /// Get the sealed block. - pub const fn sealed_block(&self) -> &RecoveredBlock { + pub const fn sealed_block(&self) -> &RecoveredBlock { &self.sealed_block } @@ -36,12 +48,20 @@ impl BlockResult { &self.execution_outcome } - /// Make a journal of the block result. This indexes the bundle state. - pub fn make_journal(&self, host_block: u64, prev_journal_hash: B256) -> BlockUpdate<'_> { - BlockUpdate::new( - host_block, - prev_journal_hash, - BundleStateIndex::from(self.execution_outcome.bundle()), - ) + /// Calculate the [`BundleStateIndex`], making a sorted index of the + /// contents of [`BundleState`] in the [`ExecutionOutcome`]. + /// + /// [`BundleState`]: trevm::revm::database::BundleState + pub fn index_bundle_state(&self) -> BundleStateIndex<'_> { + BundleStateIndex::from(self.execution_outcome.bundle()) + } + + const fn journal_meta(&self, prev_journal_hash: B256) -> JournalMeta<'_> { + JournalMeta::new(self.host_height, prev_journal_hash, Cow::Borrowed(self.header())) + } + + /// Create a [`HostJournal`] by indexing the bundle state and block header. + pub fn make_host_journal(&self, prev_journal_hash: B256) -> HostJournal<'_> { + HostJournal::new(self.journal_meta(prev_journal_hash), self.index_bundle_state()) } } diff --git a/crates/journal/src/host.rs b/crates/journal/src/host.rs index 49661db8..162d92fa 100644 --- a/crates/journal/src/host.rs +++ b/crates/journal/src/host.rs @@ -11,7 +11,7 @@ use trevm::journal::{BundleStateIndex, JournalDecode, JournalDecodeError, Journa #[derive(Debug, Clone)] pub struct HostJournal<'a> { /// The metadata - meta: JournalMeta, + meta: JournalMeta<'a>, /// The changes. journal: BundleStateIndex<'a>, @@ -33,17 +33,17 @@ impl Eq for HostJournal<'_> {} impl<'a> HostJournal<'a> { /// Create a new journal. - pub const fn new(meta: JournalMeta, journal: BundleStateIndex<'a>) -> Self { + pub const fn new(meta: JournalMeta<'a>, journal: BundleStateIndex<'a>) -> Self { Self { meta, journal, serialized: OnceLock::new(), hash: OnceLock::new() } } /// Deconstruct the `HostJournal` into its parts. - pub fn into_parts(self) -> (JournalMeta, BundleStateIndex<'a>) { + pub fn into_parts(self) -> (JournalMeta<'a>, BundleStateIndex<'a>) { (self.meta, self.journal) } /// Get the journal meta. - pub const fn meta(&self) -> &JournalMeta { + pub const fn meta(&self) -> &JournalMeta<'a> { &self.meta } @@ -63,12 +63,12 @@ impl<'a> HostJournal<'a> { } /// Get the rollup block header. - pub const fn header(&self) -> &Header { + pub fn header(&self) -> &Header { self.meta.header() } /// Get the rollup height. - pub const fn rollup_height(&self) -> u64 { + pub fn rollup_height(&self) -> u64 { self.meta.rollup_height() } @@ -166,7 +166,7 @@ pub(crate) mod test { #[test] fn roundtrip() { let original = HostJournal::new( - JournalMeta::new(u64::MAX, B256::repeat_byte(0xff), Header::default()), + JournalMeta::new(u64::MAX, B256::repeat_byte(0xff), Cow::Owned(Header::default())), make_state_diff(), ); diff --git a/crates/journal/src/meta.rs b/crates/journal/src/meta.rs index 80d549d0..b929fbd6 100644 --- a/crates/journal/src/meta.rs +++ b/crates/journal/src/meta.rs @@ -1,10 +1,12 @@ +use std::borrow::Cow; + use alloy::{consensus::Header, primitives::B256}; use trevm::journal::{JournalDecode, JournalDecodeError, JournalEncode}; /// Metadata for a block journal. This includes the block header, the host /// height, and the hash of the previous journal. #[derive(Debug, Clone, PartialEq, Eq)] -pub struct JournalMeta { +pub struct JournalMeta<'a> { /// The host height. host_height: u64, @@ -12,18 +14,18 @@ pub struct JournalMeta { prev_journal_hash: B256, /// The rollup block header. - header: Header, + header: Cow<'a, Header>, } -impl JournalMeta { +impl<'a> JournalMeta<'a> { /// Create a new `JournalMeta`. - pub const fn new(host_height: u64, prev_journal_hash: B256, header: Header) -> Self { + pub const fn new(host_height: u64, prev_journal_hash: B256, header: Cow<'a, Header>) -> Self { Self { host_height, prev_journal_hash, header } } /// Deconstruct the `JournalMeta` into its parts. pub fn into_parts(self) -> (u64, B256, Header) { - (self.host_height, self.prev_journal_hash, self.header) + (self.host_height, self.prev_journal_hash, self.header.into_owned()) } /// Get the host height. @@ -37,17 +39,17 @@ impl JournalMeta { } /// Get the rollup block header. - pub const fn header(&self) -> &Header { - &self.header + pub fn header(&self) -> &Header { + self.header.as_ref() } /// Get the rollup height. - pub const fn rollup_height(&self) -> u64 { + pub fn rollup_height(&self) -> u64 { self.header.number } } -impl JournalEncode for JournalMeta { +impl JournalEncode for JournalMeta<'_> { fn serialized_size(&self) -> usize { 8 + 32 + self.header.serialized_size() } @@ -59,7 +61,7 @@ impl JournalEncode for JournalMeta { } } -impl JournalDecode for JournalMeta { +impl JournalDecode for JournalMeta<'static> { fn decode(buf: &mut &[u8]) -> Result { let host_height = JournalDecode::decode(buf)?; let prev_journal_hash = JournalDecode::decode(buf)?; @@ -78,7 +80,7 @@ mod test { let original = JournalMeta { host_height: 13871, prev_journal_hash: B256::repeat_byte(0x7), - header: Header::default(), + header: Cow::Owned(Header::default()), }; let buf = original.encoded(); diff --git a/crates/journal/src/set.rs b/crates/journal/src/set.rs index a91d20f4..fac4fba3 100644 --- a/crates/journal/src/set.rs +++ b/crates/journal/src/set.rs @@ -297,11 +297,15 @@ mod test { use super::*; use crate::{HostJournal, JournalMeta}; use alloy::{consensus::Header, primitives::Bytes}; + use std::borrow::Cow; use trevm::{journal::BundleStateIndex, revm::state::Bytecode}; fn journal_at_heights(host: u64, rollup: u64, prev_hash: B256) -> Journal<'static> { - let meta = - JournalMeta::new(host, prev_hash, Header { number: rollup, ..Default::default() }); + let meta = JournalMeta::new( + host, + prev_hash, + Cow::Owned(Header { number: rollup, ..Default::default() }), + ); let host = HostJournal::new(meta, Default::default()); Journal::V1(host) @@ -389,7 +393,11 @@ mod test { std::borrow::Cow::Owned(Bytecode::new_legacy(Bytes::from_static(&[0, 1, 2, 3]))), ); let j1_alt = Journal::V1(HostJournal::new( - JournalMeta::new(101, j0.journal_hash(), Header { number: 1, ..Default::default() }), + JournalMeta::new( + 101, + j0.journal_hash(), + Cow::Owned(Header { number: 1, ..Default::default() }), + ), j1_alt_state, )); diff --git a/crates/journal/src/versions.rs b/crates/journal/src/versions.rs index 52040961..6e2cb0d8 100644 --- a/crates/journal/src/versions.rs +++ b/crates/journal/src/versions.rs @@ -27,7 +27,7 @@ impl<'a> Journal<'a> { } /// Get the rollup block header. - pub const fn header(&self) -> &Header { + pub fn header(&self) -> &Header { match self { Journal::V1(journal) => journal.header(), } @@ -48,7 +48,7 @@ impl<'a> Journal<'a> { } /// Get the rollup height. - pub const fn rollup_height(&self) -> u64 { + pub fn rollup_height(&self) -> u64 { match self { Journal::V1(journal) => journal.rollup_height(), } @@ -91,11 +91,12 @@ impl JournalDecode for Journal<'static> { mod test { use super::*; use crate::{host::test::make_state_diff, JournalMeta}; + use std::borrow::Cow; #[test] fn roundtrip() { let journal = Journal::V1(HostJournal::new( - JournalMeta::new(42, B256::repeat_byte(0x17), Header::default()), + JournalMeta::new(42, B256::repeat_byte(0x17), Cow::Owned(Header::default())), make_state_diff(), )); let mut buf = Vec::new();