Skip to content

Commit

Permalink
First tests with pyrevm
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielSchiavini committed Mar 8, 2024
1 parent 45c6b6a commit 5a70826
Show file tree
Hide file tree
Showing 9 changed files with 582 additions and 0 deletions.
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"lark==1.1.2",
"hypothesis[lark]>=5.37.1,<6.0",
"eth-stdlib==0.2.6",
"pyrevm==0.2.1",
],
"lint": [
"black==23.12.0",
Expand Down
14 changes: 14 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from web3.contract import Contract
from web3.providers.eth_tester import EthereumTesterProvider

from tests.revm.revm_env import RevmEnv
from tests.utils import working_directory
from vyper import compiler
from vyper.ast.grammar import parse_vyper_source
Expand Down Expand Up @@ -157,6 +158,11 @@ def tester():
return EthereumTester(backend=backend)


@pytest.fixture(scope="module")
def revm_env():
return RevmEnv()


def zero_gas_price_strategy(web3, transaction_params=None):
return 0 # zero gas price makes testing simpler.

Expand Down Expand Up @@ -340,6 +346,14 @@ def fn(source_code, *args, **kwargs):
return fn


@pytest.fixture(scope="module")
def get_revm_contract(revm_env, optimize, output_formats):
def fn(source_code, *args, **kwargs):
return revm_env.deploy_source(source_code, optimize, output_formats, *args, **kwargs)

return fn


@pytest.fixture
def get_contract_with_gas_estimation(tester, w3, optimize, output_formats):
def get_contract_with_gas_estimation(source_code, *args, **kwargs):
Expand Down
6 changes: 6 additions & 0 deletions tests/functional/builtins/folding/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import pytest


@pytest.fixture(scope="module")
def get_contract(get_revm_contract):
return get_revm_contract
File renamed without changes.
47 changes: 47 additions & 0 deletions tests/revm/abi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# wrapper module around whatever encoder we are using
from typing import Any

from eth.codecs.abi.decoder import Decoder
from eth.codecs.abi.encoder import Encoder
from eth.codecs.abi.exceptions import ABIError
from eth.codecs.abi.nodes import ABITypeNode, BytesNode
from eth.codecs.abi.parser import Parser

_parsers: dict[str, ABITypeNode] = {}


class _Encoder(Encoder):
"""
Custom encoder that parses bytes from hex to bytes
"""

@classmethod
def visit_BytesNode(cls, node: BytesNode, value: bytes | str) -> bytes:
if isinstance(value, str):
assert value.startswith("0x"), "Sanity check failed: expected hex string"
value = bytes.fromhex(value[2:])
return super().visit_BytesNode(node, value)


def _get_parser(schema: str):
try:
return _parsers[schema]
except KeyError:
_parsers[schema] = (ret := Parser.parse(schema))
return ret


def abi_encode(schema: str, data: Any) -> bytes:
return _Encoder.encode(_get_parser(schema), data)


def abi_decode(schema: str, data: bytes) -> Any:
return Decoder.decode(_get_parser(schema), data)


def is_abi_encodable(abi_type: str, data: Any) -> bool:
try:
abi_encode(abi_type, data)
return True
except ABIError:
return False
Loading

0 comments on commit 5a70826

Please sign in to comment.