Update ``grin client status`` (#599)

* Add status type
* Add /v1/status handler
* Updated client for API::Status type
* Remove unused dependency
* Moved user agent up
This commit is contained in:
Quentin Le Sceller
2018-01-11 00:25:48 -05:00
committed by Ignotus Peverell
parent ac5010e8f7
commit cbc40c9824
4 changed files with 103 additions and 42 deletions
+29 -25
View File
@@ -12,43 +12,47 @@
// See the License for the specific language governing permissions and
// limitations under the License.
extern crate grin_p2p as p2p;
extern crate term;
use api;
use grin::ServerConfig;
pub fn show_status(config: &ServerConfig) {
println!();
let title=format!("Grin Server Status ");
let mut t = term::stdout().unwrap();
let mut e = term::stdout().unwrap();
t.fg(term::color::MAGENTA).unwrap();
writeln!(t, "{}", title).unwrap();
writeln!(t, "--------------------------").unwrap();
t.reset().unwrap();
writeln!(e, "Protocol version: {}", p2p::msg::PROTOCOL_VERSION).unwrap();
writeln!(e, "User agent: {}", p2p::msg::USER_AGENT).unwrap();
match get_tip_from_node(config) {
Ok(tip) => {
writeln!(e, "Chain height: {}", tip.height).unwrap();
writeln!(e, "Total difficulty: {}", tip.total_difficulty).unwrap();
writeln!(e, "Last block pushed: {}", tip.last_block_pushed).unwrap()
}
Err(_) => writeln!(e, "WARNING: Client failed to get data. Is your `grin server` offline or broken?").unwrap()
};
e.reset().unwrap();
println!();
println!();
let title = format!("Grin Server Status ");
let mut t = term::stdout().unwrap();
let mut e = term::stdout().unwrap();
t.fg(term::color::MAGENTA).unwrap();
writeln!(t, "{}", title).unwrap();
writeln!(t, "--------------------------").unwrap();
t.reset().unwrap();
match get_status_from_node(config) {
Ok(status) => {
writeln!(e, "Protocol version: {}", status.protocol_version).unwrap();
writeln!(e, "User agent: {}", status.user_agent).unwrap();
writeln!(e, "Connections: {}", status.connections).unwrap();
writeln!(e, "Chain height: {}", status.tip.height).unwrap();
writeln!(e, "Last block hash: {}", status.tip.last_block_pushed).unwrap();
writeln!(e, "Previous block hash: {}", status.tip.prev_block_to_last).unwrap();
writeln!(e, "Total difficulty: {}", status.tip.total_difficulty).unwrap()
}
Err(_) => writeln!(
e,
"WARNING: Client failed to get data. Is your `grin server` offline or broken?"
).unwrap(),
};
e.reset().unwrap();
println!();
}
fn get_tip_from_node(config: &ServerConfig) -> Result<api::Tip, Error> {
let url = format!("http://{}/v1/chain", config.api_http_addr);
api::client::get::<api::Tip>(url.as_str()).map_err(|e| Error::API(e))
fn get_status_from_node(config: &ServerConfig) -> Result<api::Status, Error> {
let url = format!("http://{}/v1/status", config.api_http_addr);
api::client::get::<api::Status>(url.as_str()).map_err(|e| Error::API(e))
}
/// Error type wrapping underlying module errors.
#[derive(Debug)]
enum Error {
/// Error originating from HTTP API calls.
API(api::Error)
API(api::Error),
}