Skip to content

Commit e42db92

Browse files
committed
feat: more journal
1 parent 11c5d4a commit e42db92

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

src/journal/coder.rs

Lines changed: 33 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,24 @@ impl JournalDecode for BundleState {
601616
}
602617
}
603618

619+
impl JournalEncode for Header {
620+
fn serialized_size(&self) -> usize {
621+
// Assuming the header is encoded in a way that is compatible with RLP
622+
alloy::rlp::Encodable::length(&self)
623+
}
624+
625+
fn encode(&self, buf: &mut dyn BufMut) {
626+
// Assuming the header is encoded in a way that is compatible with RLP
627+
alloy::rlp::Encodable::encode(self, buf);
628+
}
629+
}
630+
631+
impl JournalDecode for Header {
632+
fn decode(buf: &mut &[u8]) -> Result<Self> {
633+
alloy::rlp::Decodable::decode(buf).map_err(Into::into)
634+
}
635+
}
636+
604637
#[cfg(test)]
605638
mod test {
606639
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: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 JournalEncode for BlockUpdate<'_> {
25+
fn serialized_size(&self) -> usize {
26+
8 + 32 + self.journal.serialized_size()
27+
}
28+
29+
fn encode(&self, buf: &mut dyn alloy::rlp::BufMut) {
30+
self.height.encode(buf);
31+
self.prev_journal_hash.encode(buf);
32+
self.journal.encode(buf);
33+
}
34+
}
35+
36+
impl JournalDecode for BlockUpdate<'static> {
37+
fn decode(buf: &mut &[u8]) -> Result<Self, JournalDecodeError> {
38+
let original = *buf;
39+
Ok(Self {
40+
height: JournalDecode::decode(buf)?,
41+
prev_journal_hash: JournalDecode::decode(buf)?,
42+
journal: JournalDecode::decode(buf)?,
43+
serialized: OnceLock::from(original.to_vec()),
44+
hash: OnceLock::from(keccak256(original)),
45+
})
46+
}
47+
}

0 commit comments

Comments
 (0)