Merge pull request #10570 from ethereum/abiv2-isabelle-test-values-fix-bugs

Ensure empty arrays are not visited and fix formatting issues
This commit is contained in:
Leonardo 2020-12-11 17:38:15 +01:00 committed by GitHub
commit adead3072d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 13 deletions

View File

@ -924,38 +924,49 @@ void AssignCheckVisitor::ValueStream::appendValue(string& _value)
pair<string, string> AssignCheckVisitor::visit(BoolType const& _type) pair<string, string> AssignCheckVisitor::visit(BoolType const& _type)
{ {
string value = ValueGetterVisitor(counter()).visit(_type); string value = ValueGetterVisitor(counter()).visit(_type);
m_valueStream.appendValue(value); if (!m_forcedVisit)
m_valueStream.appendValue(value);
return assignAndCheckStringPair(m_varName, m_paramName, value, value, DataType::VALUE); return assignAndCheckStringPair(m_varName, m_paramName, value, value, DataType::VALUE);
} }
pair<string, string> AssignCheckVisitor::visit(IntegerType const& _type) pair<string, string> AssignCheckVisitor::visit(IntegerType const& _type)
{ {
string value = ValueGetterVisitor(counter()).visit(_type); string value = ValueGetterVisitor(counter()).visit(_type);
m_valueStream.appendValue(value); if (!m_forcedVisit)
m_valueStream.appendValue(value);
return assignAndCheckStringPair(m_varName, m_paramName, value, value, DataType::VALUE); return assignAndCheckStringPair(m_varName, m_paramName, value, value, DataType::VALUE);
} }
pair<string, string> AssignCheckVisitor::visit(FixedByteType const& _type) pair<string, string> AssignCheckVisitor::visit(FixedByteType const& _type)
{ {
string value = ValueGetterVisitor(counter()).visit(_type); string value = ValueGetterVisitor(counter()).visit(_type);
string isabelleValue = ValueGetterVisitor{}.isabelleBytesValueAsString(value); if (!m_forcedVisit)
m_valueStream.appendValue(isabelleValue); {
string isabelleValue = ValueGetterVisitor{}.isabelleBytesValueAsString(value);
m_valueStream.appendValue(isabelleValue);
}
return assignAndCheckStringPair(m_varName, m_paramName, value, value, DataType::VALUE); return assignAndCheckStringPair(m_varName, m_paramName, value, value, DataType::VALUE);
} }
pair<string, string> AssignCheckVisitor::visit(AddressType const& _type) pair<string, string> AssignCheckVisitor::visit(AddressType const& _type)
{ {
string value = ValueGetterVisitor(counter()).visit(_type); string value = ValueGetterVisitor(counter()).visit(_type);
string isabelleValue = ValueGetterVisitor{}.isabelleAddressValueAsString(value); if (!m_forcedVisit)
m_valueStream.appendValue(isabelleValue); {
string isabelleValue = ValueGetterVisitor{}.isabelleAddressValueAsString(value);
m_valueStream.appendValue(isabelleValue);
}
return assignAndCheckStringPair(m_varName, m_paramName, value, value, DataType::VALUE); return assignAndCheckStringPair(m_varName, m_paramName, value, value, DataType::VALUE);
} }
pair<string, string> AssignCheckVisitor::visit(DynamicByteArrayType const& _type) pair<string, string> AssignCheckVisitor::visit(DynamicByteArrayType const& _type)
{ {
string value = ValueGetterVisitor(counter()).visit(_type); string value = ValueGetterVisitor(counter()).visit(_type);
string isabelleValue = ValueGetterVisitor{}.isabelleBytesValueAsString(value); if (!m_forcedVisit)
m_valueStream.appendValue(isabelleValue); {
string isabelleValue = ValueGetterVisitor{}.isabelleBytesValueAsString(value);
m_valueStream.appendValue(isabelleValue);
}
DataType dataType = _type.type() == DynamicByteArrayType::BYTES ? DataType::BYTES : DataType::STRING; DataType dataType = _type.type() == DynamicByteArrayType::BYTES ? DataType::BYTES : DataType::STRING;
return assignAndCheckStringPair(m_varName, m_paramName, value, value, dataType); return assignAndCheckStringPair(m_varName, m_paramName, value, value, dataType);
} }
@ -1020,7 +1031,8 @@ pair<string, string> AssignCheckVisitor::visit(ArrayType const& _type)
pair<string, string> assignCheckBuffer; pair<string, string> assignCheckBuffer;
string wasVarName = m_varName; string wasVarName = m_varName;
string wasParamName = m_paramName; string wasParamName = m_paramName;
m_valueStream.startArray(); if (!m_forcedVisit)
m_valueStream.startArray();
for (unsigned i = 0; i < length; i++) for (unsigned i = 0; i < length; i++)
{ {
m_varName = wasVarName + "[" + to_string(i) + "]"; m_varName = wasVarName + "[" + to_string(i) + "]";
@ -1031,13 +1043,18 @@ pair<string, string> AssignCheckVisitor::visit(ArrayType const& _type)
if (i < length - 1) if (i < length - 1)
m_structCounter = wasStructCounter; m_structCounter = wasStructCounter;
} }
m_valueStream.endArray();
// Since struct visitor won't be called for zero-length // Since struct visitor won't be called for zero-length
// arrays, struct counter will not get incremented. Therefore, // arrays, struct counter will not get incremented. Therefore,
// we need to manually force a recursive struct visit. // we need to manually force a recursive struct visit.
if (length == 0 && TypeVisitor().arrayOfStruct(_type)) if (length == 0 && TypeVisitor().arrayOfStruct(_type))
{
bool previousState = m_forcedVisit;
m_forcedVisit = true;
visit(_type.t()); visit(_type.t());
m_forcedVisit = previousState;
}
if (!m_forcedVisit)
m_valueStream.endArray();
m_varName = wasVarName; m_varName = wasVarName;
m_paramName = wasParamName; m_paramName = wasParamName;
@ -1063,7 +1080,8 @@ pair<string, string> AssignCheckVisitor::visit(StructType const& _type)
string wasVarName = m_varName; string wasVarName = m_varName;
string wasParamName = m_paramName; string wasParamName = m_paramName;
m_valueStream.startStruct(); if (!m_forcedVisit)
m_valueStream.startStruct();
for (auto const& t: _type.t()) for (auto const& t: _type.t())
{ {
m_varName = wasVarName + ".m" + to_string(i); m_varName = wasVarName + ".m" + to_string(i);
@ -1077,7 +1095,8 @@ pair<string, string> AssignCheckVisitor::visit(StructType const& _type)
assignCheckBuffer.second += assign.second; assignCheckBuffer.second += assign.second;
i++; i++;
} }
m_valueStream.endStruct(); if (!m_forcedVisit)
m_valueStream.endStruct();
m_varName = wasVarName; m_varName = wasVarName;
m_paramName = wasParamName; m_paramName = wasParamName;
return assignCheckBuffer; return assignCheckBuffer;

View File

@ -748,6 +748,9 @@ private:
std::ostringstream stream; std::ostringstream stream;
void startStruct() void startStruct()
{ {
if (index >= 1)
stream << ",";
index = 0;
stream << "("; stream << "(";
} }
void endStruct() void endStruct()
@ -756,11 +759,15 @@ private:
} }
void startArray() void startArray()
{ {
if (index >= 1)
stream << ",";
index = 0;
stream << "["; stream << "[";
} }
void endArray() void endArray()
{ {
stream << "]"; stream << "]";
index++;
} }
void appendValue(std::string& _value); void appendValue(std::string& _value);
}; };
@ -793,6 +800,7 @@ private:
unsigned m_structCounter; unsigned m_structCounter;
unsigned m_structStart; unsigned m_structStart;
ValueStream m_valueStream; ValueStream m_valueStream;
bool m_forcedVisit = false;
}; };
/// Returns a valid value (as a string) for a given type. /// Returns a valid value (as a string) for a given type.