From f02d36d2624677f56008a7e7bd36470a22ac49a4 Mon Sep 17 00:00:00 2001 From: Augustus Chang Date: Wed, 18 Sep 2024 11:03:41 -0400 Subject: [PATCH] refactor tests --- contracts/src/mcms.cairo | 2 +- .../src/tests/test_mcms/test_execute.cairo | 5 +- .../src/tests/test_mcms/test_set_config.cairo | 369 +----------------- contracts/src/tests/test_mcms/utils.cairo | 109 ++---- 4 files changed, 60 insertions(+), 425 deletions(-) diff --git a/contracts/src/mcms.cairo b/contracts/src/mcms.cairo index 3b0b489b..c7c5b4b8 100644 --- a/contracts/src/mcms.cairo +++ b/contracts/src/mcms.cairo @@ -466,7 +466,7 @@ mod ManyChainMultiSig { assert( group_quorums.len() == NUM_GROUPS.into() && group_quorums.len() == group_parents.len(), - 'group quorums/parents mismatch' + 'wrong group quorums/parents len' ); let mut group_children_counts: Felt252Dict = Default::default(); diff --git a/contracts/src/tests/test_mcms/test_execute.cairo b/contracts/src/tests/test_mcms/test_execute.cairo index 053f8c14..e4c58886 100644 --- a/contracts/src/tests/test_mcms/test_execute.cairo +++ b/contracts/src/tests/test_mcms/test_execute.cairo @@ -210,13 +210,12 @@ fn test_wrong_multisig_address() { op1.multisig = contract_address_const::<119922>(); let op1_proof = *ops_proof.at(0); - start_cheat_chain_id_global(1231); let result = safe_mcms.execute(op1, op1_proof); match result { - Result::Ok(_) => panic!("expect 'wrong chain id'"), + Result::Ok(_) => panic!("expect 'wrong multisig address'"), Result::Err(panic_data) => { - assert(*panic_data.at(0) == 'wrong chain id', *panic_data.at(0)); + assert(*panic_data.at(0) == 'wrong multisig address', *panic_data.at(0)); } } } diff --git a/contracts/src/tests/test_mcms/test_set_config.cairo b/contracts/src/tests/test_mcms/test_set_config.cairo index 79db2d32..9d68415d 100644 --- a/contracts/src/tests/test_mcms/test_set_config.cairo +++ b/contracts/src/tests/test_mcms/test_set_config.cairo @@ -23,7 +23,7 @@ use snforge_std::{ cheatcodes::{events::{EventSpy}} }; use chainlink::tests::test_mcms::utils::{ - setup_mcms_deploy, setup_mcms_deploy_and_set_config_2_of_2 + setup_mcms_deploy, setup_mcms_deploy_and_set_config_2_of_2, ZERO_ARRAY, fill_array }; #[test] @@ -165,21 +165,15 @@ fn test_set_config_group_quorums_parents_mismatch() { ); match result { - Result::Ok(_) => panic!("expect 'group quorums/parents mismatch'"), + Result::Ok(_) => panic!("expect 'wrong group quorums/parents len'"), Result::Err(panic_data) => { - assert(*panic_data.at(0) == 'group quorums/parents mismatch', *panic_data.at(0)); + assert(*panic_data.at(0) == 'wrong group quorums/parents len', *panic_data.at(0)); } } // 5. test if group_quorum and group_parents not equal in length - // todo: replace with [0_u8; 32] in cairo 2.7.0 - let mut group_quorums = ArrayTrait::new(); - let mut i = 0; - while i < 32_usize { - group_quorums.append(0); - i += 1; - }; + let mut group_quorums = ZERO_ARRAY(); let result = mcms_safe .set_config( @@ -191,9 +185,9 @@ fn test_set_config_group_quorums_parents_mismatch() { ); match result { - Result::Ok(_) => panic!("expect 'group quorums/parents mismatch'"), + Result::Ok(_) => panic!("expect 'wrong group quorums/parents len'"), Result::Err(panic_data) => { - assert(*panic_data.at(0) == 'group quorums/parents mismatch', *panic_data.at(0)); + assert(*panic_data.at(0) == 'wrong group quorums/parents len', *panic_data.at(0)); } } } @@ -249,75 +243,8 @@ fn test_set_config_group_tree_malformed() { let signer_addresses = array![EthAddressZeroable::zero()]; let signer_groups = array![0]; - let mut group_quorums = array![ - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0 - ]; - - let mut group_parents = array![ - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 31 - ]; + let mut group_quorums = ZERO_ARRAY(); + let mut group_parents = fill_array(array![(31, 31)]); let clear_root = false; @@ -397,74 +324,8 @@ fn test_set_config_signer_in_disabled_group() { let mut signer_addresses = array![EthAddressZeroable::zero()]; let signer_groups = array![0]; - let mut group_quorums = array![ - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0 - ]; - let mut group_parents = array![ - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0 - ]; + let mut group_quorums = ZERO_ARRAY(); + let mut group_parents = ZERO_ARRAY(); let clear_root = false; let result = mcms_safe @@ -492,74 +353,8 @@ fn test_set_config_quorum_impossible() { let mut signer_addresses = array![EthAddressZeroable::zero()]; let signer_groups = array![0]; - let mut group_quorums = array![ - 2, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0 - ]; - let mut group_parents = array![ - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0 - ]; + let mut group_quorums = fill_array(array![(0, 2)]); + let mut group_parents = ZERO_ARRAY(); let clear_root = false; let result = mcms_safe @@ -590,74 +385,8 @@ fn test_set_config_signer_addresses_not_sorted() { u256 { high: 0, low: 1 }.into(), EthAddressZeroable::zero() ]; let signer_groups = array![0, 0]; - let mut group_quorums = array![ - 2, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0 - ]; - let mut group_parents = array![ - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0 - ]; + let mut group_quorums = fill_array(array![(0, 2)]); + let mut group_parents = ZERO_ARRAY(); let clear_root = false; let result = mcms_safe @@ -842,74 +571,8 @@ fn test_set_config_success_and_clear_root() { let signer_address_2: EthAddress = u256 { high: 0, low: 2 }.into(); let signer_addresses: Array = array![signer_address_1, signer_address_2]; let signer_groups = array![0, 0]; - let group_quorums = array![ - 2, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0 - ]; - let group_parents = array![ - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0 - ]; + let group_quorums = fill_array(array![(0, 2)]); + let group_parents = ZERO_ARRAY(); let clear_root = true; state diff --git a/contracts/src/tests/test_mcms/utils.cairo b/contracts/src/tests/test_mcms/utils.cairo index cd34e64e..bbb1195a 100644 --- a/contracts/src/tests/test_mcms/utils.cairo +++ b/contracts/src/tests/test_mcms/utils.cairo @@ -41,6 +41,44 @@ use snforge_std::{ // setup helpers // +// returns a length 32 array +// give (index, value) tuples to fill array with +// +// ex: fill_array(array!(0, 1)) will fill the 0th index with value 1 +// +// assumes that values array is sorted in ascending order of the index +fn fill_array(mut values: Array<(u32, u8)>) -> Array { + let mut result: Array = ArrayTrait::new(); + + let mut maybe_next = values.pop_front(); + + let mut i = 0; + while i < 32_u32 { + match maybe_next { + Option::Some(next) => { + let (next_index, next_value) = next; + + if i == next_index { + result.append(next_value); + maybe_next = values.pop_front(); + } else { + result.append(0); + } + }, + Option::None(_) => { result.append(0); }, + } + + i += 1; + }; + + result +} + +fn ZERO_ARRAY() -> Array { + // todo: replace with [0_u8; 32] in cairo 2.7.0+ + fill_array(array![]) +} + #[derive(Copy, Drop, Serde)] struct SignerMetadata { address: EthAddress, @@ -289,74 +327,9 @@ fn setup_mcms_deploy_and_set_config_2_of_2( let signer_addresses: Array = array![signer_address_1, signer_address_2]; let signer_groups = array![0, 0]; - let group_quorums = array![ - 2, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0 - ]; - let group_parents = array![ - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0 - ]; + let group_quorums = fill_array(array![(0, 2)]); + let group_parents = ZERO_ARRAY(); + let clear_root = false; let mut spy = spy_events();