Skip to content

Commit

Permalink
chore: move command to forester status
Browse files Browse the repository at this point in the history
  • Loading branch information
ananas-block committed Oct 2, 2024
1 parent 4dd5560 commit 515ce62
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 299 deletions.
10 changes: 7 additions & 3 deletions forester/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use clap::{Parser, Subcommand};

use crate::forester_stats;

#[derive(Parser)]
#[clap(author, version, about, long_about = None)]
pub struct Cli {
Expand All @@ -14,7 +12,6 @@ pub struct Cli {
pub enum Commands {
Start(StartArgs),
Status(StatusArgs),
ForesterStats(forester_stats::Options),
}

#[derive(Parser, Clone, Debug)]
Expand Down Expand Up @@ -117,6 +114,13 @@ pub struct StatusArgs {

#[arg(long, env = "FORESTER_PUSH_GATEWAY_URL")]
pub push_gateway_url: Option<String>,
/// Select to run compressed token program tests.
#[clap(long)]
pub full: bool,
#[clap(long)]
pub protocol_config: bool,
#[clap(long, default_value_t = true)]
pub queue: bool,
}

impl StartArgs {
Expand Down
97 changes: 30 additions & 67 deletions forester/src/forester_stats.rs → forester/src/forester_status.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
use account_compression::{AddressMerkleTreeAccount, QueueAccount, StateMerkleTreeAccount};
use anchor_lang::{AccountDeserialize, Discriminator};
use clap::Parser;
use light_concurrent_merkle_tree::copy::ConcurrentMerkleTreeCopy;
use light_hash_set::HashSet;
use light_hasher::Poseidon;
use forester_utils::forester_epoch::TreeType;
use light_client::rpc::{RpcConnection, SolanaRpcConnection};
use light_registry::{protocol_config::state::ProtocolConfigPda, EpochPda, ForesterEpochPda};
use solana_sdk::{account::ReadableAccount, commitment_config::CommitmentConfig};
#[derive(Debug, Parser)]
pub struct Options {
/// Select to run compressed token program tests.
#[clap(long)]
full: bool,
#[clap(long)]
protocol_config: bool,
#[clap(long, default_value_t = true)]
queue: bool,
}
use std::sync::Arc;
use tracing::{debug, warn};

use crate::{
cli::StatusArgs,
metrics::{push_metrics, register_metrics},
run_queue_info,
tree_data_sync::fetch_trees,
ForesterConfig,
};

pub fn fetch_foreter_stats(opts: &Options) {
pub async fn fetch_foreter_status(opts: &StatusArgs) {
let commitment_config = CommitmentConfig::confirmed();
let rpc_url = std::env::var("RPC_URL")
.expect("RPC_URL environment variable not set, export RPC_URL=<url>");
Expand Down Expand Up @@ -143,57 +140,23 @@ pub fn fetch_foreter_stats(opts: &Options) {
if opts.protocol_config {
println!("protocol config: {:?}", protocol_config_pdas[0]);
}
if opts.queue {
let account_compression_accounts = client
.get_program_accounts(&account_compression::ID)
.expect("Failed to fetch accounts for account compression program.");
for (pubkey, mut account) in account_compression_accounts {
match account.data()[0..8].try_into().unwrap() {
QueueAccount::DISCRIMINATOR => {
unsafe {
let queue = HashSet::from_bytes_copy(
&mut account.data[8 + std::mem::size_of::<QueueAccount>()..],
)
.unwrap();
let config = Arc::new(ForesterConfig::new_for_status(opts).unwrap());

println!("Queue account: {:?}", pubkey);
let mut num_of_marked_items = 0;
for i in 0..queue.get_capacity() {
if queue.get_unmarked_bucket(i).is_some() {
num_of_marked_items += 1;
}
}
println!(
"queue num of unmarked items: {:?} / {}",
num_of_marked_items,
queue.get_capacity() / 2 // div by 2 because only half of the hash set can be used before tx start to fail
);
}
}
StateMerkleTreeAccount::DISCRIMINATOR => {
println!("State Merkle tree: {:?}", pubkey);
let merkle_tree = ConcurrentMerkleTreeCopy::<Poseidon, 26>::from_bytes_copy(
&account.data[8 + std::mem::size_of::<StateMerkleTreeAccount>()..],
)
.unwrap();
println!(
"State Merkle tree next index {:?}",
merkle_tree.next_index()
);
}
AddressMerkleTreeAccount::DISCRIMINATOR => {
println!("Address Merkle tree: {:?}", pubkey);
let merkle_tree = ConcurrentMerkleTreeCopy::<Poseidon, 26>::from_bytes_copy(
&account.data[8 + std::mem::size_of::<AddressMerkleTreeAccount>()..],
)
.unwrap();
println!(
"Address Merkle tree next index {:?}",
merkle_tree.next_index()
);
}
_ => (),
}
}
if config.general_config.enable_metrics {
register_metrics();
}

debug!("Fetching trees...");
debug!("RPC URL: {}", config.external_services.rpc_url);
let rpc = SolanaRpcConnection::new(config.external_services.rpc_url.clone(), None);
let trees = fetch_trees(&rpc).await;
if trees.is_empty() {
warn!("No trees found. Exiting.");
}
run_queue_info(config.clone(), trees.clone(), TreeType::State).await;
run_queue_info(config.clone(), trees.clone(), TreeType::Address).await;

push_metrics(&config.external_services.pushgateway_url)
.await
.unwrap();
}
2 changes: 1 addition & 1 deletion forester/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub mod cli;
pub mod config;
pub mod epoch_manager;
pub mod errors;
pub mod forester_stats;
pub mod forester_status;
pub mod metrics;
pub mod photon_indexer;
pub mod pubsub_client;
Expand Down
27 changes: 4 additions & 23 deletions forester/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
use clap::Parser;
use forester::cli::{Cli, Commands};
use forester::errors::ForesterError;
use forester::metrics::{push_metrics, register_metrics};
use forester::metrics::register_metrics;
use forester::photon_indexer::PhotonIndexer;
use forester::telemetry::setup_telemetry;
use forester::tree_data_sync::fetch_trees;
use forester::{forester_stats, run_pipeline, run_queue_info, ForesterConfig};
use forester_utils::forester_epoch::TreeType;
use forester::{forester_status, run_pipeline, ForesterConfig};
use light_client::rpc::{RpcConnection, SolanaRpcConnection};
use std::sync::Arc;
use tokio::signal::ctrl_c;
use tokio::sync::{mpsc, oneshot};
use tracing::{debug, warn};
use tracing::debug;

#[tokio::main]
async fn main() -> Result<(), ForesterError> {
Expand Down Expand Up @@ -54,25 +52,8 @@ async fn main() -> Result<(), ForesterError> {
run_pipeline(config, indexer, shutdown_receiver, work_report_sender).await?
}
Commands::Status(args) => {
let config = Arc::new(ForesterConfig::new_for_status(args)?);

if config.general_config.enable_metrics {
register_metrics();
}

debug!("Fetching trees...");
debug!("RPC URL: {}", config.external_services.rpc_url);
let rpc = SolanaRpcConnection::new(config.external_services.rpc_url.clone(), None);
let trees = fetch_trees(&rpc).await;
if trees.is_empty() {
warn!("No trees found. Exiting.");
}
run_queue_info(config.clone(), trees.clone(), TreeType::State).await;
run_queue_info(config.clone(), trees.clone(), TreeType::Address).await;

push_metrics(&config.external_services.pushgateway_url).await?;
forester_status::fetch_foreter_status(args).await;
}
Commands::ForesterStats(opts) => forester_stats::fetch_foreter_stats(opts),
}
Ok(())
}
Loading

0 comments on commit 515ce62

Please sign in to comment.