Skip to content

Commit 4404ad5

Browse files
authored
refactor: BREAKING JournalEncode::encoded produces Bytes (#117)
* refactor: BREAKING JournalEncode::encoded produces Bytes * chore: version bump
1 parent 1da3f30 commit 4404ad5

File tree

5 files changed

+12
-13
lines changed

5 files changed

+12
-13
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.1"
3+
version = "0.27.2"
44
rust-version = "1.83.0"
55
edition = "2021"
66
authors = ["init4"]

src/journal/coder.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::journal::{AcctDiff, BundleStateIndex, InfoOutcome};
22
use alloy::{
33
consensus::Header,
44
primitives::{Address, Bytes, B256, U256},
5-
rlp::{Buf, BufMut},
5+
rlp::{Buf, BufMut, BytesMut},
66
};
77
use revm::{
88
bytecode::eip7702::{Eip7702Bytecode, Eip7702DecodeError},
@@ -13,7 +13,6 @@ use std::{
1313
borrow::{Cow, ToOwned},
1414
collections::BTreeMap,
1515
fmt::Debug,
16-
vec::Vec,
1716
};
1817

1918
type Result<T, E = JournalDecodeError> = core::result::Result<T, E>;
@@ -156,10 +155,10 @@ pub trait JournalEncode: Debug {
156155
fn encode(&self, buf: &mut dyn BufMut);
157156

158157
/// Shortcut to encode the type into a new vec.
159-
fn encoded(&self) -> Vec<u8> {
160-
let mut buf = Vec::new();
158+
fn encoded(&self) -> Bytes {
159+
let mut buf = BytesMut::with_capacity(self.serialized_size());
161160
self.encode(&mut buf);
162-
buf
161+
buf.freeze().into()
163162
}
164163
}
165164

@@ -641,7 +640,7 @@ mod test {
641640
let enc = JournalEncode::encoded(expected);
642641
let ty_name = core::any::type_name::<T>();
643642
assert_eq!(enc.len(), expected.serialized_size(), "{ty_name}");
644-
let dec = T::decode(&mut enc.as_slice()).expect("decoding failed");
643+
let dec = T::decode(&mut enc.as_ref()).expect("decoding failed");
645644
assert_eq!(&dec, expected, "{ty_name}");
646645
}
647646

src/journal/index.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ impl From<AcctDiff<'_>> for BundleAccount {
160160
/// # fn make_index(bundle_state: &BundleState) -> Result<(), JournalDecodeError> {
161161
/// let index = BundleStateIndex::from(bundle_state);
162162
/// let serialized_index = index.encoded();
163-
/// let decoded = BundleStateIndex::decode(&mut serialized_index.as_slice())?;
163+
/// let decoded = BundleStateIndex::decode(&mut serialized_index.as_ref())?;
164164
/// assert_eq!(index, decoded);
165165
/// # Ok(())
166166
/// # }

src/journal/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
//!
3232
//! // We can serialize it and deserialize it :)
3333
//! let serialized_index = index.encoded();
34-
//! let decoded = BundleStateIndex::decode(&mut serialized_index.as_slice())?;
34+
//! let decoded = BundleStateIndex::decode(&mut serialized_index.as_ref())?;
3535
//! assert_eq!(index, decoded);
3636
//!
3737
//! // It contains information about accounts

src/journal/update.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::journal::{BundleStateIndex, JournalDecode, JournalDecodeError, JournalEncode};
2-
use alloy::primitives::{keccak256, B256};
2+
use alloy::primitives::{keccak256, Bytes, B256};
33
use std::sync::OnceLock;
44

55
/// Journal associated with a block
@@ -15,7 +15,7 @@ pub struct BlockUpdate<'a> {
1515
journal: BundleStateIndex<'a>,
1616

1717
/// The serialized journal
18-
serialized: OnceLock<Vec<u8>>,
18+
serialized: OnceLock<Bytes>,
1919

2020
/// The hash of the serialized journal
2121
hash: OnceLock<B256>,
@@ -50,7 +50,7 @@ impl<'a> BlockUpdate<'a> {
5050

5151
/// Serialize the block update.
5252
pub fn serialized(&self) -> &[u8] {
53-
self.serialized.get_or_init(|| JournalEncode::encoded(self)).as_slice()
53+
self.serialized.get_or_init(|| JournalEncode::encoded(self)).as_ref()
5454
}
5555

5656
/// Serialize and hash the block update.
@@ -78,7 +78,7 @@ impl JournalDecode for BlockUpdate<'static> {
7878
height: JournalDecode::decode(buf)?,
7979
prev_journal_hash: JournalDecode::decode(buf)?,
8080
journal: JournalDecode::decode(buf)?,
81-
serialized: OnceLock::from(original.to_vec()),
81+
serialized: OnceLock::from(Bytes::copy_from_slice(original)),
8282
hash: OnceLock::from(keccak256(original)),
8383
})
8484
}

0 commit comments

Comments
 (0)