Skip to content

Commit

Permalink
Fix clippy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanfrey committed Apr 19, 2021
1 parent ea8b91d commit 667ddb3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 51 deletions.
22 changes: 7 additions & 15 deletions rust/src/compress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,22 +114,14 @@ pub fn decompress_batch(proof: &ics23::CompressedBatchProof) -> Result<ics23::Co
.map(|cent| -> Result<ics23::BatchEntry> {
match &cent.proof {
Some(ics23::compressed_batch_entry::Proof::Exist(ex)) => {
let exist = decompress_exist(&ex, &lookup)?;
let exist = decompress_exist(&ex, &lookup);
Ok(ics23::BatchEntry {
proof: Some(ics23::batch_entry::Proof::Exist(exist)),
})
}
Some(ics23::compressed_batch_entry::Proof::Nonexist(non)) => {
let left = non
.left
.clone()
.map(|l| decompress_exist(&l, &lookup))
.transpose()?;
let right = non
.right
.clone()
.map(|r| decompress_exist(&r, &lookup))
.transpose()?;
let left = non.left.clone().map(|l| decompress_exist(&l, &lookup));
let right = non.right.clone().map(|r| decompress_exist(&r, &lookup));
let nonexist = ics23::NonExistenceProof {
key: non.key.clone(),
left,
Expand All @@ -154,17 +146,17 @@ pub fn decompress_batch(proof: &ics23::CompressedBatchProof) -> Result<ics23::Co
fn decompress_exist(
exist: &ics23::CompressedExistenceProof,
lookup: &[ics23::InnerOp],
) -> Result<ics23::ExistenceProof> {
) -> ics23::ExistenceProof {
let path = exist
.path
.iter()
.map(|&x| lookup.get(x as usize).cloned())
.collect::<Option<Vec<_>>>()
.unwrap();
Ok(ics23::ExistenceProof {
.unwrap_or_default();
ics23::ExistenceProof {
key: exist.key.clone(),
value: exist.value.clone(),
leaf: exist.leaf.clone(),
path,
})
}
}
72 changes: 36 additions & 36 deletions rust/src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn apply_inner(inner: &InnerOp, child: &[u8]) -> Result<Hash> {
let mut image = inner.prefix.clone();
image.extend(child);
image.extend(&inner.suffix);
do_hash(inner.hash(), &image)
Ok(do_hash(inner.hash(), &image))
}

// apply_leaf will take a key, value pair and a LeafOp and return a LeafHash
Expand All @@ -22,26 +22,26 @@ pub fn apply_leaf(leaf: &LeafOp, key: &[u8], value: &[u8]) -> Result<Hash> {
hash.extend(prekey);
let preval = prepare_leaf_data(leaf.prehash_value(), leaf.length(), value)?;
hash.extend(preval);
do_hash(leaf.hash(), &hash)
Ok(do_hash(leaf.hash(), &hash))
}

fn prepare_leaf_data(prehash: HashOp, length: LengthOp, data: &[u8]) -> Result<Hash> {
ensure!(!data.is_empty(), "Input to prepare_leaf_data missing");
let h = do_hash(prehash, data)?;
let h = do_hash(prehash, data);
do_length(length, &h)
}

fn do_hash(hash: HashOp, data: &[u8]) -> Result<Hash> {
fn do_hash(hash: HashOp, data: &[u8]) -> Hash {
match hash {
HashOp::NoHash => Ok(Hash::from(data)),
HashOp::Sha256 => Ok(Hash::from(Sha256::digest(data).as_slice())),
HashOp::Sha512 => Ok(Hash::from(Sha512::digest(data).as_slice())),
HashOp::Keccak => Ok(Hash::from(Sha3_512::digest(data).as_slice())),
HashOp::Ripemd160 => Ok(Hash::from(Ripemd160::digest(data).as_slice())),
HashOp::Bitcoin => Ok(Hash::from(
Ripemd160::digest(Sha256::digest(data).as_slice()).as_slice(),
)),
HashOp::Sha512_256 => Ok(Hash::from(Sha512Trunc256::digest(data).as_slice())),
HashOp::NoHash => Hash::from(data),
HashOp::Sha256 => Hash::from(Sha256::digest(data).as_slice()),
HashOp::Sha512 => Hash::from(Sha512::digest(data).as_slice()),
HashOp::Keccak => Hash::from(Sha3_512::digest(data).as_slice()),
HashOp::Ripemd160 => Hash::from(Ripemd160::digest(data).as_slice()),
HashOp::Bitcoin => {
Hash::from(Ripemd160::digest(Sha256::digest(data).as_slice()).as_slice())
}
HashOp::Sha512_256 => Hash::from(Sha512Trunc256::digest(data).as_slice()),
}
}

Expand Down Expand Up @@ -77,43 +77,43 @@ fn proto_len(length: usize) -> Result<Hash> {
mod tests {
use super::*;

fn decode(input: &str) -> Vec<u8> {
hex::decode(input).unwrap()
}

#[test]
fn hashing_food() -> Result<()> {
let hash = do_hash(HashOp::NoHash, b"food")?;
ensure!(hash == hex::decode("666f6f64")?, "no hash fails");
fn hashing_food() {
let hash = do_hash(HashOp::NoHash, b"food");
assert!(hash == hex::decode("666f6f64").unwrap(), "no hash fails");

let hash = do_hash(HashOp::Sha256, b"food")?;
ensure!(
hash == hex::decode(
"c1f026582fe6e8cb620d0c85a72fe421ddded756662a8ec00ed4c297ad10676b"
)?,
let hash = do_hash(HashOp::Sha256, b"food");
assert!(
hash == hex::decode("c1f026582fe6e8cb620d0c85a72fe421ddded756662a8ec00ed4c297ad10676b")
.unwrap(),
"sha256 hash fails"
);

let hash = do_hash(HashOp::Sha512, b"food")?;
ensure!(hash == hex::decode("c235548cfe84fc87678ff04c9134e060cdcd7512d09ed726192151a995541ed8db9fda5204e72e7ac268214c322c17787c70530513c59faede52b7dd9ce64331")?, "sha512 hash fails");
let hash = do_hash(HashOp::Sha512, b"food");
assert!(hash == hex::decode("c235548cfe84fc87678ff04c9134e060cdcd7512d09ed726192151a995541ed8db9fda5204e72e7ac268214c322c17787c70530513c59faede52b7dd9ce64331").unwrap(), "sha512 hash fails");

let hash = do_hash(HashOp::Ripemd160, b"food")?;
ensure!(
hash == hex::decode("b1ab9988c7c7c5ec4b2b291adfeeee10e77cdd46")?,
let hash = do_hash(HashOp::Ripemd160, b"food");
assert!(
hash == hex::decode("b1ab9988c7c7c5ec4b2b291adfeeee10e77cdd46").unwrap(),
"ripemd160 hash fails"
);

let hash = do_hash(HashOp::Bitcoin, b"food")?;
ensure!(
hash == hex::decode("0bcb587dfb4fc10b36d57f2bba1878f139b75d24")?,
let hash = do_hash(HashOp::Bitcoin, b"food");
assert!(
hash == hex::decode("0bcb587dfb4fc10b36d57f2bba1878f139b75d24").unwrap(),
"bitcoin hash fails"
);

let hash = do_hash(HashOp::Sha512_256, b"food")?;
ensure!(
hash == hex::decode(
"5b3a452a6acbf1fc1e553a40c501585d5bd3cca176d562e0a0e19a3c43804e88"
)?,
let hash = do_hash(HashOp::Sha512_256, b"food");
assert!(
hash == hex::decode("5b3a452a6acbf1fc1e553a40c501585d5bd3cca176d562e0a0e19a3c43804e88")
.unwrap(),
"sha512/256 hash fails"
);

Ok(())
}

#[test]
Expand Down

0 comments on commit 667ddb3

Please sign in to comment.