Skip to content

Commit

Permalink
Merge pull request #1557 from CosmWasm/clippy-1.66.0
Browse files Browse the repository at this point in the history
Bump clippy to 1.66.0 and fix some new warnings
  • Loading branch information
webmaster128 authored Jan 2, 2023
2 parents 2bd030a + e348516 commit abc9bcc
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ workflows:
matrix:
parameters:
# Run with MSRV and some modern stable Rust
rust-version: ["1.60.0", "1.65.0"]
rust-version: ["1.60.0", "1.66.0"]
- benchmarking:
requires:
- package_vm
Expand Down
2 changes: 1 addition & 1 deletion contracts/crypto-verify/src/ethereum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub fn verify_transaction(
) -> StdResult<bool> {
let sign_bytes =
serialize_unsigned_transaction(to, nonce, gas, gas_price, value, data, chain_id);
let hash = Keccak256::digest(&sign_bytes);
let hash = Keccak256::digest(sign_bytes);
let mut rs: Vec<u8> = Vec::with_capacity(64);
rs.resize(32 - r.len(), 0); // Left pad r to 32 bytes
rs.extend_from_slice(r);
Expand Down
2 changes: 1 addition & 1 deletion packages/crypto/benches/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ fn bench_crypto(c: &mut Criterion) {

group.bench_function("secp256k1_verify", |b| {
let message = hex::decode(COSMOS_SECP256K1_MSG_HEX).unwrap();
let message_hash = Sha256::digest(&message);
let message_hash = Sha256::digest(message);
let signature = hex::decode(COSMOS_SECP256K1_SIGNATURE_HEX).unwrap();
let public_key = base64::decode(COSMOS_SECP256K1_PUBKEY_BASE64).unwrap();
b.iter(|| {
Expand Down
4 changes: 2 additions & 2 deletions packages/crypto/src/secp256k1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ mod tests {
let signature = hex::decode(sig).unwrap();

// Explicit hash
let message_hash = Sha256::digest(&message);
let message_hash = Sha256::digest(message);

// secp256k1_verify works
assert!(
Expand Down Expand Up @@ -290,7 +290,7 @@ mod tests {
let message = hex::decode(&encoded.message).unwrap();

let hash = hex::decode(&encoded.message_hash).unwrap();
let message_hash = Sha256::digest(&message);
let message_hash = Sha256::digest(message);
assert_eq!(hash.as_slice(), message_hash.as_slice());

let signature = hex::decode(&encoded.signature).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions packages/vm/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ fn save_wasm_to_disk(dir: impl Into<PathBuf>, wasm: &[u8]) -> VmResult<Checksum>
// calculate filename
let checksum = Checksum::generate(wasm);
let filename = checksum.to_hex();
let filepath = dir.into().join(&filename);
let filepath = dir.into().join(filename);

// write data to file
// Since the same filename (a collision resistent hash) cannot be generated from two different byte codes
Expand Down Expand Up @@ -561,7 +561,7 @@ mod tests {
.path()
.join(STATE_DIR)
.join(WASM_DIR)
.join(&checksum.to_hex());
.join(checksum.to_hex());
let mut file = OpenOptions::new().write(true).open(filepath).unwrap();
file.write_all(b"broken data").unwrap();

Expand Down
59 changes: 46 additions & 13 deletions packages/vm/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,12 @@ pub fn do_addr_humanize<A: BackendApi, S: Storage, Q: Querier>(
}
}

/// Return code (error code) for a valid signature
const SECP256K1_VERIFY_CODE_VALID: u32 = 0;

/// Return code (error code) for an invalid signature
const SECP256K1_VERIFY_CODE_INVALID: u32 = 1;

pub fn do_secp256k1_verify<A: BackendApi, S: Storage, Q: Querier>(
env: &Environment<A, S, Q>,
hash_ptr: u32,
Expand All @@ -225,8 +231,15 @@ pub fn do_secp256k1_verify<A: BackendApi, S: Storage, Q: Querier>(
let gas_info = GasInfo::with_cost(env.gas_config.secp256k1_verify_cost);
process_gas_info::<A, S, Q>(env, gas_info)?;
let result = secp256k1_verify(&hash, &signature, &pubkey);
Ok(result.map_or_else(
|err| match err {
let code = match result {
Ok(valid) => {
if valid {
SECP256K1_VERIFY_CODE_VALID
} else {
SECP256K1_VERIFY_CODE_INVALID
}
}
Err(err) => match err {
CryptoError::InvalidHashFormat { .. }
| CryptoError::InvalidPubkeyFormat { .. }
| CryptoError::InvalidSignatureFormat { .. }
Expand All @@ -235,8 +248,8 @@ pub fn do_secp256k1_verify<A: BackendApi, S: Storage, Q: Querier>(
panic!("Error must not happen for this call")
}
},
|valid| if valid { 0 } else { 1 },
))
};
Ok(code)
}

pub fn do_secp256k1_recover_pubkey<A: BackendApi, S: Storage, Q: Querier>(
Expand Down Expand Up @@ -272,6 +285,12 @@ pub fn do_secp256k1_recover_pubkey<A: BackendApi, S: Storage, Q: Querier>(
}
}

/// Return code (error code) for a valid signature
const ED25519_VERIFY_CODE_VALID: u32 = 0;

/// Return code (error code) for an invalid signature
const ED25519_VERIFY_CODE_INVALID: u32 = 1;

pub fn do_ed25519_verify<A: BackendApi, S: Storage, Q: Querier>(
env: &Environment<A, S, Q>,
message_ptr: u32,
Expand All @@ -285,8 +304,15 @@ pub fn do_ed25519_verify<A: BackendApi, S: Storage, Q: Querier>(
let gas_info = GasInfo::with_cost(env.gas_config.ed25519_verify_cost);
process_gas_info::<A, S, Q>(env, gas_info)?;
let result = ed25519_verify(&message, &signature, &pubkey);
Ok(result.map_or_else(
|err| match err {
let code = match result {
Ok(valid) => {
if valid {
ED25519_VERIFY_CODE_VALID
} else {
ED25519_VERIFY_CODE_INVALID
}
}
Err(err) => match err {
CryptoError::InvalidPubkeyFormat { .. }
| CryptoError::InvalidSignatureFormat { .. }
| CryptoError::GenericErr { .. } => err.code(),
Expand All @@ -296,8 +322,8 @@ pub fn do_ed25519_verify<A: BackendApi, S: Storage, Q: Querier>(
panic!("Error must not happen for this call")
}
},
|valid| if valid { 0 } else { 1 },
))
};
Ok(code)
}

pub fn do_ed25519_batch_verify<A: BackendApi, S: Storage, Q: Querier>(
Expand Down Expand Up @@ -334,8 +360,15 @@ pub fn do_ed25519_batch_verify<A: BackendApi, S: Storage, Q: Querier>(
let gas_info = GasInfo::with_cost(max(gas_cost, env.gas_config.ed25519_verify_cost));
process_gas_info::<A, S, Q>(env, gas_info)?;
let result = ed25519_batch_verify(&messages, &signatures, &public_keys);
Ok(result.map_or_else(
|err| match err {
let code = match result {
Ok(valid) => {
if valid {
ED25519_VERIFY_CODE_VALID
} else {
ED25519_VERIFY_CODE_INVALID
}
}
Err(err) => match err {
CryptoError::BatchErr { .. }
| CryptoError::InvalidPubkeyFormat { .. }
| CryptoError::InvalidSignatureFormat { .. }
Expand All @@ -344,8 +377,8 @@ pub fn do_ed25519_batch_verify<A: BackendApi, S: Storage, Q: Querier>(
panic!("Error must not happen for this call")
}
},
|valid| (!valid).into(),
))
};
Ok(code)
}

/// Prints a debug message to console.
Expand Down Expand Up @@ -588,7 +621,7 @@ mod tests {
let result = do_db_read(&env, key_ptr);
let value_ptr = result.unwrap();
assert!(value_ptr > 0);
assert_eq!(force_read(&env, value_ptr as u32), VALUE1);
assert_eq!(force_read(&env, value_ptr), VALUE1);
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion packages/vm/src/modules/file_system_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl FileSystemCache {
let filename = checksum.to_hex();
let file_path = self.latest_modules_path().join(filename);

let result = unsafe { Module::deserialize_from_file(store, &file_path) };
let result = unsafe { Module::deserialize_from_file(store, file_path) };
match result {
Ok(module) => Ok(Some(module)),
Err(DeserializeError::Io(err)) => match err.kind() {
Expand Down
2 changes: 1 addition & 1 deletion packages/vm/src/modules/pinned_memory_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl PinnedMemoryCache {
/// This is based on the values provided with `store`. No actual
/// memory size is measured here.
pub fn size(&self) -> usize {
self.modules.iter().map(|(_, module)| module.size).sum()
self.modules.values().map(|module| module.size).sum()
}
}

Expand Down
6 changes: 3 additions & 3 deletions packages/vm/src/wasm_backend/gatekeeper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ mod tests {
let mut compiler_config = Cranelift::default();
compiler_config.push_middleware(deterministic);
let store = Store::new(&Universal::new(compiler_config).engine());
let result = Module::new(&store, &wasm);
let result = Module::new(&store, wasm);
assert!(result.is_ok());
}

Expand All @@ -732,7 +732,7 @@ mod tests {
let mut compiler_config = Cranelift::default();
compiler_config.push_middleware(deterministic);
let store = Store::new(&Universal::new(compiler_config).engine());
let result = Module::new(&store, &wasm);
let result = Module::new(&store, wasm);
assert!(result
.unwrap_err()
.to_string()
Expand All @@ -759,7 +759,7 @@ mod tests {
let mut compiler_config = Cranelift::default();
compiler_config.push_middleware(deterministic);
let store = Store::new(&Universal::new(compiler_config).engine());
let result = Module::new(&store, &wasm);
let result = Module::new(&store, wasm);
assert!(result
.unwrap_err()
.to_string()
Expand Down
2 changes: 1 addition & 1 deletion packages/vm/src/wasm_backend/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ mod tests {
let serialized = {
let wasm = wat::parse_str(EXPORTED_MEMORY_WAT).unwrap();
let store = make_compile_time_store(None, &[]);
let module = Module::new(&store, &wasm).unwrap();
let module = Module::new(&store, wasm).unwrap();
module.serialize().unwrap()
};

Expand Down

0 comments on commit abc9bcc

Please sign in to comment.