Skip to content

Commit 81268e3

Browse files
r0qscameel
authored andcommitted
Add blobhash high-level global function.
1 parent f05d0a9 commit 81268e3

30 files changed

+234
-12
lines changed

Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
Language Features:
44
* Introduce global ``block.blobbasefee`` for retrieving the blob base fee of the current block.
5+
* Introduce global function ``blobhash(uint)`` for retrieving versioned hashes of blobs, akin to the homonymous Yul builtin.
56
* Yul: Introduce builtin ``blobbasefee()`` for retrieving the blob base fee of the current block.
67
* Yul: Introduce builtin ``blobhash()`` for retrieving versioned hashes of blobs associated with the transaction.
78

docs/cheatsheet.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,16 @@ Members of ``address``
5656
returns ``false`` on failure
5757
- ``<address payable>.transfer(uint256 amount)``: send given amount of Wei to :ref:`address`, throws on failure
5858

59-
.. index:: blockhash, block, block;basefee, block;blobbasefee, block;chainid, block;coinbase, block;difficulty, block;gaslimit, block;number, block;prevrandao, block;timestamp
59+
.. index:: blockhash, blobhash, block, block;basefee, block;blobbasefee, block;chainid, block;coinbase, block;difficulty, block;gaslimit, block;number, block;prevrandao, block;timestamp
6060
.. index:: gasleft, msg;data, msg;sender, msg;sig, msg;value, tx;gasprice, tx;origin
6161

6262
Block and Transaction Properties
6363
================================
6464

6565
- ``blockhash(uint blockNumber) returns (bytes32)``: hash of the given block - only works for 256 most recent blocks
66+
- ``blobhash(uint index) returns (bytes32)``: versioned hash of the ``index``-th blob associated with the current transaction.
67+
A versioned hash consists of a single byte representing the version (currently ``0x01``), followed by the last 31 bytes
68+
of the SHA256 hash of the KZG commitment (`EIP-4844 <https://eips.ethereum.org/EIPS/eip-4844>`_).
6669
- ``block.basefee`` (``uint``): current block's base fee (`EIP-3198 <https://eips.ethereum.org/EIPS/eip-3198>`_ and `EIP-1559 <https://eips.ethereum.org/EIPS/eip-1559>`_)
6770
- ``block.blobbasefee`` (``uint``): current block's blob base fee (`EIP-7516 <https://eips.ethereum.org/EIPS/eip-7516>`_ and `EIP-4844 <https://eips.ethereum.org/EIPS/eip-4844>`_)
6871
- ``block.chainid`` (``uint``): current chain id

docs/units-and-global-variables.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ Block and Transaction Properties
7474
--------------------------------
7575

7676
- ``blockhash(uint blockNumber) returns (bytes32)``: hash of the given block when ``blocknumber`` is one of the 256 most recent blocks; otherwise returns zero
77+
- ``blobhash(uint index) returns (bytes32)``: versioned hash of the ``index``-th blob associated with the current transaction.
78+
A versioned hash consists of a single byte representing the version (currently ``0x01``), followed by the last 31 bytes
79+
of the SHA256 hash of the KZG commitment (`EIP-4844 <https://eips.ethereum.org/EIPS/eip-4844>`_).
7780
- ``block.basefee`` (``uint``): current block's base fee (`EIP-3198 <https://eips.ethereum.org/EIPS/eip-3198>`_ and `EIP-1559 <https://eips.ethereum.org/EIPS/eip-1559>`_)
7881
- ``block.blobbasefee`` (``uint``): current block's blob base fee (`EIP-7516 <https://eips.ethereum.org/EIPS/eip-7516>`_ and `EIP-4844 <https://eips.ethereum.org/EIPS/eip-4844>`_)
7982
- ``block.chainid`` (``uint``): current chain id

docs/using-the-compiler.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ at each version. Backward compatibility is not guaranteed between each version.
178178
- Smaller code size and gas savings due to the introduction of ``push0`` (see `EIP-3855 <https://eips.ethereum.org/EIPS/eip-3855>`_).
179179
- ``cancun``
180180
- The block's blob base fee (`EIP-7516 <https://eips.ethereum.org/EIPS/eip-7516>`_ and `EIP-4844 <https://eips.ethereum.org/EIPS/eip-4844>`_) can be accessed via the global ``block.blobbasefee`` or ``blobbasefee()`` in inline assembly.
181+
- Introduces ``blobhash()`` in inline assembly and a corresponding global function to retrieve versioned hashes of blobs associated with the transaction (see `EIP-4844 <https://eips.ethereum.org/EIPS/eip-4844>`_).
181182

182183
.. index:: ! standard JSON, ! --standard-json
183184
.. _compiler-api:

libsolidity/analysis/GlobalContext.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,18 @@ int magicVariableToID(std::string const& _name)
5959
else if (_name == "tx") return -26;
6060
else if (_name == "type") return -27;
6161
else if (_name == "this") return -28;
62+
else if (_name == "blobhash") return -29;
6263
else
6364
solAssert(false, "Unknown magic variable: \"" + _name + "\".");
6465
}
6566

66-
inline std::vector<std::shared_ptr<MagicVariableDeclaration const>> constructMagicVariables()
67+
inline std::vector<std::shared_ptr<MagicVariableDeclaration const>> constructMagicVariables(langutil::EVMVersion _evmVersion)
6768
{
6869
static auto const magicVarDecl = [](std::string const& _name, Type const* _type) {
6970
return std::make_shared<MagicVariableDeclaration>(magicVariableToID(_name), _name, _type);
7071
};
7172

72-
return {
73+
std::vector<std::shared_ptr<MagicVariableDeclaration const>> magicVariableDeclarations = {
7374
magicVarDecl("abi", TypeProvider::magic(MagicType::Kind::ABI)),
7475
magicVarDecl("addmod", TypeProvider::function(strings{"uint256", "uint256", "uint256"}, strings{"uint256"}, FunctionType::Kind::AddMod, StateMutability::Pure)),
7576
magicVarDecl("assert", TypeProvider::function(strings{"bool"}, strings{}, FunctionType::Kind::Assert, StateMutability::Pure)),
@@ -101,11 +102,19 @@ inline std::vector<std::shared_ptr<MagicVariableDeclaration const>> constructMag
101102
FunctionType::Options::withArbitraryParameters()
102103
)),
103104
};
105+
106+
if (_evmVersion >= langutil::EVMVersion::cancun())
107+
magicVariableDeclarations.push_back(
108+
magicVarDecl("blobhash", TypeProvider::function(strings{"uint256"}, strings{"bytes32"}, FunctionType::Kind::BlobHash, StateMutability::View))
109+
);
110+
111+
return magicVariableDeclarations;
104112
}
105113

106114
}
107115

108-
GlobalContext::GlobalContext(): m_magicVariables{constructMagicVariables()}
116+
GlobalContext::GlobalContext(langutil::EVMVersion _evmVersion):
117+
m_magicVariables{constructMagicVariables(_evmVersion)}
109118
{
110119
}
111120

libsolidity/analysis/GlobalContext.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#pragma once
2525

26+
#include <liblangutil/EVMVersion.h>
2627
#include <libsolidity/ast/ASTForward.h>
2728
#include <map>
2829
#include <memory>
@@ -47,7 +48,7 @@ class GlobalContext
4748
GlobalContext(GlobalContext const&) = delete;
4849
GlobalContext& operator=(GlobalContext const&) = delete;
4950

50-
GlobalContext();
51+
GlobalContext(langutil::EVMVersion _evmVersion);
5152
void setCurrentContract(ContractDefinition const& _contract);
5253
void resetCurrentContract() { m_currentContract = nullptr; }
5354
MagicVariableDeclaration const* currentThis() const;

libsolidity/ast/Types.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3075,6 +3075,7 @@ std::string FunctionType::richIdentifier() const
30753075
case Kind::ABIEncodeCall: id += "abiencodecall"; break;
30763076
case Kind::ABIEncodeWithSignature: id += "abiencodewithsignature"; break;
30773077
case Kind::ABIDecode: id += "abidecode"; break;
3078+
case Kind::BlobHash: id += "blobhash"; break;
30783079
case Kind::MetaType: id += "metatype"; break;
30793080
}
30803081
id += "_" + stateMutabilityToString(m_stateMutability);

libsolidity/ast/Types.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,6 +1270,7 @@ class FunctionType: public Type
12701270
SetGas, ///< modify the default gas value for the function call
12711271
SetValue, ///< modify the default value transfer for the function call
12721272
BlockHash, ///< BLOCKHASH
1273+
BlobHash, ///< BLOBHASH
12731274
AddMod, ///< ADDMOD
12741275
MulMod, ///< MULMOD
12751276
ArrayPush, ///< .push() to a dynamically sized array in storage

libsolidity/codegen/ExpressionCompiler.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1035,9 +1035,13 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall)
10351035
break;
10361036
}
10371037
case FunctionType::Kind::BlockHash:
1038+
case FunctionType::Kind::BlobHash:
10381039
{
10391040
acceptAndConvert(*arguments[0], *function.parameterTypes()[0], true);
1040-
m_context << Instruction::BLOCKHASH;
1041+
if (function.kind() == FunctionType::Kind::BlockHash)
1042+
m_context << Instruction::BLOCKHASH;
1043+
else
1044+
m_context << Instruction::BLOBHASH;
10411045
break;
10421046
}
10431047
case FunctionType::Kind::AddMod:

libsolidity/codegen/ir/IRGeneratorForStatements.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1498,11 +1498,13 @@ void IRGeneratorForStatements::endVisit(FunctionCall const& _functionCall)
14981498
case FunctionType::Kind::GasLeft:
14991499
case FunctionType::Kind::Selfdestruct:
15001500
case FunctionType::Kind::BlockHash:
1501+
case FunctionType::Kind::BlobHash:
15011502
{
15021503
static std::map<FunctionType::Kind, std::string> functions = {
15031504
{FunctionType::Kind::GasLeft, "gas"},
15041505
{FunctionType::Kind::Selfdestruct, "selfdestruct"},
15051506
{FunctionType::Kind::BlockHash, "blockhash"},
1507+
{FunctionType::Kind::BlobHash, "blobhash"},
15061508
};
15071509
solAssert(functions.find(functionType->kind()) != functions.end());
15081510

0 commit comments

Comments
 (0)