Create consensus utils and move byte_reverse_hex function to it (#705)

* move byte_reverse_hex function
This commit is contained in:
Alfredo Garcia 2020-07-21 23:29:14 -03:00 committed by GitHub
parent b8de256c48
commit db2eb80b3e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 16 deletions

View File

@ -24,6 +24,7 @@ pub mod proofs;
pub mod serialization; pub mod serialization;
pub mod transaction; pub mod transaction;
pub mod types; pub mod types;
pub mod utils;
pub use ed25519_zebra; pub use ed25519_zebra;
pub use redjubjub; pub use redjubjub;

15
zebra-chain/src/utils.rs Normal file
View File

@ -0,0 +1,15 @@
//! Utility functions for chain data.
/// Returns the hexadecimal-encoded string `s` in byte-reversed order.
pub fn byte_reverse_hex(s: &str) -> String {
String::from_utf8(
s.as_bytes()
.chunks(2)
.rev()
.map(|c| c.iter())
.flatten()
.cloned()
.collect::<Vec<u8>>(),
)
.expect("input should be ascii")
}

View File

@ -16,20 +16,6 @@ pub struct RevhexCmd {
input: String, input: String,
} }
/// Returns the hexadecimal-encoded string `s` in byte-reversed order.
fn byte_reverse_hex(s: &str) -> String {
String::from_utf8(
s.as_bytes()
.chunks(2)
.rev()
.map(|c| c.iter())
.flatten()
.cloned()
.collect::<Vec<u8>>(),
)
.expect("input should be ascii")
}
impl Runnable for RevhexCmd { impl Runnable for RevhexCmd {
/// Print endian-reversed hex string. /// Print endian-reversed hex string.
fn run(&self) { fn run(&self) {
@ -40,10 +26,10 @@ impl Runnable for RevhexCmd {
// We can distinguish EOF from an empty line, because the newline is // We can distinguish EOF from an empty line, because the newline is
// included in the buffer, so empty lines return Ok(1). // included in the buffer, so empty lines return Ok(1).
while stdin().read_line(&mut input).unwrap_or(0) > 0 { while stdin().read_line(&mut input).unwrap_or(0) > 0 {
println!("{}", byte_reverse_hex(&input.trim())); println!("{}", zebra_chain::utils::byte_reverse_hex(&input.trim()));
} }
} else { } else {
println!("{}", byte_reverse_hex(&self.input)); println!("{}", zebra_chain::utils::byte_reverse_hex(&self.input));
} }
} }
} }