Skip to content

Commit

Permalink
fix: vvm version detection (#343)
Browse files Browse the repository at this point in the history
* fix vvm version detection

* fix test

* use vvm for version detection

* update example pragmas

* bump vvm version, uses local vyper version when possible, only default to vvm otherwise

* use _pick_vyper_version

* fix lint
  • Loading branch information
trocher authored Nov 22, 2024
1 parent c42ced2 commit 08d4c53
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 22 deletions.
13 changes: 0 additions & 13 deletions boa/contracts/vvm/vvm_contract.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,9 @@
import re
from functools import cached_property

from boa.contracts.abi.abi_contract import ABIContractFactory, ABIFunction
from boa.environment import Env
from boa.util.eip5202 import generate_blueprint_bytecode

# TODO: maybe this doesn't detect release candidates
VERSION_RE = re.compile(r"\s*#\s*(pragma\s+version|@version)\s+(\d+\.\d+\.\d+)")


# TODO: maybe move this up to vvm?
def _detect_version(source_code: str):
res = VERSION_RE.findall(source_code)
if len(res) < 1:
return None
# TODO: handle len(res) > 1
return res[0][1]


class VVMDeployer:
"""
Expand Down
12 changes: 8 additions & 4 deletions boa/interpret.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import vvm
import vyper
from packaging.version import Version
from vvm.utils.versioning import _pick_vyper_version, detect_version_specifier_set
from vyper.ast.parse import parse_to_ast
from vyper.cli.vyper_compile import get_search_paths
from vyper.compiler.input_bundle import (
Expand All @@ -23,7 +25,7 @@
from vyper.utils import sha256sum

from boa.contracts.abi.abi_contract import ABIContractFactory
from boa.contracts.vvm.vvm_contract import VVMDeployer, _detect_version
from boa.contracts.vvm.vvm_contract import VVMDeployer
from boa.contracts.vyper.vyper_contract import (
VyperBlueprint,
VyperContract,
Expand Down Expand Up @@ -249,8 +251,10 @@ def loads_partial(
if dedent:
source_code = textwrap.dedent(source_code)

version = _detect_version(source_code)
if version is not None and version != vyper.__version__:
specifier_set = detect_version_specifier_set(source_code)
# Use VVM only if the installed version is not in the specifier set
if specifier_set is not None and not specifier_set.contains(vyper.__version__):
version = _pick_vyper_version(specifier_set)
filename = str(filename) # help mypy
# TODO: pass name to loads_partial_vvm, not filename
return _loads_partial_vvm(source_code, version, filename)
Expand All @@ -269,7 +273,7 @@ def load_partial(filename: str, compiler_args=None):
)


def _loads_partial_vvm(source_code: str, version: str, filename: str):
def _loads_partial_vvm(source_code: str, version: Version, filename: str):
global _disk_cache

# install the requested version if not already installed
Expand Down
2 changes: 1 addition & 1 deletion examples/ERC20.vy
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# pragma version ^0.4.0
# pragma version ~=0.4.0
# @dev Implementation of ERC-20 token standard.
# @author Takayuki Jimba (@yudetamago)
# https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20.md
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ dependencies = [
"pytest-cov",

# required to compile older versions of vyper
"vvm",
"vvm>=0.3.2",

# eth-rlp requirement, not installed by default with 3.12
"typing-extensions",
Expand Down
19 changes: 19 additions & 0 deletions tests/unitary/contracts/vvm/test_vvm.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pytest

import boa

mock_3_10_path = "tests/unitary/contracts/vvm/mock_3_10.vy"
Expand Down Expand Up @@ -29,6 +31,23 @@ def test_load_vvm():
assert contract.bar() == 43


@pytest.mark.parametrize(
"version_pragma",
[
"# @version ^0.3.1",
"# @version ^0.3.7",
"# @version ==0.3.10",
"# @version ~=0.3.10",
"# @version 0.3.10",
"# pragma version >=0.3.8, <0.4.0, !=0.3.10",
"# pragma version ==0.4.0rc3",
],
)
def test_load_complex_version_vvm(version_pragma):
contract = boa.loads(version_pragma + "\nfoo: public(uint256)")
assert contract.foo() == 0


def test_loads_vvm():
with open(mock_3_10_path) as f:
code = f.read()
Expand Down
2 changes: 1 addition & 1 deletion tests/unitary/fixtures/module_contract.vy
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# pragma version ^0.4.0
# pragma version ~=0.4.0

import module_lib

Expand Down
5 changes: 3 additions & 2 deletions tests/unitary/utils/test_cache.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from unittest.mock import patch

import pytest
from packaging.version import Version
from vyper.compiler import CompilerData

from boa.contracts.vyper.vyper_contract import VyperDeployer
Expand Down Expand Up @@ -34,8 +35,8 @@ def test_cache_vvm():
code = """
x: constant(int128) = 1000
"""
version = "0.2.8"
version2 = "0.3.1"
version = Version("0.2.8")
version2 = Version("0.3.1")
assert _disk_cache is not None

# Mock vvm.compile_source
Expand Down

0 comments on commit 08d4c53

Please sign in to comment.