diff --git a/Changelog.md b/Changelog.md
index 741e64f3e2d9..0cfa621e802a 100644
--- a/Changelog.md
+++ b/Changelog.md
@@ -2,6 +2,7 @@
Language Features:
* Introduce global ``block.blobbasefee`` for retrieving the blob base fee of the current block.
+ * Introduce global function ``blobhash(uint)`` for retrieving versioned hashes of blobs, akin to the homonymous Yul builtin.
* Yul: Introduce builtin ``blobbasefee()`` for retrieving the blob base fee of the current block.
* Yul: Introduce builtin ``blobhash()`` for retrieving versioned hashes of blobs associated with the transaction.
diff --git a/docs/cheatsheet.rst b/docs/cheatsheet.rst
index e6b632dae4b6..2b68d7311982 100644
--- a/docs/cheatsheet.rst
+++ b/docs/cheatsheet.rst
@@ -56,13 +56,16 @@ Members of ``address``
returns ``false`` on failure
- ``
.transfer(uint256 amount)``: send given amount of Wei to :ref:`address`, throws on failure
-.. index:: blockhash, block, block;basefee, block;blobbasefee, block;chainid, block;coinbase, block;difficulty, block;gaslimit, block;number, block;prevrandao, block;timestamp
+.. index:: blockhash, blobhash, block, block;basefee, block;blobbasefee, block;chainid, block;coinbase, block;difficulty, block;gaslimit, block;number, block;prevrandao, block;timestamp
.. index:: gasleft, msg;data, msg;sender, msg;sig, msg;value, tx;gasprice, tx;origin
Block and Transaction Properties
================================
- ``blockhash(uint blockNumber) returns (bytes32)``: hash of the given block - only works for 256 most recent blocks
+- ``blobhash(uint index) returns (bytes32)``: versioned hash of the ``index``-th blob associated with the current transaction.
+ A versioned hash consists of a single byte representing the version (currently ``0x01``), followed by the last 31 bytes
+ of the SHA256 hash of the KZG commitment (`EIP-4844 `_).
- ``block.basefee`` (``uint``): current block's base fee (`EIP-3198 `_ and `EIP-1559 `_)
- ``block.blobbasefee`` (``uint``): current block's blob base fee (`EIP-7516 `_ and `EIP-4844 `_)
- ``block.chainid`` (``uint``): current chain id
diff --git a/docs/units-and-global-variables.rst b/docs/units-and-global-variables.rst
index f95c9c5290d8..a0422c32dfa9 100644
--- a/docs/units-and-global-variables.rst
+++ b/docs/units-and-global-variables.rst
@@ -74,6 +74,9 @@ Block and Transaction Properties
--------------------------------
- ``blockhash(uint blockNumber) returns (bytes32)``: hash of the given block when ``blocknumber`` is one of the 256 most recent blocks; otherwise returns zero
+- ``blobhash(uint index) returns (bytes32)``: versioned hash of the ``index``-th blob associated with the current transaction.
+ A versioned hash consists of a single byte representing the version (currently ``0x01``), followed by the last 31 bytes
+ of the SHA256 hash of the KZG commitment (`EIP-4844 `_).
- ``block.basefee`` (``uint``): current block's base fee (`EIP-3198 `_ and `EIP-1559 `_)
- ``block.blobbasefee`` (``uint``): current block's blob base fee (`EIP-7516 `_ and `EIP-4844 `_)
- ``block.chainid`` (``uint``): current chain id
diff --git a/docs/using-the-compiler.rst b/docs/using-the-compiler.rst
index 60fbd0884a89..93bdff8661bf 100644
--- a/docs/using-the-compiler.rst
+++ b/docs/using-the-compiler.rst
@@ -178,6 +178,7 @@ at each version. Backward compatibility is not guaranteed between each version.
- Smaller code size and gas savings due to the introduction of ``push0`` (see `EIP-3855 `_).
- ``cancun``
- The block's blob base fee (`EIP-7516 `_ and `EIP-4844 `_) can be accessed via the global ``block.blobbasefee`` or ``blobbasefee()`` in inline assembly.
+ - Introduces ``blobhash()`` in inline assembly and a corresponding global function to retrieve versioned hashes of blobs associated with the transaction (see `EIP-4844 `_).
.. index:: ! standard JSON, ! --standard-json
.. _compiler-api:
diff --git a/libsolidity/analysis/GlobalContext.cpp b/libsolidity/analysis/GlobalContext.cpp
index 8f0ce963499b..628b903617de 100644
--- a/libsolidity/analysis/GlobalContext.cpp
+++ b/libsolidity/analysis/GlobalContext.cpp
@@ -59,17 +59,18 @@ int magicVariableToID(std::string const& _name)
else if (_name == "tx") return -26;
else if (_name == "type") return -27;
else if (_name == "this") return -28;
+ else if (_name == "blobhash") return -29;
else
solAssert(false, "Unknown magic variable: \"" + _name + "\".");
}
-inline std::vector> constructMagicVariables()
+inline std::vector> constructMagicVariables(langutil::EVMVersion _evmVersion)
{
static auto const magicVarDecl = [](std::string const& _name, Type const* _type) {
return std::make_shared(magicVariableToID(_name), _name, _type);
};
- return {
+ std::vector> magicVariableDeclarations = {
magicVarDecl("abi", TypeProvider::magic(MagicType::Kind::ABI)),
magicVarDecl("addmod", TypeProvider::function(strings{"uint256", "uint256", "uint256"}, strings{"uint256"}, FunctionType::Kind::AddMod, StateMutability::Pure)),
magicVarDecl("assert", TypeProvider::function(strings{"bool"}, strings{}, FunctionType::Kind::Assert, StateMutability::Pure)),
@@ -101,11 +102,19 @@ inline std::vector> constructMag
FunctionType::Options::withArbitraryParameters()
)),
};
+
+ if (_evmVersion >= langutil::EVMVersion::cancun())
+ magicVariableDeclarations.push_back(
+ magicVarDecl("blobhash", TypeProvider::function(strings{"uint256"}, strings{"bytes32"}, FunctionType::Kind::BlobHash, StateMutability::View))
+ );
+
+ return magicVariableDeclarations;
}
}
-GlobalContext::GlobalContext(): m_magicVariables{constructMagicVariables()}
+GlobalContext::GlobalContext(langutil::EVMVersion _evmVersion):
+ m_magicVariables{constructMagicVariables(_evmVersion)}
{
}
diff --git a/libsolidity/analysis/GlobalContext.h b/libsolidity/analysis/GlobalContext.h
index 69344537cae7..3aa5e76e8966 100644
--- a/libsolidity/analysis/GlobalContext.h
+++ b/libsolidity/analysis/GlobalContext.h
@@ -23,6 +23,7 @@
#pragma once
+#include
#include
#include