Skip to content

Commit

Permalink
add the function to make a syscall to l1 (#66)
Browse files Browse the repository at this point in the history
* add the function to make a syscall to l1

* add tests
  • Loading branch information
moodysalem authored Jan 6, 2025
1 parent a29aeda commit b1fb35b
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
1 change: 1 addition & 0 deletions .codegpt/head
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
73df00a1-54a7-44b2-89fb-f7d2f68d3079
17 changes: 14 additions & 3 deletions src/governor.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use governance::execution_state::{ExecutionState};
use governance::staker::{IStakerDispatcher};
use starknet::account::{Call};
use starknet::{ClassHash, ContractAddress};
use starknet::{ClassHash, ContractAddress, EthAddress};

#[derive(Copy, Drop, Serde, starknet::Store, PartialEq, Debug)]
pub struct ProposalInfo {
Expand Down Expand Up @@ -92,6 +92,9 @@ pub trait IGovernor<TContractState> {
// Replaces the code at this address. This must be self-called via a proposal.
fn upgrade(ref self: TContractState, class_hash: ClassHash);

// Sends a message to L1 via syscall
fn send_message_to_l1(self: @TContractState, to_address: EthAddress, payload: Span<felt252>);

// Migrates to the latest version of storage layout, from the version of storage before v2.1.0
fn _migrate_old_config_storage(ref self: TContractState);
}
Expand All @@ -110,11 +113,11 @@ pub mod Governor {
use starknet::storage_access::{Store, storage_base_address_from_felt252};
use starknet::{
AccountContract, get_block_timestamp, get_caller_address, get_contract_address, get_tx_info,
syscalls::{replace_class_syscall},
syscalls::{replace_class_syscall, send_message_to_l1_syscall},
};
use super::{
Call, ClassHash, Config, ContractAddress, ExecutionState, IGovernor, IStakerDispatcher,
ProposalInfo,
ProposalInfo, EthAddress,
};


Expand Down Expand Up @@ -459,6 +462,14 @@ pub mod Governor {
replace_class_syscall(class_hash).unwrap();
}

fn send_message_to_l1(
self: @ContractState, to_address: EthAddress, payload: Span<felt252>,
) {
self.check_self_call();

send_message_to_l1_syscall(to_address.into(), payload).expect('SYSCALL_FAILED')
}

fn _migrate_old_config_storage(ref self: ContractState) {
let old_config_storage_address = storage_base_address_from_felt252(selector!("config"));
let old_config: Config = Store::read(0, old_config_storage_address).unwrap();
Expand Down
49 changes: 49 additions & 0 deletions src/governor_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,55 @@ fn test_upgrade_succeeds_self_call() {
);
}

#[test]
#[should_panic(expected: ('SELF_CALL_ONLY', 'ENTRYPOINT_FAILED'))]
fn test_send_message_to_l1_fails_if_not_self_call() {
let (_staker, _token, governor, _config) = setup();
governor.send_message_to_l1(123_felt252.try_into().unwrap(), array![1, 2, 3].span());
}


#[test]
fn test_send_message_to_l1_succeeds_self_call() {
let (staker, token, governor, config) = setup();

token.approve(staker.contract_address, config.quorum.into());
staker.stake(proposer());
advance_time(config.voting_weight_smoothing_duration);

let id = create_proposal_with_call(
governor,
token,
staker,
Call {
to: governor.contract_address,
selector: selector!("send_message_to_l1"),
calldata: array![0xabcd, 2, 0xdead, 0xbeef].span(),
},
);

advance_time(config.voting_start_delay);

set_contract_address(proposer());
governor.vote(id, true);

advance_time(config.voting_period + config.execution_delay);

governor
.execute(
id,
array![
Call {
to: governor.contract_address,
selector: selector!("send_message_to_l1"),
calldata: array![0xabcd, 2, 0xdead, 0xbeef].span(),
},
]
.span(),
);
}


#[test]
#[should_panic(expected: ('SELF_CALL_ONLY', 'ENTRYPOINT_FAILED'))]
fn test_reconfigure_fails_if_not_self_call() {
Expand Down

0 comments on commit b1fb35b

Please sign in to comment.