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,6 +924,7 @@ 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);
if (!m_forcedVisit)
m_valueStream.appendValue(value); m_valueStream.appendValue(value);
return assignAndCheckStringPair(m_varName, m_paramName, value, value, DataType::VALUE); return assignAndCheckStringPair(m_varName, m_paramName, value, value, DataType::VALUE);
} }
@ -931,6 +932,7 @@ pair<string, string> AssignCheckVisitor::visit(BoolType const& _type)
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);
if (!m_forcedVisit)
m_valueStream.appendValue(value); m_valueStream.appendValue(value);
return assignAndCheckStringPair(m_varName, m_paramName, value, value, DataType::VALUE); return assignAndCheckStringPair(m_varName, m_paramName, value, value, DataType::VALUE);
} }
@ -938,24 +940,33 @@ pair<string, string> AssignCheckVisitor::visit(IntegerType const& _type)
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);
if (!m_forcedVisit)
{
string isabelleValue = ValueGetterVisitor{}.isabelleBytesValueAsString(value); string isabelleValue = ValueGetterVisitor{}.isabelleBytesValueAsString(value);
m_valueStream.appendValue(isabelleValue); 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);
if (!m_forcedVisit)
{
string isabelleValue = ValueGetterVisitor{}.isabelleAddressValueAsString(value); string isabelleValue = ValueGetterVisitor{}.isabelleAddressValueAsString(value);
m_valueStream.appendValue(isabelleValue); 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);
if (!m_forcedVisit)
{
string isabelleValue = ValueGetterVisitor{}.isabelleBytesValueAsString(value); string isabelleValue = ValueGetterVisitor{}.isabelleBytesValueAsString(value);
m_valueStream.appendValue(isabelleValue); 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,6 +1031,7 @@ 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;
if (!m_forcedVisit)
m_valueStream.startArray(); m_valueStream.startArray();
for (unsigned i = 0; i < length; i++) for (unsigned i = 0; i < length; 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,6 +1080,7 @@ pair<string, string> AssignCheckVisitor::visit(StructType const& _type)
string wasVarName = m_varName; string wasVarName = m_varName;
string wasParamName = m_paramName; string wasParamName = m_paramName;
if (!m_forcedVisit)
m_valueStream.startStruct(); m_valueStream.startStruct();
for (auto const& t: _type.t()) for (auto const& t: _type.t())
{ {
@ -1077,6 +1095,7 @@ pair<string, string> AssignCheckVisitor::visit(StructType const& _type)
assignCheckBuffer.second += assign.second; assignCheckBuffer.second += assign.second;
i++; i++;
} }
if (!m_forcedVisit)
m_valueStream.endStruct(); m_valueStream.endStruct();
m_varName = wasVarName; m_varName = wasVarName;
m_paramName = wasParamName; m_paramName = wasParamName;

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.