Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A0-1584: Unify MockMultiaddress #770

Merged
merged 4 commits into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion finality-aleph/src/network/manager/compatibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,11 @@ mod test {
crypto::AuthorityVerifier,
network::{
manager::{compatibility::MAX_AUTHENTICATION_SIZE, SessionHandler},
mock::MockMultiaddress,
NetworkIdentity,
},
nodes::testing::new_pen,
tcp_network::{testing::new_identity, TcpMultiaddress},
testing::mocks::validator_network::MockMultiaddress,
NodeIndex, SessionId, Version,
};

Expand Down
9 changes: 6 additions & 3 deletions finality-aleph/src/network/manager/connections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,13 @@ mod tests {
use std::collections::HashSet;

use super::Connections;
use crate::{network::mock::MockPeerId, SessionId};
use crate::{
validator_network::mock::{random_keys, MockPublicKey},
SessionId,
};

fn random_peer_ids(num: usize) -> HashSet<MockPeerId> {
(0..num).map(|_| MockPeerId::random()).collect()
fn random_peer_ids(num: usize) -> HashSet<MockPublicKey> {
random_keys(num).into_keys().collect()
}

#[test]
Expand Down
10 changes: 4 additions & 6 deletions finality-aleph/src/network/manager/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,8 @@ mod tests {

use super::{Discovery, DiscoveryMessage};
use crate::{
network::{
manager::SessionHandler,
mock::{crypto_basics, MockMultiaddress, MockPeerId},
},
network::{manager::SessionHandler, mock::crypto_basics},
testing::mocks::validator_network::{random_identity, MockMultiaddress},
SessionId,
};

Expand All @@ -148,7 +146,7 @@ mod tests {

fn addresses() -> Vec<MockMultiaddress> {
(0..NUM_NODES)
.map(|_| MockMultiaddress::random_with_id(MockPeerId::random()))
.map(|_| random_identity().0[0].clone())
.collect()
}

Expand Down Expand Up @@ -177,7 +175,7 @@ mod tests {
None,
crypto_basics.1.clone(),
SessionId(43),
vec![MockMultiaddress::random_with_id(MockPeerId::random())],
random_identity().0,
)
.await
.unwrap();
Expand Down
7 changes: 4 additions & 3 deletions finality-aleph/src/network/manager/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,10 @@ mod tests {
use super::{get_common_peer_id, Handler, HandlerError};
use crate::{
network::{
mock::{crypto_basics, MockMultiaddress, MockNetworkIdentity, MockPeerId},
mock::{crypto_basics, MockNetworkIdentity},
NetworkIdentity,
},
testing::mocks::validator_network::{random_identity, MockMultiaddress},
NodeIndex, SessionId,
};

Expand Down Expand Up @@ -379,8 +380,8 @@ mod tests {
async fn fails_to_create_with_non_unique_peer_id() {
let mut crypto_basics = crypto_basics(NUM_NODES).await;
let addresses = vec![
MockMultiaddress::random_with_id(MockPeerId::random()),
MockMultiaddress::random_with_id(MockPeerId::random()),
random_identity().0[0].clone(),
lesniak43 marked this conversation as resolved.
Show resolved Hide resolved
random_identity().0[0].clone(),
];
assert!(matches!(
Handler::new(
Expand Down
85 changes: 15 additions & 70 deletions finality-aleph/src/network/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,98 +7,43 @@ use std::{

use aleph_primitives::KEY_TYPE;
use async_trait::async_trait;
use codec::{Decode, Encode};
use futures::{
channel::{mpsc, oneshot},
StreamExt,
};
use parking_lot::Mutex;
use rand::random;
use sp_keystore::{testing::KeyStore, CryptoStore};
use tokio::time::timeout;

use crate::{
crypto::{AuthorityPen, AuthorityVerifier},
network::{
manager::VersionedAuthentication, AddressedData, ConnectionCommand, Event, EventStream,
Multiaddress, Network, NetworkIdentity, NetworkSender, NetworkServiceIO, PeerId, Protocol,
Multiaddress, Network, NetworkIdentity, NetworkSender, NetworkServiceIO, Protocol,
},
testing::mocks::validator_network::{random_identity, MockMultiaddress},
validator_network::mock::MockPublicKey,
AuthorityId, NodeIndex,
};

#[derive(PartialEq, Eq, Copy, Clone, Debug, Hash, Encode, Decode)]
pub struct MockPeerId(u32);

impl MockPeerId {
pub fn random() -> Self {
MockPeerId(random())
}
}
impl fmt::Display for MockPeerId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}
}

impl PeerId for MockPeerId {}

#[derive(PartialEq, Eq, Clone, Debug, Hash, Encode, Decode)]
pub struct MockMultiaddress {
peer_id: Option<MockPeerId>,
address: u32,
}

impl MockMultiaddress {
pub fn random_with_id(peer_id: MockPeerId) -> Self {
MockMultiaddress {
peer_id: Some(peer_id),
address: random(),
}
}
}

impl Multiaddress for MockMultiaddress {
type PeerId = MockPeerId;

fn get_peer_id(&self) -> Option<Self::PeerId> {
self.peer_id
}

fn add_matching_peer_id(mut self, peer_id: Self::PeerId) -> Option<Self> {
match self.peer_id {
Some(old_peer_id) => match old_peer_id == peer_id {
true => Some(self),
false => None,
},
None => {
self.peer_id = Some(peer_id);
Some(self)
}
}
}
}

pub struct MockNetworkIdentity {
addresses: Vec<MockMultiaddress>,
peer_id: MockPeerId,
peer_id: MockPublicKey,
}

impl MockNetworkIdentity {
pub fn new() -> Self {
let peer_id = MockPeerId::random();
let addresses = (0..3)
.map(|_| MockMultiaddress::random_with_id(peer_id))
.collect();
let (addresses, peer_id) = random_identity();
MockNetworkIdentity { addresses, peer_id }
}
}

impl NetworkIdentity for MockNetworkIdentity {
type PeerId = MockPeerId;
type PeerId = MockPublicKey;
type Multiaddress = MockMultiaddress;

fn identity(&self) -> (Vec<Self::Multiaddress>, Self::PeerId) {
(self.addresses.clone(), self.peer_id)
(self.addresses.clone(), self.peer_id.clone())
}
}

Expand Down Expand Up @@ -152,7 +97,7 @@ impl<T> Default for Channel<T> {
}
}

pub type MockEvent = Event<MockMultiaddress, MockPeerId>;
pub type MockEvent = Event<MockMultiaddress, MockPublicKey>;

pub type MockData = Vec<u8>;

Expand Down Expand Up @@ -196,15 +141,15 @@ impl<M: Multiaddress + 'static> MockIO<M> {
pub struct MockEventStream(mpsc::UnboundedReceiver<MockEvent>);

#[async_trait]
impl EventStream<MockMultiaddress, MockPeerId> for MockEventStream {
impl EventStream<MockMultiaddress, MockPublicKey> for MockEventStream {
async fn next_event(&mut self) -> Option<MockEvent> {
self.0.next().await
}
}

pub struct MockNetworkSender {
sender: mpsc::UnboundedSender<(Vec<u8>, MockPeerId, Protocol)>,
peer_id: MockPeerId,
sender: mpsc::UnboundedSender<(Vec<u8>, MockPublicKey, Protocol)>,
peer_id: MockPublicKey,
protocol: Protocol,
error: Result<(), MockSenderError>,
}
Expand All @@ -219,7 +164,7 @@ impl NetworkSender for MockNetworkSender {
) -> Result<(), MockSenderError> {
self.error?;
self.sender
.unbounded_send((data.into(), self.peer_id, self.protocol))
.unbounded_send((data.into(), self.peer_id.clone(), self.protocol))
.unwrap();
Ok(())
}
Expand All @@ -228,8 +173,8 @@ impl NetworkSender for MockNetworkSender {
#[derive(Clone)]
pub struct MockNetwork {
pub add_reserved: Channel<(HashSet<MockMultiaddress>, Protocol)>,
pub remove_reserved: Channel<(HashSet<MockPeerId>, Protocol)>,
pub send_message: Channel<(Vec<u8>, MockPeerId, Protocol)>,
pub remove_reserved: Channel<(HashSet<MockPublicKey>, Protocol)>,
pub send_message: Channel<(Vec<u8>, MockPublicKey, Protocol)>,
pub event_sinks: Arc<Mutex<Vec<mpsc::UnboundedSender<MockEvent>>>>,
event_stream_taken_oneshot: Arc<Mutex<Option<oneshot::Sender<()>>>>,
pub create_sender_errors: Arc<Mutex<VecDeque<MockSenderError>>>,
Expand All @@ -256,7 +201,7 @@ impl std::error::Error for MockSenderError {}
impl Network for MockNetwork {
type SenderError = MockSenderError;
type NetworkSender = MockNetworkSender;
type PeerId = MockPeerId;
type PeerId = MockPublicKey;
type Multiaddress = MockMultiaddress;
type EventStream = MockEventStream;

Expand Down
Loading