Skip to content

Commit

Permalink
feat: conditional cbt l1 updates (#2748)
Browse files Browse the repository at this point in the history
For operational efficiency, this PR enables conditional L1 updates for
chains with custom base token. It adds a new configuration
`BASE_TOKEN_ADJUSTER_L1_UPDATE_DEVIATION` that defines how much the
token price needs to fluctuate in order for the update to be propagated
to L1. Equal to 10% by default.

---------

Co-authored-by: Roman Brodetski <[email protected]>
  • Loading branch information
ischasny and RomanBrodetski authored Aug 30, 2024
1 parent bd1920b commit 6d18061
Show file tree
Hide file tree
Showing 12 changed files with 404 additions and 229 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions core/lib/config/src/configs/base_token_adjuster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ const DEFAULT_PRICE_FETCHING_SLEEP_MS: u64 = 5_000;
/// Default number of milliseconds to sleep between transaction sending attempts
const DEFAULT_L1_TX_SENDING_SLEEP_MS: u64 = 30_000;

/// Default number of percent that the quote should change in order for update to be propagated to L1
const DEFAULT_L1_UPDATE_DEVIATION_PERCENTAGE: u32 = 10;

/// Default maximum acceptable priority fee in gwei to prevent sending transaction with extremely high priority fee.
const DEFAULT_MAX_ACCEPTABLE_PRIORITY_FEE_IN_GWEI: u64 = 100_000_000_000;

Expand Down Expand Up @@ -79,6 +82,11 @@ pub struct BaseTokenAdjusterConfig {
#[serde(default = "BaseTokenAdjusterConfig::default_l1_tx_sending_sleep_ms")]
pub l1_tx_sending_sleep_ms: u64,

/// How many percent a quote needs to change in order for update to be propagated to L1.
/// Exists to save on gas.
#[serde(default = "BaseTokenAdjusterConfig::default_l1_update_deviation_percentage")]
pub l1_update_deviation_percentage: u32,

/// Maximum number of attempts to fetch quote from a remote API before failing over
#[serde(default = "BaseTokenAdjusterConfig::default_price_fetching_max_attempts")]
pub price_fetching_max_attempts: u32,
Expand Down Expand Up @@ -107,6 +115,7 @@ impl Default for BaseTokenAdjusterConfig {
l1_receipt_checking_sleep_ms: Self::default_l1_receipt_checking_sleep_ms(),
l1_tx_sending_max_attempts: Self::default_l1_tx_sending_max_attempts(),
l1_tx_sending_sleep_ms: Self::default_l1_tx_sending_sleep_ms(),
l1_update_deviation_percentage: Self::default_l1_update_deviation_percentage(),
price_fetching_sleep_ms: Self::default_price_fetching_sleep_ms(),
price_fetching_max_attempts: Self::default_price_fetching_max_attempts(),
halt_on_error: Self::default_halt_on_error(),
Expand Down Expand Up @@ -170,6 +179,9 @@ impl BaseTokenAdjusterConfig {
pub fn default_l1_tx_sending_sleep_ms() -> u64 {
DEFAULT_L1_TX_SENDING_SLEEP_MS
}
pub fn default_l1_update_deviation_percentage() -> u32 {
DEFAULT_L1_UPDATE_DEVIATION_PERCENTAGE
}

pub fn default_price_fetching_sleep_ms() -> u64 {
DEFAULT_PRICE_FETCHING_SLEEP_MS
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 @@ -1046,6 +1046,7 @@ impl Distribution<configs::base_token_adjuster::BaseTokenAdjusterConfig> for Enc
l1_receipt_checking_sleep_ms: self.sample(rng),
l1_tx_sending_max_attempts: self.sample(rng),
l1_tx_sending_sleep_ms: self.sample(rng),
l1_update_deviation_percentage: self.sample(rng),
price_fetching_max_attempts: self.sample(rng),
price_fetching_sleep_ms: self.sample(rng),
halt_on_error: self.sample(rng),
Expand Down
8 changes: 8 additions & 0 deletions core/lib/contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ const DIAMOND_INIT_CONTRACT_FILE: (&str, &str) = (
);
const GOVERNANCE_CONTRACT_FILE: (&str, &str) = ("governance", "IGovernance.sol/IGovernance.json");
const CHAIN_ADMIN_CONTRACT_FILE: (&str, &str) = ("governance", "IChainAdmin.sol/IChainAdmin.json");
const GETTERS_FACET_CONTRACT_FILE: (&str, &str) = (
"state-transition/chain-deps/facets",
"Getters.sol/GettersFacet.json",
);

const MULTICALL3_CONTRACT_FILE: (&str, &str) = ("dev-contracts", "Multicall3.sol/Multicall3.json");
const VERIFIER_CONTRACT_FILE: (&str, &str) = ("state-transition", "Verifier.sol/Verifier.json");
Expand Down Expand Up @@ -134,6 +138,10 @@ pub fn chain_admin_contract() -> Contract {
load_contract_for_both_compilers(CHAIN_ADMIN_CONTRACT_FILE)
}

pub fn getters_facet_contract() -> Contract {
load_contract_for_both_compilers(GETTERS_FACET_CONTRACT_FILE)
}

pub fn state_transition_manager_contract() -> Contract {
load_contract_for_both_compilers(STATE_TRANSITION_CONTRACT_FILE)
}
Expand Down
4 changes: 4 additions & 0 deletions core/lib/env_config/src/base_token_adjuster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ mod tests {
l1_tx_sending_sleep_ms: 30_000,
price_fetching_max_attempts: 20,
price_fetching_sleep_ms: 10_000,
l1_update_deviation_percentage: 20,
halt_on_error: true,
}
}
Expand All @@ -45,6 +46,7 @@ mod tests {
l1_tx_sending_sleep_ms: 30_000,
price_fetching_max_attempts: 3,
price_fetching_sleep_ms: 5_000,
l1_update_deviation_percentage: 10,
halt_on_error: false,
}
}
Expand All @@ -62,6 +64,7 @@ mod tests {
BASE_TOKEN_ADJUSTER_L1_RECEIPT_CHECKING_SLEEP_MS=20000
BASE_TOKEN_ADJUSTER_L1_TX_SENDING_MAX_ATTEMPTS=10
BASE_TOKEN_ADJUSTER_L1_TX_SENDING_SLEEP_MS=30000
BASE_TOKEN_ADJUSTER_L1_UPDATE_DEVIATION_PERCENTAGE=20
BASE_TOKEN_ADJUSTER_PRICE_FETCHING_MAX_ATTEMPTS=20
BASE_TOKEN_ADJUSTER_PRICE_FETCHING_SLEEP_MS=10000
BASE_TOKEN_ADJUSTER_HALT_ON_ERROR=true
Expand All @@ -85,6 +88,7 @@ mod tests {
"BASE_TOKEN_ADJUSTER_L1_RECEIPT_CHECKING_SLEEP_MS",
"BASE_TOKEN_ADJUSTER_L1_TX_SENDING_MAX_ATTEMPTS",
"BASE_TOKEN_ADJUSTER_L1_TX_SENDING_SLEEP_MS",
"BASE_TOKEN_ADJUSTER_L1_UPDATE_DEVIATION_PERCENTAGE",
"BASE_TOKEN_ADJUSTER_PRICE_FETCHING_MAX_ATTEMPTS",
"BASE_TOKEN_ADJUSTER_PRICE_FETCHING_SLEEP_MS",
"BASE_TOKEN_ADJUSTER_HALT_ON_ERROR",
Expand Down
4 changes: 4 additions & 0 deletions core/lib/protobuf_config/src/base_token_adjuster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ impl ProtoRepr for proto::BaseTokenAdjuster {
l1_tx_sending_sleep_ms: self
.l1_tx_sending_sleep_ms
.unwrap_or(Self::Type::default_l1_tx_sending_sleep_ms()),
l1_update_deviation_percentage: self
.l1_update_deviation_percentage
.unwrap_or(Self::Type::default_l1_update_deviation_percentage()),
})
}

Expand All @@ -53,6 +56,7 @@ impl ProtoRepr for proto::BaseTokenAdjuster {
l1_receipt_checking_max_attempts: Some(this.l1_receipt_checking_max_attempts),
l1_tx_sending_max_attempts: Some(this.l1_tx_sending_max_attempts),
l1_tx_sending_sleep_ms: Some(this.l1_tx_sending_sleep_ms),
l1_update_deviation_percentage: Some(this.l1_update_deviation_percentage),
price_fetching_max_attempts: Some(this.price_fetching_max_attempts),
price_fetching_sleep_ms: Some(this.price_fetching_sleep_ms),
max_tx_gas: Some(this.max_tx_gas),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ message BaseTokenAdjuster {
optional bool halt_on_error = 10;
optional uint32 price_fetching_max_attempts = 11;
optional uint64 price_fetching_sleep_ms = 12;
optional uint32 l1_update_deviation_percentage = 13;
}
1 change: 1 addition & 0 deletions core/node/base_token_adjuster/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ zksync_eth_client.workspace = true
zksync_node_fee_model.workspace = true
zksync_utils.workspace = true
vise.workspace = true
bigdecimal.workspace = true

tokio = { workspace = true, features = ["time"] }
anyhow.workspace = true
Expand Down
Loading

0 comments on commit 6d18061

Please sign in to comment.