From 927d8427e05b6d1a3aa9a63ee8e0db4fb1b82094 Mon Sep 17 00:00:00 2001 From: AnastasiiaVashchuk <72273339+AnastasiiaVashchuk@users.noreply.github.com> Date: Tue, 4 Jun 2024 08:49:50 +0300 Subject: [PATCH] feat(node-framework): Add Main Node Client layer (#2132) Note: healthchecks for `Main Node` and `Eth` client layers will be added in the next PR(there is a general principle of change.) ## Checklist - [ ] PR title corresponds to the body of PR (we generate changelog entries from PRs). - [ ] Tests for the changes have been added / updated. - [ ] Documentation comments have been added / updated. - [ ] Code has been formatted via `zk fmt` and `zk lint`. - [ ] Spellcheck has been run via `zk spellcheck`. --- .../layers/main_node_client.rs | 48 +++++++++++++++++++ .../src/implementations/layers/mod.rs | 1 + 2 files changed, 49 insertions(+) create mode 100644 core/node/node_framework/src/implementations/layers/main_node_client.rs diff --git a/core/node/node_framework/src/implementations/layers/main_node_client.rs b/core/node/node_framework/src/implementations/layers/main_node_client.rs new file mode 100644 index 000000000000..80e5d44c350f --- /dev/null +++ b/core/node/node_framework/src/implementations/layers/main_node_client.rs @@ -0,0 +1,48 @@ +use std::num::NonZeroUsize; + +use anyhow::Context; +use zksync_types::{url::SensitiveUrl, L2ChainId}; +use zksync_web3_decl::client::{Client, DynClient, L2}; + +use crate::{ + implementations::resources::main_node_client::MainNodeClientResource, + service::ServiceContext, + wiring_layer::{WiringError, WiringLayer}, +}; + +#[derive(Debug)] +pub struct MainNodeClientLayer { + url: SensitiveUrl, + rate_limit_rps: NonZeroUsize, + l2_chain_id: L2ChainId, +} + +impl MainNodeClientLayer { + pub fn new(url: SensitiveUrl, rate_limit_rps: NonZeroUsize, l2_chain_id: L2ChainId) -> Self { + Self { + url, + rate_limit_rps, + l2_chain_id, + } + } +} + +#[async_trait::async_trait] +impl WiringLayer for MainNodeClientLayer { + fn layer_name(&self) -> &'static str { + "main_node_client_layer" + } + + async fn wire(self: Box, mut context: ServiceContext<'_>) -> Result<(), WiringError> { + let main_node_client = Client::http(self.url) + .context("failed creating JSON-RPC client for main node")? + .for_network(self.l2_chain_id.into()) + .with_allowed_requests_per_second(self.rate_limit_rps) + .build(); + + context.insert_resource(MainNodeClientResource( + Box::new(main_node_client) as Box> + ))?; + Ok(()) + } +} diff --git a/core/node/node_framework/src/implementations/layers/mod.rs b/core/node/node_framework/src/implementations/layers/mod.rs index da6e76377d1f..43b1f77e88c8 100644 --- a/core/node/node_framework/src/implementations/layers/mod.rs +++ b/core/node/node_framework/src/implementations/layers/mod.rs @@ -8,6 +8,7 @@ pub mod eth_watch; pub mod healtcheck_server; pub mod house_keeper; pub mod l1_gas; +pub mod main_node_client; pub mod metadata_calculator; pub mod object_store; pub mod pk_signing_eth_client;