Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion book/src/drive/keep-history-documents.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,12 @@ largest documents plus both endings; every erase is admitted against the
same worst-case estimate and the actual fee is what it removed. GroveDB's
query surface has no key-only result shape over a range, so the enumeration
reads revision bodies it discards, and the estimate charges for that read
honestly.
honestly. The type's largest document is sized with each string bound past
`u16::MAX` bytes held at `u16::MAX`. The balance updates the refunds cause
for other identities are billed on top, to the identity submitting the
erase, for a full chunk of recipients whatever the erase removes; `check_tx`
holds the submitter to that charge as well, though it does not validate the
erase's state.

## The `getDocumentHistory` query

Expand Down
84 changes: 83 additions & 1 deletion packages/rs-dpp/src/data_contract/document_type/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1129,9 +1129,10 @@ pub trait DocumentTypeV0Methods: DocumentTypeV0Getters + DocumentTypeV0MethodsVe
.max_size
{
0 => self.max_size_v0(platform_version),
1 => self.max_size_v1(platform_version),
version => Err(ProtocolError::UnknownVersionMismatch {
method: "max_size".to_string(),
known_versions: vec![0],
known_versions: vec![0, 1],
received: version,
}),
}
Expand Down Expand Up @@ -1699,6 +1700,87 @@ mod tests {
);
}

// --------------------------------------------------------------
// DocumentTypeV0Methods::max_size
// --------------------------------------------------------------

/// A string of 20000 characters without `maxBytes` counts as `u16::MAX`
/// bytes toward the largest size, as a string without `maxLength` does,
/// from protocol version 15. Generation 0, which protocol version 14
/// selects, fails on the overflow.
#[test]
fn should_hold_a_string_past_16383_characters_at_u16_max_in_the_largest_size() {
let platform_version = PlatformVersion::latest();
let long = build_doc_type("note", note_schema(long_text()));
let unbounded = build_doc_type(
"note",
note_schema(platform_value!({"type": "string", "position": 0})),
);
assert_eq!(
long.as_ref()
.max_size(platform_version)
.expect("the long string is sized"),
u16::MAX
);
assert_eq!(
unbounded
.as_ref()
.max_size(platform_version)
.expect("the unbounded string is sized"),
u16::MAX
);

let platform_version_14 = PlatformVersion::get(14).expect("expected version 14");
let long_14 = build_doc_type_at("note", note_schema(long_text()), platform_version_14);
assert!(matches!(
long_14.as_ref().max_size(platform_version_14),
Err(ProtocolError::Overflow(_))
));
}

/// Below the cap both generations size every property alike, an object's
/// members and a typed array's elements included.
#[test]
fn should_size_bounded_properties_as_protocol_version_14_does() {
let platform_version_14 = PlatformVersion::get(14).expect("expected version 14");
let schema = platform_value!({
"type": "object",
"properties": {
"title": {"type": "string", "minLength": 3, "maxLength": 100, "position": 0},
"count": {"type": "integer", "minimum": 0, "maximum": 1000, "position": 1},
"data": {"type": "array", "byteArray": true, "maxItems": 64, "position": 2},
"meta": {
"type": "object",
"properties": {
"name": {"type": "string", "maxLength": 10, "position": 0},
"rank": {"type": "integer", "minimum": 0, "maximum": 9, "position": 1},
},
"additionalProperties": false,
"position": 3,
},
"tags": {
"type": "array",
"items": {"type": "string", "maxLength": 10},
"maxItems": 4,
"position": 4,
},
},
"additionalProperties": false,
});
let at_latest = build_doc_type("doc", schema.clone());
let at_14 = build_doc_type_at("doc", schema, platform_version_14);
let size = at_latest
.as_ref()
.max_size(PlatformVersion::latest())
.expect("sized");
assert_eq!(
size,
at_14.as_ref().max_size(platform_version_14).expect("sized")
);
// The title alone is 400 bytes, and nothing comes near the cap
assert!(size > 400 && size < 1024, "{size}");
}

// --------------------------------------------------------------
// DocumentTypeBasicMethods::requires_revision / initial_revision
// --------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,27 @@ pub trait DocumentTypeV0MethodsVersioned: DocumentTypeV0Getters + DocumentTypeBa
Ok(total_size)
}

/// Generation 0 with each property sized by
/// [`DocumentPropertyType::saturating_max_byte_size`]: a string of 16384
/// or more characters without `maxBytes` has a byte bound past `u16::MAX`,
/// which generation 0 fails on with an overflow and this generation holds
/// at `u16::MAX`. Every other type is sized as generation 0 sizes it, and
/// the total saturates as generation 0's does.
fn max_size_v1(&self, platform_version: &PlatformVersion) -> Result<u16, ProtocolError> {
let mut total_size = 0u16;

for document_property in self.flattened_properties().values() {
if let Some(size) = document_property
.property_type
.saturating_max_byte_size(platform_version)?
{
total_size = total_size.saturating_add(size);
}
}

Ok(total_size)
}

/// Figures out the prefunded voting balance (v0) for a document in a document type
fn prefunded_voting_balance_for_document_v0(
&self,
Expand Down
Loading
Loading