Skip to content

Commit 853f50f

Browse files
committed
account for gas estimation difference during spikes
1 parent 995236f commit 853f50f

File tree

5 files changed

+31
-7
lines changed

5 files changed

+31
-7
lines changed

core/lib/types/src/fee_model.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,8 @@ fn compute_batch_fee_model_input_v2(
408408
// might, so we use U256 for them.
409409
let l1_batch_overhead_wei = U256::from(l1_gas_price) * U256::from(batch_overhead_l1_gas);
410410

411+
tracing::info!(%l1_gas_price, %l1_pubdata_price, %l1_batch_overhead_wei, "starting batch fee model computation");
412+
411413
let fair_l2_gas_price = {
412414
// Firstly, we calculate which part of the overall overhead each unit of L2 gas should cover.
413415
let l1_batch_overhead_per_gas =
@@ -438,6 +440,8 @@ fn compute_batch_fee_model_input_v2(
438440
l1_pubdata_price + pubdata_overhead_wei
439441
};
440442

443+
tracing::info!(%fair_l2_gas_price, %fair_pubdata_price, "finishing batch fee model computation");
444+
441445
PubdataIndependentBatchFeeModelInput {
442446
l1_gas_price,
443447
fair_l2_gas_price,

core/node/api_server/src/web3/namespaces/eth.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,20 +100,24 @@ impl EthNamespace {
100100
_block: Option<BlockNumber>,
101101
state_override: Option<StateOverride>,
102102
) -> Result<U256, Web3Error> {
103+
tracing::info!("Estimating gas for request: {:?}", request);
103104
let mut request_with_gas_per_pubdata_overridden = request;
104105
self.state
105106
.set_nonce_for_call_request(&mut request_with_gas_per_pubdata_overridden)
106107
.await?;
107108

108109
if let Some(eip712_meta) = &mut request_with_gas_per_pubdata_overridden.eip712_meta {
110+
tracing::info!("Request has eip712 metadata");
109111
if eip712_meta.gas_per_pubdata == U256::zero() {
112+
tracing::info!("But its gas per pubdata is zero");
110113
eip712_meta.gas_per_pubdata = DEFAULT_L2_TX_GAS_PER_PUBDATA_BYTE.into();
111114
}
112115
}
113116

114117
let is_eip712 = request_with_gas_per_pubdata_overridden
115118
.eip712_meta
116119
.is_some();
120+
tracing::info!("Is EIP712? {is_eip712}");
117121
let mut connection = self.state.acquire_connection().await?;
118122
let block_args = BlockArgs::pending(&mut connection).await?;
119123
drop(connection);
@@ -122,6 +126,7 @@ impl EthNamespace {
122126
self.state.api_config.max_tx_size,
123127
block_args.use_evm_emulator(),
124128
)?;
129+
tracing::info!("Converted to L2 transaction {tx:?}");
125130

126131
// The user may not include the proper transaction type during the estimation of
127132
// the gas fee. However, it is needed for the bootloader checks to pass properly.
@@ -132,6 +137,7 @@ impl EthNamespace {
132137
// When we're estimating fee, we are trying to deduce values related to fee, so we should
133138
// not consider provided ones.
134139
let gas_price = self.state.tx_sender.gas_price().await?;
140+
tracing::info!("Using gas price {gas_price}");
135141
tx.common_data.fee.max_fee_per_gas = gas_price.into();
136142
tx.common_data.fee.max_priority_fee_per_gas = tx.common_data.fee.max_fee_per_gas;
137143

@@ -153,6 +159,7 @@ impl EthNamespace {
153159
search_kind,
154160
)
155161
.await?;
162+
tracing::info!("Final fee {fee:?}");
156163
Ok(fee.gas_limit)
157164
}
158165

core/node/api_server/src/web3/state.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ impl RpcState {
425425
call_request: &mut CallRequest,
426426
) -> Result<(), Web3Error> {
427427
if call_request.nonce.is_some() {
428+
tracing::info!("Nonce is already set, skipping");
428429
return Ok(());
429430
}
430431
let mut connection = self.acquire_connection().await?;
@@ -437,6 +438,7 @@ impl RpcState {
437438
.get_address_historical_nonce(from, latest_block_number)
438439
.await
439440
.map_err(DalError::generalize)?;
441+
tracing::info!("Setting nonce to {}", address_historical_nonce);
440442
call_request.nonce = Some(address_historical_nonce);
441443
Ok(())
442444
}

core/node/fee_model/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,15 @@ impl BatchFeeModelInputProvider for ApiFeeInputProvider {
123123
.context("no batches were found in the DB")?;
124124

125125
if !latest_batch_header.is_sealed {
126-
tracing::trace!(
126+
tracing::info!(
127127
latest_batch_number = %latest_batch_header.number,
128+
fee_input = ?latest_batch_header.fee_input,
128129
"Found an open batch; reporting its fee input"
129130
);
130131
return Ok(latest_batch_header.fee_input);
131132
}
132133

133-
tracing::trace!(
134+
tracing::info!(
134135
latest_batch_number = %latest_batch_header.number,
135136
"No open batch found; fetching from base provider"
136137
);

core/tests/ts-integration/src/helpers.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as zksync from 'zksync-ethers';
33
import * as ethers from 'ethers';
44
import * as hre from 'hardhat';
55
import { ZkSyncArtifact } from '@matterlabs/hardhat-zksync-solc/dist/src/types';
6-
import { TimeoutError } from 'ethers/src.ts/utils/errors';
6+
import { TransactionReceipt } from 'ethers';
77

88
export const SYSTEM_CONTEXT_ADDRESS = '0x000000000000000000000000000000000000800b';
99

@@ -75,7 +75,7 @@ export async function waitForNewL1Batch(wallet: zksync.Wallet): Promise<zksync.t
7575
const MAX_ATTEMPTS = 3;
7676

7777
let txResponse = null;
78-
let txReceipt = null;
78+
let txReceipt: TransactionReceipt | null = null;
7979
let nonce = Number(await wallet.getNonce());
8080
for (let i = 0; i < MAX_ATTEMPTS; i++) {
8181
// Send a dummy transaction and wait for it to execute. We override `maxFeePerGas` as the default ethers behavior
@@ -91,12 +91,22 @@ export async function waitForNewL1Batch(wallet: zksync.Wallet): Promise<zksync.t
9191
} else {
9292
console.log('Gas price has not gone up, waiting longer');
9393
}
94-
txReceipt = await wallet.provider.waitForTransaction(txResponse.hash, 1, 3000).catch((_: TimeoutError) => null);
94+
txReceipt = await wallet.provider.waitForTransaction(txResponse.hash, 1, 3000).catch((e) => {
95+
if (ethers.isError(e, 'TIMEOUT')) {
96+
console.log(`Transaction timed out, potentially gas price went up (attempt ${i + 1}/${MAX_ATTEMPTS})`);
97+
return null;
98+
} else if (ethers.isError(e, 'UNKNOWN_ERROR') && e.message.match(/Not enough gas/)) {
99+
console.log(
100+
`Transaction did not have enough gas, likely gas price went up (attempt ${i + 1}/${MAX_ATTEMPTS})`
101+
);
102+
return null;
103+
} else {
104+
return Promise.reject(e);
105+
}
106+
});
95107
if (txReceipt) {
96108
// Transaction got executed, so we can safely assume it will be sealed in the next batch
97109
break;
98-
} else {
99-
console.log(`Transaction timed out (attempt ${i + 1}/${MAX_ATTEMPTS})`);
100110
}
101111
}
102112
if (!txReceipt) {

0 commit comments

Comments
 (0)