Skip to content

Commit

Permalink
feature gate lossful conversions
Browse files Browse the repository at this point in the history
Signed-off-by: Gregory Edison <[email protected]>
  • Loading branch information
greged93 committed Nov 25, 2024
1 parent f6aa1b7 commit 8583350
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 99 deletions.
3 changes: 3 additions & 0 deletions crates/scroll/revm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

pub mod states;

#[cfg(feature = "test-utils")]
mod test_utils;

#[cfg(feature = "optimism")]
pub use revm::primitives::OptimismFields;

Expand Down
31 changes: 0 additions & 31 deletions crates/scroll/revm/src/states/account_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,37 +42,6 @@ impl From<(AccountInfo, &ScrollPostExecutionContext)> for ScrollAccountInfo {
}
}

// This conversion can cause a loss of information since performed without additional context.
impl From<AccountInfo> for ScrollAccountInfo {
fn from(info: AccountInfo) -> Self {
let (code_size, poseidon_code_hash) = info
.code
.as_ref()
.map(|code| (code.len() as u64, hash_code(code.original_byte_slice())))
.unwrap_or((0, POSEIDON_EMPTY));
Self {
balance: info.balance,
nonce: info.nonce,
code_hash: info.code_hash,
code: info.code,
code_size,
poseidon_code_hash,
}
}
}

// This conversion causes a loss of information.
impl From<ScrollAccountInfo> for AccountInfo {
fn from(info: ScrollAccountInfo) -> Self {
Self {
balance: info.balance,
nonce: info.nonce,
code_hash: info.code_hash,
code: info.code,
}
}
}

impl Default for ScrollAccountInfo {
fn default() -> Self {
Self {
Expand Down
14 changes: 0 additions & 14 deletions crates/scroll/revm/src/states/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,6 @@ impl From<(BundleState, &ScrollPostExecutionContext)> for ScrollBundleState {
}
}

// This conversion can cause a loss of information since performed without additional context.
impl From<BundleState> for ScrollBundleState {
fn from(bundle: BundleState) -> Self {
(bundle, &ScrollPostExecutionContext::default()).into()
}
}

// This conversion can cause a loss of information since performed without additional context.
impl From<(BundleState, &())> for ScrollBundleState {
fn from((bundle, _): (BundleState, &())) -> Self {
bundle.into()
}
}

impl ScrollBundleState {
/// Return builder instance for further manipulation
pub fn builder(revert_range: RangeInclusive<u64>) -> ScrollBundleBuilder {
Expand Down
31 changes: 0 additions & 31 deletions crates/scroll/revm/src/states/changes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,6 @@ impl From<(StateChangeset, &ScrollPostExecutionContext)> for ScrollStateChangese
}
}

// This conversion can cause a loss of information since performed without additional context.
impl From<StateChangeset> for ScrollStateChangeset {
fn from(changeset: StateChangeset) -> Self {
Self {
accounts: changeset
.accounts
.into_iter()
.map(|(add, acc)| (add, acc.map(Into::into)))
.collect(),
storage: changeset.storage,
contracts: changeset.contracts,
}
}
}

/// Code copy of the [`PlainStateReverts`] to accommodate for [`ScrollAccountInfo`].
#[derive(Clone, Debug, Default)]
pub struct ScrollPlainStateReverts {
Expand Down Expand Up @@ -74,22 +59,6 @@ impl From<(PlainStateReverts, &ScrollPostExecutionContext)> for ScrollPlainState
}
}

// This conversion can cause a loss of information since performed without additional context.
impl From<PlainStateReverts> for ScrollPlainStateReverts {
fn from(reverts: PlainStateReverts) -> Self {
Self {
accounts: reverts
.accounts
.into_iter()
.map(|accounts| {
accounts.into_iter().map(|(add, acc)| (add, acc.map(Into::into))).collect()
})
.collect(),
storage: reverts.storage,
}
}
}

impl ScrollPlainStateReverts {
/// Constructs new [`ScrollPlainStateReverts`] with pre-allocated capacity.
pub fn with_capacity(capacity: usize) -> Self {
Expand Down
23 changes: 0 additions & 23 deletions crates/scroll/revm/src/states/reverts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,6 @@ impl From<(AccountRevert, &ScrollPostExecutionContext)> for ScrollAccountRevert
}
}

// This conversion can cause a loss of information since performed without additional context.
impl From<AccountRevert> for ScrollAccountRevert {
fn from(account: AccountRevert) -> Self {
Self {
account: account.account.into(),
storage: account.storage,
previous_status: account.previous_status,
wipe_storage: account.wipe_storage,
}
}
}

impl ScrollAccountRevert {
/// The approximate size of changes needed to store this account revert.
/// `1 + storage_reverts_len`
Expand Down Expand Up @@ -163,14 +151,3 @@ impl From<(AccountInfoRevert, &ScrollPostExecutionContext)> for ScrollAccountInf
}
}
}

// This conversion can cause a loss of information since performed without additional context.
impl From<AccountInfoRevert> for ScrollAccountInfoRevert {
fn from(account: AccountInfoRevert) -> Self {
match account {
AccountInfoRevert::DoNothing => Self::DoNothing,
AccountInfoRevert::DeleteIt => Self::DeleteIt,
AccountInfoRevert::RevertTo(account) => Self::RevertTo(account.into()),
}
}
}
104 changes: 104 additions & 0 deletions crates/scroll/revm/src/test_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
use crate::{
shared::AccountInfo,
states::{
ScrollAccountInfoRevert, ScrollAccountRevert, ScrollBundleState, ScrollPlainStateReverts,
ScrollStateChangeset,
},
ScrollAccountInfo,
};
use reth_scroll_primitives::{hash_code, ScrollPostExecutionContext, POSEIDON_EMPTY};
use revm::db::{
states::{reverts::AccountInfoRevert, PlainStateReverts, StateChangeset},
AccountRevert, BundleState,
};

impl From<(BundleState, &())> for ScrollBundleState {
fn from((bundle, _): (BundleState, &())) -> Self {
(bundle, &ScrollPostExecutionContext::default()).into()
}
}

impl From<BundleState> for ScrollBundleState {
fn from(bundle: BundleState) -> Self {
(bundle, &ScrollPostExecutionContext::default()).into()
}
}

impl From<AccountInfo> for ScrollAccountInfo {
fn from(info: AccountInfo) -> Self {
let (code_size, poseidon_code_hash) = info
.code
.as_ref()
.map(|code| (code.len() as u64, hash_code(code.original_byte_slice())))
.unwrap_or((0, POSEIDON_EMPTY));
Self {
balance: info.balance,
nonce: info.nonce,
code_hash: info.code_hash,
code: info.code,
code_size,
poseidon_code_hash,
}
}
}

impl From<ScrollAccountInfo> for AccountInfo {
fn from(info: ScrollAccountInfo) -> Self {
Self {
balance: info.balance,
nonce: info.nonce,
code_hash: info.code_hash,
code: info.code,
}
}
}

impl From<AccountRevert> for ScrollAccountRevert {
fn from(account: AccountRevert) -> Self {
Self {
account: account.account.into(),
storage: account.storage,
previous_status: account.previous_status,
wipe_storage: account.wipe_storage,
}
}
}

impl From<AccountInfoRevert> for ScrollAccountInfoRevert {
fn from(account: AccountInfoRevert) -> Self {
match account {
AccountInfoRevert::DoNothing => Self::DoNothing,
AccountInfoRevert::DeleteIt => Self::DeleteIt,
AccountInfoRevert::RevertTo(account) => Self::RevertTo(account.into()),
}
}
}

impl From<StateChangeset> for ScrollStateChangeset {
fn from(changeset: StateChangeset) -> Self {
Self {
accounts: changeset
.accounts
.into_iter()
.map(|(add, acc)| (add, acc.map(Into::into)))
.collect(),
storage: changeset.storage,
contracts: changeset.contracts,
}
}
}

impl From<PlainStateReverts> for ScrollPlainStateReverts {
fn from(reverts: PlainStateReverts) -> Self {
Self {
accounts: reverts
.accounts
.into_iter()
.map(|accounts| {
accounts.into_iter().map(|(add, acc)| (add, acc.map(Into::into))).collect()
})
.collect(),
storage: reverts.storage,
}
}
}

0 comments on commit 8583350

Please sign in to comment.