Skip to content

Commit

Permalink
Allow configuring binary search kind
Browse files Browse the repository at this point in the history
  • Loading branch information
slowli committed Oct 1, 2024
1 parent 1179fc2 commit db22a76
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 4 deletions.
10 changes: 10 additions & 0 deletions core/bin/external_node/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,10 @@ pub(crate) struct OptionalENConfig {
/// The max possible number of gas that `eth_estimateGas` is allowed to overestimate.
#[serde(default = "OptionalENConfig::default_estimate_gas_acceptable_overestimation")]
pub estimate_gas_acceptable_overestimation: u32,
/// Enables optimizations for the binary search of the gas limit in `eth_estimateGas`. These optimizations are currently
/// considered experimental.
#[serde(default)]
pub estimate_gas_optimize_search: bool,
/// The multiplier to use when suggesting gas price. Should be higher than one,
/// otherwise if the L1 prices soar, the suggested gas price won't be sufficient to be included in block.
#[serde(default = "OptionalENConfig::default_gas_price_scale_factor")]
Expand Down Expand Up @@ -558,6 +562,11 @@ impl OptionalENConfig {
web3_json_rpc.estimate_gas_acceptable_overestimation,
default_estimate_gas_acceptable_overestimation
),
estimate_gas_optimize_search: general_config
.api_config
.as_ref()
.map(|a| a.web3_json_rpc.estimate_gas_optimize_search)
.unwrap_or_default(),
gas_price_scale_factor: load_config_or_default!(
general_config.api_config,
web3_json_rpc.gas_price_scale_factor,
Expand Down Expand Up @@ -1382,6 +1391,7 @@ impl From<&ExternalNodeConfig> for InternalApiConfig {
estimate_gas_acceptable_overestimation: config
.optional
.estimate_gas_acceptable_overestimation,
estimate_gas_optimize_search: config.optional.estimate_gas_optimize_search,
bridge_addresses: BridgeAddresses {
l1_erc20_default_bridge: config.remote.l1_erc20_bridge_proxy_addr,
l2_erc20_default_bridge: config.remote.l2_erc20_bridge_addr,
Expand Down
5 changes: 5 additions & 0 deletions core/lib/config/src/configs/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ pub struct Web3JsonRpcConfig {
pub estimate_gas_scale_factor: f64,
/// The max possible number of gas that `eth_estimateGas` is allowed to overestimate.
pub estimate_gas_acceptable_overestimation: u32,
/// Enables optimizations for the binary search of the gas limit in `eth_estimateGas`. These optimizations are currently
/// considered experimental.
#[serde(default)]
pub estimate_gas_optimize_search: bool,
/// Max possible size of an ABI encoded tx (in bytes).
pub max_tx_size: usize,
/// Max number of cache misses during one VM execution. If the number of cache misses exceeds this value, the API server panics.
Expand Down Expand Up @@ -237,6 +241,7 @@ impl Web3JsonRpcConfig {
gas_price_scale_factor: 1.2,
estimate_gas_scale_factor: 1.2,
estimate_gas_acceptable_overestimation: 1000,
estimate_gas_optimize_search: false,
max_tx_size: 1000000,
vm_execution_cache_misses_limit: Default::default(),
vm_concurrency_limit: Default::default(),
Expand Down
1 change: 1 addition & 0 deletions core/lib/config/src/testonly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ impl Distribution<configs::api::Web3JsonRpcConfig> for EncodeDist {
gas_price_scale_factor: self.sample(rng),
estimate_gas_scale_factor: self.sample(rng),
estimate_gas_acceptable_overestimation: self.sample(rng),
estimate_gas_optimize_search: self.sample(rng),
max_tx_size: self.sample(rng),
vm_execution_cache_misses_limit: self.sample(rng),
vm_concurrency_limit: self.sample(rng),
Expand Down
1 change: 1 addition & 0 deletions core/lib/env_config/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ mod tests {
estimate_gas_scale_factor: 1.0f64,
gas_price_scale_factor: 1.2,
estimate_gas_acceptable_overestimation: 1000,
estimate_gas_optimize_search: false,
max_tx_size: 1000000,
vm_execution_cache_misses_limit: None,
vm_concurrency_limit: Some(512),
Expand Down
2 changes: 2 additions & 0 deletions core/lib/protobuf_config/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ impl ProtoRepr for proto::Web3JsonRpc {
&self.estimate_gas_acceptable_overestimation,
)
.context("acceptable_overestimation")?,
estimate_gas_optimize_search: self.estimate_gas_optimize_search.unwrap_or(false),
max_tx_size: required(&self.max_tx_size)
.and_then(|x| Ok((*x).try_into()?))
.context("max_tx_size")?,
Expand Down Expand Up @@ -167,6 +168,7 @@ impl ProtoRepr for proto::Web3JsonRpc {
estimate_gas_acceptable_overestimation: Some(
this.estimate_gas_acceptable_overestimation,
),
estimate_gas_optimize_search: Some(this.estimate_gas_optimize_search),
max_tx_size: Some(this.max_tx_size.try_into().unwrap()),
vm_execution_cache_misses_limit: this
.vm_execution_cache_misses_limit
Expand Down
2 changes: 2 additions & 0 deletions core/lib/protobuf_config/src/proto/config/api.proto
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ message Web3JsonRpc {
repeated MaxResponseSizeOverride max_response_body_size_overrides = 31;
repeated string api_namespaces = 32; // Optional, if empty all namespaces are available
optional bool extended_api_tracing = 33; // optional, default false
optional bool estimate_gas_optimize_search = 34; // optional, default false

reserved 15; reserved "l1_to_l2_transactions_compatibility_mode";
reserved 11; reserved "request_timeout";
reserved 12; reserved "account_pks";
Expand Down
11 changes: 10 additions & 1 deletion core/node/api_server/src/tx_sender/gas_estimation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,19 @@ pub(crate) enum BinarySearchKind {
/// Full binary search.
Full,
/// Binary search with an optimized initial pivot.
#[allow(dead_code)] // FIXME
Optimized,
}

impl BinarySearchKind {
pub(crate) fn new(optimize: bool) -> Self {
if optimize {
Self::Optimized
} else {
Self::Full
}
}
}

impl TxSender {
#[tracing::instrument(level = "debug", skip_all, fields(
initiator = ?tx.initiator_account(),
Expand Down
3 changes: 2 additions & 1 deletion core/node/api_server/src/web3/namespaces/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ impl EthNamespace {
let scale_factor = self.state.api_config.estimate_gas_scale_factor;
let acceptable_overestimation =
self.state.api_config.estimate_gas_acceptable_overestimation;
let search_kind = BinarySearchKind::new(self.state.api_config.estimate_gas_optimize_search);

let fee = self
.state
Expand All @@ -139,7 +140,7 @@ impl EthNamespace {
scale_factor,
acceptable_overestimation as u64,
state_override,
BinarySearchKind::Full,
search_kind,
)
.await?;
Ok(fee.gas_limit)
Expand Down
3 changes: 2 additions & 1 deletion core/node/api_server/src/web3/namespaces/zks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ impl ZksNamespace {
let scale_factor = self.state.api_config.estimate_gas_scale_factor;
let acceptable_overestimation =
self.state.api_config.estimate_gas_acceptable_overestimation;
let search_kind = BinarySearchKind::new(self.state.api_config.estimate_gas_optimize_search);

Ok(self
.state
Expand All @@ -114,7 +115,7 @@ impl ZksNamespace {
scale_factor,
acceptable_overestimation as u64,
state_override,
BinarySearchKind::Full,
search_kind,
)
.await?)
}
Expand Down
4 changes: 3 additions & 1 deletion core/node/api_server/src/web3/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl BlockStartInfo {
}

/// Configuration values for the API.
/// This structure is detached from `ZkSyncConfig`, since different node types (main, external, etc)
/// 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)]
Expand All @@ -101,6 +101,7 @@ pub struct InternalApiConfig {
pub max_tx_size: usize,
pub estimate_gas_scale_factor: f64,
pub estimate_gas_acceptable_overestimation: u32,
pub estimate_gas_optimize_search: bool,
pub bridge_addresses: api::BridgeAddresses,
pub bridgehub_proxy_addr: Option<Address>,
pub state_transition_proxy_addr: Option<Address>,
Expand Down Expand Up @@ -128,6 +129,7 @@ impl InternalApiConfig {
estimate_gas_scale_factor: web3_config.estimate_gas_scale_factor,
estimate_gas_acceptable_overestimation: web3_config
.estimate_gas_acceptable_overestimation,
estimate_gas_optimize_search: web3_config.estimate_gas_optimize_search,
bridge_addresses: api::BridgeAddresses {
l1_erc20_default_bridge: contracts_config.l1_erc20_bridge_proxy_addr,
l2_erc20_default_bridge: contracts_config.l2_erc20_bridge_addr,
Expand Down

0 comments on commit db22a76

Please sign in to comment.