diff --git a/substrate/client/consensus/beefy/src/communication/gossip.rs b/substrate/client/consensus/beefy/src/communication/gossip.rs index 9be648f879..d60622b03e 100644 --- a/substrate/client/consensus/beefy/src/communication/gossip.rs +++ b/substrate/client/consensus/beefy/src/communication/gossip.rs @@ -23,7 +23,7 @@ use sc_network_gossip::{MessageIntent, ValidationResult, Validator, ValidatorCon use sp_core::hashing::twox_64; use sp_runtime::traits::{Block, Hash, Header, NumberFor}; -use codec::{Decode, Encode}; +use codec::{Decode, DecodeAll, Encode}; use log::{debug, trace}; use parking_lot::{Mutex, RwLock}; use sc_utils::mpsc::{tracing_unbounded, TracingUnboundedReceiver, TracingUnboundedSender}; @@ -374,7 +374,7 @@ where mut data: &[u8], ) -> ValidationResult { let raw = data; - let action = match GossipMessage::::decode(&mut data) { + let action = match GossipMessage::::decode_all(&mut data) { Ok(GossipMessage::Vote(msg)) => self.validate_vote(msg, sender, raw), Ok(GossipMessage::FinalityProof(proof)) => self.validate_finality_proof(proof, sender), Err(e) => { @@ -402,7 +402,7 @@ where fn message_expired<'a>(&'a self) -> Box bool + 'a> { let filter = self.gossip_filter.read(); - Box::new(move |_topic, mut data| match GossipMessage::::decode(&mut data) { + Box::new(move |_topic, mut data| match GossipMessage::::decode_all(&mut data) { Ok(GossipMessage::Vote(msg)) => { let round = msg.commitment.block_number; let set_id = msg.commitment.validator_set_id; @@ -446,7 +446,7 @@ where return do_rebroadcast } - match GossipMessage::::decode(&mut data) { + match GossipMessage::::decode_all(&mut data) { Ok(GossipMessage::Vote(msg)) => { let round = msg.commitment.block_number; let set_id = msg.commitment.validator_set_id; diff --git a/substrate/client/consensus/beefy/src/communication/request_response/incoming_requests_handler.rs b/substrate/client/consensus/beefy/src/communication/request_response/incoming_requests_handler.rs index 46056ac304..8240dd7110 100644 --- a/substrate/client/consensus/beefy/src/communication/request_response/incoming_requests_handler.rs +++ b/substrate/client/consensus/beefy/src/communication/request_response/incoming_requests_handler.rs @@ -16,7 +16,7 @@ //! Helper for handling (i.e. answering) BEEFY justifications requests from a remote peer. -use codec::Decode; +use codec::DecodeAll; use futures::{channel::oneshot, StreamExt}; use log::{debug, error, trace}; use sc_client_api::BlockBackend; @@ -77,7 +77,7 @@ impl IncomingRequest { F: FnOnce(usize) -> Vec, { let netconfig::IncomingRequest { payload, peer, pending_response } = raw; - let payload = match JustificationRequest::decode(&mut payload.as_ref()) { + let payload = match JustificationRequest::decode_all(&mut payload.as_ref()) { Ok(payload) => payload, Err(err) => { let response = netconfig::OutgoingResponse { diff --git a/substrate/client/consensus/beefy/src/justification.rs b/substrate/client/consensus/beefy/src/justification.rs index 731acdfa63..4855457fd2 100644 --- a/substrate/client/consensus/beefy/src/justification.rs +++ b/substrate/client/consensus/beefy/src/justification.rs @@ -17,7 +17,7 @@ // along with this program. If not, see . use crate::keystore::BeefyKeystore; -use codec::{Decode, Encode}; +use codec::{DecodeAll, Encode}; use sp_consensus::Error as ConsensusError; use sp_consensus_beefy::{ crypto::{AuthorityId, Signature}, @@ -43,7 +43,7 @@ pub(crate) fn decode_and_verify_finality_proof( target_number: NumberFor, validator_set: &ValidatorSet, ) -> Result, (ConsensusError, u32)> { - let proof = >::decode(&mut &*encoded) + let proof = >::decode_all(&mut &*encoded) .map_err(|_| (ConsensusError::InvalidJustification, 0))?; verify_with_validator_set::(target_number, validator_set, &proof).map(|_| proof) } diff --git a/substrate/client/consensus/beefy/src/worker.rs b/substrate/client/consensus/beefy/src/worker.rs index 91b00ddb2c..0bbe55767d 100644 --- a/substrate/client/consensus/beefy/src/worker.rs +++ b/substrate/client/consensus/beefy/src/worker.rs @@ -30,7 +30,7 @@ use crate::{ round::{Rounds, VoteImportResult}, BeefyVoterLinks, LOG_TARGET, }; -use codec::{Codec, Decode, Encode}; +use codec::{Codec, Decode, DecodeAll, Encode}; use futures::{stream::Fuse, FutureExt, StreamExt}; use log::{debug, error, info, log_enabled, trace, warn}; use sc_client_api::{Backend, FinalityNotification, FinalityNotifications, HeaderBackend}; @@ -810,7 +810,7 @@ where self.gossip_engine .messages_for(votes_topic::()) .filter_map(|notification| async move { - let vote = GossipMessage::::decode(&mut ¬ification.message[..]) + let vote = GossipMessage::::decode_all(&mut ¬ification.message[..]) .ok() .and_then(|message| message.unwrap_vote()); trace!(target: LOG_TARGET, "🥩 Got vote message: {:?}", vote); @@ -822,7 +822,7 @@ where self.gossip_engine .messages_for(proofs_topic::()) .filter_map(|notification| async move { - let proof = GossipMessage::::decode(&mut ¬ification.message[..]) + let proof = GossipMessage::::decode_all(&mut ¬ification.message[..]) .ok() .and_then(|message| message.unwrap_finality_proof()); trace!(target: LOG_TARGET, "🥩 Got gossip proof message: {:?}", proof); diff --git a/substrate/client/consensus/grandpa/src/communication/gossip.rs b/substrate/client/consensus/grandpa/src/communication/gossip.rs index c4c885d48e..5688aff3ea 100644 --- a/substrate/client/consensus/grandpa/src/communication/gossip.rs +++ b/substrate/client/consensus/grandpa/src/communication/gossip.rs @@ -87,7 +87,7 @@ use ahash::{AHashMap, AHashSet}; use log::{debug, trace}; -use parity_scale_codec::{Decode, Encode}; +use parity_scale_codec::{Decode, DecodeAll, Encode}; use prometheus_endpoint::{register, CounterVec, Opts, PrometheusError, Registry, U64}; use rand::seq::SliceRandom; use sc_network::{PeerId, ReputationChange}; @@ -546,9 +546,8 @@ impl Peers { who: &PeerId, update: NeighborPacket, ) -> Result>, Misbehavior> { - let peer = match self.inner.get_mut(who) { - None => return Ok(None), - Some(p) => p, + let Some(peer) = self.inner.get_mut(who) else { + return Ok(None) }; let invalid_change = peer.view.set_id > update.set_id || @@ -1040,9 +1039,8 @@ impl Inner { request: CatchUpRequestMessage, set_state: &environment::SharedVoterSetState, ) -> (Option>, Action) { - let local_view = match self.local_view { - None => return (None, Action::Discard(Misbehavior::OutOfScopeMessage.cost())), - Some(ref view) => view, + let Some(local_view) = &self.local_view else { + return (None, Action::Discard(Misbehavior::OutOfScopeMessage.cost())) }; if request.set_id != local_view.set_id { @@ -1175,10 +1173,8 @@ impl Inner { Err(misbehavior) => (misbehavior.cost(), None), }; - let (catch_up, report) = match update_res { - Ok(_) => self.try_catch_up(who), - _ => (None, None), - }; + let (catch_up, report) = + if update_res.is_ok() { self.try_catch_up(who) } else { (None, None) }; let neighbor_topics = topics.unwrap_or_default(); @@ -1435,7 +1431,7 @@ impl GossipValidator { let message_name; let action = { - match GossipMessage::::decode(&mut data) { + match GossipMessage::::decode_all(&mut data) { Ok(GossipMessage::Vote(ref message)) => { message_name = Some("vote"); self.inner.write().validate_round_message(who, message) @@ -1599,9 +1595,8 @@ impl sc_network_gossip::Validator for GossipValidator return false, - Some(x) => x, + let Some((maybe_round, set_id)) = inner.live_topics.topic_info(topic) else { + return false }; if let MessageIntent::Broadcast = intent { @@ -1622,12 +1617,11 @@ impl sc_network_gossip::Validator for GossipValidator v, - None => return false, // cannot evaluate until we have a local view. + let Some(local_view) = &inner.local_view else { + return false // cannot evaluate until we have a local view. }; - match GossipMessage::::decode(&mut data) { + match GossipMessage::::decode_all(&mut data) { Err(_) => false, Ok(GossipMessage::Commit(full)) => { // we only broadcast commit messages if they're for the same @@ -1658,13 +1652,12 @@ impl sc_network_gossip::Validator for GossipValidator {}, }; - let local_view = match inner.local_view { - Some(ref v) => v, - None => return true, // no local view means we can't evaluate or hold any topic. + let Some(local_view) = &inner.local_view else { + return true // no local view means we can't evaluate or hold any topic. }; // global messages -- only keep the best commit. - match GossipMessage::::decode(&mut data) { + match GossipMessage::::decode_all(&mut data) { Err(_) => true, Ok(GossipMessage::Commit(full)) => match local_view.last_commit { Some((number, round, set_id)) => diff --git a/substrate/client/consensus/grandpa/src/communication/mod.rs b/substrate/client/consensus/grandpa/src/communication/mod.rs index 9d90035d71..c074985856 100644 --- a/substrate/client/consensus/grandpa/src/communication/mod.rs +++ b/substrate/client/consensus/grandpa/src/communication/mod.rs @@ -45,7 +45,7 @@ use finality_grandpa::{ voter_set::VoterSet, Message::{Precommit, Prevote, PrimaryPropose}, }; -use parity_scale_codec::{Decode, Encode}; +use parity_scale_codec::{Decode, DecodeAll, Encode}; use sc_network::{NetworkBlock, NetworkSyncForkRequest, ReputationChange}; use sc_network_gossip::{GossipEngine, Network as GossipNetwork}; use sc_telemetry::{telemetry, TelemetryHandle, CONSENSUS_DEBUG, CONSENSUS_INFO}; @@ -352,7 +352,7 @@ impl, S: Syncing> NetworkBridge { let telemetry = self.telemetry.clone(); let incoming = self.gossip_engine.lock().messages_for(topic).filter_map(move |notification| { - let decoded = GossipMessage::::decode(&mut ¬ification.message[..]); + let decoded = GossipMessage::::decode_all(&mut ¬ification.message[..]); match decoded { Err(ref e) => { @@ -651,7 +651,7 @@ fn incoming_global( .messages_for(topic) .filter_map(|notification| { // this could be optimized by decoding piecewise. - let decoded = GossipMessage::::decode(&mut ¬ification.message[..]); + let decoded = GossipMessage::::decode_all(&mut ¬ification.message[..]); if let Err(ref e) = decoded { trace!( target: LOG_TARGET, diff --git a/substrate/client/consensus/grandpa/src/communication/periodic.rs b/substrate/client/consensus/grandpa/src/communication/periodic.rs index f3f7572864..daa7529202 100644 --- a/substrate/client/consensus/grandpa/src/communication/periodic.rs +++ b/substrate/client/consensus/grandpa/src/communication/periodic.rs @@ -108,7 +108,7 @@ impl Stream for NeighborPacketWorker { // // Note: In case poll_unpin is called after the resetted delay fires again, this // will drop one tick. Deemed as very unlikely and also not critical. - while let Poll::Ready(()) = this.delay.poll_unpin(cx) {} + while this.delay.poll_unpin(cx).is_ready() {} if let Some((ref to, ref packet)) = this.last { return Poll::Ready(Some((to.clone(), GossipMessage::::from(packet.clone())))) diff --git a/substrate/client/consensus/grandpa/src/justification.rs b/substrate/client/consensus/grandpa/src/justification.rs index c300a3d7ac..a38cb113b4 100644 --- a/substrate/client/consensus/grandpa/src/justification.rs +++ b/substrate/client/consensus/grandpa/src/justification.rs @@ -23,7 +23,7 @@ use std::{ }; use finality_grandpa::{voter_set::VoterSet, Error as GrandpaError}; -use parity_scale_codec::{Decode, Encode}; +use parity_scale_codec::{Decode, DecodeAll, Encode}; use sp_blockchain::{Error as ClientError, HeaderBackend}; use sp_consensus_grandpa::AuthorityId; use sp_runtime::traits::{Block as BlockT, Header as HeaderT, NumberFor}; @@ -136,7 +136,7 @@ impl GrandpaJustification { where NumberFor: finality_grandpa::BlockNumberOps, { - let justification = GrandpaJustification::::decode(&mut &*encoded) + let justification = GrandpaJustification::::decode_all(&mut &*encoded) .map_err(|_| ClientError::JustificationDecode)?; if ( diff --git a/substrate/client/consensus/grandpa/src/warp_proof.rs b/substrate/client/consensus/grandpa/src/warp_proof.rs index ec2d25c328..9acf1f2187 100644 --- a/substrate/client/consensus/grandpa/src/warp_proof.rs +++ b/substrate/client/consensus/grandpa/src/warp_proof.rs @@ -16,7 +16,7 @@ //! Utilities for generating and verifying GRANDPA warp sync proofs. -use sp_runtime::codec::{self, Decode, Encode}; +use parity_scale_codec::{Decode, DecodeAll, Encode}; use crate::{ best_justification, find_scheduled_change, AuthoritySetChanges, AuthoritySetHardFork, @@ -38,7 +38,7 @@ use std::{collections::HashMap, sync::Arc}; pub enum Error { /// Decoding error. #[error("Failed to decode block hash: {0}.")] - DecodeScale(#[from] codec::Error), + DecodeScale(#[from] parity_scale_codec::Error), /// Client backend error. #[error("{0}")] Client(#[from] sp_blockchain::Error), @@ -137,7 +137,7 @@ impl WarpSyncProof { .and_then(|just| just.into_justification(GRANDPA_ENGINE_ID)) .ok_or_else(|| Error::MissingData)?; - let justification = GrandpaJustification::::decode(&mut &justification[..])?; + let justification = GrandpaJustification::::decode_all(&mut &justification[..])?; let proof = WarpSyncFragment { header: header.clone(), justification }; let proof_size = proof.encoded_size(); @@ -291,7 +291,7 @@ where authorities: AuthorityList, ) -> Result, Box> { let EncodedProof(proof) = proof; - let proof = WarpSyncProof::::decode(&mut proof.as_slice()) + let proof = WarpSyncProof::::decode_all(&mut proof.as_slice()) .map_err(|e| format!("Proof decoding error: {:?}", e))?; let last_header = proof .proofs @@ -318,8 +318,9 @@ where #[cfg(test)] mod tests { - use super::{codec::Encode, WarpSyncProof}; + use super::WarpSyncProof; use crate::{AuthoritySetChanges, GrandpaJustification}; + use parity_scale_codec::Encode; use rand::prelude::*; use sc_block_builder::BlockBuilderProvider; use sp_blockchain::HeaderBackend;