Skip to content
This repository was archived by the owner on Nov 6, 2020. It is now read-only.

Commit 35c607f

Browse files
ordiandvdplm
authored andcommitted
updater: fix static id hashes initialization (#10755)
* updater: fix static id hashes initialization * Update updater/src/updater.rs
1 parent 20248c4 commit 35c607f

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

updater/src/updater.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ pub struct Updater<O = OperationsContractClient, F = fetch::Client, T = StdTimeP
159159
const CLIENT_ID: &str = "parity";
160160

161161
lazy_static! {
162-
static ref CLIENT_ID_HASH: H256 = H256::from_slice(CLIENT_ID.as_bytes());
162+
static ref CLIENT_ID_HASH: H256 = h256_from_str_resizing(CLIENT_ID);
163163
}
164164

165165
lazy_static! {
@@ -177,7 +177,16 @@ lazy_static! {
177177
}
178178

179179
lazy_static! {
180-
static ref PLATFORM_ID_HASH: H256 = H256::from_slice(PLATFORM.as_bytes());
180+
static ref PLATFORM_ID_HASH: H256 = h256_from_str_resizing(&PLATFORM);
181+
}
182+
183+
184+
// Pads the bytes with zeros or truncates the last bytes to H256::len_bytes()
185+
// before the conversion to match the previous behavior.
186+
fn h256_from_str_resizing(s: &str) -> H256 {
187+
let mut bytes = s.as_bytes().to_vec();
188+
bytes.resize(H256::len_bytes(), 0);
189+
H256::from_slice(&bytes)
181190
}
182191

183192
/// Client trait for getting latest release information from operations contract.
@@ -1253,4 +1262,11 @@ pub mod tests {
12531262
// and since our update policy requires consensus, the client should be disabled
12541263
assert!(client.is_disabled());
12551264
}
1265+
1266+
#[test]
1267+
fn static_hashes_do_not_panic() {
1268+
let client_id_hash: H256 = *CLIENT_ID_HASH;
1269+
assert_eq!(&format!("{:x}", client_id_hash), "7061726974790000000000000000000000000000000000000000000000000000");
1270+
let _: H256 = *PLATFORM_ID_HASH;
1271+
}
12561272
}

0 commit comments

Comments
 (0)