Merge pull request #12548 from joshieDo/other-signatures

Add event and error identifiers to --hashes
This commit is contained in:
chriseth 2022-02-09 16:06:57 +01:00 committed by GitHub
commit 5539a745df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 142 additions and 25 deletions

View File

@ -6,6 +6,7 @@ Language Features:
Compiler Features:
* Commandline Interface: Event and error signatures are also returned when using ``--hashes``.
* Yul Optimizer: Remove ``mstore`` and ``sstore`` operations if the slot already contains the same value.
* Yul: Emit immutable references for pure yul code when requested.

View File

@ -192,7 +192,7 @@ FunctionDefinition const* ContractDefinition::receiveFunction() const
return nullptr;
}
vector<EventDefinition const*> const& ContractDefinition::interfaceEvents() const
vector<EventDefinition const*> const& ContractDefinition::definedInterfaceEvents() const
{
return m_interfaceEvents.init([&]{
set<string> eventsSeen;
@ -213,11 +213,20 @@ vector<EventDefinition const*> const& ContractDefinition::interfaceEvents() cons
interfaceEvents.push_back(e);
}
}
return interfaceEvents;
});
}
vector<EventDefinition const*> const ContractDefinition::usedInterfaceEvents() const
{
solAssert(annotation().creationCallGraph.set(), "");
return convertContainer<std::vector<EventDefinition const*>>(
(*annotation().creationCallGraph)->emittedEvents +
(*annotation().deployedCallGraph)->emittedEvents
);
}
vector<ErrorDefinition const*> ContractDefinition::interfaceErrors(bool _requireCallGraph) const
{
set<ErrorDefinition const*, CompareByID> result;
@ -227,10 +236,9 @@ vector<ErrorDefinition const*> ContractDefinition::interfaceErrors(bool _require
if (_requireCallGraph)
solAssert(annotation().creationCallGraph.set(), "");
if (annotation().creationCallGraph.set())
{
result += (*annotation().creationCallGraph)->usedErrors;
result += (*annotation().deployedCallGraph)->usedErrors;
}
result +=
(*annotation().creationCallGraph)->usedErrors +
(*annotation().deployedCallGraph)->usedErrors;
return convertContainer<vector<ErrorDefinition const*>>(move(result));
}

View File

@ -519,7 +519,8 @@ public:
return ranges::subrange<decltype(b)>(b, e) | ranges::views::values;
}
std::vector<EventDefinition const*> events() const { return filteredNodes<EventDefinition>(m_subNodes); }
std::vector<EventDefinition const*> const& interfaceEvents() const;
std::vector<EventDefinition const*> const& definedInterfaceEvents() const;
std::vector<EventDefinition const*> const usedInterfaceEvents() const;
/// @returns all errors defined in this contract or any base contract
/// and all errors referenced during execution.
/// @param _requireCallGraph if false, do not fail if the call graph has not been computed yet.

View File

@ -101,7 +101,7 @@ Json::Value ABI::generate(ContractDefinition const& _contractDef)
method["stateMutability"] = stateMutabilityToString(externalFunctionType->stateMutability());
abi.emplace(std::move(method));
}
for (auto const& it: _contractDef.interfaceEvents())
for (auto const& it: _contractDef.definedInterfaceEvents())
{
Json::Value event{Json::objectValue};
event["type"] = "event";

View File

@ -75,11 +75,14 @@
#include <libsolutil/IpfsHash.h>
#include <libsolutil/JSON.h>
#include <libsolutil/Algorithms.h>
#include <libsolutil/FunctionSelector.h>
#include <json/json.h>
#include <boost/algorithm/string/replace.hpp>
#include <range/v3/view/concat.hpp>
#include <utility>
#include <map>
#include <limits>
@ -1013,15 +1016,34 @@ Json::Value const& CompilerStack::natspecDev(Contract const& _contract) const
return _contract.devDocumentation.init([&]{ return Natspec::devDocumentation(*_contract.contract); });
}
Json::Value CompilerStack::methodIdentifiers(string const& _contractName) const
Json::Value CompilerStack::interfaceSymbols(string const& _contractName) const
{
if (m_stackState < AnalysisPerformed)
solThrow(CompilerError, "Analysis was not successful.");
Json::Value methodIdentifiers(Json::objectValue);
Json::Value interfaceSymbols(Json::objectValue);
// Always have a methods object
interfaceSymbols["methods"] = Json::objectValue;
for (auto const& it: contractDefinition(_contractName).interfaceFunctions())
methodIdentifiers[it.second->externalSignature()] = it.first.hex();
return methodIdentifiers;
interfaceSymbols["methods"][it.second->externalSignature()] = it.first.hex();
for (ErrorDefinition const* error: contractDefinition(_contractName).interfaceErrors())
{
string signature = error->functionType(true)->externalSignature();
interfaceSymbols["errors"][signature] = toHex(toCompactBigEndian(selectorFromSignature32(signature), 4));
}
for (EventDefinition const* event: ranges::concat_view(
contractDefinition(_contractName).definedInterfaceEvents(),
contractDefinition(_contractName).usedInterfaceEvents()
))
if (!event->isAnonymous())
{
string signature = event->functionType(true)->externalSignature();
interfaceSymbols["events"][signature] = toHex(u256(h256::Arith(keccak256(signature))));
}
return interfaceSymbols;
}
bytes CompilerStack::cborMetadata(string const& _contractName, bool _forIR) const

View File

@ -327,8 +327,8 @@ public:
/// Prerequisite: Successful call to parse or compile.
Json::Value const& natspecDev(std::string const& _contractName) const;
/// @returns a JSON representing a map of method identifiers (hashes) to function names.
Json::Value methodIdentifiers(std::string const& _contractName) const;
/// @returns a JSON object with the three members ``methods``, ``events``, ``errors``. Each is a map, mapping identifiers (hashes) to function names.
Json::Value interfaceSymbols(std::string const& _contractName) const;
/// @returns the Contract Metadata matching the pipeline selected using the viaIR setting.
std::string const& metadata(std::string const& _contractName) const { return metadata(contract(_contractName)); }

View File

@ -78,7 +78,7 @@ Json::Value Natspec::userDocumentation(ContractDefinition const& _contractDef)
doc["methods"][it.second->externalSignature()]["notice"] = value;
}
for (auto const& event: _contractDef.interfaceEvents())
for (auto const& event: _contractDef.definedInterfaceEvents())
{
string value = extractDoc(event->annotation().docTags, "notice");
if (!value.empty())

View File

@ -1298,7 +1298,7 @@ Json::Value StandardCompiler::compileSolidity(StandardCompiler::InputsAndSetting
if (compilationSuccess && isArtifactRequested(_inputsAndSettings.outputSelection, file, name, "evm.legacyAssembly", wildcardMatchesExperimental))
evmData["legacyAssembly"] = compilerStack.assemblyJSON(contractName);
if (isArtifactRequested(_inputsAndSettings.outputSelection, file, name, "evm.methodIdentifiers", wildcardMatchesExperimental))
evmData["methodIdentifiers"] = compilerStack.methodIdentifiers(contractName);
evmData["methodIdentifiers"] = compilerStack.interfaceSymbols(contractName)["methods"];
if (compilationSuccess && isArtifactRequested(_inputsAndSettings.outputSelection, file, name, "evm.gasEstimates", wildcardMatchesExperimental))
evmData["gasEstimates"] = compilerStack.gasEstimates(contractName);

View File

@ -270,15 +270,29 @@ void CommandLineInterface::handleSignatureHashes(string const& _contract)
if (!m_options.compiler.outputs.signatureHashes)
return;
Json::Value methodIdentifiers = m_compiler->methodIdentifiers(_contract);
string out;
for (auto const& name: methodIdentifiers.getMemberNames())
out += methodIdentifiers[name].asString() + ": " + name + "\n";
Json::Value interfaceSymbols = m_compiler->interfaceSymbols(_contract);
string out = "Function signatures:\n";
for (auto const& name: interfaceSymbols["methods"].getMemberNames())
out += interfaceSymbols["methods"][name].asString() + ": " + name + "\n";
if (interfaceSymbols.isMember("errors"))
{
out += "\nError signatures:\n";
for (auto const& name: interfaceSymbols["errors"].getMemberNames())
out += interfaceSymbols["errors"][name].asString() + ": " + name + "\n";
}
if (interfaceSymbols.isMember("events"))
{
out += "\nEvent signatures:\n";
for (auto const& name: interfaceSymbols["events"].getMemberNames())
out += interfaceSymbols["events"][name].asString() + ": " + name + "\n";
}
if (!m_options.output.dir.empty())
createFile(m_compiler->filesystemFriendlyName(_contract) + ".signatures", out);
else
sout() << "Function signatures:" << endl << out;
sout() << out;
}
void CommandLineInterface::handleMetadata(string const& _contract)
@ -822,7 +836,7 @@ void CommandLineInterface::handleCombinedJSON()
m_compiler->runtimeObject(contractName).functionDebugData
);
if (m_options.compiler.combinedJsonRequests->signatureHashes)
contractData[g_strSignatureHashes] = m_compiler->methodIdentifiers(contractName);
contractData[g_strSignatureHashes] = m_compiler->interfaceSymbols(contractName)["methods"];
if (m_options.compiler.combinedJsonRequests->natspecDev)
contractData[g_strNatspecDev] = m_compiler->natspecDev(contractName);
if (m_options.compiler.combinedJsonRequests->natspecUser)

View File

@ -0,0 +1 @@
--hashes

View File

@ -0,0 +1,28 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.0;
error fileLevelError(uint z);
library L {
event libraryEvent(uint r);
error libraryError(uint r);
error libraryErrorUnused(uint u);
event libraryEventUnused(uint u);
}
contract C {
struct S { uint x; }
event ev(uint y);
event anon_ev(uint y) anonymous;
error err(uint z, uint w);
function f(S memory s) public {
emit L.libraryEvent(3);
if (s.x > 1)
revert fileLevelError(3);
else
revert L.libraryError(4);
}
}

View File

@ -0,0 +1,24 @@
======= hashes/input.sol:C =======
Function signatures:
3fc03eeb: f((uint256))
Error signatures:
619a0bb7: err(uint256,uint256)
82b5f64f: fileLevelError(uint256)
8c41f45c: libraryError(uint256)
Event signatures:
2d4dd5fe18ada5a020a9f5591539a8dc3010a5c074ba6a70e1c956659f02786a: ev(uint256)
81f3fb02f88d32d3bb08c80c9a622ca3b3223292f131c6ad049811f9a8a606dc: libraryEvent(uint256)
======= hashes/input.sol:L =======
Function signatures:
Error signatures:
8c41f45c: libraryError(uint256)
c61c03f5: libraryErrorUnused(uint256)
Event signatures:
81f3fb02f88d32d3bb08c80c9a622ca3b3223292f131c6ad049811f9a8a606dc: libraryEvent(uint256)
0a994ad3600197f16ffe1ea1101caea3174efe5ebd9ba9a75d6d5524c5de28cd: libraryEventUnused(uint256)

View File

@ -0,0 +1,17 @@
{
"language": "Solidity",
"sources":
{
"A":
{
"content": "// SPDX-License-Identifier: GPL-3.0\npragma solidity >=0.0; contract C { }"
}
},
"settings":
{
"outputSelection":
{
"*": { "*": ["evm.methodIdentifiers"] }
}
}
}

View File

@ -0,0 +1 @@
{"contracts":{"A":{"C":{"evm":{"methodIdentifiers":{}}}}},"sources":{"A":{"id":0}}}

View File

@ -435,7 +435,7 @@ TestCase::TestResult SemanticTest::runTest(
{
soltestAssert(
m_allowNonExistingFunctions ||
m_compiler.methodIdentifiers(m_compiler.lastContractName(m_sources.mainSourceFile)).isMember(test.call().signature),
m_compiler.interfaceSymbols(m_compiler.lastContractName(m_sources.mainSourceFile))["methods"].isMember(test.call().signature),
"The function " + test.call().signature + " is not known to the compiler"
);

View File

@ -58,7 +58,7 @@ optional<CompilerOutput> SolidityCompilationFramework::compileContract()
else
contractName = m_compilerInput.contractName;
evmasm::LinkerObject obj = m_compiler.object(contractName);
Json::Value methodIdentifiers = m_compiler.methodIdentifiers(contractName);
Json::Value methodIdentifiers = m_compiler.interfaceSymbols(contractName)["methods"];
return CompilerOutput{obj.bytecode, methodIdentifiers};
}
}

View File

@ -91,7 +91,7 @@ public:
/// @returns method identifiers in contract called @param _contractName.
Json::Value methodIdentifiers(std::string const& _contractName)
{
return m_compiler.methodIdentifiers(_contractName);
return m_compiler.interfaceSymbols(_contractName)["methods"];
}
/// @returns Compilation output comprising EVM bytecode and list of
/// method identifiers in contract if compilation is successful,