From 39fdd9f16d82e5a3e4cafb3e4525a7f608e347d7 Mon Sep 17 00:00:00 2001 From: Grady Laksmono Date: Tue, 20 Nov 2018 08:40:01 +0700 Subject: [PATCH 1/5] Update the contract limit size to follow EIP170. Add tests against the size limit. --- eth/vm/forks/spurious_dragon/constants.py | 2 +- .../vm/test_spurious_dragon_computation.py | 85 +++++++++++++++++++ 2 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 tests/core/vm/test_spurious_dragon_computation.py diff --git a/eth/vm/forks/spurious_dragon/constants.py b/eth/vm/forks/spurious_dragon/constants.py index 304d4c88cf..f97e76374b 100644 --- a/eth/vm/forks/spurious_dragon/constants.py +++ b/eth/vm/forks/spurious_dragon/constants.py @@ -10,4 +10,4 @@ # https://github.com/ethereum/EIPs/issues/170 -EIP170_CODE_SIZE_LIMIT = 24577 +EIP170_CODE_SIZE_LIMIT = 24576 diff --git a/tests/core/vm/test_spurious_dragon_computation.py b/tests/core/vm/test_spurious_dragon_computation.py new file mode 100644 index 0000000000..9e3f195747 --- /dev/null +++ b/tests/core/vm/test_spurious_dragon_computation.py @@ -0,0 +1,85 @@ +import pytest + +from eth_utils import ( + to_canonical_address, +) + +from eth.vm.message import ( + Message, +) + +from eth.vm.forks.spurious_dragon.computation import ( + SpuriousDragonComputation, +) + +from eth.vm.transaction_context import ( + BaseTransactionContext, +) + + +NORMALIZED_ADDRESS_A = "0x0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6" +NORMALIZED_ADDRESS_B = "0xcd1722f3947def4cf144679da39c4c32bdc35681" +CANONICAL_ADDRESS_A = to_canonical_address("0x0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6") +CANONICAL_ADDRESS_B = to_canonical_address("0xcd1722f3947def4cf144679da39c4c32bdc35681") +CONTRACT_CODE_A = b"" +CONTRACT_CODE_B = b"" +CONTRACT_CODE_C = b"" + + +@pytest.fixture +def state(chain_without_block_validation): + state = chain_without_block_validation.get_vm().state + state.account_db.set_balance(CANONICAL_ADDRESS_A, 1000) + return state + + +@pytest.fixture +def transaction_context(): + tx_context = BaseTransactionContext( + gas_price=1, + origin=CANONICAL_ADDRESS_B, + ) + return tx_context + + +def test_code_size_limit(transaction_context, state): + """ + CONTRACT_CODE_A size is greater than EIP170_CODE_SIZE_LIMIT + """ + message_contract_code_a = Message( + to=CANONICAL_ADDRESS_A, + sender=CANONICAL_ADDRESS_B, + value=100, + data=b'', + code=CONTRACT_CODE_A, + gas=100, + ) + computation = SpuriousDragonComputation( + state=state, + message=message_contract_code_a, + transaction_context=transaction_context, + ) + + """ + TODO: CONTRACT_CODE_B size is equal to EIP170_CODE_SIZE_LIMIT + """ + message_contract_code_b = Message( + to=CANONICAL_ADDRESS_A, + sender=CANONICAL_ADDRESS_B, + value=100, + data=b'', + code=CONTRACT_CODE_B, + gas=100, + ) + + """ + TODO: CONTRACT_CODE_C size is lower than EIP170_CODE_SIZE_LIMIT + """ + message_contract_code_c = Message( + to=CANONICAL_ADDRESS_A, + sender=CANONICAL_ADDRESS_B, + value=100, + data=b'', + code=CONTRACT_CODE_C, + gas=100, + ) From 43f379bfcd70c730de20be61c2d54609567681d3 Mon Sep 17 00:00:00 2001 From: "german@hopu.eu" Date: Thu, 18 Jul 2019 21:44:07 +0200 Subject: [PATCH 2/5] Added test for computation.output EIP170_CODE_SIZE_LIMIT --- .../vm/test_spurious_dragon_computation.py | 117 +++++++++--------- 1 file changed, 56 insertions(+), 61 deletions(-) diff --git a/tests/core/vm/test_spurious_dragon_computation.py b/tests/core/vm/test_spurious_dragon_computation.py index 9e3f195747..7ede6bb9fe 100644 --- a/tests/core/vm/test_spurious_dragon_computation.py +++ b/tests/core/vm/test_spurious_dragon_computation.py @@ -1,85 +1,80 @@ -import pytest +from unittest import mock +import pytest from eth_utils import ( to_canonical_address, ) -from eth.vm.message import ( - Message, +from eth import ( + Chain, +) +from eth.vm.computation import ( + BaseComputation, ) - from eth.vm.forks.spurious_dragon.computation import ( SpuriousDragonComputation, ) - +from eth.vm.forks.spurious_dragon.constants import ( + EIP170_CODE_SIZE_LIMIT +) +from eth.vm.message import ( + Message, +) from eth.vm.transaction_context import ( BaseTransactionContext, ) - NORMALIZED_ADDRESS_A = "0x0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6" NORMALIZED_ADDRESS_B = "0xcd1722f3947def4cf144679da39c4c32bdc35681" -CANONICAL_ADDRESS_A = to_canonical_address("0x0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6") -CANONICAL_ADDRESS_B = to_canonical_address("0xcd1722f3947def4cf144679da39c4c32bdc35681") -CONTRACT_CODE_A = b"" -CONTRACT_CODE_B = b"" -CONTRACT_CODE_C = b"" +CANONICAL_ADDRESS_A = to_canonical_address(NORMALIZED_ADDRESS_A) +CANONICAL_ADDRESS_B = to_canonical_address(NORMALIZED_ADDRESS_B) @pytest.fixture -def state(chain_without_block_validation): - state = chain_without_block_validation.get_vm().state - state.account_db.set_balance(CANONICAL_ADDRESS_A, 1000) - return state +def make_computation(): + message = Message( + to=CANONICAL_ADDRESS_B, + sender=CANONICAL_ADDRESS_A, + value=1, + data=b'', + code=b'', + gas=5000000, + ) + transaction_context = BaseTransactionContext(gas_price=1, origin=CANONICAL_ADDRESS_B, ) + def _make_computation(chain) -> BaseComputation: + state = chain.get_vm().state + state.account_db.set_balance(CANONICAL_ADDRESS_A, 1000) + computation = SpuriousDragonComputation( + state=state, + message=message, + transaction_context=transaction_context, + ) -@pytest.fixture -def transaction_context(): - tx_context = BaseTransactionContext( - gas_price=1, - origin=CANONICAL_ADDRESS_B, - ) - return tx_context + return computation + return _make_computation -def test_code_size_limit(transaction_context, state): - """ - CONTRACT_CODE_A size is greater than EIP170_CODE_SIZE_LIMIT - """ - message_contract_code_a = Message( - to=CANONICAL_ADDRESS_A, - sender=CANONICAL_ADDRESS_B, - value=100, - data=b'', - code=CONTRACT_CODE_A, - gas=100, - ) - computation = SpuriousDragonComputation( - state=state, - message=message_contract_code_a, - transaction_context=transaction_context, - ) - """ - TODO: CONTRACT_CODE_B size is equal to EIP170_CODE_SIZE_LIMIT - """ - message_contract_code_b = Message( - to=CANONICAL_ADDRESS_A, - sender=CANONICAL_ADDRESS_B, - value=100, - data=b'', - code=CONTRACT_CODE_B, - gas=100, - ) +@pytest.mark.parametrize('chain_without_block_validation', [Chain, ], indirect=True) +@pytest.mark.parametrize( + 'computation_output', + [ + b'\x00' * (EIP170_CODE_SIZE_LIMIT + 1), + b'\x00' * EIP170_CODE_SIZE_LIMIT, + b'\x00' * (EIP170_CODE_SIZE_LIMIT - 1), + ] +) +def test_computation_output_size_limit(chain_without_block_validation, + make_computation, + computation_output): + computation: BaseComputation = make_computation(chain_without_block_validation) + computation.output = computation_output - """ - TODO: CONTRACT_CODE_C size is lower than EIP170_CODE_SIZE_LIMIT - """ - message_contract_code_c = Message( - to=CANONICAL_ADDRESS_A, - sender=CANONICAL_ADDRESS_B, - value=100, - data=b'', - code=CONTRACT_CODE_C, - gas=100, - ) + with mock.patch.object(SpuriousDragonComputation, 'apply_message', return_value=computation): + computation.apply_create_message() + + if len(computation_output) >= EIP170_CODE_SIZE_LIMIT: + assert computation.is_error + else: + assert not computation.is_error From b9cdb6f835a489a1ad5605e26042f7e172c213f6 Mon Sep 17 00:00:00 2001 From: "german@hopu.eu" Date: Thu, 18 Jul 2019 22:16:16 +0200 Subject: [PATCH 3/5] Adding comment suggestions --- eth/vm/forks/spurious_dragon/constants.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eth/vm/forks/spurious_dragon/constants.py b/eth/vm/forks/spurious_dragon/constants.py index f97e76374b..e14555d66f 100644 --- a/eth/vm/forks/spurious_dragon/constants.py +++ b/eth/vm/forks/spurious_dragon/constants.py @@ -10,4 +10,4 @@ # https://github.com/ethereum/EIPs/issues/170 -EIP170_CODE_SIZE_LIMIT = 24576 +EIP170_CODE_SIZE_LIMIT = 24576 # 2**14 + 2**13 From 92c142fa4407bac810f3d370a52a192d149a30c3 Mon Sep 17 00:00:00 2001 From: "german@hopu.eu" Date: Thu, 18 Jul 2019 22:59:57 +0200 Subject: [PATCH 4/5] Updated state.set_balance --- tests/core/vm/test_spurious_dragon_computation.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/core/vm/test_spurious_dragon_computation.py b/tests/core/vm/test_spurious_dragon_computation.py index 7ede6bb9fe..e5a9c4721d 100644 --- a/tests/core/vm/test_spurious_dragon_computation.py +++ b/tests/core/vm/test_spurious_dragon_computation.py @@ -44,7 +44,8 @@ def make_computation(): def _make_computation(chain) -> BaseComputation: state = chain.get_vm().state - state.account_db.set_balance(CANONICAL_ADDRESS_A, 1000) + print("State: {}".format(state)) + state.set_balance(CANONICAL_ADDRESS_A, 1000) computation = SpuriousDragonComputation( state=state, message=message, From 435058d2f2867844ed3eddea59c59feb44a3673f Mon Sep 17 00:00:00 2001 From: "german@hopu.eu" Date: Thu, 18 Jul 2019 23:05:50 +0200 Subject: [PATCH 5/5] Cleaning print flags --- tests/core/vm/test_spurious_dragon_computation.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/core/vm/test_spurious_dragon_computation.py b/tests/core/vm/test_spurious_dragon_computation.py index e5a9c4721d..c1bd2e2833 100644 --- a/tests/core/vm/test_spurious_dragon_computation.py +++ b/tests/core/vm/test_spurious_dragon_computation.py @@ -44,7 +44,6 @@ def make_computation(): def _make_computation(chain) -> BaseComputation: state = chain.get_vm().state - print("State: {}".format(state)) state.set_balance(CANONICAL_ADDRESS_A, 1000) computation = SpuriousDragonComputation( state=state,