2020-03-27 22:55:55 -07:00
|
|
|
use clap::{
|
|
|
|
crate_description, crate_name, value_t, value_t_or_exit, App, AppSettings, Arg, SubCommand,
|
|
|
|
};
|
|
|
|
use console::Emoji;
|
|
|
|
use log::*;
|
|
|
|
use solana_clap_utils::{
|
|
|
|
input_parsers::pubkey_of,
|
|
|
|
input_validators::{is_pubkey, is_slot, is_url},
|
|
|
|
};
|
|
|
|
use solana_client::rpc_client::RpcClient;
|
|
|
|
use solana_metrics::datapoint_error;
|
2020-04-06 21:41:53 -07:00
|
|
|
use solana_sdk::{clock::Slot, native_token::lamports_to_sol, pubkey::Pubkey, system_program};
|
2020-03-27 22:55:55 -07:00
|
|
|
use solana_stake_monitor::*;
|
|
|
|
use std::{fs, io, process};
|
|
|
|
|
2020-04-06 21:41:53 -07:00
|
|
|
fn load_accounts_info(data_file: &str) -> AccountsInfo {
|
2020-03-27 22:55:55 -07:00
|
|
|
let data_file_new = data_file.to_owned() + "new";
|
2020-04-06 21:41:53 -07:00
|
|
|
let accounts_info = solana_cli_config::load_config_file(&data_file_new)
|
2020-03-27 22:55:55 -07:00
|
|
|
.or_else(|_| solana_cli_config::load_config_file(data_file))
|
|
|
|
.unwrap_or_default();
|
|
|
|
|
|
|
|
// Ensure `data_file` always exists
|
2020-04-06 21:41:53 -07:00
|
|
|
save_accounts_info(data_file, &accounts_info).expect("save_accounts_info");
|
2020-03-27 22:55:55 -07:00
|
|
|
|
2020-04-06 21:41:53 -07:00
|
|
|
accounts_info
|
2020-03-27 22:55:55 -07:00
|
|
|
}
|
|
|
|
|
2020-04-06 21:41:53 -07:00
|
|
|
fn save_accounts_info(data_file: &str, accounts_info: &AccountsInfo) -> io::Result<()> {
|
2020-03-27 22:55:55 -07:00
|
|
|
let data_file_new = data_file.to_owned() + "new";
|
2020-04-06 21:41:53 -07:00
|
|
|
solana_cli_config::save_config_file(&accounts_info, &data_file_new)?;
|
2020-03-27 22:55:55 -07:00
|
|
|
let _ = fs::remove_file(data_file);
|
|
|
|
fs::rename(&data_file_new, data_file)
|
|
|
|
}
|
|
|
|
|
2020-04-06 21:41:53 -07:00
|
|
|
fn command_record(data_file: &str, json_rpc_url: String, first_slot: Slot, batch_size: u64) {
|
|
|
|
let mut accounts_info = load_accounts_info(&data_file);
|
2020-03-27 22:55:55 -07:00
|
|
|
|
|
|
|
info!("RPC URL: {}", json_rpc_url);
|
|
|
|
let rpc_client = RpcClient::new(json_rpc_url);
|
2020-04-06 21:41:53 -07:00
|
|
|
if accounts_info.slot < first_slot {
|
|
|
|
accounts_info.slot = first_slot;
|
2020-03-27 22:55:55 -07:00
|
|
|
}
|
2020-04-06 21:41:53 -07:00
|
|
|
|
2020-03-27 22:55:55 -07:00
|
|
|
loop {
|
2020-04-06 21:41:53 -07:00
|
|
|
process_slots(&rpc_client, &mut accounts_info, batch_size);
|
|
|
|
save_accounts_info(data_file, &accounts_info).unwrap_or_else(|err| {
|
2020-03-27 22:55:55 -07:00
|
|
|
datapoint_error!(
|
|
|
|
"stake-monitor-failure",
|
|
|
|
(
|
|
|
|
"err",
|
2020-04-06 21:41:53 -07:00
|
|
|
format!("failed to save accounts_info: {}", err),
|
2020-03-27 22:55:55 -07:00
|
|
|
String
|
|
|
|
)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-06 21:41:53 -07:00
|
|
|
fn command_enroll(data_file: &str, json_rpc_url: String, account_address: &Pubkey) {
|
|
|
|
info!("RPC URL: {}", json_rpc_url);
|
|
|
|
let rpc_client = RpcClient::new(json_rpc_url);
|
|
|
|
let slot = rpc_client.get_slot().expect("get slot");
|
2020-03-27 22:55:55 -07:00
|
|
|
|
2020-04-06 21:41:53 -07:00
|
|
|
let account = rpc_client
|
|
|
|
.get_account(account_address)
|
|
|
|
.unwrap_or_else(|err| {
|
|
|
|
eprintln!(
|
|
|
|
"Unable to get account info for {}: {}",
|
|
|
|
account_address, err
|
|
|
|
);
|
|
|
|
process::exit(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
if account.owner != system_program::id() && !account.data.is_empty() {
|
|
|
|
eprintln!("{} is not a system account", account_address);
|
|
|
|
process::exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut accounts_info = load_accounts_info(data_file);
|
|
|
|
accounts_info.enroll_system_account(account_address, slot, account.lamports);
|
|
|
|
save_accounts_info(data_file, &accounts_info).unwrap();
|
|
|
|
println!(
|
|
|
|
"Enrolled {} at slot {} with a balance of {} SOL",
|
|
|
|
account_address,
|
|
|
|
slot,
|
|
|
|
lamports_to_sol(account.lamports)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn command_check(data_file: &str, account_address: &Pubkey) {
|
|
|
|
let accounts_info = load_accounts_info(data_file);
|
|
|
|
|
|
|
|
if let Some(account_info) = accounts_info.account_info.get(&account_address.to_string()) {
|
|
|
|
if let Some(slot) = account_info.compliant_since {
|
2020-03-27 22:55:55 -07:00
|
|
|
println!(
|
2020-04-06 21:41:53 -07:00
|
|
|
"{}Account compliant since slot {} with a balance of {} SOL",
|
2020-03-27 22:55:55 -07:00
|
|
|
Emoji("✅ ", ""),
|
|
|
|
slot,
|
2020-04-06 21:41:53 -07:00
|
|
|
lamports_to_sol(account_info.lamports)
|
2020-03-27 22:55:55 -07:00
|
|
|
);
|
|
|
|
process::exit(0);
|
|
|
|
} else {
|
|
|
|
eprintln!(
|
2020-04-06 21:41:53 -07:00
|
|
|
"{}Account not compliant due to: {:?}",
|
2020-03-27 22:55:55 -07:00
|
|
|
Emoji("❌ ", ""),
|
2020-04-06 21:41:53 -07:00
|
|
|
account_info.transactions.last().unwrap()
|
2020-03-27 22:55:55 -07:00
|
|
|
);
|
|
|
|
process::exit(1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
eprintln!("{} Unknown stake account", Emoji("⚠️ ", ""));
|
|
|
|
process::exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
solana_logger::setup_with_default("solana=info");
|
|
|
|
solana_metrics::set_panic_hook("stake-monitor");
|
|
|
|
|
|
|
|
let matches = App::new(crate_name!())
|
|
|
|
.about(crate_description!())
|
|
|
|
.version(solana_clap_utils::version!())
|
|
|
|
.setting(AppSettings::SubcommandRequiredElseHelp)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("data_file")
|
|
|
|
.long("data-file")
|
|
|
|
.value_name("PATH")
|
|
|
|
.takes_value(true)
|
|
|
|
.default_value("stake-info.yml")
|
|
|
|
.global(true)
|
|
|
|
.help(
|
|
|
|
"Output YAML file that receives the information for all stake accounts.\
|
|
|
|
This file is updated atomically after each batch of slots is processed.",
|
|
|
|
),
|
|
|
|
)
|
2020-04-06 21:41:53 -07:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("json_rpc_url")
|
|
|
|
.long("url")
|
|
|
|
.value_name("URL")
|
|
|
|
.takes_value(true)
|
|
|
|
.validator(is_url)
|
|
|
|
.help("JSON RPC URL for the cluster"),
|
|
|
|
)
|
|
|
|
.arg({
|
|
|
|
let arg = Arg::with_name("config_file")
|
|
|
|
.short("C")
|
|
|
|
.long("config")
|
|
|
|
.value_name("PATH")
|
|
|
|
.takes_value(true)
|
|
|
|
.help("Configuration file to use");
|
|
|
|
if let Some(ref config_file) = *solana_cli_config::CONFIG_FILE {
|
|
|
|
arg.default_value(&config_file)
|
|
|
|
} else {
|
|
|
|
arg
|
|
|
|
}
|
|
|
|
})
|
2020-03-27 22:55:55 -07:00
|
|
|
.subcommand(
|
|
|
|
SubCommand::with_name("record")
|
|
|
|
.about("Monitor all Cluster transactions for state account compliance")
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("first_slot")
|
|
|
|
.long("--first-slot")
|
|
|
|
.value_name("SLOT")
|
|
|
|
.validator(is_slot)
|
|
|
|
.takes_value(true)
|
|
|
|
.default_value("0")
|
|
|
|
.help("Don't process slots lower than this value"),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("batch_size")
|
|
|
|
.long("--batch-size")
|
|
|
|
.value_name("NUMBER")
|
|
|
|
.takes_value(true)
|
|
|
|
.default_value("10")
|
|
|
|
.help("Process up to this many slots in one batch"),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.subcommand(
|
|
|
|
SubCommand::with_name("check")
|
2020-04-06 21:41:53 -07:00
|
|
|
.about("Check if an account is in compliance")
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("account_address")
|
|
|
|
.index(1)
|
|
|
|
.value_name("ADDRESS")
|
|
|
|
.validator(is_pubkey)
|
|
|
|
.required(true)
|
|
|
|
.help("Account address"),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.subcommand(
|
|
|
|
SubCommand::with_name("enroll")
|
|
|
|
.about("Enroll a system account for balance monitoring")
|
2020-03-27 22:55:55 -07:00
|
|
|
.arg(
|
2020-04-06 21:41:53 -07:00
|
|
|
Arg::with_name("account_address")
|
2020-03-27 22:55:55 -07:00
|
|
|
.index(1)
|
|
|
|
.value_name("ADDRESS")
|
|
|
|
.validator(is_pubkey)
|
|
|
|
.required(true)
|
2020-04-06 21:41:53 -07:00
|
|
|
.help("Account address"),
|
2020-03-27 22:55:55 -07:00
|
|
|
),
|
|
|
|
)
|
|
|
|
.get_matches();
|
|
|
|
|
|
|
|
let data_file = value_t_or_exit!(matches, "data_file", String);
|
2020-04-06 21:41:53 -07:00
|
|
|
let json_rpc_url = value_t!(matches, "json_rpc_url", String).unwrap_or_else(|_| {
|
|
|
|
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()
|
|
|
|
};
|
|
|
|
config.json_rpc_url
|
|
|
|
});
|
2020-03-27 22:55:55 -07:00
|
|
|
|
|
|
|
match matches.subcommand() {
|
|
|
|
("record", Some(matches)) => {
|
|
|
|
let batch_size = value_t_or_exit!(matches, "batch_size", u64);
|
|
|
|
let first_slot = value_t_or_exit!(matches, "first_slot", Slot);
|
2020-04-06 21:41:53 -07:00
|
|
|
command_record(&data_file, json_rpc_url, first_slot, batch_size);
|
2020-03-27 22:55:55 -07:00
|
|
|
}
|
|
|
|
("check", Some(matches)) => {
|
2020-04-06 21:41:53 -07:00
|
|
|
let account_address = pubkey_of(&matches, "account_address").unwrap();
|
|
|
|
command_check(&data_file, &account_address);
|
|
|
|
}
|
|
|
|
("enroll", Some(matches)) => {
|
|
|
|
let account_address = pubkey_of(&matches, "account_address").unwrap();
|
|
|
|
command_enroll(&data_file, json_rpc_url, &account_address);
|
2020-03-27 22:55:55 -07:00
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|