Skip to content

Commit 5f8b487

Browse files
authored
Merge pull request #13571 from ethereum/explicit-fixedhash
Drop implicit alignment argument from FixedHash (and improve FunctionSelector helpers)
2 parents d1b8991 + 2a41295 commit 5f8b487

27 files changed

+123
-66
lines changed

libsolidity/analysis/PostTypeChecker.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ struct ReservedErrorSelector: public PostTypeChecker::Checker
411411
);
412412
else
413413
{
414-
uint32_t selector = util::selectorFromSignature32(_error.functionType(true)->externalSignature());
414+
uint32_t selector = util::selectorFromSignatureU32(_error.functionType(true)->externalSignature());
415415
if (selector == 0 || ~selector == 0)
416416
m_errorReporter.syntaxError(
417417
2855_error,

libsolidity/analysis/PostTypeContractLevelChecker.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ bool PostTypeContractLevelChecker::check(ContractDefinition const& _contract)
5252
for (ErrorDefinition const* error: _contract.interfaceErrors())
5353
{
5454
string signature = error->functionType(true)->externalSignature();
55-
uint32_t hash = util::selectorFromSignature32(signature);
55+
uint32_t hash = util::selectorFromSignatureU32(signature);
5656
// Fail if there is a different signature for the same hash.
5757
if (!errorHashes[hash].empty() && !errorHashes[hash].count(signature))
5858
{

libsolidity/ast/AST.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <libsolidity/ast/ASTVisitor.h>
2828
#include <libsolidity/ast/AST_accept.h>
2929
#include <libsolidity/ast/TypeProvider.h>
30+
#include <libsolutil/FunctionSelector.h>
3031
#include <libsolutil/Keccak256.h>
3132

3233
#include <range/v3/view/tail.hpp>
@@ -281,8 +282,7 @@ vector<pair<util::FixedHash<4>, FunctionTypePointer>> const& ContractDefinition:
281282
if (signaturesSeen.count(functionSignature) == 0)
282283
{
283284
signaturesSeen.insert(functionSignature);
284-
util::FixedHash<4> hash(util::keccak256(functionSignature));
285-
interfaceFunctionList.emplace_back(hash, fun);
285+
interfaceFunctionList.emplace_back(util::selectorFromSignatureH32(functionSignature), fun);
286286
}
287287
}
288288
}

libsolidity/ast/Types.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3583,12 +3583,12 @@ string FunctionType::externalSignature() const
35833583

35843584
u256 FunctionType::externalIdentifier() const
35853585
{
3586-
return util::selectorFromSignature32(externalSignature());
3586+
return util::selectorFromSignatureU32(externalSignature());
35873587
}
35883588

35893589
string FunctionType::externalIdentifierHex() const
35903590
{
3591-
return util::FixedHash<4>(util::keccak256(externalSignature())).hex();
3591+
return util::selectorFromSignatureH32(externalSignature()).hex();
35923592
}
35933593

35943594
bool FunctionType::isPure() const

libsolidity/codegen/ArrayUtils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -889,7 +889,7 @@ void ArrayUtils::popStorageArrayElement(ArrayType const& _type) const
889889
}
890890
sstore(ref, slot_value)
891891
})");
892-
code("panicSelector", util::selectorFromSignature("Panic(uint256)").str());
892+
code("panicSelector", util::selectorFromSignatureU256("Panic(uint256)").str());
893893
code("emptyArrayPop", to_string(unsigned(util::PanicCode::EmptyArrayPop)));
894894
m_context.appendInlineAssembly(code.render(), {"ref", "slot_value", "length"});
895895
m_context << Instruction::POP << Instruction::POP << Instruction::POP;

libsolidity/codegen/CompilerUtils.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ void CompilerUtils::revertWithStringData(Type const& _argumentType)
9595
{
9696
solAssert(_argumentType.isImplicitlyConvertibleTo(*TypeProvider::fromElementaryTypeName("string memory")));
9797
fetchFreeMemoryPointer();
98-
m_context << util::selectorFromSignature("Error(string)");
98+
m_context << util::selectorFromSignatureU256("Error(string)");
9999
m_context << Instruction::DUP2 << Instruction::MSTORE;
100100
m_context << u256(4) << Instruction::ADD;
101101
// Stack: <string data> <mem pos of encoding start>
@@ -111,7 +111,7 @@ void CompilerUtils::revertWithError(
111111
)
112112
{
113113
fetchFreeMemoryPointer();
114-
m_context << util::selectorFromSignature(_signature);
114+
m_context << util::selectorFromSignatureU256(_signature);
115115
m_context << Instruction::DUP2 << Instruction::MSTORE;
116116
m_context << u256(4) << Instruction::ADD;
117117
// Stack: <arguments...> <mem pos of encoding start>

libsolidity/codegen/ContractCompiler.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ size_t ContractCompiler::deployLibrary(ContractDefinition const& _contract)
253253
return(codepos, subSize)
254254
}
255255
)")
256-
("panicSelector", util::selectorFromSignature("Panic(uint256)").str())
256+
("panicSelector", util::selectorFromSignatureU256("Panic(uint256)").str())
257257
("panicCode", "0")
258258
.render(),
259259
{"subSize", "subOffset"}
@@ -1046,7 +1046,7 @@ void ContractCompiler::handleCatch(vector<ASTPointer<TryCatchClause>> const& _ca
10461046
solAssert(m_context.evmVersion().supportsReturndata(), "");
10471047

10481048
// stack: <selector>
1049-
m_context << Instruction::DUP1 << util::selectorFromSignature32("Error(string)") << Instruction::EQ;
1049+
m_context << Instruction::DUP1 << util::selectorFromSignatureU32("Error(string)") << Instruction::EQ;
10501050
m_context << Instruction::ISZERO;
10511051
m_context.appendConditionalJumpTo(panicTag);
10521052
m_context << Instruction::POP; // remove selector
@@ -1078,7 +1078,7 @@ void ContractCompiler::handleCatch(vector<ASTPointer<TryCatchClause>> const& _ca
10781078
solAssert(m_context.evmVersion().supportsReturndata(), "");
10791079

10801080
// stack: <selector>
1081-
m_context << util::selectorFromSignature32("Panic(uint256)") << Instruction::EQ;
1081+
m_context << util::selectorFromSignatureU32("Panic(uint256)") << Instruction::EQ;
10821082
m_context << Instruction::ISZERO;
10831083
m_context.appendConditionalJumpTo(fallbackTag);
10841084

libsolidity/codegen/ExpressionCompiler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1329,7 +1329,7 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall)
13291329
// hash the signature
13301330
if (auto const* stringType = dynamic_cast<StringLiteralType const*>(selectorType))
13311331
{
1332-
m_context << util::selectorFromSignature(stringType->value());
1332+
m_context << util::selectorFromSignatureU256(stringType->value());
13331333
dataOnStack = TypeProvider::fixedBytes(4);
13341334
}
13351335
else

libsolidity/codegen/YulUtilFunctions.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ string YulUtilFunctions::requireOrAssertFunction(bool _assert, Type const* _mess
233233
.render();
234234

235235
int const hashHeaderSize = 4;
236-
u256 const errorHash = util::selectorFromSignature("Error(string)");
236+
u256 const errorHash = util::selectorFromSignatureU256("Error(string)");
237237

238238
string const encodeFunc = ABIFunctions(m_evmVersion, m_revertStrings, m_functionCollector)
239239
.tupleEncoder(
@@ -4426,7 +4426,7 @@ string YulUtilFunctions::revertReasonIfDebugBody(
44264426
revert(start, <overallLength>)
44274427
)");
44284428
templ("allocate", _allocation);
4429-
templ("sig", util::selectorFromSignature("Error(string)").str());
4429+
templ("sig", util::selectorFromSignatureU256("Error(string)").str());
44304430
templ("length", to_string(_message.length()));
44314431

44324432
size_t words = (_message.length() + 31) / 32;
@@ -4454,7 +4454,7 @@ string YulUtilFunctions::panicFunction(util::PanicCode _code)
44544454
}
44554455
)")
44564456
("functionName", functionName)
4457-
("selector", util::selectorFromSignature("Panic(uint256)").str())
4457+
("selector", util::selectorFromSignatureU256("Panic(uint256)").str())
44584458
("code", toCompactHexWithPrefix(static_cast<unsigned>(_code)))
44594459
.render();
44604460
});

libsolidity/codegen/ir/IRGeneratorForStatements.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,7 +1196,7 @@ void IRGeneratorForStatements::endVisit(FunctionCall const& _functionCall)
11961196
// hash the signature
11971197
Type const& selectorType = type(*arguments.front());
11981198
if (auto const* stringType = dynamic_cast<StringLiteralType const*>(&selectorType))
1199-
selector = formatNumber(util::selectorFromSignature(stringType->value()));
1199+
selector = formatNumber(util::selectorFromSignatureU256(stringType->value()));
12001200
else
12011201
{
12021202
// Used to reset the free memory pointer later.
@@ -1785,7 +1785,7 @@ void IRGeneratorForStatements::endVisit(MemberAccess const& _memberAccess)
17851785
""
17861786
);
17871787
define(IRVariable{_memberAccess}) << formatNumber(
1788-
util::selectorFromSignature(functionType.externalSignature())
1788+
util::selectorFromSignatureU256(functionType.externalSignature())
17891789
) << "\n";
17901790
}
17911791
else if (functionType.kind() == FunctionType::Kind::Event)
@@ -3234,7 +3234,7 @@ void IRGeneratorForStatements::handleCatch(TryStatement const& _tryStatement)
32343234

32353235
if (TryCatchClause const* errorClause = _tryStatement.errorClause())
32363236
{
3237-
appendCode() << "case " << selectorFromSignature32("Error(string)") << " {\n";
3237+
appendCode() << "case " << selectorFromSignatureU32("Error(string)") << " {\n";
32383238
setLocation(*errorClause);
32393239
string const dataVariable = m_context.newYulVariable();
32403240
appendCode() << "let " << dataVariable << " := " << m_utils.tryDecodeErrorMessageFunction() << "()\n";
@@ -3254,7 +3254,7 @@ void IRGeneratorForStatements::handleCatch(TryStatement const& _tryStatement)
32543254
}
32553255
if (TryCatchClause const* panicClause = _tryStatement.panicClause())
32563256
{
3257-
appendCode() << "case " << selectorFromSignature32("Panic(uint256)") << " {\n";
3257+
appendCode() << "case " << selectorFromSignatureU32("Panic(uint256)") << " {\n";
32583258
setLocation(*panicClause);
32593259
string const success = m_context.newYulVariable();
32603260
string const code = m_context.newYulVariable();
@@ -3317,7 +3317,7 @@ void IRGeneratorForStatements::revertWithError(
33173317
})");
33183318
templ("pos", m_context.newYulVariable());
33193319
templ("end", m_context.newYulVariable());
3320-
templ("hash", util::selectorFromSignature(_signature).str());
3320+
templ("hash", util::selectorFromSignatureU256(_signature).str());
33213321
templ("allocateUnbounded", m_utils.allocateUnboundedFunction());
33223322

33233323
vector<string> errorArgumentVars;

0 commit comments

Comments
 (0)