Using clap parser for cli
This commit is contained in:
parent
ea2fbe09ca
commit
5d05abebde
File diff suppressed because it is too large
Load Diff
54
Cargo.toml
54
Cargo.toml
|
@ -6,32 +6,32 @@ edition = "2021"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
solana-client = { git = "https://github.com/blockworks-foundation/solana.git" }
|
||||
solana-sdk = { git = "https://github.com/blockworks-foundation/solana.git" }
|
||||
solana-clap-utils = { git = "https://github.com/blockworks-foundation/solana.git" }
|
||||
solana-cli-config = { git = "https://github.com/blockworks-foundation/solana.git" }
|
||||
solana-pubsub-client = { git = "https://github.com/blockworks-foundation/solana.git" }
|
||||
solana-account-decoder = { git = "https://github.com/blockworks-foundation/solana.git" }
|
||||
solana-entry = { git = "https://github.com/blockworks-foundation/solana.git" }
|
||||
solana-faucet = { git = "https://github.com/blockworks-foundation/solana.git" }
|
||||
solana-gossip = { git = "https://github.com/blockworks-foundation/solana.git" }
|
||||
solana-ledger = { git = "https://github.com/blockworks-foundation/solana.git" }
|
||||
solana-measure = { git = "https://github.com/blockworks-foundation/solana.git" }
|
||||
solana-metrics = { git = "https://github.com/blockworks-foundation/solana.git" }
|
||||
solana-perf = { git = "https://github.com/blockworks-foundation/solana.git" }
|
||||
solana-poh = { git = "https://github.com/blockworks-foundation/solana.git" }
|
||||
solana-rayon-threadlimit = { git = "https://github.com/blockworks-foundation/solana.git" }
|
||||
solana-rpc-client-api = { git = "https://github.com/blockworks-foundation/solana.git" }
|
||||
solana-runtime = { git = "https://github.com/blockworks-foundation/solana.git" }
|
||||
solana-send-transaction-service = { git = "https://github.com/blockworks-foundation/solana.git" }
|
||||
solana-stake-program = { git = "https://github.com/blockworks-foundation/solana.git" }
|
||||
solana-storage-bigtable = { git = "https://github.com/blockworks-foundation/solana.git" }
|
||||
solana-streamer = { git = "https://github.com/blockworks-foundation/solana.git" }
|
||||
solana-tpu-client = { git = "https://github.com/blockworks-foundation/solana.git" }
|
||||
solana-transaction-status = { git = "https://github.com/blockworks-foundation/solana.git" }
|
||||
solana-version = { git = "https://github.com/blockworks-foundation/solana.git" }
|
||||
solana-vote-program = { git = "https://github.com/blockworks-foundation/solana.git" }
|
||||
solana-rpc = { git = "https://github.com/blockworks-foundation/solana.git" }
|
||||
solana-client = { git = "https://github.com/solana-labs/solana.git" }
|
||||
solana-sdk = { git = "https://github.com/solana-labs/solana.git" }
|
||||
solana-clap-utils = { git = "https://github.com/solana-labs/solana.git" }
|
||||
solana-cli-config = { git = "https://github.com/solana-labs/solana.git" }
|
||||
solana-pubsub-client = { git = "https://github.com/solana-labs/solana.git" }
|
||||
solana-account-decoder = { git = "https://github.com/solana-labs/solana.git" }
|
||||
solana-entry = { git = "https://github.com/solana-labs/solana.git" }
|
||||
solana-faucet = { git = "https://github.com/solana-labs/solana.git" }
|
||||
solana-gossip = { git = "https://github.com/solana-labs/solana.git" }
|
||||
solana-ledger = { git = "https://github.com/solana-labs/solana.git" }
|
||||
solana-measure = { git = "https://github.com/solana-labs/solana.git" }
|
||||
solana-metrics = { git = "https://github.com/solana-labs/solana.git" }
|
||||
solana-perf = { git = "https://github.com/solana-labs/solana.git" }
|
||||
solana-poh = { git = "https://github.com/solana-labs/solana.git" }
|
||||
solana-rayon-threadlimit = { git = "https://github.com/solana-labs/solana.git" }
|
||||
solana-rpc-client-api = { git = "https://github.com/solana-labs/solana.git" }
|
||||
solana-runtime = { git = "https://github.com/solana-labs/solana.git" }
|
||||
solana-send-transaction-service = { git = "https://github.com/solana-labs/solana.git" }
|
||||
solana-stake-program = { git = "https://github.com/solana-labs/solana.git" }
|
||||
solana-storage-bigtable = { git = "https://github.com/solana-labs/solana.git" }
|
||||
solana-streamer = { git = "https://github.com/solana-labs/solana.git" }
|
||||
solana-tpu-client = { git = "https://github.com/solana-labs/solana.git" }
|
||||
solana-transaction-status = { git = "https://github.com/solana-labs/solana.git" }
|
||||
solana-version = { git = "https://github.com/solana-labs/solana.git" }
|
||||
solana-vote-program = { git = "https://github.com/solana-labs/solana.git" }
|
||||
solana-rpc = { git = "https://github.com/solana-labs/solana.git" }
|
||||
|
||||
tokio = { version = "1.14.1", features = ["full"]}
|
||||
futures = "0.3.25"
|
||||
|
@ -40,7 +40,7 @@ jsonrpc-core-client = { version = "18.0.0" }
|
|||
jsonrpc-derive = "18.0.0"
|
||||
jsonrpc-http-server = "18.0.0"
|
||||
jsonrpc-pubsub = "18.0.0"
|
||||
clap = "2.33.1"
|
||||
clap = { version = "4.0.29", features = ["derive"] }
|
||||
|
||||
base64 = "0.13.0"
|
||||
bincode = "1.3.3"
|
||||
|
|
131
src/cli.rs
131
src/cli.rs
|
@ -1,106 +1,43 @@
|
|||
use {
|
||||
clap::{App, Arg, ArgMatches},
|
||||
solana_clap_utils::input_validators::{is_url, is_url_or_moniker},
|
||||
solana_cli_config::ConfigInput,
|
||||
std::net::SocketAddr,
|
||||
};
|
||||
use {clap::Parser, solana_cli_config::ConfigInput, std::net::SocketAddr};
|
||||
|
||||
/// Holds the configuration for a single run of the benchmark
|
||||
pub struct Config {
|
||||
pub rpc_addr: SocketAddr,
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(
|
||||
version,
|
||||
about = "A lite version of solana rpc to send and confirm transactions.",
|
||||
long_about = "Lite rpc is optimized to send and confirm transactions for solana blockchain. \
|
||||
When it recieves a transaction it will directly send it to next few leaders. It then adds the signature into internal map. It listen to block subscriptions for confirmed and finalized blocks. \
|
||||
It also has a websocket port for subscription to onSlotChange and onSignature subscriptions. \
|
||||
"
|
||||
)]
|
||||
pub struct Args {
|
||||
#[arg(short, long, default_value_t = SocketAddr::from(([127, 0, 0, 1], 8899)))]
|
||||
pub port: SocketAddr,
|
||||
#[arg(short, long, default_value_t = SocketAddr::from(([127, 0, 0, 1], 8900)))]
|
||||
pub subscription_port: SocketAddr,
|
||||
pub json_rpc_url: String,
|
||||
#[arg(short, long, default_value_t = String::new())]
|
||||
pub rpc_url: String,
|
||||
#[arg(short, long, default_value_t = String::new())]
|
||||
pub websocket_url: String,
|
||||
}
|
||||
|
||||
impl Default for Config {
|
||||
fn default() -> Config {
|
||||
Config {
|
||||
rpc_addr: SocketAddr::from(([127, 0, 0, 1], 8899)),
|
||||
json_rpc_url: ConfigInput::default().json_rpc_url,
|
||||
websocket_url: ConfigInput::default().websocket_url,
|
||||
subscription_port: SocketAddr::from(([127, 0, 0, 1], 8900)),
|
||||
impl Args {
|
||||
pub fn resolve_address(&mut self) {
|
||||
if self.rpc_url.is_empty() {
|
||||
let (_, rpc_url) = ConfigInput::compute_json_rpc_url_setting(
|
||||
self.rpc_url.as_str(),
|
||||
&ConfigInput::default().json_rpc_url,
|
||||
);
|
||||
self.rpc_url = rpc_url;
|
||||
}
|
||||
if self.websocket_url.is_empty() {
|
||||
let (_, ws_url) = ConfigInput::compute_websocket_url_setting(
|
||||
&self.websocket_url.as_str(),
|
||||
"",
|
||||
self.rpc_url.as_str(),
|
||||
"",
|
||||
);
|
||||
self.websocket_url = ws_url;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Defines and builds the CLI args for a run of the benchmark
|
||||
pub fn build_args<'a, 'b>(version: &'b str) -> App<'a, 'b> {
|
||||
App::new("lite rpc")
|
||||
.about("a lite version of solana rpc to send and confirm transactions")
|
||||
.version(version)
|
||||
.arg(
|
||||
Arg::with_name("json-rpc-url")
|
||||
.short("u")
|
||||
.long("url")
|
||||
.value_name("URL_OR_MONIKER")
|
||||
.takes_value(true)
|
||||
.global(true)
|
||||
.validator(is_url_or_moniker)
|
||||
.help(
|
||||
"URL for Solana's JSON RPC or moniker (or their first letter): \
|
||||
[mainnet-beta, testnet, devnet, localhost]",
|
||||
),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("websocket-url")
|
||||
.long("ws")
|
||||
.value_name("URL")
|
||||
.takes_value(true)
|
||||
.global(true)
|
||||
.validator(is_url)
|
||||
.help("WebSocket URL for the solana cluster"),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("port")
|
||||
.long("port")
|
||||
.short("p")
|
||||
.takes_value(true)
|
||||
.global(true)
|
||||
.help("Port on which which lite rpc will listen to rpc requests"),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("subscription-port")
|
||||
.long("subscription-port")
|
||||
.short("sp")
|
||||
.takes_value(true)
|
||||
.global(true)
|
||||
.help("subscription port on which which lite rpc will use to create subscriptions"),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn extract_args(matches: &ArgMatches) -> Config {
|
||||
let mut args = Config::default();
|
||||
|
||||
let config = if let Some(config_file) = matches.value_of("config_file") {
|
||||
solana_cli_config::Config::load(config_file).unwrap_or_default()
|
||||
} else {
|
||||
solana_cli_config::Config::default()
|
||||
};
|
||||
let (_, json_rpc_url) = ConfigInput::compute_json_rpc_url_setting(
|
||||
matches.value_of("json-rpc-url").unwrap_or(""),
|
||||
&config.json_rpc_url,
|
||||
);
|
||||
args.json_rpc_url = json_rpc_url;
|
||||
|
||||
let (_, websocket_url) = ConfigInput::compute_websocket_url_setting(
|
||||
matches.value_of("websocket-url").unwrap_or(""),
|
||||
&config.websocket_url,
|
||||
matches.value_of("json-rpc-url").unwrap_or(""),
|
||||
&config.json_rpc_url,
|
||||
);
|
||||
args.websocket_url = websocket_url;
|
||||
if let Some(port) = matches.value_of("port") {
|
||||
let port: u16 = port.parse().expect("can't parse port");
|
||||
args.rpc_addr = SocketAddr::from(([127, 0, 0, 1], port));
|
||||
}
|
||||
|
||||
if let Some(port) = matches.value_of("subscription-port") {
|
||||
let port: u16 = port.parse().expect("can't parse subscription-port");
|
||||
args.subscription_port = SocketAddr::from(([127, 0, 0, 1], port));
|
||||
} else {
|
||||
let port = args.rpc_addr.port().saturating_add(1);
|
||||
args.subscription_port = SocketAddr::from(([127, 0, 0, 1], port));
|
||||
}
|
||||
args
|
||||
}
|
||||
|
|
18
src/main.rs
18
src/main.rs
|
@ -1,5 +1,6 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use clap::Parser;
|
||||
use context::LiteRpcSubsrciptionControl;
|
||||
use jsonrpc_core::MetaIoHandler;
|
||||
use jsonrpc_http_server::{hyper, AccessControlAllowOrigin, DomainsValidation, ServerBuilder};
|
||||
|
@ -16,14 +17,19 @@ mod context;
|
|||
mod pubsub;
|
||||
mod rpc;
|
||||
|
||||
pub fn main() {
|
||||
let matches = cli::build_args(solana_version::version!()).get_matches();
|
||||
let cli_config = cli::extract_args(&matches);
|
||||
use cli::Args;
|
||||
|
||||
let cli::Config {
|
||||
json_rpc_url,
|
||||
pub fn main() {
|
||||
let mut cli_config = Args::parse();
|
||||
cli_config.resolve_address();
|
||||
println!(
|
||||
"Using rpc server {} and ws server {}",
|
||||
cli_config.rpc_url, cli_config.websocket_url
|
||||
);
|
||||
let Args {
|
||||
rpc_url: json_rpc_url,
|
||||
websocket_url,
|
||||
rpc_addr,
|
||||
port: rpc_addr,
|
||||
subscription_port,
|
||||
..
|
||||
} = &cli_config;
|
||||
|
|
Loading…
Reference in New Issue