Skip to content
Snippets Groups Projects
blockchain.rs 1.49 KiB
Newer Older
/// define blockchain subcommands
#[derive(Clone, Default, Debug, clap::Parser)]
pub enum Subcommand {
	#[clap(hide = true)]
	Repart {
		// Number of transactions per block to target
		target: u32,
		#[clap(short = 'o', long = "old-repart")]
		// Old/actual repartition
		actual_repart: Option<u32>,
	},
	#[clap(hide = true)]
	SpamRoll { actual_repart: usize },
	/// Get information about runtime
	RuntimeInfo,
	/// Check current block
	#[default]
	CurrentBlock,
}

/// handle blockchain commands
pub async fn handle_command(data: Data, command: Subcommand) -> anyhow::Result<()> {
	let mut data = data.build_client().await?.build_indexer().await?;
	match command {
		Subcommand::Repart {
			target,
			actual_repart,
		} => {
			commands::net_test::repart(&data, target, actual_repart).await?
		}
		Subcommand::SpamRoll { actual_repart } => {
			commands::net_test::spam_roll(&data, actual_repart).await?
		}
		Subcommand::RuntimeInfo => {
			data = data.fetch_system_properties().await?;
			commands::runtime::runtime_info(data).await;
		}
		Subcommand::CurrentBlock => {
			println!(
				"current block on {}: {}",
				data.cfg.duniter_endpoint,
				data.client()
					.storage()
					.fetch(&runtime::storage().system().number(), None)
					.await?
					.unwrap()
			);
		}
	}
	Ok(())
}

/// get genesis hash
pub async fn fetch_genesis_hash(data: &Data) -> Result<Hash, anyhow::Error> {
	Ok(data
		.client()
		.storage()
		.fetch(&runtime::storage().system().block_hash(0), None)
		.await?
		.unwrap())
}