-
Notifications
You must be signed in to change notification settings - Fork 73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: starknet timelock execution strategy #600
Merged
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
2e5fdfd
chore: lock cairo-version in Scarb config
27a61ec
feat: simple quorum proposal status function
41e9968
feat: timelock base
f9d0d26
chore: updated lib
74d5a90
feat: timelock queue function
d7800d6
chore: removed legacy simple quorum impl
0a89dd2
feat added state to simple quorum impl
0bc5744
chore: update imports
2fcc94f
feat: events
0910c8d
feat: timelock execution impl
4415ae6
feat: space manager util
762f68b
feat: use space manager in timelock
a774b22
fix: timelock interface
8f2a481
chore: tests for timelock execution strategy
3778551
refactor: updated interfaces
e79f421
chore: lots of tests
f0592bb
refactor: added timelock delay view func
2a7f417
chore: complete unit tests
36e19f3
refactor: zero check on space address and duplicates check when initi…
e6b0f20
chore: add comments clarifying unsafe usafe
b677a04
chore: more tests on enabling/disabling spaces
1931cc2
chore: unit tests on space manager
2ac9a92
chore: removed redundant cairo version id
c1108e1
chore: fixed comment
bcf746d
chore: test for against votes not included in quorum
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,353 @@ | ||
use starknet::ContractAddress; | ||
use sx::types::{Proposal, ProposalStatus}; | ||
|
||
#[starknet::interface] | ||
trait ITimelockExecutionStrategy<TContractState> { | ||
fn execute_queued_proposal(ref self: TContractState, payload: Span<felt252>); | ||
|
||
fn veto(ref self: TContractState, execution_payload_hash: felt252); | ||
|
||
fn set_veto_guardian(ref self: TContractState, new_veto_guardian: ContractAddress); | ||
|
||
fn veto_guardian(self: @TContractState) -> ContractAddress; | ||
|
||
fn set_timelock_delay(ref self: TContractState, new_timelock_delay: u32); | ||
|
||
fn timelock_delay(self: @TContractState) -> u32; | ||
|
||
fn enable_space(ref self: TContractState, space: ContractAddress); | ||
|
||
fn disable_space(ref self: TContractState, space: ContractAddress); | ||
|
||
fn is_space_enabled(self: @TContractState, space: ContractAddress) -> bool; | ||
|
||
/// Transfers the ownership to `new_owner`. | ||
fn transfer_ownership(ref self: TContractState, new_owner: ContractAddress); | ||
/// Renounces the ownership of the contract. CAUTION: This is a one-way operation. | ||
fn renounce_ownership(ref self: TContractState); | ||
|
||
fn owner(self: @TContractState) -> ContractAddress; | ||
} | ||
|
||
#[starknet::contract] | ||
mod TimelockExecutionStrategy { | ||
use core::zeroable::Zeroable; | ||
use starknet::{ContractAddress, info, syscalls}; | ||
use openzeppelin::access::ownable::Ownable; | ||
use sx::interfaces::IExecutionStrategy; | ||
use super::ITimelockExecutionStrategy; | ||
use sx::types::{Proposal, ProposalStatus}; | ||
use sx::utils::{SimpleQuorum, SpaceManager}; | ||
|
||
#[storage] | ||
struct Storage { | ||
_timelock_delay: u32, | ||
_veto_guardian: ContractAddress, | ||
_proposal_execution_time: LegacyMap::<felt252, u32> | ||
} | ||
|
||
#[event] | ||
#[derive(Drop, starknet::Event)] | ||
enum Event { | ||
TimelockExecutionStrategySetUp: TimelockExecutionStrategySetUp, | ||
TimelockDelaySet: TimelockDelaySet, | ||
VetoGuardianSet: VetoGuardianSet, | ||
CallQueued: CallQueued, | ||
ProposalQueued: ProposalQueued, | ||
CallExecuted: CallExecuted, | ||
ProposalExecuted: ProposalExecuted, | ||
ProposalVetoed: ProposalVetoed | ||
} | ||
|
||
#[derive(Drop, PartialEq, starknet::Event)] | ||
struct TimelockExecutionStrategySetUp { | ||
owner: ContractAddress, | ||
veto_guardian: ContractAddress, | ||
timelock_delay: u32, | ||
quorum: u256 | ||
} | ||
|
||
#[derive(Drop, PartialEq, starknet::Event)] | ||
struct TimelockDelaySet { | ||
new_timelock_delay: u32 | ||
} | ||
|
||
#[derive(Drop, PartialEq, starknet::Event)] | ||
struct VetoGuardianSet { | ||
new_veto_guardian: ContractAddress | ||
} | ||
|
||
#[derive(Drop, PartialEq, starknet::Event)] | ||
struct CallQueued { | ||
call: CallWithSalt, | ||
execution_time: u32 | ||
} | ||
|
||
#[derive(Drop, PartialEq, starknet::Event)] | ||
struct ProposalQueued { | ||
execution_payload_hash: felt252, | ||
execution_time: u32 | ||
} | ||
|
||
#[derive(Drop, PartialEq, starknet::Event)] | ||
struct CallExecuted { | ||
call: CallWithSalt | ||
} | ||
|
||
#[derive(Drop, PartialEq, starknet::Event)] | ||
struct ProposalExecuted { | ||
execution_payload_hash: felt252 | ||
} | ||
|
||
#[derive(Drop, PartialEq, starknet::Event)] | ||
struct ProposalVetoed { | ||
execution_payload_hash: felt252 | ||
} | ||
|
||
#[derive(Drop, PartialEq, Serde)] | ||
struct CallWithSalt { | ||
to: ContractAddress, | ||
selector: felt252, | ||
calldata: Array<felt252>, | ||
salt: felt252 | ||
} | ||
|
||
#[constructor] | ||
fn constructor( | ||
ref self: ContractState, | ||
owner: ContractAddress, | ||
veto_guardian: ContractAddress, | ||
spaces: Span<ContractAddress>, | ||
timelock_delay: u32, | ||
quorum: u256 | ||
) { | ||
// Migration to components planned ; disregard the `unsafe` keyword, | ||
// it is actually safe. | ||
let mut state = Ownable::unsafe_new_contract_state(); | ||
Ownable::InternalImpl::initializer(ref state, owner); | ||
|
||
let mut state = SimpleQuorum::unsafe_new_contract_state(); | ||
SimpleQuorum::InternalImpl::initializer(ref state, quorum); | ||
|
||
let mut state = SpaceManager::unsafe_new_contract_state(); | ||
SpaceManager::InternalImpl::initializer(ref state, spaces); | ||
|
||
self._timelock_delay.write(timelock_delay); | ||
self._veto_guardian.write(veto_guardian); | ||
} | ||
|
||
#[external(v0)] | ||
impl ExecutionStrategy of IExecutionStrategy<ContractState> { | ||
fn execute( | ||
ref self: ContractState, | ||
proposal_id: u256, | ||
proposal: Proposal, | ||
votes_for: u256, | ||
votes_against: u256, | ||
votes_abstain: u256, | ||
payload: Array<felt252> | ||
) { | ||
// Migration to components planned ; disregard the `unsafe` keyword, | ||
// it is actually safe. | ||
let state = SpaceManager::unsafe_new_contract_state(); | ||
pscott marked this conversation as resolved.
Show resolved
Hide resolved
|
||
SpaceManager::InternalImpl::assert_only_spaces(@state); | ||
let state = SimpleQuorum::unsafe_new_contract_state(); | ||
let proposal_status = SimpleQuorum::InternalImpl::get_proposal_status( | ||
pscott marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@state, @proposal, votes_for, votes_against, votes_abstain | ||
); | ||
assert( | ||
proposal_status == ProposalStatus::Accepted(()) | ||
|| proposal_status == ProposalStatus::VotingPeriodAccepted(()), | ||
'Invalid Proposal Status' | ||
); | ||
|
||
assert( | ||
self._proposal_execution_time.read(proposal.execution_payload_hash).is_zero(), | ||
'Duplicate Hash' | ||
); | ||
|
||
let execution_time = info::get_block_timestamp().try_into().unwrap() | ||
+ self._timelock_delay.read(); | ||
self._proposal_execution_time.write(proposal.execution_payload_hash, execution_time); | ||
|
||
let mut payload = payload.span(); | ||
let mut calls = Serde::<Array<CallWithSalt>>::deserialize(ref payload).unwrap(); | ||
|
||
loop { | ||
match calls.pop_front() { | ||
Option::Some(call) => { | ||
self | ||
.emit( | ||
Event::CallQueued( | ||
CallQueued { call: call, execution_time: execution_time } | ||
) | ||
); | ||
}, | ||
Option::None(()) => { | ||
break; | ||
} | ||
}; | ||
} | ||
} | ||
|
||
fn get_proposal_status( | ||
self: @ContractState, | ||
proposal: Proposal, | ||
votes_for: u256, | ||
votes_against: u256, | ||
votes_abstain: u256, | ||
) -> ProposalStatus { | ||
let state = SimpleQuorum::unsafe_new_contract_state(); | ||
SimpleQuorum::InternalImpl::get_proposal_status( | ||
@state, @proposal, votes_for, votes_against, votes_abstain | ||
) | ||
} | ||
|
||
fn get_strategy_type(self: @ContractState) -> felt252 { | ||
'SimpleQuorumTimelock' | ||
} | ||
} | ||
|
||
#[external(v0)] | ||
impl TimelockExecutionStrategy of ITimelockExecutionStrategy<ContractState> { | ||
fn execute_queued_proposal(ref self: ContractState, mut payload: Span<felt252>) { | ||
let execution_payload_hash = poseidon::poseidon_hash_span(payload); | ||
let execution_time = self._proposal_execution_time.read(execution_payload_hash); | ||
assert(execution_time.is_non_zero(), 'Proposal Not Queued'); | ||
assert( | ||
info::get_block_timestamp().try_into().unwrap() >= execution_time, 'Delay Not Met' | ||
); | ||
|
||
// Reset the execution time to 0 to prevent reentrancy. | ||
self._proposal_execution_time.write(execution_payload_hash, 0); | ||
|
||
let mut calls = Serde::<Array<CallWithSalt>>::deserialize(ref payload).unwrap(); | ||
loop { | ||
match calls.pop_front() { | ||
Option::Some(call) => { | ||
syscalls::call_contract_syscall( | ||
call.to, call.selector, call.calldata.span() | ||
) | ||
.expect('Call Failed'); | ||
|
||
self.emit(Event::CallExecuted(CallExecuted { call: call })); | ||
}, | ||
Option::None(()) => { | ||
break; | ||
} | ||
}; | ||
}; | ||
|
||
self | ||
.emit( | ||
Event::ProposalExecuted( | ||
ProposalExecuted { execution_payload_hash: execution_payload_hash } | ||
) | ||
); | ||
} | ||
|
||
fn veto(ref self: ContractState, execution_payload_hash: felt252) { | ||
self.assert_only_veto_guardian(); | ||
assert( | ||
self._proposal_execution_time.read(execution_payload_hash).is_non_zero(), | ||
'Proposal Not Queued' | ||
); | ||
self._proposal_execution_time.write(execution_payload_hash, 0); | ||
self | ||
.emit( | ||
Event::ProposalVetoed( | ||
ProposalVetoed { execution_payload_hash: execution_payload_hash } | ||
) | ||
); | ||
} | ||
|
||
fn set_veto_guardian(ref self: ContractState, new_veto_guardian: ContractAddress) { | ||
// Migration to components planned ; disregard the `unsafe` keyword, | ||
// it is actually safe. | ||
let state = Ownable::unsafe_new_contract_state(); | ||
Ownable::InternalImpl::assert_only_owner(@state); | ||
self._veto_guardian.write(new_veto_guardian); | ||
|
||
self | ||
.emit( | ||
Event::VetoGuardianSet(VetoGuardianSet { new_veto_guardian: new_veto_guardian }) | ||
); | ||
} | ||
|
||
fn veto_guardian(self: @ContractState) -> ContractAddress { | ||
self._veto_guardian.read() | ||
} | ||
|
||
fn set_timelock_delay(ref self: ContractState, new_timelock_delay: u32) { | ||
// Migration to components planned ; disregard the `unsafe` keyword, | ||
// it is actually safe. | ||
let state = Ownable::unsafe_new_contract_state(); | ||
Ownable::InternalImpl::assert_only_owner(@state); | ||
self._timelock_delay.write(new_timelock_delay); | ||
|
||
self | ||
.emit( | ||
Event::TimelockDelaySet( | ||
TimelockDelaySet { new_timelock_delay: new_timelock_delay } | ||
) | ||
); | ||
} | ||
|
||
fn timelock_delay(self: @ContractState) -> u32 { | ||
self._timelock_delay.read() | ||
} | ||
|
||
fn enable_space(ref self: ContractState, space: ContractAddress) { | ||
// Migration to components planned ; disregard the `unsafe` keyword, | ||
// it is actually safe. | ||
let state = Ownable::unsafe_new_contract_state(); | ||
Ownable::InternalImpl::assert_only_owner(@state); | ||
let mut state = SpaceManager::unsafe_new_contract_state(); | ||
SpaceManager::InternalImpl::enable_space(ref state, space); | ||
} | ||
|
||
fn disable_space(ref self: ContractState, space: ContractAddress) { | ||
// Migration to components planned ; disregard the `unsafe` keyword, | ||
// it is actually safe. | ||
let state = Ownable::unsafe_new_contract_state(); | ||
Ownable::InternalImpl::assert_only_owner(@state); | ||
let mut state = SpaceManager::unsafe_new_contract_state(); | ||
SpaceManager::InternalImpl::disable_space(ref state, space); | ||
} | ||
|
||
fn is_space_enabled(self: @ContractState, space: ContractAddress) -> bool { | ||
// Migration to components planned ; disregard the `unsafe` keyword, | ||
// it is actually safe. | ||
let state = SpaceManager::unsafe_new_contract_state(); | ||
SpaceManager::InternalImpl::is_space_enabled(@state, space) | ||
} | ||
|
||
fn owner(self: @ContractState) -> ContractAddress { | ||
// Migration to components planned ; disregard the `unsafe` keyword, | ||
// it is actually safe. | ||
let state = Ownable::unsafe_new_contract_state(); | ||
Ownable::OwnableImpl::owner(@state) | ||
} | ||
|
||
fn transfer_ownership(ref self: ContractState, new_owner: ContractAddress) { | ||
// Migration to components planned ; disregard the `unsafe` keyword, | ||
// it is actually safe. | ||
let mut state = Ownable::unsafe_new_contract_state(); | ||
Ownable::OwnableImpl::transfer_ownership(ref state, new_owner); | ||
} | ||
|
||
fn renounce_ownership(ref self: ContractState) { | ||
// Migration to components planned ; disregard the `unsafe` keyword, | ||
// it is actually safe. | ||
let mut state = Ownable::unsafe_new_contract_state(); | ||
Ownable::OwnableImpl::renounce_ownership(ref state); | ||
} | ||
} | ||
|
||
#[generate_trait] | ||
impl InternalImpl of InternalTrait { | ||
fn assert_only_veto_guardian(self: @ContractState) { | ||
assert(self._veto_guardian.read() == info::get_caller_address(), 'Unauthorized Caller'); | ||
} | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
btw: https://github.com/OpenZeppelin/cairo-contracts/releases/tag/v0.8.0 ! Getting close :D