Skip to content

Commit

Permalink
refactor(dpp): change String and ByteArray DocumentPropertyType sizes…
Browse files Browse the repository at this point in the history
… to structs (#1874)
  • Loading branch information
pauldelucia authored Jun 23, 2024
1 parent 9edcd10 commit e1e85b8
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ use crate::data_contract::document_type::property_names::{
CAN_BE_DELETED, CREATION_RESTRICTION_MODE, DOCUMENTS_KEEP_HISTORY, DOCUMENTS_MUTABLE,
TRADE_MODE, TRANSFERABLE,
};
use crate::data_contract::document_type::{property_names, DocumentType};
use crate::data_contract::document_type::{
property_names, ByteArrayPropertySizes, DocumentType, StringPropertySizes,
};
use crate::data_contract::errors::DataContractError;
use crate::data_contract::storage_requirements::keys_for_document_type::StorageKeyRequirements;
use crate::identity::SecurityLevel;
Expand Down Expand Up @@ -383,7 +385,7 @@ impl DocumentTypeV0 {
})?;

// Validate indexed property type
match property_definition.property_type {
match &property_definition.property_type {
// Array and objects aren't supported for indexing yet
DocumentPropertyType::Array(_)
| DocumentPropertyType::Object(_)
Expand All @@ -399,9 +401,9 @@ impl DocumentTypeV0 {
)))
}
// Indexed byte array size must be limited
DocumentPropertyType::ByteArray(_, maybe_max_size)
if maybe_max_size.is_none()
|| maybe_max_size.unwrap()
DocumentPropertyType::ByteArray(sizes)
if sizes.max_size.is_none()
|| sizes.max_size.unwrap()
> MAX_INDEXED_BYTE_ARRAY_PROPERTY_LENGTH =>
{
Err(ProtocolError::ConsensusError(Box::new(
Expand All @@ -419,9 +421,9 @@ impl DocumentTypeV0 {
)))
}
// Indexed string length must be limited
DocumentPropertyType::String(_, maybe_max_length)
if maybe_max_length.is_none()
|| maybe_max_length.unwrap()
DocumentPropertyType::String(sizes)
if sizes.max_length.is_none()
|| sizes.max_length.unwrap()
> MAX_INDEXED_STRING_PROPERTY_LENGTH =>
{
Err(ProtocolError::ConsensusError(Box::new(
Expand Down Expand Up @@ -567,12 +569,14 @@ fn insert_values(
Some("application/x.dash.dpp.identifier") => {
DocumentPropertyType::Identifier
}
Some(_) | None => DocumentPropertyType::ByteArray(
inner_properties
.get_optional_integer(property_names::MIN_ITEMS)?,
inner_properties
.get_optional_integer(property_names::MAX_ITEMS)?,
),
Some(_) | None => {
DocumentPropertyType::ByteArray(ByteArrayPropertySizes {
min_size: inner_properties
.get_optional_integer(property_names::MIN_ITEMS)?,
max_size: inner_properties
.get_optional_integer(property_names::MAX_ITEMS)?,
})
}
}
} else {
return Err(DataContractError::InvalidContractStructure(
Expand Down Expand Up @@ -622,10 +626,12 @@ fn insert_values(
}

"string" => {
field_type = DocumentPropertyType::String(
inner_properties.get_optional_integer(property_names::MIN_LENGTH)?,
inner_properties.get_optional_integer(property_names::MAX_LENGTH)?,
);
field_type = DocumentPropertyType::String(StringPropertySizes {
min_length: inner_properties
.get_optional_integer(property_names::MIN_LENGTH)?,
max_length: inner_properties
.get_optional_integer(property_names::MAX_LENGTH)?,
});
document_properties.insert(
prefixed_property_key,
DocumentProperty {
Expand Down Expand Up @@ -673,10 +679,10 @@ fn insert_values_nested(
let field_type = match type_value {
"integer" => DocumentPropertyType::Integer,
"number" => DocumentPropertyType::Number,
"string" => DocumentPropertyType::String(
inner_properties.get_optional_integer(property_names::MIN_LENGTH)?,
inner_properties.get_optional_integer(property_names::MAX_LENGTH)?,
),
"string" => DocumentPropertyType::String(StringPropertySizes {
min_length: inner_properties.get_optional_integer(property_names::MIN_LENGTH)?,
max_length: inner_properties.get_optional_integer(property_names::MAX_LENGTH)?,
}),
"array" => {
// Only handling bytearrays for v1
// Return an error if it is not a byte array
Expand All @@ -689,10 +695,14 @@ fn insert_values_nested(
Some("application/x.dash.dpp.identifier") => {
DocumentPropertyType::Identifier
}
Some(_) | None => DocumentPropertyType::ByteArray(
inner_properties.get_optional_integer(property_names::MIN_ITEMS)?,
inner_properties.get_optional_integer(property_names::MAX_ITEMS)?,
),
Some(_) | None => {
DocumentPropertyType::ByteArray(ByteArrayPropertySizes {
min_size: inner_properties
.get_optional_integer(property_names::MIN_ITEMS)?,
max_size: inner_properties
.get_optional_integer(property_names::MAX_ITEMS)?,
})
}
}
} else {
return Err(DataContractError::InvalidContractStructure(
Expand Down
Loading

0 comments on commit e1e85b8

Please sign in to comment.