Skip to content

Commit

Permalink
GA tests (#153)
Browse files Browse the repository at this point in the history
* Bump ethers from 5.6.7 to 5.6.8 (#147)

Bumps [ethers](https://github.com/ethers-io/ethers.js/tree/HEAD/packages/ethers) from 5.6.7 to 5.6.8.
- [Release notes](https://github.com/ethers-io/ethers.js/releases)
- [Changelog](https://github.com/ethers-io/ethers.js/blob/master/CHANGELOG.md)
- [Commits](https://github.com/ethers-io/ethers.js/commits/v5.6.8/packages/ethers)

---
updated-dependencies:
- dependency-name: ethers
  dependency-type: direct:development
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump typescript from 4.6.4 to 4.7.2 (#146)

Bumps [typescript](https://github.com/Microsoft/TypeScript) from 4.6.4 to 4.7.2.
- [Release notes](https://github.com/Microsoft/TypeScript/releases)
- [Commits](microsoft/TypeScript@v4.6.4...v4.7.2)

---
updated-dependencies:
- dependency-name: typescript
  dependency-type: direct:development
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Add starknet auth (#138)

* Update  to 0.8.1 and devnet 0.2.1; Format

* Add execute lib; Switch to authenticate method

* Add felt_to_uint256; Add starknet_tx auth

* Use felt instead of u256

* Add test for starknet tx auth

* Add starknet_tx_auth and starknet_account_auth; blocked by compilation on starknet_account_auth

* Remove starknet_account test; Rename starknet_account to starknet_sig

* Add comment about hash

* Update to 0.8.2

* Add voting_strategy_params_all to setup functions

* Bump npm from 8.10.0 to 8.11.0 (#149)

Bumps [npm](https://github.com/npm/cli) from 8.10.0 to 8.11.0.
- [Release notes](https://github.com/npm/cli/releases)
- [Changelog](https://github.com/npm/cli/blob/latest/CHANGELOG.md)
- [Commits](npm/cli@v8.10.0...v8.11.0)

---
updated-dependencies:
- dependency-name: npm
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* renaming tests

* renamed cairo contracts

* fixed starknet tx auth test

* formatting

* updated starknet hardhat

* fixed account import

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: pscott <30843220+pscott@users.noreply.github.com>
  • Loading branch information
3 people authored May 27, 2022
1 parent 06de52c commit d856f47
Show file tree
Hide file tree
Showing 28 changed files with 688 additions and 467 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,8 @@ jobs:
run: |
sudo apt install -y libgmp3-dev
python -m pip install --upgrade pip
pip install starknet-devnet==0.1.19
pip install werkzeug==2.0.3
pip install starknet-devnet==0.2.2
pip install openzeppelin-cairo-contracts
pip install marshmallow-dataclass==8.5.3 --force
- name: Compile Contracts
run: yarn compile
Expand Down
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@ Programmable governance on StarkNet. Refer to the [documentation](https://docs.s
```ml
contracts
├─ starknet
│ ├─ authenticators
│ ├─ Authenticators
│ │ ├─ EthTx.cairo — "Authenticate user via an Ethereum transaction"
│ │ ├─ StarkTx.cairo — "Authenticate user via a StarkNet transaction"
│ │ ├─ StarkSig.cairo — "Authenticate user via a Starknet signature"
│ │ └─ Vanilla.cairo — "Dummy authentication"
│ ├─ voting-strategies
│ ├─ VotingStrategies
│ │ ├─ SingleSlotProof.cairo — "Enables the use the contents of an Ethereum storage slot as voting power"
│ │ ├─ Vanilla.cairo — "Voting power of 1 for every user"
│ │ └─ Whitelist.cairo — "Predetermined voting power for members in a whitelist, otherwise zero"
│ ├─ execution-strategies
│ ├─ ExecutionStrategies
│ │ ├─ Vanilla.cairo — "Dummy execution"
│ │ └─ ZodiacRelayer.cairo — "Strategy to execute proposal transactions using an Ethereum Gnosis Safe"
│ ├─ interfaces
│ ├─ Interfaces
│ │ ├─ IAuthenticator.cairo — "Interface for all authenticators"
│ │ ├─ IExecutionStrategy.cairo — "Interface for all execution strategies"
│ │ └─ IVotingStrategy.cairo — "Interface for all voting strategies"
Expand All @@ -34,7 +36,7 @@ contracts
│ │ ├─ slot_key.cairo — "Function to find the slot key for a slot in the Ethereum state"
│ │ ├─ vote.cairo.cairo — "User vote data type"
│ │ └─ words.cairo — "Small 64 bit word library"
│ ├─ test-contracts
│ ├─ TestContracts
│ │ ├─ Test_array2d.cairo
│ │ ├─ Test_words.cairo
│ │ └─ Test_words_to_uint256.cairo
Expand All @@ -44,7 +46,7 @@ contracts
│ └─ IStarknetCore.sol — "Authenticate user via an Ethereum transaction"
├─ StarkNetCommit
│ └─ StarknetCommit.sol — "Authenticate user via an Ethereum transaction"
├─ SnapshotXZodiacModule
├─ ZodiacModule
│ ├─ ProposalRelayer.sol — "Authenticate user via an Ethereum transaction"
│ ├─ SnapshotXL1Executor.sol — "Authenticate user via an Ethereum transaction"
│ └─ deps.sol — "No authentication of users"
Expand Down
10 changes: 3 additions & 7 deletions contracts/starknet/Authenticators/EthTx.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ from starkware.cairo.common.memcpy import memcpy
from starkware.cairo.common.math import assert_not_equal
from contracts.starknet.lib.eth_address import EthAddress
from contracts.starknet.lib.hash_array import hash_array
from contracts.starknet.lib.execute import execute

# Address of the StarkNet Commit L1 contract which acts as the origin address of the messages sent to this contract.
@storage_var
Expand Down Expand Up @@ -42,7 +43,7 @@ func commit{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr :
end
@external
func execute{syscall_ptr : felt*, range_check_ptr, pedersen_ptr : HashBuiltin*}(
func authenticate{syscall_ptr : felt*, range_check_ptr, pedersen_ptr : HashBuiltin*}(
target : felt, function_selector : felt, calldata_len : felt, calldata : felt*
):
alloc_locals
Expand All @@ -65,11 +66,6 @@ func execute{syscall_ptr : felt*, range_check_ptr, pedersen_ptr : HashBuiltin*}(
# Clear the hash from the contract by writing the zero address to the mapping.
commit_store.write(hash, EthAddress(0))
# Execute the function call with calldata supplied.
call_contract(
contract_address=target,
function_selector=function_selector,
calldata_size=calldata_len,
calldata=calldata,
)
execute(target, function_selector, calldata_len, calldata)
return ()
end
36 changes: 36 additions & 0 deletions contracts/starknet/Authenticators/StarkSig.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
%lang starknet

from contracts.starknet.lib.execute import execute
from contracts.starknet.lib.felt_to_uint256 import felt_to_uint256
from starkware.starknet.common.syscalls import get_caller_address
from starkware.cairo.common.cairo_builtins import BitwiseBuiltin
from starkware.cairo.common.math import assert_not_zero
from starkware.cairo.common.uint256 import uint256_eq
# from openzeppelin.account.IAccount import IAccount

# TODO: change `hash` to actual data, not simply the hash
@external
func authenticate{syscall_ptr : felt*, range_check_ptr, bitwise_ptr : BitwiseBuiltin*}(
hash : felt,
sig_len : felt,
sig : felt*,
target : felt,
function_selector : felt,
calldata_len : felt,
calldata : felt*,
) -> ():
# Voter address or proposer address should be located in calldata[0]
let user_address = calldata[0]

# Will throw if signature is invalid
# with_attr error_message("Invalid signature"):
# IAccount.is_valid_signature(
# contract_address=user_address, hash=hash, signature_len=sig_len, signature=sig
# )
# end
# Call the contract
execute(target, function_selector, calldata_len, calldata)
return ()
end
22 changes: 22 additions & 0 deletions contracts/starknet/Authenticators/StarkTx.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
%lang starknet
from contracts.starknet.lib.execute import execute
from contracts.starknet.lib.felt_to_uint256 import felt_to_uint256
from starkware.starknet.common.syscalls import get_caller_address
from starkware.cairo.common.cairo_builtins import BitwiseBuiltin
from starkware.cairo.common.uint256 import uint256_eq

@external
func authenticate{syscall_ptr : felt*, range_check_ptr, bitwise_ptr : BitwiseBuiltin*}(
target : felt, function_selector : felt, calldata_len : felt, calldata : felt*
) -> ():
let (caller_address) = get_caller_address()

with_attr error_message("Incorrect caller"):
assert caller_address = calldata[0]
end
# Call the contract
execute(target, function_selector, calldata_len, calldata)
return ()
end
11 changes: 3 additions & 8 deletions contracts/starknet/Authenticators/Vanilla.cairo
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
%lang starknet
from starkware.starknet.common.syscalls import call_contract
from contracts.starknet.lib.execute import execute

# Forwards `data` to `target` without verifying anything.
@external
func execute{syscall_ptr : felt*, range_check_ptr}(
func authenticate{syscall_ptr : felt*, range_check_ptr}(
target : felt, function_selector : felt, calldata_len : felt, calldata : felt*
) -> ():
# TODO: Actually verify the signature

# Call the contract
call_contract(
contract_address=target,
function_selector=function_selector,
calldata_size=calldata_len,
calldata=calldata,
)
execute(target, function_selector, calldata_len, calldata)

return ()
end
11 changes: 9 additions & 2 deletions contracts/starknet/Space.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ from starkware.cairo.common.uint256 import Uint256, uint256_add, uint256_lt, uin
from starkware.cairo.common.bool import TRUE, FALSE
from starkware.cairo.common.hash_state import hash_init, hash_update
from starkware.cairo.common.math import (
assert_lt, assert_le, assert_nn, assert_not_zero, assert_lt_felt
assert_lt,
assert_le,
assert_nn,
assert_not_zero,
assert_lt_felt,
)

from contracts.starknet.Interfaces.IVotingStrategy import IVotingStrategy
Expand All @@ -23,7 +27,10 @@ from contracts.starknet.lib.array2d import Immutable2DArray, construct_array2d,
from contracts.starknet.lib.slot_key import get_slot_key

from openzeppelin.access.ownable import (
Ownable_only_owner, Ownable_transfer_ownership, Ownable_get_owner, Ownable_initializer
Ownable_only_owner,
Ownable_transfer_ownership,
Ownable_get_owner,
Ownable_initializer,
)

#
Expand Down
11 changes: 9 additions & 2 deletions contracts/starknet/VotingStrategies/SingleSlotProof.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,15 @@ func get_voting_power{
let (local fact_registry_addr) = fact_registry_store.read()

# Decoding voting strategy parameters
let (slot, proof_sizes_bytes_len, proof_sizes_bytes, proof_sizes_words_len, proof_sizes_words,
proofs_concat_len, proofs_concat) = decode_param_array(user_params_len, user_params)
let (
slot,
proof_sizes_bytes_len,
proof_sizes_bytes,
proof_sizes_words_len,
proof_sizes_words,
proofs_concat_len,
proofs_concat,
) = decode_param_array(user_params_len, user_params)

# Checking that the parameters array is valid and then extracting the individual parameters
# For the single slot proof strategy, the parameters array is length 2 where the first element is the
Expand Down
17 changes: 17 additions & 0 deletions contracts/starknet/lib/execute.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
%lang starknet
from starkware.starknet.common.syscalls import call_contract

# Forwards `data` to `target` without verifying anything.
@external
func execute{syscall_ptr : felt*, range_check_ptr}(
target : felt, function_selector : felt, calldata_len : felt, calldata : felt*
) -> ():
call_contract(
contract_address=target,
function_selector=function_selector,
calldata_size=calldata_len,
calldata=calldata,
)

return ()
end
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"concurrently": "^7.2.1",
"ethereumjs-util": "^7.1.4",
"install": "^0.13.0",
"npm": "^8.10.0",
"npm": "^8.11.0",
"starknet": "^2.5.1",
"sx-api": "snapshot-labs/sx-api",
"wait-on": "^6.0.1",
Expand All @@ -43,7 +43,7 @@
"@nomiclabs/hardhat-etherscan": "^3.0.3",
"@nomiclabs/hardhat-solhint": "^2.0.1",
"@nomiclabs/hardhat-waffle": "^2.0.2",
"@shardlabs/starknet-hardhat-plugin": "^0.5.3",
"@shardlabs/starknet-hardhat-plugin": "^0.5.5",
"@typechain/ethers-v5": "^7.2.0",
"@typechain/hardhat": "^2.3.1",
"@types/chai": "^4.3.0",
Expand All @@ -61,7 +61,7 @@
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-promise": "^5.2.0",
"ethereum-waffle": "^3.4.0",
"ethers": "^5.6.7",
"ethers": "^5.6.8",
"hardhat": "^2.9.5",
"hardhat-gas-reporter": "^1.0.7",
"prettier": "^2.5.1",
Expand All @@ -71,6 +71,6 @@
"solidity-coverage": "^0.7.18",
"ts-node": "^10.8.0",
"typechain": "^5.2.0",
"typescript": "^4.6.4"
"typescript": "^4.7.2"
}
}
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
https://github.com/starkware-libs/cairo-lang/releases/download/v0.8.0/cairo-lang-0.8.0.zip
https://github.com/starkware-libs/cairo-lang/releases/download/v0.8.2/cairo-lang-0.8.2.zip
openzeppelin-cairo-contracts
4 changes: 2 additions & 2 deletions scripts/test-crosschain.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

yarn wait-on tcp:8000 &&
wait-on tcp:8545 &&
yarn hardhat test test/crosschain/ZodiacExecution.ts --network 'ethereumLocal' --starknet-network 'starknetLocal' &&
yarn hardhat test test/crosschain/ZodiacExecution.test.ts --network 'ethereumLocal' --starknet-network 'starknetLocal' &&
sleep 10 &&
yarn hardhat test test/crosschain/EthTxAuth.ts --network 'ethereumLocal' --starknet-network 'starknetLocal'
yarn hardhat test test/crosschain/EthTxAuth.test.ts --network 'ethereumLocal' --starknet-network 'starknetLocal'
if [ $? -eq 0 ]
then
kill -9 $(lsof -t -i:8545)
Expand Down
30 changes: 20 additions & 10 deletions test/crosschain/EthTxAuth.ts → test/crosschain/EthTxAuth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers';
import { expect } from 'chai';
import { Contract } from 'ethers';
import { starknet, network, ethers } from 'hardhat';
import { stark } from 'starknet';
import { StarknetContract, Account, HttpNetworkConfig } from 'hardhat/types';
import { strToShortStringArr } from '@snapshot-labs/sx';
import { createExecutionHash, getCommit, getProposeCalldata } from '../shared/helpers';
import { ethTxAuthSetup } from '../shared/setup';
import { proposeSelector, voteSelector } from '../shared/constants';

const { getSelectorFromName } = stark;

// Dummy tx
const tx1 = {
Expand Down Expand Up @@ -94,13 +96,15 @@ describe('L1 interaction with Snapshot X', function () {
// Committing the hash of the payload to the StarkNet Commit L1 contract
await starknetCommit
.connect(signer)
.commit(getCommit(BigInt(space.address), proposeSelector, proposeCalldata));
.commit(
getCommit(BigInt(space.address), BigInt(getSelectorFromName('propose')), proposeCalldata)
);
// Checking that the L1 -> L2 message has been propogated
expect((await starknet.devnet.flush()).consumed_messages.from_l1).to.have.a.lengthOf(1);
// Creating proposal
await ethTxAuthenticator.invoke('execute', {
target: BigInt(space.address),
function_selector: proposeSelector,
function_selector: BigInt(getSelectorFromName('propose')),
calldata: proposeCalldata,
});
});
Expand All @@ -109,18 +113,20 @@ describe('L1 interaction with Snapshot X', function () {
await starknet.devnet.loadL1MessagingContract(networkUrl, mockStarknetMessaging.address);
await starknetCommit
.connect(signer)
.commit(getCommit(BigInt(space.address), proposeSelector, proposeCalldata));
.commit(
getCommit(BigInt(space.address), BigInt(getSelectorFromName('propose')), proposeCalldata)
);
await starknet.devnet.flush();
await ethTxAuthenticator.invoke('execute', {
target: BigInt(space.address),
function_selector: proposeSelector,
function_selector: BigInt(getSelectorFromName('propose')),
calldata: proposeCalldata,
});
// Second attempt at calling execute should fail
try {
await ethTxAuthenticator.invoke('execute', {
target: BigInt(space.address),
function_selector: proposeSelector,
function_selector: BigInt(getSelectorFromName('propose')),
calldata: proposeCalldata,
});
} catch (err: any) {
Expand All @@ -132,12 +138,14 @@ describe('L1 interaction with Snapshot X', function () {
await starknet.devnet.loadL1MessagingContract(networkUrl, mockStarknetMessaging.address);
await starknetCommit
.connect(signer)
.commit(getCommit(BigInt(space.address), voteSelector, proposeCalldata)); // Wrong selector
.commit(
getCommit(BigInt(space.address), BigInt(getSelectorFromName('vote')), proposeCalldata)
); // Wrong selector
await starknet.devnet.flush();
try {
await ethTxAuthenticator.invoke('execute', {
target: BigInt(space.address),
function_selector: proposeSelector,
function_selector: BigInt(getSelectorFromName('propose')),
calldata: proposeCalldata,
});
} catch (err: any) {
Expand All @@ -150,12 +158,14 @@ describe('L1 interaction with Snapshot X', function () {
proposeCalldata[0] = BigInt(ethers.Wallet.createRandom().address); // Random l1 address in the calldata
await starknetCommit
.connect(signer)
.commit(getCommit(BigInt(space.address), proposeSelector, proposeCalldata));
.commit(
getCommit(BigInt(space.address), BigInt(getSelectorFromName('propose')), proposeCalldata)
);
await starknet.devnet.flush();
try {
await ethTxAuthenticator.invoke('execute', {
target: BigInt(space.address),
function_selector: proposeSelector,
function_selector: BigInt(getSelectorFromName('propose')),
calldata: proposeCalldata,
});
} catch (err: any) {
Expand Down
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit d856f47

Please sign in to comment.