Skip to content

Commit

Permalink
back to musl + lint
Browse files Browse the repository at this point in the history
  • Loading branch information
augustbleeds committed Sep 27, 2024
1 parent 8165834 commit 7a90077
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 92 deletions.
6 changes: 3 additions & 3 deletions .github/actions/install-cairo/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ runs:
id: install-scarb
shell: bash
run: |
wget https://github.com/software-mansion/scarb/releases/download/${{ inputs.scarb_version }}/scarb-${{ inputs.scarb_version }}-x86_64-unknown-linux-gnu.tar.gz
tar -xvzf scarb-${{ inputs.scarb_version }}-x86_64-unknown-linux-gnu.tar.gz
mv -vf scarb-${{ inputs.scarb_version }}-x86_64-unknown-linux-gnu scarb-build
wget https://github.com/software-mansion/scarb/releases/download/${{ inputs.scarb_version }}/scarb-${{ inputs.scarb_version }}-x86_64-unknown-linux-musl.tar.gz
tar -xvzf scarb-${{ inputs.scarb_version }}-x86_64-unknown-linux-musl.tar.gz
mv -vf scarb-${{ inputs.scarb_version }}-x86_64-unknown-linux-musl scarb-build
echo "$GITHUB_WORKSPACE/scarb-build/bin" >> $GITHUB_PATH
115 changes: 57 additions & 58 deletions contracts/src/mcms.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ struct ExpiringRootAndOpCount {
op_count: u64
}

// based of https://github.com/starkware-libs/cairo/blob/1b747da1ec7e43a6fd0c0a4cbce302616408bc72/corelib/src/starknet/eth_signature.cairo#L25
// based of
// https://github.com/starkware-libs/cairo/blob/1b747da1ec7e43a6fd0c0a4cbce302616408bc72/corelib/src/starknet/eth_signature.cairo#L25
pub fn recover_eth_ecdsa(msg_hash: u256, signature: Signature) -> Result<EthAddress, felt252> {
if !is_signature_entry_valid::<Secp256k1Point>(signature.r) {
return Result::Err('Signature out of range');
Expand Down Expand Up @@ -237,7 +238,7 @@ mod ManyChainMultiSig {
ownable: OwnableComponent::Storage,
// s_signers is used to easily validate the existence of the signer by its address.
s_signers: LegacyMap<EthAddress, Signer>,
// begin s_config (defined in storage bc Config struct cannot support maps)
// begin s_config (defined in storage bc Config struct cannot support maps)
_s_config_signers_len: u8,
_s_config_signers: LegacyMap<u8, Signer>,
// no _s_config_group_len because there are always 32 groups
Expand All @@ -264,7 +265,7 @@ mod ManyChainMultiSig {
to: ContractAddress,
selector: felt252,
data: Span<felt252>,
// no value because value is sent through ERC20 tokens, even the native STRK token
// no value because value is sent through ERC20 tokens, even the native STRK token
}

#[derive(Drop, starknet::Event)]
Expand Down Expand Up @@ -309,36 +310,35 @@ mod ManyChainMultiSig {

let mut prev_address = EthAddressZeroable::zero();
let mut group_vote_counts: Felt252Dict<u8> = Default::default();
while let Option::Some(signature) = signatures
.pop_front() {
let signer_address = match recover_eth_ecdsa(msg_hash, signature) {
Result::Ok(signer_address) => signer_address,
Result::Err(e) => panic_with_felt252(e),
};
while let Option::Some(signature) = signatures.pop_front() {
let signer_address = match recover_eth_ecdsa(msg_hash, signature) {
Result::Ok(signer_address) => signer_address,
Result::Err(e) => panic_with_felt252(e),
};

assert(
to_u256(prev_address) < to_u256(signer_address.clone()),
'signer address must increase'
);
prev_address = signer_address;
assert(
to_u256(prev_address) < to_u256(signer_address.clone()),
'signer address must increase'
);
prev_address = signer_address;

let signer = self.get_signer_by_address(signer_address);
assert(signer.address == signer_address, 'invalid signer');
let signer = self.get_signer_by_address(signer_address);
assert(signer.address == signer_address, 'invalid signer');

let mut group = signer.group;
loop {
let counts = group_vote_counts.get(group.into());
group_vote_counts.insert(group.into(), counts + 1);
if counts + 1 != self._s_config_group_quorums.read(group) {
break;
}
if group == 0 {
// reached root
break;
}
group = self._s_config_group_parents.read(group)
};
let mut group = signer.group;
loop {
let counts = group_vote_counts.get(group.into());
group_vote_counts.insert(group.into(), counts + 1);
if counts + 1 != self._s_config_group_quorums.read(group) {
break;
}
if group == 0 {
// reached root
break;
}
group = self._s_config_group_parents.read(group)
};
};

let root_group_quorum = self._s_config_group_quorums.read(0);
assert(root_group_quorum > 0, 'root group missing quorum');
Expand Down Expand Up @@ -368,7 +368,8 @@ mod ManyChainMultiSig {
let op_count = self.s_expiring_root_and_op_count.read().op_count;
let current_root_metadata = self.s_root_metadata.read();

// new root can be set only if the current op_count is the expected post op count (unless an override is requested)
// new root can be set only if the current op_count is the expected post op count
// (unless an override is requested)
assert(
op_count == current_root_metadata.post_op_count
|| current_root_metadata.override_previous_root,
Expand Down Expand Up @@ -471,15 +472,14 @@ mod ManyChainMultiSig {

let mut group_children_counts: Felt252Dict<u8> = Default::default();
let mut i = 0;
while i < signer_groups
.len() {
let group = *signer_groups.at(i);
assert(group < NUM_GROUPS, 'out of bounds group');
// increment count for each group
group_children_counts
.insert(group.into(), group_children_counts.get(group.into()) + 1);
i += 1;
};
while i < signer_groups.len() {
let group = *signer_groups.at(i);
assert(group < NUM_GROUPS, 'out of bounds group');
// increment count for each group
group_children_counts
.insert(group.into(), group_children_counts.get(group.into()) + 1);
i += 1;
};

let mut j = 0;
while j < NUM_GROUPS {
Expand All @@ -505,7 +505,8 @@ mod ManyChainMultiSig {
.insert(
group_parent.into(), group_children_counts.get(group_parent.into()) + 1
);
// the above line clobbers group_children_counts[0] in last iteration, don't use it after the loop ends
// the above line clobbers group_children_counts[0] in last iteration, don't use
// it after the loop ends
}
j += 1;
};
Expand All @@ -521,7 +522,7 @@ mod ManyChainMultiSig {
};
// reset s_signers
self.s_signers.write(old_signer.address, empty_signer);
// reset _s_config_signers
// reset _s_config_signers
self._s_config_signers.write(i.into(), empty_signer);
i += 1;
};
Expand All @@ -539,27 +540,25 @@ mod ManyChainMultiSig {
let mut signers = ArrayTrait::<Signer>::new();
let mut prev_signer_address = EthAddressZeroable::zero();
let mut i: u8 = 0;
while i
.into() < signer_addresses
.len() {
let signer_address = *signer_addresses.at(i.into());
assert(
to_u256(prev_signer_address) < to_u256(signer_address),
'signer addresses not sorted'
);
while i.into() < signer_addresses.len() {
let signer_address = *signer_addresses.at(i.into());
assert(
to_u256(prev_signer_address) < to_u256(signer_address),
'signer addresses not sorted'
);

let signer = Signer {
address: signer_address, index: i, group: *signer_groups.at(i.into())
};
let signer = Signer {
address: signer_address, index: i, group: *signer_groups.at(i.into())
};

self.s_signers.write(signer_address, signer);
self._s_config_signers.write(i.into(), signer);
self.s_signers.write(signer_address, signer);
self._s_config_signers.write(i.into(), signer);

signers.append(signer);
signers.append(signer);

prev_signer_address = signer_address;
i += 1;
};
prev_signer_address = signer_address;
i += 1;
};

// length will always be less than MAX_NUM_SIGNERS so try_into will never panic
self._s_config_signers_len.write(signer_addresses.len().try_into().unwrap());
Expand Down
19 changes: 10 additions & 9 deletions contracts/src/tests/test_mcms/test_set_config.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ fn test_not_owner() {
#[test]
#[feature("safe_dispatcher")]
fn test_set_config_out_of_bound_signers() {
// 1. test if len(signer_address) = 0 => revert
// 1. test if len(signer_address) = 0 => revert
let (_, _, mcms_safe) = setup_mcms_deploy();

let signer_addresses = array![];
Expand Down Expand Up @@ -404,16 +404,17 @@ fn test_set_config_signer_addresses_not_sorted() {
}

// test success, root not cleared, event emitted
// 12. successful => test without clearing root. test the state of storage variables and that event was emitted
// 12. successful => test without clearing root. test the state of storage variables and that event
// was emitted
//
// ┌──────┐
// ┌─►│2-of-2│
// │ └──────┘
// │ ▲
// │ │
// ┌──┴───┐ ┌──┴───┐
// signer 1 signer 2
// └──────┘ └──────┘
// │ └──────┘
// │ ▲
// │ │
// ┌──┴───┐ ┌──┴───┐
// signer 1 signer 2
// └──────┘ └──────┘
#[test]
fn test_set_config_success_dont_clear_root() {
let signer_address_1: EthAddress = (0x141).try_into().unwrap();
Expand Down Expand Up @@ -531,7 +532,7 @@ fn test_set_config_success_dont_clear_root() {
}


// test that the config was reset
// test that the config was reset
#[test]
fn test_set_config_success_and_clear_root() {
// mock the contract state
Expand Down
41 changes: 19 additions & 22 deletions contracts/src/tests/test_mcms/utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ use snforge_std::{

// 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<u8> {
let mut result: Array<u8> = ArrayTrait::new();
Expand Down Expand Up @@ -146,7 +146,7 @@ fn insecure_sign(z: u256, e: u256) -> (u256, u256, bool) {
let n_u512: u512 = u256_wide_mul(N, (0x1).into());

// "random" number k would be generated by a pseudo-random number generator
// in secure applications it's important that k is random, or else the private key can
// in secure applications it's important that k is random, or else the private key can
// be derived from r and s
let k = 777;

Expand Down Expand Up @@ -196,17 +196,15 @@ fn merkle_root(leafs: Array<u256>) -> (u256, Span<u256>, Span<Span<u256>>) {
let proof2 = array![*level.at(0), metadata];

// level length is always even (except when it's 1)
while level
.len() > 1 {
let mut i = 0;
let mut new_level: Array<u256> = ArrayTrait::new();
while i < level
.len() {
new_level.append(hash_pair(*(level.at(i)), *level.at(i + 1)));
i += 2
};
level = new_level.span();
while level.len() > 1 {
let mut i = 0;
let mut new_level: Array<u256> = ArrayTrait::new();
while i < level.len() {
new_level.append(hash_pair(*(level.at(i)), *level.at(i + 1)));
i += 2
};
level = new_level.span();
};

let mut metadata_proof = *level.at(0);

Expand Down Expand Up @@ -271,17 +269,16 @@ fn set_root_args(

let mut signatures: Array<Signature> = ArrayTrait::new();

while let Option::Some(signer_metadata) = signers_metadata
.pop_front() {
let (r, s, y_parity) = insecure_sign(message_hash, signer_metadata.private_key);
let signature = Signature { r: r, s: s, y_parity: y_parity };
let address = recover_eth_ecdsa(message_hash, signature).unwrap();
while let Option::Some(signer_metadata) = signers_metadata.pop_front() {
let (r, s, y_parity) = insecure_sign(message_hash, signer_metadata.private_key);
let signature = Signature { r: r, s: s, y_parity: y_parity };
let address = recover_eth_ecdsa(message_hash, signature).unwrap();

// sanity check
assert(address == signer_metadata.address, 'signer not equal');
// sanity check
assert(address == signer_metadata.address, 'signer not equal');

signatures.append(signature);
};
signatures.append(signature);
};

let ops = array![op1.clone(), op2.clone()];

Expand Down

0 comments on commit 7a90077

Please sign in to comment.