Skip to content

Commit

Permalink
feat(node-framework): Add Main Node Client layer (#2132)
Browse files Browse the repository at this point in the history
Note: healthchecks for `Main Node` and `Eth` client layers will be added
in the next PR(there is a general principle of change.)

## Checklist

<!-- Check your PR fulfills the following items. -->
<!-- For draft PRs check the boxes as you complete them. -->

- [ ] 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`.
  • Loading branch information
AnastasiiaVashchuk authored Jun 4, 2024
1 parent f179681 commit 927d842
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<Self>, 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<DynClient<L2>>
))?;
Ok(())
}
}
1 change: 1 addition & 0 deletions core/node/node_framework/src/implementations/layers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 927d842

Please sign in to comment.