@@ -14,7 +14,7 @@ use ansi_term::Color::{Purple, Red, White, Yellow};
1414use failure:: { bail, Fail } ;
1515use itertools:: Itertools ;
1616use num_format:: { Locale , ToFormattedString } ;
17- use prettytable:: { row, Table } ;
17+ use prettytable:: { row, Cell , Row , Table } ;
1818use qrcode:: render:: unicode;
1919use serde:: { de:: DeserializeOwned , Deserialize , Serialize } ;
2020
@@ -24,6 +24,7 @@ use witnet_crypto::{
2424 key:: { ExtendedPK , ExtendedSK } ,
2525} ;
2626use witnet_data_structures:: {
27+ capabilities:: { Capability , ALL_CAPABILITIES } ,
2728 chain:: {
2829 priority:: { PrioritiesEstimate , Priority , PriorityEstimate , TimeToBlock } ,
2930 tapi:: { current_active_wips, ActiveWips } ,
@@ -49,7 +50,8 @@ use witnet_data_structures::{
4950use witnet_node:: actors:: {
5051 chain_manager:: run_dr_locally,
5152 json_rpc:: api:: {
52- AddrType , GetBlockChainParams , GetTransactionOutput , PeersResult , QueryStakesParams ,
53+ AddrType , GetBlockChainParams , GetTransactionOutput , PeersResult , QueryPowersParams ,
54+ QueryPowersRecord , QueryStakesParams ,
5355 } ,
5456 messages:: {
5557 AuthorizeStake , BuildDrt , BuildStakeParams , BuildStakeResponse , BuildUnstakeParams ,
@@ -2009,6 +2011,74 @@ pub fn query_stakes(
20092011 Ok ( ( ) )
20102012}
20112013
2014+ pub fn query_powers (
2015+ addr : SocketAddr ,
2016+ capability : Option < String > ,
2017+ all : bool ,
2018+ ) -> Result < ( ) , failure:: Error > {
2019+ let mut stream = start_client ( addr) ?;
2020+ let params = if all {
2021+ QueryPowersParams :: All ( true )
2022+ } else {
2023+ match capability {
2024+ Some ( c) => match Capability :: from_str ( & c) {
2025+ Ok ( c) => QueryPowersParams :: Capability ( c) ,
2026+ Err ( _) => QueryPowersParams :: Capability ( Capability :: Mining ) ,
2027+ } ,
2028+ None => QueryPowersParams :: Capability ( Capability :: Mining ) ,
2029+ }
2030+ } ;
2031+
2032+ let response = send_request (
2033+ & mut stream,
2034+ & format ! (
2035+ r#"{{"jsonrpc": "2.0","method": "queryPowers", "params": {}, "id": 1}}"# ,
2036+ serde_json:: to_string( & params) . unwrap( )
2037+ ) ,
2038+ ) ?;
2039+
2040+ let mut powers: Vec < QueryPowersRecord > = parse_response ( & response) ?;
2041+ powers. sort_by_key ( |power| Reverse ( power. powers [ 0 ] ) ) ;
2042+
2043+ let mut powers_table = Table :: new ( ) ;
2044+ powers_table. set_format ( * prettytable:: format:: consts:: FORMAT_NO_BORDER_LINE_SEPARATOR ) ;
2045+ if all {
2046+ let mut header = vec ! [
2047+ Cell :: new( "Validator" ) . style_spec( "c" ) ,
2048+ Cell :: new( "Withdrawer" ) . style_spec( "c" ) ,
2049+ ] ;
2050+ for capability in ALL_CAPABILITIES {
2051+ let capability_str: & ' static str = capability. into ( ) ;
2052+ header. push ( Cell :: new ( capability_str) . style_spec ( "c" ) ) ;
2053+ }
2054+ powers_table. set_titles ( Row :: new ( header) ) ;
2055+ for power in powers. iter ( ) {
2056+ let mut row = vec ! [
2057+ Cell :: new( & power. validator. to_string( ) ) ,
2058+ Cell :: new( & power. withdrawer. to_string( ) ) ,
2059+ ] ;
2060+ for p in & power. powers {
2061+ row. push ( Cell :: new ( & p. to_formatted_string ( & Locale :: en) ) . style_spec ( "r" ) ) ;
2062+ }
2063+ powers_table. add_row ( Row :: new ( row) ) ;
2064+ }
2065+ } else {
2066+ let capability_str: & ' static str = params. into ( ) ;
2067+ powers_table. set_titles ( row ! [ c->"Validator" , c->"Withdrawer" , c->capability_str] ) ;
2068+ for power in powers. iter ( ) {
2069+ powers_table. add_row ( row ! [
2070+ power. validator,
2071+ power. withdrawer,
2072+ r->( power. powers[ 0 ] ) . to_formatted_string( & Locale :: en) ,
2073+ ] ) ;
2074+ }
2075+ }
2076+ powers_table. printstd ( ) ;
2077+ println ! ( ) ;
2078+
2079+ Ok ( ( ) )
2080+ }
2081+
20122082#[ derive( Serialize , Deserialize ) ]
20132083struct SignatureWithData {
20142084 address : String ,
0 commit comments