Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(node-framework): Add consensus support (#1546)
## What ❔ - Moves consensus-related logic to the consensus module (so that all the logic in its own dedicated task). - Creates a layer for the consensus task. ## Why ❔ mhm ## 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`. - [ ] Linkcheck has been run via `zk linkcheck`.
- Loading branch information
Showing
15 changed files
with
357 additions
and
46 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
//! This module provides convenience functions to run consensus components in different modes | ||
//! as expected by the zkSync Era. | ||
//! | ||
//! This module simply glues APIs that are already publicly exposed by the `consensus` module, | ||
//! so in case any custom behavior is needed, these APIs should be used directly. | ||
|
||
use std::sync::Arc; | ||
|
||
use zksync_concurrency::{ctx, limiter, time}; | ||
use zksync_dal::{ConnectionPool, Core}; | ||
|
||
use super::{ | ||
config::{Config, Secrets}, | ||
fetcher::Fetcher, | ||
storage::Store, | ||
}; | ||
use crate::sync_layer::{sync_action::ActionQueueSender, MainNodeClient, SyncState}; | ||
|
||
/// Runs the consensus task in the main node mode. | ||
pub async fn run_main_node( | ||
ctx: &ctx::Ctx, | ||
cfg: super::MainNodeConfig, | ||
pool: ConnectionPool<Core>, | ||
) -> anyhow::Result<()> { | ||
// Consensus is a new component. | ||
// For now in case of error we just log it and allow the server | ||
// to continue running. | ||
if let Err(err) = cfg.run(ctx, Store(pool)).await { | ||
tracing::error!(%err, "Consensus actor failed"); | ||
} else { | ||
tracing::info!("Consensus actor stopped"); | ||
} | ||
Ok(()) | ||
} | ||
|
||
/// Runs the consensus in the fetcher mode (e.g. for the external node needs). | ||
/// The fetcher implementation may either be p2p or centralized. | ||
pub async fn run_fetcher( | ||
ctx: &ctx::Ctx, | ||
cfg: Option<(Config, Secrets)>, | ||
pool: ConnectionPool<Core>, | ||
sync_state: SyncState, | ||
main_node_client: Arc<dyn MainNodeClient>, | ||
actions: ActionQueueSender, | ||
) -> anyhow::Result<()> { | ||
let fetcher = Fetcher { | ||
store: Store(pool), | ||
sync_state: sync_state.clone(), | ||
client: main_node_client, | ||
limiter: limiter::Limiter::new( | ||
ctx, | ||
limiter::Rate { | ||
burst: 10, | ||
refresh: time::Duration::milliseconds(30), | ||
}, | ||
), | ||
}; | ||
let res = match cfg { | ||
Some((cfg, secrets)) => fetcher.run_p2p(ctx, actions, cfg.p2p(&secrets)?).await, | ||
None => fetcher.run_centralized(ctx, actions).await, | ||
}; | ||
tracing::info!("Consensus actor stopped"); | ||
res | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.