From 2ffd6f5d0011e86fb2fc883f5e47d418b10c988d Mon Sep 17 00:00:00 2001 From: Quantum Explorer Date: Fri, 15 Mar 2024 16:05:25 +0700 Subject: [PATCH] fixes from comments --- .../document_create_transition/mod.rs | 39 +++++++++++++++---- .../document_create_transition/v0/mod.rs | 30 ++++++++++---- .../document_replace_transition/mod.rs | 31 +++++++++++---- .../document_replace_transition/v0/mod.rs | 20 +++++++--- .../v0/mod.rs | 12 ++++-- 5 files changed, 99 insertions(+), 33 deletions(-) diff --git a/packages/rs-dpp/src/state_transition/state_transitions/document/documents_batch_transition/document_transition/document_create_transition/mod.rs b/packages/rs-dpp/src/state_transition/state_transitions/document/documents_batch_transition/document_transition/document_create_transition/mod.rs index 623b2b3e19e..f9900da9c08 100644 --- a/packages/rs-dpp/src/state_transition/state_transitions/document/documents_batch_transition/document_transition/document_create_transition/mod.rs +++ b/packages/rs-dpp/src/state_transition/state_transitions/document/documents_batch_transition/document_transition/document_create_transition/mod.rs @@ -34,12 +34,17 @@ impl Default for DocumentCreateTransition { /// document from create transition pub trait DocumentFromCreateTransition { - /// Attempts to create a new `Document` from the given `DocumentCreateTransition` reference and `owner_id`. + /// Attempts to create a new `Document` from the given `DocumentCreateTransition` reference, `owner_id`, and additional metadata. /// /// # Arguments /// - /// * `value` - A reference to the `DocumentCreateTransition` containing information about the document being created. + /// * `document_create_transition` - A reference to the `DocumentCreateTransition` containing information about the document being created. /// * `owner_id` - The `Identifier` of the document's owner. + /// * `block_time` - The timestamp (in milliseconds) representing when the document creation is being processed. + /// * `requires_created_at` - A boolean indicating if a `created_at` timestamp is required for the document. + /// * `requires_updated_at` - A boolean indicating if an `updated_at` timestamp is required for the document. + /// * `data_contract` - A reference to the `DataContract` associated with this document, defining its structure and rules. + /// * `platform_version` - A reference to the `PlatformVersion` indicating the version of the platform for compatibility. /// /// # Returns /// @@ -47,18 +52,26 @@ pub trait DocumentFromCreateTransition { fn try_from_create_transition( document_create_transition: &DocumentCreateTransition, owner_id: Identifier, - block_time: Option, + block_time: TimestampMillis, + requires_created_at: bool, + requires_updated_at: bool, data_contract: &DataContract, platform_version: &PlatformVersion, ) -> Result where Self: Sized; - /// Attempts to create a new `Document` from the given `DocumentCreateTransition` instance and `owner_id`. + + /// Attempts to create a new `Document` from the given `DocumentCreateTransition` instance, `owner_id`, and additional metadata. /// /// # Arguments /// - /// * `value` - A `DocumentCreateTransition` instance containing information about the document being created. + /// * `document_create_transition` - A `DocumentCreateTransition` instance containing information about the document being created. /// * `owner_id` - The `Identifier` of the document's owner. + /// * `block_time` - The timestamp (in milliseconds) representing when the document creation is being processed. + /// * `requires_created_at` - A boolean indicating if a `created_at` timestamp is required for the document. + /// * `requires_updated_at` - A boolean indicating if an `updated_at` timestamp is required for the document. + /// * `data_contract` - A reference to the `DataContract` associated with this document, defining its structure and rules. + /// * `platform_version` - A reference to the `PlatformVersion` indicating the version of the platform for compatibility. /// /// # Returns /// @@ -66,7 +79,9 @@ pub trait DocumentFromCreateTransition { fn try_from_owned_create_transition( document_create_transition: DocumentCreateTransition, owner_id: Identifier, - block_time: Option, + block_time: TimestampMillis, + requires_created_at: bool, + requires_updated_at: bool, data_contract: &DataContract, platform_version: &PlatformVersion, ) -> Result @@ -78,7 +93,9 @@ impl DocumentFromCreateTransition for Document { fn try_from_create_transition( document_create_transition: &DocumentCreateTransition, owner_id: Identifier, - block_time: Option, + block_time: TimestampMillis, + requires_created_at: bool, + requires_updated_at: bool, data_contract: &DataContract, platform_version: &PlatformVersion, ) -> Result @@ -90,6 +107,8 @@ impl DocumentFromCreateTransition for Document { v0, owner_id, block_time, + requires_created_at, + requires_updated_at, data_contract, platform_version, ), @@ -99,7 +118,9 @@ impl DocumentFromCreateTransition for Document { fn try_from_owned_create_transition( document_create_transition: DocumentCreateTransition, owner_id: Identifier, - block_time: Option, + block_time: TimestampMillis, + requires_created_at: bool, + requires_updated_at: bool, data_contract: &DataContract, platform_version: &PlatformVersion, ) -> Result @@ -111,6 +132,8 @@ impl DocumentFromCreateTransition for Document { v0, owner_id, block_time, + requires_created_at, + requires_updated_at, data_contract, platform_version, ), diff --git a/packages/rs-dpp/src/state_transition/state_transitions/document/documents_batch_transition/document_transition/document_create_transition/v0/mod.rs b/packages/rs-dpp/src/state_transition/state_transitions/document/documents_batch_transition/document_transition/document_create_transition/v0/mod.rs index 892450cdb0e..2eb55dec6f0 100644 --- a/packages/rs-dpp/src/state_transition/state_transitions/document/documents_batch_transition/document_transition/document_create_transition/v0/mod.rs +++ b/packages/rs-dpp/src/state_transition/state_transitions/document/documents_batch_transition/document_transition/document_create_transition/v0/mod.rs @@ -165,7 +165,9 @@ pub trait DocumentFromCreateTransitionV0 { fn try_from_owned_create_transition_v0( v0: DocumentCreateTransitionV0, owner_id: Identifier, - block_time: Option, + block_time: TimestampMillis, + requires_created_at: bool, + requires_updated_at: bool, data_contract: &DataContract, platform_version: &PlatformVersion, ) -> Result @@ -184,7 +186,9 @@ pub trait DocumentFromCreateTransitionV0 { fn try_from_create_transition_v0( v0: &DocumentCreateTransitionV0, owner_id: Identifier, - block_time: Option, + block_time: TimestampMillis, + requires_created_at: bool, + requires_updated_at: bool, data_contract: &DataContract, platform_version: &PlatformVersion, ) -> Result @@ -196,7 +200,9 @@ impl DocumentFromCreateTransitionV0 for Document { fn try_from_owned_create_transition_v0( v0: DocumentCreateTransitionV0, owner_id: Identifier, - block_time: Option, + block_time: TimestampMillis, + requires_created_at: bool, + requires_updated_at: bool, data_contract: &DataContract, platform_version: &PlatformVersion, ) -> Result @@ -216,6 +222,9 @@ impl DocumentFromCreateTransitionV0 for Document { let document_type = data_contract.document_type_for_name(document_type_name.as_str())?; + let created_at = if requires_created_at { Some(block_time) } else { None }; + let updated_at = if requires_updated_at { Some(block_time) } else { None }; + match platform_version .dpp .document_versions @@ -226,8 +235,8 @@ impl DocumentFromCreateTransitionV0 for Document { owner_id, properties: data, revision: document_type.initial_revision(), - created_at: block_time, - updated_at: block_time, + created_at, + updated_at, } .into()), version => Err(ProtocolError::UnknownVersionMismatch { @@ -243,7 +252,9 @@ impl DocumentFromCreateTransitionV0 for Document { fn try_from_create_transition_v0( v0: &DocumentCreateTransitionV0, owner_id: Identifier, - block_time: Option, + block_time: TimestampMillis, + requires_created_at: bool, + requires_updated_at: bool, data_contract: &DataContract, platform_version: &PlatformVersion, ) -> Result @@ -263,6 +274,9 @@ impl DocumentFromCreateTransitionV0 for Document { let document_type = data_contract.document_type_for_name(document_type_name.as_str())?; + let created_at = if requires_created_at { Some(block_time) } else { None }; + let updated_at = if requires_updated_at { Some(block_time) } else { None }; + match platform_version .dpp .document_versions @@ -273,8 +287,8 @@ impl DocumentFromCreateTransitionV0 for Document { owner_id, properties: data.clone(), revision: document_type.initial_revision(), - created_at: block_time, - updated_at: block_time, + created_at, + updated_at, } .into()), version => Err(ProtocolError::UnknownVersionMismatch { diff --git a/packages/rs-dpp/src/state_transition/state_transitions/document/documents_batch_transition/document_transition/document_replace_transition/mod.rs b/packages/rs-dpp/src/state_transition/state_transitions/document/documents_batch_transition/document_transition/document_replace_transition/mod.rs index 2b7830cc758..d0bb9694dff 100644 --- a/packages/rs-dpp/src/state_transition/state_transitions/document/documents_batch_transition/document_transition/document_replace_transition/mod.rs +++ b/packages/rs-dpp/src/state_transition/state_transitions/document/documents_batch_transition/document_transition/document_replace_transition/mod.rs @@ -24,12 +24,16 @@ pub enum DocumentReplaceTransition { /// document from replace transition pub trait DocumentFromReplaceTransition { - /// Attempts to create a new `Document` from the given `DocumentReplaceTransition` reference and `owner_id`. + /// Attempts to create a new `Document` from the given `DocumentReplaceTransition` reference, `owner_id`, and additional metadata. /// /// # Arguments /// - /// * `value` - A reference to the `DocumentReplaceTransition` containing information about the document being replaced. + /// * `document_replace_transition_action` - A reference to the `DocumentReplaceTransition` containing information about the document being replaced. /// * `owner_id` - The `Identifier` of the document's owner. + /// * `created_at` - An optional timestamp representing when the document was created. + /// * `block_time` - The timestamp representing the blockchain time at which the document was updated. + /// * `requires_updated_at` - A boolean indicating if an `updated_at` timestamp is required for the document. + /// * `platform_version` - A reference to the `PlatformVersion` indicating the version of the platform for compatibility. /// /// # Returns /// @@ -38,17 +42,23 @@ pub trait DocumentFromReplaceTransition { document_replace_transition_action: &DocumentReplaceTransition, owner_id: Identifier, created_at: Option, - block_time: Option, + block_time: u64, + requires_updated_at: bool, platform_version: &PlatformVersion, ) -> Result where Self: Sized; - /// Attempts to create a new `Document` from the given `DocumentReplaceTransition` instance and `owner_id`. + + /// Attempts to create a new `Document` from the given `DocumentReplaceTransition` instance, `owner_id`, and additional metadata. /// /// # Arguments /// - /// * `value` - A `DocumentReplaceTransition` instance containing information about the document being replaced. + /// * `document_replace_transition_action` - A `DocumentReplaceTransition` instance containing information about the document being replaced. /// * `owner_id` - The `Identifier` of the document's owner. + /// * `created_at` - An optional timestamp representing when the document was created. + /// * `block_time` - The timestamp representing the blockchain time at which the document was updated. + /// * `requires_updated_at` - A boolean indicating if an `updated_at` timestamp is required for the document. + /// * `platform_version` - A reference to the `PlatformVersion` indicating the version of the platform for compatibility. /// /// # Returns /// @@ -57,7 +67,8 @@ pub trait DocumentFromReplaceTransition { document_replace_transition_action: DocumentReplaceTransition, owner_id: Identifier, created_at: Option, - block_time: Option, + block_time: u64, + requires_updated_at: bool, platform_version: &PlatformVersion, ) -> Result where @@ -69,7 +80,8 @@ impl DocumentFromReplaceTransition for Document { document_replace_transition: &DocumentReplaceTransition, owner_id: Identifier, created_at: Option, - block_time: Option, + block_time: u64, + requires_updated_at: bool, platform_version: &PlatformVersion, ) -> Result { match document_replace_transition { @@ -78,6 +90,7 @@ impl DocumentFromReplaceTransition for Document { owner_id, created_at, block_time, + requires_updated_at, platform_version, ), } @@ -87,7 +100,8 @@ impl DocumentFromReplaceTransition for Document { document_replace_transition: DocumentReplaceTransition, owner_id: Identifier, created_at: Option, - block_time: Option, + block_time: u64, + requires_updated_at: bool, platform_version: &PlatformVersion, ) -> Result { match document_replace_transition { @@ -96,6 +110,7 @@ impl DocumentFromReplaceTransition for Document { owner_id, created_at, block_time, + requires_updated_at, platform_version, ), } diff --git a/packages/rs-dpp/src/state_transition/state_transitions/document/documents_batch_transition/document_transition/document_replace_transition/v0/mod.rs b/packages/rs-dpp/src/state_transition/state_transitions/document/documents_batch_transition/document_transition/document_replace_transition/v0/mod.rs index 35f1b742e07..34a8e00c0e0 100644 --- a/packages/rs-dpp/src/state_transition/state_transitions/document/documents_batch_transition/document_transition/document_replace_transition/v0/mod.rs +++ b/packages/rs-dpp/src/state_transition/state_transitions/document/documents_batch_transition/document_transition/document_replace_transition/v0/mod.rs @@ -262,7 +262,8 @@ pub trait DocumentFromReplaceTransitionV0 { value: &DocumentReplaceTransitionV0, owner_id: Identifier, created_at: Option, - block_time: Option, + block_time: u64, + requires_updated_at: bool, platform_version: &PlatformVersion, ) -> Result where @@ -282,7 +283,8 @@ pub trait DocumentFromReplaceTransitionV0 { value: DocumentReplaceTransitionV0, owner_id: Identifier, created_at: Option, - block_time: Option, + block_time: u64, + requires_updated_at: bool, platform_version: &PlatformVersion, ) -> Result where @@ -294,7 +296,8 @@ impl DocumentFromReplaceTransitionV0 for Document { value: &DocumentReplaceTransitionV0, owner_id: Identifier, created_at: Option, - block_time: Option, + block_time: u64, + requires_updated_at: bool, platform_version: &PlatformVersion, ) -> Result { let DocumentReplaceTransitionV0 { @@ -305,6 +308,8 @@ impl DocumentFromReplaceTransitionV0 for Document { let id = base.id(); + let updated_at = if requires_updated_at { Some(block_time) } else { None }; + match platform_version .dpp .document_versions @@ -316,7 +321,7 @@ impl DocumentFromReplaceTransitionV0 for Document { properties: data.clone(), revision: Some(*revision), created_at, - updated_at: block_time, + updated_at, } .into()), version => Err(ProtocolError::UnknownVersionMismatch { @@ -331,7 +336,8 @@ impl DocumentFromReplaceTransitionV0 for Document { value: DocumentReplaceTransitionV0, owner_id: Identifier, created_at: Option, - block_time: Option, + block_time: u64, + requires_updated_at: bool, platform_version: &PlatformVersion, ) -> Result { let DocumentReplaceTransitionV0 { @@ -342,6 +348,8 @@ impl DocumentFromReplaceTransitionV0 for Document { let id = base.id(); + let updated_at = if requires_updated_at { Some(block_time) } else { None }; + match platform_version .dpp .document_versions @@ -353,7 +361,7 @@ impl DocumentFromReplaceTransitionV0 for Document { properties: data, revision: Some(revision), created_at, - updated_at: block_time, + updated_at, } .into()), version => Err(ProtocolError::UnknownVersionMismatch { diff --git a/packages/rs-drive/src/drive/verify/state_transition/verify_state_transition_was_executed_with_proof/v0/mod.rs b/packages/rs-drive/src/drive/verify/state_transition/verify_state_transition_was_executed_with_proof/v0/mod.rs index bfdb7f33b7c..285057fa9d0 100644 --- a/packages/rs-drive/src/drive/verify/state_transition/verify_state_transition_was_executed_with_proof/v0/mod.rs +++ b/packages/rs-drive/src/drive/verify/state_transition/verify_state_transition_was_executed_with_proof/v0/mod.rs @@ -3,7 +3,7 @@ use std::sync::Arc; use dpp::data_contract::accessors::v0::DataContractV0Getters; use dpp::data_contract::document_type::accessors::DocumentTypeV0Getters; use dpp::data_contract::serialized_version::DataContractInSerializationFormat; -use dpp::document::{Document, DocumentV0Getters}; +use dpp::document::{Document, DocumentV0Getters, property_names}; use dpp::document::document_methods::DocumentMethodsV0; use dpp::identity::{PartialIdentity, TimestampMillis}; use dpp::prelude::{DataContract, Identifier}; @@ -123,10 +123,14 @@ impl Drive { match transition { DocumentTransition::Create(create_transition) => { let document = document.ok_or(Error::Proof(ProofError::IncorrectProof(format!("proof did not contain document with id {} expected to exist because of state transition (create)", create_transition.base().id()))))?; + let requires_created_at = document_type.required_fields().contains(property_names::CREATED_AT); + let requires_updated_at = document_type.required_fields().contains(property_names::UPDATED_AT); let expected_document = Document::try_from_create_transition( create_transition, documents_batch_transition.owner_id(), - Some(block_time), + block_time, + requires_created_at, + requires_updated_at, &contract, platform_version, )?; @@ -143,11 +147,13 @@ impl Drive { } DocumentTransition::Replace(replace_transition) => { let document = document.ok_or(Error::Proof(ProofError::IncorrectProof(format!("proof did not contain document with id {} expected to exist because of state transition (replace)", replace_transition.base().id()))))?; + let requires_updated_at = document_type.required_fields().contains(property_names::UPDATED_AT); let expected_document = Document::try_from_replace_transition( replace_transition, documents_batch_transition.owner_id(), document.created_at(), //we can trust the created at (as we don't care) - Some(block_time), + block_time, + requires_updated_at, platform_version, )?;