Skip to content

Commit

Permalink
feat(light-node): make light-node more flexible to configure
Browse files Browse the repository at this point in the history
  • Loading branch information
pashinov authored and Rexagon committed Feb 6, 2025
1 parent e164e05 commit 23e6417
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 52 deletions.
7 changes: 5 additions & 2 deletions cli/src/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use tycho_control::{ControlEndpoint, ControlServer, ControlServerConfig, Control
use tycho_core::block_strider::{
ArchiveBlockProvider, ArchiveBlockProviderConfig, BlockProvider, BlockProviderExt,
BlockStrider, BlockSubscriberExt, BlockchainBlockProvider, BlockchainBlockProviderConfig,
FileZerostateProvider, GcSubscriber, MetricsSubscriber, OptionalBlockStuff,
ColdBootType, FileZerostateProvider, GcSubscriber, MetricsSubscriber, OptionalBlockStuff,
PersistentBlockStriderState, PsSubscriber, ShardStateApplier, Starter, StarterConfig,
StateSubscriber, StateSubscriberContext, StorageBlockProvider,
};
Expand Down Expand Up @@ -236,7 +236,10 @@ impl Node {
self.zerostate,
self.starter_config.clone(),
)
.cold_boot(zerostates.map(FileZerostateProvider), false)
.cold_boot(
ColdBootType::LatestPersistent,
zerostates.map(FileZerostateProvider),
)
.await?
}
};
Expand Down
2 changes: 0 additions & 2 deletions core/src/block_strider/archive_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,6 @@ where
}
}

pub struct ArchiveHandlerPrepared {}

struct Inner<S> {
storage: Storage,
archive_subscriber: S,
Expand Down
6 changes: 4 additions & 2 deletions core/src/block_strider/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ use tycho_util::futures::JoinTask;
use tycho_util::metrics::HistogramGuard;
use tycho_util::FastHashMap;

pub use self::archive_handler::{ArchiveHandler, ArchiveHandlerPrepared};
pub use self::archive_handler::ArchiveHandler;
pub use self::provider::{
ArchiveBlockProvider, ArchiveBlockProviderConfig, BlockProvider, BlockProviderExt,
BlockchainBlockProvider, BlockchainBlockProviderConfig, ChainBlockProvider, CheckProof,
EmptyBlockProvider, OptionalBlockStuff, ProofChecker, RetryConfig, StorageBlockProvider,
};
pub use self::starter::{FileZerostateProvider, Starter, StarterConfig, ZerostateProvider};
pub use self::starter::{
ColdBootType, FileZerostateProvider, Starter, StarterConfig, ZerostateProvider,
};
pub use self::state::{BlockStriderState, PersistentBlockStriderState, TempBlockStriderState};
pub use self::state_applier::ShardStateApplier;
#[cfg(any(test, feature = "test"))]
Expand Down
14 changes: 7 additions & 7 deletions core/src/block_strider/starter/cold_boot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use tycho_util::sync::rayon_run;
use tycho_util::time::now_sec;
use tycho_util::FastHashMap;

use super::{StarterInner, ZerostateProvider};
use super::{ColdBootType, StarterInner, ZerostateProvider};
use crate::block_strider::{CheckProof, ProofChecker};
use crate::blockchain_rpc::{BlockchainRpcClient, DataRequirement};
use crate::overlay_client::PunishReason;
Expand All @@ -31,21 +31,21 @@ impl StarterInner {
#[tracing::instrument(skip_all)]
pub async fn cold_boot<P>(
&self,
boot_type: ColdBootType,
zerostates: Option<P>,
sync_from_genesis: bool,
) -> Result<BlockId>
where
P: ZerostateProvider,
{
tracing::info!("started");

let last_mc_block_id = match sync_from_genesis {
true => {
let last_mc_block_id = match boot_type {
ColdBootType::Genesis => {
let zerostates = zerostates.context("zerostate should be present")?;
let (handle, _) = self.import_zerostates(zerostates).await?;
*handle.id()
let (genersis_handle, _) = self.import_zerostates(zerostates).await?;
*genersis_handle.id()
}
false => {
ColdBootType::LatestPersistent => {
// Find the last known key block (or zerostate)
// from which we can start downloading other key blocks
let init_block = self.prepare_init_block(zerostates).await?;
Expand Down
11 changes: 7 additions & 4 deletions core/src/block_strider/starter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,21 @@ impl Starter {
/// Returns the last masterchain key block id.
pub async fn cold_boot<P>(
&self,
boot_type: ColdBootType,
zerostate_provider: Option<P>,
sync_from_genesis: bool,
) -> Result<BlockId>
where
P: ZerostateProvider,
{
self.inner
.cold_boot(zerostate_provider, sync_from_genesis)
.await
self.inner.cold_boot(boot_type, zerostate_provider).await
}
}

pub enum ColdBootType {
Genesis,
LatestPersistent,
}

struct StarterInner {
storage: Storage,
blockchain_rpc_client: BlockchainRpcClient,
Expand Down
33 changes: 29 additions & 4 deletions light-node/examples/node.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use anyhow::Context;
use clap::Parser;
use tycho_core::block_strider::PrintSubscriber;
use tycho_core::block_strider::{
ArchiveBlockProvider, BlockProviderExt, BlockchainBlockProvider, ColdBootType, PrintSubscriber,
StorageBlockProvider,
};
use tycho_light_node::CmdRun;

type Config = tycho_light_node::NodeConfig<()>;
Expand Down Expand Up @@ -30,10 +33,32 @@ async fn main() -> anyhow::Result<()> {
let config: Config =
tycho_light_node::NodeConfig::from_file(args.node.config.as_ref().context("no config")?)?;

let mut node = args.node.create(config).await?;
let init_block_id = node.init(import_zerostate, false).await?;
let mut node = args.node.create(config.clone()).await?;

let archive_block_provider = ArchiveBlockProvider::new(
node.blockchain_rpc_client().clone(),
node.storage().clone(),
config.archive_block_provider.clone(),
);

let storage_block_provider = StorageBlockProvider::new(node.storage().clone());

let blockchain_block_provider = BlockchainBlockProvider::new(
node.blockchain_rpc_client().clone(),
node.storage().clone(),
config.blockchain_block_provider.clone(),
)
.with_fallback(archive_block_provider.clone());

let init_block_id = node
.init(ColdBootType::LatestPersistent, import_zerostate)
.await?;
node.update_validator_set(&init_block_id).await?;
node.run(PrintSubscriber).await?;
node.run(
archive_block_provider.chain((blockchain_block_provider, storage_block_provider)),
PrintSubscriber,
)
.await?;

Ok(tokio::signal::ctrl_c().await?)
}
40 changes: 9 additions & 31 deletions light-node/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ use clap::Args;
use everscale_crypto::ed25519;
use everscale_types::models::*;
use tycho_core::block_strider::{
ArchiveBlockProvider, ArchiveBlockProviderConfig, BlockProviderExt, BlockStrider,
BlockSubscriber, BlockSubscriberExt, BlockchainBlockProvider, BlockchainBlockProviderConfig,
BlockProvider, BlockStrider, BlockSubscriber, BlockSubscriberExt, ColdBootType,
FileZerostateProvider, GcSubscriber, MetricsSubscriber, PersistentBlockStriderState, Starter,
StarterConfig, StorageBlockProvider,
StarterConfig,
};
use tycho_core::blockchain_rpc::{
BlockchainRpcClient, BlockchainRpcService, NoopBroadcastListener,
Expand Down Expand Up @@ -100,8 +99,6 @@ pub struct Node<C> {
blockchain_rpc_client: BlockchainRpcClient,

rpc_config: Option<RpcConfig>,
blockchain_block_provider_config: BlockchainBlockProviderConfig,
archive_block_provider_config: ArchiveBlockProviderConfig,
starter_config: StarterConfig,

run_handle: Option<tokio::task::JoinHandle<()>>,
Expand Down Expand Up @@ -227,22 +224,20 @@ impl<C> Node<C> {
blockchain_rpc_client,
config,
rpc_config: node_config.rpc,
blockchain_block_provider_config: node_config.blockchain_block_provider,
archive_block_provider_config: node_config.archive_block_provider,
starter_config: node_config.starter,
run_handle: None,
})
}

pub async fn init(
&self,
boot_type: ColdBootType,
import_zerostate: Option<Vec<PathBuf>>,
sync_from_genesis: bool,
) -> Result<BlockId> {
self.wait_for_neighbours().await;

let init_block_id = self
.boot(import_zerostate, sync_from_genesis)
.boot(boot_type, import_zerostate)
.await
.context("failed to init node")?;

Expand All @@ -265,8 +260,8 @@ impl<C> Node<C> {
/// Initialize the node and return the init block id.
async fn boot(
&self,
boot_type: ColdBootType,
zerostates: Option<Vec<PathBuf>>,
sync_from_genesis: bool,
) -> Result<BlockId> {
let node_state = self.storage.node_state();

Expand All @@ -279,7 +274,7 @@ impl<C> Node<C> {
self.zerostate,
self.starter_config.clone(),
)
.cold_boot(zerostates.map(FileZerostateProvider), sync_from_genesis)
.cold_boot(boot_type, zerostates.map(FileZerostateProvider))
.await?
}
};
Expand All @@ -292,35 +287,18 @@ impl<C> Node<C> {
Ok(last_mc_block_id)
}

pub async fn run<S>(&mut self, subscriber: S) -> Result<()>
pub async fn run<P, S>(&mut self, provider: P, subscriber: S) -> Result<()>
where
P: BlockProvider,
S: BlockSubscriber,
{
// Create block strider
let archive_block_provider = ArchiveBlockProvider::new(
self.blockchain_rpc_client.clone(),
self.storage.clone(),
self.archive_block_provider_config.clone(),
);

let storage_block_provider = StorageBlockProvider::new(self.storage.clone());

let strider_state =
PersistentBlockStriderState::new(self.zerostate.as_block_id(), self.storage.clone());

let blockchain_block_provider = BlockchainBlockProvider::new(
self.blockchain_rpc_client.clone(),
self.storage.clone(),
self.blockchain_block_provider_config.clone(),
)
.with_fallback(archive_block_provider.clone());

let gc_subscriber = GcSubscriber::new(self.storage.clone());

let block_strider = BlockStrider::builder()
.with_provider(
archive_block_provider.chain((blockchain_block_provider, storage_block_provider)),
)
.with_provider(provider)
.with_state(strider_state)
.with_block_subscriber((subscriber, MetricsSubscriber).chain(gc_subscriber))
.build();
Expand Down

0 comments on commit 23e6417

Please sign in to comment.