Skip to content

Commit

Permalink
Deduplicate refunds
Browse files Browse the repository at this point in the history
  • Loading branch information
slowli committed Jan 15, 2025
1 parent 7e2f9c7 commit c26578a
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 58 deletions.
1 change: 0 additions & 1 deletion core/lib/multivm/src/versions/vm_fast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ pub use self::{
mod bytecode;
mod events;
mod glue;
mod refund;
#[cfg(test)]
mod tests;
mod tracers;
Expand Down
3 changes: 2 additions & 1 deletion core/lib/multivm/src/versions/vm_fast/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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,
Expand Down
65 changes: 11 additions & 54 deletions core/lib/multivm/src/versions/vm_latest/tracers/refunds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -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,
},
Expand Down Expand Up @@ -99,58 +99,15 @@ impl<S> RefundsTracer<S> {
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 {
Expand Down
1 change: 1 addition & 0 deletions core/lib/multivm/src/versions/vm_latest/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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};
Expand Down

0 comments on commit c26578a

Please sign in to comment.