Skip to content

Commit

Permalink
feat(api): Get historical fee input (matter-labs#919)
Browse files Browse the repository at this point in the history
## What ❔

Whenever an API transaction is related an old block, use the historical
fee data

## Why ❔

<!-- Why are these changes done? What goal do they contribute to? What
are the principles behind them? -->
<!-- Example: PR templates ensure PR reviewers, observers, and future
iterators are in context about the evolution of repos. -->

## Checklist

<!-- Check your PR fulfills the following items. -->
<!-- For draft PRs check the boxes as you complete them. -->

- [ ] PR title corresponds to the body of PR (we generate changelog
entries from PRs).
- [ ] Tests for the changes have been added / updated.
- [ ] Documentation comments have been added / updated.
- [ ] Code has been formatted via `zk fmt` and `zk lint`.
- [ ] Spellcheck has been run via `zk spellcheck`.
  • Loading branch information
StanislavBreadless authored Jan 23, 2024
1 parent bce8604 commit 8e1009f
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions core/lib/zksync_core/src/api_server/execution_sandbox/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use zksync_system_constants::{
use zksync_types::{
api,
block::{pack_block_info, unpack_block_info, MiniblockHasher},
fee_model::BatchFeeInput,
get_nonce_key,
utils::{decompose_full_nonce, nonces_to_full_nonce, storage_key_for_eth_balance},
AccountTreeId, L1BatchNumber, MiniblockNumber, Nonce, ProtocolVersionId, StorageKey,
Expand Down Expand Up @@ -76,6 +77,7 @@ pub(super) fn apply_vm_in_sandbox<T>(
vm_l1_batch_number,
l1_batch_timestamp,
protocol_version,
historical_fee_input,
} = rt_handle
.block_on(block_args.resolve_block_info(&mut connection))
.with_context(|| format!("failed resolving block numbers for {block_args:?}"))?;
Expand Down Expand Up @@ -196,6 +198,9 @@ pub(super) fn apply_vm_in_sandbox<T>(
..
} = shared_args;

// In case we are executing in a past block, we'll
// use the historical fee data.
let fee_input = historical_fee_input.unwrap_or(fee_input);
let fee_input = if adjust_pubdata_price {
adjust_pubdata_price_for_tx(
fee_input,
Expand Down Expand Up @@ -314,6 +319,7 @@ pub(crate) struct ResolvedBlockInfo {
pub vm_l1_batch_number: L1BatchNumber,
pub l1_batch_timestamp: u64,
pub protocol_version: ProtocolVersionId,
pub historical_fee_input: Option<BatchFeeInput>,
}

impl BlockArgs {
Expand All @@ -324,6 +330,15 @@ impl BlockArgs {
)
}

pub(crate) fn is_estimate_like(&self) -> bool {
matches!(
self.block_id,
api::BlockId::Number(api::BlockNumber::Pending)
| api::BlockId::Number(api::BlockNumber::Latest)
| api::BlockId::Number(api::BlockNumber::Committed)
)
}

pub(crate) async fn resolve_block_info(
&self,
connection: &mut StorageProcessor<'_>,
Expand Down Expand Up @@ -361,6 +376,18 @@ impl BlockArgs {
state_l2_block_number = self.resolved_block_number;
};

let historical_fee_input = if !self.is_estimate_like() {
let miniblock_header = connection
.blocks_dal()
.get_miniblock_header(self.resolved_block_number)
.await?
.context("The resolved miniblock is not in storage")?;

Some(miniblock_header.batch_fee_input)
} else {
None
};

// Blocks without version specified are considered to be of `Version9`.
// TODO: remove `unwrap_or` when protocol version ID will be assigned for each block.
let protocol_version = connection
Expand All @@ -374,6 +401,7 @@ impl BlockArgs {
vm_l1_batch_number,
l1_batch_timestamp,
protocol_version,
historical_fee_input,
})
}
}

0 comments on commit 8e1009f

Please sign in to comment.