Skip to content

Commit

Permalink
fix inline
Browse files Browse the repository at this point in the history
  • Loading branch information
Flydexo committed Jan 11, 2024
1 parent b22e081 commit 8fcaf85
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 72 deletions.
1 change: 1 addition & 0 deletions src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod governance_token;
mod governor;
mod interfaces;
mod timelock;
mod utils;

#[cfg(test)]
mod airdrop_test;
Expand Down
105 changes: 33 additions & 72 deletions src/timelock.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,62 +3,8 @@ use core::traits::TryInto;
use core::result::ResultTrait;
use starknet::{ContractAddress};
use starknet::account::{Call};
use core::integer::{u128_to_felt252, u64_try_from_felt252, u128_safe_divmod};
use starknet::storage_access::{StorePacking};

#[starknet::interface]
trait ITimelock<TStorage> {
// Queue a list of calls to be executed after the delay. Only the owner may call this.
fn queue(ref self: TStorage, calls: Span<Call>) -> felt252;

// Cancel a queued proposal before it is executed. Only the owner may call this.
fn cancel(ref self: TStorage, id: felt252);

// Execute a list of calls that have previously been queued. Anyone may call this.
fn execute(ref self: TStorage, calls: Span<Call>) -> Array<Span<felt252>>;

// Return the execution window, i.e. the start and end timestamp in which the call can be executed
fn get_execution_window(self: @TStorage, id: felt252) -> ExecutionWindow;

// Get the current owner
fn get_owner(self: @TStorage) -> ContractAddress;

// Returns the delay and the window for call execution
fn get_configuration(self: @TStorage) -> TimelockConfig;

// Transfer ownership, i.e. the address that can queue and cancel calls. This must be self-called via #queue.
fn transfer(ref self: TStorage, to: ContractAddress);
// Configure the delay and the window for call execution. This must be self-called via #queue.
fn configure(ref self: TStorage, config: TimelockConfig);
}


const TWO_POW_64: u128 = 0x10000000000000000;

#[inline(always)]
impl ThreeU64TupleStorePacking of StorePacking<(u64, u64, u64), felt252> {
fn pack(value: (u64, u64, u64)) -> felt252 {
let (a, b, c) = value;
u256 { low: TwoU64TupleStorePacking::pack((a, b)), high: c.into() }.try_into().unwrap()
}
fn unpack(value: felt252) -> (u64, u64, u64) {
let u256_value: u256 = value.into();
let (a, b) = TwoU64TupleStorePacking::unpack(u256_value.low);
(a, b, (u256_value.high).try_into().unwrap())
}
}

#[inline(always)]
impl TwoU64TupleStorePacking of StorePacking<(u64, u64), u128> {
fn pack(value: (u64, u64)) -> u128 {
let (a, b) = value;
a.into() + b.into() * TWO_POW_64
}
fn unpack(value: u128) -> (u64, u64) {
let (q, r) = u128_safe_divmod(value, TWO_POW_64.try_into().unwrap());
(r.try_into().unwrap(), q.try_into().unwrap())
}
}
use governance::utils::timestamps::{ThreeU64TupleStorePacking, TwoU64TupleStorePacking};

#[derive(Copy, Drop, Serde)]
struct ExecutionState {
Expand All @@ -67,11 +13,12 @@ struct ExecutionState {
canceled: u64
}

#[inline(always)]
impl ExecutionStateStorePacking of StorePacking<ExecutionState, felt252> {
#[inline(always)]
fn pack(value: ExecutionState) -> felt252 {
ThreeU64TupleStorePacking::pack((value.started, value.executed, value.canceled))
}
#[inline(always)]
fn unpack(value: felt252) -> ExecutionState {
let (started, executed, canceled) = ThreeU64TupleStorePacking::unpack(value);
ExecutionState { started, executed, canceled }
Expand All @@ -84,41 +31,55 @@ struct TimelockConfig {
window: u64,
}

#[inline(always)]
impl TimelockConfigStorePacking of StorePacking<TimelockConfig, u128> {
#[inline(always)]
fn pack(value: TimelockConfig) -> u128 {
TwoU64TupleStorePacking::pack((value.delay, value.window))
}
#[inline(always)]
fn unpack(value: u128) -> TimelockConfig {
let (delay, window) = TwoU64TupleStorePacking::unpack(value);
TimelockConfig { delay, window }
}
}

#[starknet::interface]
trait ITimelock<TStorage> {
// Queue a list of calls to be executed after the delay. Only the owner may call this.
fn queue(ref self: TStorage, calls: Span<Call>) -> felt252;

// Cancel a queued proposal before it is executed. Only the owner may call this.
fn cancel(ref self: TStorage, id: felt252);

// Execute a list of calls that have previously been queued. Anyone may call this.
fn execute(ref self: TStorage, calls: Span<Call>) -> Array<Span<felt252>>;

// Return the execution window, i.e. the start and end timestamp in which the call can be executed
fn get_execution_window(self: @TStorage, id: felt252) -> ExecutionWindow;

// Get the current owner
fn get_owner(self: @TStorage) -> ContractAddress;

// Returns the delay and the window for call execution
fn get_configuration(self: @TStorage) -> TimelockConfig;

// Transfer ownership, i.e. the address that can queue and cancel calls. This must be self-called via #queue.
fn transfer(ref self: TStorage, to: ContractAddress);
// Configure the delay and the window for call execution. This must be self-called via #queue.
fn configure(ref self: TStorage, config: TimelockConfig);
}

#[derive(Copy, Drop, Serde)]
struct ExecutionWindow {
earliest: u64,
latest: u64
}

#[inline(always)]
impl ExecutionWindowStorePacking of StorePacking<ExecutionWindow, u128> {
fn pack(value: ExecutionWindow) -> u128 {
TwoU64TupleStorePacking::pack((value.earliest, value.latest))
}
fn unpack(value: u128) -> ExecutionWindow {
let (earliest, latest) = TwoU64TupleStorePacking::unpack(value);
ExecutionWindow { earliest, latest, }
}
}


#[starknet::contract]
mod Timelock {
use super::{
ITimelock, ContractAddress, Call, TimelockConfig, ExecutionState,
TimelockConfigStorePacking, ExecutionStateStorePacking, ExecutionWindow,
ExecutionWindowStorePacking, TwoU64TupleStorePacking, ThreeU64TupleStorePacking
TimelockConfigStorePacking, ExecutionStateStorePacking, ExecutionWindow
};
use governance::call_trait::{CallTrait, HashCall};
use hash::{LegacyHash};
Expand Down Expand Up @@ -285,7 +246,7 @@ mod Timelock {

let latest = earliest + configuration.window;

ExecutionWindow { earliest: earliest, latest: latest }
ExecutionWindow { earliest, latest }
}

fn get_owner(self: @ContractState) -> ContractAddress {
Expand Down
1 change: 1 addition & 0 deletions src/utils.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod timestamps;
31 changes: 31 additions & 0 deletions src/utils/timestamps.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use core::integer::{u128_to_felt252, u64_try_from_felt252, u128_safe_divmod};
use starknet::storage_access::{StorePacking};

const TWO_POW_64: u128 = 0x10000000000000000;

impl ThreeU64TupleStorePacking of StorePacking<(u64, u64, u64), felt252> {
#[inline(always)]
fn pack(value: (u64, u64, u64)) -> felt252 {
let (a, b, c) = value;
u256 { low: TwoU64TupleStorePacking::pack((a, b)), high: c.into() }.try_into().unwrap()
}
#[inline(always)]
fn unpack(value: felt252) -> (u64, u64, u64) {
let u256_value: u256 = value.into();
let (a, b) = TwoU64TupleStorePacking::unpack(u256_value.low);
(a, b, (u256_value.high).try_into().unwrap())
}
}

impl TwoU64TupleStorePacking of StorePacking<(u64, u64), u128> {
#[inline(always)]
fn pack(value: (u64, u64)) -> u128 {
let (a, b) = value;
a.into() + b.into() * TWO_POW_64
}
#[inline(always)]
fn unpack(value: u128) -> (u64, u64) {
let (q, r) = u128_safe_divmod(value, TWO_POW_64.try_into().unwrap());
(r.try_into().unwrap(), q.try_into().unwrap())
}
}

0 comments on commit 8fcaf85

Please sign in to comment.