Skip to content

Commit

Permalink
Change ABI sorting order.
Browse files Browse the repository at this point in the history
  • Loading branch information
chriseth committed Sep 3, 2019
1 parent dce5250 commit cf8bb6d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Expand Up @@ -5,6 +5,7 @@ Language Features:


Compiler Features:
* ABI Output: Change sorting order of functions from selector to kind, name.
* Optimizer: Add rule that replaces the BYTE opcode by 0 if the first argument is larger than 31.
* Yul Optimizer: Take side-effect-freeness of user-defined functions into account.

Expand Down
20 changes: 14 additions & 6 deletions libsolidity/interface/ABI.cpp
Expand Up @@ -40,7 +40,7 @@ bool anyDataStoredInStorage(TypePointers const& _pointers)

Json::Value ABI::generate(ContractDefinition const& _contractDef)
{
Json::Value abi(Json::arrayValue);
vector<Json::Value> abi;

for (auto it: _contractDef.interfaceFunctions())
{
Expand Down Expand Up @@ -71,7 +71,7 @@ Json::Value ABI::generate(ContractDefinition const& _contractDef)
it.second->returnParameterTypes(),
_contractDef.isLibrary()
);
abi.append(std::move(method));
abi.emplace_back(std::move(method));
}
if (_contractDef.constructor())
{
Expand All @@ -88,7 +88,7 @@ Json::Value ABI::generate(ContractDefinition const& _contractDef)
constrType.parameterTypes(),
_contractDef.isLibrary()
);
abi.append(std::move(method));
abi.emplace_back(std::move(method));
}
if (_contractDef.fallbackFunction())
{
Expand All @@ -98,7 +98,7 @@ Json::Value ABI::generate(ContractDefinition const& _contractDef)
method["type"] = "fallback";
method["payable"] = externalFunctionType->isPayable();
method["stateMutability"] = stateMutabilityToString(externalFunctionType->stateMutability());
abi.append(std::move(method));
abi.emplace_back(std::move(method));
}
for (auto const& it: _contractDef.interfaceEvents())
{
Expand All @@ -117,10 +117,18 @@ Json::Value ABI::generate(ContractDefinition const& _contractDef)
params.append(std::move(param));
}
event["inputs"] = std::move(params);
abi.append(std::move(event));
abi.emplace_back(std::move(event));
}

return abi;
// If we use stable_sort, functions of the same name will be sorted by selector.
stable_sort(abi.begin(), abi.end(), [](Json::Value const& _a, Json::Value const& _b) -> bool {
return make_tuple(_a["type"], _a["name"]) < make_tuple(_b["type"], _b["name"]);
});

Json::Value abiJson{Json::arrayValue};
for (auto& f: abi)
abiJson.emplace_back(std::move(f));
return abiJson;
}

Json::Value ABI::formatTypeList(
Expand Down

0 comments on commit cf8bb6d

Please sign in to comment.