Skip to content

Commit 1da3f30

Browse files
authored
feat: more journal (#115)
* feat: more journal * lint: clippy * fix: type inference
1 parent 11c5d4a commit 1da3f30

File tree

8 files changed

+128
-8
lines changed

8 files changed

+128
-8
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "trevm"
3-
version = "0.27.0"
3+
version = "0.27.1"
44
rust-version = "1.83.0"
55
edition = "2021"
66
authors = ["init4"]

src/evm.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2134,11 +2134,12 @@ where
21342134
let Self { mut inner, state: TransactedState { result } } = self;
21352135

21362136
trevm_try!(inner.db_mut().try_commit(result.state), Trevm { inner, state: NeedsTx::new() });
2137+
21372138
Ok((result.result, Trevm { inner, state: NeedsTx::new() }))
21382139
}
21392140

2140-
/// Accept the state changes, commiting them to the database. Do not return
2141-
/// the [`ExecutionResult.`]
2141+
/// Accept the state changes, commiting them to the database, dropping the
2142+
/// [`ExecutionResult`].
21422143
pub fn accept_state(self) -> EvmNeedsTx<Db, Insp>
21432144
where
21442145
Db: DatabaseCommit,

src/journal/coder.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::journal::{AcctDiff, BundleStateIndex, InfoOutcome};
22
use alloy::{
3+
consensus::Header,
34
primitives::{Address, Bytes, B256, U256},
45
rlp::{Buf, BufMut},
56
};
@@ -69,6 +70,9 @@ pub enum JournalDecodeError {
6970

7071
/// Error decoding an EIP-7702 bytecode.
7172
Eip7702Decode(Eip7702DecodeError),
73+
74+
/// RLP decoding error.
75+
Rlp(alloy::rlp::Error),
7276
}
7377

7478
impl core::fmt::Display for JournalDecodeError {
@@ -89,6 +93,9 @@ impl core::fmt::Display for JournalDecodeError {
8993
Self::Eip7702Decode(e) => {
9094
write!(f, "error decoding EIP-7702 bytecode: {e}")
9195
}
96+
Self::Rlp(e) => {
97+
write!(f, "error decoding RLP: {e}")
98+
}
9299
}
93100
}
94101
}
@@ -97,6 +104,7 @@ impl core::error::Error for JournalDecodeError {
97104
fn cause(&self) -> Option<&dyn core::error::Error> {
98105
match self {
99106
Self::Eip7702Decode(e) => Some(e),
107+
Self::Rlp(e) => Some(e),
100108
_ => None,
101109
}
102110
}
@@ -108,6 +116,7 @@ impl core::error::Error for JournalDecodeError {
108116
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
109117
match self {
110118
Self::Eip7702Decode(e) => Some(e),
119+
Self::Rlp(e) => Some(e),
111120
_ => None,
112121
}
113122
}
@@ -119,6 +128,12 @@ impl From<Eip7702DecodeError> for JournalDecodeError {
119128
}
120129
}
121130

131+
impl From<alloy::rlp::Error> for JournalDecodeError {
132+
fn from(err: alloy::rlp::Error) -> Self {
133+
Self::Rlp(err)
134+
}
135+
}
136+
122137
macro_rules! check_len {
123138
($buf:ident, $ty_name:literal, $len:expr) => {
124139
let rem = $buf.remaining();
@@ -601,6 +616,22 @@ impl JournalDecode for BundleState {
601616
}
602617
}
603618

619+
impl JournalEncode for Header {
620+
fn serialized_size(&self) -> usize {
621+
alloy::rlp::Encodable::length(&self)
622+
}
623+
624+
fn encode(&self, buf: &mut dyn BufMut) {
625+
alloy::rlp::Encodable::encode(self, buf);
626+
}
627+
}
628+
629+
impl JournalDecode for Header {
630+
fn decode(buf: &mut &[u8]) -> Result<Self> {
631+
alloy::rlp::Decodable::decode(buf).map_err(Into::into)
632+
}
633+
}
634+
604635
#[cfg(test)]
605636
mod test {
606637
use super::*;

src/journal/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,6 @@ pub use coder::{JournalDecode, JournalDecodeError, JournalEncode};
5656

5757
mod index;
5858
pub use index::{AcctDiff, BundleStateIndex, InfoOutcome};
59+
60+
mod update;
61+
pub use update::BlockUpdate;

src/journal/update.rs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
use crate::journal::{BundleStateIndex, JournalDecode, JournalDecodeError, JournalEncode};
2+
use alloy::primitives::{keccak256, B256};
3+
use std::sync::OnceLock;
4+
5+
/// Journal associated with a block
6+
#[derive(Debug, Clone, PartialEq, Eq)]
7+
pub struct BlockUpdate<'a> {
8+
/// The height of the block.
9+
height: u64,
10+
11+
/// The previous journal hash.
12+
prev_journal_hash: B256,
13+
14+
/// The indexed changes.
15+
journal: BundleStateIndex<'a>,
16+
17+
/// The serialized journal
18+
serialized: OnceLock<Vec<u8>>,
19+
20+
/// The hash of the serialized journal
21+
hash: OnceLock<B256>,
22+
}
23+
24+
impl<'a> BlockUpdate<'a> {
25+
/// Create a new block update.
26+
pub const fn new(height: u64, prev_journal_hash: B256, journal: BundleStateIndex<'a>) -> Self {
27+
Self {
28+
height,
29+
prev_journal_hash,
30+
journal,
31+
serialized: OnceLock::new(),
32+
hash: OnceLock::new(),
33+
}
34+
}
35+
36+
/// Get the height of the block.
37+
pub const fn height(&self) -> u64 {
38+
self.height
39+
}
40+
41+
/// Get the previous journal hash.
42+
pub const fn prev_journal_hash(&self) -> B256 {
43+
self.prev_journal_hash
44+
}
45+
46+
/// Get the journal index.
47+
pub const fn journal(&self) -> &BundleStateIndex<'a> {
48+
&self.journal
49+
}
50+
51+
/// Serialize the block update.
52+
pub fn serialized(&self) -> &[u8] {
53+
self.serialized.get_or_init(|| JournalEncode::encoded(self)).as_slice()
54+
}
55+
56+
/// Serialize and hash the block update.
57+
pub fn journal_hash(&self) -> B256 {
58+
*self.hash.get_or_init(|| keccak256(self.serialized()))
59+
}
60+
}
61+
62+
impl JournalEncode for BlockUpdate<'_> {
63+
fn serialized_size(&self) -> usize {
64+
8 + 32 + self.journal.serialized_size()
65+
}
66+
67+
fn encode(&self, buf: &mut dyn alloy::rlp::BufMut) {
68+
self.height.encode(buf);
69+
self.prev_journal_hash.encode(buf);
70+
self.journal.encode(buf);
71+
}
72+
}
73+
74+
impl JournalDecode for BlockUpdate<'static> {
75+
fn decode(buf: &mut &[u8]) -> Result<Self, JournalDecodeError> {
76+
let original = *buf;
77+
Ok(Self {
78+
height: JournalDecode::decode(buf)?,
79+
prev_journal_hash: JournalDecode::decode(buf)?,
80+
journal: JournalDecode::decode(buf)?,
81+
serialized: OnceLock::from(original.to_vec()),
82+
hash: OnceLock::from(keccak256(original)),
83+
})
84+
}
85+
}

src/states.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ pub(crate) mod sealed {
153153
///
154154
/// ## Basic usage:
155155
///
156-
/// Invoking with just a DB type will use [`()`] for the ext
156+
/// Invoking with just a DB type will use `()` for the ext
157157
///
158158
/// ```
159159
/// use trevm::trevm_aliases;

src/system/eip2935.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ mod test {
8888
trevm.apply_eip2935().unwrap();
8989

9090
assert_eq!(
91-
trevm.try_read_storage(HISTORY_STORAGE_ADDRESS, slot).unwrap(),
92-
prev_hash.into()
91+
trevm.try_read_storage(HISTORY_STORAGE_ADDRESS, slot).unwrap().to_be_bytes(),
92+
prev_hash
9393
);
9494
assert_eq!(
9595
trevm.try_read_code(HISTORY_STORAGE_ADDRESS).unwrap().unwrap(),

src/system/eip4788.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ mod test {
104104
U256::from(timestamp)
105105
);
106106
assert_eq!(
107-
trevm.try_read_storage(BEACON_ROOTS_ADDRESS, root_slot).unwrap(),
108-
parent_beacon_root.into()
107+
trevm.try_read_storage(BEACON_ROOTS_ADDRESS, root_slot).unwrap().to_be_bytes(),
108+
parent_beacon_root
109109
);
110110
assert_eq!(
111111
trevm.try_read_code(BEACON_ROOTS_ADDRESS).unwrap().unwrap(),

0 commit comments

Comments
 (0)