Skip to content

Commit

Permalink
chore: fix clippy warnings for rust 1.49
Browse files Browse the repository at this point in the history
  • Loading branch information
tmpolaczyk committed Dec 22, 2020
1 parent 19c5cf4 commit c2f169f
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 116 deletions.
15 changes: 1 addition & 14 deletions bridges/ethereum/src/eth.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::config::Config;
use crate::multibimap::MultiBiMap;
use ethabi::{Bytes, Token};
use ethabi::Bytes;
use futures::Future;
use futures_locks::RwLock;
use std::collections::{HashMap, HashSet};
Expand Down Expand Up @@ -427,19 +427,6 @@ impl EthState {
}
}

/// Assume the first return value of an event log is a U256 and return it
pub fn read_u256_from_event_log(value: &web3::types::Log) -> Result<U256, ()> {
let event_types = vec![ethabi::ParamType::Uint(0)];
let event_data = ethabi::decode(&event_types, &value.data.0);
log::debug!("Event data: {:?}", event_data);

// Errors are handled by the caller, if this fails there is nothing we can do
match event_data.map_err(|_| ())?.get(0).ok_or(())? {
Token::Uint(x) => Ok(*x),
_ => Err(()),
}
}

/// Possible ethereum events emited by the WRB ethereum contract
pub enum WrbEvent {
/// A new data request has been posted to ethereum
Expand Down
3 changes: 3 additions & 0 deletions config/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1067,6 +1067,7 @@ where
}
}

#[allow(clippy::unnecessary_wraps)]
fn from_millis<'de, D>(deserializer: D) -> Result<Option<Duration>, D::Error>
where
D: Deserializer<'de>,
Expand All @@ -1076,6 +1077,7 @@ where
Err(_) => None,
})
}

fn to_secs<S>(val: &Option<Duration>, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
Expand All @@ -1087,6 +1089,7 @@ where
}
}

#[allow(clippy::unnecessary_wraps)]
fn from_secs<'de, D>(deserializer: D) -> Result<Option<Duration>, D::Error>
where
D: Deserializer<'de>,
Expand Down
5 changes: 3 additions & 2 deletions crypto/src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ where
let seed_bytes = self.seed.as_ref();
let seed_len = seed_bytes.len();

if seed_len < 16 || seed_len > 64 {
// Seed length must be between 16 and 64 bytes (128 and 512 bits)
if !(16..=64).contains(&seed_len) {
return Err(MasterKeyGenError::InvalidSeedLength);
}

Expand Down Expand Up @@ -148,7 +149,7 @@ pub enum KeyDerivationError {
#[fail(display = "The length of the hmac key is invalid")]
InvalidKeyLength,
/// Invalid seed length
#[fail(display = "The length of the seed is invalid, must be between 128/256 bits")]
#[fail(display = "The length of the seed is invalid, must be between 128/512 bits")]
InvalidSeedLength,
/// Secp256k1 internal error
#[fail(display = "Error in secp256k1 crate")]
Expand Down
8 changes: 3 additions & 5 deletions net/src/client/tcp/actors/jsonrpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,11 +357,9 @@ impl StreamHandler<NotifySubscriptionId, Error> for JsonRpcClient {

fn is_connection_error(err: &Error) -> bool {
match err {
Error::RequestFailed { error_kind, .. } => match error_kind {
TransportErrorKind::Transport(_) => true,
TransportErrorKind::Unreachable => true,
_ => false,
},
Error::RequestFailed { error_kind, .. } => {
matches!(error_kind, TransportErrorKind::Transport(_) | TransportErrorKind::Unreachable)
}
Error::RequestTimedOut(_) => true,
Error::Mailbox(_) => true,
_ => false,
Expand Down
8 changes: 5 additions & 3 deletions node/src/actors/chain_manager/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,9 @@ impl Handler<AddSuperBlockVote> for ChainManager {
AddSuperBlockVote { superblock_vote }: AddSuperBlockVote,
ctx: &mut Context<Self>,
) -> Self::Result {
self.add_superblock_vote(superblock_vote, ctx)
self.add_superblock_vote(superblock_vote, ctx);

Ok(())
}
}

Expand All @@ -657,7 +659,7 @@ impl Handler<GetBlocksEpochRange> for ChainManager {
type Result = Result<Vec<(Epoch, Hash)>, ChainManagerError>;

fn handle(&mut self, msg: GetBlocksEpochRange, _ctx: &mut Context<Self>) -> Self::Result {
self.get_blocks_epoch_range(msg)
Ok(self.get_blocks_epoch_range(msg))
}
}

Expand Down Expand Up @@ -1027,7 +1029,7 @@ impl Handler<PeersBeacons> for ChainManager {
// This is the only point in the whole base code for the state
// machine to move into `Synced` state.
self.update_state_machine(StateMachine::Synced);
self.add_temp_superblock_votes(ctx).unwrap();
self.add_temp_superblock_votes(ctx);
}
Ok(peers_to_unregister)
}
Expand Down
119 changes: 39 additions & 80 deletions node/src/actors/chain_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -783,15 +783,7 @@ impl ChainManager {
let random_waiting = rng.gen_range(checkpoints_period, end_range);
ctx.run_later(
Duration::from_secs(u64::from(random_waiting)),
|act, ctx| match act.add_superblock_vote(res, ctx) {
Ok(()) => (),
Err(e) => {
log::error!(
"Error when broadcasting recently created superblock: {}",
e
);
}
},
|act, ctx| act.add_superblock_vote(res, ctx),
);
})
})
Expand All @@ -811,7 +803,7 @@ impl ChainManager {
self.chain_state.get_consensus_constants()
}

fn add_temp_superblock_votes(&mut self, ctx: &mut Context<Self>) -> Result<(), failure::Error> {
fn add_temp_superblock_votes(&mut self, ctx: &mut Context<Self>) {
let consensus_constants = self.consensus_constants();

let superblock_period = u32::from(consensus_constants.superblock_period);
Expand All @@ -820,7 +812,7 @@ impl ChainManager {
log::debug!("add_temp_superblock_votes {:?}", superblock_vote);
// Check if we already received this vote
if self.chain_state.superblock_state.contains(&superblock_vote) {
return Ok(());
return;
}

// Validate secp256k1 signature
Expand All @@ -846,15 +838,9 @@ impl ChainManager {
})
.spawn(ctx);
}

Ok(())
}

fn add_superblock_vote(
&mut self,
superblock_vote: SuperBlockVote,
ctx: &mut Context<Self>,
) -> Result<(), failure::Error> {
fn add_superblock_vote(&mut self, superblock_vote: SuperBlockVote, ctx: &mut Context<Self>) {
log::trace!(
"AddSuperBlockVote received while StateMachine is in state {:?}",
self.sm_state
Expand All @@ -869,7 +855,7 @@ impl ChainManager {

// Check if we already received this vote
if self.chain_state.superblock_state.contains(&superblock_vote) {
return Ok(());
return;
}

// Validate secp256k1 signature
Expand Down Expand Up @@ -946,8 +932,6 @@ impl ChainManager {
.into_actor(act)
})
.spawn(ctx);

Ok(())
}

#[must_use]
Expand Down Expand Up @@ -1123,16 +1107,10 @@ impl ChainManager {
})
})
.into_actor(act)
.and_then(|res, act, ctx| match act.add_superblock_vote(res, ctx) {
Ok(()) => actix::fut::ok(()),
Err(e) => {
log::error!(
"Error when broadcasting recently created superblock: {}",
e
);
.and_then(|res, act, ctx| {
act.add_superblock_vote(res, ctx);

actix::fut::err(())
}
actix::fut::ok(())
})
},
);
Expand Down Expand Up @@ -1223,15 +1201,9 @@ impl ChainManager {
let fut = futures::future::ok(self.get_blocks_epoch_range(
GetBlocksEpochRange::new_with_limit(init_epoch..=final_epoch, 0),
))
.and_then(move |res| match res {
Ok(v) => {
let block_hashes: Vec<Hash> = v.into_iter().map(|(_epoch, hash)| hash).collect();
futures::future::ok(block_hashes)
}
Err(e) => {
log::error!("Error in GetBlocksEpochRange: {}", e);
futures::future::err(())
}
.and_then(move |res| {
let block_hashes: Vec<Hash> = res.into_iter().map(|(_epoch, hash)| hash).collect();
futures::future::ok(block_hashes)
})
.and_then(move |block_hashes| {
let aux = block_hashes.into_iter().map(move |hash| {
Expand All @@ -1257,24 +1229,16 @@ impl ChainManager {
})
.into_actor(self)
.and_then(move |block_headers, act, _ctx| {
let last_hash = act
let v = act
.get_blocks_epoch_range(
GetBlocksEpochRange::new_with_limit_from_end(..init_epoch, 1),
)
.map(move |v| {
v.first()
);
let last_hash = v.first()
.map(|(_epoch, hash)| *hash)
.unwrap_or(genesis_hash)
});
match last_hash {
Ok(last_hash) => actix::fut::ok((block_headers, last_hash)),
Err(e) => {
log::error!("Error in GetBlocksEpochRange: {}", e);
actix::fut::err(())
}
}
.unwrap_or(genesis_hash);

actix::fut::ok((block_headers, last_hash))
})
.map_err(|e, _, _| log::error!("Superblock building failed: {:?}", e))
.and_then(move |(block_headers, last_hash), act, ctx| {
let consensus = if act.sm_state == StateMachine::Synced || act.sm_state == StateMachine::AlmostSynced {

Expand Down Expand Up @@ -1687,21 +1651,19 @@ impl ChainManager {

// If there is a superblock to consolidate, and we got the confirmed block beacons, send
// notification
if let Ok(beacons) = beacons {
let consolidated_block_hashes: Vec<Hash> =
beacons.iter().cloned().map(|(_epoch, hash)| hash).collect();
let superblock_notify = SuperBlockNotify {
superblock,
consolidated_block_hashes,
};
let consolidated_block_hashes: Vec<Hash> =
beacons.iter().cloned().map(|(_epoch, hash)| hash).collect();
let superblock_notify = SuperBlockNotify {
superblock,
consolidated_block_hashes,
};

// Store the list of block hashes that pertain to this superblock
InventoryManager::from_registry().do_send(AddItem {
item: StoreInventoryItem::Superblock(superblock_notify.clone()),
});
// Store the list of block hashes that pertain to this superblock
InventoryManager::from_registry().do_send(AddItem {
item: StoreInventoryItem::Superblock(superblock_notify.clone()),
});

JsonRpcServer::from_registry().do_send(superblock_notify);
}
JsonRpcServer::from_registry().do_send(superblock_notify);
}

/// Let JSON-RPC clients know when the node changes its status
Expand All @@ -1718,7 +1680,7 @@ impl ChainManager {
limit,
limit_from_end,
}: GetBlocksEpochRange,
) -> Result<Vec<(Epoch, Hash)>, ChainManagerError> {
) -> Vec<(Epoch, Hash)> {
log::debug!("GetBlocksEpochRange received {:?}", range);

// Accept this message in any state
Expand All @@ -1736,7 +1698,7 @@ impl ChainManager {
// Return all the blocks from this epoch range
let hashes: Vec<(Epoch, Hash)> = block_chain_range.collect();

Ok(hashes)
hashes
} else if limit_from_end {
let mut hashes: Vec<(Epoch, Hash)> = block_chain_range
// Take the last "limit" blocks
Expand All @@ -1747,14 +1709,14 @@ impl ChainManager {
// Reverse again to return them in non-reversed order
hashes.reverse();

Ok(hashes)
hashes
} else {
let hashes: Vec<(Epoch, Hash)> = block_chain_range
// Take the first "limit" blocks
.take(limit)
.collect();

Ok(hashes)
hashes
}
}

Expand All @@ -1771,15 +1733,9 @@ impl ChainManager {
let fut = futures::future::ok(
self.get_blocks_epoch_range(GetBlocksEpochRange::new_with_limit(epoch.., 0)),
)
.and_then(move |res| match res {
Ok(v) => {
let block_hashes: Vec<Hash> = v.into_iter().map(|(_epoch, hash)| hash).collect();
futures::future::ok(block_hashes)
}
Err(e) => {
log::error!("Error in GetBlocksEpochRange: {}", e);
futures::future::err(())
}
.and_then(move |res| {
let block_hashes: Vec<Hash> = res.into_iter().map(|(_epoch, hash)| hash).collect();
futures::future::ok(block_hashes)
})
.and_then(move |block_hashes| {
// For each block, collect all the transactions that may be valid if this block is
Expand Down Expand Up @@ -1837,7 +1793,10 @@ impl ChainManager {

actix::fut::ok(())
})
.map_err(|e, _, _| log::error!("{:?}", e));
.map_err(|(), _, _| {
// Errors at this point should be impossible because we explicitly ignore them
log::error!("Unknown error in reinsert_transactions_from_unconfirmed_blocks");
});

Box::new(fut)
}
Expand Down
2 changes: 1 addition & 1 deletion p2p/src/sessions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ where
inbound_consolidated: BoundedSessions::default(),
inbound_network_ranges: Default::default(),
inbound_unconsolidated: BoundedSessions::default(),
magic_number: 0 as u16,
magic_number: 0,
outbound_consolidated: BoundedSessions::default(),
outbound_consolidated_consensus: BoundedSessions::default(),
outbound_unconsolidated: BoundedSessions::default(),
Expand Down
6 changes: 3 additions & 3 deletions rad/src/filters/deviation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub fn standard_filter(
}
let bool_matrix = boolean_standard_filter(&input, sigmas_float)?;

keep_rows(input, &bool_matrix, context)
Ok(keep_rows(input, &bool_matrix, context))
}
Some(_rad_types) => {
// 1D array
Expand Down Expand Up @@ -87,7 +87,7 @@ fn keep_rows(
input: &RadonArray,
keep: &RadonArray,
context: &mut ReportContext<RadonTypes>,
) -> Result<RadonTypes, RadError> {
) -> RadonTypes {
let mut result = vec![];
let mut bool_vec = vec![];
for (item, keep_array) in input.value().into_iter().zip(keep.value()) {
Expand Down Expand Up @@ -115,7 +115,7 @@ fn keep_rows(
metadata.update_liars(bool_vec);
}

Ok(RadonTypes::from(RadonArray::from(result)))
RadonTypes::from(RadonArray::from(result))
}

// Return an array with the same dimensions as the input, with a boolean indicating
Expand Down
Loading

0 comments on commit c2f169f

Please sign in to comment.