Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Added proto3 compatibility tests. And import noise.
Browse files Browse the repository at this point in the history
  • Loading branch information
Attila Vágvölgyi committed Dec 3, 2021
1 parent f4c5727 commit cb009e0
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 4 deletions.
6 changes: 5 additions & 1 deletion client/authority-discovery/build.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
fn main() {
prost_build::compile_protos(&["src/worker/schema/dht.proto"], &["src/worker/schema"]).unwrap();
prost_build::compile_protos(
&["src/worker/schema/dht-v1.proto", "src/worker/schema/dht-v2.proto"],
&["src/worker/schema"],
)
.unwrap();
}
5 changes: 4 additions & 1 deletion client/authority-discovery/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ use sp_runtime::{generic::BlockId, traits::Block as BlockT};
mod addr_cache;
/// Dht payload schemas generated from Protobuf definitions via Prost crate in build.rs.
mod schema {
include!(concat!(env!("OUT_DIR"), "/authority_discovery.rs"));
#[cfg(test)]
mod tests;

include!(concat!(env!("OUT_DIR"), "/authority_discovery_v2.rs"));
}
#[cfg(test)]
pub mod tests;
Expand Down
14 changes: 14 additions & 0 deletions client/authority-discovery/src/worker/schema/dht-v1.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
syntax = "proto3";

package authority_discovery_v1;

// First we need to serialize the addresses in order to be able to sign them.
message AuthorityAddresses {
repeated bytes addresses = 1;
}

// Then we need to serialize addresses and signature to send them over the wire.
message SignedAuthorityAddresses {
bytes addresses = 1;
bytes signature = 2;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
syntax = "proto3";

package authority_discovery;
package authority_discovery_v2;

// First we need to serialize the addresses in order to be able to sign them.
message AuthorityRecord {
Expand Down
90 changes: 90 additions & 0 deletions client/authority-discovery/src/worker/schema/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// This file is part of Substrate.

// Copyright (C) 2017-2021 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

mod schema_v1 {
include!(concat!(env!("OUT_DIR"), "/authority_discovery_v1.rs"));
}

use super::*;
use libp2p::multiaddr::Multiaddr;
use prost::Message;
use sc_network::PeerId;

#[test]
fn v2_decodes_v1() {
let peer_id = PeerId::random();
let multiaddress: Multiaddr =
format!("/ip4/127.0.0.1/tcp/3003/p2p/{}", peer_id).parse().unwrap();
let vec_addresses = vec![multiaddress.to_vec()];
let vec_auth_signature = b"Totally valid signature, I promise!".to_vec();

let addresses_v1 = schema_v1::AuthorityAddresses { addresses: vec_addresses.clone() };
let mut vec_addresses_v1 = vec![];
addresses_v1.encode(&mut vec_addresses_v1).unwrap();
let signed_addresses_v1 = schema_v1::SignedAuthorityAddresses {
addresses: vec_addresses_v1.clone(),
signature: vec_auth_signature.clone(),
};
let mut vec_signed_addresses_v1 = vec![];
signed_addresses_v1.encode(&mut vec_signed_addresses_v1).unwrap();

let signed_record_v2_decoded =
SignedAuthorityRecord::decode(vec_signed_addresses_v1.as_slice()).unwrap();

assert_eq!(&signed_record_v2_decoded.record, &vec_addresses_v1);
assert_eq!(&signed_record_v2_decoded.auth_signature, &vec_auth_signature);
assert_eq!(&signed_record_v2_decoded.peer_signature, &None);

let record_v2_decoded = AuthorityRecord::decode(vec_addresses_v1.as_slice()).unwrap();
assert_eq!(&record_v2_decoded.addresses, &vec_addresses);
}

#[test]
fn v1_decodes_v2() {
let peer_secret = sc_network::Keypair::generate_ed25519();
let peer_public = peer_secret.public();
let peer_id = peer_public.to_peer_id();
let multiaddress: Multiaddr =
format!("/ip4/127.0.0.1/tcp/3003/p2p/{}", peer_id).parse().unwrap();
let vec_addresses = vec![multiaddress.to_vec()];
let vec_auth_signature = b"Totally valid signature, I promise!".to_vec();
let vec_peer_signature = b"Surprisingly hard to crack crypto".to_vec();

let record_v2 = AuthorityRecord { addresses: vec_addresses.clone() };
let mut vec_record_v2 = vec![];
record_v2.encode(&mut vec_record_v2).unwrap();
let vec_peer_public = peer_public.to_protobuf_encoding();
let peer_signature_v2 =
PeerSignature { public_key: vec_peer_public, signature: vec_peer_signature };
let signed_record_v2 = SignedAuthorityRecord {
record: vec_record_v2.clone(),
auth_signature: vec_auth_signature.clone(),
peer_signature: Some(peer_signature_v2.clone()),
};
let mut vec_signed_record_v2 = vec![];
signed_record_v2.encode(&mut vec_signed_record_v2).unwrap();

let signed_addresses_v1_decoded =
schema_v1::SignedAuthorityAddresses::decode(vec_signed_record_v2.as_slice()).unwrap();

assert_eq!(&signed_addresses_v1_decoded.addresses, &vec_record_v2);
assert_eq!(&signed_addresses_v1_decoded.signature, &vec_auth_signature);

let addresses_v2_decoded = AuthorityRecord::decode(vec_record_v2.as_slice()).unwrap();
assert_eq!(&addresses_v2_decoded.addresses, &vec_addresses);
}
6 changes: 5 additions & 1 deletion client/network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,11 @@ pub use protocol::{
sync::{StateDownloadProgress, SyncState, WarpSyncPhase, WarpSyncProgress},
PeerInfo,
};
pub use service::*;
pub use service::{
DecodingError, IfDisconnected, KademliaKey, Keypair, NetworkService, NetworkWorker,
NotificationSender, NotificationSenderReady, OutboundFailure, PublicKey, RequestFailure,
Signature, SigningError,
};

pub use sc_peerset::ReputationChange;
use sp_runtime::traits::{Block as BlockT, NumberFor};
Expand Down
20 changes: 20 additions & 0 deletions client/network/src/service/signature.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
// This file is part of Substrate.
//
// Copyright (C) 2017-2021 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
// If you read this, you are very thorough, congratulations.

use super::*;

/// A result of signing a message with a network identity. Since `PeerId` is potentially a hash of a
Expand Down

0 comments on commit cb009e0

Please sign in to comment.