Additional type info for ABI.

This commit is contained in:
chriseth 2019-07-02 16:04:52 +02:00
parent 15cba9163e
commit a30be56c27
24 changed files with 165 additions and 36 deletions

View File

@ -8,6 +8,7 @@ Language Features:
Compiler Features:
* ABI: Additional internal type info in the field ``internalType``.
* eWasm: Highly experimental eWasm output using ``--ewasm`` in the commandline interface or output selection of ``ewasm.wast`` in standard-json.
* Metadata: Update the swarm hash, changes ``bzzr0`` to ``bzzr1`` and urls to use ``bzz-raw://``.
* Standard JSON Interface: Compile only selected sources and contracts.

View File

@ -44,14 +44,13 @@ Json::Value ABI::generate(ContractDefinition const& _contractDef)
for (auto it: _contractDef.interfaceFunctions())
{
if (
_contractDef.isLibrary() &&
(it.second->stateMutability() > StateMutability::View ||
anyDataStoredInStorage(it.second->parameterTypes() + it.second->returnParameterTypes()))
)
if (_contractDef.isLibrary() && (
it.second->stateMutability() > StateMutability::View ||
anyDataStoredInStorage(it.second->parameterTypes() + it.second->returnParameterTypes())
))
continue;
auto externalFunctionType = it.second->interfaceFunctionType();
FunctionType const* externalFunctionType = it.second->interfaceFunctionType();
solAssert(!!externalFunctionType, "");
Json::Value method;
method["type"] = "function";
@ -63,18 +62,21 @@ Json::Value ABI::generate(ContractDefinition const& _contractDef)
method["inputs"] = formatTypeList(
externalFunctionType->parameterNames(),
externalFunctionType->parameterTypes(),
it.second->parameterTypes(),
_contractDef.isLibrary()
);
method["outputs"] = formatTypeList(
externalFunctionType->returnParameterNames(),
externalFunctionType->returnParameterTypes(),
it.second->returnParameterTypes(),
_contractDef.isLibrary()
);
abi.append(method);
abi.append(std::move(method));
}
if (_contractDef.constructor())
{
auto externalFunctionType = FunctionType(*_contractDef.constructor(), false).interfaceFunctionType();
FunctionType constrType(*_contractDef.constructor(), false);
FunctionType const* externalFunctionType = constrType.interfaceFunctionType();
solAssert(!!externalFunctionType, "");
Json::Value method;
method["type"] = "constructor";
@ -83,19 +85,20 @@ Json::Value ABI::generate(ContractDefinition const& _contractDef)
method["inputs"] = formatTypeList(
externalFunctionType->parameterNames(),
externalFunctionType->parameterTypes(),
constrType.parameterTypes(),
_contractDef.isLibrary()
);
abi.append(method);
abi.append(std::move(method));
}
if (_contractDef.fallbackFunction())
{
auto externalFunctionType = FunctionType(*_contractDef.fallbackFunction(), false).interfaceFunctionType();
FunctionType const* externalFunctionType = FunctionType(*_contractDef.fallbackFunction(), false).interfaceFunctionType();
solAssert(!!externalFunctionType, "");
Json::Value method;
method["type"] = "fallback";
method["payable"] = externalFunctionType->isPayable();
method["stateMutability"] = stateMutabilityToString(externalFunctionType->stateMutability());
abi.append(method);
abi.append(std::move(method));
}
for (auto const& it: _contractDef.interfaceEvents())
{
@ -106,15 +109,15 @@ Json::Value ABI::generate(ContractDefinition const& _contractDef)
Json::Value params(Json::arrayValue);
for (auto const& p: it->parameters())
{
auto type = p->annotation().type->interfaceType(false);
solAssert(type.get(), "");
Type const* type = p->annotation().type->interfaceType(false);
solAssert(type, "");
Json::Value input;
auto param = formatType(p->name(), *type.get(), false);
auto param = formatType(p->name(), *type, *p->annotation().type, false);
param["indexed"] = p->isIndexed();
params.append(param);
params.append(std::move(param));
}
event["inputs"] = params;
abi.append(event);
event["inputs"] = std::move(params);
abi.append(std::move(event));
}
return abi;
@ -122,31 +125,39 @@ Json::Value ABI::generate(ContractDefinition const& _contractDef)
Json::Value ABI::formatTypeList(
vector<string> const& _names,
vector<TypePointer> const& _types,
vector<TypePointer> const& _encodingTypes,
vector<TypePointer> const& _solidityTypes,
bool _forLibrary
)
{
Json::Value params(Json::arrayValue);
solAssert(_names.size() == _types.size(), "Names and types vector size does not match");
solAssert(_names.size() == _encodingTypes.size(), "Names and types vector size does not match");
solAssert(_names.size() == _solidityTypes.size(), "");
for (unsigned i = 0; i < _names.size(); ++i)
{
solAssert(_types[i], "");
params.append(formatType(_names[i], *_types[i], _forLibrary));
solAssert(_encodingTypes[i], "");
params.append(formatType(_names[i], *_encodingTypes[i], *_solidityTypes[i], _forLibrary));
}
return params;
}
Json::Value ABI::formatType(string const& _name, Type const& _type, bool _forLibrary)
Json::Value ABI::formatType(
string const& _name,
Type const& _encodingType,
Type const& _solidityType,
bool _forLibrary
)
{
Json::Value ret;
ret["name"] = _name;
string suffix = (_forLibrary && _type.dataStoredIn(DataLocation::Storage)) ? " storage" : "";
if (_type.isValueType() || (_forLibrary && _type.dataStoredIn(DataLocation::Storage)))
ret["type"] = _type.canonicalName() + suffix;
else if (ArrayType const* arrayType = dynamic_cast<ArrayType const*>(&_type))
ret["internalType"] = _solidityType.toString(true);
string suffix = (_forLibrary && _encodingType.dataStoredIn(DataLocation::Storage)) ? " storage" : "";
if (_encodingType.isValueType() || (_forLibrary && _encodingType.dataStoredIn(DataLocation::Storage)))
ret["type"] = _encodingType.canonicalName() + suffix;
else if (ArrayType const* arrayType = dynamic_cast<ArrayType const*>(&_encodingType))
{
if (arrayType->isByteArray())
ret["type"] = _type.canonicalName() + suffix;
ret["type"] = _encodingType.canonicalName() + suffix;
else
{
string suffix;
@ -155,7 +166,12 @@ Json::Value ABI::formatType(string const& _name, Type const& _type, bool _forLib
else
suffix = string("[") + arrayType->length().str() + "]";
solAssert(arrayType->baseType(), "");
Json::Value subtype = formatType("", *arrayType->baseType(), _forLibrary);
Json::Value subtype = formatType(
"",
*arrayType->baseType(),
*dynamic_cast<ArrayType const&>(_solidityType).baseType(),
_forLibrary
);
if (subtype.isMember("components"))
{
ret["type"] = subtype["type"].asString() + suffix;
@ -165,16 +181,16 @@ Json::Value ABI::formatType(string const& _name, Type const& _type, bool _forLib
ret["type"] = subtype["type"].asString() + suffix;
}
}
else if (StructType const* structType = dynamic_cast<StructType const*>(&_type))
else if (StructType const* structType = dynamic_cast<StructType const*>(&_encodingType))
{
ret["type"] = "tuple";
ret["components"] = Json::arrayValue;
for (auto const& member: structType->members(nullptr))
{
solAssert(member.type, "");
auto t = member.type->interfaceType(_forLibrary);
solAssert(t.get(), "");
ret["components"].append(formatType(member.name, *t.get(), _forLibrary));
Type const* t = member.type->interfaceType(_forLibrary);
solAssert(t, "");
ret["components"].append(formatType(member.name, *t, *member.type, _forLibrary));
}
}
else

View File

@ -45,15 +45,25 @@ private:
/// @returns a json value suitable for a list of types in function input or output
/// parameters or other places. If @a _forLibrary is true, complex types are referenced
/// by name, otherwise they are anonymously expanded.
/// @a _solidityTypes is the list of original Solidity types where @a _encodingTypes is the list of
/// ABI types used for the actual encoding.
static Json::Value formatTypeList(
std::vector<std::string> const& _names,
std::vector<TypePointer> const& _types,
std::vector<TypePointer> const& _encodingTypes,
std::vector<TypePointer> const& _solidityTypes,
bool _forLibrary
);
/// @returns a Json object with "name", "type" and potentially "components" keys, according
/// to the ABI specification.
/// @returns a Json object with "name", "type", "internalType" and potentially
/// "components" keys, according to the ABI specification.
/// If it is possible to express the type as a single string, it is allowed to return a single string.
static Json::Value formatType(std::string const& _name, Type const& _type, bool _forLibrary);
/// @a _solidityType is the original Solidity type and @a _encodingTypes is the
/// ABI type used for the actual encoding.
static Json::Value formatType(
std::string const& _name,
Type const& _encodingType,
Type const& _solidityType,
bool _forLibrary
);
};
}

View File

@ -9,6 +9,7 @@ contract test {
// "inputs":
// [
// {
// "internalType": "uint256",
// "name": "a",
// "type": "uint256"
// }
@ -17,6 +18,7 @@ contract test {
// "outputs":
// [
// {
// "internalType": "uint256",
// "name": "d",
// "type": "uint256"
// }

View File

@ -8,14 +8,17 @@ contract test {
// "inputs":
// [
// {
// "internalType": "uint256",
// "name": "param1",
// "type": "uint256"
// },
// {
// "internalType": "contract test",
// "name": "param2",
// "type": "address"
// },
// {
// "internalType": "bool",
// "name": "param3",
// "type": "bool"
// }

View File

@ -13,10 +13,12 @@ contract test {
// "inputs":
// [
// {
// "internalType": "uint256",
// "name": "",
// "type": "uint256"
// },
// {
// "internalType": "uint256",
// "name": "k",
// "type": "uint256"
// }
@ -25,10 +27,12 @@ contract test {
// "outputs":
// [
// {
// "internalType": "uint256",
// "name": "ret_k",
// "type": "uint256"
// },
// {
// "internalType": "uint256",
// "name": "ret_g",
// "type": "uint256"
// }

View File

@ -11,6 +11,7 @@ contract test {
// "inputs":
// [
// {
// "internalType": "uint256",
// "name": "k",
// "type": "uint256"
// }
@ -19,6 +20,7 @@ contract test {
// "outputs":
// [
// {
// "internalType": "uint256",
// "name": "",
// "type": "uint256"
// }

View File

@ -15,11 +15,13 @@ contract C {
// "components":
// [
// {
// "internalType": "uint256[2]",
// "name": "x",
// "type": "uint256[2]"
// }
// ],
// "indexed": false,
// "internalType": "struct C.T",
// "name": "t",
// "type": "tuple"
// },
@ -27,6 +29,7 @@ contract C {
// "components":
// [
// {
// "internalType": "uint256",
// "name": "a",
// "type": "uint256"
// },
@ -34,19 +37,23 @@ contract C {
// "components":
// [
// {
// "internalType": "uint256[2]",
// "name": "x",
// "type": "uint256[2]"
// }
// ],
// "internalType": "struct C.T[]",
// "name": "sub",
// "type": "tuple[]"
// },
// {
// "internalType": "bytes",
// "name": "b",
// "type": "bytes"
// }
// ],
// "indexed": false,
// "internalType": "struct C.S",
// "name": "s",
// "type": "tuple"
// }

View File

@ -13,6 +13,7 @@ contract test {
// "inputs":
// [
// {
// "internalType": "uint256",
// "name": "a",
// "type": "uint256"
// }
@ -21,6 +22,7 @@ contract test {
// "outputs":
// [
// {
// "internalType": "uint256",
// "name": "d",
// "type": "uint256"
// }
@ -35,11 +37,13 @@ contract test {
// [
// {
// "indexed": false,
// "internalType": "uint256",
// "name": "b",
// "type": "uint256"
// },
// {
// "indexed": true,
// "internalType": "address",
// "name": "c",
// "type": "address"
// }
@ -59,6 +63,7 @@ contract test {
// [
// {
// "indexed": false,
// "internalType": "uint256",
// "name": "a",
// "type": "uint256"
// }

View File

@ -9,6 +9,7 @@ contract test {
// "inputs":
// [
// {
// "internalType": "function (uint256) external returns (uint256)",
// "name": "x",
// "type": "function"
// }

View File

@ -0,0 +1,23 @@
contract test {
function g(function(test) external returns (test[] memory) x) public {}
}
// ----
// :test
// [
// {
// "constant": false,
// "inputs":
// [
// {
// "internalType": "function (contract test) external returns (contract test[])",
// "name": "x",
// "type": "function"
// }
// ],
// "name": "g",
// "outputs": [],
// "payable": false,
// "stateMutability": "nonpayable",
// "type": "function"
// }
// ]

View File

@ -14,6 +14,7 @@ contract Derived is Base {
// "inputs":
// [
// {
// "internalType": "uint256",
// "name": "p",
// "type": "uint256"
// }
@ -22,6 +23,7 @@ contract Derived is Base {
// "outputs":
// [
// {
// "internalType": "uint256",
// "name": "i",
// "type": "uint256"
// }
@ -36,6 +38,7 @@ contract Derived is Base {
// [
// {
// "indexed": true,
// "internalType": "bytes32",
// "name": "evtArgBase",
// "type": "bytes32"
// }
@ -53,6 +56,7 @@ contract Derived is Base {
// "inputs":
// [
// {
// "internalType": "uint256",
// "name": "p",
// "type": "uint256"
// }
@ -61,6 +65,7 @@ contract Derived is Base {
// "outputs":
// [
// {
// "internalType": "uint256",
// "name": "i",
// "type": "uint256"
// }
@ -74,6 +79,7 @@ contract Derived is Base {
// "inputs":
// [
// {
// "internalType": "bytes32",
// "name": "p",
// "type": "bytes32"
// }
@ -82,6 +88,7 @@ contract Derived is Base {
// "outputs":
// [
// {
// "internalType": "bytes32",
// "name": "i",
// "type": "bytes32"
// }
@ -96,6 +103,7 @@ contract Derived is Base {
// [
// {
// "indexed": true,
// "internalType": "uint256",
// "name": "evtArgDerived",
// "type": "uint256"
// }
@ -109,6 +117,7 @@ contract Derived is Base {
// [
// {
// "indexed": true,
// "internalType": "bytes32",
// "name": "evtArgBase",
// "type": "bytes32"
// }

View File

@ -11,10 +11,12 @@ library test {
// "inputs":
// [
// {
// "internalType": "uint256[]",
// "name": "c",
// "type": "uint256[]"
// },
// {
// "internalType": "library test",
// "name": "d",
// "type": "test"
// }
@ -23,6 +25,7 @@ library test {
// "outputs":
// [
// {
// "internalType": "uint256[]",
// "name": "e",
// "type": "uint256[]"
// }

View File

@ -10,6 +10,7 @@ contract test {
// "inputs":
// [
// {
// "internalType": "uint256",
// "name": "a",
// "type": "uint256"
// }
@ -18,6 +19,7 @@ contract test {
// "outputs":
// [
// {
// "internalType": "uint256",
// "name": "d",
// "type": "uint256"
// }
@ -31,6 +33,7 @@ contract test {
// "inputs":
// [
// {
// "internalType": "uint256",
// "name": "b",
// "type": "uint256"
// }
@ -39,6 +42,7 @@ contract test {
// "outputs":
// [
// {
// "internalType": "uint256",
// "name": "e",
// "type": "uint256"
// }

View File

@ -11,6 +11,7 @@ contract test {
// "inputs":
// [
// {
// "internalType": "uint256",
// "name": "b",
// "type": "uint256"
// }
@ -19,6 +20,7 @@ contract test {
// "outputs":
// [
// {
// "internalType": "uint256",
// "name": "e",
// "type": "uint256"
// }
@ -32,6 +34,7 @@ contract test {
// "inputs":
// [
// {
// "internalType": "uint256",
// "name": "a",
// "type": "uint256"
// }
@ -40,6 +43,7 @@ contract test {
// "outputs":
// [
// {
// "internalType": "uint256",
// "name": "d",
// "type": "uint256"
// }

View File

@ -9,10 +9,12 @@ contract test {
// "inputs":
// [
// {
// "internalType": "uint256",
// "name": "a",
// "type": "uint256"
// },
// {
// "internalType": "uint256",
// "name": "b",
// "type": "uint256"
// }
@ -21,6 +23,7 @@ contract test {
// "outputs":
// [
// {
// "internalType": "uint256",
// "name": "d",
// "type": "uint256"
// }

View File

@ -8,14 +8,17 @@ contract test {
// "inputs":
// [
// {
// "internalType": "uint256",
// "name": "param1",
// "type": "uint256"
// },
// {
// "internalType": "contract test",
// "name": "param2",
// "type": "address"
// },
// {
// "internalType": "bool",
// "name": "param3",
// "type": "bool"
// }

View File

@ -10,10 +10,12 @@ contract test {
// "inputs":
// [
// {
// "internalType": "uint256",
// "name": "a",
// "type": "uint256"
// },
// {
// "internalType": "uint256",
// "name": "b",
// "type": "uint256"
// }
@ -22,6 +24,7 @@ contract test {
// "outputs":
// [
// {
// "internalType": "uint256",
// "name": "d",
// "type": "uint256"
// }
@ -35,6 +38,7 @@ contract test {
// "inputs":
// [
// {
// "internalType": "uint32",
// "name": "a",
// "type": "uint32"
// }
@ -43,6 +47,7 @@ contract test {
// "outputs":
// [
// {
// "internalType": "uint256",
// "name": "b",
// "type": "uint256"
// }

View File

@ -17,6 +17,7 @@ contract test {
// "outputs":
// [
// {
// "internalType": "enum test.ActionChoices",
// "name": "",
// "type": "uint8"
// }
@ -29,6 +30,7 @@ contract test {
// "inputs":
// [
// {
// "internalType": "enum test.ActionChoices",
// "name": "param",
// "type": "uint8"
// }

View File

@ -15,6 +15,7 @@ contract C {
// "outputs":
// [
// {
// "internalType": "uint256",
// "name": "x",
// "type": "uint256"
// },
@ -22,6 +23,7 @@ contract C {
// "components":
// [
// {
// "internalType": "uint256",
// "name": "a",
// "type": "uint256"
// },
@ -29,14 +31,17 @@ contract C {
// "components":
// [
// {
// "internalType": "uint256[2]",
// "name": "x",
// "type": "uint256[2]"
// }
// ],
// "internalType": "struct C.T[]",
// "name": "sub",
// "type": "tuple[]"
// }
// ],
// "internalType": "struct C.S",
// "name": "s",
// "type": "tuple"
// }

View File

@ -17,18 +17,22 @@ contract C {
// "components":
// [
// {
// "internalType": "contract C[]",
// "name": "x",
// "type": "address[]"
// },
// {
// "internalType": "contract C",
// "name": "y",
// "type": "address"
// }
// ],
// "internalType": "struct C.S",
// "name": "s",
// "type": "tuple"
// },
// {
// "internalType": "contract C",
// "name": "c",
// "type": "address"
// }

View File

@ -10,14 +10,17 @@ contract test {
// "inputs":
// [
// {
// "internalType": "string",
// "name": "a",
// "type": "string"
// },
// {
// "internalType": "bytes",
// "name": "b",
// "type": "bytes"
// },
// {
// "internalType": "uint256[]",
// "name": "c",
// "type": "uint256[]"
// }

View File

@ -16,6 +16,7 @@ library L {
// "components":
// [
// {
// "internalType": "uint256",
// "name": "a",
// "type": "uint256"
// },
@ -23,18 +24,22 @@ library L {
// "components":
// [
// {
// "internalType": "uint256[2]",
// "name": "x",
// "type": "uint256[2]"
// }
// ],
// "internalType": "struct L.T[]",
// "name": "sub",
// "type": "tuple[]"
// },
// {
// "internalType": "bytes",
// "name": "b",
// "type": "bytes"
// }
// ],
// "internalType": "struct L.S",
// "name": "s",
// "type": "tuple"
// }

View File

@ -10,10 +10,12 @@ contract test {
// "inputs":
// [
// {
// "internalType": "uint256",
// "name": "a",
// "type": "uint256"
// },
// {
// "internalType": "uint256",
// "name": "b",
// "type": "uint256"
// }
@ -22,6 +24,7 @@ contract test {
// "outputs":
// [
// {
// "internalType": "uint256",
// "name": "d",
// "type": "uint256"
// }
@ -35,6 +38,7 @@ contract test {
// "inputs":
// [
// {
// "internalType": "uint32",
// "name": "a",
// "type": "uint32"
// }
@ -43,6 +47,7 @@ contract test {
// "outputs":
// [
// {
// "internalType": "uint256",
// "name": "b",
// "type": "uint256"
// }