Run cargo fmt on the whole code base (#9394)

* Run cargo fmt on the whole code base

* Second run

* Add CI check

* Fix compilation

* More unnecessary braces

* Handle weights

* Use --all

* Use correct attributes...

* Fix UI tests

* AHHHHHHHHH

* 🤦

* Docs

* Fix compilation

* 🤷

* Please stop

* 🤦 x 2

* More

* make rustfmt.toml consistent with polkadot

Co-authored-by: André Silva <andrerfosilva@gmail.com>
This commit is contained in:
Bastian Köcher
2021-07-21 16:32:32 +02:00
committed by GitHub
parent d451c38c1c
commit 7b56ab15b4
1010 changed files with 53339 additions and 51208 deletions
@@ -18,21 +18,26 @@
//! Tests for the communication portion of the GRANDPA crate.
use sp_utils::mpsc::{tracing_unbounded, TracingUnboundedReceiver, TracingUnboundedSender};
use super::{
gossip::{self, GossipValidator},
Round, SetId, VoterSet,
};
use crate::{communication::GRANDPA_PROTOCOL_NAME, environment::SharedVoterSetState};
use futures::prelude::*;
use sc_network::{config::Role, Event as NetworkEvent, ObservedRole, PeerId};
use sc_network_test::{Block, Hash};
use sc_network_gossip::Validator;
use std::sync::Arc;
use sp_keyring::Ed25519Keyring;
use parity_scale_codec::Encode;
use sp_runtime::traits::NumberFor;
use std::{borrow::Cow, pin::Pin, task::{Context, Poll}};
use crate::communication::GRANDPA_PROTOCOL_NAME;
use crate::environment::SharedVoterSetState;
use sc_network::{config::Role, Event as NetworkEvent, ObservedRole, PeerId};
use sc_network_gossip::Validator;
use sc_network_test::{Block, Hash};
use sp_finality_grandpa::AuthorityList;
use super::gossip::{self, GossipValidator};
use super::{VoterSet, Round, SetId};
use sp_keyring::Ed25519Keyring;
use sp_runtime::traits::NumberFor;
use sp_utils::mpsc::{tracing_unbounded, TracingUnboundedReceiver, TracingUnboundedSender};
use std::{
borrow::Cow,
pin::Pin,
sync::Arc,
task::{Context, Poll},
};
#[derive(Debug)]
pub(crate) enum Event {
@@ -79,13 +84,14 @@ impl super::Network<Block> for TestNetwork {
_peers: Vec<sc_network::PeerId>,
_hash: Hash,
_number: NumberFor<Block>,
) {}
) {
}
}
impl sc_network_gossip::ValidatorContext<Block> for TestNetwork {
fn broadcast_topic(&mut self, _: Hash, _: bool) { }
fn broadcast_topic(&mut self, _: Hash, _: bool) {}
fn broadcast_message(&mut self, _: Hash, _: Vec<u8>, _: bool) { }
fn broadcast_message(&mut self, _: Hash, _: Vec<u8>, _: bool) {}
fn send_message(&mut self, who: &sc_network::PeerId, data: Vec<u8>) {
<Self as sc_network_gossip::Network<Block>>::write_notification(
@@ -96,7 +102,7 @@ impl sc_network_gossip::ValidatorContext<Block> for TestNetwork {
);
}
fn send_topic(&mut self, _: &sc_network::PeerId, _: Hash, _: bool) { }
fn send_topic(&mut self, _: &sc_network::PeerId, _: Hash, _: bool) {}
}
pub(crate) struct Tester {
@@ -107,15 +113,17 @@ pub(crate) struct Tester {
impl Tester {
fn filter_network_events<F>(self, mut pred: F) -> impl Future<Output = Self>
where F: FnMut(Event) -> bool
where
F: FnMut(Event) -> bool,
{
let mut s = Some(self);
futures::future::poll_fn(move |cx| loop {
match Stream::poll_next(Pin::new(&mut s.as_mut().unwrap().events), cx) {
Poll::Ready(None) => panic!("concluded early"),
Poll::Ready(Some(item)) => if pred(item) {
return Poll::Ready(s.take().unwrap())
},
Poll::Ready(Some(item)) =>
if pred(item) {
return Poll::Ready(s.take().unwrap())
},
Poll::Pending => return Poll::Pending,
}
})
@@ -145,8 +153,7 @@ fn config() -> crate::Config {
// dummy voter set state
fn voter_set_state() -> SharedVoterSetState<Block> {
use crate::authorities::AuthoritySet;
use crate::environment::VoterSetState;
use crate::{authorities::AuthoritySet, environment::VoterSetState};
use finality_grandpa::round::State as RoundState;
use sp_core::{crypto::Public, H256};
use sp_finality_grandpa::AuthorityId;
@@ -157,20 +164,13 @@ fn voter_set_state() -> SharedVoterSetState<Block> {
let voters = vec![(AuthorityId::from_slice(&[1; 32]), 1)];
let voters = AuthoritySet::genesis(voters).unwrap();
let set_state = VoterSetState::live(
0,
&voters,
base,
);
let set_state = VoterSetState::live(0, &voters, base);
set_state.into()
}
// needs to run in a tokio runtime.
pub(crate) fn make_test_network() -> (
impl Future<Output = Tester>,
TestNetwork,
) {
pub(crate) fn make_test_network() -> (impl Future<Output = Tester>, TestNetwork) {
let (tx, rx) = tracing_unbounded("test");
let net = TestNetwork { sender: tx };
@@ -185,13 +185,7 @@ pub(crate) fn make_test_network() -> (
}
}
let bridge = super::NetworkBridge::new(
net.clone(),
config(),
voter_set_state(),
None,
None,
);
let bridge = super::NetworkBridge::new(net.clone(), config(), voter_set_state(), None, None);
(
futures::future::ready(Tester {
@@ -204,19 +198,16 @@ pub(crate) fn make_test_network() -> (
}
fn make_ids(keys: &[Ed25519Keyring]) -> AuthorityList {
keys.iter()
.map(|key| key.clone().public().into())
.map(|id| (id, 1))
.collect()
keys.iter().map(|key| key.clone().public().into()).map(|id| (id, 1)).collect()
}
struct NoopContext;
impl sc_network_gossip::ValidatorContext<Block> for NoopContext {
fn broadcast_topic(&mut self, _: Hash, _: bool) { }
fn broadcast_message(&mut self, _: Hash, _: Vec<u8>, _: bool) { }
fn send_message(&mut self, _: &sc_network::PeerId, _: Vec<u8>) { }
fn send_topic(&mut self, _: &sc_network::PeerId, _: Hash, _: bool) { }
fn broadcast_topic(&mut self, _: Hash, _: bool) {}
fn broadcast_message(&mut self, _: Hash, _: Vec<u8>, _: bool) {}
fn send_message(&mut self, _: &sc_network::PeerId, _: Vec<u8>) {}
fn send_topic(&mut self, _: &sc_network::PeerId, _: Hash, _: bool) {}
}
#[test]
@@ -232,9 +223,12 @@ fn good_commit_leads_to_relay() {
let target_hash: Hash = [1; 32].into();
let target_number = 500;
let precommit = finality_grandpa::Precommit { target_hash: target_hash.clone(), target_number };
let precommit =
finality_grandpa::Precommit { target_hash: target_hash.clone(), target_number };
let payload = sp_finality_grandpa::localized_payload(
round, set_id, &finality_grandpa::Message::Precommit(precommit.clone())
round,
set_id,
&finality_grandpa::Message::Precommit(precommit.clone()),
);
let mut precommits = Vec::new();
@@ -247,24 +241,21 @@ fn good_commit_leads_to_relay() {
auth_data.push((signature, public[i].0.clone()))
}
finality_grandpa::CompactCommit {
target_hash,
target_number,
precommits,
auth_data,
}
finality_grandpa::CompactCommit { target_hash, target_number, precommits, auth_data }
};
let encoded_commit = gossip::GossipMessage::<Block>::Commit(gossip::FullCommitMessage {
round: Round(round),
set_id: SetId(set_id),
message: commit,
}).encode();
})
.encode();
let id = sc_network::PeerId::random();
let global_topic = super::global_topic::<Block>(set_id);
let test = make_test_network().0
let test = make_test_network()
.0
.then(move |tester| {
// register a peer.
tester.gossip_validator.new_peer(&mut NoopContext, &id, ObservedRole::Full);
@@ -272,7 +263,8 @@ fn good_commit_leads_to_relay() {
})
.then(move |(tester, id)| {
// start round, dispatch commit, and wait for broadcast.
let (commits_in, _) = tester.net_handle.global_communication(SetId(1), voter_set, false);
let (commits_in, _) =
tester.net_handle.global_communication(SetId(1), voter_set, false);
{
let (action, ..) = tester.gossip_validator.do_validate(&id, &encoded_commit[..]);
@@ -301,7 +293,10 @@ fn good_commit_leads_to_relay() {
let _ = sender.unbounded_send(NetworkEvent::NotificationsReceived {
remote: sender_id.clone(),
messages: vec![(GRANDPA_PROTOCOL_NAME.into(), commit_to_send.clone().into())],
messages: vec![(
GRANDPA_PROTOCOL_NAME.into(),
commit_to_send.clone().into(),
)],
});
// Add a random peer which will be the recipient of this message
@@ -316,13 +311,11 @@ fn good_commit_leads_to_relay() {
// Announce its local set has being on the current set id through a neighbor
// packet, otherwise it won't be eligible to receive the commit
let _ = {
let update = gossip::VersionedNeighborPacket::V1(
gossip::NeighborPacket {
round: Round(round),
set_id: SetId(set_id),
commit_finalized_height: 1,
}
);
let update = gossip::VersionedNeighborPacket::V1(gossip::NeighborPacket {
round: Round(round),
set_id: SetId(set_id),
commit_finalized_height: 1,
});
let msg = gossip::GossipMessage::<Block>::Neighbor(update);
@@ -333,31 +326,27 @@ fn good_commit_leads_to_relay() {
};
true
}
},
_ => false,
});
// when the commit comes in, we'll tell the callback it was good.
let handle_commit = commits_in.into_future()
.map(|(item, _)| {
match item.unwrap() {
finality_grandpa::voter::CommunicationIn::Commit(_, _, mut callback) => {
callback.run(finality_grandpa::voter::CommitProcessingOutcome::good());
},
_ => panic!("commit expected"),
}
});
let handle_commit = commits_in.into_future().map(|(item, _)| match item.unwrap() {
finality_grandpa::voter::CommunicationIn::Commit(_, _, mut callback) => {
callback.run(finality_grandpa::voter::CommitProcessingOutcome::good());
},
_ => panic!("commit expected"),
});
// once the message is sent and commit is "handled" we should have
// a repropagation event coming from the network.
let fut = future::join(send_message, handle_commit).then(move |(tester, ())| {
tester.filter_network_events(move |event| match event {
Event::WriteNotification(_, data) => {
data == encoded_commit
}
_ => false,
let fut = future::join(send_message, handle_commit)
.then(move |(tester, ())| {
tester.filter_network_events(move |event| match event {
Event::WriteNotification(_, data) => data == encoded_commit,
_ => false,
})
})
})
.map(|_| ());
// Poll both the future sending and handling the commit, as well as the underlying
@@ -382,9 +371,12 @@ fn bad_commit_leads_to_report() {
let target_hash: Hash = [1; 32].into();
let target_number = 500;
let precommit = finality_grandpa::Precommit { target_hash: target_hash.clone(), target_number };
let precommit =
finality_grandpa::Precommit { target_hash: target_hash.clone(), target_number };
let payload = sp_finality_grandpa::localized_payload(
round, set_id, &finality_grandpa::Message::Precommit(precommit.clone())
round,
set_id,
&finality_grandpa::Message::Precommit(precommit.clone()),
);
let mut precommits = Vec::new();
@@ -397,24 +389,21 @@ fn bad_commit_leads_to_report() {
auth_data.push((signature, public[i].0.clone()))
}
finality_grandpa::CompactCommit {
target_hash,
target_number,
precommits,
auth_data,
}
finality_grandpa::CompactCommit { target_hash, target_number, precommits, auth_data }
};
let encoded_commit = gossip::GossipMessage::<Block>::Commit(gossip::FullCommitMessage {
round: Round(round),
set_id: SetId(set_id),
message: commit,
}).encode();
})
.encode();
let id = sc_network::PeerId::random();
let global_topic = super::global_topic::<Block>(set_id);
let test = make_test_network().0
let test = make_test_network()
.0
.map(move |tester| {
// register a peer.
tester.gossip_validator.new_peer(&mut NoopContext, &id, ObservedRole::Full);
@@ -422,7 +411,8 @@ fn bad_commit_leads_to_report() {
})
.then(move |(tester, id)| {
// start round, dispatch commit, and wait for broadcast.
let (commits_in, _) = tester.net_handle.global_communication(SetId(1), voter_set, false);
let (commits_in, _) =
tester.net_handle.global_communication(SetId(1), voter_set, false);
{
let (action, ..) = tester.gossip_validator.do_validate(&id, &encoded_commit[..]);
@@ -449,35 +439,35 @@ fn bad_commit_leads_to_report() {
});
let _ = sender.unbounded_send(NetworkEvent::NotificationsReceived {
remote: sender_id.clone(),
messages: vec![(GRANDPA_PROTOCOL_NAME.into(), commit_to_send.clone().into())],
messages: vec![(
GRANDPA_PROTOCOL_NAME.into(),
commit_to_send.clone().into(),
)],
});
true
}
},
_ => false,
});
// when the commit comes in, we'll tell the callback it was bad.
let handle_commit = commits_in.into_future()
.map(|(item, _)| {
match item.unwrap() {
finality_grandpa::voter::CommunicationIn::Commit(_, _, mut callback) => {
callback.run(finality_grandpa::voter::CommitProcessingOutcome::bad());
},
_ => panic!("commit expected"),
}
});
let handle_commit = commits_in.into_future().map(|(item, _)| match item.unwrap() {
finality_grandpa::voter::CommunicationIn::Commit(_, _, mut callback) => {
callback.run(finality_grandpa::voter::CommitProcessingOutcome::bad());
},
_ => panic!("commit expected"),
});
// once the message is sent and commit is "handled" we should have
// a report event coming from the network.
let fut = future::join(send_message, handle_commit).then(move |(tester, ())| {
tester.filter_network_events(move |event| match event {
Event::Report(who, cost_benefit) => {
who == id && cost_benefit == super::cost::INVALID_COMMIT
}
_ => false,
let fut = future::join(send_message, handle_commit)
.then(move |(tester, ())| {
tester.filter_network_events(move |event| match event {
Event::Report(who, cost_benefit) =>
who == id && cost_benefit == super::cost::INVALID_COMMIT,
_ => false,
})
})
})
.map(|_| ());
// Poll both the future sending and handling the commit, as well as the underlying
@@ -508,7 +498,8 @@ fn peer_with_higher_view_leads_to_catch_up_request() {
set_id: SetId(0),
round: Round(10),
commit_finalized_height: 50,
}).encode(),
})
.encode(),
);
// neighbor packets are always discard
@@ -518,27 +509,23 @@ fn peer_with_higher_view_leads_to_catch_up_request() {
}
// a catch up request should be sent to the peer for round - 1
tester.filter_network_events(move |event| match event {
Event::WriteNotification(peer, message) => {
assert_eq!(
peer,
id,
);
tester
.filter_network_events(move |event| match event {
Event::WriteNotification(peer, message) => {
assert_eq!(peer, id,);
assert_eq!(
message,
gossip::GossipMessage::<Block>::CatchUpRequest(
gossip::CatchUpRequestMessage {
set_id: SetId(0),
round: Round(9),
}
).encode(),
);
assert_eq!(
message,
gossip::GossipMessage::<Block>::CatchUpRequest(
gossip::CatchUpRequestMessage { set_id: SetId(0), round: Round(9) }
)
.encode(),
);
true
},
_ => false,
})
true
},
_ => false,
})
.map(|_| ())
});