mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-01 10:07:56 +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:
@@ -25,26 +25,35 @@
|
||||
//! configuration as an extra peers set.
|
||||
//! - Use [`TransactionsHandlerPrototype::build`] then [`TransactionsHandler::run`] to obtain a
|
||||
//! `Future` that processes transactions.
|
||||
//!
|
||||
|
||||
use crate::{
|
||||
ExHashT, Event, ObservedRole,
|
||||
config::{self, ProtocolId, TransactionPool, TransactionImportFuture, TransactionImport},
|
||||
error, protocol::message, service::NetworkService, utils::{interval, LruHashSet},
|
||||
config::{self, ProtocolId, TransactionImport, TransactionImportFuture, TransactionPool},
|
||||
error,
|
||||
protocol::message,
|
||||
service::NetworkService,
|
||||
utils::{interval, LruHashSet},
|
||||
Event, ExHashT, ObservedRole,
|
||||
};
|
||||
|
||||
use codec::{Decode, Encode};
|
||||
use futures::{channel::mpsc, prelude::*, stream::FuturesUnordered};
|
||||
use libp2p::{multiaddr, PeerId};
|
||||
use log::{trace, debug, warn};
|
||||
use prometheus_endpoint::{
|
||||
Registry, Counter, PrometheusError, register, U64
|
||||
};
|
||||
use log::{debug, trace, warn};
|
||||
use prometheus_endpoint::{register, Counter, PrometheusError, Registry, U64};
|
||||
use sp_runtime::traits::Block as BlockT;
|
||||
use std::borrow::Cow;
|
||||
use std::collections::{HashMap, hash_map::Entry};
|
||||
use std::sync::{atomic::{AtomicBool, Ordering}, Arc};
|
||||
use std::{iter, num::NonZeroUsize, pin::Pin, task::Poll, time};
|
||||
use std::{
|
||||
borrow::Cow,
|
||||
collections::{hash_map::Entry, HashMap},
|
||||
iter,
|
||||
num::NonZeroUsize,
|
||||
pin::Pin,
|
||||
sync::{
|
||||
atomic::{AtomicBool, Ordering},
|
||||
Arc,
|
||||
},
|
||||
task::Poll,
|
||||
time,
|
||||
};
|
||||
|
||||
/// Interval at which we propagate transactions;
|
||||
const PROPAGATE_TIMEOUT: time::Duration = time::Duration::from_millis(2900);
|
||||
@@ -84,10 +93,13 @@ struct Metrics {
|
||||
impl Metrics {
|
||||
fn register(r: &Registry) -> Result<Self, PrometheusError> {
|
||||
Ok(Metrics {
|
||||
propagated_transactions: register(Counter::new(
|
||||
"sync_propagated_transactions",
|
||||
"Number of transactions propagated to at least one peer",
|
||||
)?, r)?,
|
||||
propagated_transactions: register(
|
||||
Counter::new(
|
||||
"sync_propagated_transactions",
|
||||
"Number of transactions propagated to at least one peer",
|
||||
)?,
|
||||
r,
|
||||
)?,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -106,7 +118,7 @@ impl<H: ExHashT> Future for PendingTransaction<H> {
|
||||
let mut this = self.project();
|
||||
|
||||
if let Poll::Ready(import_result) = Pin::new(&mut this.validation).poll_unpin(cx) {
|
||||
return Poll::Ready((this.tx_hash.clone(), import_result));
|
||||
return Poll::Ready((this.tx_hash.clone(), import_result))
|
||||
}
|
||||
|
||||
Poll::Pending
|
||||
@@ -128,7 +140,7 @@ impl TransactionsHandlerPrototype {
|
||||
proto.push_str(protocol_id.as_ref());
|
||||
proto.push_str("/transactions/1");
|
||||
proto
|
||||
})
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -143,7 +155,7 @@ impl TransactionsHandlerPrototype {
|
||||
out_peers: 0,
|
||||
reserved_nodes: Vec::new(),
|
||||
non_reserved_mode: config::NonReservedPeerMode::Deny,
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -182,10 +194,7 @@ impl TransactionsHandlerPrototype {
|
||||
},
|
||||
};
|
||||
|
||||
let controller = TransactionsHandlerController {
|
||||
to_handler,
|
||||
gossip_enabled,
|
||||
};
|
||||
let controller = TransactionsHandlerController { to_handler, gossip_enabled };
|
||||
|
||||
Ok((handler, controller))
|
||||
}
|
||||
@@ -264,7 +273,7 @@ impl<B: BlockT + 'static, H: ExHashT> TransactionsHandler<B, H> {
|
||||
/// interrupted.
|
||||
pub async fn run(mut self) {
|
||||
loop {
|
||||
futures::select!{
|
||||
futures::select! {
|
||||
_ = self.propagate_timeout.next().fuse() => {
|
||||
self.propagate_transactions();
|
||||
},
|
||||
@@ -301,7 +310,7 @@ impl<B: BlockT + 'static, H: ExHashT> TransactionsHandler<B, H> {
|
||||
.collect::<multiaddr::Multiaddr>();
|
||||
let result = self.service.add_peers_to_reserved_set(
|
||||
self.protocol_name.clone(),
|
||||
iter::once(addr).collect()
|
||||
iter::once(addr).collect(),
|
||||
);
|
||||
if let Err(err) = result {
|
||||
log::error!(target: "sync", "Add reserved peer failed: {}", err);
|
||||
@@ -312,22 +321,30 @@ impl<B: BlockT + 'static, H: ExHashT> TransactionsHandler<B, H> {
|
||||
.collect::<multiaddr::Multiaddr>();
|
||||
let result = self.service.remove_peers_from_reserved_set(
|
||||
self.protocol_name.clone(),
|
||||
iter::once(addr).collect()
|
||||
iter::once(addr).collect(),
|
||||
);
|
||||
if let Err(err) = result {
|
||||
log::error!(target: "sync", "Removing reserved peer failed: {}", err);
|
||||
}
|
||||
},
|
||||
|
||||
Event::NotificationStreamOpened { remote, protocol, role, .. } if protocol == self.protocol_name => {
|
||||
let _was_in = self.peers.insert(remote, Peer {
|
||||
known_transactions: LruHashSet::new(NonZeroUsize::new(MAX_KNOWN_TRANSACTIONS)
|
||||
.expect("Constant is nonzero")),
|
||||
role,
|
||||
});
|
||||
Event::NotificationStreamOpened { remote, protocol, role, .. }
|
||||
if protocol == self.protocol_name =>
|
||||
{
|
||||
let _was_in = self.peers.insert(
|
||||
remote,
|
||||
Peer {
|
||||
known_transactions: LruHashSet::new(
|
||||
NonZeroUsize::new(MAX_KNOWN_TRANSACTIONS).expect("Constant is nonzero"),
|
||||
),
|
||||
role,
|
||||
},
|
||||
);
|
||||
debug_assert!(_was_in.is_none());
|
||||
}
|
||||
Event::NotificationStreamClosed { remote, protocol } if protocol == self.protocol_name => {
|
||||
Event::NotificationStreamClosed { remote, protocol }
|
||||
if protocol == self.protocol_name =>
|
||||
{
|
||||
let _peer = self.peers.remove(&remote);
|
||||
debug_assert!(_peer.is_some());
|
||||
}
|
||||
@@ -335,7 +352,7 @@ impl<B: BlockT + 'static, H: ExHashT> TransactionsHandler<B, H> {
|
||||
Event::NotificationsReceived { remote, messages } => {
|
||||
for (protocol, message) in messages {
|
||||
if protocol != self.protocol_name {
|
||||
continue;
|
||||
continue
|
||||
}
|
||||
|
||||
if let Ok(m) = <message::Transactions<B::Extrinsic> as Decode>::decode(
|
||||
@@ -349,28 +366,24 @@ impl<B: BlockT + 'static, H: ExHashT> TransactionsHandler<B, H> {
|
||||
},
|
||||
|
||||
// Not our concern.
|
||||
Event::NotificationStreamOpened { .. } | Event::NotificationStreamClosed { .. } => {}
|
||||
Event::NotificationStreamOpened { .. } | Event::NotificationStreamClosed { .. } => {},
|
||||
}
|
||||
}
|
||||
|
||||
/// Called when peer sends us new transactions
|
||||
fn on_transactions(
|
||||
&mut self,
|
||||
who: PeerId,
|
||||
transactions: message::Transactions<B::Extrinsic>,
|
||||
) {
|
||||
fn on_transactions(&mut self, who: PeerId, transactions: message::Transactions<B::Extrinsic>) {
|
||||
// sending transaction to light node is considered a bad behavior
|
||||
if matches!(self.local_role, config::Role::Light) {
|
||||
debug!(target: "sync", "Peer {} is trying to send transactions to the light node", who);
|
||||
self.service.disconnect_peer(who, self.protocol_name.clone());
|
||||
self.service.report_peer(who, rep::UNEXPECTED_TRANSACTIONS);
|
||||
return;
|
||||
return
|
||||
}
|
||||
|
||||
// Accept transactions only when enabled
|
||||
if !self.gossip_enabled.load(Ordering::Relaxed) {
|
||||
trace!(target: "sync", "{} Ignoring transactions while disabled", who);
|
||||
return;
|
||||
return
|
||||
}
|
||||
|
||||
trace!(target: "sync", "Received {} transactions from {}", transactions.len(), who);
|
||||
@@ -382,7 +395,7 @@ impl<B: BlockT + 'static, H: ExHashT> TransactionsHandler<B, H> {
|
||||
"Ignoring any further transactions that exceed `MAX_PENDING_TRANSACTIONS`({}) limit",
|
||||
MAX_PENDING_TRANSACTIONS,
|
||||
);
|
||||
break;
|
||||
break
|
||||
}
|
||||
|
||||
let hash = self.transaction_pool.hash_of(&t);
|
||||
@@ -400,7 +413,7 @@ impl<B: BlockT + 'static, H: ExHashT> TransactionsHandler<B, H> {
|
||||
},
|
||||
Entry::Occupied(mut entry) => {
|
||||
entry.get_mut().push(who.clone());
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -408,7 +421,8 @@ impl<B: BlockT + 'static, H: ExHashT> TransactionsHandler<B, H> {
|
||||
|
||||
fn on_handle_transaction_import(&mut self, who: PeerId, import: TransactionImport) {
|
||||
match import {
|
||||
TransactionImport::KnownGood => self.service.report_peer(who, rep::ANY_TRANSACTION_REFUND),
|
||||
TransactionImport::KnownGood =>
|
||||
self.service.report_peer(who, rep::ANY_TRANSACTION_REFUND),
|
||||
TransactionImport::NewGood => self.service.report_peer(who, rep::GOOD_TRANSACTION),
|
||||
TransactionImport::Bad => self.service.report_peer(who, rep::BAD_TRANSACTION),
|
||||
TransactionImport::None => {},
|
||||
@@ -416,14 +430,11 @@ impl<B: BlockT + 'static, H: ExHashT> TransactionsHandler<B, H> {
|
||||
}
|
||||
|
||||
/// Propagate one transaction.
|
||||
pub fn propagate_transaction(
|
||||
&mut self,
|
||||
hash: &H,
|
||||
) {
|
||||
pub fn propagate_transaction(&mut self, hash: &H) {
|
||||
debug!(target: "sync", "Propagating transaction [{:?}]", hash);
|
||||
// Accept transactions only when enabled
|
||||
if !self.gossip_enabled.load(Ordering::Relaxed) {
|
||||
return;
|
||||
return
|
||||
}
|
||||
if let Some(transaction) = self.transaction_pool.transaction(hash) {
|
||||
let propagated_to = self.do_propagate_transactions(&[(hash.clone(), transaction)]);
|
||||
@@ -441,7 +452,7 @@ impl<B: BlockT + 'static, H: ExHashT> TransactionsHandler<B, H> {
|
||||
for (who, peer) in self.peers.iter_mut() {
|
||||
// never send transactions to the light node
|
||||
if matches!(peer.role, ObservedRole::Light) {
|
||||
continue;
|
||||
continue
|
||||
}
|
||||
|
||||
let (hashes, to_send): (Vec<_>, Vec<_>) = transactions
|
||||
@@ -454,16 +465,13 @@ impl<B: BlockT + 'static, H: ExHashT> TransactionsHandler<B, H> {
|
||||
|
||||
if !to_send.is_empty() {
|
||||
for hash in hashes {
|
||||
propagated_to
|
||||
.entry(hash)
|
||||
.or_default()
|
||||
.push(who.to_base58());
|
||||
propagated_to.entry(hash).or_default().push(who.to_base58());
|
||||
}
|
||||
trace!(target: "sync", "Sending {} transactions to {}", to_send.len(), who);
|
||||
self.service.write_notification(
|
||||
who.clone(),
|
||||
self.protocol_name.clone(),
|
||||
to_send.encode()
|
||||
to_send.encode(),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -479,7 +487,7 @@ impl<B: BlockT + 'static, H: ExHashT> TransactionsHandler<B, H> {
|
||||
fn propagate_transactions(&mut self) {
|
||||
// Accept transactions only when enabled
|
||||
if !self.gossip_enabled.load(Ordering::Relaxed) {
|
||||
return;
|
||||
return
|
||||
}
|
||||
debug!(target: "sync", "Propagating transactions");
|
||||
let transactions = self.transaction_pool.transactions();
|
||||
|
||||
Reference in New Issue
Block a user