Add more arg in command line arguments (#46)

This commit is contained in:
Sydhds
2025-10-01 17:24:28 +02:00
committed by GitHub
parent b7967b85e8
commit e1c96b3ef7
5 changed files with 106 additions and 25 deletions

View File

@@ -186,6 +186,9 @@ fn proof_generation_bench(c: &mut Criterion) {
proof_service_count,
transaction_channel_size: 500,
proof_sender_channel_size: 500,
registration_min_amount: AppArgs::default_minimal_amount_for_registration(),
rln_identifier: AppArgs::default_rln_identifier_name(),
spam_limit: 1_000_000u64,
};
// Tokio notify - wait for some time after spawning run_prover then notify it's ready to accept

View File

@@ -146,10 +146,13 @@ fn proof_generation_bench(c: &mut Criterion) {
no_config: true,
metrics_ip: IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)),
metrics_port: 30051,
broadcast_channel_size: 100,
broadcast_channel_size: 500,
proof_service_count,
transaction_channel_size: 100,
proof_sender_channel_size: 100,
transaction_channel_size: 500,
proof_sender_channel_size: 500,
registration_min_amount: AppArgs::default_minimal_amount_for_registration(),
rln_identifier: AppArgs::default_rln_identifier_name(),
spam_limit: 1_000_000u64,
};
// Tokio notify - wait for some time after spawning run_prover then notify it's ready to accept

View File

@@ -1,9 +1,14 @@
use std::net::IpAddr;
use std::path::PathBuf;
use std::str::FromStr;
// third-party
use alloy::primitives::Address;
use alloy::primitives::ruint::ParseError;
use alloy::primitives::{Address, U256};
use chrono::{DateTime, Utc};
use clap::Parser;
use clap_config::ClapConfig;
use derive_more::Display;
use serde::{Deserialize, Serialize};
use url::Url;
/// Broadcast channel size
@@ -28,6 +33,12 @@ const ARGS_DEFAULT_TRANSACTION_CHANNEL_SIZE: &str = "256";
/// the broadcast channel.
const ARGS_DEFAULT_PROOF_SENDER_CHANNEL_SIZE: &str = "100";
const ARGS_DEFAULT_RLN_IDENTIFIER_NAME: &str = "test-rln-identifier";
const ARGS_DEFAULT_PROVER_SPAM_LIMIT: u64 = 10_000_u64;
pub const ARGS_DEFAULT_GENESIS: DateTime<Utc> = DateTime::from_timestamp(1431648000, 0).unwrap();
const ARGS_DEFAULT_PROVER_MINIMAL_AMOUNT_FOR_REGISTRATION: WrappedU256 =
WrappedU256(U256::from_le_slice(10u64.to_le_bytes().as_slice()));
#[derive(Debug, Clone, Parser, ClapConfig)]
#[command(about = "RLN prover service", long_about = None)]
pub struct AppArgs {
@@ -58,21 +69,24 @@ pub struct AppArgs {
short = 'k',
long = "ksc",
default_value = "0x011b9de308BE357BbF24EfB387a270a14A04E5d2",
help = "Karma smart contract address"
help = "Karma smart contract address",
help_heading = "smart contract"
)]
pub ksc_address: Option<Address>,
#[arg(
short = 'r',
long = "rlnsc",
default_value = "0xc98994691E96D2f4CA2a718Bc8FDF30bd21d1c59",
help = "RLN smart contract address"
help = "RLN smart contract address",
help_heading = "smart contract"
)]
pub rlnsc_address: Option<Address>,
#[arg(
short = 't',
long = "tsc",
default_value = "0x011b9de308BE357BbF24EfB387a270a14A04E5d2",
help = "KarmaTiers smart contract address"
help = "KarmaTiers smart contract address",
help_heading = "smart contract"
)]
pub tsc_address: Option<Address>,
#[arg(
@@ -94,7 +108,7 @@ pub struct AppArgs {
long = "config",
help = "Config file path",
default_value = "./config.toml",
help_heading = "config"
help_heading = "config file"
)]
pub config_path: PathBuf,
#[arg(
@@ -102,17 +116,47 @@ pub struct AppArgs {
help = "Dont read a config file",
required = false,
action,
help_heading = "Do not try to read config file"
help_heading = "config file"
)]
pub no_config: bool,
#[arg(
long = "metrics-ip",
default_value = "::1",
help = "Prometheus Metrics ip"
help = "Prometheus Metrics ip",
help_heading = "monitoring"
)]
pub metrics_ip: IpAddr,
#[arg(long = "metrics-port", default_value = "30031", help = "Metrics port")]
#[arg(
long = "metrics-port",
default_value = "30031",
help = "Metrics port",
help_heading = "monitoring"
)]
pub metrics_port: u16,
#[arg(
help_heading = "RLN",
long = "rln-identifier",
default_value_t = AppArgs::default_rln_identifier_name(),
help = "RLN identifier name"
)]
pub rln_identifier: String,
#[arg(
help_heading = "RLN",
long = "spam-limit",
help = "RLN spam limit",
default_value_t = AppArgs::default_spam_limit(),
)]
pub spam_limit: u64,
#[arg(
help_heading = "prover config",
long = "registration-min",
help = "Minimal amount of Karma to register a user in the prover",
default_value_t = AppArgs::default_minimal_amount_for_registration(),
)]
pub registration_min_amount: WrappedU256,
// Hidden option - expect user set it via a config file
#[arg(
long = "broadcast-channel-size",
@@ -144,6 +188,40 @@ pub struct AppArgs {
pub proof_sender_channel_size: usize,
}
impl AppArgs {
pub fn default_spam_limit() -> u64 {
ARGS_DEFAULT_PROVER_SPAM_LIMIT
}
pub fn default_genesis() -> DateTime<Utc> {
ARGS_DEFAULT_GENESIS
}
pub fn default_minimal_amount_for_registration() -> WrappedU256 {
ARGS_DEFAULT_PROVER_MINIMAL_AMOUNT_FOR_REGISTRATION
}
pub fn default_rln_identifier_name() -> String {
ARGS_DEFAULT_RLN_IDENTIFIER_NAME.to_string()
}
}
#[derive(Debug, Clone, Serialize, Deserialize, Display)]
pub struct WrappedU256(U256);
impl WrappedU256 {
pub fn to_u256(&self) -> U256 {
self.0
}
}
impl FromStr for WrappedU256 {
type Err = ParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(WrappedU256(U256::from_str(s)?))
}
}
#[cfg(test)]
mod tests {
use super::*;
@@ -159,6 +237,8 @@ mod tests {
..Default::default()
};
println!("config: {:?}", config);
{
let args_1 = vec!["program", "--ip", "127.0.0.1", "--port", "50051"];
let cmd = <AppArgs as CommandFactory>::command();

View File

@@ -27,15 +27,13 @@ use std::net::SocketAddr;
use std::str::FromStr;
use std::time::Duration;
// third-party
use alloy::primitives::U256;
use alloy::providers::{ProviderBuilder, WsConnect};
use alloy::signers::local::PrivateKeySigner;
use chrono::{DateTime, Utc};
use tokio::task::JoinSet;
use tracing::{debug, info};
use zeroize::Zeroizing;
// internal
pub use crate::args::{AppArgs, AppArgsConfig};
pub use crate::args::{ARGS_DEFAULT_GENESIS, AppArgs, AppArgsConfig};
use crate::epoch_service::EpochService;
use crate::error::AppError;
use crate::grpc_service::GrpcProverService;
@@ -52,15 +50,9 @@ use rln_proof::RlnIdentifier;
use smart_contract::KarmaTiers::KarmaTiersInstance;
use smart_contract::{KarmaTiersError, TIER_LIMITS};
const RLN_IDENTIFIER_NAME: &[u8] = b"test-rln-identifier";
const PROVER_SPAM_LIMIT: RateLimit = RateLimit::new(10_000u64);
const GENESIS: DateTime<Utc> = DateTime::from_timestamp(1431648000, 0).unwrap();
const PROVER_MINIMAL_AMOUNT_FOR_REGISTRATION: U256 =
U256::from_le_slice(10u64.to_le_bytes().as_slice());
pub async fn run_prover(app_args: AppArgs) -> Result<(), AppError> {
// Epoch
let epoch_service = EpochService::try_from((Duration::from_secs(60 * 2), GENESIS))
let epoch_service = EpochService::try_from((Duration::from_secs(60 * 2), ARGS_DEFAULT_GENESIS))
.expect("Failed to create epoch service");
// Alloy provider (Smart contract provider)
@@ -115,7 +107,7 @@ pub async fn run_prover(app_args: AppArgs) -> Result<(), AppError> {
app_args.merkle_tree_path.clone(),
epoch_service.epoch_changes.clone(),
epoch_service.current_epoch.clone(),
PROVER_SPAM_LIMIT,
RateLimit::new(app_args.spam_limit),
tier_limits,
)?;
@@ -155,7 +147,7 @@ pub async fn run_prover(app_args: AppArgs) -> Result<(), AppError> {
app_args.ksc_address.unwrap(),
app_args.rlnsc_address.unwrap(),
user_db_service.get_user_db(),
PROVER_MINIMAL_AMOUNT_FOR_REGISTRATION,
app_args.registration_min_amount.to_u256(),
))
};
@@ -174,7 +166,7 @@ pub async fn run_prover(app_args: AppArgs) -> Result<(), AppError> {
// grpc
let rln_identifier = RlnIdentifier::new(RLN_IDENTIFIER_NAME);
let rln_identifier = RlnIdentifier::new(app_args.rln_identifier.as_bytes());
let addr = SocketAddr::new(app_args.ip, app_args.port);
info!("Listening on: {}", addr);
let prover_grpc_service = {
@@ -209,7 +201,7 @@ pub async fn run_prover(app_args: AppArgs) -> Result<(), AppError> {
broadcast_sender,
current_epoch,
user_db,
PROVER_SPAM_LIMIT,
RateLimit::new(app_args.spam_limit),
u64::from(i),
);
proof_service.serve().await

View File

@@ -252,6 +252,9 @@ async fn test_grpc_gen_proof() {
proof_service_count: 8,
transaction_channel_size: 500,
proof_sender_channel_size: 500,
registration_min_amount: AppArgs::default_minimal_amount_for_registration(),
rln_identifier: AppArgs::default_rln_identifier_name(),
spam_limit: AppArgs::default_spam_limit(),
};
info!("Starting prover with args: {:?}", app_args);