|
| 1 | +use crate::JournalMeta; |
| 2 | +use alloy::{ |
| 3 | + consensus::Header, |
| 4 | + primitives::{keccak256, Bytes, B256}, |
| 5 | +}; |
| 6 | +use std::sync::OnceLock; |
| 7 | +use trevm::journal::{BundleStateIndex, JournalDecode, JournalDecodeError, JournalEncode}; |
| 8 | + |
| 9 | +/// Journal associated with a host block. The journal is an index over the EVM |
| 10 | +/// state changes. It can be used to repopulate |
| 11 | +#[derive(Debug, Clone)] |
| 12 | +pub struct HostJournal<'a> { |
| 13 | + /// The metadata |
| 14 | + meta: JournalMeta, |
| 15 | + |
| 16 | + /// The changes. |
| 17 | + journal: BundleStateIndex<'a>, |
| 18 | + |
| 19 | + /// The serialized journal |
| 20 | + serialized: OnceLock<Bytes>, |
| 21 | + |
| 22 | + /// The hash of the serialized journal |
| 23 | + hash: OnceLock<B256>, |
| 24 | +} |
| 25 | + |
| 26 | +impl PartialEq for HostJournal<'_> { |
| 27 | + fn eq(&self, other: &Self) -> bool { |
| 28 | + self.meta == other.meta && self.journal == other.journal |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +impl Eq for HostJournal<'_> {} |
| 33 | + |
| 34 | +impl<'a> HostJournal<'a> { |
| 35 | + /// Create a new journal. |
| 36 | + pub const fn new(meta: JournalMeta, journal: BundleStateIndex<'a>) -> Self { |
| 37 | + Self { meta, journal, serialized: OnceLock::new(), hash: OnceLock::new() } |
| 38 | + } |
| 39 | + |
| 40 | + /// Deconstruct the `HostJournal` into its parts. |
| 41 | + pub fn into_parts(self) -> (JournalMeta, BundleStateIndex<'a>) { |
| 42 | + (self.meta, self.journal) |
| 43 | + } |
| 44 | + |
| 45 | + /// Get the journal meta. |
| 46 | + pub const fn meta(&self) -> &JournalMeta { |
| 47 | + &self.meta |
| 48 | + } |
| 49 | + |
| 50 | + /// Get the journal. |
| 51 | + pub const fn journal(&self) -> &BundleStateIndex<'a> { |
| 52 | + &self.journal |
| 53 | + } |
| 54 | + |
| 55 | + /// Get the host height. |
| 56 | + pub const fn host_height(&self) -> u64 { |
| 57 | + self.meta.host_height() |
| 58 | + } |
| 59 | + |
| 60 | + /// Get the previous journal hash. |
| 61 | + pub const fn prev_journal_hash(&self) -> B256 { |
| 62 | + self.meta.prev_journal_hash() |
| 63 | + } |
| 64 | + |
| 65 | + /// Get the rollup block header. |
| 66 | + pub const fn header(&self) -> &Header { |
| 67 | + self.meta.header() |
| 68 | + } |
| 69 | + |
| 70 | + /// Get the rollup height. |
| 71 | + pub const fn rollup_height(&self) -> u64 { |
| 72 | + self.meta.rollup_height() |
| 73 | + } |
| 74 | + |
| 75 | + /// Serialize the journal. |
| 76 | + pub fn serialized(&self) -> &Bytes { |
| 77 | + self.serialized.get_or_init(|| JournalEncode::encoded(self)) |
| 78 | + } |
| 79 | + |
| 80 | + /// Serialize and hash the journal. |
| 81 | + pub fn journal_hash(&self) -> B256 { |
| 82 | + *self.hash.get_or_init(|| keccak256(self.serialized())) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +impl JournalEncode for HostJournal<'_> { |
| 87 | + fn serialized_size(&self) -> usize { |
| 88 | + 8 + 32 + self.journal.serialized_size() |
| 89 | + } |
| 90 | + |
| 91 | + fn encode(&self, buf: &mut dyn alloy::rlp::BufMut) { |
| 92 | + self.meta.encode(buf); |
| 93 | + self.journal.encode(buf); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +impl JournalDecode for HostJournal<'static> { |
| 98 | + fn decode(buf: &mut &[u8]) -> Result<Self, JournalDecodeError> { |
| 99 | + let original = *buf; |
| 100 | + |
| 101 | + let meta = JournalMeta::decode(buf)?; |
| 102 | + let journal = JournalDecode::decode(buf)?; |
| 103 | + |
| 104 | + let bytes_read = original.len() - buf.len(); |
| 105 | + let original = &original[..bytes_read]; |
| 106 | + |
| 107 | + Ok(Self { |
| 108 | + meta, |
| 109 | + journal, |
| 110 | + serialized: OnceLock::from(Bytes::copy_from_slice(original)), |
| 111 | + hash: OnceLock::from(keccak256(original)), |
| 112 | + }) |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +#[cfg(test)] |
| 117 | +pub(crate) mod test { |
| 118 | + use super::*; |
| 119 | + use alloy::primitives::{Address, KECCAK256_EMPTY, U256}; |
| 120 | + use std::{borrow::Cow, collections::BTreeMap}; |
| 121 | + use trevm::{ |
| 122 | + journal::{AcctDiff, InfoOutcome}, |
| 123 | + revm::{ |
| 124 | + database::states::StorageSlot, |
| 125 | + state::{AccountInfo, Bytecode}, |
| 126 | + }, |
| 127 | + }; |
| 128 | + |
| 129 | + pub(crate) fn make_state_diff() -> BundleStateIndex<'static> { |
| 130 | + let mut bsi = BundleStateIndex::default(); |
| 131 | + |
| 132 | + let bytecode = Bytecode::new_legacy(Bytes::from_static(b"world")); |
| 133 | + let code_hash = bytecode.hash_slow(); |
| 134 | + |
| 135 | + bsi.new_contracts.insert(code_hash, Cow::Owned(bytecode)); |
| 136 | + |
| 137 | + bsi.state.insert( |
| 138 | + Address::repeat_byte(0x99), |
| 139 | + AcctDiff { |
| 140 | + outcome: InfoOutcome::Diff { |
| 141 | + old: Cow::Owned(AccountInfo { |
| 142 | + balance: U256::from(38), |
| 143 | + nonce: 7, |
| 144 | + code_hash: KECCAK256_EMPTY, |
| 145 | + code: None, |
| 146 | + }), |
| 147 | + new: Cow::Owned(AccountInfo { |
| 148 | + balance: U256::from(23828839), |
| 149 | + nonce: 83, |
| 150 | + code_hash, |
| 151 | + code: None, |
| 152 | + }), |
| 153 | + }, |
| 154 | + storage_diff: BTreeMap::from_iter([( |
| 155 | + U256::MAX, |
| 156 | + Cow::Owned(StorageSlot { |
| 157 | + previous_or_original_value: U256::from(123456), |
| 158 | + present_value: U256::from(654321), |
| 159 | + }), |
| 160 | + )]), |
| 161 | + }, |
| 162 | + ); |
| 163 | + bsi |
| 164 | + } |
| 165 | + |
| 166 | + #[test] |
| 167 | + fn roundtrip() { |
| 168 | + let original = HostJournal::new( |
| 169 | + JournalMeta::new(u64::MAX, B256::repeat_byte(0xff), Header::default()), |
| 170 | + make_state_diff(), |
| 171 | + ); |
| 172 | + |
| 173 | + let buf = original.encoded(); |
| 174 | + |
| 175 | + let decoded = HostJournal::decode(&mut &buf[..]).unwrap(); |
| 176 | + assert_eq!(original, decoded); |
| 177 | + } |
| 178 | +} |
0 commit comments