Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add exception message to from #140

Merged
merged 3 commits into from
Dec 11, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions eth_tester/backends/pyevm/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@
FORK_BYZANTIUM,
)
from eth_tester.exceptions import (
BackendDistributionNotFound,
BlockNotFound,
TransactionNotFound,
TransactionFailed,
BackendDistributionNotFound,
TransactionNotFound,
ValidationError,
)

from eth_tester.utils.formatting import (
Expand Down Expand Up @@ -471,6 +472,8 @@ def _get_normalized_and_unsigned_evm_transaction(self, transaction, block_number
return evm_transaction

def _get_normalized_and_signed_evm_transaction(self, transaction, block_number='latest'):
if transaction['from'] not in self._key_lookup:
raise ValidationError('"from" key not available, does this account exist?')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This message seems like it could be improved a bit.

No "from" key was provided in the transaction which is required for transaction signing

A bit more concise and hints at how the user can fix it.

Copy link
Contributor Author

@jacqueswww jacqueswww Dec 10, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am changing this message to No valid "from" key was provided... the reason is that one can specify a public key, but the the key isn't available to use (this is how I found the error, I specified a public key of a contract) ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we wanted to get really fancy, we could list the valid keys and show the actual key, but that shouldn't hold up this PR.

signing_key = self._key_lookup[transaction['from']]
normalized_transaction = self._normalize_transaction(transaction, block_number)
evm_transaction = self.chain.create_unsigned_transaction(**normalized_transaction)
Expand Down
101 changes: 60 additions & 41 deletions tests/backends/test_pyevm.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,18 @@
import pytest
from eth_utils import to_wei

from eth_tester import (
EthereumTester,
PyEVMBackend,
)
from eth_tester import EthereumTester, PyEVMBackend
from eth_tester.backends.pyevm.main import (
generate_genesis_state_for_keys,
get_default_account_keys,
get_default_genesis_params
)
from eth_tester.backends.pyevm.utils import (
is_pyevm_available,
)
from eth_tester.utils.backend_testing import (
BaseTestBackendDirect,
get_default_genesis_params,
)
from eth_tester.backends.pyevm.utils import is_pyevm_available
from eth_tester.exceptions import ValidationError
from eth_tester.utils.backend_testing import BaseTestBackendDirect, SIMPLE_TRANSACTION


ZERO_ADDRESS_HEX = "0x0000000000000000000000000000000000000000"


@pytest.fixture
Expand All @@ -29,10 +26,9 @@ def eth_tester():


class TestPyEVMBackendDirect(BaseTestBackendDirect):

def test_generate_custom_genesis_state(self):
state_overrides = {'balance': to_wei(900000, 'ether')}
invalid_overrides = {'gato': 'con botas'}
state_overrides = {"balance": to_wei(900000, "ether")}
invalid_overrides = {"gato": "con botas"}

# Test creating a specific number of accounts
account_keys = get_default_account_keys(quantity=2)
Expand All @@ -41,88 +37,111 @@ def test_generate_custom_genesis_state(self):
assert len(account_keys) == 10

# Test the underlying state merging functionality
genesis_state = generate_genesis_state_for_keys(account_keys=account_keys, overrides=state_overrides)
genesis_state = generate_genesis_state_for_keys(
account_keys=account_keys, overrides=state_overrides
)
assert len(genesis_state) == len(account_keys) == 10
for _public_address, account_state in genesis_state.items():
assert account_state['balance'] == state_overrides['balance']
assert account_state['code'] == b''
assert account_state["balance"] == state_overrides["balance"]
assert account_state["code"] == b""

# Only existing default genesis state keys can be overridden
with pytest.raises(ValueError):
_invalid_genesis_state = generate_genesis_state_for_keys(account_keys=account_keys,
overrides=invalid_overrides)
_invalid_genesis_state = generate_genesis_state_for_keys(
account_keys=account_keys, overrides=invalid_overrides
)

# Use staticmethod state overriding
genesis_state = PyEVMBackend._generate_genesis_state(overrides=state_overrides, num_accounts=3)
genesis_state = PyEVMBackend._generate_genesis_state(
overrides=state_overrides, num_accounts=3
)
assert len(genesis_state) == 3
for _public_address, account_state in genesis_state.items():
assert account_state['balance'] == state_overrides['balance']
assert account_state['code'] == b''
assert account_state["balance"] == state_overrides["balance"]
assert account_state["code"] == b""
carver marked this conversation as resolved.
Show resolved Hide resolved

# Only existing default genesis state keys can be overridden
with pytest.raises(ValueError):
_invalid_genesis_state = PyEVMBackend._generate_genesis_state(overrides=invalid_overrides)
_invalid_genesis_state = PyEVMBackend._generate_genesis_state(
overrides=invalid_overrides
)

def test_override_genesis_state(self):
state_overrides = {'balance': to_wei(900000, 'ether')}
state_overrides = {"balance": to_wei(900000, "ether")}
test_accounts = 3

# Initialize PyEVM backend with custom genesis state
genesis_state = PyEVMBackend._generate_genesis_state(overrides=state_overrides,
num_accounts=test_accounts)
genesis_state = PyEVMBackend._generate_genesis_state(
overrides=state_overrides, num_accounts=test_accounts
)

# Test the correct number of accounts are created with the specified balance override
pyevm_backend = PyEVMBackend(genesis_state=genesis_state)
assert len(pyevm_backend.account_keys) == test_accounts
for private_key in pyevm_backend.account_keys:
account = private_key.public_key.to_canonical_address()
balance = pyevm_backend.get_balance(account=account)
assert balance == state_overrides['balance']
assert balance == state_overrides["balance"]

# Test integration with EthereumTester
tester = EthereumTester(backend=pyevm_backend)
for private_key in pyevm_backend.account_keys:
account = private_key.public_key.to_checksum_address()
balance = tester.get_balance(account=account)
assert balance == state_overrides['balance']
assert balance == state_overrides["balance"]

def test_generate_custom_genesis_parameters(self):

# Establish parameter overrides, for example a custom genesis gas limit
param_overrides = {'gas_limit': 4750000}
param_overrides = {"gas_limit": 4750000}

# Test the underlying default parameter merging functionality
genesis_params = get_default_genesis_params(overrides=param_overrides)
assert genesis_params['block_number'] == 0
assert genesis_params['gas_limit'] == param_overrides['gas_limit']
assert genesis_params["block_number"] == 0
assert genesis_params["gas_limit"] == param_overrides["gas_limit"]

# Use the the staticmethod to generate custom genesis parameters
genesis_params = PyEVMBackend._generate_genesis_params(param_overrides)
assert genesis_params['block_number'] == 0
assert genesis_params['gas_limit'] == param_overrides['gas_limit']
assert genesis_params["block_number"] == 0
assert genesis_params["gas_limit"] == param_overrides["gas_limit"]

# Only existing default genesis parameter keys can be overridden
invalid_overrides = {'gato': 'con botas'}
invalid_overrides = {"gato": "con botas"}
with pytest.raises(ValueError):
_invalid_genesis_params = PyEVMBackend._generate_genesis_params(overrides=invalid_overrides)
_invalid_genesis_params = PyEVMBackend._generate_genesis_params(
overrides=invalid_overrides
)

def test_override_genesis_parameters(self):

# Establish a custom gas limit
param_overrides = {'gas_limit': 4750000}
param_overrides = {"gas_limit": 4750000}
block_one_gas_limit = 4745362

# Initialize PyEVM backend with custom genesis parameters
genesis_params = PyEVMBackend._generate_genesis_params(overrides=param_overrides)
genesis_params = PyEVMBackend._generate_genesis_params(
overrides=param_overrides
)
pyevm_backend = PyEVMBackend(genesis_parameters=genesis_params)
genesis_block = pyevm_backend.get_block_by_number(0)
assert genesis_block['gas_limit'] == param_overrides['gas_limit']
assert genesis_block["gas_limit"] == param_overrides["gas_limit"]
genesis_block = pyevm_backend.get_block_by_number(1)
assert genesis_block['gas_limit'] == block_one_gas_limit
assert genesis_block["gas_limit"] == block_one_gas_limit

# Integrate with EthereumTester
tester = EthereumTester(backend=pyevm_backend)
genesis_block = tester.get_block_by_number(0)
assert genesis_block['gas_limit'] == param_overrides['gas_limit']
assert genesis_block["gas_limit"] == param_overrides["gas_limit"]
genesis_block = tester.get_block_by_number(1)
assert genesis_block['gas_limit'] == block_one_gas_limit
assert genesis_block["gas_limit"] == block_one_gas_limit

def test_send_transaction_invalid_from(self, eth_tester):
accounts = eth_tester.get_accounts()
assert accounts, "No accounts available for transaction sending"

with pytest.raises(ValidationError) as excinfo:
self._send_and_check_transaction(
eth_tester, SIMPLE_TRANSACTION, ZERO_ADDRESS_HEX
)

assert "does this account exist?" in str(excinfo.value)
carver marked this conversation as resolved.
Show resolved Hide resolved