Skip to content

Commit

Permalink
Merge branch 'base-token-gas-oracle' into base-token-gas-oracle-bigde…
Browse files Browse the repository at this point in the history
…cimal
  • Loading branch information
juan518munoz committed May 20, 2024
2 parents c199c4e + 79e3b97 commit 8f98dbe
Show file tree
Hide file tree
Showing 59 changed files with 742 additions and 549 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion core/bin/external_node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ zksync_snapshots_applier.workspace = true
zksync_object_store.workspace = true
prometheus_exporter.workspace = true
zksync_health_check.workspace = true
zksync_web3_decl = { workspace = true, features = ["client"] }
zksync_web3_decl.workspace = true
zksync_types.workspace = true
zksync_block_reverter.workspace = true
zksync_shared_metrics.workspace = true
Expand Down
31 changes: 18 additions & 13 deletions core/bin/external_node/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use zksync_eth_client::EthInterface;
use zksync_health_check::{async_trait, CheckHealth, Health, HealthStatus};
use zksync_types::{L1ChainId, L2ChainId};
use zksync_web3_decl::{
client::{DynClient, L2},
client::{DynClient, L1, L2},
error::ClientRpcContext,
namespaces::{EthNamespaceClient, ZksNamespaceClient},
};
Expand Down Expand Up @@ -43,10 +43,10 @@ impl CheckHealth for MainNodeHealthCheck {

/// Ethereum client health check.
#[derive(Debug)]
pub(crate) struct EthClientHealthCheck(Box<dyn EthInterface>);
pub(crate) struct EthClientHealthCheck(Box<DynClient<L1>>);

impl From<Box<dyn EthInterface>> for EthClientHealthCheck {
fn from(client: Box<dyn EthInterface>) -> Self {
impl From<Box<DynClient<L1>>> for EthClientHealthCheck {
fn from(client: Box<DynClient<L1>>) -> Self {
Self(client.for_component("ethereum_health_check"))
}
}
Expand Down Expand Up @@ -75,7 +75,7 @@ impl CheckHealth for EthClientHealthCheck {
pub(crate) struct ValidateChainIdsTask {
l1_chain_id: L1ChainId,
l2_chain_id: L2ChainId,
eth_client: Box<dyn EthInterface>,
eth_client: Box<DynClient<L1>>,
main_node_client: Box<DynClient<L2>>,
}

Expand All @@ -85,7 +85,7 @@ impl ValidateChainIdsTask {
pub fn new(
l1_chain_id: L1ChainId,
l2_chain_id: L2ChainId,
eth_client: Box<dyn EthInterface>,
eth_client: Box<DynClient<L1>>,
main_node_client: Box<DynClient<L2>>,
) -> Self {
Self {
Expand All @@ -97,7 +97,7 @@ impl ValidateChainIdsTask {
}

async fn check_eth_client(
eth_client: Box<dyn EthInterface>,
eth_client: Box<DynClient<L1>>,
expected: L1ChainId,
) -> anyhow::Result<()> {
loop {
Expand Down Expand Up @@ -218,14 +218,16 @@ impl ValidateChainIdsTask {

#[cfg(test)]
mod tests {
use zksync_eth_client::clients::MockEthereum;
use zksync_types::U64;
use zksync_web3_decl::client::MockClient;
use zksync_web3_decl::client::{MockClient, L1};

use super::*;

#[tokio::test]
async fn validating_chain_ids_errors() {
let eth_client = MockClient::builder(L1::default())
.method("eth_chainId", || Ok(U64::from(9)))
.build();
let main_node_client = MockClient::builder(L2::default())
.method("eth_chainId", || Ok(U64::from(270)))
.method("zks_L1ChainId", || Ok(U64::from(3)))
Expand All @@ -234,7 +236,7 @@ mod tests {
let validation_task = ValidateChainIdsTask::new(
L1ChainId(3), // << mismatch with the Ethereum client
L2ChainId::default(),
Box::<MockEthereum>::default(),
Box::new(eth_client.clone()),
Box::new(main_node_client.clone()),
);
let (_stop_sender, stop_receiver) = watch::channel(false);
Expand All @@ -251,7 +253,7 @@ mod tests {
let validation_task = ValidateChainIdsTask::new(
L1ChainId(9), // << mismatch with the main node client
L2ChainId::from(270),
Box::<MockEthereum>::default(),
Box::new(eth_client.clone()),
Box::new(main_node_client),
);
let err = validation_task
Expand All @@ -272,7 +274,7 @@ mod tests {
let validation_task = ValidateChainIdsTask::new(
L1ChainId(9),
L2ChainId::from(271), // << mismatch with the main node client
Box::<MockEthereum>::default(),
Box::new(eth_client),
Box::new(main_node_client),
);
let err = validation_task
Expand All @@ -288,6 +290,9 @@ mod tests {

#[tokio::test]
async fn validating_chain_ids_success() {
let eth_client = MockClient::builder(L1::default())
.method("eth_chainId", || Ok(U64::from(9)))
.build();
let main_node_client = MockClient::builder(L2::default())
.method("eth_chainId", || Ok(U64::from(270)))
.method("zks_L1ChainId", || Ok(U64::from(9)))
Expand All @@ -296,7 +301,7 @@ mod tests {
let validation_task = ValidateChainIdsTask::new(
L1ChainId(9),
L2ChainId::default(),
Box::<MockEthereum>::default(),
Box::new(eth_client),
Box::new(main_node_client),
);
let (stop_sender, stop_receiver) = watch::channel(false);
Expand Down
21 changes: 9 additions & 12 deletions core/bin/external_node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use zksync_dal::{metrics::PostgresMetrics, ConnectionPool, Core, CoreDal};
use zksync_db_connection::{
connection_pool::ConnectionPoolBuilder, healthcheck::ConnectionPoolHealthCheck,
};
use zksync_eth_client::EthInterface;
use zksync_health_check::{AppHealthCheck, HealthStatus, ReactiveHealthCheck};
use zksync_metadata_calculator::{
api_server::{TreeApiClient, TreeApiHttpClient},
Expand Down Expand Up @@ -48,7 +47,7 @@ use zksync_storage::RocksDB;
use zksync_types::L2ChainId;
use zksync_utils::wait_for_tasks::ManagedTasks;
use zksync_web3_decl::{
client::{Client, DynClient, L2},
client::{Client, DynClient, L1, L2},
jsonrpsee,
namespaces::EnNamespaceClient,
};
Expand Down Expand Up @@ -203,11 +202,10 @@ async fn run_core(
config: &ExternalNodeConfig,
connection_pool: ConnectionPool<Core>,
main_node_client: Box<DynClient<L2>>,
eth_client: Box<dyn EthInterface>,
eth_client: Box<DynClient<L1>>,
task_handles: &mut Vec<JoinHandle<anyhow::Result<()>>>,
app_health: &AppHealthCheck,
stop_receiver: watch::Receiver<bool>,
fee_params_fetcher: Arc<MainNodeFeeParamsFetcher>,
singleton_pool_builder: &ConnectionPoolBuilder<Core>,
) -> anyhow::Result<SyncState> {
// Create components.
Expand Down Expand Up @@ -312,8 +310,6 @@ async fn run_core(
}

let sk_handle = task::spawn(state_keeper.run());
let fee_params_fetcher_handle =
tokio::spawn(fee_params_fetcher.clone().run(stop_receiver.clone()));
let remote_diamond_proxy_addr = config.remote.diamond_proxy_addr;
let diamond_proxy_addr = if let Some(addr) = config.optional.contracts_diamond_proxy_addr {
anyhow::ensure!(
Expand Down Expand Up @@ -378,7 +374,6 @@ async fn run_core(

task_handles.extend([
sk_handle,
fee_params_fetcher_handle,
consistency_checker_handle,
commitment_generator_handle,
updater_handle,
Expand Down Expand Up @@ -433,6 +428,10 @@ async fn run_api(
stop_receiver.clone(),
)));

let fee_params_fetcher_handle =
tokio::spawn(fee_params_fetcher.clone().run(stop_receiver.clone()));
task_handles.push(fee_params_fetcher_handle);

let tx_sender_builder =
TxSenderBuilder::new(config.into(), connection_pool.clone(), Arc::new(tx_proxy));

Expand Down Expand Up @@ -571,7 +570,7 @@ async fn init_tasks(
connection_pool: ConnectionPool<Core>,
singleton_pool_builder: ConnectionPoolBuilder<Core>,
main_node_client: Box<DynClient<L2>>,
eth_client: Box<dyn EthInterface>,
eth_client: Box<DynClient<L1>>,
task_handles: &mut Vec<JoinHandle<anyhow::Result<()>>>,
app_health: &AppHealthCheck,
stop_receiver: watch::Receiver<bool>,
Expand Down Expand Up @@ -633,8 +632,6 @@ async fn init_tasks(
task_handles.push(tokio::spawn(fetcher.run(stop_receiver.clone())));
}

let fee_params_fetcher = Arc::new(MainNodeFeeParamsFetcher::new(main_node_client.clone()));

let sync_state = if components.contains(&Component::Core) {
run_core(
config,
Expand All @@ -644,7 +641,6 @@ async fn init_tasks(
task_handles,
app_health,
stop_receiver.clone(),
fee_params_fetcher.clone(),
&singleton_pool_builder,
)
.await?
Expand All @@ -661,6 +657,7 @@ async fn init_tasks(
};

if components.contains(&Component::HttpApi) || components.contains(&Component::WsApi) {
let fee_params_fetcher = Arc::new(MainNodeFeeParamsFetcher::new(main_node_client.clone()));
run_api(
task_handles,
config,
Expand Down Expand Up @@ -862,7 +859,7 @@ async fn run_node(
connection_pool: ConnectionPool<Core>,
singleton_pool_builder: ConnectionPoolBuilder<Core>,
main_node_client: Box<DynClient<L2>>,
eth_client: Box<dyn EthInterface>,
eth_client: Box<DynClient<L1>>,
) -> anyhow::Result<()> {
tracing::warn!("The external node is in the alpha phase, and should be used with caution.");
tracing::info!("Started the external node");
Expand Down
12 changes: 8 additions & 4 deletions core/bin/external_node/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ use zksync_types::{
api, ethabi, fee_model::FeeParams, Address, L1BatchNumber, L2BlockNumber, ProtocolVersionId,
H256, U64,
};
use zksync_web3_decl::{client::MockClient, jsonrpsee::core::ClientError};
use zksync_web3_decl::{
client::{MockClient, L1},
jsonrpsee::core::ClientError,
};

use super::*;

Expand Down Expand Up @@ -96,8 +99,8 @@ fn expected_health_components(components: &ComponentsToRun) -> Vec<&'static str>
output
}

fn mock_eth_client(diamond_proxy_addr: Address) -> MockEthereum {
MockEthereum::default().with_call_handler(move |call, _| {
fn mock_eth_client(diamond_proxy_addr: Address) -> MockClient<L1> {
let mock = MockEthereum::builder().with_call_handler(move |call, _| {
tracing::info!("L1 call: {call:?}");
if call.to == Some(diamond_proxy_addr) {
let call_signature = &call.data.as_ref().unwrap().0[..4];
Expand All @@ -121,7 +124,8 @@ fn mock_eth_client(diamond_proxy_addr: Address) -> MockEthereum {
}
}
panic!("Unexpected L1 call: {call:?}");
})
});
mock.build().into_client()
}

#[test_casing(5, ["all", "core", "api", "tree", "tree,tree_api"])]
Expand Down
22 changes: 22 additions & 0 deletions core/lib/basic_types/src/web3/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,28 @@ impl Serialize for BlockId {
}
}

impl<'de> Deserialize<'de> for BlockId {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
#[derive(Deserialize)]
#[serde(untagged)]
enum BlockIdRepresentation {
Number(BlockNumber),
Hash {
#[serde(rename = "blockHash")]
block_hash: H256,
},
}

Ok(match BlockIdRepresentation::deserialize(deserializer)? {
BlockIdRepresentation::Number(number) => Self::Number(number),
BlockIdRepresentation::Hash { block_hash } => Self::Hash(block_hash),
})
}
}

impl From<U64> for BlockId {
fn from(num: U64) -> Self {
BlockNumber::Number(num).into()
Expand Down
21 changes: 21 additions & 0 deletions core/lib/basic_types/src/web3/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
use super::*;

#[test]
fn block_id_can_be_deserialized() {
let block_id: BlockId = serde_json::from_str("\"latest\"").unwrap();
assert_eq!(block_id, BlockId::Number(BlockNumber::Latest));
let block_id: BlockId = serde_json::from_str("\"pending\"").unwrap();
assert_eq!(block_id, BlockId::Number(BlockNumber::Pending));
let block_id: BlockId = serde_json::from_str("\"earliest\"").unwrap();
assert_eq!(block_id, BlockId::Number(BlockNumber::Earliest));
let block_id: BlockId = serde_json::from_str("\"0x12\"").unwrap();
assert_eq!(
block_id,
BlockId::Number(BlockNumber::Number(U64::from(0x12)))
);

let block_id: BlockId = serde_json::from_str(
r#"{ "blockHash": "0x4242424242424242424242424242424242424242424242424242424242424242" }"#,
)
.unwrap();
assert_eq!(block_id, BlockId::Hash(H256::repeat_byte(0x42)));
}

#[test]
fn block_can_be_deserialized() {
let post_dencun = r#"
Expand Down

This file was deleted.

21 changes: 0 additions & 21 deletions core/lib/dal/src/pruning_dal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,25 +443,4 @@ impl PruningDal<'_, '_> {
.await?;
Ok(())
}

// This method must be separate as VACUUM is not supported inside a transaction
pub async fn run_vacuum_after_hard_pruning(&mut self) -> DalResult<()> {
sqlx::query!(
r#"
VACUUM FREEZE l1_batches,
miniblocks,
storage_logs,
events,
call_traces,
l2_to_l1_logs,
transactions
"#,
)
.instrument("hard_prune_batches_range#vacuum")
.report_latency()
.execute(self.storage)
.await?;

Ok(())
}
}
4 changes: 2 additions & 2 deletions core/lib/eth_client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ zksync_types.workspace = true
zksync_eth_signer.workspace = true
zksync_config.workspace = true
zksync_contracts.workspace = true
zksync_web3_decl = { workspace = true, features = ["client"] }
zksync_web3_decl.workspace = true

thiserror.workspace = true
async-trait.workspace = true
Expand All @@ -27,7 +27,7 @@ tracing.workspace = true
rlp.workspace = true

[dev-dependencies]
static_assertions.workspace = true
assert_matches.workspace = true
tokio = { workspace = true, features = ["full"] }
pretty_assertions.workspace = true
hex.workspace = true
Expand Down
Loading

0 comments on commit 8f98dbe

Please sign in to comment.