mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Improve FunctionSelector helpers
This commit is contained in:
@@ -342,7 +342,7 @@ BOOST_AUTO_TEST_CASE(external_function)
|
||||
BOTH_ENCODERS(
|
||||
compileAndRun(sourceCode);
|
||||
callContractFunction("f(uint256)", u256(0));
|
||||
string functionIdF = asString(m_contractAddress.ref()) + asString(FixedHash<4>(keccak256("f(uint256)")).ref());
|
||||
string functionIdF = asString(m_contractAddress.ref()) + asString(util::selectorFromSignatureH32("f(uint256)").ref());
|
||||
REQUIRE_LOG_DATA(encodeArgs(functionIdF, functionIdF));
|
||||
)
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@
|
||||
|
||||
#include <liblangutil/Scanner.h>
|
||||
|
||||
#include <libsolutil/Keccak256.h>
|
||||
#include <libsolutil/FunctionSelector.h>
|
||||
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
@@ -170,6 +170,5 @@ FunctionTypePointer AnalysisFramework::retrieveFunctionBySignature(
|
||||
std::string const& _signature
|
||||
)
|
||||
{
|
||||
FixedHash<4> hash(util::keccak256(_signature));
|
||||
return _contract.interfaceFunctions()[hash];
|
||||
return _contract.interfaceFunctions()[util::selectorFromSignatureH32(_signature)];
|
||||
}
|
||||
|
||||
@@ -78,7 +78,7 @@ public:
|
||||
{
|
||||
u256 gasUsed = 0;
|
||||
GasMeter::GasConsumption gas;
|
||||
util::FixedHash<4> hash(util::keccak256(_sig));
|
||||
util::FixedHash<4> hash = util::selectorFromSignatureH32(_sig);
|
||||
for (bytes const& arguments: _argumentVariants)
|
||||
{
|
||||
sendMessage(hash.asBytes() + arguments, false, 0);
|
||||
|
||||
@@ -1627,7 +1627,7 @@ BOOST_AUTO_TEST_CASE(bytes_from_calldata_to_memory)
|
||||
ALSO_VIA_YUL(
|
||||
DISABLE_EWASM_TESTRUN();
|
||||
compileAndRun(sourceCode);
|
||||
bytes calldata1 = FixedHash<4>(util::keccak256("f()")).asBytes() + bytes(61, 0x22) + bytes(12, 0x12);
|
||||
bytes calldata1 = util::selectorFromSignatureH32("f()").asBytes() + bytes(61, 0x22) + bytes(12, 0x12);
|
||||
sendMessage(calldata1, false);
|
||||
BOOST_CHECK(m_transactionSuccessful);
|
||||
BOOST_CHECK(m_output == encodeArgs(util::keccak256(bytes{'a', 'b', 'c'} + calldata1)));
|
||||
@@ -1872,8 +1872,8 @@ BOOST_AUTO_TEST_CASE(bytes_in_arguments)
|
||||
|
||||
compileAndRun(sourceCode);
|
||||
|
||||
string innercalldata1 = asString(FixedHash<4>(util::keccak256("f(uint256,uint256)")).asBytes() + encodeArgs(8, 9));
|
||||
string innercalldata2 = asString(FixedHash<4>(util::keccak256("g(uint256)")).asBytes() + encodeArgs(3));
|
||||
string innercalldata1 = asString(util::selectorFromSignatureH32("f(uint256,uint256)").asBytes() + encodeArgs(8, 9));
|
||||
string innercalldata2 = asString(util::selectorFromSignatureH32("g(uint256)").asBytes() + encodeArgs(3));
|
||||
bytes calldata = encodeArgs(
|
||||
12, 32 * 4, u256(32 * 4 + 32 + (innercalldata1.length() + 31) / 32 * 32), 13,
|
||||
u256(innercalldata1.length()), innercalldata1,
|
||||
@@ -2205,8 +2205,8 @@ BOOST_AUTO_TEST_CASE(calldata_struct_function_type)
|
||||
)";
|
||||
compileAndRun(sourceCode, 0, "C");
|
||||
|
||||
bytes fn_C_g = m_contractAddress.asBytes() + FixedHash<4>(util::keccak256("g(uint256)")).asBytes() + bytes(8,0);
|
||||
bytes fn_C_h = m_contractAddress.asBytes() + FixedHash<4>(util::keccak256("h(uint256)")).asBytes() + bytes(8,0);
|
||||
bytes fn_C_g = m_contractAddress.asBytes() + util::selectorFromSignatureH32("g(uint256)").asBytes() + bytes(8,0);
|
||||
bytes fn_C_h = m_contractAddress.asBytes() + util::selectorFromSignatureH32("h(uint256)").asBytes() + bytes(8,0);
|
||||
ABI_CHECK(callContractFunctionNoEncoding("f((function))", fn_C_g), encodeArgs(42 * 3));
|
||||
ABI_CHECK(callContractFunctionNoEncoding("f((function))", fn_C_h), encodeArgs(23));
|
||||
}
|
||||
@@ -3007,7 +3007,7 @@ BOOST_AUTO_TEST_CASE(receive_external_function_type)
|
||||
compileAndRun(sourceCode, 0, "C");
|
||||
ABI_CHECK(callContractFunction(
|
||||
"f(function)",
|
||||
m_contractAddress.asBytes() + FixedHash<4>(util::keccak256("g()")).asBytes() + bytes(32 - 4 - 20, 0)
|
||||
m_contractAddress.asBytes() + util::selectorFromSignatureH32("g()").asBytes() + bytes(32 - 4 - 20, 0)
|
||||
), encodeArgs(u256(7)));
|
||||
)
|
||||
}
|
||||
@@ -3026,7 +3026,7 @@ BOOST_AUTO_TEST_CASE(return_external_function_type)
|
||||
compileAndRun(sourceCode, 0, "C");
|
||||
ABI_CHECK(
|
||||
callContractFunction("f()"),
|
||||
m_contractAddress.asBytes() + FixedHash<4>(util::keccak256("g()")).asBytes() + bytes(32 - 4 - 20, 0)
|
||||
m_contractAddress.asBytes() + util::selectorFromSignatureH32("g()").asBytes() + bytes(32 - 4 - 20, 0)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -338,9 +338,9 @@ solidity::frontend::test::ParameterList ContractABIUtils::failureParameters(byte
|
||||
parameters.push_back(Parameter{bytes(), "", ABIType{ABIType::HexString, ABIType::AlignNone, 4}, FormatInfo{}});
|
||||
|
||||
uint64_t selector = fromBigEndian<uint64_t>(bytes{_bytes.begin(), _bytes.begin() + 4});
|
||||
if (selector == selectorFromSignature32("Panic(uint256)"))
|
||||
if (selector == selectorFromSignatureU32("Panic(uint256)"))
|
||||
parameters.push_back(Parameter{bytes(), "", ABIType{ABIType::Hex}, FormatInfo{}});
|
||||
else if (selector == selectorFromSignature32("Error(string)"))
|
||||
else if (selector == selectorFromSignatureU32("Error(string)"))
|
||||
{
|
||||
parameters.push_back(Parameter{bytes(), "", ABIType{ABIType::Hex}, FormatInfo{}});
|
||||
parameters.push_back(Parameter{bytes(), "", ABIType{ABIType::UnsignedDec}, FormatInfo{}});
|
||||
|
||||
Reference in New Issue
Block a user