Skip to content

Commit

Permalink
refactor: fix clippy warnings (part 4) (#313)
Browse files Browse the repository at this point in the history
  • Loading branch information
cgorenflo authored Mar 19, 2024
1 parent bb5c357 commit 6e646b5
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 80 deletions.
17 changes: 7 additions & 10 deletions contracts/multisig-prover/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,9 @@ mod tests {
.try_into()
.unwrap();
let service_name = "service_name";
for encoding in vec![Encoder::Abi, Encoder::Bcs] {
for encoding in [Encoder::Abi, Encoder::Bcs] {
let mut deps = mock_dependencies();
let info = mock_info(&instantiator, &[]);
let info = mock_info(instantiator, &[]);
let env = mock_env();

let msg = InstantiateMsg {
Expand All @@ -219,7 +219,7 @@ mod tests {
service_name: service_name.to_string(),
chain_name: "Ethereum".to_string(),
worker_set_diff_threshold: 0,
encoder: encoding.clone(),
encoder: encoding,
key_type: multisig::key::KeyType::Ecdsa,
};

Expand All @@ -236,10 +236,7 @@ mod tests {
assert_eq!(config.multisig, multisig_address);
assert_eq!(config.service_registry, service_registry_address);
assert_eq!(config.destination_chain_id, destination_chain_id);
assert_eq!(
config.signing_threshold,
signing_threshold.try_into().unwrap()
);
assert_eq!(config.signing_threshold, signing_threshold);
assert_eq!(config.service_name, service_name);
assert_eq!(config.encoder, encoding)
}
Expand All @@ -249,7 +246,7 @@ mod tests {
let total_weight: Uint256 = operators
.iter()
.fold(Uint256::zero(), |acc, x| acc + x.weight);
let quorum = Uint256::try_from(total_weight.mul_ceil(test_data::threshold())).unwrap();
let quorum = total_weight.mul_ceil(test_data::threshold());
WorkerSet {
signers: operators
.into_iter()
Expand Down Expand Up @@ -448,7 +445,7 @@ mod tests {
let total_weight: Uint256 = new_worker_set
.iter()
.fold(Uint256::zero(), |acc, x| acc + x.weight);
let quorum = Uint256::try_from(total_weight.mul_ceil(test_data::threshold())).unwrap();
let quorum = total_weight.mul_ceil(test_data::threshold());
mocks::voting_verifier::confirm_worker_set(
&mut test_case.app,
test_case.voting_verifier_address.clone(),
Expand Down Expand Up @@ -519,7 +516,7 @@ mod tests {
let total_weight: Uint256 = new_worker_set
.iter()
.fold(Uint256::zero(), |acc, x| acc + x.weight);
let quorum = Uint256::try_from(total_weight.mul_ceil(test_data::threshold())).unwrap();
let quorum = total_weight.mul_ceil(test_data::threshold());
mocks::voting_verifier::confirm_worker_set(
&mut test_case.app,
test_case.voting_verifier_address.clone(),
Expand Down
22 changes: 9 additions & 13 deletions contracts/multisig-prover/src/encoding/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,13 @@ pub fn command_params(

#[cfg(test)]
mod test {
use connection_router_api::CrossChainId;
use elliptic_curve::consts::U32;
use ethers::types::Signature as EthersSignature;
use generic_array::GenericArray;
use hex::FromHex;
use k256::ecdsa::Signature as K256Signature;

use connection_router_api::CrossChainId;
use multisig::key::KeyType;

use crate::{
Expand All @@ -257,7 +257,7 @@ mod test {

use super::*;

fn decode_command_params<'a>(encoded_params: impl Into<Vec<u8>>) -> Vec<Token> {
fn decode_command_params(encoded_params: impl Into<Vec<u8>>) -> Vec<Token> {
ethabi::decode(
&[
ParamType::String,
Expand All @@ -270,9 +270,7 @@ mod test {
.unwrap()
}

fn decode_operator_transfer_command_params<'a>(
encoded_params: impl Into<Vec<u8>>,
) -> Vec<Token> {
fn decode_operator_transfer_command_params(encoded_params: impl Into<Vec<u8>>) -> Vec<Token> {
ethabi::decode(
&[
ParamType::Array(Box::new(ParamType::Address)),
Expand Down Expand Up @@ -319,7 +317,7 @@ mod test {
.for_each(|((id, ty), params)| match (id, ty, params) {
(Token::FixedBytes(id), Token::String(ty), Token::Bytes(params)) => {
let command = Command {
id: id.to_owned().try_into().unwrap(),
id: id.to_owned().into(),
ty: match ty.as_str() {
"approveContractCall" => CommandType::ApproveContractCall,
"transferOperatorship" => CommandType::TransferOperatorship,
Expand Down Expand Up @@ -395,8 +393,7 @@ mod test {
let tokens = decode_operator_transfer_command_params(res.unwrap());
let mut signers: Vec<Signer> = new_worker_set.signers.into_values().collect();
signers.sort_by_key(|signer| evm_address(signer.pub_key.as_ref()).unwrap());
let mut i = 0;
for signer in signers {
for (i, signer) in signers.into_iter().enumerate() {
assert_eq!(
tokens[0].clone().into_array().unwrap()[i],
Token::Address(ethereum_types::Address::from_slice(
Expand All @@ -412,7 +409,6 @@ mod test {
&signer.weight.to_be_bytes()
))
);
i = i + 1;
}
assert_eq!(
tokens[2],
Expand Down Expand Up @@ -449,7 +445,7 @@ mod test {
test_data
.commands
.into_iter()
.zip(res.data.commands.into_iter())
.zip(res.data.commands)
.for_each(|(expected_command, command)| {
assert_eq!(command.id, expected_command.id);
assert_eq!(command.ty, expected_command.ty);
Expand Down Expand Up @@ -492,7 +488,7 @@ mod test {
(
Signer {
address: op.address,
weight: op.weight.into(),
weight: op.weight,
pub_key: op.pub_key,
},
op.signature,
Expand Down Expand Up @@ -532,7 +528,7 @@ mod test {
expected_data
.commands
.into_iter()
.zip(res.commands.into_iter())
.zip(res.commands)
.for_each(|(expected_command, command)| {
assert_eq!(command.id, expected_command.id);
assert_eq!(command.ty, expected_command.ty);
Expand Down Expand Up @@ -571,7 +567,7 @@ mod test {
(
Signer {
address: op.address,
weight: op.weight.into(),
weight: op.weight,
pub_key: op.pub_key,
},
op.signature,
Expand Down
50 changes: 20 additions & 30 deletions contracts/multisig-prover/src/encoding/bcs.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
use axelar_wasm_std::operators::Operators;
use bcs::to_bytes;
use cosmwasm_std::{HexBinary, Uint256};

use crate::error::ContractError;

use itertools::Itertools;
use sha3::{Digest, Keccak256};

use axelar_wasm_std::operators::Operators;
use multisig::{key::Signature, msg::Signer, worker_set::WorkerSet};

use crate::error::ContractError;
use crate::types::{CommandBatch, Operator};

use super::Data;
use sha3::{Digest, Keccak256};

// TODO: all of the public functions in this file should be moved to a trait,
// that has an abi and bcs implementation (and possibly others)
Expand Down Expand Up @@ -192,14 +191,13 @@ fn u256_to_u64(chain_id: Uint256) -> u64 {

#[cfg(test)]
mod test {

use std::vec;

use axelar_wasm_std::operators::Operators;
use bcs::from_bytes;
use connection_router_api::{CrossChainId, Message};
use cosmwasm_std::{Addr, HexBinary, Uint256};

use axelar_wasm_std::operators::Operators;
use connection_router_api::{CrossChainId, Message};
use multisig::{
key::{PublicKey, Signature},
msg::Signer,
Expand All @@ -218,6 +216,7 @@ mod test {
};

use super::msg_digest;

#[test]
fn test_transfer_operatorship_params() {
let worker_set = test_data::new_worker_set();
Expand Down Expand Up @@ -316,15 +315,9 @@ mod test {

assert!(proof.is_ok());
let proof = proof.unwrap();
let decoded_proof: Result<(Vec<Vec<u8>>, Vec<u128>, u128, Vec<Vec<u8>>), _> =
from_bytes(&proof);
let decoded_proof: Result<Proof, _> = from_bytes(&proof);
assert!(decoded_proof.is_ok());
let (operators, weights, quorum_decoded, signatures): (
Vec<Vec<u8>>,
Vec<u128>,
u128,
Vec<Vec<u8>>,
) = decoded_proof.unwrap();
let (operators, weights, quorum_decoded, signatures): Proof = decoded_proof.unwrap();

assert_eq!(operators.len(), signers.len());
assert_eq!(weights.len(), signers.len());
Expand Down Expand Up @@ -375,16 +368,11 @@ mod test {

#[test]
fn test_command_params() {
let res = command_params(
"Ethereum".into(),
"00".into(),
"01".repeat(32).into(),
&[2; 32],
);
let res = command_params("Ethereum".into(), "00".into(), "01".repeat(32), &[2; 32]);
assert!(res.is_ok());

let res = res.unwrap();
let params = from_bytes(&res.to_vec());
let params = from_bytes(&res);
assert!(params.is_ok());
let (source_chain, source_address, destination_address, payload_hash): (
String,
Expand All @@ -407,7 +395,7 @@ mod test {
#[test]
fn test_invalid_destination_address() {
let res = command_params("Ethereum".into(), "00".into(), "01".into(), &[2; 32]);
assert!(!res.is_ok());
assert!(res.is_err());
}

#[test]
Expand All @@ -426,15 +414,14 @@ mod test {
params: command_params(
source_chain.into(),
source_address.into(),
destination_address.clone().into(),
destination_address.clone(),
&payload_hash,
)
.unwrap(),
}],
};
let encoded = encode(&data);
let decoded: Result<(u64, Vec<[u8; 32]>, Vec<String>, Vec<Vec<u8>>), _> =
from_bytes(&encoded.to_vec());
let decoded: Result<DecodedData, _> = from_bytes(&encoded);
assert!(decoded.is_ok());
let (chain_id, command_ids, command_types, params) = decoded.unwrap();

Expand Down Expand Up @@ -474,7 +461,7 @@ mod test {
#[test]
fn test_msg_to_sign() {
let mut builder = CommandBatchBuilder::new(1u128.into(), crate::encoding::Encoder::Bcs);
let _ = builder
builder
.add_message(Message {
cc_id: "ethereum:foobar:1".parse().unwrap(),
destination_address: "0F".repeat(32).parse().unwrap(),
Expand All @@ -488,7 +475,7 @@ mod test {
assert_eq!(msg.len(), 32);

let mut builder = CommandBatchBuilder::new(1u128.into(), crate::encoding::Encoder::Bcs);
let _ = builder
builder
.add_message(Message {
cc_id: "ethereum:foobar:2".parse().unwrap(),
destination_address: "0A".repeat(32).parse().unwrap(),
Expand Down Expand Up @@ -536,7 +523,7 @@ mod test {
let command_batch = CommandBatch {
message_ids: vec![],
id: BatchId::new(
&vec![CrossChainId {
&[CrossChainId {
chain: "AXELAR".to_string().try_into().unwrap(),
id: "foobar".to_string().try_into().unwrap(),
}],
Expand Down Expand Up @@ -569,4 +556,7 @@ mod test {
assert_eq!(encoded.len(), approval.to_vec().len());
assert_eq!(encoded.to_vec(), approval.to_vec());
}

type Proof = (Vec<Vec<u8>>, Vec<u128>, u128, Vec<Vec<u8>>);
type DecodedData = (u64, Vec<[u8; 32]>, Vec<String>, Vec<Vec<u8>>);
}
9 changes: 4 additions & 5 deletions contracts/multisig-prover/src/test/mocks/multisig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use cosmwasm_std::{
};
use cw_multi_test::{App, Executor};
use cw_storage_plus::Map;

use multisig::key::{KeyType, KeyTyped, PublicKey};
use multisig::{
msg::{ExecuteMsg, InstantiateMsg, QueryMsg},
Expand Down Expand Up @@ -68,7 +69,7 @@ pub fn register_pub_keys(app: &mut App, multisig_address: Addr, workers: Vec<Tes
worker.address,
multisig_address.clone(),
&ExecuteMsg::RegisterPublicKey {
public_key: worker.pub_key.into(),
public_key: worker.pub_key,
signed_sender_address: HexBinary::from_hex("00").unwrap(),
},
&[],
Expand Down Expand Up @@ -110,7 +111,7 @@ mod query {
(
Signer {
address: op.address,
weight: op.weight.into(),
weight: op.weight,
pub_key: op.pub_key,
},
op.signature,
Expand All @@ -131,8 +132,6 @@ mod query {
worker: String,
key_type: KeyType,
) -> PublicKey {
PUB_KEYS
.load(deps.storage, (worker, key_type.clone()))
.unwrap()
PUB_KEYS.load(deps.storage, (worker, key_type)).unwrap()
}
}
5 changes: 3 additions & 2 deletions integration-tests/tests/message_routing.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use connection_router_api::{CrossChainId, Message};
use cosmwasm_std::{HexBinary, Uint128};

use connection_router_api::{CrossChainId, Message};

use crate::test_utils::AXL_DENOMINATION;

mod test_utils;
pub mod test_utils;
/// Tests that a single message can be routed fully through the protocol. Submits a message to the
/// gateway, votes on the poll, routes the message to the outgoing gateway, triggers signing at the prover
/// and signs via multisig. Also tests that rewards are distributed as expected for voting and signing.
Expand Down
Loading

0 comments on commit 6e646b5

Please sign in to comment.