Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: conditional cbt l1 updates #2748

Merged
merged 13 commits into from
Aug 30, 2024
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
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
31 changes: 30 additions & 1 deletion core/node/base_token_adjuster/src/base_token_ratio_persister.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
use std::{cmp::max, fmt::Debug, sync::Arc, time::Instant};
use std::{
cmp::max,
fmt::Debug,
ops::{Div, Mul},
sync::{Arc, RwLock},
time::Instant,
};

use anyhow::Context as _;
use bigdecimal::{BigDecimal, ToPrimitive};
use tokio::{sync::watch, time::sleep};
use zksync_config::configs::base_token_adjuster::BaseTokenAdjusterConfig;
use zksync_dal::{ConnectionPool, Core, CoreDal};
Expand Down Expand Up @@ -33,6 +40,7 @@ pub struct BaseTokenRatioPersister {
base_token_address: Address,
price_api_client: Arc<dyn PriceAPIClient>,
l1_params: Option<BaseTokenRatioPersisterL1Params>,
last_persisted_l1_ratio: Arc<RwLock<Option<BigDecimal>>>,
}
ischasny marked this conversation as resolved.
Show resolved Hide resolved

impl BaseTokenRatioPersister {
Expand All @@ -50,6 +58,7 @@ impl BaseTokenRatioPersister {
base_token_address,
price_api_client,
l1_params,
last_persisted_l1_ratio: Arc::new(RwLock::new(None)),
}
}

Expand Down Expand Up @@ -124,6 +133,21 @@ impl BaseTokenRatioPersister {
return Ok(());
};

if let Some(ref prev_ratio) = *self.last_persisted_l1_ratio.read().unwrap() {
let current_ratio = BigDecimal::from(new_ratio.numerator.get())
.div(BigDecimal::from(new_ratio.denominator.get()));
let deviation = (prev_ratio - current_ratio.clone())
.abs()
.div(prev_ratio.clone())
.mul(BigDecimal::from(100))
.to_u32()
.unwrap();

ischasny marked this conversation as resolved.
Show resolved Hide resolved
if deviation < self.config.l1_update_deviation_percentage {
return Ok(());
}
}

let max_attempts = self.config.l1_tx_sending_max_attempts;
let sleep_duration = self.config.l1_tx_sending_sleep_duration();
let mut prev_base_fee_per_gas: Option<u64> = None;
Expand Down Expand Up @@ -155,6 +179,11 @@ impl BaseTokenRatioPersister {
}]
.observe(start_time.elapsed());

*self.last_persisted_l1_ratio.write().unwrap() = Some(
BigDecimal::from(new_ratio.numerator.get())
.div(BigDecimal::from(new_ratio.denominator.get())),
);

return Ok(());
}
Err(err) => {
Expand Down
Loading