cargo +nightly fmt (#3540)

* cargo +nightly fmt

* add cargo-fmt check to ci

* update ci

* fmt

* fmt

* skip macro

* ignore bridges
This commit is contained in:
Shawn Tabrizi
2021-08-02 12:47:33 +02:00
committed by GitHub
parent 30e3012270
commit ff5d56fb76
350 changed files with 20617 additions and 21266 deletions
@@ -19,11 +19,9 @@
use thiserror::Error;
use polkadot_node_subsystem_util::{Fault, runtime};
use polkadot_subsystem::SubsystemError;
use polkadot_node_primitives::disputes::DisputeMessageCheckError;
use polkadot_node_subsystem_util::{runtime, Fault};
use polkadot_subsystem::SubsystemError;
#[derive(Debug, Error)]
#[error(transparent)]
@@ -14,8 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
use std::collections::{HashMap, HashSet, hash_map::Entry};
use std::collections::{hash_map::Entry, HashMap, HashSet};
use futures::channel::{mpsc, oneshot};
@@ -24,11 +23,10 @@ use polkadot_node_primitives::{CandidateVotes, DisputeMessage, SignedDisputeStat
use polkadot_node_subsystem_util::runtime::RuntimeInfo;
use polkadot_primitives::v1::{CandidateHash, DisputeStatement, Hash, SessionIndex};
use polkadot_subsystem::{
messages::{AllMessages, DisputeCoordinatorMessage},
ActiveLeavesUpdate, SubsystemContext,
messages::{AllMessages, DisputeCoordinatorMessage}
};
/// For each ongoing dispute we have a `SendTask` which takes care of it.
///
/// It is going to spawn real tasks as it sees fit for getting the votes of the particular dispute
@@ -39,10 +37,10 @@ pub use send_task::TaskFinish;
/// Error and [`Result`] type for sender
mod error;
pub use error::{Result, Error, Fatal, NonFatal};
pub use error::{Error, Fatal, NonFatal, Result};
use crate::{LOG_TARGET, Metrics};
use self::error::NonFatalResult;
use crate::{Metrics, LOG_TARGET};
/// The `DisputeSender` keeps track of all ongoing disputes we need to send statements out.
///
@@ -68,8 +66,7 @@ pub struct DisputeSender {
metrics: Metrics,
}
impl DisputeSender
{
impl DisputeSender {
/// Create a new `DisputeSender` which can be used to start dispute sendings.
pub fn new(tx: mpsc::Sender<TaskFinish>, metrics: Metrics) -> Self {
Self {
@@ -98,18 +95,13 @@ impl DisputeSender
"Dispute sending already active."
);
return Ok(())
}
},
Entry::Vacant(vacant) => {
let send_task = SendTask::new(
ctx,
runtime,
&self.active_sessions,
self.tx.clone(),
req,
)
.await?;
let send_task =
SendTask::new(ctx, runtime, &self.active_sessions, self.tx.clone(), req)
.await?;
vacant.insert(send_task);
}
},
}
Ok(())
}
@@ -143,9 +135,8 @@ impl DisputeSender
let active_disputes: HashSet<_> = active_disputes.into_iter().map(|(_, c)| c).collect();
// Cleanup obsolete senders:
self.disputes.retain(
|candidate_hash, _| active_disputes.contains(candidate_hash)
);
self.disputes
.retain(|candidate_hash, _| active_disputes.contains(candidate_hash));
for dispute in self.disputes.values_mut() {
if have_new_sessions || dispute.has_failed_sends() {
@@ -162,7 +153,6 @@ impl DisputeSender
/// Receive message from a sending task.
pub async fn on_task_message(&mut self, msg: TaskFinish) {
let TaskFinish { candidate_hash, receiver, result } = msg;
self.metrics.on_sent_request(result.as_metrics_label());
@@ -176,7 +166,7 @@ impl DisputeSender
"Received `FromSendingTask::Finished` for non existing dispute."
);
return
}
},
Some(task) => task,
};
task.on_finished_send(&receiver, result);
@@ -194,7 +184,9 @@ impl DisputeSender
let (session_index, candidate_hash) = dispute;
// We need some relay chain head for context for receiving session info information:
let ref_head = self.active_sessions.values().next().ok_or(NonFatal::NoActiveHeads)?;
let info = runtime.get_session_info_by_index(ctx.sender(), *ref_head, session_index).await?;
let info = runtime
.get_session_info_by_index(ctx.sender(), *ref_head, session_index)
.await?;
let our_index = match info.validator_info.our_index {
None => {
tracing::trace!(
@@ -202,7 +194,7 @@ impl DisputeSender
"Not a validator in that session - not starting dispute sending."
);
return Ok(())
}
},
Some(index) => index,
};
@@ -215,39 +207,25 @@ impl DisputeSender
"No votes for active dispute?! - possible, due to race."
);
return Ok(())
}
},
Some(votes) => votes,
};
let our_valid_vote = votes
.valid
.iter()
.find(|(_, i, _)| *i == our_index);
let our_valid_vote = votes.valid.iter().find(|(_, i, _)| *i == our_index);
let our_invalid_vote = votes
.invalid
.iter()
.find(|(_, i, _)| *i == our_index);
let our_invalid_vote = votes.invalid.iter().find(|(_, i, _)| *i == our_index);
let (valid_vote, invalid_vote) =
if let Some(our_valid_vote) = our_valid_vote {
// Get some invalid vote as well:
let invalid_vote = votes
.invalid
.get(0)
.ok_or(NonFatal::MissingVotesFromCoordinator)?;
(our_valid_vote, invalid_vote)
} else if let Some(our_invalid_vote) = our_invalid_vote {
// Get some valid vote as well:
let valid_vote = votes
.valid
.get(0)
.ok_or(NonFatal::MissingVotesFromCoordinator)?;
(valid_vote, our_invalid_vote)
} else {
return Err(From::from(NonFatal::MissingVotesFromCoordinator))
}
;
let (valid_vote, invalid_vote) = if let Some(our_valid_vote) = our_valid_vote {
// Get some invalid vote as well:
let invalid_vote = votes.invalid.get(0).ok_or(NonFatal::MissingVotesFromCoordinator)?;
(our_valid_vote, invalid_vote)
} else if let Some(our_invalid_vote) = our_invalid_vote {
// Get some valid vote as well:
let valid_vote = votes.valid.get(0).ok_or(NonFatal::MissingVotesFromCoordinator)?;
(valid_vote, our_invalid_vote)
} else {
return Err(From::from(NonFatal::MissingVotesFromCoordinator))
};
let (kind, valid_index, signature) = valid_vote;
let valid_public = info
.session_info
@@ -290,7 +268,7 @@ impl DisputeSender
invalid_signed,
*invalid_index,
votes.candidate_receipt,
&info.session_info
&info.session_info,
)
.map_err(NonFatal::InvalidDisputeFromCoordinator)?;
@@ -333,12 +311,13 @@ async fn get_active_session_indeces<Context: SubsystemContext>(
}
/// Retrieve Set of active disputes from the dispute coordinator.
async fn get_active_disputes<Context: SubsystemContext>(ctx: &mut Context)
-> NonFatalResult<Vec<(SessionIndex, CandidateHash)>> {
async fn get_active_disputes<Context: SubsystemContext>(
ctx: &mut Context,
) -> NonFatalResult<Vec<(SessionIndex, CandidateHash)>> {
let (tx, rx) = oneshot::channel();
ctx.send_message(AllMessages::DisputeCoordinator(
DisputeCoordinatorMessage::ActiveDisputes(tx)
))
ctx.send_message(AllMessages::DisputeCoordinator(DisputeCoordinatorMessage::ActiveDisputes(
tx,
)))
.await;
rx.await.map_err(|_| NonFatal::AskActiveDisputesCanceled)
}
@@ -351,10 +330,7 @@ async fn get_candidate_votes<Context: SubsystemContext>(
) -> NonFatalResult<Option<CandidateVotes>> {
let (tx, rx) = oneshot::channel();
ctx.send_message(AllMessages::DisputeCoordinator(
DisputeCoordinatorMessage::QueryCandidateVotes(
vec![(session_index, candidate_hash)],
tx
)
DisputeCoordinatorMessage::QueryCandidateVotes(vec![(session_index, candidate_hash)], tx),
))
.await;
rx.await
@@ -14,37 +14,32 @@
// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
use std::collections::{HashMap, HashSet};
use std::collections::HashMap;
use std::collections::HashSet;
use futures::Future;
use futures::FutureExt;
use futures::SinkExt;
use futures::channel::mpsc;
use futures::future::RemoteHandle;
use futures::{channel::mpsc, future::RemoteHandle, Future, FutureExt, SinkExt};
use polkadot_node_network_protocol::{
IfDisconnected,
request_response::{
OutgoingRequest, OutgoingResult, Recipient, Requests,
v1::{DisputeRequest, DisputeResponse},
}
OutgoingRequest, OutgoingResult, Recipient, Requests,
},
IfDisconnected,
};
use polkadot_node_subsystem_util::runtime::RuntimeInfo;
use polkadot_primitives::v1::{
AuthorityDiscoveryId, CandidateHash, Hash, SessionIndex, ValidatorIndex,
};
use polkadot_subsystem::{
SubsystemContext,
messages::{AllMessages, NetworkBridgeMessage},
SubsystemContext,
};
use super::error::{Fatal, Result};
use crate::LOG_TARGET;
use crate::metrics::FAILED;
use crate::metrics::SUCCEEDED;
use crate::{
metrics::{FAILED, SUCCEEDED},
LOG_TARGET,
};
/// Delivery status for a particular dispute.
///
@@ -104,27 +99,18 @@ impl TaskResult {
}
}
impl SendTask
{
impl SendTask {
/// Initiates sending a dispute message to peers.
pub async fn new<Context: SubsystemContext>(
ctx: &mut Context,
runtime: &mut RuntimeInfo,
active_sessions: &HashMap<SessionIndex,Hash>,
active_sessions: &HashMap<SessionIndex, Hash>,
tx: mpsc::Sender<TaskFinish>,
request: DisputeRequest,
) -> Result<Self> {
let mut send_task = Self {
request,
deliveries: HashMap::new(),
has_failed_sends: false,
tx,
};
send_task.refresh_sends(
ctx,
runtime,
active_sessions,
).await?;
let mut send_task =
Self { request, deliveries: HashMap::new(), has_failed_sends: false, tx };
send_task.refresh_sends(ctx, runtime, active_sessions).await?;
Ok(send_task)
}
@@ -150,12 +136,8 @@ impl SendTask
self.deliveries.retain(|k, _| new_authorities.contains(k));
// Start any new tasks that are needed:
let new_statuses = send_requests(
ctx,
self.tx.clone(),
add_authorities,
self.request.clone(),
).await?;
let new_statuses =
send_requests(ctx, self.tx.clone(), add_authorities, self.request.clone()).await?;
self.deliveries.extend(new_statuses.into_iter());
self.has_failed_sends = false;
@@ -180,7 +162,7 @@ impl SendTask
self.has_failed_sends = true;
// Remove state, so we know what to try again:
self.deliveries.remove(authority);
}
},
TaskResult::Succeeded => {
let status = match self.deliveries.get_mut(&authority) {
None => {
@@ -194,16 +176,15 @@ impl SendTask
"Received `FromSendingTask::Finished` for non existing task."
);
return
}
},
Some(status) => status,
};
// We are done here:
*status = DeliveryStatus::Succeeded;
}
},
}
}
/// Determine all validators that should receive the given dispute requests.
///
/// This is all parachain validators of the session the candidate occurred and all authorities
@@ -232,7 +213,8 @@ impl SendTask
// Current authorities:
for (session_index, head) in active_sessions.iter() {
let info = runtime.get_session_info_by_index(ctx.sender(), *head, *session_index).await?;
let info =
runtime.get_session_info_by_index(ctx.sender(), *head, *session_index).await?;
let session_info = &info.session_info;
let new_set = session_info
.discovery_keys
@@ -246,7 +228,6 @@ impl SendTask
}
}
/// Start sending of the given message to all given authorities.
///
/// And spawn tasks for handling the response.
@@ -260,10 +241,8 @@ async fn send_requests<Context: SubsystemContext>(
let mut reqs = Vec::with_capacity(receivers.len());
for receiver in receivers {
let (outgoing, pending_response) = OutgoingRequest::new(
Recipient::Authority(receiver.clone()),
req.clone(),
);
let (outgoing, pending_response) =
OutgoingRequest::new(Recipient::Authority(receiver.clone()), req.clone());
reqs.push(Requests::DisputeSending(outgoing));
@@ -275,8 +254,7 @@ async fn send_requests<Context: SubsystemContext>(
);
let (remote, remote_handle) = fut.remote_handle();
ctx.spawn("dispute-sender", remote.boxed())
.map_err(Fatal::SpawnTask)?;
ctx.spawn("dispute-sender", remote.boxed()).map_err(Fatal::SpawnTask)?;
statuses.insert(receiver, DeliveryStatus::Pending(remote_handle));
}
@@ -306,8 +284,8 @@ async fn wait_response_task(
%err,
"Error sending dispute statements to node."
);
TaskFinish { candidate_hash, receiver, result: TaskResult::Failed}
}
TaskFinish { candidate_hash, receiver, result: TaskResult::Failed }
},
Ok(DisputeResponse::Confirmed) => {
tracing::trace!(
target: LOG_TARGET,
@@ -316,7 +294,7 @@ async fn wait_response_task(
"Sending dispute message succeeded"
);
TaskFinish { candidate_hash, receiver, result: TaskResult::Succeeded }
}
},
};
if let Err(err) = tx.feed(msg).await {
tracing::debug!(