mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-12 21:41:12 +00:00
change to vecdeque (#5937)
This commit is contained in:
@@ -29,7 +29,7 @@ use libp2p::swarm::{NetworkBehaviourAction, NetworkBehaviourEventProcess, PollPa
|
|||||||
use log::debug;
|
use log::debug;
|
||||||
use sp_consensus::{BlockOrigin, import_queue::{IncomingBlock, Origin}};
|
use sp_consensus::{BlockOrigin, import_queue::{IncomingBlock, Origin}};
|
||||||
use sp_runtime::{traits::{Block as BlockT, NumberFor}, ConsensusEngineId, Justification};
|
use sp_runtime::{traits::{Block as BlockT, NumberFor}, ConsensusEngineId, Justification};
|
||||||
use std::{borrow::Cow, iter, task::{Context, Poll}, time::Duration};
|
use std::{borrow::Cow, collections::VecDeque, iter, task::{Context, Poll}, time::Duration};
|
||||||
|
|
||||||
/// General behaviour of the network. Combines all protocols together.
|
/// General behaviour of the network. Combines all protocols together.
|
||||||
#[derive(NetworkBehaviour)]
|
#[derive(NetworkBehaviour)]
|
||||||
@@ -51,7 +51,7 @@ pub struct Behaviour<B: BlockT, H: ExHashT> {
|
|||||||
|
|
||||||
/// Queue of events to produce for the outside.
|
/// Queue of events to produce for the outside.
|
||||||
#[behaviour(ignore)]
|
#[behaviour(ignore)]
|
||||||
events: Vec<BehaviourOut<B>>,
|
events: VecDeque<BehaviourOut<B>>,
|
||||||
|
|
||||||
/// Role of our local node, as originally passed from the configuration.
|
/// Role of our local node, as originally passed from the configuration.
|
||||||
#[behaviour(ignore)]
|
#[behaviour(ignore)]
|
||||||
@@ -118,7 +118,7 @@ impl<B: BlockT, H: ExHashT> Behaviour<B, H> {
|
|||||||
block_requests,
|
block_requests,
|
||||||
finality_proof_requests,
|
finality_proof_requests,
|
||||||
light_client_handler,
|
light_client_handler,
|
||||||
events: Vec::new(),
|
events: VecDeque::new(),
|
||||||
role,
|
role,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -183,7 +183,7 @@ impl<B: BlockT, H: ExHashT> Behaviour<B, H> {
|
|||||||
engine_id,
|
engine_id,
|
||||||
role,
|
role,
|
||||||
};
|
};
|
||||||
self.events.push(BehaviourOut::Event(ev));
|
self.events.push_back(BehaviourOut::Event(ev));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -241,26 +241,26 @@ Behaviour<B, H> {
|
|||||||
fn inject_event(&mut self, event: CustomMessageOutcome<B>) {
|
fn inject_event(&mut self, event: CustomMessageOutcome<B>) {
|
||||||
match event {
|
match event {
|
||||||
CustomMessageOutcome::BlockImport(origin, blocks) =>
|
CustomMessageOutcome::BlockImport(origin, blocks) =>
|
||||||
self.events.push(BehaviourOut::BlockImport(origin, blocks)),
|
self.events.push_back(BehaviourOut::BlockImport(origin, blocks)),
|
||||||
CustomMessageOutcome::JustificationImport(origin, hash, nb, justification) =>
|
CustomMessageOutcome::JustificationImport(origin, hash, nb, justification) =>
|
||||||
self.events.push(BehaviourOut::JustificationImport(origin, hash, nb, justification)),
|
self.events.push_back(BehaviourOut::JustificationImport(origin, hash, nb, justification)),
|
||||||
CustomMessageOutcome::FinalityProofImport(origin, hash, nb, proof) =>
|
CustomMessageOutcome::FinalityProofImport(origin, hash, nb, proof) =>
|
||||||
self.events.push(BehaviourOut::FinalityProofImport(origin, hash, nb, proof)),
|
self.events.push_back(BehaviourOut::FinalityProofImport(origin, hash, nb, proof)),
|
||||||
CustomMessageOutcome::BlockRequest { target, request } => {
|
CustomMessageOutcome::BlockRequest { target, request } => {
|
||||||
match self.block_requests.send_request(&target, request) {
|
match self.block_requests.send_request(&target, request) {
|
||||||
block_requests::SendRequestOutcome::Ok => {
|
block_requests::SendRequestOutcome::Ok => {
|
||||||
self.events.push(BehaviourOut::RequestStarted {
|
self.events.push_back(BehaviourOut::RequestStarted {
|
||||||
peer: target,
|
peer: target,
|
||||||
protocol: self.block_requests.protocol_name().to_vec(),
|
protocol: self.block_requests.protocol_name().to_vec(),
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
block_requests::SendRequestOutcome::Replaced { request_duration, .. } => {
|
block_requests::SendRequestOutcome::Replaced { request_duration, .. } => {
|
||||||
self.events.push(BehaviourOut::RequestFinished {
|
self.events.push_back(BehaviourOut::RequestFinished {
|
||||||
peer: target.clone(),
|
peer: target.clone(),
|
||||||
protocol: self.block_requests.protocol_name().to_vec(),
|
protocol: self.block_requests.protocol_name().to_vec(),
|
||||||
request_duration,
|
request_duration,
|
||||||
});
|
});
|
||||||
self.events.push(BehaviourOut::RequestStarted {
|
self.events.push_back(BehaviourOut::RequestStarted {
|
||||||
peer: target,
|
peer: target,
|
||||||
protocol: self.block_requests.protocol_name().to_vec(),
|
protocol: self.block_requests.protocol_name().to_vec(),
|
||||||
});
|
});
|
||||||
@@ -275,7 +275,7 @@ Behaviour<B, H> {
|
|||||||
CustomMessageOutcome::NotificationStreamOpened { remote, protocols, roles } => {
|
CustomMessageOutcome::NotificationStreamOpened { remote, protocols, roles } => {
|
||||||
let role = reported_roles_to_observed_role(&self.role, &remote, roles);
|
let role = reported_roles_to_observed_role(&self.role, &remote, roles);
|
||||||
for engine_id in protocols {
|
for engine_id in protocols {
|
||||||
self.events.push(BehaviourOut::Event(Event::NotificationStreamOpened {
|
self.events.push_back(BehaviourOut::Event(Event::NotificationStreamOpened {
|
||||||
remote: remote.clone(),
|
remote: remote.clone(),
|
||||||
engine_id,
|
engine_id,
|
||||||
role: role.clone(),
|
role: role.clone(),
|
||||||
@@ -284,14 +284,14 @@ Behaviour<B, H> {
|
|||||||
},
|
},
|
||||||
CustomMessageOutcome::NotificationStreamClosed { remote, protocols } =>
|
CustomMessageOutcome::NotificationStreamClosed { remote, protocols } =>
|
||||||
for engine_id in protocols {
|
for engine_id in protocols {
|
||||||
self.events.push(BehaviourOut::Event(Event::NotificationStreamClosed {
|
self.events.push_back(BehaviourOut::Event(Event::NotificationStreamClosed {
|
||||||
remote: remote.clone(),
|
remote: remote.clone(),
|
||||||
engine_id,
|
engine_id,
|
||||||
}));
|
}));
|
||||||
},
|
},
|
||||||
CustomMessageOutcome::NotificationsReceived { remote, messages } => {
|
CustomMessageOutcome::NotificationsReceived { remote, messages } => {
|
||||||
let ev = Event::NotificationsReceived { remote, messages };
|
let ev = Event::NotificationsReceived { remote, messages };
|
||||||
self.events.push(BehaviourOut::Event(ev));
|
self.events.push_back(BehaviourOut::Event(ev));
|
||||||
},
|
},
|
||||||
CustomMessageOutcome::PeerNewBest(peer_id, number) => {
|
CustomMessageOutcome::PeerNewBest(peer_id, number) => {
|
||||||
self.light_client_handler.update_best_block(&peer_id, number);
|
self.light_client_handler.update_best_block(&peer_id, number);
|
||||||
@@ -305,14 +305,14 @@ impl<B: BlockT, H: ExHashT> NetworkBehaviourEventProcess<block_requests::Event<B
|
|||||||
fn inject_event(&mut self, event: block_requests::Event<B>) {
|
fn inject_event(&mut self, event: block_requests::Event<B>) {
|
||||||
match event {
|
match event {
|
||||||
block_requests::Event::AnsweredRequest { peer, total_handling_time } => {
|
block_requests::Event::AnsweredRequest { peer, total_handling_time } => {
|
||||||
self.events.push(BehaviourOut::AnsweredRequest {
|
self.events.push_back(BehaviourOut::AnsweredRequest {
|
||||||
peer,
|
peer,
|
||||||
protocol: self.block_requests.protocol_name().to_vec(),
|
protocol: self.block_requests.protocol_name().to_vec(),
|
||||||
build_time: total_handling_time,
|
build_time: total_handling_time,
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
block_requests::Event::Response { peer, original_request, response, request_duration } => {
|
block_requests::Event::Response { peer, original_request, response, request_duration } => {
|
||||||
self.events.push(BehaviourOut::RequestFinished {
|
self.events.push_back(BehaviourOut::RequestFinished {
|
||||||
peer: peer.clone(),
|
peer: peer.clone(),
|
||||||
protocol: self.block_requests.protocol_name().to_vec(),
|
protocol: self.block_requests.protocol_name().to_vec(),
|
||||||
request_duration,
|
request_duration,
|
||||||
@@ -324,7 +324,7 @@ impl<B: BlockT, H: ExHashT> NetworkBehaviourEventProcess<block_requests::Event<B
|
|||||||
// There doesn't exist any mechanism to report cancellations yet.
|
// There doesn't exist any mechanism to report cancellations yet.
|
||||||
// We would normally disconnect the node, but this event happens as the result of
|
// We would normally disconnect the node, but this event happens as the result of
|
||||||
// a disconnect, so there's nothing more to do.
|
// a disconnect, so there's nothing more to do.
|
||||||
self.events.push(BehaviourOut::RequestFinished {
|
self.events.push_back(BehaviourOut::RequestFinished {
|
||||||
peer,
|
peer,
|
||||||
protocol: self.block_requests.protocol_name().to_vec(),
|
protocol: self.block_requests.protocol_name().to_vec(),
|
||||||
request_duration,
|
request_duration,
|
||||||
@@ -333,7 +333,7 @@ impl<B: BlockT, H: ExHashT> NetworkBehaviourEventProcess<block_requests::Event<B
|
|||||||
block_requests::Event::RequestTimeout { peer, request_duration, .. } => {
|
block_requests::Event::RequestTimeout { peer, request_duration, .. } => {
|
||||||
// There doesn't exist any mechanism to report timeouts yet, so we process them by
|
// There doesn't exist any mechanism to report timeouts yet, so we process them by
|
||||||
// disconnecting the node.
|
// disconnecting the node.
|
||||||
self.events.push(BehaviourOut::RequestFinished {
|
self.events.push_back(BehaviourOut::RequestFinished {
|
||||||
peer: peer.clone(),
|
peer: peer.clone(),
|
||||||
protocol: self.block_requests.protocol_name().to_vec(),
|
protocol: self.block_requests.protocol_name().to_vec(),
|
||||||
request_duration,
|
request_duration,
|
||||||
@@ -396,20 +396,20 @@ impl<B: BlockT, H: ExHashT> NetworkBehaviourEventProcess<DiscoveryOut>
|
|||||||
self.substrate.add_discovered_nodes(iter::once(peer_id));
|
self.substrate.add_discovered_nodes(iter::once(peer_id));
|
||||||
}
|
}
|
||||||
DiscoveryOut::ValueFound(results) => {
|
DiscoveryOut::ValueFound(results) => {
|
||||||
self.events.push(BehaviourOut::Event(Event::Dht(DhtEvent::ValueFound(results))));
|
self.events.push_back(BehaviourOut::Event(Event::Dht(DhtEvent::ValueFound(results))));
|
||||||
}
|
}
|
||||||
DiscoveryOut::ValueNotFound(key) => {
|
DiscoveryOut::ValueNotFound(key) => {
|
||||||
self.events.push(BehaviourOut::Event(Event::Dht(DhtEvent::ValueNotFound(key))));
|
self.events.push_back(BehaviourOut::Event(Event::Dht(DhtEvent::ValueNotFound(key))));
|
||||||
}
|
}
|
||||||
DiscoveryOut::ValuePut(key) => {
|
DiscoveryOut::ValuePut(key) => {
|
||||||
self.events.push(BehaviourOut::Event(Event::Dht(DhtEvent::ValuePut(key))));
|
self.events.push_back(BehaviourOut::Event(Event::Dht(DhtEvent::ValuePut(key))));
|
||||||
}
|
}
|
||||||
DiscoveryOut::ValuePutFailed(key) => {
|
DiscoveryOut::ValuePutFailed(key) => {
|
||||||
self.events.push(BehaviourOut::Event(Event::Dht(DhtEvent::ValuePutFailed(key))));
|
self.events.push_back(BehaviourOut::Event(Event::Dht(DhtEvent::ValuePutFailed(key))));
|
||||||
}
|
}
|
||||||
DiscoveryOut::RandomKademliaStarted(protocols) => {
|
DiscoveryOut::RandomKademliaStarted(protocols) => {
|
||||||
for protocol in protocols {
|
for protocol in protocols {
|
||||||
self.events.push(BehaviourOut::RandomKademliaStarted(protocol));
|
self.events.push_back(BehaviourOut::RandomKademliaStarted(protocol));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -418,8 +418,8 @@ impl<B: BlockT, H: ExHashT> NetworkBehaviourEventProcess<DiscoveryOut>
|
|||||||
|
|
||||||
impl<B: BlockT, H: ExHashT> Behaviour<B, H> {
|
impl<B: BlockT, H: ExHashT> Behaviour<B, H> {
|
||||||
fn poll<TEv>(&mut self, _: &mut Context, _: &mut impl PollParameters) -> Poll<NetworkBehaviourAction<TEv, BehaviourOut<B>>> {
|
fn poll<TEv>(&mut self, _: &mut Context, _: &mut impl PollParameters) -> Poll<NetworkBehaviourAction<TEv, BehaviourOut<B>>> {
|
||||||
if !self.events.is_empty() {
|
if let Some(event) = self.events.pop_front() {
|
||||||
return Poll::Ready(NetworkBehaviourAction::GenerateEvent(self.events.remove(0)))
|
return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event))
|
||||||
}
|
}
|
||||||
|
|
||||||
Poll::Pending
|
Poll::Pending
|
||||||
|
|||||||
Reference in New Issue
Block a user