From d0d2839ce0f681b89c7cc5dbb7b0ca784b96002f Mon Sep 17 00:00:00 2001 From: Andrew Fleming Date: Thu, 20 Oct 2022 16:28:47 -0400 Subject: [PATCH] import utils from nile (#450) --- tests/access/OwnableBaseSuite.py | 6 +- tests/access/test_AccessControl.py | 6 +- tests/access/test_Ownable.py | 3 +- tests/account/test_Account.py | 3 +- tests/account/test_EthAccount.py | 3 +- tests/introspection/test_ERC165.py | 4 +- tests/security/test_initializable.py | 3 +- tests/security/test_pausable.py | 4 +- tests/security/test_reentrancy.py | 5 +- tests/security/test_safemath.py | 6 +- tests/signers.py | 2 +- tests/token/erc20/ERC20BaseSuite.py | 8 +- tests/token/erc20/test_ERC20Burnable.py | 8 +- tests/token/erc20/test_ERC20Mintable.py | 7 +- tests/token/erc20/test_ERC20Pausable.py | 4 +- tests/token/erc20/test_ERC20Upgradeable.py | 8 +- tests/token/erc721/ERC721BaseSuite.py | 7 +- .../test_ERC721EnumerableMintableBurnable.py | 12 ++- .../erc721/test_ERC721MintableBurnable.py | 10 ++- .../erc721/test_ERC721MintablePausable.py | 8 +- .../erc721/test_ERC721SafeMintableMock.py | 7 +- tests/upgrades/test_Proxy.py | 7 +- tests/upgrades/test_upgrades.py | 6 +- tests/utils.py | 85 +------------------ 24 files changed, 75 insertions(+), 147 deletions(-) diff --git a/tests/access/OwnableBaseSuite.py b/tests/access/OwnableBaseSuite.py index 97c8668be..63b513dd4 100644 --- a/tests/access/OwnableBaseSuite.py +++ b/tests/access/OwnableBaseSuite.py @@ -1,9 +1,7 @@ import pytest from signers import MockSigner -from utils import ( - ZERO_ADDRESS, - assert_event_emitted, -) +from nile.utils import ZERO_ADDRESS +from utils import assert_event_emitted signer = MockSigner(123456789987654321) diff --git a/tests/access/test_AccessControl.py b/tests/access/test_AccessControl.py index 1a1b073b3..e1f81fc08 100644 --- a/tests/access/test_AccessControl.py +++ b/tests/access/test_AccessControl.py @@ -1,11 +1,9 @@ import pytest from pathlib import Path from signers import MockSigner +from nile.utils import TRUE, FALSE, assert_revert from utils import ( - TRUE, FALSE, - assert_event_emitted, assert_revert, - get_contract_class, cached_contract, - State, Account + State, Account, assert_event_emitted, get_contract_class, cached_contract ) DEFAULT_ADMIN_ROLE = 0 diff --git a/tests/access/test_Ownable.py b/tests/access/test_Ownable.py index 4239f0d3e..d9541faef 100644 --- a/tests/access/test_Ownable.py +++ b/tests/access/test_Ownable.py @@ -1,6 +1,7 @@ import pytest from signers import MockSigner -from utils import get_contract_class, assert_revert, cached_contract, State, Account +from nile.utils import assert_revert +from utils import State, Account, get_contract_class, cached_contract from OwnableBaseSuite import OwnableBase diff --git a/tests/account/test_Account.py b/tests/account/test_Account.py index 0c055fbfc..3fbbf7766 100644 --- a/tests/account/test_Account.py +++ b/tests/account/test_Account.py @@ -1,6 +1,7 @@ import pytest from signers import MockSigner, get_raw_invoke -from utils import assert_revert, get_contract_class, cached_contract, TRUE, State, Account +from nile.utils import TRUE, assert_revert +from utils import get_contract_class, cached_contract, State, Account signer = MockSigner(123456789987654321) diff --git a/tests/account/test_EthAccount.py b/tests/account/test_EthAccount.py index 819c58abc..2a198c7b9 100644 --- a/tests/account/test_EthAccount.py +++ b/tests/account/test_EthAccount.py @@ -1,5 +1,6 @@ import pytest -from utils import assert_revert, get_contract_class, cached_contract, TRUE, FALSE, State +from nile.utils import assert_revert, TRUE, FALSE +from utils import get_contract_class, cached_contract, State from signers import MockEthSigner, get_raw_invoke private_key = b'\x01' * 32 diff --git a/tests/introspection/test_ERC165.py b/tests/introspection/test_ERC165.py index 495a4d322..52a8962a8 100644 --- a/tests/introspection/test_ERC165.py +++ b/tests/introspection/test_ERC165.py @@ -1,10 +1,8 @@ import pytest +from nile.utils import assert_revert, TRUE, FALSE from utils import ( - assert_revert, get_contract_class, cached_contract, - TRUE, - FALSE, State ) diff --git a/tests/security/test_initializable.py b/tests/security/test_initializable.py index f1ff12b3c..602a0bf77 100644 --- a/tests/security/test_initializable.py +++ b/tests/security/test_initializable.py @@ -1,5 +1,6 @@ import pytest -from utils import TRUE, FALSE, assert_revert, get_contract_class, State +from nile.utils import TRUE, FALSE, assert_revert +from utils import get_contract_class, State @pytest.mark.asyncio diff --git a/tests/security/test_pausable.py b/tests/security/test_pausable.py index 3a963cbae..a7e391ff4 100644 --- a/tests/security/test_pausable.py +++ b/tests/security/test_pausable.py @@ -1,8 +1,8 @@ import pytest from signers import MockSigner +from nile.utils import TRUE, FALSE, assert_revert from utils import ( - TRUE, FALSE, assert_revert, assert_event_emitted, - get_contract_class, cached_contract, State, Account + get_contract_class, cached_contract, assert_event_emitted, State, Account ) diff --git a/tests/security/test_reentrancy.py b/tests/security/test_reentrancy.py index eda86c878..8c096fe9d 100644 --- a/tests/security/test_reentrancy.py +++ b/tests/security/test_reentrancy.py @@ -1,7 +1,6 @@ import pytest -from utils import ( - assert_revert, get_contract_class, State -) +from nile.utils import assert_revert +from utils import get_contract_class, State INITIAL_COUNTER = 0 diff --git a/tests/security/test_safemath.py b/tests/security/test_safemath.py index 20da705a2..f551eb47f 100644 --- a/tests/security/test_safemath.py +++ b/tests/security/test_safemath.py @@ -1,9 +1,9 @@ import pytest -from utils import ( +from nile.utils import ( MAX_UINT256, assert_revert, add_uint, sub_uint, - mul_uint, div_rem_uint, to_uint, - get_contract_class, State + mul_uint, div_rem_uint, to_uint ) +from utils import get_contract_class, State @pytest.fixture(scope='module') diff --git a/tests/signers.py b/tests/signers.py index f202ce7dd..2c35cd20d 100644 --- a/tests/signers.py +++ b/tests/signers.py @@ -2,7 +2,7 @@ from starkware.starknet.services.api.gateway.transaction import InvokeFunction from starkware.starknet.business_logic.transaction.objects import InternalTransaction, TransactionExecutionInfo from nile.signer import Signer, from_call_to_call_array, get_transaction_hash, TRANSACTION_VERSION -from utils import to_uint +from nile.utils import to_uint import eth_keys diff --git a/tests/token/erc20/ERC20BaseSuite.py b/tests/token/erc20/ERC20BaseSuite.py index eccf0f189..9bfc1e0a8 100644 --- a/tests/token/erc20/ERC20BaseSuite.py +++ b/tests/token/erc20/ERC20BaseSuite.py @@ -1,9 +1,11 @@ import pytest from signers import MockSigner -from utils import ( +from nile.utils import ( to_uint, add_uint, sub_uint, str_to_felt, MAX_UINT256, ZERO_ADDRESS, - INVALID_UINT256, TRUE, assert_revert, assert_event_emitted, assert_events_emitted, - contract_path, State + INVALID_UINT256, TRUE, assert_revert +) +from utils import ( + assert_event_emitted, assert_events_emitted, contract_path, State ) diff --git a/tests/token/erc20/test_ERC20Burnable.py b/tests/token/erc20/test_ERC20Burnable.py index 80c7d537e..a15fba3a6 100644 --- a/tests/token/erc20/test_ERC20Burnable.py +++ b/tests/token/erc20/test_ERC20Burnable.py @@ -1,9 +1,11 @@ import pytest from signers import MockSigner +from nile.utils import ( + add_uint, sub_uint, ZERO_ADDRESS, INVALID_UINT256, assert_revert, +) from utils import ( - add_uint, sub_uint, ZERO_ADDRESS, INVALID_UINT256, - get_contract_class, cached_contract, assert_revert, assert_event_emitted, - assert_events_emitted, State, Account + get_contract_class, cached_contract, assert_events_emitted, + assert_event_emitted, State, Account ) from ERC20BaseSuite import ( ERC20Base, NAME, SYMBOL, DECIMALS, INIT_SUPPLY, AMOUNT, UINT_ONE, UINT_ZERO diff --git a/tests/token/erc20/test_ERC20Mintable.py b/tests/token/erc20/test_ERC20Mintable.py index a1b80ee5c..efdd5c49d 100644 --- a/tests/token/erc20/test_ERC20Mintable.py +++ b/tests/token/erc20/test_ERC20Mintable.py @@ -1,9 +1,10 @@ import pytest from signers import MockSigner +from nile.utils import ( + add_uint, sub_uint, MAX_UINT256, ZERO_ADDRESS, INVALID_UINT256, assert_revert +) from utils import ( - add_uint, sub_uint, MAX_UINT256, ZERO_ADDRESS, INVALID_UINT256, - get_contract_class, cached_contract, assert_revert, assert_event_emitted, - State, Account + get_contract_class, cached_contract, assert_event_emitted, State, Account ) from ERC20BaseSuite import ERC20Base, NAME, SYMBOL, DECIMALS, INIT_SUPPLY, UINT_ONE from access.OwnableBaseSuite import OwnableBase diff --git a/tests/token/erc20/test_ERC20Pausable.py b/tests/token/erc20/test_ERC20Pausable.py index 54431c06b..6ee597997 100644 --- a/tests/token/erc20/test_ERC20Pausable.py +++ b/tests/token/erc20/test_ERC20Pausable.py @@ -1,8 +1,8 @@ import pytest from signers import MockSigner +from nile.utils import TRUE, FALSE, assert_revert from utils import ( - TRUE, FALSE, assert_revert, get_contract_class, - cached_contract, State, Account + get_contract_class, cached_contract, State, Account ) from ERC20BaseSuite import ERC20Base, NAME, SYMBOL, DECIMALS, INIT_SUPPLY, AMOUNT from access.OwnableBaseSuite import OwnableBase diff --git a/tests/token/erc20/test_ERC20Upgradeable.py b/tests/token/erc20/test_ERC20Upgradeable.py index 022209668..cf3713640 100644 --- a/tests/token/erc20/test_ERC20Upgradeable.py +++ b/tests/token/erc20/test_ERC20Upgradeable.py @@ -1,10 +1,8 @@ import pytest +from starkware.starknet.public.abi import get_selector_from_name from signers import MockSigner -from utils import ( - to_uint, sub_uint, str_to_felt, assert_revert, TRUE, - get_contract_class, cached_contract, State, Account, - get_selector_from_name -) +from nile.utils import to_uint, sub_uint, str_to_felt, assert_revert, TRUE +from utils import get_contract_class, cached_contract, State, Account signer = MockSigner(123456789987654321) diff --git a/tests/token/erc721/ERC721BaseSuite.py b/tests/token/erc721/ERC721BaseSuite.py index 6fba11a59..abd8abe4d 100644 --- a/tests/token/erc721/ERC721BaseSuite.py +++ b/tests/token/erc721/ERC721BaseSuite.py @@ -1,8 +1,11 @@ import pytest from signers import MockSigner -from utils import ( +from nile.utils import ( str_to_felt, ZERO_ADDRESS, TRUE, FALSE, assert_revert, INVALID_UINT256, - assert_event_emitted, assert_events_emitted, to_uint, sub_uint, add_uint, + to_uint, sub_uint, add_uint +) +from utils import ( + assert_event_emitted, assert_events_emitted, ) diff --git a/tests/token/erc721/test_ERC721EnumerableMintableBurnable.py b/tests/token/erc721/test_ERC721EnumerableMintableBurnable.py index 5704d7d41..5ee789508 100644 --- a/tests/token/erc721/test_ERC721EnumerableMintableBurnable.py +++ b/tests/token/erc721/test_ERC721EnumerableMintableBurnable.py @@ -1,11 +1,15 @@ import pytest from signers import MockSigner +from nile.utils import ( + MAX_UINT256, ZERO_ADDRESS, TRUE, assert_revert, to_uint, sub_uint, add_uint +) from utils import ( - MAX_UINT256, get_contract_class, cached_contract, TRUE, - assert_revert, assert_event_emitted, assert_events_emitted, to_uint, - sub_uint, add_uint, ZERO_ADDRESS, State, Account + get_contract_class, cached_contract, assert_event_emitted, + assert_events_emitted, State, Account +) +from ERC721BaseSuite import ( + ERC721Base, NAME, SYMBOL, NONEXISTENT_TOKEN, DATA, RECIPIENT ) -from ERC721BaseSuite import ERC721Base, NAME, SYMBOL, NONEXISTENT_TOKEN, DATA, RECIPIENT from access.OwnableBaseSuite import OwnableBase diff --git a/tests/token/erc721/test_ERC721MintableBurnable.py b/tests/token/erc721/test_ERC721MintableBurnable.py index fdedc0411..c43545910 100644 --- a/tests/token/erc721/test_ERC721MintableBurnable.py +++ b/tests/token/erc721/test_ERC721MintableBurnable.py @@ -1,11 +1,13 @@ import pytest from signers import MockSigner +from nile.utils import ZERO_ADDRESS, assert_revert, to_uint, sub_uint from utils import ( - ZERO_ADDRESS, assert_revert, assert_event_emitted, - assert_events_emitted, get_contract_class, cached_contract, to_uint, - sub_uint, State, Account + get_contract_class, cached_contract, assert_event_emitted, + assert_events_emitted, State, Account +) +from ERC721BaseSuite import ( + ERC721Base, NAME, SYMBOL, NONEXISTENT_TOKEN, TOKENS, TOKEN ) -from ERC721BaseSuite import ERC721Base, NAME, SYMBOL, NONEXISTENT_TOKEN, TOKENS, TOKEN from access.OwnableBaseSuite import OwnableBase diff --git a/tests/token/erc721/test_ERC721MintablePausable.py b/tests/token/erc721/test_ERC721MintablePausable.py index 13e69ef59..37c7af929 100644 --- a/tests/token/erc721/test_ERC721MintablePausable.py +++ b/tests/token/erc721/test_ERC721MintablePausable.py @@ -1,10 +1,12 @@ import pytest from signers import MockSigner +from nile.utils import TRUE, FALSE, ZERO_ADDRESS, assert_revert, to_uint from utils import ( - str_to_felt, TRUE, FALSE, get_contract_class, cached_contract, - assert_revert, assert_event_emitted, to_uint, ZERO_ADDRESS, State, Account + get_contract_class, cached_contract, assert_event_emitted, State, Account +) +from ERC721BaseSuite import ( + ERC721Base, NAME, SYMBOL, TOKENS, TOKEN, NONEXISTENT_TOKEN, DATA ) -from ERC721BaseSuite import ERC721Base, NAME, SYMBOL, TOKENS, TOKEN, NONEXISTENT_TOKEN, DATA from access.OwnableBaseSuite import OwnableBase diff --git a/tests/token/erc721/test_ERC721SafeMintableMock.py b/tests/token/erc721/test_ERC721SafeMintableMock.py index c753863f1..6b9881d1b 100644 --- a/tests/token/erc721/test_ERC721SafeMintableMock.py +++ b/tests/token/erc721/test_ERC721SafeMintableMock.py @@ -1,9 +1,10 @@ import pytest from signers import MockSigner +from nile.utils import ( + ZERO_ADDRESS, INVALID_UINT256, assert_revert, to_uint +) from utils import ( - ZERO_ADDRESS, INVALID_UINT256, assert_revert, - assert_event_emitted, get_contract_class, cached_contract, to_uint, - State, Account + get_contract_class, cached_contract, assert_event_emitted, State, Account ) from ERC721BaseSuite import ERC721Base, NAME, SYMBOL, DATA, TOKEN, TOKENS from access.OwnableBaseSuite import OwnableBase diff --git a/tests/upgrades/test_Proxy.py b/tests/upgrades/test_Proxy.py index a6854ed1b..911bd818b 100644 --- a/tests/upgrades/test_Proxy.py +++ b/tests/upgrades/test_Proxy.py @@ -1,12 +1,13 @@ import pytest +from starkware.starknet.public.abi import get_selector_from_name from signers import MockSigner +from nile.utils import ( + assert_revert, assert_revert_entry_point +) from utils import ( - assert_revert, get_contract_class, cached_contract, assert_event_emitted, - assert_revert_entry_point, - get_selector_from_name, State, Account ) diff --git a/tests/upgrades/test_upgrades.py b/tests/upgrades/test_upgrades.py index 80f0090f3..75e9c7b20 100644 --- a/tests/upgrades/test_upgrades.py +++ b/tests/upgrades/test_upgrades.py @@ -1,15 +1,13 @@ import pytest from signers import MockSigner +from starkware.starknet.public.abi import get_selector_from_name +from nile.utils import assert_revert, assert_revert_entry_point, FALSE, TRUE from utils import ( State, Account, - assert_revert, - assert_revert_entry_point, assert_event_emitted, get_contract_class, cached_contract, - get_selector_from_name, - FALSE, TRUE ) diff --git a/tests/utils.py b/tests/utils.py index 26fb77654..4a98b2d89 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -1,21 +1,12 @@ """Utilities for testing Cairo contracts.""" from pathlib import Path -import math import os -from starkware.starknet.public.abi import get_selector_from_name from starkware.starknet.compiler.compile import compile_starknet_files -from starkware.starkware_utils.error_handling import StarkException from starkware.starknet.testing.starknet import StarknetContract from starkware.starknet.testing.starknet import Starknet from starkware.starknet.business_logic.execution.objects import OrderedEvent - - -MAX_UINT256 = (2**128 - 1, 2**128 - 1) -INVALID_UINT256 = (MAX_UINT256[0] + 1, MAX_UINT256[1]) -ZERO_ADDRESS = 0 -TRUE = 1 -FALSE = 0 +from starkware.starknet.public.abi import get_selector_from_name _root = Path(__file__).parent.parent @@ -28,80 +19,6 @@ def contract_path(name): return str(_root / "src" / name) -def str_to_felt(text): - b_text = bytes(text, "ascii") - return int.from_bytes(b_text, "big") - - -def felt_to_str(felt): - b_felt = felt.to_bytes(31, "big") - return b_felt.decode() - - -def uint(a): - return(a, 0) - - -def to_uint(a): - """Takes in value, returns uint256-ish tuple.""" - return (a & ((1 << 128) - 1), a >> 128) - - -def from_uint(uint): - """Takes in uint256-ish tuple, returns value.""" - return uint[0] + (uint[1] << 128) - - -def add_uint(a, b): - """Returns the sum of two uint256-ish tuples.""" - a = from_uint(a) - b = from_uint(b) - c = a + b - return to_uint(c) - - -def sub_uint(a, b): - """Returns the difference of two uint256-ish tuples.""" - a = from_uint(a) - b = from_uint(b) - c = a - b - return to_uint(c) - - -def mul_uint(a, b): - """Returns the product of two uint256-ish tuples.""" - a = from_uint(a) - b = from_uint(b) - c = a * b - return to_uint(c) - - -def div_rem_uint(a, b): - """Returns the quotient and remainder of two uint256-ish tuples.""" - a = from_uint(a) - b = from_uint(b) - c = math.trunc(a / b) - m = a % b - return (to_uint(c), to_uint(m)) - - -async def assert_revert(fun, reverted_with=None): - try: - await fun - assert False - except StarkException as err: - _, error = err.args - if reverted_with is not None: - assert reverted_with in error['message'] - - -async def assert_revert_entry_point(fun, invalid_selector): - selector_hex = hex(get_selector_from_name(invalid_selector)) - entry_point_msg = f"Entry point {selector_hex} not found in contract" - - await assert_revert(fun, entry_point_msg) - - def assert_event_emitted(tx_exec_info, from_address, name, data, order=0): """Assert one single event is fired with correct data.""" assert_events_emitted(tx_exec_info, [(order, from_address, name, data)])