Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub trait DocumentHashV0Method: DocumentPlatformConversionMethodsV0 {
) -> Result<Vec<u8>, ProtocolError> {
let mut buf = contract.id().to_vec();
buf.extend(document_type.name().as_bytes()); // TODO: Why we put it here?
buf.extend(self.serialize(document_type, platform_version)?);
buf.extend(self.serialize(document_type, contract, platform_version)?);
Ok(hash_double_to_vec(buf))
}
}
14 changes: 7 additions & 7 deletions packages/rs-dpp/src/document/extended_document/v0/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl ExtendedDocumentPlatformSerializationMethodsV0 for ExtendedDocumentV0 {
/// The serialization of an extended document follows the pattern:
/// data contract | document type name | document
fn serialize_v0(&self, platform_version: &PlatformVersion) -> Result<Vec<u8>, ProtocolError> {
let mut buffer: Vec<u8> = 0.encode_var_vec(); //version 0
let mut buffer: Vec<u8> = 0u64.encode_var_vec(); //version 0

buffer.append(
&mut self
Expand All @@ -40,11 +40,11 @@ impl ExtendedDocumentPlatformSerializationMethodsV0 for ExtendedDocumentV0 {
);
buffer.push(self.document_type_name.len() as u8);
buffer.extend(self.document_type_name.as_bytes());
buffer.append(
&mut self
.document
.serialize(self.document_type()?, platform_version)?,
);
buffer.append(&mut self.document.serialize(
self.document_type()?,
&self.data_contract,
platform_version,
)?);
Ok(buffer)
}
}
Expand Down Expand Up @@ -110,7 +110,7 @@ impl ExtendedDocumentPlatformConversionMethodsV0 for ExtendedDocumentV0 {
match platform_version
.dpp
.document_versions
.document_serialization_version
.extended_document_serialization_version
.default_current_version
{
0 => self.serialize_v0(platform_version),
Expand Down
3 changes: 3 additions & 0 deletions packages/rs-dpp/src/document/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ mod tests {
let serialized = <Document as DocumentPlatformConversionMethodsV0>::serialize(
&document,
document_type,
&contract,
platform_version,
)
.expect("should serialize");
Expand Down Expand Up @@ -306,6 +307,7 @@ mod tests {
let serialized = <Document as DocumentPlatformConversionMethodsV0>::serialize(
&document,
document_type,
&contract,
platform_version,
)
.expect("should serialize");
Expand All @@ -322,6 +324,7 @@ mod tests {
let serialized = <Document as DocumentPlatformConversionMethodsV0>::serialize(
&document,
document_type,
&contract,
platform_version,
)
.expect("should serialize");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,32 @@ use platform_version::version::PlatformVersion;

pub(in crate::document) trait DocumentPlatformDeserializationMethodsV0 {
/// Reads a serialized document and creates a Document from it.
/// Version 0 will always decode integers as i64s,
/// as all integers were stored as i64 in version 0
fn from_bytes_v0(
serialized_document: &[u8],
document_type: DocumentTypeRef,
platform_version: &PlatformVersion,
) -> Result<Self, DataContractError>
where
Self: Sized;

/// Reads a serialized document and creates a Document from it.
/// Version 1 properly uses the data contract encoded integer types
fn from_bytes_v1(
serialized_document: &[u8],
document_type: DocumentTypeRef,
platform_version: &PlatformVersion,
) -> Result<Self, DataContractError>
where
Self: Sized;
}

#[cfg(feature = "extended-document")]
pub(in crate::document) trait ExtendedDocumentPlatformDeserializationMethodsV0 {
/// Reads a serialized document and creates a Document from it.
/// Version 0 will always decode integers as i64s,
/// as all integers were stored as i64 in version 0
fn from_bytes_v0(
serialized_document: &[u8],
platform_version: &PlatformVersion,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub(in crate::document) mod serialize;
mod v0;

use crate::data_contract::document_type::DocumentTypeRef;
use crate::data_contract::DataContract;
use crate::document::{Document, DocumentV0};
#[cfg(feature = "validation")]
use crate::prelude::ConsensusValidationResult;
Expand All @@ -18,22 +19,28 @@ impl DocumentPlatformConversionMethodsV0 for Document {
fn serialize(
&self,
document_type: DocumentTypeRef,
data_contract: &DataContract,
platform_version: &PlatformVersion,
) -> Result<Vec<u8>, ProtocolError> {
match self {
Document::V0(document_v0) => document_v0.serialize(document_type, platform_version),
Document::V0(document_v0) => {
document_v0.serialize(document_type, data_contract, platform_version)
}
}
}

fn serialize_specific_version(
&self,
document_type: DocumentTypeRef,
data_contract: &DataContract,
feature_version: FeatureVersion,
) -> Result<Vec<u8>, ProtocolError> {
match self {
Document::V0(document_v0) => {
document_v0.serialize_specific_version(document_type, feature_version)
}
Document::V0(document_v0) => document_v0.serialize_specific_version(
document_type,
data_contract,
feature_version,
),
}
}

Expand Down Expand Up @@ -116,7 +123,7 @@ mod tests {
.expect("expected to get a random document");

let serialized_document = document
.serialize(document_type, platform_version)
.serialize(document_type, &contract, platform_version)
.expect("expected to serialize");

let deserialized_document = Document::from_bytes(
Expand All @@ -131,7 +138,7 @@ mod tests {
.random_document(Some(3333), platform_version)
.expect("expected to get a random document");
document
.serialize(document_type, platform_version)
.serialize(document_type, &contract, platform_version)
.expect("expected to serialize consume");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@ pub(in crate::document) trait DocumentPlatformSerializationMethodsV0 {
///
/// The serialization of a document follows the pattern:
/// id 32 bytes + owner_id 32 bytes + encoded values byte arrays
/// In serialize v0 all integers are always encoded as i64s
fn serialize_v0(&self, document_type: DocumentTypeRef) -> Result<Vec<u8>, ProtocolError>;

/// Serializes the document.
///
/// The serialization of a document follows the pattern:
/// id 32 bytes + owner_id 32 bytes + encoded values byte arrays
/// Serialize v1 will encode integers normally with their known size
fn serialize_v1(&self, document_type: DocumentTypeRef) -> Result<Vec<u8>, ProtocolError>;
}

#[cfg(feature = "extended-document")]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::data_contract::document_type::DocumentTypeRef;
use crate::prelude::DataContract;
#[cfg(feature = "validation")]
use crate::validation::ConsensusValidationResult;
use crate::version::PlatformVersion;
Expand All @@ -13,6 +14,7 @@ pub trait DocumentPlatformConversionMethodsV0: Clone {
fn serialize(
&self,
document_type: DocumentTypeRef,
data_contract: &DataContract,
platform_version: &PlatformVersion,
) -> Result<Vec<u8>, ProtocolError>;

Expand All @@ -23,6 +25,7 @@ pub trait DocumentPlatformConversionMethodsV0: Clone {
fn serialize_specific_version(
&self,
document_type: DocumentTypeRef,
data_contract: &DataContract,
feature_version: FeatureVersion,
) -> Result<Vec<u8>, ProtocolError>;

Expand Down
Loading
Loading