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
135 changes: 21 additions & 114 deletions packages/platform-test-suite/test/functional/platform/Document.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ const {
},
} = Dash;

const getDocumentObject = (document) => {
const documentObject = document.toObject();

// Delete createdAt and updatedAt fields because they could vary slightly
delete documentObject.$createdAt;
delete documentObject.$updatedAt;

return documentObject;
};

describe('Platform', () => {
describe('Document', function main() {
this.timeout(700000);
Expand Down Expand Up @@ -219,33 +229,28 @@ describe('Platform', () => {
expect(fetchedDocument)
.to
.exist();
expect(document.toObject())
.to
.deep
.equal(fetchedDocument.toObject());

expect(fetchedDocument.getUpdatedAt())
.to
.be
.deep
.equal(fetchedDocument.getCreatedAt());
.to.be.greaterThanOrEqual(document.getUpdatedAt());
expect(fetchedDocument.getCreatedAt())
.to.be.greaterThanOrEqual(document.getCreatedAt());

expect(getDocumentObject(document)).to.deep.equal(getDocumentObject(fetchedDocument));
expect(fetchedDocument.getUpdatedAt()).to.be.deep.equal(fetchedDocument.getCreatedAt());
});

it('should be able to fetch created document by created timestamp', async () => {
const [fetchedDocument] = await client.platform.documents.get(
'customContracts.indexedDocument',
{
where: [['$createdAt', '==', document.getCreatedAt()
where: [['$createdAt', '>', document.getCreatedAt()
.getTime()]],
orderBy: [['$createdAt', 'desc']],
},
);

expect(fetchedDocument)
.to
.exist();
expect(document.toObject())
.to
.deep
.equal(fetchedDocument.toObject());
expect(fetchedDocument).to.exist();
expect(getDocumentObject(document)).to.deep.equal(getDocumentObject(fetchedDocument));
});

it('should be able to update document', async () => {
Expand Down Expand Up @@ -361,65 +366,6 @@ describe('Platform', () => {
.greaterThanOrEqual(0);
});

it('should fail to update document with timestamp in violated time frame', async () => {
const [storedDocument] = await client.platform.documents.get(
'customContracts.indexedDocument',
{ where: [['$id', '==', document.getId()]] },
);

const updatedAt = storedDocument.getUpdatedAt();

updatedAt.setMinutes(updatedAt.getMinutes() - 10);

let broadcastError;

const documentsBatchTransition = await client.platform.documents.broadcast({
replace: [storedDocument],
}, identity);

// Additional wait time to mitigate testnet latency
await waitForSTPropagated();

const transitions = documentsBatchTransition.getTransitions();
transitions[0].setRevision(transitions[0].getRevision() + 1);
transitions[0].setUpdatedAt(updatedAt);

documentsBatchTransition.setTransitions(transitions);
const identityContractNonce = await client.platform
.nonceManager
.getIdentityContractNonce(identity.getId(), storedDocument.getDataContractId()) + 1;

documentsBatchTransition.setIdentityContractNonce(identityContractNonce);
const signedTransition = await signStateTransition(
client.platform,
documentsBatchTransition,
identity,
1,
);

try {
await client.platform.broadcastStateTransition(signedTransition);
} catch (e) {
client.platform.nonceManager.setIdentityContractNonce(
identity.getId(),
dataContractFixture.getId(),
identityContractNonce,
);
broadcastError = e;
}

expect(broadcastError)
.to
.exist();
expect(broadcastError.code)
.to
.be
.equal(4008);
expect(broadcastError.message)
.to
.match(/Document \w* updatedAt timestamp .* are out of block time window from .* and .*/);
});

it('should be able to delete a document', async () => {
await client.platform.documents.broadcast({
delete: [document],
Expand All @@ -437,44 +383,5 @@ describe('Platform', () => {
.not
.exist();
});

it('should fail to create a new document with timestamp in violated time frame', async () => {
document = await client.platform.documents.create(
'customContracts.indexedDocument',
identity,
{
firstName: 'myName',
lastName: 'lastName',
},
);

const timestamp = document.getCreatedAt();

timestamp.setMinutes(timestamp.getMinutes() - 10);

document.setCreatedAt(timestamp);
document.setUpdatedAt(timestamp);

let broadcastError;

try {
await client.platform.documents.broadcast({
create: [document],
}, identity);
} catch (e) {
broadcastError = e;
}

expect(broadcastError)
.to
.exist();
expect(broadcastError.message)
.to
.match(/Document \w* createdAt timestamp .* are out of block time window from .* and .*/);
expect(broadcastError.code)
.to
.be
.equal(4008);
});
});
});
3 changes: 2 additions & 1 deletion packages/rs-dpp/src/block/block_info/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::block::epoch::{Epoch, EPOCH_0};
use crate::prelude::TimestampMillis;
use bincode::{Decode, Encode};
use serde::{Deserialize, Serialize};

Expand All @@ -16,7 +17,7 @@ pub const DEFAULT_BLOCK_INFO: BlockInfo = BlockInfo {
#[derive(Clone, Default, Debug, PartialEq, Eq, Encode, Decode, Serialize, Deserialize)]
pub struct BlockInfo {
/// Block time in milliseconds
pub time_ms: u64,
pub time_ms: TimestampMillis,

/// Block height
pub height: u64,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod v0;
pub(in crate::document) use v0::*;
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use crate::document::document_methods::DocumentGetRawForDocumentTypeV0;
use crate::document::DocumentV0Getters;

pub trait DocumentIsEqualIgnoringTimestampsV0:
DocumentV0Getters + DocumentGetRawForDocumentTypeV0
{
/// Return a value given the path to its key and the document type for a contract.
fn is_equal_ignoring_timestamps_v0(&self, rhs: &Self) -> bool {
self.id() == rhs.id()
&& self.owner_id() == rhs.owner_id()
&& self.properties() == rhs.properties()
&& self.revision() == rhs.revision()
}
}
9 changes: 9 additions & 0 deletions packages/rs-dpp/src/document/document_methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ use crate::ProtocolError;
mod get_raw_for_contract;
mod get_raw_for_document_type;
mod hash;
mod is_equal_ignoring_timestamps;

pub(in crate::document) use get_raw_for_contract::*;
pub(in crate::document) use get_raw_for_document_type::*;
pub(in crate::document) use hash::*;
pub(in crate::document) use is_equal_ignoring_timestamps::*;

pub trait DocumentMethodsV0 {
/// Return a value given the path to its key and the document type for a contract.
Expand Down Expand Up @@ -39,4 +41,11 @@ pub trait DocumentMethodsV0 {
) -> Result<Vec<u8>, ProtocolError>;

fn increment_revision(&mut self) -> Result<(), ProtocolError>;

/// Returns if the documents are equal but ignoring the timestamp
fn is_equal_ignoring_timestamps(
&self,
rhs: &Self,
platform_version: &PlatformVersion,
) -> Result<bool, ProtocolError>;
}
26 changes: 25 additions & 1 deletion packages/rs-dpp/src/document/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use crate::data_contract::document_type::DocumentTypeRef;
use crate::data_contract::DataContract;
use crate::document::document_methods::{
DocumentGetRawForContractV0, DocumentGetRawForDocumentTypeV0, DocumentHashV0Method,
DocumentMethodsV0,
DocumentIsEqualIgnoringTimestampsV0, DocumentMethodsV0,
};
use crate::document::errors::DocumentError;
use crate::version::PlatformVersion;
Expand Down Expand Up @@ -178,6 +178,30 @@ impl DocumentMethodsV0 for Document {

Ok(())
}

fn is_equal_ignoring_timestamps(
&self,
rhs: &Self,
platform_version: &PlatformVersion,
) -> Result<bool, ProtocolError> {
match (self, rhs) {
(Document::V0(document_v0), Document::V0(rhs_v0)) => {
match platform_version
.dpp
.document_versions
.document_method_versions
.is_equal_ignoring_timestamps
{
0 => Ok(document_v0.is_equal_ignoring_timestamps_v0(rhs_v0)),
version => Err(ProtocolError::UnknownVersionMismatch {
method: "DocumentMethodV0::is_equal_ignoring_timestamps".to_string(),
known_versions: vec![0],
received: version,
}),
}
}
}
}
}

#[cfg(test)]
Expand Down
5 changes: 5 additions & 0 deletions packages/rs-dpp/src/document/v0/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use serde::{Deserialize, Serialize};

use crate::document::document_methods::{
DocumentGetRawForContractV0, DocumentGetRawForDocumentTypeV0, DocumentHashV0Method,
DocumentIsEqualIgnoringTimestampsV0,
};

use crate::identity::TimestampMillis;
Expand Down Expand Up @@ -64,6 +65,10 @@ impl DocumentGetRawForContractV0 for DocumentV0 {
//automatically done
}

impl DocumentIsEqualIgnoringTimestampsV0 for DocumentV0 {
//automatically done
}

impl DocumentGetRawForDocumentTypeV0 for DocumentV0 {
//automatically done
}
Expand Down
Loading