Skip to content

Commit

Permalink
add proofs return value to helper func
Browse files Browse the repository at this point in the history
  • Loading branch information
augustbleeds committed Sep 17, 2024
1 parent f4a1646 commit ee75032
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 25 deletions.
2 changes: 0 additions & 2 deletions contracts/src/mcms.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use alexandria_encoding::sol_abi::encode::SolAbiEncodeTrait;

#[starknet::interface]
trait IManyChainMultiSig<TContractState> {
// todo: check length of byte array is 32
fn set_root(
ref self: TContractState,
root: u256,
Expand All @@ -23,7 +22,6 @@ trait IManyChainMultiSig<TContractState> {
signatures: Array<Signature>
);
fn execute(ref self: TContractState, op: Op, proof: Span<u256>);
// // todo: check length of group_quorums and group_parents
fn set_config(
ref self: TContractState,
signer_addresses: Span<EthAddress>,
Expand Down
1 change: 1 addition & 0 deletions contracts/src/tests/test_mcms.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod test_set_config;
mod test_set_root;
mod test_execute;
mod utils;

60 changes: 60 additions & 0 deletions contracts/src/tests/test_mcms/test_execute.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use chainlink::mcms::{
recover_eth_ecdsa, hash_pair, hash_op, hash_metadata, ExpiringRootAndOpCount, RootMetadata,
Config, Signer, eip_191_message_hash, ManyChainMultiSig, Op,
ManyChainMultiSig::{
NewRoot, InternalFunctionsTrait, contract_state_for_testing,
s_signersContractMemberStateTrait, s_expiring_root_and_op_countContractMemberStateTrait,
s_root_metadataContractMemberStateTrait
},
IManyChainMultiSigDispatcher, IManyChainMultiSigDispatcherTrait,
IManyChainMultiSigSafeDispatcher, IManyChainMultiSigSafeDispatcherTrait, IManyChainMultiSig,
ManyChainMultiSig::{MAX_NUM_SIGNERS},
};
use snforge_std::{
declare, ContractClassTrait, start_cheat_caller_address_global, start_cheat_caller_address,
stop_cheat_caller_address, stop_cheat_caller_address_global, start_cheat_chain_id_global,
spy_events, EventSpyAssertionsTrait, // Add for assertions on the EventSpy
test_address, // the contract being tested,
start_cheat_chain_id,
cheatcodes::{events::{EventSpy}}, start_cheat_block_timestamp_global,
start_cheat_block_timestamp, start_cheat_account_contract_address_global,
start_cheat_account_contract_address
};
use chainlink::tests::test_mcms::test_set_config::{setup_2_of_2_mcms_no_root, setup};
use chainlink::tests::test_mcms::test_set_root::{new_setup_2_of_2_mcms};
// 1. test no more operations to execute
// 2. test wrong chain id
// 3. test wrong multisig address
// 4. test root has expired
// 5. test wrong nonce
// 6. test wrong proof
// 7. test contract call fails (it doesn't exist)
// 8. test success

// #[test]
// fn test_success() {
// let (
// mut spy,
// mcms_address,
// mcms,
// safe_mcms,
// config,
// signer_addresses,
// signer_groups,
// group_quorums,
// group_parents,
// clear_root,
// root,
// valid_until,
// metadata,
// metadata_proof,
// signatures,
// ops
// ) =
// new_setup_2_of_2_mcms();

// mcms.set_root(root, valid_until, metadata, metadata_proof, signatures);

// // execute
// }

43 changes: 20 additions & 23 deletions contracts/src/tests/test_mcms/test_set_root.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,24 @@ use snforge_std::{
// https://www.npmjs.com/package/merkletreejs
// the proof does not include the root or the leaf

// metadata should be the last leaf
// will only work with an even number of ops
fn merkle_root(leafs: Array<u256>) -> (u256, Span<u256>) {
// simplified logic will only work when len(ops) = 2
// metadata nodes is the last leaf so that len(leafs) = 3
fn merkle_root(leafs: Array<u256>) -> (u256, Span<u256>, Span<u256>, Span<u256>) {
let mut level: Array<u256> = ArrayTrait::new();
let odd = leafs.len() % 2 == 1;
let mut metadata: Option<u256> = Option::None;

if odd {
metadata = Option::Some(*leafs.at(leafs.len() - 1));
let mut i = 0;
let metadata = *leafs.at(leafs.len() - 1);
let mut i = 0;

// we assume metadata is last leaf so we exclude for now
while i < leafs.len() - 1 {
level.append(*leafs.at(i));
i += 1;
}
}
// we assume metadata is last leaf so we exclude for now
while i < leafs.len() - 1 {
level.append(*leafs.at(i));
i += 1;
};

let mut level = level.span();
let mut level = level.span(); // [leaf1, leaf2]

let proof1 = array![*level.at(1), metadata];
let proof2 = array![*level.at(0), metadata];

// level length is always even (except when it's 1)
while level
Expand All @@ -87,12 +86,9 @@ fn merkle_root(leafs: Array<u256>) -> (u256, Span<u256>) {
let mut metadata_proof = *level.at(0);

// based on merkletree.js lib we use, the odd leaf out is not hashed until the very end
let root = match metadata {
Option::Some(m) => hash_pair(*level.at(0), m),
Option::None => (*level.at(0)),
};
let root = hash_pair(*level.at(0), metadata);

(root, array![metadata_proof].span())
(root, array![metadata_proof].span(), proof1.span(), proof2.span())
}

#[derive(Copy, Drop, Serde)]
Expand Down Expand Up @@ -150,7 +146,7 @@ fn generate_set_root_params_custom_op_count(
let metadata_hash = hash_metadata(metadata, valid_until);

// create merkle tree
let (root, metadata_proof) = merkle_root(array![op1_hash, op2_hash, metadata_hash]);
let (root, metadata_proof, _, _) = merkle_root(array![op1_hash, op2_hash, metadata_hash]);

let encoded_root = BytesTrait::new_empty().encode(root).encode(valid_until);
let message_hash = eip_191_message_hash(encoded_root.keccak());
Expand Down Expand Up @@ -221,7 +217,7 @@ fn generate_set_root_params_1(
let metadata_hash = hash_metadata(metadata, valid_until);

// create merkle tree
let (root, metadata_proof) = merkle_root(array![op1_hash, op2_hash, metadata_hash]);
let (root, metadata_proof, _, _) = merkle_root(array![op1_hash, op2_hash, metadata_hash]);

let encoded_root = BytesTrait::new_empty().encode(root).encode(valid_until);
let message_hash = eip_191_message_hash(encoded_root.keccak());
Expand Down Expand Up @@ -424,7 +420,7 @@ fn new_setup_2_of_2_mcms_wrong_multisig() -> (
let metadata_hash = hash_metadata(metadata, valid_until);

// create merkle tree
let (root, metadata_proof) = merkle_root(array![op1_hash, op2_hash, metadata_hash]);
let (root, metadata_proof, _, _) = merkle_root(array![op1_hash, op2_hash, metadata_hash]);

let encoded_root = BytesTrait::new_empty().encode(root).encode(valid_until);
let message_hash = eip_191_message_hash(encoded_root.keccak());
Expand Down Expand Up @@ -998,3 +994,4 @@ fn test_wrong_pre_op_count() {
// }
// }


0 comments on commit ee75032

Please sign in to comment.