Skip to content

Commit 3fa3035

Browse files
Seulgi Kimsgkim126
authored andcommitted
Use FromStr instead of From to convert a string to a hash
This commit also removes 0x prefix for string literals.
1 parent d4030a6 commit 3fa3035

File tree

5 files changed

+24
-11
lines changed

5 files changed

+24
-11
lines changed

core/src/consensus/tendermint/message.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ mod tests {
382382
use super::super::Step;
383383
use super::*;
384384
use rlp::rlp_encode_and_decode_test;
385+
use std::str::FromStr;
385386

386387
#[test]
387388
fn step_ordering() {
@@ -457,7 +458,9 @@ mod tests {
457458
on: VoteOn {
458459
step: VoteStep::new(2, 3, Step::Commit),
459460
block_hash: Some(
460-
H256::from("07feab4c39250abf60b77d7589a5b61fdf409bd837e936376381d19db1e1f050").into()
461+
H256::from_str("07feab4c39250abf60b77d7589a5b61fdf409bd837e936376381d19db1e1f050")
462+
.unwrap()
463+
.into()
461464
),
462465
},
463466
},
@@ -467,7 +470,9 @@ mod tests {
467470
on: VoteOn {
468471
step: VoteStep::new(2, 3, Step::Commit),
469472
block_hash: Some(
470-
H256::from("07feab4c39250abf60b77d7589a5b61fdf409bd837e936376381d19db1e1f050").into()
473+
H256::from_str("07feab4c39250abf60b77d7589a5b61fdf409bd837e936376381d19db1e1f050")
474+
.unwrap()
475+
.into()
471476
),
472477
},
473478
}
@@ -488,7 +493,9 @@ mod tests {
488493
signer_index: 0x1234,
489494
on: VoteOn {
490495
step: VoteStep::new(2, 3, Step::Commit),
491-
block_hash: Some(H256::from("07feab4c39250abf60b77d7589a5b61fdf409bd837e936376381d19db1e1f050").into()),
496+
block_hash: Some(
497+
H256::from_str("07feab4c39250abf60b77d7589a5b61fdf409bd837e936376381d19db1e1f050").unwrap().into(),
498+
),
492499
},
493500
};
494501
rlp_encode_and_decode_test!(message);
@@ -501,7 +508,8 @@ mod tests {
501508
let step = Step::Commit;
502509
let signature = Signature::random();
503510
let signer_index = 0x1234;
504-
let block_hash = Some(H256::from("07feab4c39250abf60b77d7589a5b61fdf409bd837e936376381d19db1e1f050").into());
511+
let block_hash =
512+
Some(H256::from_str("07feab4c39250abf60b77d7589a5b61fdf409bd837e936376381d19db1e1f050").unwrap().into());
505513
let consensus_message = ConsensusMessage {
506514
signature,
507515
signer_index,

json/src/hash.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,17 @@ impl_hash!(H520, Hash520);
9999
#[cfg(test)]
100100
mod test {
101101
use super::H256;
102+
use std::str::FromStr;
102103

103104
#[test]
104105
fn hash_deserialization() {
105106
let s = r#"["", "5a39ed1020c04d4d84539975b893a4e7c53eab6c2965db8bc3468093a31bc5ae"]"#;
106107
let deserialized: Vec<H256> = serde_json::from_str(s).unwrap();
107108
assert_eq!(deserialized, vec![
108109
H256(primitives::H256::zero()),
109-
H256(primitives::H256::from("5a39ed1020c04d4d84539975b893a4e7c53eab6c2965db8bc3468093a31bc5ae")),
110+
H256(
111+
primitives::H256::from_str("5a39ed1020c04d4d84539975b893a4e7c53eab6c2965db8bc3468093a31bc5ae").unwrap()
112+
),
110113
]);
111114
}
112115

json/src/scheme/genesis.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,15 @@ mod tests {
7777
prev_view: 0x0.into(),
7878
cur_view: 0x0.into(),
7979
precommits: vec![
80-
H520(Core520::from("0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")),
80+
H520(Core520::from_str("0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000").unwrap()),
8181
]
8282
}),
8383
author: Some(PlatformAddress::from_str("fjjh0000AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAtc0").unwrap()),
8484
timestamp: Some(0x07.into()),
85-
parent_hash: Some(H256(Core256::from("0x9000000000000000000000000000000000000000000000000000000000000000"))),
85+
parent_hash: Some(H256(Core256::from_str("9000000000000000000000000000000000000000000000000000000000000000").unwrap())),
8686
transactions_root: None,
87-
state_root: Some(H256(Core256::from("0xd7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544"))),
88-
next_validator_set_hash: Some(H256(Core256::from("0xd7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544"))),
87+
state_root: Some(H256(Core256::from_str("d7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544").unwrap())),
88+
next_validator_set_hash: Some(H256(Core256::from_str("d7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544").unwrap())),
8989
extra_data: Some(Bytes::from_str("0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa").unwrap()),
9090
});
9191
}

json/src/scheme/seal.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ mod tests {
4545
use crate::bytes::Bytes;
4646
use crate::hash::H520;
4747
use primitives::H520 as Core520;
48+
use std::str::FromStr;
4849

4950
#[test]
5051
fn seal_deserialization() {
@@ -69,7 +70,7 @@ mod tests {
6970
Seal::Tendermint(TendermintSeal {
7071
prev_view: 0x3.into(),
7172
cur_view: 0x4.into(),
72-
precommits: vec![H520(Core520::from("0x4000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004"))]
73+
precommits: vec![H520(Core520::from_str("4000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004").unwrap())]
7374
}),
7475
]);
7576
}

state/src/impls/top_level.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,7 @@ impl TopState for TopLevelState {
623623

624624
#[cfg(test)]
625625
mod tests_state {
626+
use std::str::FromStr;
626627
use std::sync::Arc;
627628

628629
use cdb::{new_journaldb, Algorithm};
@@ -970,7 +971,7 @@ mod tests_state {
970971
let mut state = get_temp_state();
971972
let a = Public::default();
972973
state.get_account_mut(&a).unwrap();
973-
assert_eq!(Ok(H256::from("d53db6d6b7631919a40209daec6271dc77c6ae67155afa6ce0efabf7978de67a")), state.commit());
974+
assert_eq!(Ok(H256::from_str("d53db6d6b7631919a40209daec6271dc77c6ae67155afa6ce0efabf7978de67a").unwrap()), state.commit());
974975
}
975976

976977
#[test]

0 commit comments

Comments
 (0)