Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(levm): main merge #1348

Closed
wants to merge 4 commits into from
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
2 changes: 2 additions & 0 deletions crates/l2/proposer/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ pub enum CommitterError {
FailedToParseLastCommittedBlock(#[from] FromStrRadixErr),
#[error("Committer failed retrieve block from storage: {0}")]
FailedToRetrieveBlockFromStorage(#[from] StoreError),
#[error("Committer failed retrieve data from storage")]
FailedToRetrieveDataFromStorage,
#[error("Committer failed to generate blobs bundle: {0}")]
FailedToGenerateBlobsBundle(#[from] BlobsBundleError),
#[error("Committer failed to get information from storage")]
Expand Down
30 changes: 24 additions & 6 deletions crates/l2/proposer/l1_committer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use ethrex_core::{
},
Address, H256, U256,
};
use ethrex_storage::Store;
use ethrex_storage::{error::StoreError, Store};
use ethrex_vm::{evm_state, execute_block, get_state_transitions};
use keccak_hash::keccak;
use secp256k1::SecretKey;
Expand Down Expand Up @@ -132,7 +132,7 @@ impl Committer {
deposits,
)?;

let blobs_bundle = self.generate_blobs_bundle(state_diff.clone())?;
let blobs_bundle = self.generate_blobs_bundle(&state_diff)?;

let head_block_hash = block_to_commit.hash();
match self
Expand Down Expand Up @@ -242,18 +242,36 @@ impl Committer {
let account_updates = get_state_transitions(&mut state);

let mut modified_accounts = HashMap::new();
account_updates.iter().for_each(|account_update| {
for account_update in &account_updates {
let prev_nonce = match state
.database()
.ok_or(CommitterError::FailedToRetrieveDataFromStorage)?
// If we want the state_diff of a batch, we will have to change the -1 with the `batch_size`
// and we may have to keep track of the latestCommittedBlock (last block of the batch),
// the batch_size and the latestCommittedBatch in the contract.
.get_account_info(block.header.number - 1, account_update.address)
.map_err(StoreError::from)?
{
Some(acc) => acc.nonce,
None => 0,
};

modified_accounts.insert(
account_update.address,
AccountStateDiff {
new_balance: account_update.info.clone().map(|info| info.balance),
nonce_diff: account_update.info.clone().map(|info| info.nonce as u16),
nonce_diff: (account_update
.info
.clone()
.ok_or(CommitterError::FailedToRetrieveDataFromStorage)?
.nonce
- prev_nonce) as u16,
storage: account_update.added_storage.clone().into_iter().collect(),
bytecode: account_update.code.clone(),
bytecode_hash: None,
},
);
});
}

let state_diff = StateDiff {
modified_accounts,
Expand Down Expand Up @@ -287,7 +305,7 @@ impl Committer {
/// Generate the blob bundle necessary for the EIP-4844 transaction.
pub fn generate_blobs_bundle(
&self,
state_diff: StateDiff,
state_diff: &StateDiff,
) -> Result<BlobsBundle, CommitterError> {
let blob_data = state_diff.encode().map_err(CommitterError::from)?;

Expand Down
6 changes: 3 additions & 3 deletions crates/l2/proposer/state_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use super::errors::StateDiffError;
#[derive(Clone)]
pub struct AccountStateDiff {
pub new_balance: Option<U256>,
pub nonce_diff: Option<u16>,
pub nonce_diff: u16,
pub storage: Vec<(H256, U256)>,
pub bytecode: Option<Bytes>,
pub bytecode_hash: Option<H256>,
Expand Down Expand Up @@ -125,9 +125,9 @@ impl AccountStateDiff {
encoded.extend_from_slice(buf);
}

if let Some(nonce_diff) = self.nonce_diff {
if self.nonce_diff != 0 {
r#type += AccountStateDiffType::NonceDiff as u8;
encoded.extend(nonce_diff.to_be_bytes());
encoded.extend(self.nonce_diff.to_be_bytes());
}

if !self.storage.is_empty() {
Expand Down
164 changes: 3 additions & 161 deletions crates/vm/levm/src/gas_cost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,167 +374,9 @@ pub fn mcopy(
.checked_add(memory_expansion_cost)
.ok_or(OutOfGasError::GasCostOverflow)
}
/*
#[allow(clippy::too_many_arguments)]
pub fn call(
current_call_frame: &mut CallFrame,
args_size: usize,
args_offset: usize,
ret_size: usize,
ret_offset: usize,
value: U256,
is_cached: bool,
account_is_empty: bool,
) -> Result<U256, OutOfGasError> {
let arg_sum = args_offset
.checked_add(args_size)
.ok_or(OutOfGasError::GasCostOverflow)?;
let ret_sum = ret_offset
.checked_add(ret_size)
.ok_or(OutOfGasError::GasCostOverflow)?;

let memory_expansion_cost = if arg_sum > ret_sum {
current_call_frame
.memory
.expansion_cost(args_offset, args_size)?
} else {
current_call_frame
.memory
.expansion_cost(ret_offset, ret_size)?
};

let positive_value_cost = if !value.is_zero() {
NON_ZERO_VALUE_COST
.checked_add(BASIC_FALLBACK_FUNCTION_STIPEND)
.ok_or(OutOfGasError::GasCostOverflow)?
} else {
U256::zero()
};

let address_access_cost = if !is_cached {
COLD_ADDRESS_ACCESS_COST
} else {
WARM_ADDRESS_ACCESS_COST
};

let value_to_empty_account_cost = if !value.is_zero() && account_is_empty {
VALUE_TO_EMPTY_ACCOUNT_COST
} else {
U256::zero()
};

memory_expansion_cost
.checked_add(address_access_cost)
.ok_or(OutOfGasError::GasCostOverflow)?
.checked_add(positive_value_cost)
.ok_or(OutOfGasError::GasCostOverflow)?
.checked_add(value_to_empty_account_cost)
.ok_or(OutOfGasError::GasCostOverflow)
}

pub fn callcode(
current_call_frame: &mut CallFrame,
args_size: usize,
args_offset: usize,
ret_size: usize,
ret_offset: usize,
value: U256,
is_cached: bool,
) -> Result<U256, OutOfGasError> {
let transfer_cost = if value == U256::zero() {
U256::zero()
} else {
NON_ZERO_VALUE_COST
// Should also add BASIC_FALLBACK_FUNCTION_STIPEND??
// See https://www.evm.codes/?fork=cancun#f2 and call impl
};

compute_gas_call(
current_call_frame,
args_size,
args_offset,
ret_size,
ret_offset,
is_cached,
)?
.checked_add(transfer_cost)
.ok_or(OutOfGasError::GasCostOverflow)
}

pub fn delegatecall(
current_call_frame: &mut CallFrame,
args_size: usize,
args_offset: usize,
ret_size: usize,
ret_offset: usize,
is_cached: bool,
) -> Result<U256, OutOfGasError> {
compute_gas_call(
current_call_frame,
args_size,
args_offset,
ret_size,
ret_offset,
is_cached,
)
}

pub fn staticcall(
current_call_frame: &mut CallFrame,
args_size: usize,
args_offset: usize,
ret_size: usize,
ret_offset: usize,
is_cached: bool,
) -> Result<U256, OutOfGasError> {
compute_gas_call(
current_call_frame,
args_size,
args_offset,
ret_size,
ret_offset,
is_cached,
)
}

fn compute_gas_call(
current_call_frame: &mut CallFrame,
args_size: usize,
args_offset: usize,
ret_size: usize,
ret_offset: usize,
is_cached: bool,
) -> Result<U256, OutOfGasError> {
let arg_sum = args_offset
.checked_add(args_size)
.ok_or(OutOfGasError::GasCostOverflow)?;
let ret_sum = ret_offset
.checked_add(ret_size)
.ok_or(OutOfGasError::GasCostOverflow)?;

let memory_expansion_cost = if arg_sum > ret_sum {
current_call_frame
.memory
.expansion_cost(args_offset, args_size)?
} else {
current_call_frame
.memory
.expansion_cost(ret_offset, ret_size)?
};

let access_cost = if is_cached {
WARM_ADDRESS_ACCESS_COST
} else {
COLD_ADDRESS_ACCESS_COST
};

memory_expansion_cost
.checked_add(access_cost)
.ok_or(OutOfGasError::GasCostOverflow)
}
*/
pub fn create(
current_call_frame: &mut CallFrame,
current_call_frame: &CallFrame,
code_offset_in_memory: usize,
code_size_in_memory: usize,
) -> Result<U256, OutOfGasError> {
Expand All @@ -547,7 +389,7 @@ pub fn create(
}

pub fn create_2(
current_call_frame: &mut CallFrame,
current_call_frame: &CallFrame,
code_offset_in_memory: usize,
code_size_in_memory: usize,
) -> Result<U256, OutOfGasError> {
Expand All @@ -560,7 +402,7 @@ pub fn create_2(
}

fn compute_gas_create(
current_call_frame: &mut CallFrame,
current_call_frame: &CallFrame,
code_offset_in_memory: usize,
code_size_in_memory: usize,
is_create_2: bool,
Expand Down
19 changes: 8 additions & 11 deletions crates/vm/levm/src/opcode_handlers/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,14 @@ impl VM {

let target_address = word_to_address(current_call_frame.stack.pop()?);

let (target_account_info, target_account_is_cold) = self.access_account(target_address);

self.increase_consumed_gas(
current_call_frame,
gas_cost::selfdestruct(target_account_is_cold, target_account_info.is_empty())
.map_err(VMError::OutOfGas)?,
)?;

let (current_account_info, _current_account_is_cold) =
self.access_account(current_call_frame.to);

Expand All @@ -501,17 +509,6 @@ impl VM {
.insert(current_call_frame.to);
}

let (target_account_info, target_account_is_cold) = self.access_account(target_address);

self.increase_account_balance(target_address, target_account_info.balance)?;

// Don't like to consume the gas after the operation but i need to don't cache both at the same time
self.increase_consumed_gas(
current_call_frame,
gas_cost::selfdestruct(target_account_is_cold, target_account_info.is_empty())
.map_err(VMError::OutOfGas)?,
)?;

Ok(OpcodeSuccess::Result(ResultReason::SelfDestruct))
}
}
7 changes: 0 additions & 7 deletions crates/vm/levm/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -642,13 +642,6 @@ impl VM {
current_call_frame.sub_return_data_offset = ret_offset;
current_call_frame.sub_return_data_size = ret_size;

/*
// Update sender account and recipient in cache
self.cache.add_account(&msg_sender, &sender_account);
self.cache.add_account(&to, &recipient_account);

// self.call_frames.push(new_call_frame.clone());
*/
let tx_report = self.execute(&mut new_call_frame);

// Add gas used by the sub-context to the current one after it's execution.
Expand Down
Loading