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: Base Token Fundamentals #2204

Merged
merged 54 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
e40eb73
node framework + config flow
shahar4 Jun 10, 2024
476addc
migration + dal
shahar4 Jun 11, 2024
8967d86
use dal
shahar4 Jun 12, 2024
7e19e94
fmt
shahar4 Jun 12, 2024
4ab267e
fmt
shahar4 Jun 12, 2024
876a6e1
fix compilation
ischasny Jun 12, 2024
3eb58d1
wired base token adjuster into the fee model
ischasny Jun 13, 2024
e6c2686
Wired base token price to fee calculator
ischasny Jun 13, 2024
63777b0
add sqlx queries
ischasny Jun 13, 2024
d3ba4c7
Lint fixes
ischasny Jun 13, 2024
9b9fdf6
update cargo lock
ischasny Jun 13, 2024
6f4cce6
Adjusted interface
ischasny Jun 14, 2024
a2b45e1
add fee model tests
ischasny Jun 14, 2024
62d54dd
rename base_token_ratio
ischasny Jun 14, 2024
09aa927
flow seems complete
shahar4 Jun 19, 2024
bbd9f7c
working sqlx + converting params themselves
shahar4 Jun 20, 2024
313444f
real working sqlx
shahar4 Jun 20, 2024
9160131
flow through updating params
shahar4 Jun 21, 2024
cce90e1
one happy test
shahar4 Jun 21, 2024
92ce099
self CR and unit test. i tests out
shahar4 Jun 23, 2024
3adef45
merge main
shahar4 Jun 23, 2024
adba682
cargo lock
shahar4 Jun 23, 2024
681735a
retry fetching price and some self CR to make CI happy
shahar4 Jun 23, 2024
34f7252
cleanup: trait, config, running locally
shahar4 Jun 24, 2024
686ace0
fmt and todo
shahar4 Jun 24, 2024
d1ac367
merge main
shahar4 Jun 25, 2024
554c901
base token cfg non optional. nume+deno again. conversion in params. u…
shahar4 Jun 25, 2024
cd7befa
merge main
shahar4 Jun 25, 2024
58fea32
zk db prepare
shahar4 Jun 25, 2024
2909d4a
revert unwanted contract changes. run adjuster in ci
shahar4 Jun 26, 2024
66f8aea
self review nits
shahar4 Jun 26, 2024
06a4c1e
use constructor instead of deleted new
shahar4 Jun 26, 2024
2a54ef1
separate the adjuster to two
shahar4 Jun 28, 2024
0415900
merge main
shahar4 Jun 28, 2024
c044dcd
happy tests
shahar4 Jun 28, 2024
5f8dac7
remove gas adjuster abstraction
shahar4 Jun 29, 2024
f732804
added config for initial values
shahar4 Jun 30, 2024
50a3764
fmt
shahar4 Jun 30, 2024
5067528
always init fetcher with new
shahar4 Jun 30, 2024
7ad6300
removing initial config. abstracting BaseTokenFetcher
shahar4 Jul 1, 2024
334a1be
start the adjuster from the l1 gas layer
shahar4 Jul 1, 2024
6fba96c
Merge main.
shahar4 Jul 1, 2024
430aac0
adjusting to framework changes
shahar4 Jul 1, 2024
ac0bc96
adjuster constructer async and inserts an initial ratio
shahar4 Jul 1, 2024
9647fc2
rely on db ratio wout retry. start adjuster layer not from l1 gas layer
shahar4 Jul 2, 2024
7a9614d
move match into get_latest_price
shahar4 Jul 2, 2024
9344f9b
PR review
shahar4 Jul 2, 2024
8f45299
changed names
shahar4 Jul 2, 2024
4462253
merge main
shahar4 Jul 2, 2024
2b5d92c
precision and scale to NUMERIC
shahar4 Jul 2, 2024
91a5a60
remove contracts change from bad main merge
shahar4 Jul 2, 2024
cfdeb10
pass ci
shahar4 Jul 2, 2024
c35a035
default_interval not public and comments
shahar4 Jul 3, 2024
11b9fed
nits
shahar4 Jul 3, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ members = [
"core/node/contract_verification_server",
"core/node/api_server",
"core/node/tee_verifier_input_producer",
"core/node/base_token_adjuster",
# Libraries
"core/lib/db_connection",
"core/lib/zksync_core_leftovers",
Expand Down Expand Up @@ -65,6 +66,7 @@ members = [
"core/lib/web3_decl",
"core/lib/snapshots_applier",
"core/lib/crypto_primitives",
"core/lib/external_price_api",
popzxc marked this conversation as resolved.
Show resolved Hide resolved
# Test infrastructure
"core/tests/test_account",
"core/tests/loadnext",
Expand Down Expand Up @@ -260,3 +262,4 @@ zksync_node_consensus = { path = "core/node/consensus" }
zksync_contract_verification_server = { path = "core/node/contract_verification_server" }
zksync_node_api_server = { path = "core/node/api_server" }
zksync_tee_verifier_input_producer = { path = "core/node/tee_verifier_input_producer" }
zksync_base_token_adjuster = {path = "core/node/base_token_adjuster"}
2 changes: 1 addition & 1 deletion core/bin/external_node/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1332,7 +1332,7 @@ impl From<&ExternalNodeConfig> for InternalApiConfig {
l2_testnet_paymaster_addr: config.remote.l2_testnet_paymaster_addr,
req_entities_limit: config.optional.req_entities_limit,
fee_history_limit: config.optional.fee_history_limit,
base_token_address: Some(config.remote.base_token_addr),
base_token_address: config.remote.base_token_addr,
filters_disabled: config.optional.filters_disabled,
dummy_verifier: config.remote.dummy_verifier,
l1_batch_commit_data_generator_mode: config.remote.l1_batch_commit_data_generator_mode,
Expand Down
8 changes: 5 additions & 3 deletions core/bin/zksync_server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ use zksync_config::{
L1Secrets, ObservabilityConfig, PrometheusConfig, ProofDataHandlerConfig,
ProtectiveReadsWriterConfig, Secrets,
},
ApiConfig, ContractVerifierConfig, DBConfig, EthConfig, EthWatchConfig, GasAdjusterConfig,
GenesisConfig, ObjectStoreConfig, PostgresConfig, SnapshotsCreatorConfig,
ApiConfig, BaseTokenAdjusterConfig, ContractVerifierConfig, DBConfig, EthConfig,
EthWatchConfig, GasAdjusterConfig, GenesisConfig, ObjectStoreConfig, PostgresConfig,
SnapshotsCreatorConfig,
};
use zksync_core_leftovers::{
genesis_init, is_genesis_needed,
Expand Down Expand Up @@ -47,7 +48,7 @@ struct Cli {
/// Comma-separated list of components to launch.
#[arg(
long,
default_value = "api,tree,eth,state_keeper,housekeeper,tee_verifier_input_producer,commitment_generator"
default_value = "api,tree,eth,state_keeper,housekeeper,tee_verifier_input_producer,commitment_generator,base_token_adjuster"
)]
components: ComponentsToRun,
/// Path to the yaml config. If set, it will be used instead of env vars.
Expand Down Expand Up @@ -270,6 +271,7 @@ fn load_env_config() -> anyhow::Result<TempConfigStore> {
snapshot_creator: SnapshotsCreatorConfig::from_env().ok(),
protective_reads_writer_config: ProtectiveReadsWriterConfig::from_env().ok(),
core_object_store: ObjectStoreConfig::from_env().ok(),
base_token_adjuster_config: BaseTokenAdjusterConfig::from_env().ok(),
commitment_generator: None,
pruning: None,
snapshot_recovery: None,
Expand Down
17 changes: 17 additions & 0 deletions core/bin/zksync_server/src/node_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use zksync_node_api_server::{
};
use zksync_node_framework::{
implementations::layers::{
base_token_adjuster::BaseTokenAdjusterLayer,
circuit_breaker_checker::CircuitBreakerCheckerLayer,
commitment_generator::CommitmentGeneratorLayer,
consensus::{ConsensusLayer, Mode as ConsensusMode},
Expand Down Expand Up @@ -145,11 +146,14 @@ impl MainNodeBuilder {
.context("Gas adjuster")?;
let state_keeper_config = try_load_config!(self.configs.state_keeper_config);
let eth_sender_config = try_load_config!(self.configs.eth);
let base_token_adjuster_config = try_load_config!(self.configs.base_token_adjuster);
let sequencer_l1_gas_layer = SequencerL1GasLayer::new(
gas_adjuster_config,
self.genesis_config.clone(),
state_keeper_config,
try_load_config!(eth_sender_config.sender).pubdata_sending_mode,
base_token_adjuster_config,
self.contracts_config.base_token_addr,
);
self.node.add_layer(sequencer_l1_gas_layer);
Ok(self)
Expand Down Expand Up @@ -455,6 +459,16 @@ impl MainNodeBuilder {
Ok(self)
}

fn add_base_token_adjuster_layer(mut self) -> anyhow::Result<Self> {
let config = try_load_config!(self.configs.base_token_adjuster);
self.node.add_layer(BaseTokenAdjusterLayer::new(
self.contracts_config.base_token_addr,
config,
));

Ok(self)
}

pub fn build(mut self, mut components: Vec<Component>) -> anyhow::Result<ZkStackService> {
// Add "base" layers (resources and helper tasks).
self = self
Expand Down Expand Up @@ -542,6 +556,9 @@ impl MainNodeBuilder {
Component::VmRunnerProtectiveReads => {
self = self.add_vm_runner_protective_reads_layer()?;
}
Component::BaseTokenAdjuster => {
self = self.add_base_token_adjuster_layer()?;
}
}
}
Ok(self.node.build()?)
Expand Down
27 changes: 27 additions & 0 deletions core/lib/config/src/configs/base_token_adjuster.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use std::time::Duration;

use serde::Deserialize;

// By default external APIs shall be polled every 30 seconds for a new price.
pub const DEFAULT_INTERVAL_MS: u64 = 30_000;

#[derive(Debug, Clone, PartialEq, Deserialize)]
pub struct BaseTokenAdjusterConfig {
/// How often to fetch external APIs for a new ETH<->Base-Token price.
pub price_polling_interval_ms: Option<u64>,
shahar4 marked this conversation as resolved.
Show resolved Hide resolved
}

impl BaseTokenAdjusterConfig {
pub fn for_tests() -> Self {
Self {
price_polling_interval_ms: Some(DEFAULT_INTERVAL_MS),
}
}

pub fn price_polling_interval(&self) -> Duration {
match self.price_polling_interval_ms {
Some(interval) => Duration::from_millis(interval),
None => Duration::from_millis(DEFAULT_INTERVAL_MS),
}
}
}
6 changes: 4 additions & 2 deletions core/lib/config/src/configs/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ pub struct ContractsConfig {
pub l2_testnet_paymaster_addr: Option<Address>,
pub l1_multicall3_addr: Address,
pub ecosystem_contracts: Option<EcosystemContracts>,
pub base_token_addr: Option<Address>,
// Used for the RPC API and by the BaseTokenAdjuster. If not set, ETH is the chain's base token
// and the "ETH address" is SHARED_BRIDGE_ETHER_TOKEN_ADDRESS.
pub base_token_addr: Address,
}

impl ContractsConfig {
Expand All @@ -56,7 +58,7 @@ impl ContractsConfig {
l2_testnet_paymaster_addr: Some(Address::repeat_byte(0x11)),
l1_multicall3_addr: Address::repeat_byte(0x12),
governance_addr: Address::repeat_byte(0x13),
base_token_addr: Some(Address::repeat_byte(0x14)),
base_token_addr: Address::repeat_byte(0x14),
ecosystem_contracts: Some(EcosystemContracts::for_tests()),
}
}
Expand Down
2 changes: 2 additions & 0 deletions core/lib/config/src/configs/general.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{
configs::{
base_token_adjuster::BaseTokenAdjusterConfig,
chain::{CircuitBreakerConfig, MempoolConfig, OperationsManagerConfig, StateKeeperConfig},
fri_prover_group::FriProverGroupConfig,
house_keeper::HouseKeeperConfig,
Expand Down Expand Up @@ -41,4 +42,5 @@ pub struct GeneralConfig {
pub snapshot_recovery: Option<SnapshotRecoveryConfig>,
pub pruning: Option<PruningConfig>,
pub core_object_store: Option<ObjectStoreConfig>,
pub base_token_adjuster: Option<BaseTokenAdjusterConfig>,
}
2 changes: 2 additions & 0 deletions core/lib/config/src/configs/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Public re-exports
pub use self::{
api::ApiConfig,
base_token_adjuster::BaseTokenAdjusterConfig,
commitment_generator::CommitmentGeneratorConfig,
contract_verifier::ContractVerifierConfig,
contracts::{ContractsConfig, EcosystemContracts},
Expand All @@ -27,6 +28,7 @@ pub use self::{
};

pub mod api;
pub mod base_token_adjuster;
pub mod chain;
mod commitment_generator;
pub mod consensus;
Expand Down
5 changes: 3 additions & 2 deletions core/lib/config/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#![allow(clippy::upper_case_acronyms, clippy::derive_partial_eq_without_eq)]

pub use crate::configs::{
ApiConfig, ContractVerifierConfig, ContractsConfig, DBConfig, EthConfig, EthWatchConfig,
GasAdjusterConfig, GenesisConfig, ObjectStoreConfig, PostgresConfig, SnapshotsCreatorConfig,
ApiConfig, BaseTokenAdjusterConfig, ContractVerifierConfig, ContractsConfig, DBConfig,
EthConfig, EthWatchConfig, GasAdjusterConfig, GenesisConfig, ObjectStoreConfig, PostgresConfig,
SnapshotsCreatorConfig,
};

pub mod configs;
Expand Down

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE IF EXISTS base_token_ratios;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CREATE TABLE base_token_ratios (
id SERIAL PRIMARY KEY,
created_at TIMESTAMP NOT NULL,
updated_at TIMESTAMP NOT NULL,

ratio_timestamp TIMESTAMP NOT NULL,
numerator NUMERIC NOT NULL,
denominator NUMERIC NOT NULL,
shahar4 marked this conversation as resolved.
Show resolved Hide resolved

used_in_l1 BOOLEAN NOT NULL DEFAULT FALSE
);
Loading
Loading