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

Fix for VVM with non-fixed version pragma #315

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
44 changes: 41 additions & 3 deletions boa/contracts/vvm/vvm_contract.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,58 @@
import re
from functools import cached_property

from vvm.utils.convert import to_vyper_version
from boa.contracts.abi.abi_contract import ABIContractFactory, ABIFunction
from boa.environment import Env

# TODO: maybe this doesn't detect release candidates
VERSION_RE = re.compile(r"\s*#\s*(pragma\s+version|@version)\s+(\d+\.\d+\.\d+)")
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):
# 1. Detects version from source file
# 2. In case of ^ or >= and if vyper_version is provided, returns vyper_version is allowed
def _detect_version(source_code: str, vyper_version: str = None) -> str:
res = VERSION_RE.findall(source_code)
if len(res) < 1:
return None
# TODO: handle len(res) > 1
return res[0][1]

# Exact version specified
if res[0][1] == "":
return res[0][2]
# Caret means we check if vyper_version is compatible
elif res[0][1] == "^":
if vyper_version:
min_version = to_vyper_version(res[0][2])
# Compute maximum version according to rules
min_nonallowed_version = to_vyper_version(
f"0.{min_version.minor+1}.0"
if min_version.major == 0
else f"{min_version.major+1}.0.0"
)
vy_version = to_vyper_version(vyper_version)
if min_version <= vy_version and vy_version < min_nonallowed_version:
return vyper_version
else:
# Else use minimum allowed version
return res[0][2]
else:
return res[0][2]
# Greater-Equal means we check if vyper_version is compatible
elif res[0][1] == ">=":
# If vyper_version is allowed by >=, use it
if vyper_version:
if to_vyper_version(vyper_version) >= to_vyper_version(res[0][2]):
return vyper_version
else:
# Else use minimum allowed version
return res[0][2]
else:
return res[0][2]
# Unknown Operand
else:
return None


class VVMDeployer:
Expand Down
2 changes: 1 addition & 1 deletion boa/interpret.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def loads_partial(
if dedent:
source_code = textwrap.dedent(source_code)

version = _detect_version(source_code)
version = _detect_version(source_code, vyper_version=vyper.__version__)
if version is not None and version != vyper.__version__:
filename = str(filename) # help mypy
return _loads_partial_vvm(source_code, version, filename)
Expand Down
65 changes: 65 additions & 0 deletions tests/unitary/contracts/vvm/test_vvm.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import boa
from boa.contracts.vvm.vvm_contract import _detect_version

mock_3_10_path = "tests/unitary/contracts/vvm/mock_3_10.vy"

Expand Down Expand Up @@ -37,3 +38,67 @@ def test_loads_vvm():

assert contract.foo() == 42
assert contract.bar() == 43


def test_detect_version():
code_floating = """
# pragma version ^0.3.9
@external
def foo() -> uint256:
x: uint256 = 1
return x + 7
"""
assert _detect_version(code_floating) == "0.3.9"

code_ge = """
# pragma version >=0.3.9
@external
def foo() -> uint256:
x: uint256 = 1
return x + 7
"""
assert _detect_version(code_ge) == "0.3.9"

code_fixed = """
# pragma version 0.3.9
@external
def foo() -> uint256:
x: uint256 = 1
return x + 7
"""
assert _detect_version(code_fixed) == "0.3.9"


def test_detect_version_with_vyper_version():
code_floating = """
# pragma version ^0.3.9
@external
def foo() -> uint256:
x: uint256 = 1
return x + 7
"""
assert _detect_version(code_floating, vyper_version="0.3.9") == "0.3.9"
assert _detect_version(code_floating, vyper_version="0.3.10") == "0.3.10"
assert _detect_version(code_floating, vyper_version="0.4.0") == "0.3.9"

code_ge = """
# pragma version >=0.3.9
@external
def foo() -> uint256:
x: uint256 = 1
return x + 7
"""
assert _detect_version(code_ge, vyper_version="0.3.9") == "0.3.9"
assert _detect_version(code_ge, vyper_version="0.3.10") == "0.3.10"
assert _detect_version(code_ge, vyper_version="0.4.0") == "0.4.0"

code_fixed = """
# pragma version 0.3.9
@external
def foo() -> uint256:
x: uint256 = 1
return x + 7
"""
assert _detect_version(code_fixed, vyper_version="0.3.9") == "0.3.9"
assert _detect_version(code_fixed, vyper_version="0.3.10") == "0.3.9"
assert _detect_version(code_fixed, vyper_version="0.4.0") == "0.3.9"
Loading