From 51ac031ac5b694d47061eb777d1c9ab513d98140 Mon Sep 17 00:00:00 2001 From: Andrei Silviu Dragnea Date: Wed, 13 Sep 2023 11:38:37 +0300 Subject: [PATCH] NDEV-2043: Use less #[cfg(not(feature = "library"))] --- evm_loader/program/src/error.rs | 4 --- evm_loader/program/src/evm/memory.rs | 5 ++-- evm_loader/program/src/evm/mod.rs | 32 +++++++++--------------- evm_loader/program/src/evm/stack.rs | 7 +++--- evm_loader/program/src/executor/cache.rs | 12 ++++----- evm_loader/program/src/executor/state.rs | 2 -- evm_loader/program/src/state_account.rs | 16 ++++-------- 7 files changed, 28 insertions(+), 50 deletions(-) diff --git a/evm_loader/program/src/error.rs b/evm_loader/program/src/error.rs index fc671467fc..40631225e6 100644 --- a/evm_loader/program/src/error.rs +++ b/evm_loader/program/src/error.rs @@ -199,7 +199,6 @@ macro_rules! Err { /// # map_err(|s| E!(ProgramError::InvalidArgument; "s={:?}", s)) /// ``` /// -#[cfg(not(feature = "library"))] macro_rules! E { ( $n:expr; $($args:expr),* ) => ({ #[cfg(target_os = "solana")] @@ -212,7 +211,6 @@ macro_rules! E { }); } -#[cfg(not(feature = "library"))] #[must_use] fn format_revert_error(msg: &[u8]) -> Option<&str> { if msg.starts_with(&[0x08, 0xc3, 0x79, 0xa0]) { @@ -240,7 +238,6 @@ fn format_revert_error(msg: &[u8]) -> Option<&str> { } } -#[cfg(not(feature = "library"))] #[must_use] fn format_revert_panic(msg: &[u8]) -> Option { if msg.starts_with(&[0x4e, 0x48, 0x7b, 0x71]) { @@ -257,7 +254,6 @@ fn format_revert_panic(msg: &[u8]) -> Option { } } -#[cfg(not(feature = "library"))] pub fn print_revert_message(msg: &[u8]) { if msg.is_empty() { return solana_program::msg!("Revert"); diff --git a/evm_loader/program/src/evm/memory.rs b/evm_loader/program/src/evm/memory.rs index 88c40d05ff..325c367dfd 100644 --- a/evm_loader/program/src/evm/memory.rs +++ b/evm_loader/program/src/evm/memory.rs @@ -54,7 +54,6 @@ impl Memory { } } - #[cfg(not(feature = "library"))] pub fn from_buffer(v: &[u8]) -> Self { let capacity = v.len().next_power_of_two().max(MEMORY_CAPACITY); @@ -71,6 +70,8 @@ impl Memory { data, capacity, size: v.len(), + #[cfg(feature = "library")] + tracer: None, } } } @@ -280,7 +281,6 @@ impl Drop for Memory { } } -#[cfg(not(feature = "library"))] impl serde::Serialize for Memory { fn serialize(&self, serializer: S) -> Result where @@ -291,7 +291,6 @@ impl serde::Serialize for Memory { } } -#[cfg(not(feature = "library"))] impl<'de> serde::Deserialize<'de> for Memory { fn deserialize(deserializer: D) -> Result where diff --git a/evm_loader/program/src/evm/mod.rs b/evm_loader/program/src/evm/mod.rs index bbb303da00..e8df6f32c7 100644 --- a/evm_loader/program/src/evm/mod.rs +++ b/evm_loader/program/src/evm/mod.rs @@ -6,7 +6,6 @@ use std::{marker::PhantomData, ops::Range}; use ethnum::U256; use maybe_async::maybe_async; -#[cfg(not(feature = "library"))] use serde::{Deserialize, Serialize}; use solana_program::log::sol_log_data; @@ -77,12 +76,11 @@ macro_rules! trace_end_step { pub(crate) use trace_end_step; pub(crate) use tracing_event; -#[derive(Debug, Clone, Eq, PartialEq)] -#[cfg_attr(not(feature = "library"), derive(Serialize, Deserialize))] +#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)] pub enum ExitStatus { Stop, - Return(#[cfg_attr(not(feature = "library"), serde(with = "serde_bytes"))] Vec), - Revert(#[cfg_attr(not(feature = "library"), serde(with = "serde_bytes"))] Vec), + Return(#[serde(with = "serde_bytes")] Vec), + Revert(#[serde(with = "serde_bytes")] Vec), Suicide, StepLimit, } @@ -115,36 +113,31 @@ impl ExitStatus { } } -#[derive(Debug, Eq, PartialEq)] -#[cfg_attr(not(feature = "library"), derive(Serialize, Deserialize))] +#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)] pub enum Reason { Call, Create, } -#[derive(Debug, Copy, Clone)] -#[cfg_attr(not(feature = "library"), derive(Serialize, Deserialize))] +#[derive(Debug, Copy, Clone, Serialize, Deserialize)] pub struct Context { pub caller: Address, pub contract: Address, - #[cfg_attr(not(feature = "library"), serde(with = "ethnum::serde::bytes::le"))] + #[serde(with = "ethnum::serde::bytes::le")] pub value: U256, pub code_address: Option
, } -#[cfg_attr( - not(feature = "library"), - derive(Serialize, Deserialize), - serde(bound = "B: Database") -)] +#[derive(Serialize, Deserialize)] +#[serde(bound = "B: Database")] pub struct Machine { origin: Address, context: Context, - #[cfg_attr(not(feature = "library"), serde(with = "ethnum::serde::bytes::le"))] + #[serde(with = "ethnum::serde::bytes::le")] gas_price: U256, - #[cfg_attr(not(feature = "library"), serde(with = "ethnum::serde::bytes::le"))] + #[serde(with = "ethnum::serde::bytes::le")] gas_limit: U256, execution_code: Buffer, @@ -161,16 +154,15 @@ pub struct Machine { parent: Option>, - #[cfg_attr(not(feature = "library"), serde(skip))] + #[serde(skip)] phantom: PhantomData<*const B>, #[cfg(feature = "library")] - #[cfg_attr(not(feature = "library"), serde(skip))] + #[serde(skip)] tracer: TracerTypeOpt, } impl Machine { - #[cfg(not(feature = "library"))] pub fn serialize_into(&self, buffer: &mut [u8]) -> Result { let mut cursor = std::io::Cursor::new(buffer); diff --git a/evm_loader/program/src/evm/stack.rs b/evm_loader/program/src/evm/stack.rs index c531761306..4906550ff6 100644 --- a/evm_loader/program/src/evm/stack.rs +++ b/evm_loader/program/src/evm/stack.rs @@ -279,7 +279,6 @@ impl Drop for Stack { } } -#[cfg(not(feature = "library"))] impl serde::Serialize for Stack { fn serialize(&self, serializer: S) -> Result where @@ -294,7 +293,6 @@ impl serde::Serialize for Stack { } } -#[cfg(not(feature = "library"))] impl<'de> serde::Deserialize<'de> for Stack { fn deserialize(deserializer: D) -> Result where @@ -317,7 +315,10 @@ impl<'de> serde::Deserialize<'de> for Stack { return Err(E::invalid_length(v.len(), &self)); } - let mut stack = Stack::new(); + let mut stack = Stack::new( + #[cfg(feature = "library")] + None, + ); unsafe { stack.top = stack.begin.add(v.len()); diff --git a/evm_loader/program/src/executor/cache.rs b/evm_loader/program/src/executor/cache.rs index 963e694f7a..062d2829a1 100644 --- a/evm_loader/program/src/executor/cache.rs +++ b/evm_loader/program/src/executor/cache.rs @@ -1,18 +1,16 @@ use std::{cell::RefCell, collections::BTreeMap, rc::Rc}; use ethnum::U256; -#[cfg(not(feature = "library"))] use serde::{Deserialize, Serialize}; use solana_program::{account_info::AccountInfo, pubkey::Pubkey}; -#[derive(Clone)] -#[cfg_attr(not(feature = "library"), derive(Serialize, Deserialize))] +#[derive(Clone, Serialize, Deserialize)] pub struct OwnedAccountInfo { pub key: Pubkey, pub is_signer: bool, pub is_writable: bool, pub lamports: u64, - #[cfg_attr(not(feature = "library"), serde(with = "serde_bytes"))] + #[serde(with = "serde_bytes")] pub data: Vec, pub owner: Pubkey, pub executable: bool, @@ -56,11 +54,11 @@ impl<'a> solana_program::account_info::IntoAccountInfo<'a> for &'a mut OwnedAcco } } -#[cfg_attr(not(feature = "library"), derive(Serialize, Deserialize))] +#[derive(Serialize, Deserialize)] pub struct Cache { pub solana_accounts: BTreeMap, - #[cfg_attr(not(feature = "library"), serde(with = "ethnum::serde::bytes::le"))] + #[serde(with = "ethnum::serde::bytes::le")] pub block_number: U256, - #[cfg_attr(not(feature = "library"), serde(with = "ethnum::serde::bytes::le"))] + #[serde(with = "ethnum::serde::bytes::le")] pub block_timestamp: U256, } diff --git a/evm_loader/program/src/executor/state.rs b/evm_loader/program/src/executor/state.rs index 39a2867391..0d04a36c10 100644 --- a/evm_loader/program/src/executor/state.rs +++ b/evm_loader/program/src/executor/state.rs @@ -27,7 +27,6 @@ pub struct ExecutorState<'a, B: AccountStorage> { } impl<'a, B: AccountStorage> ExecutorState<'a, B> { - #[cfg(not(feature = "library"))] pub fn serialize_into(&self, buffer: &mut [u8]) -> Result { let mut cursor = std::io::Cursor::new(buffer); @@ -37,7 +36,6 @@ impl<'a, B: AccountStorage> ExecutorState<'a, B> { cursor.position().try_into().map_err(Error::from) } - #[cfg(not(feature = "library"))] pub fn deserialize_from(buffer: &[u8], backend: &'a B) -> Result { let (cache, actions, stack, exit_status) = bincode::deserialize(buffer)?; Ok(Self { diff --git a/evm_loader/program/src/state_account.rs b/evm_loader/program/src/state_account.rs index 309f594c22..3b8b44a25b 100644 --- a/evm_loader/program/src/state_account.rs +++ b/evm_loader/program/src/state_account.rs @@ -1,12 +1,15 @@ #[cfg(not(feature = "library"))] use { crate::account::program, + crate::types::{Address, Transaction}, + ethnum::U256, +}; + +use { crate::account::EthereumAccount, crate::account::Holder, crate::config::OPERATOR_PRIORITY_SLOTS, crate::error::Error, - crate::types::{Address, Transaction}, - ethnum::U256, solana_program::account_info::AccountInfo, solana_program::clock::Clock, solana_program::sysvar::Sysvar, @@ -91,7 +94,6 @@ impl<'a> State<'a> { Ok(storage) } - #[cfg(not(feature = "library"))] pub fn restore( program_id: &Pubkey, info: &'a AccountInfo<'a>, @@ -126,7 +128,6 @@ impl<'a> State<'a> { Ok((storage, blocked_accounts)) } - #[cfg(not(feature = "library"))] pub fn finalize(self, deposit: Deposit<'a>) -> Result, ProgramError> { debug_print!("Finalize Storage {}", self.info.key); @@ -153,7 +154,6 @@ impl<'a> State<'a> { system_program.transfer(source, self.info, crate::config::PAYMENT_TO_DEPOSIT) } - #[cfg(not(feature = "library"))] fn withdraw_deposit(&self, target: &AccountInfo<'a>) -> Result<(), ProgramError> { let source_lamports = self .info @@ -222,7 +222,6 @@ impl<'a> State<'a> { Ok(()) } - #[cfg(not(feature = "library"))] pub fn update_blocked_accounts(&mut self, accounts: I) -> Result<(), Error> where I: ExactSizeIterator, @@ -250,7 +249,6 @@ impl<'a> State<'a> { Ok(()) } - #[cfg(not(feature = "library"))] fn check_blocked_accounts( &self, program_id: &Pubkey, @@ -284,7 +282,6 @@ impl<'a> State<'a> { Ok(blocked_accounts) } - #[cfg(not(feature = "library"))] #[must_use] pub fn evm_data(&self) -> Ref<[u8]> { let (begin, end) = self.evm_data_region(); @@ -293,7 +290,6 @@ impl<'a> State<'a> { Ref::map(data, |d| &d[begin..end]) } - #[cfg(not(feature = "library"))] #[must_use] pub fn evm_data_mut(&mut self) -> RefMut<[u8]> { let (begin, end) = self.evm_data_region(); @@ -302,7 +298,6 @@ impl<'a> State<'a> { RefMut::map(data, |d| &mut d[begin..end]) } - #[cfg(not(feature = "library"))] #[must_use] fn evm_data_region(&self) -> (usize, usize) { let (_, accounts_region_end) = self.blocked_accounts_region(); @@ -321,7 +316,6 @@ impl<'a> State<'a> { (begin, end) } - #[cfg(not(feature = "library"))] #[must_use] fn account_exists(program_id: &Pubkey, info: &AccountInfo) -> bool { (info.owner == program_id)