mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Improve typehash() computation for nested structs
This commit is contained in:
parent
f9a48b94d4
commit
9b4da6bdea
@ -389,22 +389,39 @@ std::vector<std::pair<ASTPointer<IdentifierPath>, std::optional<Token>>> UsingFo
|
||||
return ranges::zip_view(m_functionsOrLibrary, m_operators) | ranges::to<vector>;
|
||||
}
|
||||
|
||||
std::string StructDefinition::encodeType() const
|
||||
void StructDefinition::insertEncodedSubtypes(std::set<std::string>& subtypes) const
|
||||
{
|
||||
for (size_t i = 0; i < m_members.size(); i++)
|
||||
{
|
||||
if (m_members[i]->type()->category() == Type::Category::Struct)
|
||||
{
|
||||
Declaration const* declaration = m_members[i]->type()->typeDefinition();
|
||||
StructDefinition const* structDef = dynamic_cast<StructDefinition const*>(declaration);
|
||||
solAssert(structDef != nullptr);
|
||||
|
||||
subtypes.insert(structDef->encodeTypeWithoutSubtypes());
|
||||
structDef->insertEncodedSubtypes(subtypes);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::string StructDefinition::encodeTypeWithoutSubtypes() const
|
||||
{
|
||||
std::string str = name() + "(";
|
||||
std::set<std::string> nested; // std::set enables duplicates elimination and ordered enumeration
|
||||
for (size_t i = 0; i < m_members.size(); i++)
|
||||
{
|
||||
str += i == 0 ? "" : ",";
|
||||
str += m_members[i]->type()->canonicalName() + " " + m_members[i]->name();
|
||||
if (m_members[i]->type()->category() == Type::Category::Struct) {
|
||||
Declaration const* declaration = m_members[i]->type()->typeDefinition();
|
||||
if (StructDefinition const* structDef = dynamic_cast<StructDefinition const*>(declaration)) {
|
||||
nested.insert(structDef->encodeType());
|
||||
}
|
||||
}
|
||||
}
|
||||
return std::accumulate(nested.begin(), nested.end(), str + ")");
|
||||
return str + ")";
|
||||
}
|
||||
|
||||
std::string StructDefinition::encodeType() const
|
||||
{
|
||||
// std::set enables duplicates elimination and ordered enumeration
|
||||
std::set<std::string> subtypes;
|
||||
insertEncodedSubtypes(subtypes);
|
||||
return std::accumulate(subtypes.begin(), subtypes.end(), encodeTypeWithoutSubtypes());
|
||||
}
|
||||
|
||||
util::h256 StructDefinition::typehash() const
|
||||
|
@ -741,7 +741,13 @@ public:
|
||||
|
||||
std::vector<ASTPointer<VariableDeclaration>> const& members() const { return m_members; }
|
||||
|
||||
/// @returns the EIP-712 compatible struct encoding.
|
||||
/// Fills set with the EIP-712 compatible struct encodings without subtypes concatenated.
|
||||
void insertEncodedSubtypes(std::set<std::string>& subtypes) const;
|
||||
|
||||
/// @returns the EIP-712 compatible struct encoding but without subtypes concatenated.
|
||||
std::string encodeTypeWithoutSubtypes() const;
|
||||
|
||||
/// @returns the EIP-712 compatible struct encoding with subtypes sorted and concatenated.
|
||||
std::string encodeType() const;
|
||||
|
||||
/// @returns the EIP-712 compatible typehash of this struct.
|
||||
|
@ -1944,8 +1944,10 @@ void IRGeneratorForStatements::endVisit(MemberAccess const& _memberAccess)
|
||||
else if (member == "typehash")
|
||||
{
|
||||
Type const* arg = dynamic_cast<MagicType const&>(*_memberAccess.expression().annotation().type).typeArgument();
|
||||
auto const& structType = dynamic_cast<StructType const&>(*arg);
|
||||
StructDefinition const& struct_ = structType.structDefinition();
|
||||
solAssert(arg != nullptr);
|
||||
StructType const* structType = dynamic_cast<StructType const*>(arg);
|
||||
solAssert(structType != nullptr);
|
||||
StructDefinition const& struct_ = structType->structDefinition();
|
||||
define(_memberAccess) << formatNumber(struct_.typehash()) << "\n";
|
||||
}
|
||||
else if (member == "min" || member == "max")
|
||||
|
Loading…
Reference in New Issue
Block a user