Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 25 additions & 37 deletions evm_loader/lib/src/account_storage.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use async_trait::async_trait;
use evm_loader::account::legacy::{
LegacyEtherData, LegacyStorageData, ACCOUNT_PREFIX_LEN_BEFORE_REVISION,
TAG_ACCOUNT_BALANCE_BEFORE_REVISION, TAG_ACCOUNT_CONTRACT_BEFORE_REVISION,
TAG_ACCOUNT_CONTRACT_DEPRECATED, TAG_STORAGE_CELL_BEFORE_REVISION, TAG_STORAGE_CELL_DEPRECATED,
LegacyEtherData, LegacyStorageData, TAG_ACCOUNT_CONTRACT_DEPRECATED,
TAG_STORAGE_CELL_DEPRECATED,
};
use evm_loader::account::{ACCOUNT_PREFIX_LEN, TAG_ACCOUNT_CONTRACT, TAG_STORAGE_CELL};
use evm_loader::account::{TAG_ACCOUNT_CONTRACT, TAG_STORAGE_CELL};
use evm_loader::account_storage::find_slot_hash;
use evm_loader::types::Address;
use solana_sdk::rent::Rent;
Expand All @@ -13,7 +12,6 @@ use solana_sdk::sysvar::slot_hashes;
use std::collections::HashSet;
use std::{cell::RefCell, collections::HashMap, convert::TryInto, rc::Rc};

use crate::account_update::update_account;
use crate::NeonResult;
use crate::{rpc::Rpc, NeonError};
use ethnum::U256;
Expand Down Expand Up @@ -177,11 +175,7 @@ impl<T: Rpc> EmulatorAccountStorage<'_, T> {
is_writable: bool,
) -> NeonResult<(Pubkey, Option<Account>, Option<Account>)> {
let (pubkey, _) = address.find_balance_address(self.program_id(), chain_id);
let mut account = self.use_account(pubkey, is_writable).await?;

if let Some(account) = &mut account {
update_account(&self.program_id, &pubkey, account)?;
}
let account = self.use_account(pubkey, is_writable).await?;

let legacy_account = if account.is_none() && (chain_id == self.default_chain_id()) {
let (legacy_pubkey, _) = address.find_solana_address(self.program_id());
Expand All @@ -199,11 +193,7 @@ impl<T: Rpc> EmulatorAccountStorage<'_, T> {
is_writable: bool,
) -> NeonResult<(Pubkey, Option<Account>)> {
let (pubkey, _) = address.find_solana_address(self.program_id());
let mut account = self.use_account(pubkey, is_writable).await?;

if let Some(account) = &mut account {
update_account(&self.program_id, &pubkey, account)?;
}
let account = self.use_account(pubkey, is_writable).await?;

Ok((pubkey, account))
}
Expand All @@ -217,14 +207,10 @@ impl<T: Rpc> EmulatorAccountStorage<'_, T> {
let (base, _) = address.find_solana_address(self.program_id());
let cell_address = StorageCellAddress::new(self.program_id(), &base, &index);

let mut account = self
let account = self
.use_account(*cell_address.pubkey(), is_writable)
.await?;

if let Some(account) = &mut account {
update_account(&self.program_id, cell_address.pubkey(), account)?;
}

Ok((*cell_address.pubkey(), account))
}

Expand Down Expand Up @@ -384,26 +370,28 @@ impl<T: Rpc> EmulatorAccountStorage<'_, T> {
}
}

if matches!(
tag,
TAG_ACCOUNT_BALANCE_BEFORE_REVISION
| TAG_ACCOUNT_CONTRACT_BEFORE_REVISION
| TAG_STORAGE_CELL_BEFORE_REVISION
) {
const PREFIX_BEFORE: usize = ACCOUNT_PREFIX_LEN_BEFORE_REVISION;
const PREFIX_AFTER: usize = ACCOUNT_PREFIX_LEN;
const EXPANSION_LEN: usize = PREFIX_AFTER - PREFIX_BEFORE;
if !account.is_writable {
continue;
}

account.is_writable = true;
account.is_legacy = true;
let required_header_realloc = match tag {
TAG_ACCOUNT_CONTRACT => {
let contract = ContractAccount::from_account(self.program_id(), info)?;
contract.required_header_realloc()
}
TAG_STORAGE_CELL => {
let cell = StorageCell::from_account(self.program_id(), info)?;
cell.required_header_realloc()
}
_ => 0,
};

let lamports = self
.rent
.minimum_balance(EXPANSION_LEN)
.saturating_sub(self.rent.minimum_balance(0));
let header_realloc_lamports = self
.rent
.minimum_balance(required_header_realloc)
.saturating_sub(self.rent.minimum_balance(0));

self.gas = self.gas.saturating_add(lamports);
}
self.gas = self.gas.saturating_add(header_realloc_lamports);
}

for a in additional_balances {
Expand Down
48 changes: 0 additions & 48 deletions evm_loader/lib/src/account_update.rs

This file was deleted.

7 changes: 1 addition & 6 deletions evm_loader/lib/src/commands/get_balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ use evm_loader::account::BalanceAccount;
use serde::{Deserialize, Serialize};
use solana_sdk::{account::Account, pubkey::Pubkey};

use crate::{
account_storage::account_info, account_update::update_account, rpc::Rpc, types::BalanceAddress,
NeonResult,
};
use crate::{account_storage::account_info, rpc::Rpc, types::BalanceAddress, NeonResult};

use serde_with::{serde_as, DisplayFromStr};

Expand Down Expand Up @@ -51,8 +48,6 @@ fn read_account(
) -> NeonResult<GetBalanceResponse> {
let solana_address = address.find_pubkey(program_id);

update_account(program_id, &solana_address, &mut account)?;

let account_info = account_info(&solana_address, &mut account);
let balance_account = BalanceAccount::from_account(program_id, account_info)?;

Expand Down
4 changes: 1 addition & 3 deletions evm_loader/lib/src/commands/get_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use evm_loader::{
use serde::{Deserialize, Serialize};
use solana_sdk::{account::Account, pubkey::Pubkey};

use crate::{account_storage::account_info, account_update::update_account, rpc::Rpc, NeonResult};
use crate::{account_storage::account_info, rpc::Rpc, NeonResult};

use serde_with::{hex::Hex, serde_as, DisplayFromStr};

Expand Down Expand Up @@ -51,8 +51,6 @@ fn read_account(
return Ok(GetContractResponse::empty(solana_address));
};

update_account(program_id, &solana_address, &mut account)?;

let account_info = account_info(&solana_address, &mut account);
let (chain_id, code) =
if let Ok(contract) = ContractAccount::from_account(program_id, account_info.clone()) {
Expand Down
1 change: 0 additions & 1 deletion evm_loader/lib/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pub mod account_storage;
pub mod account_update;
pub mod build_info;
pub mod build_info_common;
pub mod commands;
Expand Down
51 changes: 22 additions & 29 deletions evm_loader/program/src/account/ether_balance.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use std::{
cell::{Ref, RefMut},
mem::size_of,
};
use std::mem::size_of;

use crate::{
account::{TAG_ACCOUNT_CONTRACT, TAG_EMPTY},
Expand All @@ -13,7 +10,9 @@ use crate::{
use ethnum::U256;
use solana_program::{account_info::AccountInfo, pubkey::Pubkey, rent::Rent, system_program};

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

#[repr(C, packed)]
pub struct Header {
Expand All @@ -22,13 +21,14 @@ pub struct Header {
pub trx_count: u64,
pub balance: U256,
}
impl AccountHeader for Header {
const VERSION: u8 = 0;
}

pub struct BalanceAccount<'a> {
account: AccountInfo<'a>,
}

const HEADER_OFFSET: usize = ACCOUNT_PREFIX_LEN;

impl<'a> BalanceAccount<'a> {
#[must_use]
pub fn required_account_size() -> usize {
Expand Down Expand Up @@ -97,52 +97,44 @@ impl<'a> BalanceAccount<'a> {
rent,
)?;

super::set_tag(&crate::ID, &account, TAG_ACCOUNT_BALANCE)?;
let mut balance_account = Self { account };
super::set_tag(&crate::ID, &account, TAG_ACCOUNT_BALANCE, Header::VERSION)?;
{
let mut header = balance_account.header_mut();
let mut header = super::header_mut::<Header>(&account);
header.address = address;
header.chain_id = chain_id;
header.trx_count = 0;
header.balance = U256::ZERO;
}

Ok(balance_account)
Ok(Self { account })
}

#[must_use]
pub fn pubkey(&self) -> &'a Pubkey {
self.account.key
}

#[inline]
fn header(&self) -> Ref<Header> {
super::section(&self.account, HEADER_OFFSET)
}

#[inline]
fn header_mut(&mut self) -> RefMut<Header> {
super::section_mut(&self.account, HEADER_OFFSET)
}

#[must_use]
pub fn address(&self) -> Address {
self.header().address
let header = super::header::<Header>(&self.account);
header.address
}

#[must_use]
pub fn chain_id(&self) -> u64 {
self.header().chain_id
let header = super::header::<Header>(&self.account);
header.chain_id
}

#[must_use]
pub fn nonce(&self) -> u64 {
self.header().trx_count
let header = super::header::<Header>(&self.account);
header.trx_count
}

#[must_use]
pub fn exists(&self) -> bool {
let header = self.header();
let header = super::header::<Header>(&self.account);

({ header.trx_count } > 0) || ({ header.balance } > 0)
}
Expand All @@ -152,7 +144,7 @@ impl<'a> BalanceAccount<'a> {
}

pub fn increment_nonce_by(&mut self, value: u64) -> Result<()> {
let mut header = self.header_mut();
let mut header = super::header_mut::<Header>(&self.account);

header.trx_count = header
.trx_count
Expand All @@ -164,7 +156,8 @@ impl<'a> BalanceAccount<'a> {

#[must_use]
pub fn balance(&self) -> U256 {
self.header().balance
let header = super::header::<Header>(&self.account);
header.balance
}

pub fn transfer(&mut self, target: &mut BalanceAccount, value: U256) -> Result<()> {
Expand All @@ -179,7 +172,7 @@ impl<'a> BalanceAccount<'a> {
}

pub fn burn(&mut self, value: U256) -> Result<()> {
let mut header = self.header_mut();
let mut header = super::header_mut::<Header>(&self.account);

header.balance = header
.balance
Expand All @@ -194,7 +187,7 @@ impl<'a> BalanceAccount<'a> {
}

pub fn mint(&mut self, value: U256) -> Result<()> {
let mut header = self.header_mut();
let mut header = super::header_mut::<Header>(&self.account);

header.balance = header
.balance
Expand Down
Loading