mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-19 19:21:03 +00:00
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:
@@ -18,10 +18,18 @@
|
||||
|
||||
use futures::prelude::*;
|
||||
use libp2p::PeerId;
|
||||
use rand::distributions::{Distribution, Uniform, WeightedIndex};
|
||||
use rand::seq::IteratorRandom;
|
||||
use sc_peerset::{DropReason, IncomingIndex, Message, Peerset, PeersetConfig, ReputationChange, SetConfig, SetId};
|
||||
use std::{collections::HashMap, collections::HashSet, pin::Pin, task::Poll};
|
||||
use rand::{
|
||||
distributions::{Distribution, Uniform, WeightedIndex},
|
||||
seq::IteratorRandom,
|
||||
};
|
||||
use sc_peerset::{
|
||||
DropReason, IncomingIndex, Message, Peerset, PeersetConfig, ReputationChange, SetConfig, SetId,
|
||||
};
|
||||
use std::{
|
||||
collections::{HashMap, HashSet},
|
||||
pin::Pin,
|
||||
task::Poll,
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn run() {
|
||||
@@ -40,30 +48,28 @@ fn test_once() {
|
||||
let mut reserved_nodes = HashSet::<PeerId>::new();
|
||||
|
||||
let (mut peerset, peerset_handle) = Peerset::from_config(PeersetConfig {
|
||||
sets: vec![
|
||||
SetConfig {
|
||||
bootnodes: (0..Uniform::new_inclusive(0, 4).sample(&mut rng))
|
||||
sets: vec![SetConfig {
|
||||
bootnodes: (0..Uniform::new_inclusive(0, 4).sample(&mut rng))
|
||||
.map(|_| {
|
||||
let id = PeerId::random();
|
||||
known_nodes.insert(id.clone());
|
||||
id
|
||||
})
|
||||
.collect(),
|
||||
reserved_nodes: {
|
||||
(0..Uniform::new_inclusive(0, 2).sample(&mut rng))
|
||||
.map(|_| {
|
||||
let id = PeerId::random();
|
||||
known_nodes.insert(id.clone());
|
||||
reserved_nodes.insert(id.clone());
|
||||
id
|
||||
})
|
||||
.collect(),
|
||||
reserved_nodes: {
|
||||
(0..Uniform::new_inclusive(0, 2).sample(&mut rng))
|
||||
.map(|_| {
|
||||
let id = PeerId::random();
|
||||
known_nodes.insert(id.clone());
|
||||
reserved_nodes.insert(id.clone());
|
||||
id
|
||||
})
|
||||
.collect()
|
||||
},
|
||||
in_peers: Uniform::new_inclusive(0, 25).sample(&mut rng),
|
||||
out_peers: Uniform::new_inclusive(0, 25).sample(&mut rng),
|
||||
reserved_only: Uniform::new_inclusive(0, 10).sample(&mut rng) == 0,
|
||||
.collect()
|
||||
},
|
||||
],
|
||||
in_peers: Uniform::new_inclusive(0, 25).sample(&mut rng),
|
||||
out_peers: Uniform::new_inclusive(0, 25).sample(&mut rng),
|
||||
reserved_only: Uniform::new_inclusive(0, 10).sample(&mut rng) == 0,
|
||||
}],
|
||||
});
|
||||
|
||||
futures::executor::block_on(futures::future::poll_fn(move |cx| {
|
||||
@@ -81,33 +87,28 @@ fn test_once() {
|
||||
for _ in 0..2500 {
|
||||
// Each of these weights corresponds to an action that we may perform.
|
||||
let action_weights = [150, 90, 90, 30, 30, 1, 1, 4, 4];
|
||||
match WeightedIndex::new(&action_weights)
|
||||
.unwrap()
|
||||
.sample(&mut rng)
|
||||
{
|
||||
match WeightedIndex::new(&action_weights).unwrap().sample(&mut rng) {
|
||||
// If we generate 0, poll the peerset.
|
||||
0 => match Stream::poll_next(Pin::new(&mut peerset), cx) {
|
||||
Poll::Ready(Some(Message::Connect { peer_id, .. })) => {
|
||||
if let Some(id) = incoming_nodes
|
||||
.iter()
|
||||
.find(|(_, v)| **v == peer_id)
|
||||
.map(|(&id, _)| id)
|
||||
if let Some(id) =
|
||||
incoming_nodes.iter().find(|(_, v)| **v == peer_id).map(|(&id, _)| id)
|
||||
{
|
||||
incoming_nodes.remove(&id);
|
||||
}
|
||||
assert!(connected_nodes.insert(peer_id));
|
||||
}
|
||||
},
|
||||
Poll::Ready(Some(Message::Drop { peer_id, .. })) => {
|
||||
connected_nodes.remove(&peer_id);
|
||||
}
|
||||
},
|
||||
Poll::Ready(Some(Message::Accept(n))) => {
|
||||
assert!(connected_nodes.insert(incoming_nodes.remove(&n).unwrap()))
|
||||
}
|
||||
},
|
||||
Poll::Ready(Some(Message::Reject(n))) => {
|
||||
assert!(!connected_nodes.contains(&incoming_nodes.remove(&n).unwrap()))
|
||||
}
|
||||
},
|
||||
Poll::Ready(None) => panic!(),
|
||||
Poll::Pending => {}
|
||||
Poll::Pending => {},
|
||||
},
|
||||
|
||||
// If we generate 1, discover a new node.
|
||||
@@ -115,32 +116,29 @@ fn test_once() {
|
||||
let new_id = PeerId::random();
|
||||
known_nodes.insert(new_id.clone());
|
||||
peerset.add_to_peers_set(SetId::from(0), new_id);
|
||||
}
|
||||
},
|
||||
|
||||
// If we generate 2, adjust a random reputation.
|
||||
2 => {
|
||||
2 =>
|
||||
if let Some(id) = known_nodes.iter().choose(&mut rng) {
|
||||
let val = Uniform::new_inclusive(i32::MIN, i32::MAX)
|
||||
.sample(&mut rng);
|
||||
let val = Uniform::new_inclusive(i32::MIN, i32::MAX).sample(&mut rng);
|
||||
peerset_handle.report_peer(id.clone(), ReputationChange::new(val, ""));
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
// If we generate 3, disconnect from a random node.
|
||||
3 => {
|
||||
3 =>
|
||||
if let Some(id) = connected_nodes.iter().choose(&mut rng).cloned() {
|
||||
connected_nodes.remove(&id);
|
||||
peerset.dropped(SetId::from(0), id, DropReason::Unknown);
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
// If we generate 4, connect to a random node.
|
||||
4 => {
|
||||
if let Some(id) = known_nodes
|
||||
.iter()
|
||||
.filter(|n| {
|
||||
incoming_nodes.values().all(|m| m != *n)
|
||||
&& !connected_nodes.contains(*n)
|
||||
incoming_nodes.values().all(|m| m != *n) &&
|
||||
!connected_nodes.contains(*n)
|
||||
})
|
||||
.choose(&mut rng)
|
||||
{
|
||||
@@ -148,7 +146,7 @@ fn test_once() {
|
||||
incoming_nodes.insert(next_incoming_id.clone(), id.clone());
|
||||
next_incoming_id.0 += 1;
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
// 5 and 6 are the reserved-only mode.
|
||||
5 => peerset_handle.set_reserved_only(SetId::from(0), true),
|
||||
@@ -156,21 +154,18 @@ fn test_once() {
|
||||
|
||||
// 7 and 8 are about switching a random node in or out of reserved mode.
|
||||
7 => {
|
||||
if let Some(id) = known_nodes
|
||||
.iter()
|
||||
.filter(|n| !reserved_nodes.contains(*n))
|
||||
.choose(&mut rng)
|
||||
if let Some(id) =
|
||||
known_nodes.iter().filter(|n| !reserved_nodes.contains(*n)).choose(&mut rng)
|
||||
{
|
||||
peerset_handle.add_reserved_peer(SetId::from(0), id.clone());
|
||||
reserved_nodes.insert(id.clone());
|
||||
}
|
||||
}
|
||||
8 => {
|
||||
},
|
||||
8 =>
|
||||
if let Some(id) = reserved_nodes.iter().choose(&mut rng).cloned() {
|
||||
reserved_nodes.remove(&id);
|
||||
peerset_handle.remove_reserved_peer(SetId::from(0), id);
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
_ => unreachable!(),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user