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

feat: introduce block.prevrandao as alias for block.difficulty #3085

Merged
merged 10 commits into from Sep 8, 2022
13 changes: 9 additions & 4 deletions docs/constants-and-vars.rst
Expand Up @@ -11,11 +11,12 @@ Environment variables always exist in the namespace and are primarily used to pr
Block and Transaction Properties
--------------------------------

==================== ================ =============================================
==================== ================ =========================================================
Name Type Value
==================== ================ =============================================
``block.coinbase`` ``address`` Current block miners address
==================== ================ =========================================================
``block.coinbase`` ``address`` Current block miner's address
``block.difficulty`` ``uint256`` Current block difficulty
``block.prevrandao`` ``uint256`` Current randomness beacon provided by the beacon chain
``block.number`` ``uint256`` Current block number
``block.prevhash`` ``bytes32`` Equivalent to ``blockhash(block.number - 1)``
``block.timestamp`` ``uint256`` Current block epoch timestamp
Expand All @@ -26,7 +27,11 @@ Name Type Value
``msg.value`` ``uint256`` Number of wei sent with the message
``tx.origin`` ``address`` Sender of the transaction (full call chain)
``tx.gasprice`` ``uint256`` Gas price of current transaction in wei
==================== ================ =============================================
==================== ================ =========================================================

pcaversaccio marked this conversation as resolved.
Show resolved Hide resolved
.. note::

``block.prevrandao`` is an alias for ``block.difficulty``. Since ``block.difficulty`` is considered deprecated according to `EIP-4399 <https://eips.ethereum.org/EIPS/eip-4399>`_ after "The Merge" (Paris hard fork), we recommend using ``block.prevrandao``.

.. note::

Expand Down
2 changes: 1 addition & 1 deletion tests/compiler/test_opcodes.py
Expand Up @@ -43,7 +43,7 @@ def test_version_check(evm_version):

def test_get_opcodes(evm_version):
op = opcodes.get_opcodes()
if evm_version == "berlin":
if evm_version in ("paris", "berlin"):
assert "CHAINID" in op
assert op["SLOAD"][-1] == 2100
elif evm_version == "istanbul":
Expand Down
7 changes: 7 additions & 0 deletions tests/parser/syntax/test_block.py
Expand Up @@ -138,6 +138,13 @@ def foo():
""",
"""
@external
def foo():
x: uint256 = block.prevrandao + 185
if tx.origin == self:
y: Bytes[35] = concat(block.prevhash, b"dog")
""",
"""
@external
def foo() -> uint256:
return tx.gasprice
""",
Expand Down
18 changes: 18 additions & 0 deletions vyper/codegen/expr.py
Expand Up @@ -42,13 +42,15 @@
TypeCheckFailure,
TypeMismatch,
UnimplementedException,
VyperException,
)
from vyper.utils import (
DECIMAL_DIVISOR,
SizeLimits,
bytes_to_int,
is_checksum_encoded,
string_to_bytes,
vyper_warn,
)

ENVIRONMENT_VARIABLES = {"block", "msg", "tx", "chain"}
Expand Down Expand Up @@ -279,7 +281,23 @@ def parse_Attribute(self):
return IRnode.from_list(["callvalue"], typ=BaseType("uint256"))
elif key == "msg.gas":
return IRnode.from_list(["gas"], typ="uint256")
elif key == "block.prevrandao":
if not version_check(begin="paris"):
warning = VyperException(
"tried to use block.prevrandao in pre-Paris "
"environment! Suggest using block.difficulty instead.",
self.expr,
)
vyper_warn(str(warning))
return IRnode.from_list(["prevrandao"], typ="uint256")
elif key == "block.difficulty":
if version_check(begin="paris"):
warning = VyperException(
"tried to use block.difficulty in post-Paris "
"environment! Suggest using block.prevrandao instead.",
self.expr,
)
vyper_warn(str(warning))
return IRnode.from_list(["difficulty"], typ="uint256")
elif key == "block.timestamp":
return IRnode.from_list(["timestamp"], typ=BaseType("uint256"))
Expand Down
6 changes: 4 additions & 2 deletions vyper/evm/opcodes.py
Expand Up @@ -3,7 +3,7 @@
from vyper.exceptions import CompilerPanic
from vyper.typing import OpcodeGasCost, OpcodeMap, OpcodeRulesetMap, OpcodeRulesetValue, OpcodeValue

active_evm_version: int = 3
active_evm_version: int = 4

# EVM version rules work as follows:
# 1. Fork rules go from oldest (lowest value) to newest (highest value).
Expand All @@ -24,11 +24,12 @@
"petersburg": 1,
"istanbul": 2,
"berlin": 3,
"paris": 4,
# ETC Forks
"atlantis": 0,
"agharta": 1,
}
DEFAULT_EVM_VERSION: str = "berlin"
DEFAULT_EVM_VERSION: str = "paris"


# opcode as hex value
Expand Down Expand Up @@ -84,6 +85,7 @@
"TIMESTAMP": (0x42, 0, 1, 2),
"NUMBER": (0x43, 0, 1, 2),
"DIFFICULTY": (0x44, 0, 1, 2),
"PREVRANDAO": (0x44, 0, 1, 2),
charles-cooper marked this conversation as resolved.
Show resolved Hide resolved
"GASLIMIT": (0x45, 0, 1, 2),
"CHAINID": (0x46, 0, 1, (None, None, 2)),
"SELFBALANCE": (0x47, 0, 1, (None, None, 5)),
Expand Down
1 change: 1 addition & 0 deletions vyper/semantics/environment.py
Expand Up @@ -11,6 +11,7 @@
"block": {
"coinbase": AddressDefinition,
"difficulty": Uint256Definition,
"prevrandao": Uint256Definition,
"number": Uint256Definition,
"gaslimit": Uint256Definition,
"basefee": Uint256Definition,
Expand Down