diff --git a/res/indexer-queries.graphql b/res/indexer-queries.graphql index 21cfb6a3e30188d4b46d881410cf9ae2ff2a6a5c..304a8eeb30949d7dffda7a972c37348d88b883fa 100644 --- a/res/indexer-queries.graphql +++ b/res/indexer-queries.graphql @@ -4,8 +4,8 @@ query IdentityNameByIndex($index: Int!) { } } -query NamesByIndexes($idtyids: [Int!]) { - identities(where: {index_in: $idtyids}) { +query NamesByIndexes($indexes: [Int!]!) { + identities(where: {index_in: $indexes}) { index name } diff --git a/src/commands/smith.rs b/src/commands/smith.rs index 2f2d5b86f55065b1d574f646ef1c59279085960e..034948b617a3e4924863fa4f6d9736095801e485 100644 --- a/src/commands/smith.rs +++ b/src/commands/smith.rs @@ -3,6 +3,7 @@ use crate::*; use commands::identity::try_get_idty_index_by_name; #[cfg(feature = "gdev")] use runtime::runtime_types::gdev_runtime::opaque::SessionKeys as RuntimeSessionKeys; +use std::collections::HashMap; use std::ops::Deref; type SessionKeys = [u8; 128]; @@ -68,7 +69,7 @@ pub enum Subcommand { /// handle smith commands pub async fn handle_command(data: Data, command: Subcommand) -> Result<(), GcliError> { - let mut data = data.build_client().await?; + let mut data = data.build_client().await?.build_indexer().await?; match command { Subcommand::GoOnline => { go_online(&data).await?; @@ -222,14 +223,60 @@ pub async fn online(data: &Data) -> Result<(), anyhow::Error> { .await? .unwrap_or_default(); - println!("Online:"); - println!("{online_authorities:?}"); + if let Some(indexer) = &data.indexer { + let mut names = HashMap::<IdtyId, String>::new(); + indexer + .names_by_indexes(&online_authorities) + .await + .into_iter() + .for_each(|e| { + names.insert(e.0, e.1); + }); + println!("Online:"); + println!( + "{}", + online_authorities + .iter() + .map(|i| &names + .get(i) + .expect("panic! found authorities whith no name")[..]) + .collect::<Vec<&str>>() + .join(", ") + ); - println!("Incoming:"); - println!("{incoming_authorities:?}"); + println!("Incoming:"); + println!( + "{}", + incoming_authorities + .iter() + .map(|i| &names + .get(i) + .expect("panic! found authorities whith no name")[..]) + .collect::<Vec<&str>>() + .join(", ") + ); - println!("Outgoing:"); - println!("{outgoing_authorities:?}"); + println!("Outgoing:"); + println!( + "{}", + outgoing_authorities + .iter() + .map(|i| &names + .get(i) + .expect("panic! found authorities whith no name")[..]) + .collect::<Vec<&str>>() + .join(", ") + ); + } else { + println!("Online:"); + println!("{online_authorities:?}"); + + println!("Incoming:"); + println!("{incoming_authorities:?}"); + + println!("Outgoing:"); + println!("{outgoing_authorities:?}"); + } Ok(()) } diff --git a/src/indexer.rs b/src/indexer.rs index 9b4e2c1ec6b6961ef7c7925daff34830e8a38999..f940b30969db2f3fb1d17d7545d5e20737dafd5c 100644 --- a/src/indexer.rs +++ b/src/indexer.rs @@ -15,7 +15,7 @@ pub struct Indexer { } impl Indexer { - // query + /// graphql query without error management async fn query<T: GraphQLQuery>( &self, var: <T as GraphQLQuery>::Variables, @@ -38,6 +38,18 @@ impl Indexer { .map(|idty| idty.name) } + /// index → name (multiple) + pub async fn names_by_indexes(&self, indexes: &[IdtyId]) -> Vec<(IdtyId, String)> { + self.query::<NamesByIndexes>(names_by_indexes::Variables { + indexes: indexes.iter().map(|i| *i as i64).collect(), + }) + .await + .identities + .into_iter() + .map(|idty| (idty.index as IdtyId, idty.name)) + .collect() + } + /// pubkey → name pub async fn username_by_pubkey(&self, pubkey: &str) -> Option<String> { self.query::<IdentityNameByPubkey>(identity_name_by_pubkey::Variables { diff --git a/src/indexer/queries.rs b/src/indexer/queries.rs index 1c3f52cb6a71f0a9b2b02adad8b096c9c91c4a40..29b657286492dc5c12cc77e7722ecab8adb1c80c 100644 --- a/src/indexer/queries.rs +++ b/src/indexer/queries.rs @@ -1,7 +1,6 @@ use graphql_client::GraphQLQuery; use sp_core::Bytes; -// index → identity #[derive(GraphQLQuery)] #[graphql( schema_path = "res/indexer-schema.json", @@ -9,7 +8,6 @@ use sp_core::Bytes; )] pub struct IdentityNameByIndex; -// index → identity info #[derive(GraphQLQuery)] #[graphql( schema_path = "res/indexer-schema.json", @@ -17,7 +15,6 @@ pub struct IdentityNameByIndex; )] pub struct IdentityInfo; -// pubkey → identity #[derive(GraphQLQuery)] #[graphql( schema_path = "res/indexer-schema.json", @@ -25,7 +22,6 @@ pub struct IdentityInfo; )] pub struct IdentityNameByPubkey; -// pubkey → wasidentity #[derive(GraphQLQuery)] #[graphql( schema_path = "res/indexer-schema.json", @@ -53,3 +49,10 @@ pub struct BlockByNumber; query_path = "res/indexer-queries.graphql" )] pub struct GenesisHash; + +#[derive(GraphQLQuery, Debug)] +#[graphql( + schema_path = "res/indexer-schema.json", + query_path = "res/indexer-queries.graphql" +)] +pub struct NamesByIndexes;