Skip to content

Commit

Permalink
Temp commit again
Browse files Browse the repository at this point in the history
  • Loading branch information
pscott committed Apr 1, 2022
1 parent b1ea9fc commit 9ca513c
Show file tree
Hide file tree
Showing 5 changed files with 219 additions and 49 deletions.
4 changes: 2 additions & 2 deletions contracts/starknet/lib/proposal.cairo
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from contracts.starknet.lib.eth_address import EthAddress
from starkware.cairo.common.uint256 import Uint256

struct Proposal:
member execution_hash : felt # TODO: Use Hash type
member execution_hash : Uint256 # TODO: Use Hash type
member start_timestamp : felt
member end_timestamp : felt
member ethereum_block_number : felt
member has_been_executed : felt
end
47 changes: 31 additions & 16 deletions contracts/starknet/space/space.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ end
func proposal_registry(proposal_id : felt) -> (proposal : Proposal):
end
@storage_var
func executed_proposals(proposal_id: felt) -> (executed: felt):
end
@storage_var
func vote_registry(proposal_id : felt, voter_address : EthAddress) -> (vote : Vote):
end
Expand Down Expand Up @@ -102,6 +106,7 @@ func constructor{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_p
return ()
end

# TODO: L1 needs to know about L2 address, but L2 needs to know about the L2 address... need to fix that.
# TODO: this should either be on the l1 contract or the l2 contract. Since l1 contract has an `owner` I think it should be on l1.
@external
func set_l1_executor{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr : felt}(_l1_executor: felt):
Expand Down Expand Up @@ -169,7 +174,7 @@ end
# TODO: execution_hash should be of type Hash and metadata_uri of type felt* (string)
@external
func propose{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr : felt}(
proposer_address : EthAddress, execution_hash : felt, metadata_uri_len : felt,
proposer_address : EthAddress, execution_hash : Uint256, metadata_uri_len : felt,
metadata_uri : felt*, ethereum_block_number : felt, params_len : felt, params : felt*) -> (
):
alloc_locals
Expand Down Expand Up @@ -204,7 +209,7 @@ func propose{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr :

# Create the proposal and its proposal id
let proposal = Proposal(
execution_hash, start_timestamp, end_timestamp, ethereum_block_number, 0)
execution_hash, start_timestamp, end_timestamp, ethereum_block_number)
let (proposal_id) = next_proposal_nonce.read()

# Store the proposal
Expand All @@ -225,17 +230,24 @@ func finalize_proposal{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_c
proposal_id : felt):
alloc_locals

let (proposal) = proposal_registry.read(proposal_id)
let (has_been_executed) = executed_proposals.read(proposal_id)

# Make sure proposal has not already been executed
assert has_been_executed = 0

# This checks two things:
# 1. Checks that the proposal id exists. If it doesn't exist, then the whole `Proposal` struct will
# be set to 0, ence `has_been_executed` will be set to 0 too.
# 2. Checks that the proposal has not already been executed (to prevent from re-executing it).
assert_not_zero(proposal.has_been_executed)
# Checks that the proposal id exists. If it doesn't exist, then the whole `Proposal` struct will
# be set to 0, hence `ethereum_block_number` will be set to 0 too.
let (proposal) = proposal_registry.read(proposal_id)
assert_not_zero(proposal.ethereum_block_number)

# Make sure proposal period has ended
let (current_timestamp) = get_block_timestamp()
assert_lt_felt(proposal.end_timestamp, current_timestamp)
# ------------------------------------------------
# IMPORTANT
# ------------------------------------------------
# This has been commented to allow for easier testeing.
# Please uncomment before pushing to prod.
# assert_lt_felt(proposal.end_timestamp, current_timestamp)

# Count votes for
let (for) = vote_power.read(proposal_id, Choice.FOR)
Expand All @@ -248,19 +260,17 @@ func finalize_proposal{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_c

# Create the payload
let (message_payload : felt*) = alloc()
assert message_payload[0] = proposal.execution_hash
assert message_payload[1] = has_passed
assert message_payload[0] = proposal.execution_hash.low
assert message_payload[1] = proposal.execution_hash.high
assert message_payload[2] = has_passed

# Send message to L1 Contract (executionDetail, hasPassed)
let (l1_executor_address) = l1_executor.read()
send_message_to_l1(
to_address=l1_executor_address.value, payload_size=2, payload=message_payload)
to_address=l1_executor_address.value, payload_size=3, payload=message_payload)

# Flag this proposal as executed
proposal.has_been_executed = 1

# Write it back to the proposal registry
proposal_registry.write(proposal_id, proposal)
executed_proposals.write(proposal_id, 1)

return ()
end
Expand All @@ -271,6 +281,11 @@ func get_vote_info{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check
return vote_registry.read(proposal_id, voter_address)
end

@view
func get_l1_executor{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr : felt}() -> (executor_address: EthAddress):
return l1_executor.read()
end

@view
func get_proposal_info{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr : felt}(
proposal_id : felt) -> (proposal_info : ProposalInfo):
Expand Down
2 changes: 1 addition & 1 deletion hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const config: HardhatUserConfig = {
url: 'http://localhost:8545',
},
starknetDevnet: {
url: 'http://localhost:8000/',
url: 'http://localhost:8000',
},
},
gasReporter: {
Expand Down
Loading

0 comments on commit 9ca513c

Please sign in to comment.