mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-12 18:11:10 +00:00
Replace NodeIndex with PeerId everywhere (#2077)
* Replace NodeIndex with PeerId * Fix tests * More test fixing * Whitespace
This commit is contained in:
committed by
Arkadiy Paronyan
parent
6fa40ec199
commit
6e394464b8
@@ -20,7 +20,7 @@ use std::time::{Duration, Instant};
|
||||
use log::{debug, trace, warn};
|
||||
use crate::protocol::Context;
|
||||
use fork_tree::ForkTree;
|
||||
use network_libp2p::{Severity, NodeIndex};
|
||||
use network_libp2p::{Severity, PeerId};
|
||||
use client::{BlockStatus, ClientInfo};
|
||||
use consensus::BlockOrigin;
|
||||
use consensus::import_queue::{ImportQueue, IncomingBlock};
|
||||
@@ -96,8 +96,8 @@ type PendingJustification<B> = (<B as BlockT>::Hash, NumberFor<B>);
|
||||
struct PendingJustifications<B: BlockT> {
|
||||
justifications: ForkTree<B::Hash, NumberFor<B>, ()>,
|
||||
pending_requests: VecDeque<PendingJustification<B>>,
|
||||
peer_requests: HashMap<NodeIndex, PendingJustification<B>>,
|
||||
previous_requests: HashMap<PendingJustification<B>, Vec<(NodeIndex, Instant)>>,
|
||||
peer_requests: HashMap<PeerId, PendingJustification<B>>,
|
||||
previous_requests: HashMap<PendingJustification<B>, Vec<(PeerId, Instant)>>,
|
||||
importing_requests: HashSet<PendingJustification<B>>,
|
||||
}
|
||||
|
||||
@@ -117,7 +117,7 @@ impl<B: BlockT> PendingJustifications<B> {
|
||||
/// justification request for block #10 to a peer at block #2), and we also
|
||||
/// throttle requests to the same peer if a previous justification request
|
||||
/// yielded no results.
|
||||
fn dispatch(&mut self, peers: &mut HashMap<NodeIndex, PeerSync<B>>, protocol: &mut Context<B>) {
|
||||
fn dispatch(&mut self, peers: &mut HashMap<PeerId, PeerSync<B>>, protocol: &mut Context<B>) {
|
||||
if self.pending_requests.is_empty() {
|
||||
return;
|
||||
}
|
||||
@@ -134,11 +134,11 @@ impl<B: BlockT> PendingJustifications<B> {
|
||||
if sync.state != PeerSyncState::Available || self.peer_requests.contains_key(&peer) {
|
||||
None
|
||||
} else {
|
||||
Some((*peer, sync.best_number))
|
||||
Some((peer.clone(), sync.best_number))
|
||||
}
|
||||
}).collect::<VecDeque<_>>();
|
||||
|
||||
let mut last_peer = available_peers.back().map(|p| p.0);
|
||||
let mut last_peer = available_peers.back().map(|p| p.0.clone());
|
||||
let mut unhandled_requests = VecDeque::new();
|
||||
|
||||
loop {
|
||||
@@ -164,11 +164,11 @@ impl<B: BlockT> PendingJustifications<B> {
|
||||
};
|
||||
|
||||
if !peer_eligible {
|
||||
available_peers.push_back((peer, peer_best_number));
|
||||
available_peers.push_back((peer.clone(), peer_best_number));
|
||||
|
||||
// we tried all peers and none can answer this request
|
||||
if Some(peer) == last_peer {
|
||||
last_peer = available_peers.back().map(|p| p.0);
|
||||
last_peer = available_peers.back().map(|p| p.0.clone());
|
||||
|
||||
let request = self.pending_requests.pop_front()
|
||||
.expect("verified to be Some in the beginning of the loop; qed");
|
||||
@@ -179,12 +179,12 @@ impl<B: BlockT> PendingJustifications<B> {
|
||||
continue;
|
||||
}
|
||||
|
||||
last_peer = available_peers.back().map(|p| p.0);
|
||||
last_peer = available_peers.back().map(|p| p.0.clone());
|
||||
|
||||
let request = self.pending_requests.pop_front()
|
||||
.expect("verified to be Some in the beginning of the loop; qed");
|
||||
|
||||
self.peer_requests.insert(peer, request);
|
||||
self.peer_requests.insert(peer.clone(), request);
|
||||
|
||||
peers.get_mut(&peer)
|
||||
.expect("peer was is taken from available_peers; available_peers is a subset of peers; qed")
|
||||
@@ -235,7 +235,7 @@ impl<B: BlockT> PendingJustifications<B> {
|
||||
}
|
||||
|
||||
/// Retry any pending request if a peer disconnected.
|
||||
fn peer_disconnected(&mut self, who: NodeIndex) {
|
||||
fn peer_disconnected(&mut self, who: PeerId) {
|
||||
if let Some(request) = self.peer_requests.remove(&who) {
|
||||
self.pending_requests.push_front(request);
|
||||
}
|
||||
@@ -281,7 +281,7 @@ impl<B: BlockT> PendingJustifications<B> {
|
||||
/// was `None`.
|
||||
fn on_response(
|
||||
&mut self,
|
||||
who: NodeIndex,
|
||||
who: PeerId,
|
||||
justification: Option<Justification>,
|
||||
import_queue: &ImportQueue<B>,
|
||||
) {
|
||||
@@ -343,7 +343,7 @@ impl<B: BlockT> PendingJustifications<B> {
|
||||
/// Relay chain sync strategy.
|
||||
pub struct ChainSync<B: BlockT> {
|
||||
genesis_hash: B::Hash,
|
||||
peers: HashMap<NodeIndex, PeerSync<B>>,
|
||||
peers: HashMap<PeerId, PeerSync<B>>,
|
||||
blocks: BlockCollection<B>,
|
||||
best_queued_number: NumberFor<B>,
|
||||
best_queued_hash: B::Hash,
|
||||
@@ -436,8 +436,8 @@ impl<B: BlockT> ChainSync<B> {
|
||||
}
|
||||
|
||||
/// Returns peer sync status (if any).
|
||||
pub(crate) fn peer_info(&self, who: NodeIndex) -> Option<PeerInfo<B>> {
|
||||
self.peers.get(&who).map(|peer| {
|
||||
pub(crate) fn peer_info(&self, who: &PeerId) -> Option<PeerInfo<B>> {
|
||||
self.peers.get(who).map(|peer| {
|
||||
PeerInfo {
|
||||
best_hash: peer.best_hash,
|
||||
best_number: peer.best_number,
|
||||
@@ -457,7 +457,7 @@ impl<B: BlockT> ChainSync<B> {
|
||||
}
|
||||
|
||||
/// Handle new connected peer.
|
||||
pub(crate) fn new_peer(&mut self, protocol: &mut Context<B>, who: NodeIndex) {
|
||||
pub(crate) fn new_peer(&mut self, protocol: &mut Context<B>, who: PeerId) {
|
||||
// Initialize some variables to determine if
|
||||
// is_offline or is_major_syncing should be updated
|
||||
// after processing this new peer.
|
||||
@@ -465,7 +465,7 @@ impl<B: BlockT> ChainSync<B> {
|
||||
let previous_best_seen = self.best_seen_block();
|
||||
let previous_state = self.state(&previous_best_seen);
|
||||
|
||||
if let Some(info) = protocol.peer_info(who) {
|
||||
if let Some(info) = protocol.peer_info(&who) {
|
||||
let status = block_status(&*protocol.client(), &self.queue_blocks, info.best_hash);
|
||||
match (status, info.best_number) {
|
||||
(Err(e), _) => {
|
||||
@@ -497,7 +497,7 @@ impl<B: BlockT> ChainSync<B> {
|
||||
if our_best > As::sa(0) {
|
||||
let common_best = ::std::cmp::min(our_best, info.best_number);
|
||||
debug!(target:"sync", "New peer with unknown best hash {} ({}), searching for common ancestor.", info.best_hash, info.best_number);
|
||||
self.peers.insert(who, PeerSync {
|
||||
self.peers.insert(who.clone(), PeerSync {
|
||||
common_number: As::sa(0),
|
||||
best_hash: info.best_hash,
|
||||
best_number: info.best_number,
|
||||
@@ -508,7 +508,7 @@ impl<B: BlockT> ChainSync<B> {
|
||||
} else {
|
||||
// We are at genesis, just start downloading
|
||||
debug!(target:"sync", "New peer with best hash {} ({}).", info.best_hash, info.best_number);
|
||||
self.peers.insert(who, PeerSync {
|
||||
self.peers.insert(who.clone(), PeerSync {
|
||||
common_number: As::sa(0),
|
||||
best_hash: info.best_hash,
|
||||
best_number: info.best_number,
|
||||
@@ -520,7 +520,7 @@ impl<B: BlockT> ChainSync<B> {
|
||||
},
|
||||
(Ok(BlockStatus::Queued), _) | (Ok(BlockStatus::InChainWithState), _) | (Ok(BlockStatus::InChainPruned), _) => {
|
||||
debug!(target:"sync", "New peer with known best hash {} ({}).", info.best_hash, info.best_number);
|
||||
self.peers.insert(who, PeerSync {
|
||||
self.peers.insert(who.clone(), PeerSync {
|
||||
common_number: info.best_number,
|
||||
best_hash: info.best_hash,
|
||||
best_number: info.best_number,
|
||||
@@ -590,7 +590,7 @@ impl<B: BlockT> ChainSync<B> {
|
||||
pub(crate) fn on_block_data(
|
||||
&mut self,
|
||||
protocol: &mut Context<B>,
|
||||
who: NodeIndex,
|
||||
who: PeerId,
|
||||
request: message::BlockRequest<B>,
|
||||
response: message::BlockResponse<B>
|
||||
) {
|
||||
@@ -603,7 +603,7 @@ impl<B: BlockT> ChainSync<B> {
|
||||
let peer_state = peer.state.clone();
|
||||
match peer_state {
|
||||
PeerSyncState::DownloadingNew(start_block) => {
|
||||
self.blocks.clear_peer_download(who);
|
||||
self.blocks.clear_peer_download(&who);
|
||||
peer.state = PeerSyncState::Available;
|
||||
self.blocks.insert(start_block, blocks, who);
|
||||
self.blocks
|
||||
@@ -627,7 +627,7 @@ impl<B: BlockT> ChainSync<B> {
|
||||
header: b.header,
|
||||
body: b.body,
|
||||
justification: b.justification,
|
||||
origin: Some(who),
|
||||
origin: Some(who.clone()),
|
||||
}
|
||||
}).collect()
|
||||
},
|
||||
@@ -698,7 +698,7 @@ impl<B: BlockT> ChainSync<B> {
|
||||
pub(crate) fn on_block_justification_data(
|
||||
&mut self,
|
||||
protocol: &mut Context<B>,
|
||||
who: NodeIndex,
|
||||
who: PeerId,
|
||||
_request: message::BlockRequest<B>,
|
||||
response: message::BlockResponse<B>,
|
||||
) {
|
||||
@@ -757,7 +757,7 @@ impl<B: BlockT> ChainSync<B> {
|
||||
if self.is_stopping.load(Ordering::SeqCst) {
|
||||
return
|
||||
}
|
||||
let peers: Vec<NodeIndex> = self.peers.keys().map(|p| *p).collect();
|
||||
let peers: Vec<PeerId> = self.peers.keys().map(|p| p.clone()).collect();
|
||||
for peer in peers {
|
||||
self.download_new(protocol, peer);
|
||||
}
|
||||
@@ -846,7 +846,7 @@ impl<B: BlockT> ChainSync<B> {
|
||||
}
|
||||
|
||||
/// Handle new block announcement.
|
||||
pub(crate) fn on_block_announce(&mut self, protocol: &mut Context<B>, who: NodeIndex, hash: B::Hash, header: &B::Header) {
|
||||
pub(crate) fn on_block_announce(&mut self, protocol: &mut Context<B>, who: PeerId, hash: B::Hash, header: &B::Header) {
|
||||
let number = *header.number();
|
||||
if number <= As::sa(0) {
|
||||
trace!(target: "sync", "Ignored invalid block announcement from {}: {}", who, hash);
|
||||
@@ -921,10 +921,10 @@ impl<B: BlockT> ChainSync<B> {
|
||||
}
|
||||
|
||||
/// Handle disconnected peer.
|
||||
pub(crate) fn peer_disconnected(&mut self, protocol: &mut Context<B>, who: NodeIndex) {
|
||||
pub(crate) fn peer_disconnected(&mut self, protocol: &mut Context<B>, who: PeerId) {
|
||||
let previous_best_seen = self.best_seen_block();
|
||||
let previous_state = self.state(&previous_best_seen);
|
||||
self.blocks.clear_peer_download(who);
|
||||
self.blocks.clear_peer_download(&who);
|
||||
self.peers.remove(&who);
|
||||
if self.peers.len() == 0 {
|
||||
// We're not connected to any peer anymore.
|
||||
@@ -958,7 +958,7 @@ impl<B: BlockT> ChainSync<B> {
|
||||
self.best_queued_number = As::sa(0);
|
||||
}
|
||||
}
|
||||
let ids: Vec<NodeIndex> = self.peers.drain().map(|(id, _)| id).collect();
|
||||
let ids: Vec<PeerId> = self.peers.drain().map(|(id, _)| id).collect();
|
||||
for id in ids {
|
||||
self.new_peer(protocol, id);
|
||||
}
|
||||
@@ -971,7 +971,7 @@ impl<B: BlockT> ChainSync<B> {
|
||||
}
|
||||
|
||||
// Download old block with known parent.
|
||||
fn download_stale(&mut self, protocol: &mut Context<B>, who: NodeIndex, hash: &B::Hash) {
|
||||
fn download_stale(&mut self, protocol: &mut Context<B>, who: PeerId, hash: &B::Hash) {
|
||||
if let Some(ref mut peer) = self.peers.get_mut(&who) {
|
||||
match peer.state {
|
||||
PeerSyncState::Available => {
|
||||
@@ -992,7 +992,7 @@ impl<B: BlockT> ChainSync<B> {
|
||||
}
|
||||
|
||||
// Download old block with unknown parent.
|
||||
fn download_unknown_stale(&mut self, protocol: &mut Context<B>, who: NodeIndex, hash: &B::Hash) {
|
||||
fn download_unknown_stale(&mut self, protocol: &mut Context<B>, who: PeerId, hash: &B::Hash) {
|
||||
if let Some(ref mut peer) = self.peers.get_mut(&who) {
|
||||
match peer.state {
|
||||
PeerSyncState::Available => {
|
||||
@@ -1013,7 +1013,7 @@ impl<B: BlockT> ChainSync<B> {
|
||||
}
|
||||
|
||||
// Issue a request for a peer to download new blocks, if any are available
|
||||
fn download_new(&mut self, protocol: &mut Context<B>, who: NodeIndex) {
|
||||
fn download_new(&mut self, protocol: &mut Context<B>, who: PeerId) {
|
||||
if let Some(ref mut peer) = self.peers.get_mut(&who) {
|
||||
// when there are too many blocks in the queue => do not try to download new blocks
|
||||
if self.queue_blocks.len() > MAX_IMPORTING_BLOCKS {
|
||||
@@ -1023,7 +1023,7 @@ impl<B: BlockT> ChainSync<B> {
|
||||
match peer.state {
|
||||
PeerSyncState::Available => {
|
||||
trace!(target: "sync", "Considering new block download from {}, common block is {}, best is {:?}", who, peer.common_number, peer.best_number);
|
||||
if let Some(range) = self.blocks.needed_blocks(who, MAX_BLOCKS_TO_REQUEST, peer.best_number, peer.common_number) {
|
||||
if let Some(range) = self.blocks.needed_blocks(who.clone(), MAX_BLOCKS_TO_REQUEST, peer.best_number, peer.common_number) {
|
||||
trace!(target: "sync", "Requesting blocks from {}, ({} to {})", who, range.start, range.end);
|
||||
let request = message::generic::BlockRequest {
|
||||
id: 0,
|
||||
@@ -1044,7 +1044,7 @@ impl<B: BlockT> ChainSync<B> {
|
||||
}
|
||||
}
|
||||
|
||||
fn request_ancestry(protocol: &mut Context<B>, who: NodeIndex, block: NumberFor<B>) {
|
||||
fn request_ancestry(protocol: &mut Context<B>, who: PeerId, block: NumberFor<B>) {
|
||||
trace!(target: "sync", "Requesting ancestry block #{} from {}", block, who);
|
||||
let request = message::generic::BlockRequest {
|
||||
id: 0,
|
||||
|
||||
Reference in New Issue
Block a user