Skip to content

Commit

Permalink
Less #[cfg(not(feature = "library"))]
Browse files Browse the repository at this point in the history
  • Loading branch information
andreisilviudragnea committed Sep 21, 2023
1 parent c601bfd commit f241842
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 50 deletions.
4 changes: 0 additions & 4 deletions evm_loader/program/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand All @@ -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]) {
Expand Down Expand Up @@ -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<U256> {
if msg.starts_with(&[0x4e, 0x48, 0x7b, 0x71]) {
Expand All @@ -257,7 +254,6 @@ fn format_revert_panic(msg: &[u8]) -> Option<U256> {
}
}

#[cfg(not(feature = "library"))]
pub fn print_revert_message(msg: &[u8]) {
if msg.is_empty() {
return solana_program::msg!("Revert");
Expand Down
5 changes: 2 additions & 3 deletions evm_loader/program/src/evm/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -71,6 +70,8 @@ impl Memory {
data,
capacity,
size: v.len(),
#[cfg(feature = "library")]
tracer: None,
}
}
}
Expand Down Expand Up @@ -280,7 +281,6 @@ impl Drop for Memory {
}
}

#[cfg(not(feature = "library"))]
impl serde::Serialize for Memory {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
Expand All @@ -291,7 +291,6 @@ impl serde::Serialize for Memory {
}
}

#[cfg(not(feature = "library"))]
impl<'de> serde::Deserialize<'de> for Memory {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
Expand Down
32 changes: 12 additions & 20 deletions evm_loader/program/src/evm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<u8>),
Revert(#[cfg_attr(not(feature = "library"), serde(with = "serde_bytes"))] Vec<u8>),
Return(#[serde(with = "serde_bytes")] Vec<u8>),
Revert(#[serde(with = "serde_bytes")] Vec<u8>),
Suicide,
StepLimit,
}
Expand Down Expand Up @@ -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<Address>,
}

#[cfg_attr(
not(feature = "library"),
derive(Serialize, Deserialize),
serde(bound = "B: Database")
)]
#[derive(Serialize, Deserialize)]
#[serde(bound = "B: Database")]
pub struct Machine<B: Database> {
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,
Expand All @@ -161,16 +154,15 @@ pub struct Machine<B: Database> {

parent: Option<Box<Self>>,

#[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<B: Database> Machine<B> {
#[cfg(not(feature = "library"))]
pub fn serialize_into(&self, buffer: &mut [u8]) -> Result<usize> {
let mut cursor = std::io::Cursor::new(buffer);

Expand Down
7 changes: 4 additions & 3 deletions evm_loader/program/src/evm/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,6 @@ impl Drop for Stack {
}
}

#[cfg(not(feature = "library"))]
impl serde::Serialize for Stack {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
Expand All @@ -294,7 +293,6 @@ impl serde::Serialize for Stack {
}
}

#[cfg(not(feature = "library"))]
impl<'de> serde::Deserialize<'de> for Stack {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
Expand All @@ -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());

Expand Down
12 changes: 5 additions & 7 deletions evm_loader/program/src/executor/cache.rs
Original file line number Diff line number Diff line change
@@ -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<u8>,
pub owner: Pubkey,
pub executable: bool,
Expand Down Expand Up @@ -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<Pubkey, OwnedAccountInfo>,
#[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,
}
2 changes: 0 additions & 2 deletions evm_loader/program/src/executor/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<usize> {
let mut cursor = std::io::Cursor::new(buffer);

Expand All @@ -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<Self> {
let (cache, actions, stack, exit_status) = bincode::deserialize(buffer)?;
Ok(Self {
Expand Down
16 changes: 5 additions & 11 deletions evm_loader/program/src/state_account.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -91,7 +94,6 @@ impl<'a> State<'a> {
Ok(storage)
}

#[cfg(not(feature = "library"))]
pub fn restore(
program_id: &Pubkey,
info: &'a AccountInfo<'a>,
Expand Down Expand Up @@ -126,7 +128,6 @@ impl<'a> State<'a> {
Ok((storage, blocked_accounts))
}

#[cfg(not(feature = "library"))]
pub fn finalize(self, deposit: Deposit<'a>) -> Result<FinalizedState<'a>, ProgramError> {
debug_print!("Finalize Storage {}", self.info.key);

Expand All @@ -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
Expand Down Expand Up @@ -222,7 +222,6 @@ impl<'a> State<'a> {
Ok(())
}

#[cfg(not(feature = "library"))]
pub fn update_blocked_accounts<I>(&mut self, accounts: I) -> Result<(), Error>
where
I: ExactSizeIterator<Item = BlockedAccountMeta>,
Expand Down Expand Up @@ -250,7 +249,6 @@ impl<'a> State<'a> {
Ok(())
}

#[cfg(not(feature = "library"))]
fn check_blocked_accounts(
&self,
program_id: &Pubkey,
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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)
Expand Down

0 comments on commit f241842

Please sign in to comment.