Add ban/unban in grin client (#620)

* Added ban/unban in grin client
This commit is contained in:
Quentin Le Sceller 2018-01-15 20:44:03 -05:00 committed by Ignotus Peverell
parent 9704cc35bd
commit f067e142f7
2 changed files with 111 additions and 51 deletions

View file

@ -14,12 +14,13 @@
extern crate term; extern crate term;
use std::net::SocketAddr;
use api; use api;
use grin::ServerConfig; use grin::ServerConfig;
pub fn show_status(config: &ServerConfig) { pub fn show_status(config: &ServerConfig) {
println!(); println!();
let title = format!("Grin Server Status "); let title = format!("Grin Server Status");
let mut t = term::stdout().unwrap(); let mut t = term::stdout().unwrap();
let mut e = term::stdout().unwrap(); let mut e = term::stdout().unwrap();
t.fg(term::color::MAGENTA).unwrap(); t.fg(term::color::MAGENTA).unwrap();
@ -45,6 +46,35 @@ pub fn show_status(config: &ServerConfig) {
println!(); println!();
} }
pub fn ban_peer(config: &ServerConfig, peer_addr: &SocketAddr) {
let params = "";
let mut e = term::stdout().unwrap();
let url = format!(
"http://{}/v1/peers/{}/ban",
config.api_http_addr,
peer_addr.to_string()
);
match api::client::post(url.as_str(), &params).map_err(|e| Error::API(e)) {
Ok(_) => writeln!(e, "Successfully banned peer {}", peer_addr.to_string()).unwrap(),
Err(_) => writeln!(e, "Failed to ban peer {}", peer_addr).unwrap(),
};
e.reset().unwrap();
}
pub fn unban_peer(config: &ServerConfig, peer_addr: &SocketAddr) {
let params = "";
let mut e = term::stdout().unwrap();
let url = format!(
"http://{}/v1/peers/{}/unban",
config.api_http_addr,
peer_addr.to_string()
);
match api::client::post(url.as_str(), &params).map_err(|e| Error::API(e)) {
Ok(_) => writeln!(e, "Successfully unbanned peer {}", peer_addr).unwrap(),
Err(_) => writeln!(e, "Failed to unban peer {}", peer_addr).unwrap(),
};
}
fn get_status_from_node(config: &ServerConfig) -> Result<api::Status, Error> { fn get_status_from_node(config: &ServerConfig) -> Result<api::Status, Error> {
let url = format!("http://{}/v1/status", config.api_http_addr); let url = format!("http://{}/v1/status", config.api_http_addr);
api::client::get::<api::Status>(url.as_str()).map_err(|e| Error::API(e)) api::client::get::<api::Status>(url.as_str()).map_err(|e| Error::API(e))

View file

@ -151,7 +151,22 @@ fn main() {
.subcommand(SubCommand::with_name("client") .subcommand(SubCommand::with_name("client")
.about("Communicates with the Grin server") .about("Communicates with the Grin server")
.subcommand(SubCommand::with_name("status") .subcommand(SubCommand::with_name("status")
.about("current status of the Grin chain"))) .about("current status of the Grin chain"))
.subcommand(SubCommand::with_name("ban")
.about("Ban peer")
.arg(Arg::with_name("peer")
.short("p")
.long("peer")
.help("Peer ip and port (e.g. 10.12.12.13:13414)")
.takes_value(true)))
.subcommand(SubCommand::with_name("unban")
.about("Unban peer")
.arg(Arg::with_name("peer")
.short("p")
.long("peer")
.help("Peer ip and port (e.g. 10.12.12.13:13414)")
.takes_value(true))))
// specification of the wallet commands and options // specification of the wallet commands and options
.subcommand(SubCommand::with_name("wallet") .subcommand(SubCommand::with_name("wallet")
@ -370,6 +385,24 @@ fn client_command(client_args: &ArgMatches, global_config: GlobalConfig) {
("status", Some(_)) => { ("status", Some(_)) => {
client::show_status(&server_config); client::show_status(&server_config);
} }
("ban", Some(peer_args)) => {
if let Some(peer) = peer_args.value_of("peer") {
if let Ok(addr) = peer.parse() {
client::ban_peer(&server_config, &addr);
} else {
panic!("Invalid peer address format");
}
}
}
("unban", Some(peer_args)) => {
if let Some(peer) = peer_args.value_of("peer") {
if let Ok(addr) = peer.parse() {
client::unban_peer(&server_config, &addr);
} else {
panic!("Invalid peer address format");
}
}
}
_ => panic!("Unknown client command, use 'grin help client' for details"), _ => panic!("Unknown client command, use 'grin help client' for details"),
} }
} }
@ -411,12 +444,12 @@ fn wallet_command(wallet_args: &ArgMatches, global_config: GlobalConfig) {
let wallet_seed = let wallet_seed =
wallet::WalletSeed::from_file(&wallet_config).expect("Failed to read wallet seed file."); wallet::WalletSeed::from_file(&wallet_config).expect("Failed to read wallet seed file.");
let passphrase = wallet_args.value_of("pass").expect( let passphrase = wallet_args
"Failed to read passphrase.", .value_of("pass")
); .expect("Failed to read passphrase.");
let mut keychain = wallet_seed.derive_keychain(&passphrase).expect( let mut keychain = wallet_seed
"Failed to derive keychain from seed file and passphrase.", .derive_keychain(&passphrase)
); .expect("Failed to derive keychain from seed file and passphrase.");
match wallet_args.subcommand() { match wallet_args.subcommand() {
("listen", Some(listen_args)) => { ("listen", Some(listen_args)) => {
@ -445,24 +478,22 @@ fn wallet_command(wallet_args: &ArgMatches, global_config: GlobalConfig) {
} }
}*/ }*/
("send", Some(send_args)) => { ("send", Some(send_args)) => {
let amount = send_args.value_of("amount").expect( let amount = send_args
"Amount to send required", .value_of("amount")
); .expect("Amount to send required");
let amount = core::core::amount_from_hr_string(amount).expect( let amount = core::core::amount_from_hr_string(amount)
"Could not parse amount as a number with optional decimal point.", .expect("Could not parse amount as a number with optional decimal point.");
); let minimum_confirmations: u64 = send_args
let minimum_confirmations: u64 =
send_args
.value_of("minimum_confirmations") .value_of("minimum_confirmations")
.unwrap() .unwrap()
.parse() .parse()
.expect("Could not parse minimum_confirmations as a whole number."); .expect("Could not parse minimum_confirmations as a whole number.");
let selection_strategy = send_args.value_of("selection_strategy").expect( let selection_strategy = send_args
"Selection strategy required", .value_of("selection_strategy")
); .expect("Selection strategy required");
let dest = send_args.value_of("dest").expect( let dest = send_args
"Destination wallet address required", .value_of("dest")
); .expect("Destination wallet address required");
let max_outputs = 500; let max_outputs = 500;
let result = wallet::issue_send_tx( let result = wallet::issue_send_tx(
&wallet_config, &wallet_config,
@ -474,15 +505,13 @@ fn wallet_command(wallet_args: &ArgMatches, global_config: GlobalConfig) {
(selection_strategy == "all"), (selection_strategy == "all"),
); );
match result { match result {
Ok(_) => { Ok(_) => info!(
info!(
LOGGER, LOGGER,
"Tx sent: {} grin to {} (strategy '{}')", "Tx sent: {} grin to {} (strategy '{}')",
amount_to_hr_string(amount), amount_to_hr_string(amount),
dest, dest,
selection_strategy, selection_strategy,
) ),
}
Err(wallet::Error::NotEnoughFunds(available)) => { Err(wallet::Error::NotEnoughFunds(available)) => {
error!( error!(
LOGGER, LOGGER,
@ -490,7 +519,10 @@ fn wallet_command(wallet_args: &ArgMatches, global_config: GlobalConfig) {
amount_to_hr_string(available), amount_to_hr_string(available),
); );
} }
Err(wallet::Error::FeeExceedsAmount {sender_amount, recipient_fee}) => { Err(wallet::Error::FeeExceedsAmount {
sender_amount,
recipient_fee,
}) => {
error!( error!(
LOGGER, LOGGER,
"Recipient rejected the transfer because transaction fee ({}) exceeded amount ({}).", "Recipient rejected the transfer because transaction fee ({}) exceeded amount ({}).",
@ -504,14 +536,12 @@ fn wallet_command(wallet_args: &ArgMatches, global_config: GlobalConfig) {
}; };
} }
("burn", Some(send_args)) => { ("burn", Some(send_args)) => {
let amount = send_args.value_of("amount").expect( let amount = send_args
"Amount to burn required", .value_of("amount")
); .expect("Amount to burn required");
let amount = core::core::amount_from_hr_string(amount).expect( let amount = core::core::amount_from_hr_string(amount)
"Could not parse amount as number with optional decimal point.", .expect("Could not parse amount as number with optional decimal point.");
); let minimum_confirmations: u64 = send_args
let minimum_confirmations: u64 =
send_args
.value_of("minimum_confirmations") .value_of("minimum_confirmations")
.unwrap() .unwrap()
.parse() .parse()