mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-08 14:48:01 +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
@@ -26,7 +26,7 @@ struct TestLink {}
|
||||
|
||||
impl Link<Block> for TestLink {}
|
||||
|
||||
fn prepare_good_block() -> (client::Client<test_client::Backend, test_client::Executor, Block, test_client::runtime::RuntimeApi>, Hash, u64, IncomingBlock<Block>) {
|
||||
fn prepare_good_block() -> (client::Client<test_client::Backend, test_client::Executor, Block, test_client::runtime::RuntimeApi>, Hash, u64, PeerId, IncomingBlock<Block>) {
|
||||
let client = test_client::new();
|
||||
let block = client.new_block().unwrap().bake().unwrap();
|
||||
client.import(BlockOrigin::File, block).unwrap();
|
||||
@@ -34,27 +34,28 @@ fn prepare_good_block() -> (client::Client<test_client::Backend, test_client::Ex
|
||||
let (hash, number) = (client.block_hash(1).unwrap().unwrap(), 1);
|
||||
let header = client.header(&BlockId::Number(1)).unwrap();
|
||||
let justification = client.justification(&BlockId::Number(1)).unwrap();
|
||||
(client, hash, number, IncomingBlock {
|
||||
let peer_id = PeerId::random();
|
||||
(client, hash, number, peer_id.clone(), IncomingBlock {
|
||||
hash,
|
||||
header,
|
||||
body: None,
|
||||
justification,
|
||||
origin: Some(0)
|
||||
origin: Some(peer_id.clone())
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn import_single_good_block_works() {
|
||||
let (_, _hash, number, block) = prepare_good_block();
|
||||
let (_, _hash, number, peer_id, block) = prepare_good_block();
|
||||
assert_eq!(
|
||||
import_single_block(&test_client::new(), BlockOrigin::File, block, Arc::new(PassThroughVerifier(true))),
|
||||
Ok(BlockImportResult::ImportedUnknown(number, Default::default(), Some(0)))
|
||||
Ok(BlockImportResult::ImportedUnknown(number, Default::default(), Some(peer_id)))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn import_single_good_known_block_is_ignored() {
|
||||
let (client, _hash, number, block) = prepare_good_block();
|
||||
let (client, _hash, number, _, block) = prepare_good_block();
|
||||
assert_eq!(
|
||||
import_single_block(&client, BlockOrigin::File, block, Arc::new(PassThroughVerifier(true))),
|
||||
Ok(BlockImportResult::ImportedKnown(number))
|
||||
@@ -63,11 +64,11 @@ fn import_single_good_known_block_is_ignored() {
|
||||
|
||||
#[test]
|
||||
fn import_single_good_block_without_header_fails() {
|
||||
let (_, _, _, mut block) = prepare_good_block();
|
||||
let (_, _, _, peer_id, mut block) = prepare_good_block();
|
||||
block.header = None;
|
||||
assert_eq!(
|
||||
import_single_block(&test_client::new(), BlockOrigin::File, block, Arc::new(PassThroughVerifier(true))),
|
||||
Err(BlockImportError::IncompleteHeader(Some(0)))
|
||||
Err(BlockImportError::IncompleteHeader(Some(peer_id)))
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -40,7 +40,7 @@ use crossbeam_channel::{self as channel, Sender, select};
|
||||
use futures::Future;
|
||||
use futures::sync::{mpsc, oneshot};
|
||||
use crate::message::{Message, ConsensusEngineId};
|
||||
use network_libp2p::{NodeIndex, PeerId};
|
||||
use network_libp2p::PeerId;
|
||||
use parity_codec::Encode;
|
||||
use parking_lot::{Mutex, RwLock};
|
||||
use primitives::{H256, ed25519::Public as AuthorityId};
|
||||
@@ -100,16 +100,16 @@ impl NetworkSpecialization<Block> for DummySpecialization {
|
||||
vec![]
|
||||
}
|
||||
|
||||
fn on_connect(&mut self, _ctx: &mut Context<Block>, _peer_id: NodeIndex, _status: crate::message::Status<Block>) {
|
||||
fn on_connect(&mut self, _ctx: &mut Context<Block>, _peer_id: PeerId, _status: crate::message::Status<Block>) {
|
||||
}
|
||||
|
||||
fn on_disconnect(&mut self, _ctx: &mut Context<Block>, _peer_id: NodeIndex) {
|
||||
fn on_disconnect(&mut self, _ctx: &mut Context<Block>, _peer_id: PeerId) {
|
||||
}
|
||||
|
||||
fn on_message(
|
||||
&mut self,
|
||||
_ctx: &mut Context<Block>,
|
||||
_peer_id: NodeIndex,
|
||||
_peer_id: PeerId,
|
||||
_message: &mut Option<crate::message::Message<Block>>,
|
||||
) {
|
||||
}
|
||||
@@ -166,7 +166,7 @@ impl<S: NetworkSpecialization<Block> + Clone> Link<Block> for TestLink<S> {
|
||||
self.link.blocks_processed(processed_blocks, has_error);
|
||||
}
|
||||
|
||||
fn justification_imported(&self, who: NodeIndex, hash: &Hash, number:NumberFor<Block>, success: bool) {
|
||||
fn justification_imported(&self, who: PeerId, hash: &Hash, number:NumberFor<Block>, success: bool) {
|
||||
self.link.justification_imported(who, hash, number, success);
|
||||
}
|
||||
|
||||
@@ -174,11 +174,11 @@ impl<S: NetworkSpecialization<Block> + Clone> Link<Block> for TestLink<S> {
|
||||
self.link.request_justification(hash, number);
|
||||
}
|
||||
|
||||
fn useless_peer(&self, who: NodeIndex, reason: &str) {
|
||||
fn useless_peer(&self, who: PeerId, reason: &str) {
|
||||
self.link.useless_peer(who, reason);
|
||||
}
|
||||
|
||||
fn note_useless_and_restart_sync(&self, who: NodeIndex, reason: &str) {
|
||||
fn note_useless_and_restart_sync(&self, who: PeerId, reason: &str) {
|
||||
self.link.note_useless_and_restart_sync(who, reason);
|
||||
}
|
||||
|
||||
@@ -190,7 +190,8 @@ impl<S: NetworkSpecialization<Block> + Clone> Link<Block> for TestLink<S> {
|
||||
pub struct Peer<D, S: NetworkSpecialization<Block> + Clone> {
|
||||
pub is_offline: Arc<AtomicBool>,
|
||||
pub is_major_syncing: Arc<AtomicBool>,
|
||||
pub peers: Arc<RwLock<HashMap<NodeIndex, ConnectedPeer<Block>>>>,
|
||||
pub peers: Arc<RwLock<HashMap<PeerId, ConnectedPeer<Block>>>>,
|
||||
pub peer_id: PeerId,
|
||||
client: Arc<PeersClient>,
|
||||
network_to_protocol_sender: Sender<FromNetworkMsg<Block>>,
|
||||
pub protocol_sender: Sender<ProtocolMsg<Block, S>>,
|
||||
@@ -206,7 +207,7 @@ impl<D, S: NetworkSpecialization<Block> + Clone> Peer<D, S> {
|
||||
fn new(
|
||||
is_offline: Arc<AtomicBool>,
|
||||
is_major_syncing: Arc<AtomicBool>,
|
||||
peers: Arc<RwLock<HashMap<NodeIndex, ConnectedPeer<Block>>>>,
|
||||
peers: Arc<RwLock<HashMap<PeerId, ConnectedPeer<Block>>>>,
|
||||
client: Arc<PeersClient>,
|
||||
import_queue: Box<ImportQueue<Block>>,
|
||||
network_to_protocol_sender: Sender<FromNetworkMsg<Block>>,
|
||||
@@ -222,6 +223,7 @@ impl<D, S: NetworkSpecialization<Block> + Clone> Peer<D, S> {
|
||||
is_offline,
|
||||
is_major_syncing,
|
||||
peers,
|
||||
peer_id: PeerId::random(),
|
||||
client,
|
||||
network_to_protocol_sender,
|
||||
protocol_sender,
|
||||
@@ -268,22 +270,22 @@ impl<D, S: NetworkSpecialization<Block> + Clone> Peer<D, S> {
|
||||
}
|
||||
|
||||
/// Called on connection to other indicated peer.
|
||||
fn on_connect(&self, other: NodeIndex) {
|
||||
let _ = self.network_to_protocol_sender.send(FromNetworkMsg::PeerConnected(PeerId::random(), other, String::new()));
|
||||
fn on_connect(&self, other: &Self) {
|
||||
let _ = self.network_to_protocol_sender.send(FromNetworkMsg::PeerConnected(other.peer_id.clone(), String::new()));
|
||||
}
|
||||
|
||||
/// Called on disconnect from other indicated peer.
|
||||
fn on_disconnect(&self, other: NodeIndex) {
|
||||
fn on_disconnect(&self, other: &Self) {
|
||||
let _ = self
|
||||
.network_to_protocol_sender
|
||||
.send(FromNetworkMsg::PeerDisconnected(other, String::new()));
|
||||
.send(FromNetworkMsg::PeerDisconnected(other.peer_id.clone(), String::new()));
|
||||
}
|
||||
|
||||
/// Receive a message from another peer. Return a set of peers to disconnect.
|
||||
fn receive_message(&self, from: NodeIndex, msg: Message<Block>) {
|
||||
fn receive_message(&self, from: &Self, msg: Message<Block>) {
|
||||
let _ = self
|
||||
.network_to_protocol_sender
|
||||
.send(FromNetworkMsg::CustomMessage(from, msg));
|
||||
.send(FromNetworkMsg::CustomMessage(from.peer_id.clone(), msg));
|
||||
}
|
||||
|
||||
/// Produce the next pending message to send to another peer.
|
||||
@@ -567,7 +569,7 @@ pub trait TestNetFactory: Sized {
|
||||
let is_offline = Arc::new(AtomicBool::new(true));
|
||||
let is_major_syncing = Arc::new(AtomicBool::new(false));
|
||||
let specialization = self::SpecializationFactory::create();
|
||||
let peers: Arc<RwLock<HashMap<NodeIndex, ConnectedPeer<Block>>>> = Arc::new(Default::default());
|
||||
let peers: Arc<RwLock<HashMap<PeerId, ConnectedPeer<Block>>>> = Arc::new(Default::default());
|
||||
|
||||
let (protocol_sender, network_to_protocol_sender) = Protocol::new(
|
||||
status_sinks,
|
||||
@@ -606,39 +608,38 @@ pub trait TestNetFactory: Sized {
|
||||
if self.started() {
|
||||
return;
|
||||
}
|
||||
self.mut_peers(|peers| {
|
||||
for peer in 0..peers.len() {
|
||||
peers[peer].start();
|
||||
for client in 0..peers.len() {
|
||||
if peer != client {
|
||||
peers[peer].on_connect(client as NodeIndex);
|
||||
}
|
||||
for peer in self.peers() {
|
||||
peer.start();
|
||||
for client in self.peers() {
|
||||
if peer.peer_id != client.peer_id {
|
||||
peer.on_connect(client);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
self.route(None);
|
||||
self.set_started(true);
|
||||
}
|
||||
|
||||
/// Do one step of routing.
|
||||
fn route(&mut self, disconnected: Option<HashSet<NodeIndex>>) {
|
||||
fn route(&mut self, disconnected: Option<HashSet<usize>>) {
|
||||
self.mut_peers(move |peers| {
|
||||
let mut to_disconnect = HashSet::new();
|
||||
for peer in 0..peers.len() {
|
||||
let packet = peers[peer].pending_message();
|
||||
for (peer_pos, peer) in peers.iter().enumerate() {
|
||||
let packet = peer.pending_message();
|
||||
match packet {
|
||||
None => continue,
|
||||
Some(NetworkMsg::Outgoing(recipient, packet)) => {
|
||||
let recipient = peers.iter().position(|p| p.peer_id == recipient).unwrap();
|
||||
if let Some(disconnected) = disconnected.as_ref() {
|
||||
let mut current = HashSet::new();
|
||||
current.insert(peer);
|
||||
current.insert(peer_pos);
|
||||
current.insert(recipient);
|
||||
// Not routing message between "disconnected" nodes.
|
||||
if disconnected.is_subset(¤t) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
peers[recipient].receive_message(peer as NodeIndex, packet)
|
||||
peers[recipient].receive_message(peer, packet)
|
||||
}
|
||||
Some(NetworkMsg::ReportPeer(who, _)) => {
|
||||
to_disconnect.insert(who);
|
||||
@@ -647,8 +648,10 @@ pub trait TestNetFactory: Sized {
|
||||
}
|
||||
}
|
||||
for d in to_disconnect {
|
||||
for peer in 0..peers.len() {
|
||||
peers[peer].on_disconnect(d);
|
||||
if let Some(d) = peers.iter().find(|p| p.peer_id == d) {
|
||||
for peer in 0..peers.len() {
|
||||
peers[peer].on_disconnect(d);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -659,7 +662,9 @@ pub trait TestNetFactory: Sized {
|
||||
self.mut_peers(move |peers| {
|
||||
for peer in 0..peers.len() {
|
||||
while let Some(NetworkMsg::Outgoing(recipient, packet)) = peers[peer].pending_message_fast() {
|
||||
peers[recipient].receive_message(peer as NodeIndex, packet)
|
||||
if let Some(p) = peers.iter().find(|p| p.peer_id == recipient) {
|
||||
p.receive_message(&peers[peer], packet)
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -701,7 +706,7 @@ pub trait TestNetFactory: Sized {
|
||||
|
||||
/// Perform synchronization until complete, if provided the
|
||||
/// given nodes set are excluded from sync.
|
||||
fn sync_with(&mut self, disconnected: Option<HashSet<NodeIndex>>) -> u32 {
|
||||
fn sync_with(&mut self, disconnected: Option<HashSet<usize>>) -> u32 {
|
||||
self.start();
|
||||
let mut total_steps = 0;
|
||||
let mut done = 0;
|
||||
@@ -730,7 +735,7 @@ pub trait TestNetFactory: Sized {
|
||||
|
||||
/// Perform synchronization until complete,
|
||||
/// excluding sync between certain nodes.
|
||||
fn sync_with_disconnected(&mut self, disconnected: HashSet<NodeIndex>) -> u32 {
|
||||
fn sync_with_disconnected(&mut self, disconnected: HashSet<usize>) -> u32 {
|
||||
self.sync_with(Some(disconnected))
|
||||
}
|
||||
|
||||
|
||||
@@ -18,7 +18,6 @@ use client::backend::Backend;
|
||||
use client::blockchain::HeaderBackend as BlockchainHeaderBackend;
|
||||
use crate::config::Roles;
|
||||
use consensus::BlockOrigin;
|
||||
use network_libp2p::NodeIndex;
|
||||
use std::collections::HashSet;
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
@@ -54,7 +53,7 @@ fn sync_peers_works() {
|
||||
// And then disconnect.
|
||||
for other in 0..3 {
|
||||
if other != peer {
|
||||
net.peer(peer).on_disconnect(other);
|
||||
net.peer(peer).on_disconnect(net.peer(other));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -100,7 +99,7 @@ fn sync_cycle_from_offline_to_syncing_to_offline() {
|
||||
for peer in 0..3 {
|
||||
for other in 0..3 {
|
||||
if other != peer {
|
||||
net.peer(peer).on_disconnect(other);
|
||||
net.peer(peer).on_disconnect(net.peer(other));
|
||||
}
|
||||
}
|
||||
thread::sleep(Duration::from_millis(100));
|
||||
@@ -125,8 +124,8 @@ fn syncing_node_not_major_syncing_when_disconnected() {
|
||||
assert!(net.peer(1).is_major_syncing());
|
||||
|
||||
// Disconnect peer 1 form everyone else.
|
||||
net.peer(1).on_disconnect(0);
|
||||
net.peer(1).on_disconnect(2);
|
||||
net.peer(1).on_disconnect(net.peer(0));
|
||||
net.peer(1).on_disconnect(net.peer(2));
|
||||
thread::sleep(Duration::from_millis(100));
|
||||
|
||||
// Peer 1 is not major-syncing.
|
||||
@@ -162,7 +161,7 @@ fn sync_from_two_peers_with_ancestry_search_works() {
|
||||
fn ancestry_search_works_when_backoff_is_one() {
|
||||
let _ = ::env_logger::try_init();
|
||||
let mut net = TestNet::new(3);
|
||||
|
||||
|
||||
net.peer(0).push_blocks(1, false);
|
||||
net.peer(1).push_blocks(2, false);
|
||||
net.peer(2).push_blocks(2, false);
|
||||
@@ -357,13 +356,13 @@ fn blocks_are_not_announced_by_light_nodes() {
|
||||
net.peer(0).start();
|
||||
net.peer(1).start();
|
||||
net.peer(2).start();
|
||||
net.peer(0).on_connect(1);
|
||||
net.peer(1).on_connect(2);
|
||||
net.peer(0).on_connect(net.peer(1));
|
||||
net.peer(1).on_connect(net.peer(2));
|
||||
|
||||
// Only sync between 0 -> 1, and 1 -> 2
|
||||
let mut disconnected = HashSet::new();
|
||||
disconnected.insert(0 as NodeIndex);
|
||||
disconnected.insert(2 as NodeIndex);
|
||||
disconnected.insert(0);
|
||||
disconnected.insert(2);
|
||||
net.sync_with_disconnected(disconnected);
|
||||
|
||||
// peer 0 has the best chain
|
||||
|
||||
Reference in New Issue
Block a user