Skip to content

Commit e2f1fad

Browse files
committed
Add blobhash high-level global function.
1 parent 69122e0 commit e2f1fad

File tree

7 files changed

+41
-0
lines changed

7 files changed

+41
-0
lines changed

libsolidity/analysis/GlobalContext.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ 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
}
@@ -91,6 +92,7 @@ inline std::vector<std::shared_ptr<MagicVariableDeclaration const>> constructMag
9192
magicVarDecl("sha3", TypeProvider::function(strings{"bytes memory"}, strings{"bytes32"}, FunctionType::Kind::KECCAK256, StateMutability::Pure)),
9293
magicVarDecl("suicide", TypeProvider::function(strings{"address payable"}, strings{}, FunctionType::Kind::Selfdestruct)),
9394
magicVarDecl("tx", TypeProvider::magic(MagicType::Kind::Transaction)),
95+
magicVarDecl("blobhash", TypeProvider::function(strings{"uint256"}, strings{"bytes32"}, FunctionType::Kind::BlobHash, StateMutability::View)),
9496
// Accepts a MagicType that can be any contract type or an Integer type and returns a
9597
// MagicType. The TypeChecker handles the correctness of the input and output types.
9698
magicVarDecl("type", TypeProvider::function(

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
@@ -1286,6 +1286,7 @@ class FunctionType: public Type
12861286
ABIEncodeWithSignature,
12871287
ABIDecode,
12881288
GasLeft, ///< gasleft()
1289+
BlobHash, ///< BLOBHASH
12891290
MetaType, ///< type(...)
12901291
/// Refers to a function declaration without calling context
12911292
/// (i.e. when accessed directly via the name of the containing contract).

libsolidity/codegen/ExpressionCompiler.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1460,6 +1460,12 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall)
14601460
case FunctionType::Kind::GasLeft:
14611461
m_context << Instruction::GAS;
14621462
break;
1463+
case FunctionType::Kind::BlobHash:
1464+
{
1465+
acceptAndConvert(*arguments[0], *function.parameterTypes()[0], true);
1466+
m_context << Instruction::BLOBHASH;
1467+
break;
1468+
}
14631469
case FunctionType::Kind::MetaType:
14641470
// No code to generate.
14651471
break;

libsolidity/codegen/ir/IRGeneratorForStatements.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1663,6 +1663,14 @@ void IRGeneratorForStatements::endVisit(FunctionCall const& _functionCall)
16631663

16641664
break;
16651665
}
1666+
case FunctionType::Kind::BlobHash:
1667+
{
1668+
std::string args;
1669+
for (size_t i = 0; i < arguments.size(); ++i)
1670+
args += (args.empty() ? "" : ", ") + expressionAsType(*arguments[i], *(parameterTypes[i]));
1671+
define(_functionCall) << "blobhash(" << args << ")\n";
1672+
break;
1673+
}
16661674
default:
16671675
solUnimplemented("FunctionKind " + toString(static_cast<int>(functionType->kind())) + " not yet implemented");
16681676
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
contract C {
2+
function f(uint _index) public returns (bytes32) {
3+
return blobhash(_index);
4+
}
5+
}
6+
// ====
7+
// EVMVersion: >=cancun
8+
// ----
9+
// f(uint256): 0 -> 0x0100000000000000000000000000000000000000000000000000000000000001
10+
// f(uint256): 1 -> 0x0100000000000000000000000000000000000000000000000000000000000002
11+
// f(uint256): 2 -> 0x00
12+
// f(uint256): 255 -> 0x00
13+
// f(uint256): 256 -> 0x00
14+
// f(uint256): 257 -> 0x00
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
contract C {
2+
function f() public returns (bytes32) {
3+
return (blobhash)(0);
4+
}
5+
}
6+
// ====
7+
// EVMVersion: >=cancun
8+
// ----
9+
// f() -> 0x0100000000000000000000000000000000000000000000000000000000000001

0 commit comments

Comments
 (0)