Skip to content

Commit

Permalink
NDEV-2651 Remove syscall_stabs (#279)
Browse files Browse the repository at this point in the history
* NDEV-2651 Remove syscall_stabs
---------

Co-authored-by: Semen Medvedev <[email protected]>
  • Loading branch information
s-medvedev and Semen Medvedev authored Feb 20, 2024
1 parent b730019 commit ce759a5
Show file tree
Hide file tree
Showing 49 changed files with 247 additions and 213 deletions.
42 changes: 31 additions & 11 deletions evm_loader/lib/src/account_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use evm_loader::account_storage::find_slot_hash;
use evm_loader::types::Address;
use solana_sdk::rent::Rent;
use solana_sdk::system_program;
use solana_sdk::sysvar::{slot_hashes, Sysvar};
use solana_sdk::sysvar::slot_hashes;
use std::collections::HashSet;
use std::{cell::RefCell, collections::HashMap, convert::TryInto, rc::Rc};

Expand Down Expand Up @@ -51,6 +51,7 @@ pub struct EmulatorAccountStorage<'rpc, T: Rpc> {
chains: Vec<ChainInfo>,
block_number: u64,
block_timestamp: i64,
rent: Rent,
state_overrides: Option<AccountOverrides>,
}

Expand Down Expand Up @@ -79,6 +80,14 @@ impl<'rpc, T: Rpc + BuildConfigSimulator> EmulatorAccountStorage<'rpc, T> {
Some(chains) => chains,
};

let rent_account = rpc
.get_account(&solana_sdk::sysvar::rent::id())
.await?
.value
.ok_or(NeonError::AccountNotFound(solana_sdk::sysvar::rent::id()))?;
let rent = bincode::deserialize::<Rent>(&rent_account.data)?;
info!("Rent: {rent:?}");

Ok(Self {
accounts: RefCell::new(HashMap::new()),
program_id,
Expand All @@ -88,6 +97,7 @@ impl<'rpc, T: Rpc + BuildConfigSimulator> EmulatorAccountStorage<'rpc, T> {
block_number,
block_timestamp,
state_overrides,
rent,
})
}

Expand Down Expand Up @@ -206,8 +216,6 @@ impl<T: Rpc> EmulatorAccountStorage<'_, T> {
pub async fn apply_actions(&mut self, actions: Vec<Action>) -> Result<(), NeonError> {
info!("apply_actions");

let rent = Rent::get()?;

let mut new_balance_accounts = HashSet::new();

for action in actions {
Expand Down Expand Up @@ -255,12 +263,13 @@ impl<T: Rpc> EmulatorAccountStorage<'_, T> {
let empty_size = StorageCell::required_account_size(0);

let gas = if account.is_none() {
rent.minimum_balance(cell_size)
self.rent.minimum_balance(cell_size)
} else {
let existing_value = self.storage(address, index).await;
if existing_value == [0_u8; 32] {
rent.minimum_balance(cell_size)
.saturating_sub(rent.minimum_balance(empty_size))
self.rent
.minimum_balance(cell_size)
.saturating_sub(self.rent.minimum_balance(empty_size))
} else {
0
}
Expand All @@ -287,7 +296,7 @@ impl<T: Rpc> EmulatorAccountStorage<'_, T> {
self.use_contract_account(address, true).await?;

let space = ContractAccount::required_account_size(&code);
self.gas = self.gas.saturating_add(rent.minimum_balance(space));
self.gas = self.gas.saturating_add(self.rent.minimum_balance(space));
}
Action::EvmSelfDestruct { address } => {
info!("selfdestruct {address}");
Expand All @@ -313,7 +322,8 @@ impl<T: Rpc> EmulatorAccountStorage<'_, T> {
}

self.gas = self.gas.saturating_add(
rent.minimum_balance(BalanceAccount::required_account_size())
self.rent
.minimum_balance(BalanceAccount::required_account_size())
.saturating_mul(new_balance_accounts.len() as u64),
);

Expand All @@ -324,8 +334,6 @@ impl<T: Rpc> EmulatorAccountStorage<'_, T> {
let mut accounts = self.accounts.borrow_mut();
let mut additional_balances = Vec::new();

let rent = Rent::get()?;

for (key, account) in accounts.iter_mut() {
let Some(account_data) = account.data.as_mut() else {
continue;
Expand Down Expand Up @@ -354,7 +362,9 @@ impl<T: Rpc> EmulatorAccountStorage<'_, T> {

if (legacy_data.code_size > 0) || (legacy_data.generation > 0) {
// This is a contract, we need additional gas for conversion
let lamports = rent.minimum_balance(BalanceAccount::required_account_size());
let lamports = self
.rent
.minimum_balance(BalanceAccount::required_account_size());
self.gas = self.gas.saturating_add(lamports);
}
}
Expand Down Expand Up @@ -526,6 +536,16 @@ impl<T: Rpc> AccountStorage for EmulatorAccountStorage<'_, T> {
self.block_timestamp.try_into().unwrap()
}

fn rent(&self) -> &Rent {
&self.rent
}

fn return_data(&self) -> Option<(Pubkey, Vec<u8>)> {
info!("return_data");
// TODO: implement return_data() method with SyncedAccountStorage implementation
unimplemented!();
}

async fn block_hash(&self, slot: u64) -> [u8; 32] {
info!("block_hash {slot}");

Expand Down
2 changes: 0 additions & 2 deletions evm_loader/lib/src/commands/emulate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use solana_sdk::pubkey::Pubkey;

use crate::commands::get_config::BuildConfigSimulator;
use crate::rpc::Rpc;
use crate::syscall_stubs::setup_emulator_syscall_stubs;
use crate::tracing::tracers::Tracer;
use crate::types::{EmulateRequest, TxParams};
use crate::{
Expand Down Expand Up @@ -78,7 +77,6 @@ pub async fn execute<T: Tracer>(

let step_limit = emulate_request.step_limit.unwrap_or(100000);

setup_emulator_syscall_stubs(rpc).await?;
emulate_trx(emulate_request.tx, &mut storage, step_limit, tracer).await
}

Expand Down
2 changes: 1 addition & 1 deletion evm_loader/lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub mod commands;
pub mod config;
pub mod errors;
pub mod rpc;
pub mod syscall_stubs;

pub mod tracing;
pub mod types;

Expand Down
60 changes: 0 additions & 60 deletions evm_loader/lib/src/syscall_stubs.rs

This file was deleted.

4 changes: 3 additions & 1 deletion evm_loader/program/src/account/ether_balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
types::Address,
};
use ethnum::U256;
use solana_program::{account_info::AccountInfo, pubkey::Pubkey, system_program};
use solana_program::{account_info::AccountInfo, pubkey::Pubkey, rent::Rent, system_program};

use super::{AccountsDB, ACCOUNT_PREFIX_LEN, ACCOUNT_SEED_VERSION, TAG_ACCOUNT_BALANCE};

Expand Down Expand Up @@ -46,6 +46,7 @@ impl<'a> BalanceAccount<'a> {
chain_id: u64,
accounts: &AccountsDB<'a>,
keys: Option<&KeysCache>,
rent: &Rent,
) -> Result<Self> {
let (pubkey, bump_seed) = keys.map_or_else(
|| address.find_balance_address(&crate::ID, chain_id),
Expand Down Expand Up @@ -93,6 +94,7 @@ impl<'a> BalanceAccount<'a> {
&account,
program_seeds,
ACCOUNT_PREFIX_LEN + size_of::<Header>(),
rent,
)?;

super::set_tag(&crate::ID, &account, TAG_ACCOUNT_BALANCE)?;
Expand Down
2 changes: 1 addition & 1 deletion evm_loader/program/src/account/ether_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl<'a> ContractAccount<'a> {
if system_program::check_id(info.owner) {
let seeds: &[&[u8]] = &[&[ACCOUNT_SEED_VERSION], address.as_bytes(), &[bump_seed]];
let space = required_size.min(MAX_PERMITTED_DATA_INCREASE);
system.create_pda_account(&crate::ID, operator, info, seeds, space)?;
system.create_pda_account(&crate::ID, operator, info, seeds, space, rent)?;
} else if crate::check_id(info.owner) {
super::validate_tag(&crate::ID, info, TAG_EMPTY)?;

Expand Down
4 changes: 3 additions & 1 deletion evm_loader/program/src/account/ether_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ impl<'a> StorageCell<'a> {
allocate_cells: usize,
accounts: &AccountsDB<'a>,
signer_seeds: &[&[u8]],
rent: &Rent,
) -> Result<Self> {
let base_account = accounts.get(&address.base);
let cell_account = accounts.get(&address.pubkey);
Expand All @@ -114,6 +115,7 @@ impl<'a> StorageCell<'a> {
cell_account,
address.seed(),
space,
rent,
)?;

super::set_tag(&crate::ID, cell_account, TAG_STORAGE_CELL)?;
Expand Down Expand Up @@ -200,7 +202,7 @@ impl<'a> StorageCell<'a> {
Ok(())
}

pub fn sync_lamports(&mut self, rent: Rent, accounts: &AccountsDB<'a>) -> Result<()> {
pub fn sync_lamports(&mut self, rent: &Rent, accounts: &AccountsDB<'a>) -> Result<()> {
let original_data_len = unsafe { self.account.original_data_len() };
if original_data_len == self.account.data_len() {
return Ok(());
Expand Down
16 changes: 10 additions & 6 deletions evm_loader/program/src/account/legacy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,12 @@ pub const TAG_HOLDER_DEPRECATED: u8 = 51;
pub const TAG_ACCOUNT_CONTRACT_DEPRECATED: u8 = 12;
pub const TAG_STORAGE_CELL_DEPRECATED: u8 = 42;

fn reduce_account_size(account: &AccountInfo, required_len: usize) -> Result<u64> {
fn reduce_account_size(account: &AccountInfo, required_len: usize, rent: &Rent) -> Result<u64> {
assert!(account.data_len() > required_len);

account.realloc(required_len, false)?;

// Return excessive lamports to the operator
let rent = Rent::get()?;
let minimum_balance = rent.minimum_balance(account.data_len());
if account.lamports() > minimum_balance {
let value = account.lamports() - minimum_balance;
Expand All @@ -59,6 +58,7 @@ fn update_ether_account(
legacy_data: &LegacyEtherData,
db: &AccountsDB,
keys: &KeysCache,
rent: &Rent,
) -> Result<u64> {
let pubkey = keys.contract(&crate::ID, legacy_data.address);
let account = db.get(&pubkey);
Expand All @@ -75,7 +75,7 @@ fn update_ether_account(

// Make account smaller
let required_len = ContractAccount::required_account_size(&code);
lamports_collected += reduce_account_size(account, required_len)?;
lamports_collected += reduce_account_size(account, required_len, rent)?;

// Fill it with new data
account.try_borrow_mut_data()?.fill(0);
Expand Down Expand Up @@ -103,6 +103,7 @@ fn update_ether_account(
crate::config::DEFAULT_CHAIN_ID,
db,
Some(keys),
rent,
)?;
balance.mint(legacy_data.balance)?;
balance.increment_nonce_by(legacy_data.trx_count)?;
Expand All @@ -117,6 +118,7 @@ fn update_storage_account(
legacy_data: &LegacyStorageData,
db: &AccountsDB,
keys: &KeysCache,
rent: &Rent,
) -> Result<u64> {
let mut lamports_collected = 0_u64;

Expand All @@ -137,7 +139,7 @@ fn update_storage_account(

// Make account smaller
let required_len = StorageCell::required_account_size(cells.len());
lamports_collected += reduce_account_size(&cell_account, required_len)?;
lamports_collected += reduce_account_size(&cell_account, required_len, rent)?;

// Fill it with new data
cell_account.try_borrow_mut_data()?.fill(0);
Expand Down Expand Up @@ -187,6 +189,8 @@ pub fn update_holder_account(account: &AccountInfo) -> Result<u8> {
pub fn update_legacy_accounts(accounts: &AccountsDB) -> Result<u64> {
let keys = KeysCache::new();

let rent = Rent::get()?;

let mut lamports_collected = 0_u64;
let mut legacy_storage = Vec::with_capacity(accounts.accounts_len());

Expand All @@ -203,7 +207,7 @@ pub fn update_legacy_accounts(accounts: &AccountsDB) -> Result<u64> {
match tag {
LegacyEtherData::TAG => {
let legacy_data = LegacyEtherData::from_account(&crate::ID, account)?;
lamports_collected += update_ether_account(&legacy_data, accounts, &keys)?;
lamports_collected += update_ether_account(&legacy_data, accounts, &keys, &rent)?;
}
LegacyStorageData::TAG => {
let legacy_data = LegacyStorageData::from_account(&crate::ID, account)?;
Expand All @@ -214,7 +218,7 @@ pub fn update_legacy_accounts(accounts: &AccountsDB) -> Result<u64> {
}

for data in legacy_storage {
lamports_collected += update_storage_account(&data, accounts, &keys)?;
lamports_collected += update_storage_account(&data, accounts, &keys, &rent)?;
}

Ok(lamports_collected)
Expand Down
7 changes: 4 additions & 3 deletions evm_loader/program/src/account/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use solana_program::account_info::AccountInfo;
use solana_program::program::{invoke_signed_unchecked, invoke_unchecked};
use solana_program::program_error::ProgramError;
use solana_program::pubkey::Pubkey;
use solana_program::{rent::Rent, system_instruction, sysvar::Sysvar};
use solana_program::{rent::Rent, system_instruction};
use std::convert::From;
use std::ops::Deref;

Expand Down Expand Up @@ -31,8 +31,8 @@ impl<'a> System<'a> {
new_account: &AccountInfo<'a>,
new_account_seeds: &[&[u8]],
space: usize,
rent: &Rent,
) -> Result<(), ProgramError> {
let rent = Rent::get()?;
let minimum_balance = rent.minimum_balance(space).max(1);

if new_account.lamports() > 0 {
Expand Down Expand Up @@ -81,8 +81,9 @@ impl<'a> System<'a> {
new_account: &AccountInfo<'a>,
seed: &str,
space: usize,
rent: &Rent,
) -> Result<(), ProgramError> {
let minimum_balance = Rent::get()?.minimum_balance(space).max(1);
let minimum_balance = rent.minimum_balance(space).max(1);

if new_account.lamports() > 0 {
let required_lamports = minimum_balance.saturating_sub(new_account.lamports());
Expand Down
Loading

0 comments on commit ce759a5

Please sign in to comment.