Merge pull request #10012 from ethereum/extractSelectorFunction

Extract function to compute function selector from signature.
This commit is contained in:
chriseth
2020-10-12 20:32:05 +02:00
committed by GitHub
8 changed files with 57 additions and 22 deletions
+2 -1
View File
@@ -29,6 +29,7 @@
#include <libsolutil/Algorithms.h>
#include <libsolutil/CommonData.h>
#include <libsolutil/CommonIO.h>
#include <libsolutil/FunctionSelector.h>
#include <libsolutil/Keccak256.h>
#include <libsolutil/UTF8.h>
@@ -3591,7 +3592,7 @@ string FunctionType::externalSignature() const
u256 FunctionType::externalIdentifier() const
{
return util::FixedHash<4>::Arith(util::FixedHash<4>(util::keccak256(externalSignature())));
return util::selectorFromSignature32(externalSignature());
}
string FunctionType::externalIdentifierHex() const
+4 -3
View File
@@ -28,6 +28,7 @@
#include <libsolidity/codegen/ABIFunctions.h>
#include <libsolidity/codegen/ArrayUtils.h>
#include <libsolidity/codegen/LValue.h>
#include <libsolutil/FunctionSelector.h>
#include <libevmasm/Instruction.h>
#include <libsolutil/Whiskers.h>
@@ -93,7 +94,7 @@ void CompilerUtils::revertWithStringData(Type const& _argumentType)
{
solAssert(_argumentType.isImplicitlyConvertibleTo(*TypeProvider::fromElementaryTypeName("string memory")), "");
fetchFreeMemoryPointer();
m_context << (u256(util::FixedHash<4>::Arith(util::FixedHash<4>(util::keccak256("Error(string)")))) << (256 - 32));
m_context << util::selectorFromSignature("Error(string)");
m_context << Instruction::DUP2 << Instruction::MSTORE;
m_context << u256(4) << Instruction::ADD;
// Stack: <string data> <mem pos of encoding start>
@@ -916,7 +917,7 @@ void CompilerUtils::convertType(
{
unsigned const numBytes = dynamic_cast<FixedBytesType const&>(_targetType).numBytes();
solAssert(data.size() <= 32, "");
m_context << (h256::Arith(h256(data, h256::AlignLeft)) & (~(u256(-1) >> (8 * numBytes))));
m_context << (u256(h256(data, h256::AlignLeft)) & (~(u256(-1) >> (8 * numBytes))));
}
else if (targetTypeCategory == Type::Category::Array)
{
@@ -1421,7 +1422,7 @@ void CompilerUtils::storeStringData(bytesConstRef _data)
{
for (unsigned i = 0; i < _data.size(); i += 32)
{
m_context << h256::Arith(h256(_data.cropped(i), h256::AlignLeft));
m_context << u256(h256(_data.cropped(i), h256::AlignLeft));
storeInMemoryDynamic(*TypeProvider::uint256());
}
m_context << Instruction::POP;
-2
View File
@@ -953,8 +953,6 @@ void ContractCompiler::handleCatch(vector<ASTPointer<TryCatchClause>> const& _ca
);
solAssert(m_context.evmVersion().supportsReturndata(), "");
string errorHash = FixedHash<4>(util::keccak256("Error(string)")).hex();
// Try to decode the error message.
// If this fails, leaves 0 on the stack, otherwise the pointer to the data string.
m_context.callYulFunction(m_context.utilFunctions().tryDecodeErrorMessageFunction(), 0, 1);
+2 -2
View File
@@ -33,6 +33,7 @@
#include <libevmasm/GasMeter.h>
#include <libsolutil/Common.h>
#include <libsolutil/FunctionSelector.h>
#include <libsolutil/Keccak256.h>
#include <libsolutil/Whiskers.h>
@@ -1187,8 +1188,7 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall)
// hash the signature
if (auto const* stringType = dynamic_cast<StringLiteralType const*>(selectorType))
{
FixedHash<4> hash(keccak256(stringType->value()));
m_context << (u256(FixedHash<4>::Arith(hash)) << (256 - 32));
m_context << util::selectorFromSignature(stringType->value());
dataOnStack = TypeProvider::fixedBytes(4);
}
else
+3 -6
View File
@@ -26,6 +26,7 @@
#include <libsolidity/codegen/CompilerUtils.h>
#include <libsolutil/CommonData.h>
#include <libsolutil/FunctionSelector.h>
#include <libsolutil/Whiskers.h>
#include <libsolutil/StringUtils.h>
@@ -126,11 +127,7 @@ string YulUtilFunctions::requireOrAssertFunction(bool _assert, Type const* _mess
.render();
int const hashHeaderSize = 4;
int const byteSize = 8;
u256 const errorHash =
u256(FixedHash<hashHeaderSize>::Arith(
FixedHash<hashHeaderSize>(keccak256("Error(string)"))
)) << (256 - hashHeaderSize * byteSize);
u256 const errorHash = util::selectorFromSignature("Error(string)");
string const encodeFunc = ABIFunctions(m_evmVersion, m_revertStrings, m_functionCollector)
.tupleEncoder(
@@ -3192,7 +3189,7 @@ string YulUtilFunctions::revertReasonIfDebug(RevertStrings revertStrings, string
</word>
revert(0, add(reasonPos, <end>))
})");
templ("sig", (u256(util::FixedHash<4>::Arith(util::FixedHash<4>(util::keccak256("Error(string)")))) << (256 - 32)).str());
templ("sig", util::selectorFromSignature("Error(string)").str());
templ("length", to_string(_message.length()));
size_t words = (_message.length() + 31) / 32;
@@ -43,6 +43,7 @@
#include <libsolutil/Whiskers.h>
#include <libsolutil/StringUtils.h>
#include <libsolutil/Keccak256.h>
#include <libsolutil/FunctionSelector.h>
#include <libsolutil/Visitor.h>
#include <boost/range/adaptor/transformed.hpp>
@@ -1037,10 +1038,7 @@ void IRGeneratorForStatements::endVisit(FunctionCall const& _functionCall)
// hash the signature
Type const& selectorType = type(*arguments.front());
if (auto const* stringType = dynamic_cast<StringLiteralType const*>(&selectorType))
{
FixedHash<4> hash(keccak256(stringType->value()));
selector = formatNumber(u256(FixedHash<4>::Arith(hash)) << (256 - 32));
}
selector = formatNumber(util::selectorFromSignature(stringType->value()));
else
{
// Used to reset the free memory pointer later.
@@ -1157,10 +1155,7 @@ void IRGeneratorForStatements::endVisit(FunctionCall const& _functionCall)
})");
templ("pos", m_context.newYulVariable());
templ("end", m_context.newYulVariable());
templ(
"hash",
(u256(util::FixedHash<4>::Arith(util::FixedHash<4>(util::keccak256("Error(string)")))) << (256 - 32)).str()
);
templ("hash", util::selectorFromSignature("Error(string)").str());
templ("allocateTemporary", m_utils.allocationTemporaryMemoryFunction());
templ(
"argumentVars",