mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge pull request #10012 from ethereum/extractSelectorFunction
Extract function to compute function selector from signature.
This commit is contained in:
commit
abfa136afb
@ -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
|
||||
|
@ -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;
|
||||
|
@ -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);
|
||||
|
@ -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
|
||||
|
@ -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",
|
||||
|
@ -10,6 +10,7 @@ set(sources
|
||||
Exceptions.cpp
|
||||
Exceptions.h
|
||||
FixedHash.h
|
||||
FunctionSelector.h
|
||||
IndentedWriter.cpp
|
||||
IndentedWriter.h
|
||||
InvertibleMap.h
|
||||
|
42
libsolutil/FunctionSelector.h
Normal file
42
libsolutil/FunctionSelector.h
Normal file
@ -0,0 +1,42 @@
|
||||
/*
|
||||
This file is part of solidity.
|
||||
|
||||
solidity is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
solidity is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
// SPDX-License-Identifier: GPL-3.0
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <libsolutil/Keccak256.h>
|
||||
#include <libsolutil/FixedHash.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace solidity::util
|
||||
{
|
||||
|
||||
/// @returns the ABI selector for a given function signature, as a 32 bit number.
|
||||
inline uint32_t selectorFromSignature32(std::string const& _signature)
|
||||
{
|
||||
return uint32_t(FixedHash<4>::Arith(util::FixedHash<4>(util::keccak256(_signature))));
|
||||
}
|
||||
|
||||
/// @returns the ABI selector for a given function signature, as a u256 (left aligned) number.
|
||||
inline u256 selectorFromSignature(std::string const& _signature)
|
||||
{
|
||||
return u256(selectorFromSignature32(_signature)) << (256 - 32);
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user