From c26578a57337e36fb69027aa20c608304771934e Mon Sep 17 00:00:00 2001 From: Alex Ostrovski Date: Wed, 15 Jan 2025 14:40:49 +0200 Subject: [PATCH] Deduplicate refunds --- core/lib/multivm/src/versions/vm_fast/mod.rs | 1 - core/lib/multivm/src/versions/vm_fast/vm.rs | 3 +- .../src/versions/vm_latest/tracers/refunds.rs | 65 ++++--------------- .../src/versions/vm_latest/utils/mod.rs | 1 + .../{vm_fast => vm_latest/utils}/refund.rs | 2 - 5 files changed, 14 insertions(+), 58 deletions(-) rename core/lib/multivm/src/versions/{vm_fast => vm_latest/utils}/refund.rs (99%) diff --git a/core/lib/multivm/src/versions/vm_fast/mod.rs b/core/lib/multivm/src/versions/vm_fast/mod.rs index 114fe5afb5be..58e7ad4b006c 100644 --- a/core/lib/multivm/src/versions/vm_fast/mod.rs +++ b/core/lib/multivm/src/versions/vm_fast/mod.rs @@ -9,7 +9,6 @@ pub use self::{ mod bytecode; mod events; mod glue; -mod refund; #[cfg(test)] mod tests; mod tracers; diff --git a/core/lib/multivm/src/versions/vm_fast/vm.rs b/core/lib/multivm/src/versions/vm_fast/vm.rs index 045a1470ab79..5065b8a7c67d 100644 --- a/core/lib/multivm/src/versions/vm_fast/vm.rs +++ b/core/lib/multivm/src/versions/vm_fast/vm.rs @@ -40,7 +40,7 @@ use crate::{ VmInterfaceHistoryEnabled, VmRevertReason, VmTrackingContracts, }, utils::events::extract_l2tol1logs_from_l1_messenger, - vm_fast::{events::merge_events, refund::compute_refund, version::FastVmVersion}, + vm_fast::{events::merge_events, version::FastVmVersion}, vm_latest::{ bootloader::{ utils::{apply_l2_block, apply_pubdata_to_memory}, @@ -51,6 +51,7 @@ use crate::{ get_vm_hook_params_start_position, get_vm_hook_position, TX_GAS_LIMIT_OFFSET, VM_HOOK_PARAMS_COUNT, }, + utils::refund::compute_refund, TransactionData, VmHook, }, VmVersion, diff --git a/core/lib/multivm/src/versions/vm_latest/tracers/refunds.rs b/core/lib/multivm/src/versions/vm_latest/tracers/refunds.rs index 1486164b2c0f..071c0ac37718 100644 --- a/core/lib/multivm/src/versions/vm_latest/tracers/refunds.rs +++ b/core/lib/multivm/src/versions/vm_latest/tracers/refunds.rs @@ -5,7 +5,7 @@ use zk_evm_1_5_0::{ aux_structures::Timestamp, tracing::{BeforeExecutionData, VmLocalStateData}, }; -use zksync_types::{ceil_div_u256, H256, U256}; +use zksync_types::H256; use crate::{ interface::{ @@ -20,7 +20,7 @@ use crate::{ old_vm::{history_recorder::HistoryMode, memory::SimpleMemory}, tracers::{traits::VmTracer, utils::get_vm_hook_params}, types::ZkSyncVmState, - utils::fee::get_batch_base_fee, + utils::refund::compute_refund, vm::MultiVmSubversion, VmHook, }, @@ -99,58 +99,15 @@ impl RefundsTracer { pubdata_published: u32, tx_hash: H256, ) -> u64 { - let total_gas_spent = tx_gas_limit - bootloader_refund; - - let gas_spent_on_computation = total_gas_spent - .checked_sub(gas_spent_on_pubdata) - .unwrap_or_else(|| { - tracing::error!( - "Gas spent on pubdata is greater than total gas spent. On pubdata: {}, total: {}", - gas_spent_on_pubdata, - total_gas_spent - ); - 0 - }); - - // For now, bootloader charges only for base fee. - let effective_gas_price = get_batch_base_fee(&self.l1_batch); - - let bootloader_eth_price_per_pubdata_byte = - U256::from(effective_gas_price) * U256::from(current_ergs_per_pubdata_byte); - - let fair_eth_price_per_pubdata_byte = - U256::from(self.l1_batch.fee_input.fair_pubdata_price()); - - // For now, L1 originated transactions are allowed to pay less than fair fee per pubdata, - // so we should take it into account. - let eth_price_per_pubdata_byte_for_calculation = std::cmp::min( - bootloader_eth_price_per_pubdata_byte, - fair_eth_price_per_pubdata_byte, - ); - - let fair_fee_eth = U256::from(gas_spent_on_computation) - * U256::from(self.l1_batch.fee_input.fair_l2_gas_price()) - + U256::from(pubdata_published) * eth_price_per_pubdata_byte_for_calculation; - let pre_paid_eth = U256::from(tx_gas_limit) * U256::from(effective_gas_price); - let refund_eth = pre_paid_eth.checked_sub(fair_fee_eth).unwrap_or_else(|| { - tracing::error!( - "Fair fee is greater than pre paid. Fair fee: {} wei, pre paid: {} wei", - fair_fee_eth, - pre_paid_eth - ); - U256::zero() - }); - - tracing::trace!( - "Fee benchmark for transaction with hash {}", - hex::encode(tx_hash.as_bytes()) - ); - tracing::trace!("Gas Limit: {}", tx_gas_limit); - tracing::trace!("Gas spent on computation: {}", gas_spent_on_computation); - tracing::trace!("Gas spent on pubdata: {}", gas_spent_on_pubdata); - tracing::trace!("Pubdata published: {}", pubdata_published); - - ceil_div_u256(refund_eth, effective_gas_price.into()).as_u64() + compute_refund( + &self.l1_batch, + bootloader_refund, + gas_spent_on_pubdata, + tx_gas_limit, + current_ergs_per_pubdata_byte, + pubdata_published, + tx_hash, + ) } pub(crate) fn pubdata_published(&self) -> u32 { diff --git a/core/lib/multivm/src/versions/vm_latest/utils/mod.rs b/core/lib/multivm/src/versions/vm_latest/utils/mod.rs index 97483633bc54..04821d411f84 100644 --- a/core/lib/multivm/src/versions/vm_latest/utils/mod.rs +++ b/core/lib/multivm/src/versions/vm_latest/utils/mod.rs @@ -6,6 +6,7 @@ pub mod fee; pub mod l2_blocks; pub(crate) mod logs; pub mod overhead; +pub(crate) mod refund; pub mod transaction_encoding; pub const fn heap_page_from_base(base: MemoryPage) -> MemoryPage { diff --git a/core/lib/multivm/src/versions/vm_fast/refund.rs b/core/lib/multivm/src/versions/vm_latest/utils/refund.rs similarity index 99% rename from core/lib/multivm/src/versions/vm_fast/refund.rs rename to core/lib/multivm/src/versions/vm_latest/utils/refund.rs index 1e30293b28c7..13637ff97122 100644 --- a/core/lib/multivm/src/versions/vm_fast/refund.rs +++ b/core/lib/multivm/src/versions/vm_latest/utils/refund.rs @@ -1,5 +1,3 @@ -// FIXME: dedupe? - use zksync_types::{ceil_div_u256, H256, U256}; use crate::{interface::L1BatchEnv, vm_latest::utils::fee::get_batch_base_fee};