From c1942b4182be2fea0eb0d6b3342a792ea1677be1 Mon Sep 17 00:00:00 2001 From: Artur Puzio Date: Thu, 4 Jul 2024 18:52:04 +0200 Subject: [PATCH 1/6] feat(base-token): Base token price ratio cache update frequency configurable --- core/bin/zksync_server/src/node_builder.rs | 4 +++- .../config/src/configs/base_token_adjuster.rs | 24 +++++++++++++++---- .../src/base_token_adjuster.rs | 5 ++++ .../proto/config/base_token_adjuster.proto | 1 + .../src/base_token_ratio_provider.rs | 14 +++++++---- .../layers/base_token_ratio_provider.rs | 13 ++++++++-- 6 files changed, 49 insertions(+), 12 deletions(-) diff --git a/core/bin/zksync_server/src/node_builder.rs b/core/bin/zksync_server/src/node_builder.rs index d33abdbbf199..5c8e6bece31d 100644 --- a/core/bin/zksync_server/src/node_builder.rs +++ b/core/bin/zksync_server/src/node_builder.rs @@ -153,7 +153,9 @@ impl MainNodeBuilder { fn add_sequencer_l1_gas_layer(mut self) -> anyhow::Result { // Ensure the BaseTokenRatioProviderResource is inserted if the base token is not ETH. if self.contracts_config.base_token_addr != Some(SHARED_BRIDGE_ETHER_TOKEN_ADDRESS) { - self.node.add_layer(BaseTokenRatioProviderLayer {}); + let base_token_adjuster_config = try_load_config!(self.configs.base_token_adjuster); + self.node + .add_layer(BaseTokenRatioProviderLayer::new(base_token_adjuster_config)); } let gas_adjuster_config = try_load_config!(self.configs.eth) diff --git a/core/lib/config/src/configs/base_token_adjuster.rs b/core/lib/config/src/configs/base_token_adjuster.rs index 11d669429e05..27c3072c25c1 100644 --- a/core/lib/config/src/configs/base_token_adjuster.rs +++ b/core/lib/config/src/configs/base_token_adjuster.rs @@ -2,30 +2,46 @@ use std::time::Duration; use serde::Deserialize; -/// By default the ratio persister will run every 30 seconds. +/// By default, the ratio persister will run every 30 seconds. pub const DEFAULT_INTERVAL_MS: u64 = 30_000; +/// By default, refetch ratio from db every 0.5 second +pub const DEFAULT_CACHE_UPDATE_INTERVAL: u64 = 500; + #[derive(Debug, Clone, PartialEq, Deserialize)] pub struct BaseTokenAdjusterConfig { /// How often to spark a new cycle of the ratio persister to fetch external prices and persis ratios. - #[serde(default = "BaseTokenAdjusterConfig::default_interval")] + #[serde(default = "BaseTokenAdjusterConfig::default_pooling_interval")] pub price_polling_interval_ms: u64, + + /// We (in memory) cache the ratio fetched from db. This interval defines frequency of refetch from db. + #[serde(default = "BaseTokenAdjusterConfig::default_cache_update_interval")] + pub price_cache_update_interval_ms: u64, } impl Default for BaseTokenAdjusterConfig { fn default() -> Self { Self { - price_polling_interval_ms: Self::default_interval(), + price_polling_interval_ms: Self::default_pooling_interval(), + price_cache_update_interval_ms: Self::default_cache_update_interval(), } } } impl BaseTokenAdjusterConfig { - fn default_interval() -> u64 { + fn default_pooling_interval() -> u64 { DEFAULT_INTERVAL_MS } pub fn price_polling_interval(&self) -> Duration { Duration::from_millis(self.price_polling_interval_ms) } + + fn default_cache_update_interval() -> u64 { + DEFAULT_CACHE_UPDATE_INTERVAL + } + + pub fn price_cache_update_interval(&self) -> Duration { + Duration::from_millis(self.price_cache_update_interval_ms) + } } diff --git a/core/lib/protobuf_config/src/base_token_adjuster.rs b/core/lib/protobuf_config/src/base_token_adjuster.rs index d8dea17daec0..2f93c1e3a747 100644 --- a/core/lib/protobuf_config/src/base_token_adjuster.rs +++ b/core/lib/protobuf_config/src/base_token_adjuster.rs @@ -11,12 +11,17 @@ impl ProtoRepr for proto::BaseTokenAdjuster { price_polling_interval_ms: self .price_polling_interval_ms .expect("price_polling_interval_ms"), + + price_cache_update_interval_ms: self + .price_cache_update_interval_ms + .expect("price_cache_update_inerval_ms"), }) } fn build(this: &Self::Type) -> Self { Self { price_polling_interval_ms: Some(this.price_polling_interval_ms), + price_cache_update_interval_ms: Some(this.price_cache_update_interval_ms), } } } diff --git a/core/lib/protobuf_config/src/proto/config/base_token_adjuster.proto b/core/lib/protobuf_config/src/proto/config/base_token_adjuster.proto index 67e97dd14cda..f3adad8707b5 100644 --- a/core/lib/protobuf_config/src/proto/config/base_token_adjuster.proto +++ b/core/lib/protobuf_config/src/proto/config/base_token_adjuster.proto @@ -4,4 +4,5 @@ package zksync.config.base_token_adjuster; message BaseTokenAdjuster { optional uint64 price_polling_interval_ms = 1; + optional uint64 price_cache_update_interval_ms = 2; } diff --git a/core/node/base_token_adjuster/src/base_token_ratio_provider.rs b/core/node/base_token_adjuster/src/base_token_ratio_provider.rs index 39a96556f8de..4c06b760c50e 100644 --- a/core/node/base_token_adjuster/src/base_token_ratio_provider.rs +++ b/core/node/base_token_adjuster/src/base_token_ratio_provider.rs @@ -1,13 +1,12 @@ -use std::{fmt::Debug, num::NonZeroU64, time::Duration}; +use std::{fmt::Debug, num::NonZeroU64}; use anyhow::Context; use async_trait::async_trait; use tokio::sync::watch; +use zksync_config::BaseTokenAdjusterConfig; use zksync_dal::{ConnectionPool, Core, CoreDal}; use zksync_types::fee_model::BaseTokenConversionRatio; -const CACHE_UPDATE_INTERVAL: Duration = Duration::from_millis(500); - #[async_trait] pub trait BaseTokenRatioProvider: Debug + Send + Sync { fn get_conversion_ratio(&self) -> BaseTokenConversionRatio; @@ -17,13 +16,18 @@ pub trait BaseTokenRatioProvider: Debug + Send + Sync { pub struct DBBaseTokenRatioProvider { pub pool: ConnectionPool, pub latest_ratio: BaseTokenConversionRatio, + config: BaseTokenAdjusterConfig, } impl DBBaseTokenRatioProvider { - pub async fn new(pool: ConnectionPool) -> anyhow::Result { + pub async fn new( + pool: ConnectionPool, + config: BaseTokenAdjusterConfig, + ) -> anyhow::Result { let mut fetcher = Self { pool, latest_ratio: BaseTokenConversionRatio::default(), + config, }; fetcher.latest_ratio = fetcher.get_latest_price().await?; @@ -37,7 +41,7 @@ impl DBBaseTokenRatioProvider { } pub async fn run(&mut self, mut stop_receiver: watch::Receiver) -> anyhow::Result<()> { - let mut timer = tokio::time::interval(CACHE_UPDATE_INTERVAL); + let mut timer = tokio::time::interval(self.config.price_cache_update_interval()); while !*stop_receiver.borrow_and_update() { tokio::select! { diff --git a/core/node/node_framework/src/implementations/layers/base_token_ratio_provider.rs b/core/node/node_framework/src/implementations/layers/base_token_ratio_provider.rs index d213ac68c79b..64ba72d28c77 100644 --- a/core/node/node_framework/src/implementations/layers/base_token_ratio_provider.rs +++ b/core/node/node_framework/src/implementations/layers/base_token_ratio_provider.rs @@ -1,6 +1,7 @@ use std::sync::Arc; use zksync_base_token_adjuster::DBBaseTokenRatioProvider; +use zksync_config::BaseTokenAdjusterConfig; use crate::{ implementations::resources::{ @@ -29,7 +30,15 @@ use crate::{ /// /// - `BaseTokenRatioProvider` #[derive(Debug)] -pub struct BaseTokenRatioProviderLayer; +pub struct BaseTokenRatioProviderLayer { + config: BaseTokenAdjusterConfig, +} + +impl BaseTokenRatioProviderLayer { + pub fn new(config: BaseTokenAdjusterConfig) -> Self { + Self { config } + } +} #[async_trait::async_trait] impl WiringLayer for BaseTokenRatioProviderLayer { @@ -41,7 +50,7 @@ impl WiringLayer for BaseTokenRatioProviderLayer { let replica_pool_resource = context.get_resource::>()?; let replica_pool = replica_pool_resource.get().await.unwrap(); - let ratio_provider = DBBaseTokenRatioProvider::new(replica_pool).await?; + let ratio_provider = DBBaseTokenRatioProvider::new(replica_pool, self.config).await?; context.insert_resource(BaseTokenRatioProviderResource(Arc::new( ratio_provider.clone(), From 02eda5d4f274b7c456d1ea0506e7a47a4d1c3cc9 Mon Sep 17 00:00:00 2001 From: Artur Puzio Date: Mon, 8 Jul 2024 10:33:07 +0200 Subject: [PATCH 2/6] fix: Add missing config var + spell --- core/lib/protobuf_config/src/base_token_adjuster.rs | 2 +- etc/env/base/base_token_adjuster.toml | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/core/lib/protobuf_config/src/base_token_adjuster.rs b/core/lib/protobuf_config/src/base_token_adjuster.rs index 2f93c1e3a747..850acb4bae20 100644 --- a/core/lib/protobuf_config/src/base_token_adjuster.rs +++ b/core/lib/protobuf_config/src/base_token_adjuster.rs @@ -14,7 +14,7 @@ impl ProtoRepr for proto::BaseTokenAdjuster { price_cache_update_interval_ms: self .price_cache_update_interval_ms - .expect("price_cache_update_inerval_ms"), + .expect("price_cache_update_interval_ms"), }) } diff --git a/etc/env/base/base_token_adjuster.toml b/etc/env/base/base_token_adjuster.toml index 100da3b7224f..7959cd682759 100644 --- a/etc/env/base/base_token_adjuster.toml +++ b/etc/env/base/base_token_adjuster.toml @@ -3,4 +3,6 @@ [base_token_adjuster] # How often to poll external price feeds for the base token price. -price_polling_interval_ms = "30000" +price_polling_interval_ms = "1000" + +price_cache_update_interval_ms = "1000" \ No newline at end of file From 9aa11de19cec12b953c3835247ca3aa2c1800b84 Mon Sep 17 00:00:00 2001 From: Artur Puzio Date: Mon, 8 Jul 2024 14:03:56 +0200 Subject: [PATCH 3/6] fix: Add missing config --- etc/env/file_based/general.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/etc/env/file_based/general.yaml b/etc/env/file_based/general.yaml index 4a258a7cd99d..06f829656f88 100644 --- a/etc/env/file_based/general.yaml +++ b/etc/env/file_based/general.yaml @@ -297,6 +297,7 @@ prover_group: aggregation_round: 1 base_token_adjuster: price_polling_interval_ms: 30000 + price_cache_update_interval_ms: 2000 house_keeper: l1_batch_metrics_reporting_interval_ms: 10000 From aa015ebadfccde64a641d7c1bc04194792471062 Mon Sep 17 00:00:00 2001 From: Artur Puzio Date: Mon, 8 Jul 2024 14:05:42 +0200 Subject: [PATCH 4/6] fix: better defaults for base token intervals --- etc/env/base/base_token_adjuster.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/etc/env/base/base_token_adjuster.toml b/etc/env/base/base_token_adjuster.toml index 7959cd682759..06461ef01d03 100644 --- a/etc/env/base/base_token_adjuster.toml +++ b/etc/env/base/base_token_adjuster.toml @@ -3,6 +3,6 @@ [base_token_adjuster] # How often to poll external price feeds for the base token price. -price_polling_interval_ms = "1000" +price_polling_interval_ms = "30000" -price_cache_update_interval_ms = "1000" \ No newline at end of file +price_cache_update_interval_ms = "2000" \ No newline at end of file From 3865d2071784f07e22a31d07627544e8bca8695d Mon Sep 17 00:00:00 2001 From: Artur Puzio Date: Mon, 8 Jul 2024 21:38:05 +0200 Subject: [PATCH 5/6] chore: format,lint --- core/node/base_token_adjuster/src/base_token_ratio_provider.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/core/node/base_token_adjuster/src/base_token_ratio_provider.rs b/core/node/base_token_adjuster/src/base_token_ratio_provider.rs index d3eca5ff8883..a89c2d909a15 100644 --- a/core/node/base_token_adjuster/src/base_token_ratio_provider.rs +++ b/core/node/base_token_adjuster/src/base_token_ratio_provider.rs @@ -2,7 +2,6 @@ use std::{ fmt::Debug, num::NonZeroU64, sync::{Arc, RwLock}, - time::Duration, }; use anyhow::Context; From 292cb3b62ce72ad5cd63cad89db39bc0067b4e33 Mon Sep 17 00:00:00 2001 From: Artur Puzio Date: Tue, 9 Jul 2024 12:50:54 +0200 Subject: [PATCH 6/6] style: endline + spelling --- core/lib/config/src/configs/base_token_adjuster.rs | 6 +++--- etc/env/base/base_token_adjuster.toml | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/core/lib/config/src/configs/base_token_adjuster.rs b/core/lib/config/src/configs/base_token_adjuster.rs index 27c3072c25c1..4ef253989cd2 100644 --- a/core/lib/config/src/configs/base_token_adjuster.rs +++ b/core/lib/config/src/configs/base_token_adjuster.rs @@ -11,7 +11,7 @@ pub const DEFAULT_CACHE_UPDATE_INTERVAL: u64 = 500; #[derive(Debug, Clone, PartialEq, Deserialize)] pub struct BaseTokenAdjusterConfig { /// How often to spark a new cycle of the ratio persister to fetch external prices and persis ratios. - #[serde(default = "BaseTokenAdjusterConfig::default_pooling_interval")] + #[serde(default = "BaseTokenAdjusterConfig::default_polling_interval")] pub price_polling_interval_ms: u64, /// We (in memory) cache the ratio fetched from db. This interval defines frequency of refetch from db. @@ -22,14 +22,14 @@ pub struct BaseTokenAdjusterConfig { impl Default for BaseTokenAdjusterConfig { fn default() -> Self { Self { - price_polling_interval_ms: Self::default_pooling_interval(), + price_polling_interval_ms: Self::default_polling_interval(), price_cache_update_interval_ms: Self::default_cache_update_interval(), } } } impl BaseTokenAdjusterConfig { - fn default_pooling_interval() -> u64 { + fn default_polling_interval() -> u64 { DEFAULT_INTERVAL_MS } diff --git a/etc/env/base/base_token_adjuster.toml b/etc/env/base/base_token_adjuster.toml index 06461ef01d03..b1b997eb67ac 100644 --- a/etc/env/base/base_token_adjuster.toml +++ b/etc/env/base/base_token_adjuster.toml @@ -5,4 +5,4 @@ # How often to poll external price feeds for the base token price. price_polling_interval_ms = "30000" -price_cache_update_interval_ms = "2000" \ No newline at end of file +price_cache_update_interval_ms = "2000"