Skip to content

Commit 76bf74e

Browse files
fix(platform): document serialization v1 to fix serialization and deserialization of integers (#2578)
1 parent 1e14656 commit 76bf74e

File tree

27 files changed

+1172
-411
lines changed

27 files changed

+1172
-411
lines changed

packages/rs-dpp/src/document/document_methods/hash/v0/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub trait DocumentHashV0Method: DocumentPlatformConversionMethodsV0 {
1818
) -> Result<Vec<u8>, ProtocolError> {
1919
let mut buf = contract.id().to_vec();
2020
buf.extend(document_type.name().as_bytes()); // TODO: Why we put it here?
21-
buf.extend(self.serialize(document_type, platform_version)?);
21+
buf.extend(self.serialize(document_type, contract, platform_version)?);
2222
Ok(hash_double_to_vec(buf))
2323
}
2424
}

packages/rs-dpp/src/document/extended_document/v0/serialize.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ impl ExtendedDocumentPlatformSerializationMethodsV0 for ExtendedDocumentV0 {
3131
/// The serialization of an extended document follows the pattern:
3232
/// data contract | document type name | document
3333
fn serialize_v0(&self, platform_version: &PlatformVersion) -> Result<Vec<u8>, ProtocolError> {
34-
let mut buffer: Vec<u8> = 0.encode_var_vec(); //version 0
34+
let mut buffer: Vec<u8> = 0u64.encode_var_vec(); //version 0
3535

3636
buffer.append(
3737
&mut self
@@ -40,11 +40,11 @@ impl ExtendedDocumentPlatformSerializationMethodsV0 for ExtendedDocumentV0 {
4040
);
4141
buffer.push(self.document_type_name.len() as u8);
4242
buffer.extend(self.document_type_name.as_bytes());
43-
buffer.append(
44-
&mut self
45-
.document
46-
.serialize(self.document_type()?, platform_version)?,
47-
);
43+
buffer.append(&mut self.document.serialize(
44+
self.document_type()?,
45+
&self.data_contract,
46+
platform_version,
47+
)?);
4848
Ok(buffer)
4949
}
5050
}
@@ -110,7 +110,7 @@ impl ExtendedDocumentPlatformConversionMethodsV0 for ExtendedDocumentV0 {
110110
match platform_version
111111
.dpp
112112
.document_versions
113-
.document_serialization_version
113+
.extended_document_serialization_version
114114
.default_current_version
115115
{
116116
0 => self.serialize_v0(platform_version),

packages/rs-dpp/src/document/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ mod tests {
265265
let serialized = <Document as DocumentPlatformConversionMethodsV0>::serialize(
266266
&document,
267267
document_type,
268+
&contract,
268269
platform_version,
269270
)
270271
.expect("should serialize");
@@ -306,6 +307,7 @@ mod tests {
306307
let serialized = <Document as DocumentPlatformConversionMethodsV0>::serialize(
307308
&document,
308309
document_type,
310+
&contract,
309311
platform_version,
310312
)
311313
.expect("should serialize");
@@ -322,6 +324,7 @@ mod tests {
322324
let serialized = <Document as DocumentPlatformConversionMethodsV0>::serialize(
323325
&document,
324326
document_type,
327+
&contract,
325328
platform_version,
326329
)
327330
.expect("should serialize");

packages/rs-dpp/src/document/serialization_traits/platform_serialization_conversion/deserialize/v0/mod.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,32 @@ use platform_version::version::PlatformVersion;
66

77
pub(in crate::document) trait DocumentPlatformDeserializationMethodsV0 {
88
/// Reads a serialized document and creates a Document from it.
9+
/// Version 0 will always decode integers as i64s,
10+
/// as all integers were stored as i64 in version 0
911
fn from_bytes_v0(
1012
serialized_document: &[u8],
1113
document_type: DocumentTypeRef,
1214
platform_version: &PlatformVersion,
1315
) -> Result<Self, DataContractError>
1416
where
1517
Self: Sized;
18+
19+
/// Reads a serialized document and creates a Document from it.
20+
/// Version 1 properly uses the data contract encoded integer types
21+
fn from_bytes_v1(
22+
serialized_document: &[u8],
23+
document_type: DocumentTypeRef,
24+
platform_version: &PlatformVersion,
25+
) -> Result<Self, DataContractError>
26+
where
27+
Self: Sized;
1628
}
1729

1830
#[cfg(feature = "extended-document")]
1931
pub(in crate::document) trait ExtendedDocumentPlatformDeserializationMethodsV0 {
2032
/// Reads a serialized document and creates a Document from it.
33+
/// Version 0 will always decode integers as i64s,
34+
/// as all integers were stored as i64 in version 0
2135
fn from_bytes_v0(
2236
serialized_document: &[u8],
2337
platform_version: &PlatformVersion,

packages/rs-dpp/src/document/serialization_traits/platform_serialization_conversion/mod.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ pub(in crate::document) mod serialize;
33
mod v0;
44

55
use crate::data_contract::document_type::DocumentTypeRef;
6+
use crate::data_contract::DataContract;
67
use crate::document::{Document, DocumentV0};
78
#[cfg(feature = "validation")]
89
use crate::prelude::ConsensusValidationResult;
@@ -18,22 +19,28 @@ impl DocumentPlatformConversionMethodsV0 for Document {
1819
fn serialize(
1920
&self,
2021
document_type: DocumentTypeRef,
22+
data_contract: &DataContract,
2123
platform_version: &PlatformVersion,
2224
) -> Result<Vec<u8>, ProtocolError> {
2325
match self {
24-
Document::V0(document_v0) => document_v0.serialize(document_type, platform_version),
26+
Document::V0(document_v0) => {
27+
document_v0.serialize(document_type, data_contract, platform_version)
28+
}
2529
}
2630
}
2731

2832
fn serialize_specific_version(
2933
&self,
3034
document_type: DocumentTypeRef,
35+
data_contract: &DataContract,
3136
feature_version: FeatureVersion,
3237
) -> Result<Vec<u8>, ProtocolError> {
3338
match self {
34-
Document::V0(document_v0) => {
35-
document_v0.serialize_specific_version(document_type, feature_version)
36-
}
39+
Document::V0(document_v0) => document_v0.serialize_specific_version(
40+
document_type,
41+
data_contract,
42+
feature_version,
43+
),
3744
}
3845
}
3946

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

118125
let serialized_document = document
119-
.serialize(document_type, platform_version)
126+
.serialize(document_type, &contract, platform_version)
120127
.expect("expected to serialize");
121128

122129
let deserialized_document = Document::from_bytes(
@@ -131,7 +138,7 @@ mod tests {
131138
.random_document(Some(3333), platform_version)
132139
.expect("expected to get a random document");
133140
document
134-
.serialize(document_type, platform_version)
141+
.serialize(document_type, &contract, platform_version)
135142
.expect("expected to serialize consume");
136143
}
137144
}

packages/rs-dpp/src/document/serialization_traits/platform_serialization_conversion/serialize/v0/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,15 @@ pub(in crate::document) trait DocumentPlatformSerializationMethodsV0 {
88
///
99
/// The serialization of a document follows the pattern:
1010
/// id 32 bytes + owner_id 32 bytes + encoded values byte arrays
11+
/// In serialize v0 all integers are always encoded as i64s
1112
fn serialize_v0(&self, document_type: DocumentTypeRef) -> Result<Vec<u8>, ProtocolError>;
13+
14+
/// Serializes the document.
15+
///
16+
/// The serialization of a document follows the pattern:
17+
/// id 32 bytes + owner_id 32 bytes + encoded values byte arrays
18+
/// Serialize v1 will encode integers normally with their known size
19+
fn serialize_v1(&self, document_type: DocumentTypeRef) -> Result<Vec<u8>, ProtocolError>;
1220
}
1321

1422
#[cfg(feature = "extended-document")]

packages/rs-dpp/src/document/serialization_traits/platform_serialization_conversion/v0/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::data_contract::document_type::DocumentTypeRef;
2+
use crate::prelude::DataContract;
23
#[cfg(feature = "validation")]
34
use crate::validation::ConsensusValidationResult;
45
use crate::version::PlatformVersion;
@@ -13,6 +14,7 @@ pub trait DocumentPlatformConversionMethodsV0: Clone {
1314
fn serialize(
1415
&self,
1516
document_type: DocumentTypeRef,
17+
data_contract: &DataContract,
1618
platform_version: &PlatformVersion,
1719
) -> Result<Vec<u8>, ProtocolError>;
1820

@@ -23,6 +25,7 @@ pub trait DocumentPlatformConversionMethodsV0: Clone {
2325
fn serialize_specific_version(
2426
&self,
2527
document_type: DocumentTypeRef,
28+
data_contract: &DataContract,
2629
feature_version: FeatureVersion,
2730
) -> Result<Vec<u8>, ProtocolError>;
2831

0 commit comments

Comments
 (0)