Skip to content

Commit

Permalink
fixes from comments
Browse files Browse the repository at this point in the history
  • Loading branch information
QuantumExplorer committed Mar 15, 2024
1 parent 91643df commit 2ffd6f5
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,39 +34,54 @@ 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
///
/// * `Result<Self, ProtocolError>` - A new `Document` object if successful, otherwise a `ProtocolError`.
fn try_from_create_transition(
document_create_transition: &DocumentCreateTransition,
owner_id: Identifier,
block_time: Option<TimestampMillis>,
block_time: TimestampMillis,
requires_created_at: bool,
requires_updated_at: bool,
data_contract: &DataContract,
platform_version: &PlatformVersion,
) -> Result<Self, ProtocolError>
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
///
/// * `Result<Self, ProtocolError>` - A new `Document` object if successful, otherwise a `ProtocolError`.
fn try_from_owned_create_transition(
document_create_transition: DocumentCreateTransition,
owner_id: Identifier,
block_time: Option<TimestampMillis>,
block_time: TimestampMillis,
requires_created_at: bool,
requires_updated_at: bool,
data_contract: &DataContract,
platform_version: &PlatformVersion,
) -> Result<Self, ProtocolError>
Expand All @@ -78,7 +93,9 @@ impl DocumentFromCreateTransition for Document {
fn try_from_create_transition(
document_create_transition: &DocumentCreateTransition,
owner_id: Identifier,
block_time: Option<TimestampMillis>,
block_time: TimestampMillis,
requires_created_at: bool,
requires_updated_at: bool,
data_contract: &DataContract,
platform_version: &PlatformVersion,
) -> Result<Self, ProtocolError>
Expand All @@ -90,6 +107,8 @@ impl DocumentFromCreateTransition for Document {
v0,
owner_id,
block_time,
requires_created_at,
requires_updated_at,
data_contract,
platform_version,
),
Expand All @@ -99,7 +118,9 @@ impl DocumentFromCreateTransition for Document {
fn try_from_owned_create_transition(
document_create_transition: DocumentCreateTransition,
owner_id: Identifier,
block_time: Option<TimestampMillis>,
block_time: TimestampMillis,
requires_created_at: bool,
requires_updated_at: bool,
data_contract: &DataContract,
platform_version: &PlatformVersion,
) -> Result<Self, ProtocolError>
Expand All @@ -111,6 +132,8 @@ impl DocumentFromCreateTransition for Document {
v0,
owner_id,
block_time,
requires_created_at,
requires_updated_at,
data_contract,
platform_version,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ pub trait DocumentFromCreateTransitionV0 {
fn try_from_owned_create_transition_v0(
v0: DocumentCreateTransitionV0,
owner_id: Identifier,
block_time: Option<TimestampMillis>,
block_time: TimestampMillis,
requires_created_at: bool,
requires_updated_at: bool,
data_contract: &DataContract,
platform_version: &PlatformVersion,
) -> Result<Self, ProtocolError>
Expand All @@ -184,7 +186,9 @@ pub trait DocumentFromCreateTransitionV0 {
fn try_from_create_transition_v0(
v0: &DocumentCreateTransitionV0,
owner_id: Identifier,
block_time: Option<TimestampMillis>,
block_time: TimestampMillis,
requires_created_at: bool,
requires_updated_at: bool,
data_contract: &DataContract,
platform_version: &PlatformVersion,
) -> Result<Self, ProtocolError>
Expand All @@ -196,7 +200,9 @@ impl DocumentFromCreateTransitionV0 for Document {
fn try_from_owned_create_transition_v0(
v0: DocumentCreateTransitionV0,
owner_id: Identifier,
block_time: Option<TimestampMillis>,
block_time: TimestampMillis,
requires_created_at: bool,
requires_updated_at: bool,
data_contract: &DataContract,
platform_version: &PlatformVersion,
) -> Result<Self, ProtocolError>
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -243,7 +252,9 @@ impl DocumentFromCreateTransitionV0 for Document {
fn try_from_create_transition_v0(
v0: &DocumentCreateTransitionV0,
owner_id: Identifier,
block_time: Option<TimestampMillis>,
block_time: TimestampMillis,
requires_created_at: bool,
requires_updated_at: bool,
data_contract: &DataContract,
platform_version: &PlatformVersion,
) -> Result<Self, ProtocolError>
Expand All @@ -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
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
///
Expand All @@ -38,17 +42,23 @@ pub trait DocumentFromReplaceTransition {
document_replace_transition_action: &DocumentReplaceTransition,
owner_id: Identifier,
created_at: Option<u64>,
block_time: Option<u64>,
block_time: u64,
requires_updated_at: bool,
platform_version: &PlatformVersion,
) -> Result<Self, ProtocolError>
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
///
Expand All @@ -57,7 +67,8 @@ pub trait DocumentFromReplaceTransition {
document_replace_transition_action: DocumentReplaceTransition,
owner_id: Identifier,
created_at: Option<u64>,
block_time: Option<u64>,
block_time: u64,
requires_updated_at: bool,
platform_version: &PlatformVersion,
) -> Result<Self, ProtocolError>
where
Expand All @@ -69,7 +80,8 @@ impl DocumentFromReplaceTransition for Document {
document_replace_transition: &DocumentReplaceTransition,
owner_id: Identifier,
created_at: Option<u64>,
block_time: Option<u64>,
block_time: u64,
requires_updated_at: bool,
platform_version: &PlatformVersion,
) -> Result<Self, ProtocolError> {
match document_replace_transition {
Expand All @@ -78,6 +90,7 @@ impl DocumentFromReplaceTransition for Document {
owner_id,
created_at,
block_time,
requires_updated_at,
platform_version,
),
}
Expand All @@ -87,7 +100,8 @@ impl DocumentFromReplaceTransition for Document {
document_replace_transition: DocumentReplaceTransition,
owner_id: Identifier,
created_at: Option<u64>,
block_time: Option<u64>,
block_time: u64,
requires_updated_at: bool,
platform_version: &PlatformVersion,
) -> Result<Self, ProtocolError> {
match document_replace_transition {
Expand All @@ -96,6 +110,7 @@ impl DocumentFromReplaceTransition for Document {
owner_id,
created_at,
block_time,
requires_updated_at,
platform_version,
),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,8 @@ pub trait DocumentFromReplaceTransitionV0 {
value: &DocumentReplaceTransitionV0,
owner_id: Identifier,
created_at: Option<u64>,
block_time: Option<u64>,
block_time: u64,
requires_updated_at: bool,
platform_version: &PlatformVersion,
) -> Result<Self, ProtocolError>
where
Expand All @@ -282,7 +283,8 @@ pub trait DocumentFromReplaceTransitionV0 {
value: DocumentReplaceTransitionV0,
owner_id: Identifier,
created_at: Option<u64>,
block_time: Option<u64>,
block_time: u64,
requires_updated_at: bool,
platform_version: &PlatformVersion,
) -> Result<Self, ProtocolError>
where
Expand All @@ -294,7 +296,8 @@ impl DocumentFromReplaceTransitionV0 for Document {
value: &DocumentReplaceTransitionV0,
owner_id: Identifier,
created_at: Option<u64>,
block_time: Option<u64>,
block_time: u64,
requires_updated_at: bool,
platform_version: &PlatformVersion,
) -> Result<Self, ProtocolError> {
let DocumentReplaceTransitionV0 {
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -331,7 +336,8 @@ impl DocumentFromReplaceTransitionV0 for Document {
value: DocumentReplaceTransitionV0,
owner_id: Identifier,
created_at: Option<u64>,
block_time: Option<u64>,
block_time: u64,
requires_updated_at: bool,
platform_version: &PlatformVersion,
) -> Result<Self, ProtocolError> {
let DocumentReplaceTransitionV0 {
Expand All @@ -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
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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,
)?;
Expand All @@ -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,
)?;

Expand Down

0 comments on commit 2ffd6f5

Please sign in to comment.