Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:snapshot-labs/sx-starknet into f…
Browse files Browse the repository at this point in the history
…eat_struct_packing
  • Loading branch information
pscott committed Aug 21, 2023
2 parents 0df907d + 3871e5f commit ce68c57
Show file tree
Hide file tree
Showing 27 changed files with 491 additions and 412 deletions.
6 changes: 3 additions & 3 deletions starknet/src/authenticators/eth_tx.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ mod EthTxAuthenticator {
user_proposal_validation_params: Array<felt252>,
metadata_URI: Array<felt252>
) {
let mut payload = ArrayTrait::<felt252>::new();
let mut payload = array![];
target.serialize(ref payload);
PROPOSE_SELECTOR.serialize(ref payload);
author.serialize(ref payload);
Expand Down Expand Up @@ -92,7 +92,7 @@ mod EthTxAuthenticator {
user_voting_strategies: Array<IndexedStrategy>,
metadata_URI: Array<felt252>
) {
let mut payload = ArrayTrait::<felt252>::new();
let mut payload = array![];
target.serialize(ref payload);
VOTE_SELECTOR.serialize(ref payload);
voter.serialize(ref payload);
Expand Down Expand Up @@ -124,7 +124,7 @@ mod EthTxAuthenticator {
execution_strategy: Strategy,
metadata_URI: Array<felt252>
) {
let mut payload = ArrayTrait::<felt252>::new();
let mut payload = array![];
target.serialize(ref payload);
UPDATE_PROPOSAL_SELECTOR.serialize(ref payload);
author.serialize(ref payload);
Expand Down
2 changes: 1 addition & 1 deletion starknet/src/execution_strategies/eth_relayer.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ mod EthRelayerExecutionStrategy {
// keccak hash of the proposal execution payload
let execution_hash = u256 { low: payload[2], high: payload[1] };

let mut message_payload = ArrayTrait::<felt252>::new();
let mut message_payload = array![];
space.serialize(mut message_payload);
proposal.serialize(mut message_payload);
votes_for.serialize(mut message_payload);
Expand Down
14 changes: 10 additions & 4 deletions starknet/src/factory/factory.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ trait IFactory<TContractState> {
self: @TContractState,
class_hash: ClassHash,
contract_address_salt: felt252,
calldata: Span<felt252>
initialize_calldata: Span<felt252>
) -> ContractAddress;
}

Expand All @@ -17,9 +17,11 @@ mod Factory {
use super::IFactory;
use starknet::ContractAddress;
use starknet::contract_address_const;
use starknet::syscalls::deploy_syscall;
use starknet::syscalls::{deploy_syscall, call_contract_syscall};
use starknet::ClassHash;
use result::ResultTrait;
use array::{ArrayTrait, SpanTrait};
use sx::utils::constants::INITIALIZE_SELECTOR;

#[event]
fn SpaceDeployed(class_hash: ClassHash, space_address: ContractAddress) {}
Expand All @@ -33,13 +35,17 @@ mod Factory {
self: @ContractState,
class_hash: ClassHash,
contract_address_salt: felt252,
calldata: Span<felt252>
initialize_calldata: Span<felt252>
) -> ContractAddress {
let (space_address, _) = deploy_syscall(
class_hash, contract_address_salt, calldata, false
class_hash, contract_address_salt, array![].span(), false
)
.unwrap();

// Call initializer.
call_contract_syscall(space_address, INITIALIZE_SELECTOR, initialize_calldata)
.unwrap_syscall();

SpaceDeployed(class_hash, space_address);

space_address
Expand Down
144 changes: 89 additions & 55 deletions starknet/src/space/space.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@ trait ISpace<TContractState> {
fn transfer_ownership(ref self: TContractState, new_owner: ContractAddress);
fn renounce_ownership(ref self: TContractState);
// Actions
fn initialize(
ref self: TContractState,
owner: ContractAddress,
min_voting_duration: u32,
max_voting_duration: u32,
voting_delay: u32,
proposal_validation_strategy: Strategy,
proposal_validation_strategy_metadata_URI: Array<felt252>,
voting_strategies: Array<Strategy>,
voting_strategy_metadata_URIs: Array<Array<felt252>>,
authenticators: Array<ContractAddress>,
metadata_URI: Array<felt252>,
dao_URI: Array<felt252>,
);
fn propose(
ref self: TContractState,
author: UserAddress,
Expand All @@ -53,14 +67,16 @@ trait ISpace<TContractState> {
metadata_URI: Array<felt252>,
);
fn cancel_proposal(ref self: TContractState, proposal_id: u256);
fn upgrade(ref self: TContractState, class_hash: ClassHash);
fn upgrade(
ref self: TContractState, class_hash: ClassHash, initialize_calldata: Array<felt252>
);
}

#[starknet::contract]
mod Space {
use super::ISpace;
use starknet::{ClassHash, ContractAddress, info, Store};
use starknet::storage_access::{StorePacking, StoreUsingPacking};
use starknet::{ClassHash, ContractAddress, info, Store, syscalls};
use zeroable::Zeroable;
use array::{ArrayTrait, SpanTrait};
use clone::Clone;
Expand All @@ -78,14 +94,17 @@ mod Space {
PackedProposal, IndexedStrategyTrait, IndexedStrategyImpl, UpdateSettingsCalldata,
NoUpdateU32, NoUpdateStrategy, NoUpdateArray
};
use sx::utils::reinitializable::Reinitializable;
use sx::utils::ReinitializableImpl;
use sx::utils::bits::BitSetter;
use sx::utils::legacy_hash::LegacyHashChoice;
use sx::external::ownable::Ownable;
use sx::utils::constants::INITIALIZE_SELECTOR;

#[storage]
struct Storage {
_max_voting_duration: u32,
_min_voting_duration: u32,
_max_voting_duration: u32,
_next_proposal_id: u256,
_voting_delay: u32,
_active_voting_strategies: u256,
Expand All @@ -102,9 +121,9 @@ mod Space {
fn SpaceCreated(
_space: ContractAddress,
_owner: ContractAddress,
_voting_delay: u32,
_min_voting_duration: u32,
_max_voting_duration: u32,
_voting_delay: u32,
_proposal_validation_strategy: @Strategy,
_proposal_validation_strategy_metadata_URI: @Array<felt252>,
_voting_strategies: @Array<Strategy>,
Expand Down Expand Up @@ -180,10 +199,56 @@ mod Space {
fn VotingDelayUpdated(_new_voting_delay: u32) {}

#[event]
fn Upgraded(class_hash: ClassHash) {}
fn Upgraded(class_hash: ClassHash, initialize_calldata: Array<felt252>) {}

#[external(v0)]
impl Space of ISpace<ContractState> {
fn initialize(
ref self: ContractState,
owner: ContractAddress,
min_voting_duration: u32,
max_voting_duration: u32,
voting_delay: u32,
proposal_validation_strategy: Strategy,
proposal_validation_strategy_metadata_URI: Array<felt252>,
voting_strategies: Array<Strategy>,
voting_strategy_metadata_URIs: Array<Array<felt252>>,
authenticators: Array<ContractAddress>,
metadata_URI: Array<felt252>,
dao_URI: Array<felt252>,
) {
SpaceCreated(
info::get_contract_address(),
owner,
min_voting_duration,
max_voting_duration,
voting_delay,
@proposal_validation_strategy,
@proposal_validation_strategy_metadata_URI,
@voting_strategies,
@voting_strategy_metadata_URIs,
@authenticators,
@metadata_URI,
@dao_URI
);
// Checking that the contract is not already initialized
//TODO: temporary component syntax (see imports too)
let mut state: Reinitializable::ContractState =
Reinitializable::unsafe_new_contract_state();
ReinitializableImpl::initialize(ref state);

//TODO: temporary component syntax
let mut state: Ownable::ContractState = Ownable::unsafe_new_contract_state();
Ownable::initializer(ref state);
Ownable::transfer_ownership(ref state, owner);
_set_min_voting_duration(ref self, min_voting_duration);
_set_max_voting_duration(ref self, max_voting_duration);
_set_voting_delay(ref self, voting_delay);
_set_proposal_validation_strategy(ref self, proposal_validation_strategy);
_add_voting_strategies(ref self, voting_strategies);
_add_authenticators(ref self, authenticators);
self._next_proposal_id.write(1_u256);
}
fn propose(
ref self: ContractState,
author: UserAddress,
Expand Down Expand Up @@ -231,7 +296,7 @@ mod Space {
// TODO: Lots of copying, maybe figure out how to pass snapshots to events/storage writers.
self._proposals.write(proposal_id, proposal);

self._next_proposal_id.write(proposal_id + u256 { low: 1_u128, high: 0_u128 });
self._next_proposal_id.write(proposal_id + 1_u256);

ProposalCreated(
proposal_id, author, snap_proposal, @execution_strategy.params, @metadata_URI
Expand Down Expand Up @@ -270,7 +335,7 @@ mod Space {
proposal.active_voting_strategies
);

assert(voting_power > u256 { low: 0_u128, high: 0_u128 }, 'User has no voting power');
assert(voting_power > 0_u256, 'User has no voting power');
self
._vote_power
.write(
Expand Down Expand Up @@ -345,15 +410,26 @@ mod Space {
ProposalCancelled(proposal_id);
}

fn upgrade(ref self: ContractState, class_hash: ClassHash) {
fn upgrade(
ref self: ContractState, class_hash: ClassHash, initialize_calldata: Array<felt252>
) {
let state: Ownable::ContractState = Ownable::unsafe_new_contract_state();
Ownable::assert_only_owner(@state);

assert(
class_hash.is_non_zero(), 'Class Hash cannot be zero'
); // TODO: not sure this is needed
assert(class_hash.is_non_zero(), 'Class Hash cannot be zero');
starknet::replace_class_syscall(class_hash).unwrap_syscall();
Upgraded(class_hash);

// Allowing initializer to be called again.
let mut state: Reinitializable::ContractState =
Reinitializable::unsafe_new_contract_state();
ReinitializableImpl::reinitialize(ref state);

// Call initializer on the new version.
syscalls::call_contract_syscall(
info::get_contract_address(), INITIALIZE_SELECTOR, initialize_calldata.span()
)
.unwrap_syscall();
Upgraded(class_hash, initialize_calldata);
}

fn owner(self: @ContractState) -> ContractAddress {
Expand Down Expand Up @@ -486,48 +562,6 @@ mod Space {
}
}

#[constructor]
fn constructor(
ref self: ContractState,
_owner: ContractAddress,
_max_voting_duration: u32,
_min_voting_duration: u32,
_voting_delay: u32,
_proposal_validation_strategy: Strategy,
_proposal_validation_strategy_metadata_URI: Array<felt252>,
_voting_strategies: Array<Strategy>,
_voting_strategies_metadata_URIs: Array<Array<felt252>>,
_authenticators: Array<ContractAddress>,
_metadata_URI: Array<felt252>,
_dao_URI: Array<felt252>
) {
//TODO: temporary component syntax
let mut state: Ownable::ContractState = Ownable::unsafe_new_contract_state();
Ownable::initializer(ref state);
Ownable::transfer_ownership(ref state, _owner);
_set_max_voting_duration(ref self, _max_voting_duration);
_set_min_voting_duration(ref self, _min_voting_duration);
_set_voting_delay(ref self, _voting_delay);
_set_proposal_validation_strategy(ref self, _proposal_validation_strategy.clone());
_add_voting_strategies(ref self, _voting_strategies.clone());
_add_authenticators(ref self, _authenticators.clone());
self._next_proposal_id.write(u256 { low: 1_u128, high: 0_u128 });
SpaceCreated(
info::get_contract_address(),
_owner,
_voting_delay,
_min_voting_duration,
_max_voting_duration,
@_proposal_validation_strategy,
@_proposal_validation_strategy_metadata_URI,
@_voting_strategies,
@_voting_strategies_metadata_URIs,
@_authenticators,
@_metadata_URI,
@_dao_URI
);
}

///
/// Internals
///
Expand All @@ -549,7 +583,7 @@ mod Space {
allowed_strategies: u256
) -> u256 {
user_strategies.assert_no_duplicate_indices();
let mut total_voting_power = u256 { low: 0_u128, high: 0_u128 };
let mut total_voting_power = 0_u256;
let mut i = 0_usize;
loop {
if i >= user_strategies.len() {
Expand Down
1 change: 1 addition & 0 deletions starknet/src/tests/mocks.cairo
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
mod proposal_validation_always_fail;
mod executor;
mod space_v2;
30 changes: 30 additions & 0 deletions starknet/src/tests/mocks/space_v2.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#[starknet::interface]
trait ISpaceV2<TContractState> {
fn initialize(ref self: TContractState, var: felt252);
fn get_var(self: @TContractState) -> felt252;
}

#[starknet::contract]
mod SpaceV2 {
use super::ISpaceV2;
use sx::utils::reinitializable::Reinitializable;
use sx::utils::ReinitializableImpl;
#[storage]
struct Storage {
_var: felt252
}

#[external(v0)]
impl SpaceV2 of ISpaceV2<ContractState> {
fn initialize(ref self: ContractState, var: felt252) {
// TODO: Temp component syntax
let mut state: Reinitializable::ContractState =
Reinitializable::unsafe_new_contract_state();
ReinitializableImpl::initialize(ref state);
self._var.write(var);
}
fn get_var(self: @ContractState) -> felt252 {
self._var.read()
}
}
}
Loading

0 comments on commit ce68c57

Please sign in to comment.