Skip to content

Commit

Permalink
explorerd: add startup banner and refine log message lengths
Browse files Browse the repository at this point in the history
This update adds a startup banner to explorerd that logs key details about the DarkFi Explorer Node when it starts successfully. The banner includes information such as the network, JSON-RPC endpoint, database path, configuration path, syncing details, and the connected DarkFi node, improving visibility and making debugging easier during startup. Additionally, longer log messages were shortened where possible to improve message length consistency.

Summary of Changes:
- Added the `log_started_banner` function to display the startup banner
- Updated the binary crate's main method to call `log_started_banner` after a successful startup
- Shortened some log messages to maintain a more consistent length where possible
  • Loading branch information
kalm committed Feb 24, 2025
1 parent 33a79af commit 315ef49
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 22 deletions.
45 changes: 35 additions & 10 deletions bin/explorer/explorerd/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

use lazy_static::lazy_static;
use log::{debug, error, info};
use sled_overlay::sled;
use smol::{lock::Mutex, stream::StreamExt};
use std::{
collections::{HashMap, HashSet},
path::Path,
str::FromStr,
sync::Arc,
};

use lazy_static::lazy_static;
use log::{debug, error, info};
use sled_overlay::sled;
use smol::{lock::Mutex, stream::StreamExt};
use structopt_toml::{serde::Deserialize, structopt::StructOpt, StructOptToml};
use url::Url;

Expand Down Expand Up @@ -190,7 +190,7 @@ impl ExplorerService {

// Add source code into the `ContractMetaStore`
self.db.contract_meta_store.insert_source(contract_id, &source_code)?;
info!(target: "explorerd: load_native_contract_sources", "Successfully loaded contract source for native contract {}", contract_id_str.to_string());
info!(target: "explorerd: load_native_contract_sources", "Loaded native contract source {}", contract_id_str.to_string());
}
Ok(())
}
Expand Down Expand Up @@ -218,7 +218,7 @@ impl ExplorerService {

// Load contract metadata into the `ContractMetaStore`
self.db.contract_meta_store.insert_metadata(&contract_ids, &metadatas)?;
info!(target: "explorerd: load_native_contract_metadata", "Successfully loaded metadat for native contracts");
info!(target: "explorerd: load_native_contract_metadata", "Loaded metadata for native contracts");

Ok(())
}
Expand All @@ -237,18 +237,18 @@ impl ExplorerService {
0 => {
self.reset_blocks()?;
self.reset_transactions()?;
debug!(target: "explorerd::reset_explorer_state", "Successfully reset explorer state to accept a new genesis block");
debug!(target: "explorerd::reset_explorer_state", "Reset explorer state to accept a new genesis block");
}
// Reset for all other heights
_ => {
self.reset_to_height(height)?;
debug!(target: "explorerd::reset_explorer_state", "Successfully reset blocks to height: {height}");
debug!(target: "explorerd::reset_explorer_state", "Reset blocks to height: {height}");
}
}

// Reset gas metrics to the specified height to reflect the updated blockchain state
self.db.metrics_store.reset_gas_metrics(height)?;
debug!(target: "explorerd::reset_explorer_state", "Successfully reset metrics store to height: {height}");
debug!(target: "explorerd::reset_explorer_state", "Reset metrics store to height: {height}");

Ok(())
}
Expand Down Expand Up @@ -371,6 +371,9 @@ async fn realmain(args: Args, ex: Arc<smol::Executor<'static>>) -> Result<()> {
}
};

log_started_banner(explorer.clone(), &config, &args, &config_path);
info!(target: "explorerd::", "All is good. Waiting for block notifications...");

// Signal handling for graceful termination.
let (signals_handler, signals_task) = SignalHandler::new(ex)?;
signals_handler.wait_termination(signals_task).await?;
Expand All @@ -390,3 +393,25 @@ async fn realmain(args: Args, ex: Arc<smol::Executor<'static>>) -> Result<()> {

Ok(())
}

/// Logs a banner displaying the startup details of the DarkFi Explorer Node.
fn log_started_banner(
explorer: Arc<Explorerd>,
config: &ExplorerNetworkConfig,
args: &Args,
config_path: &Path,
) {
info!(target: "explorerd", "========================================================================================");
info!(target: "explorerd", " Started DarkFi Explorer Node ");
info!(target: "explorerd", "========================================================================================");
info!(target: "explorerd", " - Network: {}", args.network);
info!(target: "explorerd", " - JSON-RPC Endpoint: {}", config.rpc.rpc_listen.to_string().trim_end_matches("/"));
info!(target: "explorerd", " - Database: {}", config.database);
info!(target: "explorerd", " - Configuration: {}", config_path.to_str().unwrap_or("Error: configuration path not found!"));
info!(target: "explorerd", " - Reset Blocks: {}", if args.reset { "Yes" } else { "No" });
info!(target: "explorerd", "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
info!(target: "explorerd", " - Synced Blocks: {}", explorer.service.db.blockchain.len());
info!(target: "explorerd", " - Synced Transactions: {}", explorer.service.db.blockchain.len());
info!(target: "explorerd", " - Connected Darkfi Node: {}", config.endpoint.to_string().trim_end_matches("/"));
info!(target: "explorerd", "========================================================================================");
}
23 changes: 11 additions & 12 deletions bin/explorer/explorerd/src/rpc_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ impl Explorerd {
// Declare a mutable variable to track the current sync height while processing blocks
let mut current_height = last_synced_height;

info!(target: "explorerd::rpc_blocks::sync_blocks", "Requested to sync from block number: {current_height}");
info!(target: "explorerd::rpc_blocks::sync_blocks", "Last confirmed block number reported by darkfid: {last_darkfid_height} - {last_darkfid_hash}");
info!(target: "explorerd::rpc_blocks::sync_blocks", "Syncing from block number: {current_height}");
info!(target: "explorerd::rpc_blocks::sync_blocks", "Last confirmed darkfid block: {last_darkfid_height} - {last_darkfid_hash}");

// A reorg is detected if the hash of the last synced block differs from the hash of the last confirmed block,
// unless the reset flag is set or the current height is 0
Expand All @@ -96,7 +96,7 @@ impl Explorerd {
if reset {
self.service.reset_explorer_state(0)?;
current_height = 0;
info!(target: "explorerd::rpc_blocks::sync_blocks", "Successfully reset explorer database based on set reset parameter");
info!(target: "explorerd::rpc_blocks::sync_blocks", "Reset explorer database based on set reset parameter");
} else if reorg_detected {
// Record the start time to measure the duration of potential reorg
let start_reorg_time = Instant::now();
Expand All @@ -107,7 +107,7 @@ impl Explorerd {

// Log only if a reorg occurred (i.e., the explorer wasn't merely catching up to Darkfi node blocks)
if current_height != last_synced_height {
info!(target: "explorerd::rpc_blocks::sync_blocks", "Successfully completed reorg to height: {current_height} [{}]", fmt_duration(start_reorg_time.elapsed()));
info!(target: "explorerd::rpc_blocks::sync_blocks", "Completed reorg to height: {current_height} [{}]", fmt_duration(start_reorg_time.elapsed()));
}

// Prepare to sync the next block after reorg if not from genesis height
Expand Down Expand Up @@ -164,7 +164,7 @@ impl Explorerd {

info!(
target: "explorerd::rpc_blocks::sync_blocks",
"Successfully synced {} blocks: explorer blocks total {} [{}]",
"Synced {} blocks: explorer blocks total {} [{}]",
blocks_synced,
self.service.db.blockchain.blocks.len(),
fmt_duration(sync_start_time.elapsed()),
Expand Down Expand Up @@ -213,7 +213,7 @@ impl Explorerd {
// If hashes match but the cur_height differs from the last synced height, reset the explorer state
if cur_height != last_synced_height {
self.service.reset_explorer_state(cur_height)?;
debug!(target: "explorerd::rpc_blocks::process_sync_blocks_reorg", "Successfully completed reorg to height: {cur_height}");
debug!(target: "explorerd::rpc_blocks::process_sync_blocks_reorg", "Completed reorg to height: {cur_height}");
}
break;
} else {
Expand Down Expand Up @@ -461,7 +461,6 @@ pub async fn subscribe_blocks(
ex.clone(),
);
info!(target: "explorerd::rpc_blocks::subscribe_blocks", "Detached subscription to background");
info!(target: "explorerd::rpc_blocks::subscribe_blocks", "All is good. Waiting for block notifications...");

let listener_task = StoppableTask::new();
listener_task.clone().start(
Expand Down Expand Up @@ -503,9 +502,9 @@ pub async fn subscribe_blocks(
)))
},
};
info!(target: "explorerd::rpc_blocks::subscribe_blocks", "=======================================");
info!(target: "explorerd::rpc_blocks::subscribe_blocks", "Block Notification: {}", darkfid_block.hash().to_string());
info!(target: "explorerd::rpc_blocks::subscribe_blocks", "=======================================");
info!(target: "explorerd::rpc_blocks::subscribe_blocks", "========================================================================================");
info!(target: "explorerd::rpc_blocks::subscribe_blocks", "| Block Notification: {} |", darkfid_block.hash().to_string());
info!(target: "explorerd::rpc_blocks::subscribe_blocks", "========================================================================================");

// Store darkfi node block height for later use
let darkfid_block_height = darkfid_block.header.height;
Expand All @@ -523,7 +522,7 @@ pub async fn subscribe_blocks(

// Execute the reorg by resetting the explorer state to reset height
explorer.service.reset_explorer_state(reset_height)?;
info!(target: "explorerd::rpc_blocks::subscribe_blocks", "Successfully completed reorg to height: {reset_height} [{}]", fmt_duration(start_reorg_time.elapsed()));
info!(target: "explorerd::rpc_blocks::subscribe_blocks", "Completed reorg to height: {reset_height} [{}]", fmt_duration(start_reorg_time.elapsed()));
}


Expand All @@ -536,7 +535,7 @@ pub async fn subscribe_blocks(
)))
}

info!(target: "explorerd::rpc_blocks::subscribe_blocks", "Successfully stored new block at height: {} [{}]", darkfid_block.header.height, fmt_duration(start_reorg_time.elapsed()));
info!(target: "explorerd::rpc_blocks::subscribe_blocks", "Stored new block at height: {} [{}]", darkfid_block.header.height, fmt_duration(start_reorg_time.elapsed()));

// Process the next block
height = darkfid_block.header.height;
Expand Down

0 comments on commit 315ef49

Please sign in to comment.