Merge pull request #7117 from ethereum/abiv2-add-bool-dynarray

Add bool type and array of bool and dynamic byte types to abiv2 protobuf spec/converter
This commit is contained in:
chriseth 2019-07-17 15:08:07 +02:00 committed by GitHub
commit c62a63a091
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 71 additions and 2 deletions

View File

@ -35,10 +35,15 @@ message ArrayType {
FixedByteType byty = 2; FixedByteType byty = 2;
AddressType adty = 3; AddressType adty = 3;
StructType stty = 4; StructType stty = 4;
BoolType boolty = 5;
DynamicByteArrayType dynbytesty = 6;
} }
repeated ArrayDimensionInfo info = 5; repeated ArrayDimensionInfo info = 7;
} }
// bool
message BoolType {}
// uint8...256, int8...256 // uint8...256, int8...256
message IntegerType { message IntegerType {
required bool is_signed = 1; required bool is_signed = 1;
@ -60,6 +65,7 @@ message ValueType {
IntegerType inty = 1; IntegerType inty = 1;
FixedByteType byty = 2; FixedByteType byty = 2;
AddressType adty = 3; AddressType adty = 3;
BoolType boolty = 4;
} }
} }

View File

@ -158,6 +158,11 @@ void ProtoConverter::checkResizeOp(std::string const& _paramName, unsigned _len)
appendChecks(DataType::VALUE, _paramName + ".length", std::to_string(_len)); appendChecks(DataType::VALUE, _paramName + ".length", std::to_string(_len));
} }
std::string ProtoConverter::boolValueAsString(unsigned _counter)
{
return ((_counter % 2) ? "true" : "false");
}
/* Input(s) /* Input(s)
* - Unsigned integer to be hashed * - Unsigned integer to be hashed
* - Width of desired uint value * - Width of desired uint value
@ -250,6 +255,15 @@ std::string ProtoConverter::structTypeAsString(StructType const&)
return {}; return {};
} }
void ProtoConverter::visit(BoolType const&)
{
visitType(
DataType::VALUE,
getBoolTypeAsString(),
boolValueAsString(getNextCounter())
);
}
void ProtoConverter::visit(IntegerType const& _x) void ProtoConverter::visit(IntegerType const& _x)
{ {
visitType( visitType(
@ -290,6 +304,9 @@ void ProtoConverter::visit(ValueType const& _x)
case ValueType::kAdty: case ValueType::kAdty:
visit(_x.adty()); visit(_x.adty());
break; break;
case ValueType::kBoolty:
visit(_x.boolty());
break;
case ValueType::VALUE_TYPE_ONEOF_NOT_SET: case ValueType::VALUE_TYPE_ONEOF_NOT_SET:
break; break;
} }
@ -350,6 +367,10 @@ std::string ProtoConverter::getValueByBaseType(ArrayType const& _x)
return fixedByteValueAsString(getFixedByteWidth(_x.byty()), getNextCounter()); return fixedByteValueAsString(getFixedByteWidth(_x.byty()), getNextCounter());
case ArrayType::kAdty: case ArrayType::kAdty:
return addressValueAsString(getNextCounter()); return addressValueAsString(getNextCounter());
case ArrayType::kBoolty:
return boolValueAsString(getNextCounter());
case ArrayType::kDynbytesty:
return bytesArrayValueAsString(getNextCounter());
// TODO: Implement structs. // TODO: Implement structs.
case ArrayType::kStty: case ArrayType::kStty:
case ArrayType::BASE_TYPE_ONEOF_NOT_SET: case ArrayType::BASE_TYPE_ONEOF_NOT_SET:
@ -357,6 +378,23 @@ std::string ProtoConverter::getValueByBaseType(ArrayType const& _x)
} }
} }
ProtoConverter::DataType ProtoConverter::getDataTypeByBaseType(ArrayType const& _x)
{
switch (_x.base_type_oneof_case())
{
case ArrayType::kInty:
case ArrayType::kByty:
case ArrayType::kAdty:
case ArrayType::kBoolty:
return DataType::VALUE;
case ArrayType::kDynbytesty:
return getDataTypeOfDynBytesType(_x.dynbytesty());
case ArrayType::kStty:
case ArrayType::BASE_TYPE_ONEOF_NOT_SET:
solUnimplemented("Proto ABIv2 fuzzer: Invalid array base type");
}
}
// Adds a resize operation for a given dimension of type `_type` and expression referenced // Adds a resize operation for a given dimension of type `_type` and expression referenced
// by `_var`. `_isStatic` is true for statically sized dimensions, false otherwise. // by `_var`. `_isStatic` is true for statically sized dimensions, false otherwise.
// `_arrayLen` is equal to length of statically sized array dimension. For dynamically // `_arrayLen` is equal to length of statically sized array dimension. For dynamically
@ -421,7 +459,8 @@ void ProtoConverter::resizeHelper(
// value is a value of base type // value is a value of base type
std::string value = getValueByBaseType(_x); std::string value = getValueByBaseType(_x);
// add assignment and check // add assignment and check
addCheckedVarDef(DataType::VALUE, _varName, _paramName, value); DataType dataType = getDataTypeByBaseType(_x);
addCheckedVarDef(dataType, _varName, _paramName, value);
} }
else else
{ {
@ -494,6 +533,12 @@ void ProtoConverter::visit(ArrayType const& _x)
case ArrayType::kAdty: case ArrayType::kAdty:
baseType = getAddressTypeAsString(_x.adty()); baseType = getAddressTypeAsString(_x.adty());
break; break;
case ArrayType::kBoolty:
baseType = getBoolTypeAsString();
break;
case ArrayType::kDynbytesty:
baseType = bytesArrayTypeAsString(_x.dynbytesty());
break;
case ArrayType::kStty: case ArrayType::kStty:
case ArrayType::BASE_TYPE_ONEOF_NOT_SET: case ArrayType::BASE_TYPE_ONEOF_NOT_SET:
return; return;

View File

@ -101,6 +101,8 @@ private:
ARRAY ARRAY
}; };
void visit(BoolType const&);
void visit(IntegerType const&); void visit(IntegerType const&);
void visit(FixedByteType const&); void visit(FixedByteType const&);
@ -127,6 +129,8 @@ private:
std::string getValueByBaseType(ArrayType const&); std::string getValueByBaseType(ArrayType const&);
DataType getDataTypeByBaseType(ArrayType const& _x);
void resizeInitArray( void resizeInitArray(
ArrayType const& _x, ArrayType const& _x,
std::string const& _baseType, std::string const& _baseType,
@ -247,6 +251,7 @@ private:
// Static declarations // Static declarations
static std::string structTypeAsString(StructType const& _x); static std::string structTypeAsString(StructType const& _x);
static std::string boolValueAsString(unsigned _counter);
static std::string intValueAsString(unsigned _width, unsigned _counter); static std::string intValueAsString(unsigned _width, unsigned _counter);
static std::string uintValueAsString(unsigned _width, unsigned _counter); static std::string uintValueAsString(unsigned _width, unsigned _counter);
static std::string integerValueAsString(bool _sign, unsigned _width, unsigned _counter); static std::string integerValueAsString(bool _sign, unsigned _width, unsigned _counter);
@ -278,6 +283,11 @@ private:
return _x.is_signed(); return _x.is_signed();
} }
static std::string getBoolTypeAsString()
{
return "bool";
}
static std::string getIntTypeAsString(IntegerType const& _x) static std::string getIntTypeAsString(IntegerType const& _x)
{ {
return ((isIntSigned(_x) ? "int" : "uint") + std::to_string(getIntWidth(_x))); return ((isIntSigned(_x) ? "int" : "uint") + std::to_string(getIntWidth(_x)));
@ -298,6 +308,14 @@ private:
return (_x.payable() ? "address payable": "address"); return (_x.payable() ? "address payable": "address");
} }
static DataType getDataTypeOfDynBytesType(DynamicByteArrayType const& _x)
{
if (_x.type() == DynamicByteArrayType::STRING)
return DataType::STRING;
else
return DataType::BYTES;
}
// Convert _counter to string and return its keccak256 hash // Convert _counter to string and return its keccak256 hash
static u256 hashUnsignedInt(unsigned _counter) static u256 hashUnsignedInt(unsigned _counter)
{ {