Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Daemonize rpc-downloader and importer-offline #1854

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/bin/importer_offline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use std::cmp::min;
use std::sync::mpsc;
use std::sync::Arc;
use std::time::Duration;

use alloy_rpc_types_eth::BlockTransactions;
use anyhow::anyhow;
Expand Down
29 changes: 22 additions & 7 deletions src/bin/rpc_downloader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,29 @@ async fn run(config: RpcDownloaderConfig) -> anyhow::Result<()> {
let rpc_storage = config.rpc_storage.init().await?;
let chain = Arc::new(BlockchainClient::new_http(&config.external_rpc, config.external_rpc_timeout).await?);

let block_end = match config.block_end {
Some(end) => BlockNumber::from(end),
None => chain.fetch_block_number().await?,
};
loop {
let block_end = match config.block_end {
Some(end) => BlockNumber::from(end),
None => chain.fetch_block_number().await?,
};

// download balances and blocks
download_balances(Arc::clone(&rpc_storage), &chain, config.initial_accounts.clone()).await?;
download_blocks(rpc_storage.clone(), chain.clone(), config.paralellism, block_end).await?;

if !config.daemon {
break;
}

// download balances and blocks
download_balances(Arc::clone(&rpc_storage), &chain, config.initial_accounts).await?;
download_blocks(rpc_storage, chain, config.paralellism, block_end).await?;
if GlobalState::is_shutdown_warn("rpc-downloader::main") {
break;
}

// wait for 1 minute before restarting the download
let wait_duration = Duration::from_secs(60);
tracing::info!("Daemon mode enabled, waiting {} seconds before restarting...", wait_duration.as_secs());
tokio::time::sleep(wait_duration).await;
}

Ok(())
}
Expand Down
12 changes: 12 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,10 @@ pub struct RpcDownloaderConfig {
#[arg(short = 'p', long = "paralellism", env = "PARALELLISM", default_value = "1")]
pub paralellism: usize,

/// Daemon mode.
#[arg(short = 'd', long = "daemon", env = "DAEMON", default_value = "false")]
pub daemon: bool,

/// Accounts to retrieve initial balance information.
///
/// For Cloudwalk networks, provide these addresses:
Expand Down Expand Up @@ -303,6 +307,14 @@ pub struct ImporterOfflineConfig {
#[clap(flatten)]
pub rpc_storage: ExternalRpcConfig,

/// Daemon mode - keeps running and checking for new blocks
#[arg(short = 'd', long = "daemon", env = "DAEMON", default_value = "false")]
pub daemon: bool,

/// Interval between daemon checks in seconds
#[arg(long = "daemon-interval", env = "DAEMON_INTERVAL", default_value = "60")]
pub daemon_interval: u64,

#[deref]
#[clap(flatten)]
pub common: CommonConfig,
Expand Down
Loading