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

Change validation & collation protocol names to include genesis hash & fork id #5876

Merged
merged 13 commits into from
Aug 30, 2022
Merged
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
Prev Previous commit
Next Next commit
Add tests for PeerSetProtocolNames
  • Loading branch information
dmitry-markin committed Aug 13, 2022
commit ec48b1cd5a1b200b52db0417826c4b68624b2eab
70 changes: 70 additions & 0 deletions node/network/protocol/src/peer_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,73 @@ impl PeerSetProtocolNames {
std::iter::once(Self::get_legacy_name(protocol)).collect()
}
}

#[cfg(test)]
mod tests {
use super::{Hash, PeerSet, PeerSetProtocolNames};

#[test]
fn protocol_names_are_correctly_generated() {
let genesis_hash = Hash::from([
122, 200, 116, 29, 232, 183, 20, 109, 138, 86, 23, 253, 70, 41, 20, 85, 127, 230, 60,
38, 90, 127, 28, 16, 231, 218, 227, 40, 88, 238, 187, 128,
]);
let name = PeerSetProtocolNames::generate_name(&genesis_hash, None, PeerSet::Validation, 3);
let expected =
"/7ac8741de8b7146d8a5617fd462914557fe63c265a7f1c10e7dae32858eebb80/validation/3";
assert_eq!(name, expected);

let name = PeerSetProtocolNames::generate_name(&genesis_hash, None, PeerSet::Collation, 5);
let expected =
"/7ac8741de8b7146d8a5617fd462914557fe63c265a7f1c10e7dae32858eebb80/collation/5";
assert_eq!(name, expected);

let fork_id = Some("test-fork");
let name =
PeerSetProtocolNames::generate_name(&genesis_hash, fork_id, PeerSet::Validation, 7);
let expected =
"/7ac8741de8b7146d8a5617fd462914557fe63c265a7f1c10e7dae32858eebb80/test-fork/validation/7";
assert_eq!(name, expected);

let name =
PeerSetProtocolNames::generate_name(&genesis_hash, fork_id, PeerSet::Collation, 11);
let expected =
"/7ac8741de8b7146d8a5617fd462914557fe63c265a7f1c10e7dae32858eebb80/test-fork/collation/11";
assert_eq!(name, expected);
}

#[test]
fn all_protocol_names_are_known() {
let genesis_hash = Hash::from([
122, 200, 116, 29, 232, 183, 20, 109, 138, 86, 23, 253, 70, 41, 20, 85, 127, 230, 60,
38, 90, 127, 28, 16, 231, 218, 227, 40, 88, 238, 187, 128,
]);
let protocol_names = PeerSetProtocolNames::new(genesis_hash, None);

let validation_main =
"/7ac8741de8b7146d8a5617fd462914557fe63c265a7f1c10e7dae32858eebb80/validation/1";
assert_eq!(
protocol_names.try_get_protocol(&validation_main.into()),
Some((PeerSet::Validation, 1)),
);

let validation_legacy = "/polkadot/validation/1";
assert_eq!(
protocol_names.try_get_protocol(&validation_legacy.into()),
Some((PeerSet::Validation, 1)),
);

let collation_main =
"/7ac8741de8b7146d8a5617fd462914557fe63c265a7f1c10e7dae32858eebb80/collation/1";
assert_eq!(
protocol_names.try_get_protocol(&collation_main.into()),
Some((PeerSet::Collation, 1)),
);

let collation_legacy = "/polkadot/collation/1";
assert_eq!(
protocol_names.try_get_protocol(&collation_legacy.into()),
Some((PeerSet::Collation, 1)),
);
}
}