-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(dpp)!: wrapping overflow issue (#2430)
- Loading branch information
1 parent
fd7ee85
commit cd1527d
Showing
11 changed files
with
182 additions
and
117 deletions.
There are no files selected for viewing
35 changes: 24 additions & 11 deletions
35
packages/rs-dpp/src/data_contract/document_type/methods/estimated_size/v0/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,32 @@ | ||
use crate::data_contract::document_type::v0::DocumentTypeV0; | ||
|
||
use crate::ProtocolError; | ||
use platform_version::version::PlatformVersion; | ||
// If another document type (like V1) ever were to exist we would need to implement estimated_size_v0 again | ||
|
||
impl DocumentTypeV0 { | ||
/// The estimated size uses the middle ceil size of all attributes | ||
pub(in crate::data_contract::document_type) fn estimated_size_v0(&self) -> u16 { | ||
let mut iter = self | ||
.flattened_properties | ||
.iter() | ||
.filter_map(|(_, document_property)| { | ||
document_property.property_type.middle_byte_size_ceil() | ||
}); | ||
let first = Some(iter.next().unwrap_or_default()); | ||
pub(in crate::data_contract::document_type) fn estimated_size_v0( | ||
&self, | ||
platform_version: &PlatformVersion, | ||
) -> Result<u16, ProtocolError> { | ||
let mut total_size = 0u16; | ||
|
||
for (_, document_property) in self.flattened_properties.iter() { | ||
// This call now returns a Result<Option<u16>, ProtocolError>. | ||
let maybe_size = document_property | ||
.property_type | ||
.middle_byte_size_ceil(platform_version)?; | ||
|
||
if let Some(size) = maybe_size { | ||
total_size = match total_size.checked_add(size) { | ||
Some(new_total) => new_total, | ||
None => { | ||
return Ok(u16::MAX); | ||
} | ||
}; | ||
} | ||
} | ||
|
||
iter.fold(first, |acc, item| acc.and_then(|acc| acc.checked_add(item))) | ||
.unwrap_or(u16::MAX) | ||
Ok(total_size) | ||
} | ||
} |
32 changes: 23 additions & 9 deletions
32
packages/rs-dpp/src/data_contract/document_type/methods/max_size/v0/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,30 @@ | ||
use crate::data_contract::document_type::v0::DocumentTypeV0; | ||
|
||
use crate::ProtocolError; | ||
use platform_version::version::PlatformVersion; | ||
// If another document type (like V1) ever were to exist we would need to implement max_size_v0 again | ||
|
||
impl DocumentTypeV0 { | ||
pub(in crate::data_contract::document_type) fn max_size_v0(&self) -> u16 { | ||
let mut iter = self | ||
.flattened_properties | ||
.iter() | ||
.filter_map(|(_, document_property)| document_property.property_type.max_byte_size()); | ||
let first = Some(iter.next().unwrap_or_default()); | ||
pub(in crate::data_contract::document_type) fn max_size_v0( | ||
&self, | ||
platform_version: &PlatformVersion, | ||
) -> Result<u16, ProtocolError> { | ||
let mut total_size = 0u16; | ||
|
||
for (_, document_property) in self.flattened_properties.iter() { | ||
let maybe_size = document_property | ||
.property_type | ||
.max_byte_size(platform_version)?; | ||
|
||
if let Some(size) = maybe_size { | ||
total_size = match total_size.checked_add(size) { | ||
Some(new_total) => new_total, | ||
None => { | ||
return Ok(u16::MAX); | ||
} | ||
}; | ||
} | ||
} | ||
|
||
iter.fold(first, |acc, item| acc.and_then(|acc| acc.checked_add(item))) | ||
.unwrap_or(u16::MAX) | ||
Ok(total_size) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.