Skip to content

Commit

Permalink
refactor(validators): make rust linter less unhappy
Browse files Browse the repository at this point in the history
  • Loading branch information
ad2ien committed Oct 17, 2022
1 parent 543eb0a commit 8a9ab45
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 42 deletions.
6 changes: 3 additions & 3 deletions src/cli/commands/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use cosmrs::tx::Fee;
use cosmrs::{bank::MsgSend, Coin};
use tracing::{error, info};

use crate::cosmos::validators::Validators;
use crate::{
cli::{
config::{DiscordBotConfig, DiscordShardingSection},
Expand All @@ -21,7 +22,6 @@ use crate::{
},
discord::{discord_client::DiscordActor, discord_server},
};
use crate::cosmos::validators::Validators;

#[derive(Command, Debug, Parser)]
#[clap(arg_required_else_help(true))]
Expand Down Expand Up @@ -72,11 +72,11 @@ impl Runnable for StartCmd {
.start();

let addr_validators = Validators::new(
config.validators.channel_id.clone(),
config.validators.channel_id,
addr_cosmos_client.clone(),
addr_discord_client.clone(),
)
.start();
.start();

let addr_tx_handler = TxHandler::<MsgSend>::new(
config.chain.chain_id.to_string(),
Expand Down
1 change: 0 additions & 1 deletion src/cli/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ pub struct ValidatorsSection {
pub channel_id: u64,
}


impl Default for ValidatorsSection {
fn default() -> Self {
Self {
Expand Down
5 changes: 4 additions & 1 deletion src/cosmos/client/handlers/get_validators_status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ impl Handler<GetValidatorsStatus> for Client<Channel> {
fn handle(&mut self, msg: GetValidatorsStatus, _ctx: &mut Self::Context) -> Self::Result {
let mut validator_client = self.clone().validator();
Box::pin(async move {
info!("handle get validators status request {}", msg.status.as_str_name());
info!(
"handle get validators status request {}",
msg.status.as_str_name()
);

let response = validator_client
.validators(tonic::Request::new(QueryValidatorsRequest {
Expand Down
2 changes: 1 addition & 1 deletion src/cosmos/client/messages/validators_status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ pub type GetValidatorsStatusResult = Result<QueryValidatorsResponse, Error>;
#[rtype(result = "GetValidatorsStatusResult")]
pub struct GetValidatorsStatus {
/// status has to be specified
pub status: BondStatus
pub status: BondStatus,
}
2 changes: 1 addition & 1 deletion src/cosmos/tx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ pub mod messages;
use crate::cosmos::client::account::Account;
use crate::cosmos::client::Client;
use crate::cosmos::tx::error::Error;
use crate::cosmos::validators::Validators;
use crate::discord::discord_client::DiscordActor;
use actix::Addr;
use cosmos_sdk_proto::cosmos::auth::v1beta1::BaseAccount;
use cosmrs::tx::{Body, Fee, Msg, SignDoc, SignerInfo};
use serenity::model::user::User;
use std::time::Duration;
use tonic::transport::Channel;
use crate::cosmos::validators::Validators;

/// Contains addresses of actors that will be used by the TxHandler
pub struct Actors {
Expand Down
38 changes: 21 additions & 17 deletions src/cosmos/validators/handlers/get_new_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use tracing::info;

use crate::cosmos::client::messages::validators_status::GetValidatorsStatus;
use crate::cosmos::tx::error::Error;
use crate::cosmos::validators::Validators;
use crate::cosmos::validators::messages::get_state_message::GetStateMessage;
use crate::cosmos::validators::messages::update_state_message::UpdateStateMessage;
use crate::cosmos::validators::Validators;
use crate::discord::discord_client::messages::send_msg::SendMessage;

impl Handler<GetStateMessage> for Validators {
Expand All @@ -17,19 +17,23 @@ impl Handler<GetStateMessage> for Validators {

let grpc_client = self.grpc_client.clone();
let discord_client = self.discord_client.clone();
let channel_id = self.channel_id.clone();
let channel_id = self.channel_id;
let validators_current_state = self.validators_current.clone();
let self_address = ctx.address().clone();
let self_address = ctx.address();

async move {
for status in [BondStatus::Unbonded, BondStatus::Bonded, BondStatus::Unbonding] {
let _ = grpc_client.send(GetValidatorsStatus { status }).await
.map_err(Error::from).and_then(|response| {
Ok({
response
.map_err(Error::from)
.and_then(|res| Ok(res.validators))
.and_then(|validator_state| {
for status in [
BondStatus::Unbonded,
BondStatus::Bonded,
BondStatus::Unbonding,
] {
let _ = grpc_client
.send(GetValidatorsStatus { status })
.await
.map_err(Error::from)
.map(|response| {
response.map_err(Error::from).map(|res| res.validators).map(
|validator_state| {
for message in Validators::compute_discord_message(
&validators_current_state,
&validator_state,
Expand All @@ -42,14 +46,14 @@ impl Handler<GetStateMessage> for Validators {
});
}
self_address.do_send(UpdateStateMessage {
validators: validator_state.clone()
validators: validator_state,
});
Ok({})
})
})
});
},
)
});
}
}
.into_actor(self).wait(ctx);
.into_actor(self)
.wait(ctx);
}
}
9 changes: 5 additions & 4 deletions src/cosmos/validators/handlers/update_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ impl Handler<UpdateStateMessage> for Validators {
fn handle(&mut self, msg: UpdateStateMessage, _ctx: &mut Self::Context) -> Self::Result {
debug!("Validators update state");

msg.validators.clone().iter().for_each(|new_val| {
let val_pos = self.validators_current.iter().position(|v|
(*v).eq(new_val)
);
msg.validators.iter().for_each(|new_val| {
let val_pos = self
.validators_current
.iter()
.position(|v| (*v).eq(new_val));
match val_pos {
None => {
self.validators_current.append(&mut msg.validators.clone());
Expand Down
3 changes: 1 addition & 2 deletions src/cosmos/validators/messages/get_state_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ pub type GetStateResult = ();
/// Empty message
#[derive(Message)]
#[rtype(result = "GetStateResult")]
pub struct GetStateMessage {
}
pub struct GetStateMessage {}
2 changes: 1 addition & 1 deletion src/cosmos/validators/messages/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pub mod update_state_message;
pub(crate) mod get_state_message;
pub mod update_state_message;
24 changes: 13 additions & 11 deletions src/cosmos/validators/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,18 @@ impl Validators {
}

fn compute_discord_message(
current_validator_state: &Vec<Validator>,
new_validator_state: &Vec<Validator>,
current_validator_state: &[Validator],
new_validator_state: &[Validator],
) -> Vec<String> {
let mut messages: Vec<String> = vec![];

for validator in new_validator_state {
let name_to_display = validator.description.as_ref().map_or_else(|| validator.operator_address.clone(),
|d| d.moniker.clone());
let name_to_display = validator
.description
.as_ref()
.map_or_else(|| validator.operator_address.clone(), |d| d.moniker.clone());

let old_state = current_validator_state.iter().find(|v|
(**v).eq(validator)
);
let old_state = current_validator_state.iter().find(|v| (**v).eq(validator));
match old_state {
None => {
messages.push(msg_to_str(Message::NewValidator, name_to_display.clone()));
Expand All @@ -80,10 +80,12 @@ impl Validators {
}

if validator.status != old_state.status {
messages.push(format!("{} {} ➡️ {}",
msg_to_str(Message::ChangedStatus, name_to_display.clone()),
get_status_txt(old_state.status),
get_status_txt(validator.status)));
messages.push(format!(
"{} {} ➡️ {}",
msg_to_str(Message::ChangedStatus, name_to_display.clone()),
get_status_txt(old_state.status),
get_status_txt(validator.status)
));
}
}
}
Expand Down

0 comments on commit 8a9ab45

Please sign in to comment.