Skip to content

Commit

Permalink
Remove RefCell from EmulatorAccountStorage::solana_accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
andreisilviudragnea committed Oct 29, 2023
1 parent 0810f2a commit 3ff6ddd
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 36 deletions.
26 changes: 11 additions & 15 deletions evm_loader/lib/src/account_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ pub struct SolanaAccount {
#[allow(clippy::module_name_repetitions)]
pub struct EmulatorAccountStorage<'a> {
pub accounts: HashMap<Address, NeonAccount>,
pub solana_accounts: RefCell<HashMap<Pubkey, SolanaAccount>>,
pub solana_accounts: HashMap<Pubkey, SolanaAccount>,
rpc_client: &'a dyn Rpc,
evm_loader: Pubkey,
block_number: u64,
Expand Down Expand Up @@ -213,7 +213,7 @@ impl<'a> EmulatorAccountStorage<'a> {

Ok(Self {
accounts: HashMap::new(),
solana_accounts: RefCell::new(HashMap::new()),
solana_accounts: HashMap::new(),
rpc_client,
evm_loader,
block_number,
Expand Down Expand Up @@ -280,9 +280,8 @@ impl<'a> EmulatorAccountStorage<'a> {
}

let entries = accounts.iter().skip(addresses.len()).zip(solana_accounts);
let mut solana_accounts_storage = self.solana_accounts.borrow_mut();
for (account, &pubkey) in entries {
solana_accounts_storage.insert(
self.solana_accounts.insert(
pubkey,
SolanaAccount {
pubkey: pubkey.into(),
Expand All @@ -294,8 +293,8 @@ impl<'a> EmulatorAccountStorage<'a> {
}
}

async fn get_account(&self, pubkey: &Pubkey) -> client_error::Result<Option<Account>> {
if let Some(account) = self.solana_accounts.borrow().get(pubkey) {
async fn get_account(&mut self, pubkey: &Pubkey) -> client_error::Result<Option<Account>> {
if let Some(account) = self.solana_accounts.get(pubkey) {
if let Some(ref data) = account.data {
return Ok(Some(data.clone()));
}
Expand All @@ -307,7 +306,6 @@ impl<'a> EmulatorAccountStorage<'a> {
.await?;

self.solana_accounts
.borrow_mut()
.entry(*pubkey)
.and_modify(|a| a.data = result.value.clone())
.or_insert(SolanaAccount {
Expand Down Expand Up @@ -357,7 +355,7 @@ impl<'a> EmulatorAccountStorage<'a> {
false
}

async fn add_solana_account(&self, pubkey: Pubkey, is_writable: bool) {
async fn add_solana_account(&mut self, pubkey: Pubkey, is_writable: bool) {
if solana_sdk::system_program::check_id(&pubkey) {
return;
}
Expand All @@ -366,21 +364,19 @@ impl<'a> EmulatorAccountStorage<'a> {
return;
}

let mut solana_accounts = self.solana_accounts.borrow_mut();

let account = SolanaAccount {
pubkey: pubkey.into(),
is_writable,
data: None,
};
if is_writable {
solana_accounts
self.solana_accounts
.entry(pubkey)
// If account is present in cache ensure the data is not lost
.and_modify(|a| a.is_writable = true)
.or_insert(account);
} else {
solana_accounts.entry(pubkey).or_insert(account);
self.solana_accounts.entry(pubkey).or_insert(account);
}
}

Expand Down Expand Up @@ -577,7 +573,7 @@ impl<'a> AccountStorage for EmulatorAccountStorage<'a> {
self.block_timestamp.try_into().unwrap()
}

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

self.add_solana_account(slot_hashes::ID, false).await;
Expand Down Expand Up @@ -752,7 +748,7 @@ impl<'a> AccountStorage for EmulatorAccountStorage<'a> {
self.chain_id
}

async fn clone_solana_account(&self, address: &Pubkey) -> OwnedAccountInfo {
async fn clone_solana_account(&mut self, address: &Pubkey) -> OwnedAccountInfo {
info!("clone_solana_account {}", address);

if address == &FAKE_OPERATOR {
Expand Down Expand Up @@ -780,7 +776,7 @@ impl<'a> AccountStorage for EmulatorAccountStorage<'a> {
}
}

async fn map_solana_account<F, R>(&self, address: &Pubkey, action: F) -> R
async fn map_solana_account<F, R>(&mut self, address: &Pubkey, action: F) -> R
where
F: FnOnce(&AccountInfo) -> R,
{
Expand Down
2 changes: 1 addition & 1 deletion evm_loader/lib/src/commands/emulate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ pub async fn execute(
emulate_trx(tx_params, chain_id, step_limit, None, &mut backend).await?;

let accounts = storage.accounts.values().cloned().collect();
let solana_accounts = storage.solana_accounts.borrow().values().cloned().collect();
let solana_accounts = storage.solana_accounts.values().cloned().collect();

Ok(EmulationResultWithAccounts {
accounts,
Expand Down
6 changes: 3 additions & 3 deletions evm_loader/lib/src/tracing/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl AccountStorage for TestAccountStorage {
self.block_timestamp
}

async fn block_hash(&self, _number: u64) -> [u8; 32] {
async fn block_hash(&mut self, _number: u64) -> [u8; 32] {
todo!()
}

Expand Down Expand Up @@ -114,11 +114,11 @@ impl AccountStorage for TestAccountStorage {
.to_be_bytes()
}

async fn clone_solana_account(&self, _address: &Pubkey) -> OwnedAccountInfo {
async fn clone_solana_account(&mut self, _address: &Pubkey) -> OwnedAccountInfo {
todo!()
}

async fn map_solana_account<F, R>(&self, _address: &Pubkey, _action: F) -> R
async fn map_solana_account<F, R>(&mut self, _address: &Pubkey, _action: F) -> R
where
F: FnOnce(&AccountInfo) -> R,
{
Expand Down
6 changes: 3 additions & 3 deletions evm_loader/program/src/account_storage/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl<'a> AccountStorage for ProgramAccountStorage<'a> {
.expect("Timestamp is positive")
}

fn block_hash(&self, slot: u64) -> [u8; 32] {
fn block_hash(&mut self, slot: u64) -> [u8; 32] {
let slot_hashes_account = self
.solana_accounts
.get(&slot_hashes::ID)
Expand Down Expand Up @@ -126,12 +126,12 @@ impl<'a> AccountStorage for ProgramAccountStorage<'a> {
.map_or_else(<[u8; 32]>::default, |a| a.get(subindex))
}

fn clone_solana_account(&self, address: &Pubkey) -> OwnedAccountInfo {
fn clone_solana_account(&mut self, address: &Pubkey) -> OwnedAccountInfo {
let info = self.solana_accounts[address];
OwnedAccountInfo::from_account_info(self.program_id, info)
}

fn map_solana_account<F, R>(&self, address: &Pubkey, action: F) -> R
fn map_solana_account<F, R>(&mut self, address: &Pubkey, action: F) -> R
where
F: FnOnce(&AccountInfo) -> R,
{
Expand Down
6 changes: 3 additions & 3 deletions evm_loader/program/src/account_storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub trait AccountStorage {
/// Get block timestamp
fn block_timestamp(&self) -> U256;
/// Get block hash
async fn block_hash(&self, number: u64) -> [u8; 32];
async fn block_hash(&mut self, number: u64) -> [u8; 32];
/// Get chain id
fn chain_id(&self) -> u64;

Expand All @@ -97,10 +97,10 @@ pub trait AccountStorage {
async fn storage(&mut self, address: &Address, index: &U256) -> [u8; 32];

/// Clone existing solana account
async fn clone_solana_account(&self, address: &Pubkey) -> OwnedAccountInfo;
async fn clone_solana_account(&mut self, address: &Pubkey) -> OwnedAccountInfo;

/// Map existing solana account
async fn map_solana_account<F, R>(&self, address: &Pubkey, action: F) -> R
async fn map_solana_account<F, R>(&mut self, address: &Pubkey, action: F) -> R
where
F: FnOnce(&AccountInfo) -> R;

Expand Down
4 changes: 2 additions & 2 deletions evm_loader/program/src/evm/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ pub trait Database {
async fn storage(&mut self, address: &Address, index: &U256) -> Result<[u8; 32]>;
fn set_storage(&mut self, address: Address, index: U256, value: [u8; 32]) -> Result<()>;

async fn block_hash(&self, number: U256) -> Result<[u8; 32]>;
async fn block_hash(&mut self, number: U256) -> Result<[u8; 32]>;
fn block_number(&self) -> Result<U256>;
fn block_timestamp(&self) -> Result<U256>;

async fn map_solana_account<F, R>(&self, address: &Pubkey, action: F) -> R
async fn map_solana_account<F, R>(&mut self, address: &Pubkey, action: F) -> R
where
F: FnOnce(&AccountInfo) -> R;

Expand Down
6 changes: 3 additions & 3 deletions evm_loader/program/src/evm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,15 @@ impl<B: Database> Machine<B> {
}

#[cfg(target_os = "solana")]
pub fn deserialize_from(buffer: &[u8], backend: &B) -> Result<Self> {
fn reinit_buffer<B: Database>(buffer: &mut Buffer, backend: &B) {
pub fn deserialize_from(buffer: &[u8], backend: &mut B) -> Result<Self> {
fn reinit_buffer<B: Database>(buffer: &mut Buffer, backend: &mut B) {
if let Some((key, range)) = buffer.uninit_data() {
*buffer =
backend.map_solana_account(&key, |i| unsafe { Buffer::from_account(i, range) });
}
}

fn reinit_machine<B: Database>(mut machine: &mut Machine<B>, backend: &B) {
fn reinit_machine<B: Database>(mut machine: &mut Machine<B>, backend: &mut B) {
loop {
reinit_buffer(&mut machine.call_data, backend);
reinit_buffer(&mut machine.execution_code, backend);
Expand Down
8 changes: 4 additions & 4 deletions evm_loader/program/src/executor/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl<'a, B: AccountStorage> ExecutorState<'a, B> {
}

#[maybe_async]
pub async fn external_account(&self, address: Pubkey) -> Result<OwnedAccountInfo> {
pub async fn external_account(&mut self, address: Pubkey) -> Result<OwnedAccountInfo> {
let metas = self
.actions
.iter()
Expand Down Expand Up @@ -221,7 +221,7 @@ impl<'a, B: AccountStorage> ExecutorState<'a, B> {
async fn insert_account_if_not_present<B: AccountStorage>(
cache: &RefCell<Cache>,
key: Pubkey,
backend: &B,
backend: &mut B,
) {
if !cache.borrow().solana_accounts.contains_key(&key) {
let owned_account_info = backend.clone_solana_account(&key).await;
Expand Down Expand Up @@ -415,7 +415,7 @@ impl<'a, B: AccountStorage> Database for ExecutorState<'a, B> {
Ok(())
}

async fn block_hash(&self, number: U256) -> Result<[u8; 32]> {
async fn block_hash(&mut self, number: U256) -> Result<[u8; 32]> {
// geth:
// - checks the overflow
// - converts to u64
Expand Down Expand Up @@ -450,7 +450,7 @@ impl<'a, B: AccountStorage> Database for ExecutorState<'a, B> {
Ok(cache.block_timestamp)
}

async fn map_solana_account<F, R>(&self, address: &Pubkey, action: F) -> R
async fn map_solana_account<F, R>(&mut self, address: &Pubkey, action: F) -> R
where
F: FnOnce(&solana_program::account_info::AccountInfo) -> R,
{
Expand Down
4 changes: 2 additions & 2 deletions evm_loader/program/src/instruction/transaction_step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,10 @@ fn deserialize_evm_state<'a, 'r>(
let buffer = state.evm_data();

let executor_state_data = &buffer[..state.evm_state_len];
let backend = ExecutorState::deserialize_from(executor_state_data, account_storage)?;
let mut backend = ExecutorState::deserialize_from(executor_state_data, account_storage)?;

let evm_data = &buffer[state.evm_state_len..][..state.evm_machine_len];
let evm = Machine::deserialize_from(evm_data, &backend)?;
let evm = Machine::deserialize_from(evm_data, &mut backend)?;

Ok((backend, evm))
}

0 comments on commit 3ff6ddd

Please sign in to comment.