Skip to content

Commit 57dd541

Browse files
acatangiugrishasobol
authored andcommitted
BEEFY and GRANDPA protocol names should use full genesis hash (paritytech#10974)
std::fmt::Display shows formats as reduced hash (e.g. 0xb0a8…dafe) Use hex::encode to format full hash. Signed-off-by: acatangiu <[email protected]>
1 parent 28d8fa9 commit 57dd541

File tree

6 files changed

+103
-6
lines changed

6 files changed

+103
-6
lines changed

Cargo.lock

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client/beefy/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ description = "BEEFY Client gadget for substrate"
1010
[dependencies]
1111
fnv = "1.0.6"
1212
futures = "0.3"
13+
hex = "0.4.2"
1314
log = "0.4"
1415
parking_lot = "0.12.0"
1516
thiserror = "1.0"
@@ -39,4 +40,5 @@ beefy-primitives = { version = "4.0.0-dev", path = "../../primitives/beefy" }
3940
sp-tracing = { version = "5.0.0", path = "../../primitives/tracing" }
4041
sc-network-test = { version = "0.8.0", path = "../network/test" }
4142

43+
serde = "1.0.136"
4244
strum = { version = "0.23", features = ["derive"] }

client/beefy/src/lib.rs

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ pub(crate) mod beefy_protocol_name {
5353
/// Name of the notifications protocol used by BEEFY.
5454
///
5555
/// Must be registered towards the networking in order for BEEFY to properly function.
56-
pub fn standard_name<Hash: std::fmt::Display>(
56+
pub fn standard_name<Hash: AsRef<[u8]>>(
5757
genesis_hash: &Hash,
5858
chain_spec: &Box<dyn ChainSpec>,
5959
) -> std::borrow::Cow<'static, str> {
6060
let chain_prefix = match chain_spec.fork_id() {
61-
Some(fork_id) => format!("/{}/{}", genesis_hash, fork_id),
62-
None => format!("/{}", genesis_hash),
61+
Some(fork_id) => format!("/{}/{}", hex::encode(genesis_hash), fork_id),
62+
None => format!("/{}", hex::encode(genesis_hash)),
6363
};
6464
format!("{}{}", chain_prefix, NAME).into()
6565
}
@@ -190,3 +190,48 @@ where
190190

191191
worker.run().await
192192
}
193+
194+
#[cfg(test)]
195+
mod tests {
196+
use super::*;
197+
use sc_chain_spec::{ChainSpec, GenericChainSpec};
198+
use serde::{Deserialize, Serialize};
199+
use sp_core::H256;
200+
use sp_runtime::{BuildStorage, Storage};
201+
202+
#[derive(Debug, Serialize, Deserialize)]
203+
struct Genesis(std::collections::BTreeMap<String, String>);
204+
impl BuildStorage for Genesis {
205+
fn assimilate_storage(&self, storage: &mut Storage) -> Result<(), String> {
206+
storage.top.extend(
207+
self.0.iter().map(|(a, b)| (a.clone().into_bytes(), b.clone().into_bytes())),
208+
);
209+
Ok(())
210+
}
211+
}
212+
213+
#[test]
214+
fn beefy_protocol_name() {
215+
let chain_spec = GenericChainSpec::<Genesis>::from_json_file(std::path::PathBuf::from(
216+
"../chain-spec/res/chain_spec.json",
217+
))
218+
.unwrap()
219+
.cloned_box();
220+
221+
// Create protocol name using random genesis hash.
222+
let genesis_hash = H256::random();
223+
let expected = format!("/{}/beefy/1", hex::encode(genesis_hash));
224+
let proto_name = beefy_protocol_name::standard_name(&genesis_hash, &chain_spec);
225+
assert_eq!(proto_name.to_string(), expected);
226+
227+
// Create protocol name using hardcoded genesis hash. Verify exact representation.
228+
let genesis_hash = [
229+
50, 4, 60, 123, 58, 106, 216, 246, 194, 188, 139, 193, 33, 212, 202, 171, 9, 55, 123,
230+
94, 8, 43, 12, 251, 187, 57, 173, 19, 188, 74, 205, 147,
231+
];
232+
let expected =
233+
"/32043c7b3a6ad8f6c2bc8bc121d4caab09377b5e082b0cfbbb39ad13bc4acd93/beefy/1".to_string();
234+
let proto_name = beefy_protocol_name::standard_name(&genesis_hash, &chain_spec);
235+
assert_eq!(proto_name.to_string(), expected);
236+
}
237+
}

client/finality-grandpa/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ dyn-clone = "1.0"
1919
fork-tree = { version = "3.0.0", path = "../../utils/fork-tree" }
2020
futures = "0.3.19"
2121
futures-timer = "3.0.1"
22+
hex = "0.4.2"
2223
log = "0.4.8"
2324
parking_lot = "0.12.0"
2425
rand = "0.8.4"
@@ -58,5 +59,7 @@ sc-network-test = { version = "0.8.0", path = "../network/test" }
5859
sp-keyring = { version = "6.0.0", path = "../../primitives/keyring" }
5960
substrate-test-runtime-client = { version = "2.0.0", path = "../../test-utils/runtime/client" }
6061
sp-tracing = { version = "5.0.0", path = "../../primitives/tracing" }
62+
63+
serde = "1.0.136"
6164
tokio = "1.15"
6265
tempfile = "3.1.0"

client/finality-grandpa/src/communication/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,13 @@ pub mod grandpa_protocol_name {
7777
/// Name of the notifications protocol used by GRANDPA.
7878
///
7979
/// Must be registered towards the networking in order for GRANDPA to properly function.
80-
pub fn standard_name<Hash: std::fmt::Display>(
80+
pub fn standard_name<Hash: AsRef<[u8]>>(
8181
genesis_hash: &Hash,
8282
chain_spec: &Box<dyn ChainSpec>,
8383
) -> std::borrow::Cow<'static, str> {
8484
let chain_prefix = match chain_spec.fork_id() {
85-
Some(fork_id) => format!("/{}/{}", genesis_hash, fork_id),
86-
None => format!("/{}", genesis_hash),
85+
Some(fork_id) => format!("/{}/{}", hex::encode(genesis_hash), fork_id),
86+
None => format!("/{}", hex::encode(genesis_hash)),
8787
};
8888
format!("{}{}", chain_prefix, NAME).into()
8989
}

client/finality-grandpa/src/communication/tests.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,3 +535,46 @@ fn peer_with_higher_view_leads_to_catch_up_request() {
535535

536536
futures::executor::block_on(test);
537537
}
538+
539+
fn local_chain_spec() -> Box<dyn sc_chain_spec::ChainSpec> {
540+
use sc_chain_spec::{ChainSpec, GenericChainSpec};
541+
use serde::{Deserialize, Serialize};
542+
use sp_runtime::{BuildStorage, Storage};
543+
544+
#[derive(Debug, Serialize, Deserialize)]
545+
struct Genesis(std::collections::BTreeMap<String, String>);
546+
impl BuildStorage for Genesis {
547+
fn assimilate_storage(&self, storage: &mut Storage) -> Result<(), String> {
548+
storage.top.extend(
549+
self.0.iter().map(|(a, b)| (a.clone().into_bytes(), b.clone().into_bytes())),
550+
);
551+
Ok(())
552+
}
553+
}
554+
let chain_spec = GenericChainSpec::<Genesis>::from_json_file(std::path::PathBuf::from(
555+
"../chain-spec/res/chain_spec.json",
556+
))
557+
.unwrap();
558+
chain_spec.cloned_box()
559+
}
560+
561+
#[test]
562+
fn grandpa_protocol_name() {
563+
let chain_spec = local_chain_spec();
564+
565+
// Create protocol name using random genesis hash.
566+
let genesis_hash = sp_core::H256::random();
567+
let expected = format!("/{}/grandpa/1", hex::encode(genesis_hash));
568+
let proto_name = grandpa_protocol_name::standard_name(&genesis_hash, &chain_spec);
569+
assert_eq!(proto_name.to_string(), expected);
570+
571+
// Create protocol name using hardcoded genesis hash. Verify exact representation.
572+
let genesis_hash = [
573+
53, 79, 112, 97, 119, 217, 39, 202, 147, 138, 225, 38, 88, 182, 215, 185, 110, 88, 8, 53,
574+
125, 210, 158, 151, 50, 113, 102, 59, 245, 199, 221, 240,
575+
];
576+
let expected =
577+
"/354f706177d927ca938ae12658b6d7b96e5808357dd29e973271663bf5c7ddf0/grandpa/1".to_string();
578+
let proto_name = grandpa_protocol_name::standard_name(&genesis_hash, &chain_spec);
579+
assert_eq!(proto_name.to_string(), expected);
580+
}

0 commit comments

Comments
 (0)