Skip to content

Commit 5539a74

Browse files
authored
Merge pull request #12548 from joshieDo/other-signatures
Add event and error identifiers to --hashes
2 parents 8510362 + 46075d0 commit 5539a74

File tree

17 files changed

+142
-25
lines changed

17 files changed

+142
-25
lines changed

Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Language Features:
66

77

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

libsolidity/ast/AST.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ FunctionDefinition const* ContractDefinition::receiveFunction() const
192192
return nullptr;
193193
}
194194

195-
vector<EventDefinition const*> const& ContractDefinition::interfaceEvents() const
195+
vector<EventDefinition const*> const& ContractDefinition::definedInterfaceEvents() const
196196
{
197197
return m_interfaceEvents.init([&]{
198198
set<string> eventsSeen;
@@ -213,11 +213,20 @@ vector<EventDefinition const*> const& ContractDefinition::interfaceEvents() cons
213213
interfaceEvents.push_back(e);
214214
}
215215
}
216-
217216
return interfaceEvents;
218217
});
219218
}
220219

220+
vector<EventDefinition const*> const ContractDefinition::usedInterfaceEvents() const
221+
{
222+
solAssert(annotation().creationCallGraph.set(), "");
223+
224+
return convertContainer<std::vector<EventDefinition const*>>(
225+
(*annotation().creationCallGraph)->emittedEvents +
226+
(*annotation().deployedCallGraph)->emittedEvents
227+
);
228+
}
229+
221230
vector<ErrorDefinition const*> ContractDefinition::interfaceErrors(bool _requireCallGraph) const
222231
{
223232
set<ErrorDefinition const*, CompareByID> result;
@@ -227,10 +236,9 @@ vector<ErrorDefinition const*> ContractDefinition::interfaceErrors(bool _require
227236
if (_requireCallGraph)
228237
solAssert(annotation().creationCallGraph.set(), "");
229238
if (annotation().creationCallGraph.set())
230-
{
231-
result += (*annotation().creationCallGraph)->usedErrors;
232-
result += (*annotation().deployedCallGraph)->usedErrors;
233-
}
239+
result +=
240+
(*annotation().creationCallGraph)->usedErrors +
241+
(*annotation().deployedCallGraph)->usedErrors;
234242
return convertContainer<vector<ErrorDefinition const*>>(move(result));
235243
}
236244

libsolidity/ast/AST.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,8 @@ class ContractDefinition: public Declaration, public StructurallyDocumented, pub
519519
return ranges::subrange<decltype(b)>(b, e) | ranges::views::values;
520520
}
521521
std::vector<EventDefinition const*> events() const { return filteredNodes<EventDefinition>(m_subNodes); }
522-
std::vector<EventDefinition const*> const& interfaceEvents() const;
522+
std::vector<EventDefinition const*> const& definedInterfaceEvents() const;
523+
std::vector<EventDefinition const*> const usedInterfaceEvents() const;
523524
/// @returns all errors defined in this contract or any base contract
524525
/// and all errors referenced during execution.
525526
/// @param _requireCallGraph if false, do not fail if the call graph has not been computed yet.

libsolidity/interface/ABI.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ Json::Value ABI::generate(ContractDefinition const& _contractDef)
101101
method["stateMutability"] = stateMutabilityToString(externalFunctionType->stateMutability());
102102
abi.emplace(std::move(method));
103103
}
104-
for (auto const& it: _contractDef.interfaceEvents())
104+
for (auto const& it: _contractDef.definedInterfaceEvents())
105105
{
106106
Json::Value event{Json::objectValue};
107107
event["type"] = "event";

libsolidity/interface/CompilerStack.cpp

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,14 @@
7575
#include <libsolutil/IpfsHash.h>
7676
#include <libsolutil/JSON.h>
7777
#include <libsolutil/Algorithms.h>
78+
#include <libsolutil/FunctionSelector.h>
7879

7980
#include <json/json.h>
8081

8182
#include <boost/algorithm/string/replace.hpp>
8283

84+
#include <range/v3/view/concat.hpp>
85+
8386
#include <utility>
8487
#include <map>
8588
#include <limits>
@@ -1013,15 +1016,34 @@ Json::Value const& CompilerStack::natspecDev(Contract const& _contract) const
10131016
return _contract.devDocumentation.init([&]{ return Natspec::devDocumentation(*_contract.contract); });
10141017
}
10151018

1016-
Json::Value CompilerStack::methodIdentifiers(string const& _contractName) const
1019+
Json::Value CompilerStack::interfaceSymbols(string const& _contractName) const
10171020
{
10181021
if (m_stackState < AnalysisPerformed)
10191022
solThrow(CompilerError, "Analysis was not successful.");
10201023

1021-
Json::Value methodIdentifiers(Json::objectValue);
1024+
Json::Value interfaceSymbols(Json::objectValue);
1025+
// Always have a methods object
1026+
interfaceSymbols["methods"] = Json::objectValue;
1027+
10221028
for (auto const& it: contractDefinition(_contractName).interfaceFunctions())
1023-
methodIdentifiers[it.second->externalSignature()] = it.first.hex();
1024-
return methodIdentifiers;
1029+
interfaceSymbols["methods"][it.second->externalSignature()] = it.first.hex();
1030+
for (ErrorDefinition const* error: contractDefinition(_contractName).interfaceErrors())
1031+
{
1032+
string signature = error->functionType(true)->externalSignature();
1033+
interfaceSymbols["errors"][signature] = toHex(toCompactBigEndian(selectorFromSignature32(signature), 4));
1034+
}
1035+
1036+
for (EventDefinition const* event: ranges::concat_view(
1037+
contractDefinition(_contractName).definedInterfaceEvents(),
1038+
contractDefinition(_contractName).usedInterfaceEvents()
1039+
))
1040+
if (!event->isAnonymous())
1041+
{
1042+
string signature = event->functionType(true)->externalSignature();
1043+
interfaceSymbols["events"][signature] = toHex(u256(h256::Arith(keccak256(signature))));
1044+
}
1045+
1046+
return interfaceSymbols;
10251047
}
10261048

10271049
bytes CompilerStack::cborMetadata(string const& _contractName, bool _forIR) const

libsolidity/interface/CompilerStack.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,8 @@ class CompilerStack: public langutil::CharStreamProvider
327327
/// Prerequisite: Successful call to parse or compile.
328328
Json::Value const& natspecDev(std::string const& _contractName) const;
329329

330-
/// @returns a JSON representing a map of method identifiers (hashes) to function names.
331-
Json::Value methodIdentifiers(std::string const& _contractName) const;
330+
/// @returns a JSON object with the three members ``methods``, ``events``, ``errors``. Each is a map, mapping identifiers (hashes) to function names.
331+
Json::Value interfaceSymbols(std::string const& _contractName) const;
332332

333333
/// @returns the Contract Metadata matching the pipeline selected using the viaIR setting.
334334
std::string const& metadata(std::string const& _contractName) const { return metadata(contract(_contractName)); }

libsolidity/interface/Natspec.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ Json::Value Natspec::userDocumentation(ContractDefinition const& _contractDef)
7878
doc["methods"][it.second->externalSignature()]["notice"] = value;
7979
}
8080

81-
for (auto const& event: _contractDef.interfaceEvents())
81+
for (auto const& event: _contractDef.definedInterfaceEvents())
8282
{
8383
string value = extractDoc(event->annotation().docTags, "notice");
8484
if (!value.empty())

libsolidity/interface/StandardCompiler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1298,7 +1298,7 @@ Json::Value StandardCompiler::compileSolidity(StandardCompiler::InputsAndSetting
12981298
if (compilationSuccess && isArtifactRequested(_inputsAndSettings.outputSelection, file, name, "evm.legacyAssembly", wildcardMatchesExperimental))
12991299
evmData["legacyAssembly"] = compilerStack.assemblyJSON(contractName);
13001300
if (isArtifactRequested(_inputsAndSettings.outputSelection, file, name, "evm.methodIdentifiers", wildcardMatchesExperimental))
1301-
evmData["methodIdentifiers"] = compilerStack.methodIdentifiers(contractName);
1301+
evmData["methodIdentifiers"] = compilerStack.interfaceSymbols(contractName)["methods"];
13021302
if (compilationSuccess && isArtifactRequested(_inputsAndSettings.outputSelection, file, name, "evm.gasEstimates", wildcardMatchesExperimental))
13031303
evmData["gasEstimates"] = compilerStack.gasEstimates(contractName);
13041304

solc/CommandLineInterface.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -270,15 +270,29 @@ void CommandLineInterface::handleSignatureHashes(string const& _contract)
270270
if (!m_options.compiler.outputs.signatureHashes)
271271
return;
272272

273-
Json::Value methodIdentifiers = m_compiler->methodIdentifiers(_contract);
274-
string out;
275-
for (auto const& name: methodIdentifiers.getMemberNames())
276-
out += methodIdentifiers[name].asString() + ": " + name + "\n";
273+
Json::Value interfaceSymbols = m_compiler->interfaceSymbols(_contract);
274+
string out = "Function signatures:\n";
275+
for (auto const& name: interfaceSymbols["methods"].getMemberNames())
276+
out += interfaceSymbols["methods"][name].asString() + ": " + name + "\n";
277+
278+
if (interfaceSymbols.isMember("errors"))
279+
{
280+
out += "\nError signatures:\n";
281+
for (auto const& name: interfaceSymbols["errors"].getMemberNames())
282+
out += interfaceSymbols["errors"][name].asString() + ": " + name + "\n";
283+
}
284+
285+
if (interfaceSymbols.isMember("events"))
286+
{
287+
out += "\nEvent signatures:\n";
288+
for (auto const& name: interfaceSymbols["events"].getMemberNames())
289+
out += interfaceSymbols["events"][name].asString() + ": " + name + "\n";
290+
}
277291

278292
if (!m_options.output.dir.empty())
279293
createFile(m_compiler->filesystemFriendlyName(_contract) + ".signatures", out);
280294
else
281-
sout() << "Function signatures:" << endl << out;
295+
sout() << out;
282296
}
283297

284298
void CommandLineInterface::handleMetadata(string const& _contract)
@@ -822,7 +836,7 @@ void CommandLineInterface::handleCombinedJSON()
822836
m_compiler->runtimeObject(contractName).functionDebugData
823837
);
824838
if (m_options.compiler.combinedJsonRequests->signatureHashes)
825-
contractData[g_strSignatureHashes] = m_compiler->methodIdentifiers(contractName);
839+
contractData[g_strSignatureHashes] = m_compiler->interfaceSymbols(contractName)["methods"];
826840
if (m_options.compiler.combinedJsonRequests->natspecDev)
827841
contractData[g_strNatspecDev] = m_compiler->natspecDev(contractName);
828842
if (m_options.compiler.combinedJsonRequests->natspecUser)

test/cmdlineTests/hashes/args

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--hashes

0 commit comments

Comments
 (0)