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

Chain ID #1763

Merged
merged 4 commits into from
Dec 17, 2019
Merged
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
99 changes: 99 additions & 0 deletions tests/parser/syntax/test_chainid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import pytest
from pytest import (
raises,
)

from vyper import (
compiler,
)
from vyper.exceptions import (
TypeMismatchException,
)

fail_list = [
"""
@public
def foo() -> int128[2]:
return [3,chain.id]
""",
"""
@public
def foo() -> decimal(wei / sec):
x: int128(wei) = as_wei_value(5, "finney")
y: int128 = chain.id + 50
return x / y
""",
"""
@public
def foo():
x: bytes[10] = slice("cow", start=0, len=chain.id)
""",
"""
@public
def foo():
x: int128 = 7
y: int128 = min(x, chain.id)
""",
"""
a: map(timestamp, int128)

@public
def add_record():
self.a[chain.id] = chain.id + 20
""",
"""
a: map(int128, timestamp)

@public
def add_record():
self.a[chain.id] = chain.id + 20
""",
"""
@public
def foo(inp: bytes[10]) -> bytes[3]:
return slice(inp, start=chain.id, len=3)
""",
]


@pytest.mark.parametrize('bad_code', fail_list)
def test_chain_fail(bad_code):

if isinstance(bad_code, tuple):
with raises(bad_code[1]):
compiler.compile_code(bad_code[0])
else:
with raises(TypeMismatchException):
compiler.compile_code(bad_code)


valid_list = [
"""
@public
@constant
def get_chain_id() -> uint256:
return chain.id
""",
"""
@public
@constant
def check_chain_id(c: uint256) -> bool:
return chain.id == c
""",
]


@pytest.mark.parametrize('good_code', valid_list)
def test_chain_success(good_code):
assert compiler.compile_code(good_code) is not None


def test_chainid_operation(get_contract_with_gas_estimation):
code = """
@public
@constant
def get_chain_id() -> uint256:
return chain.id
"""
c = get_contract_with_gas_estimation(code)
assert c.get_chain_id() == 0 # Default value of py-evm
1 change: 1 addition & 0 deletions vyper/opcodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
'NUMBER': [0x43, 0, 1, 2],
'DIFFICULTY': [0x44, 0, 1, 2],
'GASLIMIT': [0x45, 0, 1, 2],
'CHAINID': [0x46, 0, 1, 2], # Added Istanbul Ruleset
'POP': [0x50, 1, 0, 2],
'MLOAD': [0x51, 1, 1, 3],
'MSTORE': [0x52, 2, 0, 3],
Expand Down
3 changes: 3 additions & 0 deletions vyper/parser/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"block",
"msg",
"tx",
"chain",
}


Expand Down Expand Up @@ -380,6 +381,8 @@ def attribute(self):
)
elif key == "tx.origin":
return LLLnode.from_list(['origin'], typ='address', pos=getpos(self.expr))
elif key == "chain.id":
return LLLnode.from_list(['chainid'], typ='uint256', pos=getpos(self.expr))
else:
raise ParserException("Unsupported keyword: " + key, self.expr)
# Other variables
Expand Down
2 changes: 1 addition & 1 deletion vyper/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def in_bounds(cls, type_str, value):
'_init_', '___init___', '____init____',
'_default_', '___default___', '____default____',
# environment variables
'block', 'msg', 'tx',
'block', 'msg', 'tx', 'chain', 'chainid',
fubuloubu marked this conversation as resolved.
Show resolved Hide resolved
'blockhash', 'timestamp', 'timedelta',
# boolean literals
'true', 'false',
Expand Down