-
-
Notifications
You must be signed in to change notification settings - Fork 791
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat[lang]: add
blobhash()
builtin (#3962)
This commit adds access to the `BLOBHASH` opcode via the new builtin function `blobhash()`. The opcode was introduced in EIP-4844. References: https://eips.ethereum.org/EIPS/eip-4844#opcode-to-get-versioned-hashes --------- Signed-off-by: Pascal Marco Caversaccio <pascal.caversaccio@hotmail.ch> Co-authored-by: tserg <8017125+tserg@users.noreply.github.com> Co-authored-by: Daniel Schiavini <daniel.schiavini@gmail.com> Co-authored-by: Charles Cooper <cooper.charles.m@gmail.com>
- Loading branch information
1 parent
097aecf
commit 533b271
Showing
10 changed files
with
148 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import random | ||
|
||
import pytest | ||
|
||
from vyper.compiler import compile_code | ||
|
||
valid_list = [ | ||
""" | ||
@external | ||
@view | ||
def foo() -> bytes32: | ||
return blobhash(0) | ||
""", | ||
""" | ||
@external | ||
@view | ||
def foo() -> bytes32: | ||
a: bytes32 = 0x0000000000000000000000000000000000000000000000000000000000000005 | ||
a = blobhash(2) | ||
return a | ||
""", | ||
""" | ||
@external | ||
@view | ||
def foo() -> bytes32: | ||
a: bytes32 = blobhash(0) | ||
assert a != empty(bytes32) | ||
return a | ||
""", | ||
""" | ||
@external | ||
@view | ||
def foo() -> bytes32: | ||
a: bytes32 = blobhash(1337) | ||
assert a == empty(bytes32) | ||
return a | ||
""", | ||
] | ||
|
||
|
||
@pytest.mark.requires_evm_version("cancun") | ||
@pytest.mark.parametrize("good_code", valid_list) | ||
def test_blobhash_success(good_code): | ||
assert compile_code(good_code) is not None | ||
out = compile_code(good_code, output_formats=["opcodes_runtime"]) | ||
assembly = out["opcodes_runtime"].split(" ") | ||
assert "BLOBHASH" in assembly | ||
|
||
|
||
@pytest.mark.requires_evm_version("cancun") | ||
def test_get_blobhashes(env, get_contract, tx_failed): | ||
code = """ | ||
@external | ||
def get_blobhash(i: uint256) -> bytes32: | ||
return blobhash(i) | ||
""" | ||
c = get_contract(code) | ||
|
||
# mock the evm blobhash attribute | ||
env.blob_hashes = [random.randbytes(32) for _ in range(6)] | ||
|
||
for i in range(6): | ||
assert c.get_blobhash(i) == env.blob_hashes[i] | ||
|
||
assert c.get_blobhash(len(env.blob_hashes)) == b"\0" * 32 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,6 +48,7 @@ | |
"signextend", | ||
"chainid", | ||
"basefee", | ||
"blobhash", | ||
"blobbasefee", | ||
"timestamp", | ||
"blockhash", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,6 +97,7 @@ | |
"delegatecall", | ||
"codesize", | ||
"basefee", | ||
"blobhash", | ||
"blobbasefee", | ||
"prevrandao", | ||
"difficulty", | ||
|