Skip to content

Commit

Permalink
remove nonce param from . add nonce tests
Browse files Browse the repository at this point in the history
  • Loading branch information
martriay committed Oct 13, 2021
1 parent 113d247 commit 2c2e1bb
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 40 deletions.
7 changes: 4 additions & 3 deletions contracts/Account.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ func execute{
selector: felt,
calldata_len: felt,
calldata: felt*,
nonce: felt,
sig_r: felt,
sig_s: felt
) -> (response : felt):
Expand All @@ -172,14 +171,16 @@ func execute{
let (__fp__, _) = get_fp_and_pc()
# protect against replays accross accounts sharing keys
let (_address) = address.read()
local message: Message = Message(to, selector, calldata, calldata_size=calldata_len, _address, nonce)
let (_current_nonce) = current_nonce.read()
local _current_nonce = _current_nonce # prevent revokation

local message: Message = Message(to, selector, calldata, calldata_size=calldata_len, _address, _current_nonce)
local signed_message: SignedMessage = SignedMessage(&message, sig_r, sig_s)

# validate transaction
validate(&signed_message)

# bump nonce
let (_current_nonce) = current_nonce.read()
current_nonce.write(_current_nonce + 1)

# execute call
Expand Down
1 change: 0 additions & 1 deletion contracts/IAccount.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ namespace IAccount:
selector: felt,
calldata_len: felt,
calldata: felt*,
nonce: felt,
sig_r: felt,
sig_s: felt,
) -> (response: felt):
Expand Down
62 changes: 30 additions & 32 deletions test/Account.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import pytest
import asyncio
from starkware.starknet.testing.starknet import Starknet
from starkware.starkware_utils.error_handling import StarkException
from starkware.starknet.definitions.error_codes import StarknetErrorCode
from utils.Signer import Signer

signer = Signer(123456789987654321)
Expand Down Expand Up @@ -35,54 +37,50 @@ async def test_execute(account_factory):
starknet, account = account_factory
initializable = await starknet.deploy("contracts/Initializable.cairo")

transaction = await signer.build_transaction(
account, initializable.contract_address, 'initialize', [])

assert await initializable.initialized().call() == (0,)
await transaction.invoke()
await signer.send_transaction(account, initializable.contract_address, 'initialize', [])
assert await initializable.initialized().call() == (1,)


# @pytest.mark.asyncio
# async def test_nonce(account_factory):
# starknet, account = account_factory
# initializable = await starknet.deploy("contracts/Initializable.cairo")

# await signer.build_transaction(
# account, initializable.contract_address, 'initialize', []).invoke()
@pytest.mark.asyncio
async def test_nonce(account_factory):
starknet, account = account_factory
initializable = await starknet.deploy("contracts/Initializable.cairo")

# try:
# await signer.build_transaction(
# account, initializable.contract_address, 'initialize', []).invoke()
# except:
# assert 4 == 0
current_nonce, = await account.get_nonce().call()

# lower nonce
try:
await signer.send_transaction(account, initializable.contract_address, 'initialize', [], current_nonce - 1)
except StarkException as err:
_, error = err.args
assert error['code'] == StarknetErrorCode.TRANSACTION_FAILED
assert await initializable.initialized().call() == (0,)

# higher nonce
try:
await signer.send_transaction(account, initializable.contract_address, 'initialize', [], current_nonce + 1)
except StarkException as err:
_, error = err.args
assert error['code'] == StarknetErrorCode.TRANSACTION_FAILED
assert await initializable.initialized().call() == (0,)

# right nonce
await signer.send_transaction(account, initializable.contract_address, 'initialize', [], current_nonce)
assert await initializable.initialized().call() == (1,)


@pytest.mark.asyncio
async def test_L1_address_setter(account_factory):
_, account = account_factory
assert await account.get_L1_address().call() == (L1_ADDRESS,)

tx = await signer.build_transaction(
account, account.contract_address, 'set_L1_address', [ANOTHER_ADDRESS])
await tx.invoke()

await signer.send_transaction(account, account.contract_address, 'set_L1_address', [ANOTHER_ADDRESS])
assert await account.get_L1_address().call() == (ANOTHER_ADDRESS,)


@pytest.mark.asyncio
async def test_public_key_setter(account_factory):
_, account = account_factory
assert await account.get_public_key().call() == (signer.public_key,)

tx = await signer.build_transaction(
account, account.contract_address, 'set_public_key', [other.public_key])
await tx.invoke()

await signer.send_transaction(account, account.contract_address, 'set_public_key', [other.public_key])
assert await account.get_public_key().call() == (other.public_key,)

# tear down test. todo: cleanup on fixture directly
tx = await other.build_transaction(
account, account.contract_address, 'set_public_key', [signer.public_key])

await tx.invoke()
11 changes: 7 additions & 4 deletions test/utils/Signer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ def __init__(self, private_key):
def sign(self, message_hash):
return sign(msg_hash=message_hash, priv_key=self.private_key)

async def build_transaction(self, account, to, selector_name, calldata):
(nonce,) = await account.get_nonce().call()
async def send_transaction(self, account, to, selector_name, calldata, nonce=None):
if nonce is None:
nonce, = await account.get_nonce().call()

selector = get_selector_from_name(selector_name)
message_hash = hash_message(
to, selector, calldata, account.contract_address, nonce)
(sig_r, sig_s) = self.sign(message_hash)
return account.execute(to, selector, calldata, nonce, sig_r, sig_s)
sig_r, sig_s = self.sign(message_hash)

return await account.execute(to, selector, calldata, sig_r, sig_s).invoke()


def hash_message(to, selector, calldata, account_address, nonce):
Expand Down

0 comments on commit 2c2e1bb

Please sign in to comment.