zksync_node_api_server 29.13.0-non-semver-compat

ZKsync API server
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
//! Helper module to submit transactions into the ZKsync Network.

use std::{
    sync::{
        atomic::{AtomicUsize, Ordering},
        Arc,
    },
    time::Duration,
};

use anyhow::Context as _;
use async_trait::async_trait;
use serde::Serialize;
use tokio::sync::RwLock;
use zksync_config::configs::{api::Web3JsonRpcConfig, chain::StateKeeperConfig};
use zksync_dal::{
    transactions_dal::L2TxSubmissionResult, Connection, ConnectionPool, Core, CoreDal,
};
use zksync_health_check::{CheckHealth, Health, HealthStatus};
use zksync_multivm::{
    interface::{
        tracer::TimestampAsserterParams as TracerTimestampAsserterParams, OneshotTracingParams,
        TransactionExecutionMetrics,
    },
    utils::{
        derive_base_fee_and_gas_per_pubdata, get_max_batch_gas_limit, get_max_new_factory_deps,
    },
};
use zksync_node_fee_model::{ApiFeeInputProvider, BatchFeeModelInputProvider};
use zksync_object_store::ObjectStore;
use zksync_state::PostgresStorageCaches;
use zksync_types::{
    api::state_override::StateOverride,
    fee_model::BatchFeeInput,
    get_intrinsic_constants, h256_to_u256,
    l2::{error::TxCheckError::TxDuplication, L2Tx},
    transaction_request::CallOverrides,
    utils::storage_key_for_eth_balance,
    vm::FastVmMode,
    AccountTreeId, Address, L2ChainId, Nonce, ProtocolVersionId, Transaction, H160, H256, U256,
};
use zksync_vm_executor::{
    interface::TransactionFilter,
    oneshot::{CallOrExecute, EstimateGas, MultiVmBaseSystemContracts, OneshotEnvParameters},
};

pub(super) use self::{gas_estimation::BinarySearchKind, result::SubmitTxError};
use self::{master_pool_sink::MasterPoolSink, result::ApiCallResult, tx_sink::TxSink};
use crate::execution_sandbox::{
    BlockArgs, SandboxAction, SandboxExecutionOutput, SandboxExecutor, SubmitTxStage,
    VmConcurrencyBarrier, VmConcurrencyLimiter, SANDBOX_METRICS,
};

mod gas_estimation;
pub mod master_pool_sink;
pub mod proxy;
mod result;
#[cfg(test)]
pub(crate) mod tests;
pub mod tx_sink;
pub mod whitelist;

pub async fn build_tx_sender(
    tx_sender_config: &TxSenderConfig,
    web3_json_config: &Web3JsonRpcConfig,
    replica_pool: ConnectionPool<Core>,
    master_pool: ConnectionPool<Core>,
    batch_fee_model_input_provider: Arc<dyn BatchFeeModelInputProvider>,
    storage_caches: PostgresStorageCaches,
) -> anyhow::Result<(TxSender, VmConcurrencyBarrier)> {
    let master_pool_sink = MasterPoolSink::new(master_pool);
    let tx_sender_builder = TxSenderBuilder::new(
        tx_sender_config.clone(),
        replica_pool.clone(),
        Arc::new(master_pool_sink),
    );

    let max_concurrency = web3_json_config.vm_concurrency_limit;
    let (vm_concurrency_limiter, vm_barrier) = VmConcurrencyLimiter::new(max_concurrency);

    let batch_fee_input_provider = ApiFeeInputProvider::new(
        batch_fee_model_input_provider,
        replica_pool,
        web3_json_config.gas_price_scale_factor_open_batch,
    );
    let executor_options = SandboxExecutorOptions::new(
        tx_sender_config.chain_id,
        AccountTreeId::new(tx_sender_config.fee_account_addr),
        tx_sender_config.validation_computational_gas_limit,
    )
    .await?;
    let tx_sender = tx_sender_builder.build(
        Arc::new(batch_fee_input_provider),
        Arc::new(vm_concurrency_limiter),
        executor_options,
        storage_caches,
    );
    Ok((tx_sender, vm_barrier))
}

/// Oneshot executor options used by the API server sandbox.
#[derive(Debug)]
pub struct SandboxExecutorOptions {
    pub(crate) fast_vm_mode: FastVmMode,
    pub(crate) vm_dump_store: Option<Arc<dyn ObjectStore>>,
    /// Env parameters to be used when estimating gas.
    pub(crate) estimate_gas: OneshotEnvParameters<EstimateGas>,
    /// Env parameters to be used when performing `eth_call` requests.
    pub(crate) eth_call: OneshotEnvParameters<CallOrExecute>,
    pub(crate) interrupted_execution_latency_histogram: &'static vise::Histogram<Duration>,
    #[cfg(test)]
    pub(crate) storage_delay: Option<Duration>,
}

impl SandboxExecutorOptions {
    /// Loads the contracts from the local file system.
    /// This method is *currently* preferred to be used in all contexts,
    /// given that there is no way to fetch "playground" contracts from the main node.
    pub async fn new(
        chain_id: L2ChainId,
        operator_account: AccountTreeId,
        validation_computational_gas_limit: u32,
    ) -> anyhow::Result<Self> {
        let estimate_gas_contracts =
            tokio::task::spawn_blocking(MultiVmBaseSystemContracts::load_estimate_gas_blocking)
                .await
                .context("failed loading base contracts for gas estimation")?;
        let call_contracts =
            tokio::task::spawn_blocking(MultiVmBaseSystemContracts::load_eth_call_blocking)
                .await
                .context("failed loading base contracts for calls / tx execution")?;

        Ok(Self {
            fast_vm_mode: FastVmMode::Old,
            vm_dump_store: None,
            estimate_gas: OneshotEnvParameters::new(
                Arc::new(estimate_gas_contracts),
                chain_id,
                operator_account,
                u32::MAX,
            ),
            eth_call: OneshotEnvParameters::new(
                Arc::new(call_contracts),
                chain_id,
                operator_account,
                validation_computational_gas_limit,
            ),
            interrupted_execution_latency_histogram: &SANDBOX_METRICS
                .sandbox_interrupted_execution_latency,
            #[cfg(test)]
            storage_delay: None,
        })
    }

    /// Sets the fast VM mode used by this executor.
    pub fn set_fast_vm_mode(&mut self, fast_vm_mode: FastVmMode) {
        self.fast_vm_mode = fast_vm_mode;
    }

    pub fn set_vm_dump_object_store(&mut self, store: Arc<dyn ObjectStore>) {
        self.vm_dump_store = Some(store);
    }

    pub(crate) async fn mock() -> Self {
        Self::new(L2ChainId::default(), AccountTreeId::default(), u32::MAX)
            .await
            .unwrap()
    }
}

/// Builder for the `TxSender`.
#[derive(Debug)]
pub struct TxSenderBuilder {
    /// Shared TxSender configuration.
    config: TxSenderConfig,
    /// Connection pool for read requests.
    replica_connection_pool: ConnectionPool<Core>,
    /// Sink to be used to persist transactions.
    tx_sink: Arc<dyn TxSink>,
    /// Transaction filter that can be used to reject transactions.
    transaction_filter: Option<Arc<dyn TransactionFilter>>,
    /// Cache for tokens that are white-listed for AA.
    whitelisted_tokens_for_aa_cache: Option<Arc<RwLock<Vec<Address>>>>,
}

impl TxSenderBuilder {
    pub fn new(
        config: TxSenderConfig,
        replica_connection_pool: ConnectionPool<Core>,
        tx_sink: Arc<dyn TxSink>,
    ) -> Self {
        Self {
            config,
            replica_connection_pool,
            tx_sink,
            transaction_filter: None,
            whitelisted_tokens_for_aa_cache: None,
        }
    }

    pub fn with_transaction_filter(mut self, filter: Arc<dyn TransactionFilter>) -> Self {
        self.transaction_filter = Some(filter);
        self
    }

    pub fn with_whitelisted_tokens_for_aa(mut self, cache: Arc<RwLock<Vec<Address>>>) -> Self {
        self.whitelisted_tokens_for_aa_cache = Some(cache);
        self
    }

    pub fn build(
        self,
        batch_fee_input_provider: Arc<dyn BatchFeeModelInputProvider>,
        vm_concurrency_limiter: Arc<VmConcurrencyLimiter>,
        executor_options: SandboxExecutorOptions,
        storage_caches: PostgresStorageCaches,
    ) -> TxSender {
        // Use noop sealer if no sealer was explicitly provided.
        let transaction_filter = self.transaction_filter.unwrap_or_else(|| Arc::new(()));
        let whitelisted_tokens_for_aa_cache =
            self.whitelisted_tokens_for_aa_cache.unwrap_or_else(|| {
                Arc::new(RwLock::new(self.config.whitelisted_tokens_for_aa.clone()))
            });
        let missed_storage_invocation_limit = self
            .config
            .vm_execution_cache_misses_limit
            .unwrap_or(usize::MAX);
        let executor = SandboxExecutor::real(
            executor_options,
            storage_caches,
            missed_storage_invocation_limit,
            self.config.timestamp_asserter_params.clone().map(|params| {
                TracerTimestampAsserterParams {
                    address: params.address,
                    min_time_till_end: params.min_time_till_end,
                }
            }),
        );

        TxSender(Arc::new(TxSenderInner {
            sender_config: self.config,
            tx_sink: self.tx_sink,
            replica_connection_pool: self.replica_connection_pool,
            batch_fee_input_provider,
            vm_concurrency_limiter,
            whitelisted_tokens_for_aa_cache,
            transaction_filter,
            executor,
        }))
    }
}

/// Internal static `TxSender` configuration.
///
/// This structure is detached from `ZkSyncConfig`, since different node types (main, external, etc)
/// may require different configuration layouts.
/// The intention is to only keep the actually used information here.
#[derive(Debug, Clone)]
pub struct TxSenderConfig {
    pub fee_account_addr: Address,
    pub gas_price_scale_factor: f64,
    pub max_nonce_ahead: u32,
    pub max_allowed_l2_tx_gas_limit: u64,
    pub vm_execution_cache_misses_limit: Option<usize>,
    pub validation_computational_gas_limit: u32,
    pub chain_id: L2ChainId,
    pub whitelisted_tokens_for_aa: Vec<Address>,
    pub timestamp_asserter_params: Option<TimestampAsserterParams>,
}

#[derive(Debug, Clone)]
pub struct TimestampAsserterParams {
    pub address: Address,
    pub min_time_till_end: Duration,
}

impl TxSenderConfig {
    pub fn new(
        state_keeper_config: &StateKeeperConfig,
        web3_json_config: &Web3JsonRpcConfig,
        fee_account_addr: Address,
        chain_id: L2ChainId,
    ) -> Self {
        Self {
            fee_account_addr,
            gas_price_scale_factor: web3_json_config.gas_price_scale_factor,
            max_nonce_ahead: web3_json_config.max_nonce_ahead,
            max_allowed_l2_tx_gas_limit: state_keeper_config.max_allowed_l2_tx_gas_limit,
            vm_execution_cache_misses_limit: web3_json_config.vm_execution_cache_misses_limit,
            validation_computational_gas_limit: state_keeper_config
                .validation_computational_gas_limit,
            chain_id,
            whitelisted_tokens_for_aa: web3_json_config.whitelisted_tokens_for_aa.clone(),
            timestamp_asserter_params: None,
        }
    }

    pub fn with_timestamp_asserter_params(
        mut self,
        timestamp_asserter_params: TimestampAsserterParams,
    ) -> Self {
        self.timestamp_asserter_params = Some(timestamp_asserter_params);
        self
    }
}

pub struct TxSenderInner {
    pub(super) sender_config: TxSenderConfig,
    /// Sink to be used to persist transactions.
    pub tx_sink: Arc<dyn TxSink>,
    pub replica_connection_pool: ConnectionPool<Core>,
    // Used to keep track of gas prices for the fee ticker.
    pub batch_fee_input_provider: Arc<dyn BatchFeeModelInputProvider>,
    /// Used to limit the amount of VMs that can be executed simultaneously.
    pub(super) vm_concurrency_limiter: Arc<VmConcurrencyLimiter>,
    // Cache for white-listed tokens.
    pub(super) whitelisted_tokens_for_aa_cache: Arc<RwLock<Vec<Address>>>,
    /// Batch sealer used to check whether transaction can be executed by the sequencer.
    pub(super) transaction_filter: Arc<dyn TransactionFilter>,
    pub(super) executor: SandboxExecutor,
}

/// Health check details for [`TxSender`].
#[derive(Debug, Serialize)]
struct TxSenderHealthDetails {
    vm_mode: FastVmMode,
    #[serde(skip_serializing_if = "TxSenderHealthDetails::is_zero")]
    vm_divergences: usize,
}

impl TxSenderHealthDetails {
    fn is_zero(value: &usize) -> bool {
        *value == 0
    }
}

impl From<TxSenderHealthDetails> for Health {
    fn from(details: TxSenderHealthDetails) -> Self {
        let status = if details.vm_divergences > 0 {
            HealthStatus::Affected
        } else {
            HealthStatus::Ready
        };
        Health::from(status).with_details(details)
    }
}

/// Health check for [`TxSender`].
#[derive(Debug)]
struct TxSenderHealthCheck {
    vm_mode: FastVmMode,
    vm_divergence_counter: Arc<AtomicUsize>,
}

#[async_trait]
impl CheckHealth for TxSenderHealthCheck {
    fn name(&self) -> &'static str {
        "tx_sender"
    }

    async fn check_health(&self) -> Health {
        let details = TxSenderHealthDetails {
            vm_mode: self.vm_mode,
            vm_divergences: self.vm_divergence_counter.load(Ordering::Relaxed),
        };
        details.into()
    }
}

#[derive(Clone)]
pub struct TxSender(pub(super) Arc<TxSenderInner>);

impl std::fmt::Debug for TxSender {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("TxSender").finish()
    }
}

impl TxSender {
    pub fn health_check(&self) -> impl CheckHealth {
        TxSenderHealthCheck {
            vm_mode: self.0.executor.vm_mode(),
            vm_divergence_counter: self.0.executor.vm_divergence_counter(),
        }
    }

    pub(crate) fn vm_concurrency_limiter(&self) -> Arc<VmConcurrencyLimiter> {
        Arc::clone(&self.0.vm_concurrency_limiter)
    }

    pub(crate) async fn read_whitelisted_tokens_for_aa_cache(&self) -> Vec<Address> {
        self.0.whitelisted_tokens_for_aa_cache.read().await.clone()
    }

    async fn acquire_replica_connection(&self) -> anyhow::Result<Connection<'static, Core>> {
        self.0
            .replica_connection_pool
            .connection_tagged("api")
            .await
            .context("failed acquiring connection to replica DB")
    }

    #[tracing::instrument(level = "debug", name = "submit_tx", skip_all, fields(tx.hash = ?tx.hash()))]
    pub(crate) async fn submit_tx(
        &self,
        tx: L2Tx,
        block_args: BlockArgs,
    ) -> Result<SandboxExecutionOutput, SubmitTxError> {
        let tx_hash = tx.hash();
        let stage_latency = SANDBOX_METRICS.start_tx_submit_stage(tx_hash, SubmitTxStage::Validate);
        self.validate_tx(&tx, block_args.protocol_version()).await?;
        stage_latency.observe();

        let stage_latency = SANDBOX_METRICS.start_tx_submit_stage(tx_hash, SubmitTxStage::DryRun);
        // **Important.** For the main node, this method acquires a DB connection inside `get_batch_fee_input()`.
        // Thus, it must not be called it if you're holding a DB connection already.
        let fee_input = self
            .0
            .batch_fee_input_provider
            .get_batch_fee_input()
            .await
            .context("cannot get batch fee input")?;

        let vm_permit = self.0.vm_concurrency_limiter.acquire().await;
        let action = SandboxAction::Execution {
            fee_input,
            tx: tx.clone(),
        };
        let vm_permit = vm_permit.ok_or(SubmitTxError::ServerShuttingDown)?;
        let connection = self.acquire_replica_connection().await?;
        let execution_output = self
            .0
            .executor
            .execute_in_sandbox(vm_permit.clone(), connection, action, &block_args, None)
            .await?;
        tracing::info!(
            "Submit tx {tx_hash:?} with execution metrics {:?}",
            execution_output.metrics
        );
        stage_latency.observe();

        let stage_latency =
            SANDBOX_METRICS.start_tx_submit_stage(tx_hash, SubmitTxStage::VerifyExecute);
        let connection = self.acquire_replica_connection().await?;
        let validation_result = self
            .0
            .executor
            .validate_tx_in_sandbox(
                vm_permit,
                connection,
                tx.clone(),
                block_args,
                fee_input,
                &self.read_whitelisted_tokens_for_aa_cache().await,
            )
            .await;
        stage_latency.observe();

        if let Err(err) = validation_result {
            return Err(err.into());
        }
        if !execution_output.are_published_bytecodes_ok {
            return Err(SubmitTxError::FailedToPublishCompressedBytecodes);
        }
        let mut stage_latency =
            SANDBOX_METRICS.start_tx_submit_stage(tx_hash, SubmitTxStage::DbInsert);
        self.ensure_tx_executable(&tx.clone().into(), execution_output.metrics, true)
            .await?;

        let validation_traces = validation_result?;
        let submission_res_handle = self
            .0
            .tx_sink
            .submit_tx(&tx, &execution_output, validation_traces)
            .await?;

        match submission_res_handle {
            L2TxSubmissionResult::AlreadyExecuted => {
                let initiator_account = tx.initiator_account();
                let Nonce(expected_nonce) = self
                    .get_expected_nonce(initiator_account)
                    .await
                    .with_context(|| {
                        format!("failed getting expected nonce for {initiator_account:?}")
                    })?;
                Err(SubmitTxError::NonceIsTooLow(
                    expected_nonce,
                    expected_nonce + self.0.sender_config.max_nonce_ahead,
                    tx.nonce().0,
                ))
            }
            L2TxSubmissionResult::Duplicate => {
                Err(SubmitTxError::IncorrectTx(TxDuplication(tx.hash())))
            }
            L2TxSubmissionResult::InsertionInProgress => Err(SubmitTxError::InsertionInProgress),
            L2TxSubmissionResult::Proxied => {
                stage_latency.set_stage(SubmitTxStage::TxProxy);
                stage_latency.observe();
                Ok(execution_output)
            }
            L2TxSubmissionResult::Added | L2TxSubmissionResult::Replaced => {
                stage_latency.observe();
                Ok(execution_output)
            }
        }
    }

    async fn validate_tx(
        &self,
        tx: &L2Tx,
        protocol_version: ProtocolVersionId,
    ) -> Result<(), SubmitTxError> {
        // This check is intended to ensure that the gas-related values will be safe to convert to u64 in the future computations.
        let max_gas = U256::from(u64::MAX);
        if tx.common_data.fee.gas_limit > max_gas
            || tx.common_data.fee.gas_per_pubdata_limit > max_gas
        {
            return Err(SubmitTxError::GasLimitIsTooBig);
        }

        let max_allowed_gas_limit = get_max_batch_gas_limit(protocol_version.into());
        if tx.common_data.fee.gas_limit > max_allowed_gas_limit.into() {
            return Err(SubmitTxError::GasLimitIsTooBig);
        }

        let fee_input = self
            .0
            .batch_fee_input_provider
            .get_batch_fee_input()
            .await?;

        // TODO (SMA-1715): do not subsidize the overhead for the transaction

        if tx.common_data.fee.gas_limit > self.0.sender_config.max_allowed_l2_tx_gas_limit.into() {
            tracing::info!(
                "Submitted Tx is Unexecutable {:?} because of GasLimitIsTooBig {}",
                tx.hash(),
                tx.common_data.fee.gas_limit,
            );
            return Err(SubmitTxError::GasLimitIsTooBig);
        }

        // At the moment fair_l2_gas_price is rarely changed for ETH-based chains. But for CBT
        // chains it gets changed every few blocks because of token price change. We want to avoid
        // situations when transactions with low gas price gets into mempool and sit there for a
        // long time, so we require max_fee_per_gas to be at least current_l2_fair_gas_price / 2
        if tx.common_data.fee.max_fee_per_gas < (fee_input.fair_l2_gas_price() / 2).into() {
            tracing::info!(
                "Submitted Tx is Unexecutable {:?} because of MaxFeePerGasTooLow {}",
                tx.hash(),
                tx.common_data.fee.max_fee_per_gas,
            );
            return Err(SubmitTxError::MaxFeePerGasTooLow);
        }
        if tx.common_data.fee.max_fee_per_gas < tx.common_data.fee.max_priority_fee_per_gas {
            tracing::info!(
                "Submitted Tx is Unexecutable {:?} because of MaxPriorityFeeGreaterThanMaxFee {}",
                tx.hash(),
                tx.common_data.fee.max_fee_per_gas
            );
            return Err(SubmitTxError::MaxPriorityFeeGreaterThanMaxFee);
        }
        let max_new_factory_deps = get_max_new_factory_deps(protocol_version.into());
        if tx.execute.factory_deps.len() > max_new_factory_deps {
            return Err(SubmitTxError::TooManyFactoryDependencies(
                tx.execute.factory_deps.len(),
                max_new_factory_deps,
            ));
        }

        let intrinsic_consts = get_intrinsic_constants();
        assert!(
            intrinsic_consts.l2_tx_intrinsic_pubdata == 0,
            "Currently we assume that the L2 transactions do not have any intrinsic pubdata"
        );
        let min_gas_limit = U256::from(intrinsic_consts.l2_tx_intrinsic_gas);
        if tx.common_data.fee.gas_limit < min_gas_limit {
            return Err(SubmitTxError::IntrinsicGas);
        }

        // We still double-check the nonce manually
        // to make sure that only the correct nonce is submitted and the transaction's hashes never repeat
        self.validate_account_nonce(tx).await?;
        // Even though without enough balance the tx will not pass anyway
        // we check the user for enough balance explicitly here for better DevEx.
        self.validate_enough_balance(tx).await?;
        Ok(())
    }

    async fn validate_account_nonce(&self, tx: &L2Tx) -> Result<(), SubmitTxError> {
        let Nonce(expected_nonce) = self
            .get_expected_nonce(tx.initiator_account())
            .await
            .with_context(|| {
                format!(
                    "failed getting expected nonce for {:?}",
                    tx.initiator_account()
                )
            })?;

        if tx.common_data.nonce.0 < expected_nonce {
            Err(SubmitTxError::NonceIsTooLow(
                expected_nonce,
                expected_nonce + self.0.sender_config.max_nonce_ahead,
                tx.nonce().0,
            ))
        } else {
            let max_nonce = expected_nonce + self.0.sender_config.max_nonce_ahead;
            if !(expected_nonce..=max_nonce).contains(&tx.common_data.nonce.0) {
                Err(SubmitTxError::NonceIsTooHigh(
                    expected_nonce,
                    max_nonce,
                    tx.nonce().0,
                ))
            } else {
                Ok(())
            }
        }
    }

    async fn get_expected_nonce(&self, initiator_account: Address) -> anyhow::Result<Nonce> {
        let mut storage = self.acquire_replica_connection().await?;
        let latest_block_number = storage
            .blocks_dal()
            .get_sealed_l2_block_number()
            .await?
            .context("no L2 blocks in storage")?;

        let nonce = storage
            .storage_web3_dal()
            .get_address_historical_nonce(initiator_account, latest_block_number)
            .await
            .with_context(|| {
                format!("failed getting nonce for address {initiator_account:?} at L2 block #{latest_block_number}")
            })?;
        let nonce = u32::try_from(nonce)
            .map_err(|err| anyhow::anyhow!("failed converting nonce to u32: {err}"))?;
        Ok(Nonce(nonce))
    }

    async fn validate_enough_balance(&self, tx: &L2Tx) -> Result<(), SubmitTxError> {
        let paymaster = tx.common_data.paymaster_params.paymaster;
        // The paymaster is expected to pay for the tx; whatever balance the user has, we don't care.
        if paymaster != Address::default() {
            return Ok(());
        }

        let balance = self.get_balance(&tx.common_data.initiator_address).await?;
        // Estimate the minimum fee price user will agree to.
        let gas_price = tx.common_data.fee.max_fee_per_gas;
        let max_fee = tx.common_data.fee.gas_limit * gas_price;
        let max_fee_and_value = max_fee + tx.execute.value;

        if balance < max_fee_and_value {
            Err(SubmitTxError::NotEnoughBalanceForFeeValue(
                balance,
                max_fee,
                tx.execute.value,
            ))
        } else {
            Ok(())
        }
    }

    async fn get_balance(&self, initiator_address: &H160) -> anyhow::Result<U256> {
        let eth_balance_key = storage_key_for_eth_balance(initiator_address);
        let balance = self
            .acquire_replica_connection()
            .await?
            .storage_web3_dal()
            .get_value(&eth_balance_key)
            .await?;
        Ok(h256_to_u256(balance))
    }

    // For now, both L1 gas price and pubdata price are scaled with the same coefficient
    pub(crate) async fn scaled_batch_fee_input(&self) -> anyhow::Result<BatchFeeInput> {
        self.0
            .batch_fee_input_provider
            .get_batch_fee_input_scaled(
                self.0.sender_config.gas_price_scale_factor,
                self.0.sender_config.gas_price_scale_factor,
            )
            .await
    }

    pub(crate) async fn eth_call(
        &self,
        block_args: BlockArgs,
        call_overrides: CallOverrides,
        call: L2Tx,
        state_override: Option<StateOverride>,
    ) -> Result<Vec<u8>, SubmitTxError> {
        let vm_permit = self.0.vm_concurrency_limiter.acquire().await;
        let vm_permit = vm_permit.ok_or(SubmitTxError::ServerShuttingDown)?;

        let mut connection;
        let fee_input = if block_args.resolves_to_latest_sealed_l2_block() {
            let fee_input = self
                .0
                .batch_fee_input_provider
                .get_batch_fee_input()
                .await?;
            // It is important to acquire a connection after calling the provider; see the comment above.
            connection = self.acquire_replica_connection().await?;
            fee_input
        } else {
            connection = self.acquire_replica_connection().await?;
            block_args.historical_fee_input(&mut connection).await?
        };

        let action = SandboxAction::Call {
            call,
            fee_input,
            enforced_base_fee: call_overrides.enforced_base_fee,
            tracing_params: OneshotTracingParams::default(),
        };
        let result = self
            .0
            .executor
            .execute_in_sandbox(vm_permit, connection, action, &block_args, state_override)
            .await?;
        result.result.into_api_call_result()
    }

    pub async fn gas_price_and_gas_per_pubdata(&self) -> anyhow::Result<(u64, u64)> {
        let mut connection = self.acquire_replica_connection().await?;
        let protocol_version = connection
            .blocks_dal()
            .pending_protocol_version()
            .await
            .context("failed obtaining pending protocol version")?;
        drop(connection);

        let (base_fee, gas_per_pubdata) = derive_base_fee_and_gas_per_pubdata(
            self.scaled_batch_fee_input().await?,
            protocol_version.into(),
        );
        Ok((base_fee, gas_per_pubdata))
    }

    async fn ensure_tx_executable(
        &self,
        transaction: &Transaction,
        tx_metrics: TransactionExecutionMetrics,
        log_message: bool,
    ) -> Result<(), SubmitTxError> {
        // Hash is not computable for the provided `transaction` during gas estimation (it doesn't have
        // its input data set). Since we don't log a hash in this case anyway, we just use a dummy value.
        let tx_hash = if log_message {
            transaction.hash()
        } else {
            H256::zero()
        };

        if let Err(reason) = self
            .0
            .transaction_filter
            .filter_transaction(transaction, &tx_metrics)
            .await
        {
            let message = format!(
                "Tx is Unexecutable because of {reason}; inputs for decision: {tx_metrics:?}"
            );
            if log_message {
                tracing::info!("{tx_hash:#?} {message}");
            }
            return Err(SubmitTxError::Unexecutable(message));
        }
        Ok(())
    }
}