From d983337a446b7e976be0284e01bac33ffcc1ab74 Mon Sep 17 00:00:00 2001 From: Danny Ryan Date: Mon, 16 Apr 2018 20:45:59 +0900 Subject: [PATCH 1/4] epoch length test --- tests/test_chain_initialization.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/test_chain_initialization.py b/tests/test_chain_initialization.py index 95178b9..666e290 100644 --- a/tests/test_chain_initialization.py +++ b/tests/test_chain_initialization.py @@ -43,3 +43,15 @@ def test_init_first_epoch(casper, new_epoch): assert casper.dynasty() == 0 assert casper.next_validator_index() == 1 assert casper.current_epoch() == 1 + + +@pytest.mark.parametrize( + 'epoch_length, success', + [ + (10, True), + (250, True), + (256, False), + (500, False), + ] +) +def test_epoch_length(epoch_length, success, casper_config) From 7b24604db76f785d51907b62df69f67b065a4471 Mon Sep 17 00:00:00 2001 From: Danny Ryan Date: Thu, 26 Apr 2018 22:08:42 +0900 Subject: [PATCH 2/4] [bugfix/epoch-length] bound epoch_length at contract init --- casper/contracts/simple_casper.v.py | 2 + tests/conftest.py | 63 +++++++++++++++++++++++------ tests/test_chain_initialization.py | 24 ++++++++++- 3 files changed, 75 insertions(+), 14 deletions(-) diff --git a/casper/contracts/simple_casper.v.py b/casper/contracts/simple_casper.v.py index 2fc5da6..551198f 100644 --- a/casper/contracts/simple_casper.v.py +++ b/casper/contracts/simple_casper.v.py @@ -125,6 +125,8 @@ def __init__( base_interest_factor: decimal, base_penalty_factor: decimal, min_deposit_size: wei_value): + assert epoch_length > 0 and epoch_length < 256 + self.EPOCH_LENGTH = epoch_length self.WITHDRAWAL_DELAY = withdrawal_delay self.DYNASTY_LOGOUT_DELAY = dynasty_logout_delay diff --git a/tests/conftest.py b/tests/conftest.py index fc31c73..d911ac8 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -27,16 +27,6 @@ BASE_PENALTY_FACTOR = 0.002 MIN_DEPOSIT_SIZE = 1000 * 10**18 # 1000 ether -CASPER_CONFIG = { - "epoch_length": EPOCH_LENGTH, # in blocks - "withdrawal_delay": WITHDRAWAL_DELAY, # in epochs - "dynasty_logout_delay": DYNASTY_LOGOUT_DELAY, # in dynasties - "owner": OWNER, # Backdoor address - "base_interest_factor": BASE_INTEREST_FACTOR, - "base_penalty_factor": BASE_PENALTY_FACTOR, - "min_deposit_size": MIN_DEPOSIT_SIZE -} - FUNDED_PRIVKEYS = [tester.k1, tester.k2, tester.k3, tester.k4, tester.k5] DEPOSIT_AMOUNTS = [ 2000 * 10**18, @@ -105,8 +95,52 @@ def purity_checker_ct(): @pytest.fixture -def casper_config(): - return CASPER_CONFIG +def epoch_length(): + return EPOCH_LENGTH + + +@pytest.fixture +def withdrawal_delay(): + return WITHDRAWAL_DELAY + + +@pytest.fixture +def dynasty_logout_delay(): + return DYNASTY_LOGOUT_DELAY + + +@pytest.fixture +def owner(): + return OWNER + + +@pytest.fixture +def base_interest_factor(): + return BASE_INTEREST_FACTOR + + +@pytest.fixture +def base_penalty_factor(): + return BASE_PENALTY_FACTOR + + +@pytest.fixture +def min_deposit_size(): + return MIN_DEPOSIT_SIZE + + +@pytest.fixture +def casper_config(epoch_length, withdrawal_delay, dynasty_logout_delay, + owner, base_interest_factor, base_penalty_factor, min_deposit_size): + return { + "epoch_length": epoch_length, # in blocks + "withdrawal_delay": withdrawal_delay, # in epochs + "dynasty_logout_delay": dynasty_logout_delay, # in dynasties + "owner": owner, # Backdoor address + "base_interest_factor": base_interest_factor, + "base_penalty_factor": base_penalty_factor, + "min_deposit_size": min_deposit_size + } @pytest.fixture @@ -205,6 +239,9 @@ def casper_chain( nonce += 1 for tx in init_transactions: + print("tx") + print(tx.nonce) + print(tx.sender) if test_chain.head_state.gas_used + tx.startgas > test_chain.head_state.gas_limit: test_chain.mine(1) test_chain.direct_tx(tx) @@ -405,7 +442,7 @@ def induct_validators(privkeys, values): @pytest.fixture -def assert_failed(casper_chain): +def assert_failed(): def assert_failed(function_to_test, exception): with pytest.raises(exception): function_to_test() diff --git a/tests/test_chain_initialization.py b/tests/test_chain_initialization.py index 666e290..5e4b6fc 100644 --- a/tests/test_chain_initialization.py +++ b/tests/test_chain_initialization.py @@ -1,4 +1,8 @@ +import pytest + from ethereum import utils +from ethereum.tools.tester import TransactionFailed +from conftest import casper_chain def test_rlp_decoding_is_pure( @@ -48,10 +52,28 @@ def test_init_first_epoch(casper, new_epoch): @pytest.mark.parametrize( 'epoch_length, success', [ + (-1, False), + (0, False), (10, True), (250, True), (256, False), (500, False), ] ) -def test_epoch_length(epoch_length, success, casper_config) +def test_epoch_length(epoch_length, success, assert_failed, + test_chain, casper_args, casper_code, casper_ct, + dependency_transactions, sig_hasher_address, purity_checker_address, + base_sender_privkey): + def deploy_casper_contract(): + casper_chain( + test_chain, casper_args, casper_code, casper_ct, + dependency_transactions, sig_hasher_address, purity_checker_address, + base_sender_privkey + ) + + # Note: cannot use assert_tx_failed because requires casper_chain + if not success: + assert_failed(deploy_casper_contract, TransactionFailed) + return + + deploy_casper_contract() From 160fff1565f5a4315209af1c66dc45f727e9d39a Mon Sep 17 00:00:00 2001 From: Danny Ryan Date: Thu, 26 Apr 2018 22:20:35 +0900 Subject: [PATCH 3/4] [epoch-length] more generic fixture for deploying casper contract with updated args --- tests/conftest.py | 18 ++++++++++++++++++ tests/test_chain_initialization.py | 20 +++++++------------- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index d911ac8..b1d4b68 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -281,6 +281,24 @@ def casper_chain( return test_chain +@pytest.fixture +def deploy_casper_contract( + test_chain, + casper_code, + casper_ct, + dependency_transactions, + sig_hasher_address, + purity_checker_address, + base_sender_privkey): + def deploy_casper_contract(contract_args): + casper_chain( + test_chain, contract_args, casper_code, casper_ct, + dependency_transactions, sig_hasher_address, purity_checker_address, + base_sender_privkey + ) + return deploy_casper_contract + + def get_dirs(path): abs_contract_path = os.path.realpath(os.path.join(OWN_DIR, '..', 'casper', 'contracts')) sub_dirs = [x[0] for x in os.walk(abs_contract_path)] diff --git a/tests/test_chain_initialization.py b/tests/test_chain_initialization.py index 5e4b6fc..d292c41 100644 --- a/tests/test_chain_initialization.py +++ b/tests/test_chain_initialization.py @@ -60,20 +60,14 @@ def test_init_first_epoch(casper, new_epoch): (500, False), ] ) -def test_epoch_length(epoch_length, success, assert_failed, - test_chain, casper_args, casper_code, casper_ct, - dependency_transactions, sig_hasher_address, purity_checker_address, - base_sender_privkey): - def deploy_casper_contract(): - casper_chain( - test_chain, casper_args, casper_code, casper_ct, - dependency_transactions, sig_hasher_address, purity_checker_address, - base_sender_privkey - ) - +def test_epoch_length(epoch_length, success, casper_args, + deploy_casper_contract, assert_failed): # Note: cannot use assert_tx_failed because requires casper_chain if not success: - assert_failed(deploy_casper_contract, TransactionFailed) + assert_failed( + lambda: deploy_casper_contract(casper_args), + TransactionFailed + ) return - deploy_casper_contract() + deploy_casper_contract(casper_args) From ba62f54906837aad5291d4c67a319a7554f43e56 Mon Sep 17 00:00:00 2001 From: Danny Ryan Date: Thu, 26 Apr 2018 22:22:50 +0900 Subject: [PATCH 4/4] [bugfix/epoch-length] remove print debugs --- tests/conftest.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index b1d4b68..d5212ca 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -239,9 +239,6 @@ def casper_chain( nonce += 1 for tx in init_transactions: - print("tx") - print(tx.nonce) - print(tx.sender) if test_chain.head_state.gas_used + tx.startgas > test_chain.head_state.gas_limit: test_chain.mine(1) test_chain.direct_tx(tx)