Skip to content

Commit 4e9e6f3

Browse files
authored
refactor: use signet-journal better in signet-evm (#118)
1 parent c92a5ca commit 4e9e6f3

File tree

8 files changed

+74
-40
lines changed

8 files changed

+74
-40
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ members = ["crates/*"]
33
resolver = "2"
44

55
[workspace.package]
6-
version = "0.10.0"
6+
version = "0.10.1"
77
edition = "2021"
88
rust-version = "1.81"
99
authors = ["init4"]

crates/evm/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ repository.workspace = true
1111

1212
[dependencies]
1313
signet-extract.workspace = true
14+
signet-journal.workspace = true
1415
signet-types.workspace = true
1516
signet-zenith.workspace = true
1617

crates/evm/src/driver.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,8 +320,10 @@ impl<'a, 'b, C: Extractable> SignetDriver<'a, 'b, C> {
320320
Insp: Inspector<Ctx<State<Db>>>,
321321
{
322322
let ru_height = self.extracts.ru_height;
323+
let host_height = self.extracts.host_block.number();
323324
let (sealed_block, receipts) = self.finish();
324325
BlockResult {
326+
host_height,
325327
sealed_block,
326328
execution_outcome: ExecutionOutcome::new(trevm.finish(), vec![receipts], ru_height),
327329
}

crates/evm/src/result.rs

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,45 @@
11
use crate::ExecutionOutcome;
22
use alloy::{consensus::Header, primitives::B256};
3+
use signet_journal::{HostJournal, JournalMeta};
34
use signet_types::primitives::{RecoveredBlock, TransactionSigned};
4-
use trevm::journal::{BlockUpdate, BundleStateIndex};
5+
use std::borrow::Cow;
6+
use trevm::journal::BundleStateIndex;
57

68
/// Output of a block execution.
79
///
810
/// This is a convenience struct that combines the consensus block object with
911
/// the result of its execution.
1012
#[derive(Debug, Default)]
11-
pub struct BlockResult<T = TransactionSigned, H = Header> {
13+
pub struct BlockResult<T = TransactionSigned> {
14+
/// The host height.
15+
pub host_height: u64,
16+
1217
/// A reth [`RecoveredBlock`], containing the sealed block and a vec of
13-
/// transaction sender.
14-
pub sealed_block: RecoveredBlock<T, H>,
18+
/// transaction senders.
19+
pub sealed_block: RecoveredBlock<T>,
20+
1521
/// The reth [`ExecutionOutcome`] containing the net state changes and
1622
/// receipts.
1723
pub execution_outcome: ExecutionOutcome,
1824
}
1925

20-
impl<T, H> BlockResult<T, H> {
26+
impl<T> BlockResult<T> {
2127
/// Create a new block result.
2228
pub const fn new(
23-
sealed_block: RecoveredBlock<T, H>,
29+
host_height: u64,
30+
sealed_block: RecoveredBlock<T>,
2431
execution_outcome: ExecutionOutcome,
2532
) -> Self {
26-
Self { sealed_block, execution_outcome }
33+
Self { host_height, sealed_block, execution_outcome }
34+
}
35+
36+
/// Get the rollup block header.
37+
pub const fn header(&self) -> &Header {
38+
self.sealed_block.block.header.header()
2739
}
2840

2941
/// Get the sealed block.
30-
pub const fn sealed_block(&self) -> &RecoveredBlock<T, H> {
42+
pub const fn sealed_block(&self) -> &RecoveredBlock<T> {
3143
&self.sealed_block
3244
}
3345

@@ -36,12 +48,20 @@ impl<T, H> BlockResult<T, H> {
3648
&self.execution_outcome
3749
}
3850

39-
/// Make a journal of the block result. This indexes the bundle state.
40-
pub fn make_journal(&self, host_block: u64, prev_journal_hash: B256) -> BlockUpdate<'_> {
41-
BlockUpdate::new(
42-
host_block,
43-
prev_journal_hash,
44-
BundleStateIndex::from(self.execution_outcome.bundle()),
45-
)
51+
/// Calculate the [`BundleStateIndex`], making a sorted index of the
52+
/// contents of [`BundleState`] in the [`ExecutionOutcome`].
53+
///
54+
/// [`BundleState`]: trevm::revm::database::BundleState
55+
pub fn index_bundle_state(&self) -> BundleStateIndex<'_> {
56+
BundleStateIndex::from(self.execution_outcome.bundle())
57+
}
58+
59+
const fn journal_meta(&self, prev_journal_hash: B256) -> JournalMeta<'_> {
60+
JournalMeta::new(self.host_height, prev_journal_hash, Cow::Borrowed(self.header()))
61+
}
62+
63+
/// Create a [`HostJournal`] by indexing the bundle state and block header.
64+
pub fn make_host_journal(&self, prev_journal_hash: B256) -> HostJournal<'_> {
65+
HostJournal::new(self.journal_meta(prev_journal_hash), self.index_bundle_state())
4666
}
4767
}

crates/journal/src/host.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use trevm::journal::{BundleStateIndex, JournalDecode, JournalDecodeError, Journa
1111
#[derive(Debug, Clone)]
1212
pub struct HostJournal<'a> {
1313
/// The metadata
14-
meta: JournalMeta,
14+
meta: JournalMeta<'a>,
1515

1616
/// The changes.
1717
journal: BundleStateIndex<'a>,
@@ -33,17 +33,17 @@ impl Eq for HostJournal<'_> {}
3333

3434
impl<'a> HostJournal<'a> {
3535
/// Create a new journal.
36-
pub const fn new(meta: JournalMeta, journal: BundleStateIndex<'a>) -> Self {
36+
pub const fn new(meta: JournalMeta<'a>, journal: BundleStateIndex<'a>) -> Self {
3737
Self { meta, journal, serialized: OnceLock::new(), hash: OnceLock::new() }
3838
}
3939

4040
/// Deconstruct the `HostJournal` into its parts.
41-
pub fn into_parts(self) -> (JournalMeta, BundleStateIndex<'a>) {
41+
pub fn into_parts(self) -> (JournalMeta<'a>, BundleStateIndex<'a>) {
4242
(self.meta, self.journal)
4343
}
4444

4545
/// Get the journal meta.
46-
pub const fn meta(&self) -> &JournalMeta {
46+
pub const fn meta(&self) -> &JournalMeta<'a> {
4747
&self.meta
4848
}
4949

@@ -63,12 +63,12 @@ impl<'a> HostJournal<'a> {
6363
}
6464

6565
/// Get the rollup block header.
66-
pub const fn header(&self) -> &Header {
66+
pub fn header(&self) -> &Header {
6767
self.meta.header()
6868
}
6969

7070
/// Get the rollup height.
71-
pub const fn rollup_height(&self) -> u64 {
71+
pub fn rollup_height(&self) -> u64 {
7272
self.meta.rollup_height()
7373
}
7474

@@ -166,7 +166,7 @@ pub(crate) mod test {
166166
#[test]
167167
fn roundtrip() {
168168
let original = HostJournal::new(
169-
JournalMeta::new(u64::MAX, B256::repeat_byte(0xff), Header::default()),
169+
JournalMeta::new(u64::MAX, B256::repeat_byte(0xff), Cow::Owned(Header::default())),
170170
make_state_diff(),
171171
);
172172

crates/journal/src/meta.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
1+
use std::borrow::Cow;
2+
13
use alloy::{consensus::Header, primitives::B256};
24
use trevm::journal::{JournalDecode, JournalDecodeError, JournalEncode};
35

46
/// Metadata for a block journal. This includes the block header, the host
57
/// height, and the hash of the previous journal.
68
#[derive(Debug, Clone, PartialEq, Eq)]
7-
pub struct JournalMeta {
9+
pub struct JournalMeta<'a> {
810
/// The host height.
911
host_height: u64,
1012

1113
/// The hash of the previous journal.
1214
prev_journal_hash: B256,
1315

1416
/// The rollup block header.
15-
header: Header,
17+
header: Cow<'a, Header>,
1618
}
1719

18-
impl JournalMeta {
20+
impl<'a> JournalMeta<'a> {
1921
/// Create a new `JournalMeta`.
20-
pub const fn new(host_height: u64, prev_journal_hash: B256, header: Header) -> Self {
22+
pub const fn new(host_height: u64, prev_journal_hash: B256, header: Cow<'a, Header>) -> Self {
2123
Self { host_height, prev_journal_hash, header }
2224
}
2325

2426
/// Deconstruct the `JournalMeta` into its parts.
2527
pub fn into_parts(self) -> (u64, B256, Header) {
26-
(self.host_height, self.prev_journal_hash, self.header)
28+
(self.host_height, self.prev_journal_hash, self.header.into_owned())
2729
}
2830

2931
/// Get the host height.
@@ -37,17 +39,17 @@ impl JournalMeta {
3739
}
3840

3941
/// Get the rollup block header.
40-
pub const fn header(&self) -> &Header {
41-
&self.header
42+
pub fn header(&self) -> &Header {
43+
self.header.as_ref()
4244
}
4345

4446
/// Get the rollup height.
45-
pub const fn rollup_height(&self) -> u64 {
47+
pub fn rollup_height(&self) -> u64 {
4648
self.header.number
4749
}
4850
}
4951

50-
impl JournalEncode for JournalMeta {
52+
impl JournalEncode for JournalMeta<'_> {
5153
fn serialized_size(&self) -> usize {
5254
8 + 32 + self.header.serialized_size()
5355
}
@@ -59,7 +61,7 @@ impl JournalEncode for JournalMeta {
5961
}
6062
}
6163

62-
impl JournalDecode for JournalMeta {
64+
impl JournalDecode for JournalMeta<'static> {
6365
fn decode(buf: &mut &[u8]) -> Result<Self, JournalDecodeError> {
6466
let host_height = JournalDecode::decode(buf)?;
6567
let prev_journal_hash = JournalDecode::decode(buf)?;
@@ -78,7 +80,7 @@ mod test {
7880
let original = JournalMeta {
7981
host_height: 13871,
8082
prev_journal_hash: B256::repeat_byte(0x7),
81-
header: Header::default(),
83+
header: Cow::Owned(Header::default()),
8284
};
8385

8486
let buf = original.encoded();

crates/journal/src/set.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,11 +297,15 @@ mod test {
297297
use super::*;
298298
use crate::{HostJournal, JournalMeta};
299299
use alloy::{consensus::Header, primitives::Bytes};
300+
use std::borrow::Cow;
300301
use trevm::{journal::BundleStateIndex, revm::state::Bytecode};
301302

302303
fn journal_at_heights(host: u64, rollup: u64, prev_hash: B256) -> Journal<'static> {
303-
let meta =
304-
JournalMeta::new(host, prev_hash, Header { number: rollup, ..Default::default() });
304+
let meta = JournalMeta::new(
305+
host,
306+
prev_hash,
307+
Cow::Owned(Header { number: rollup, ..Default::default() }),
308+
);
305309
let host = HostJournal::new(meta, Default::default());
306310

307311
Journal::V1(host)
@@ -389,7 +393,11 @@ mod test {
389393
std::borrow::Cow::Owned(Bytecode::new_legacy(Bytes::from_static(&[0, 1, 2, 3]))),
390394
);
391395
let j1_alt = Journal::V1(HostJournal::new(
392-
JournalMeta::new(101, j0.journal_hash(), Header { number: 1, ..Default::default() }),
396+
JournalMeta::new(
397+
101,
398+
j0.journal_hash(),
399+
Cow::Owned(Header { number: 1, ..Default::default() }),
400+
),
393401
j1_alt_state,
394402
));
395403

crates/journal/src/versions.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ impl<'a> Journal<'a> {
2727
}
2828

2929
/// Get the rollup block header.
30-
pub const fn header(&self) -> &Header {
30+
pub fn header(&self) -> &Header {
3131
match self {
3232
Journal::V1(journal) => journal.header(),
3333
}
@@ -48,7 +48,7 @@ impl<'a> Journal<'a> {
4848
}
4949

5050
/// Get the rollup height.
51-
pub const fn rollup_height(&self) -> u64 {
51+
pub fn rollup_height(&self) -> u64 {
5252
match self {
5353
Journal::V1(journal) => journal.rollup_height(),
5454
}
@@ -91,11 +91,12 @@ impl JournalDecode for Journal<'static> {
9191
mod test {
9292
use super::*;
9393
use crate::{host::test::make_state_diff, JournalMeta};
94+
use std::borrow::Cow;
9495

9596
#[test]
9697
fn roundtrip() {
9798
let journal = Journal::V1(HostJournal::new(
98-
JournalMeta::new(42, B256::repeat_byte(0x17), Header::default()),
99+
JournalMeta::new(42, B256::repeat_byte(0x17), Cow::Owned(Header::default())),
99100
make_state_diff(),
100101
));
101102
let mut buf = Vec::new();

0 commit comments

Comments
 (0)