Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(dpp): document factory wouldn't allow delete transitions for immutable document types #1956

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
71 changes: 69 additions & 2 deletions packages/rs-dpp/src/document/document_factory/v0/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,8 +516,8 @@ impl DocumentFactoryV0 {
documents
.into_iter()
.map(|(document, document_type)| {
if !document_type.documents_mutable() {
return Err(DocumentError::TryingToDeleteImmutableDocument {
if !document_type.documents_can_be_deleted() {
return Err(DocumentError::TryingToDeleteIndelibleDocument {
document: Box::new(document),
}
.into());
Expand Down Expand Up @@ -552,3 +552,70 @@ impl DocumentFactoryV0 {
ids.into_iter().all_equal()
}
}

#[cfg(test)]
mod test {
use data_contracts::SystemDataContract;
use platform_version::version::PlatformVersion;
use std::collections::BTreeMap;

use crate::data_contract::accessors::v0::DataContractV0Getters;
use crate::document::document_factory::DocumentFactoryV0;
use crate::document::{Document, DocumentV0};
use crate::identifier::Identifier;
use crate::system_data_contracts::load_system_data_contract;

#[test]
/// Create a delete transition in DocumentFactoryV0 for an immutable but deletable document type
fn delete_immutable_but_deletable_documents() {
// Get a ref to the dpns preorder document type to pass to the factory
let dpns_contract =
load_system_data_contract(SystemDataContract::DPNS, PlatformVersion::latest()).unwrap();
let document_type = dpns_contract
.document_type_borrowed_for_name("preorder")
.unwrap();
let document_type_ref = document_type.as_ref();

// Create a preorder document
let document_id = Identifier::random();
let owner_id = Identifier::random();
let mut properties = BTreeMap::new();
properties.insert(
"saltedDomainHash".to_string(),
platform_value::Value::Array(vec![]),
);
let document_v0 = DocumentV0 {
id: document_id,
owner_id,
properties,
revision: Some(1),
created_at: None,
updated_at: None,
transferred_at: None,
created_at_block_height: None,
updated_at_block_height: None,
transferred_at_block_height: None,
created_at_core_block_height: None,
updated_at_core_block_height: None,
transferred_at_core_block_height: None,
};
let document = Document::V0(document_v0);

// This will be passed to the factory
let documents = vec![(document, document_type_ref)];
let mut nonce_counter = BTreeMap::new();
let platform_version = PlatformVersion::latest();

// Try to create the delete transition in the factory
let result = DocumentFactoryV0::document_delete_transitions(
documents,
&mut nonce_counter,
&platform_version,
);

// Checks
assert!(result.is_ok(), "The function should succeed");
let transitions = result.unwrap();
assert_eq!(transitions.len(), 1, "There should be one transition");
}
}
4 changes: 2 additions & 2 deletions packages/rs-dpp/src/document/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ pub enum DocumentError {
#[error("Trying To Replace Immutable Document")]
TryingToReplaceImmutableDocument { document: Box<Document> },

#[error("Trying To Delete Immutable Document")]
TryingToDeleteImmutableDocument { document: Box<Document> },
#[error("Trying to delete indelible document")]
TryingToDeleteIndelibleDocument { document: Box<Document> },

#[error("Documents have mixed owner ids")]
MismatchOwnerIdsError { documents: Vec<Document> },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -525,8 +525,8 @@ impl SpecializedDocumentFactoryV0 {
documents
.into_iter()
.map(|(document, document_type)| {
if !document_type.documents_mutable() {
return Err(DocumentError::TryingToDeleteImmutableDocument {
if !document_type.documents_can_be_deleted() {
return Err(DocumentError::TryingToDeleteIndelibleDocument {
document: Box::new(document),
}
.into());
Expand Down
2 changes: 1 addition & 1 deletion packages/wasm-dpp/src/document/errors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ pub fn from_document_to_js_error(e: DocumentError) -> JsValue {
TryingToReplaceImmutableDocumentError::new((*document).into()).into()
}
DocumentError::InvalidActionError(action) => InvalidActionError::new(action.into()).into(),
DocumentError::TryingToDeleteImmutableDocument { document } => {
DocumentError::TryingToDeleteIndelibleDocument { document } => {
TryingToDeleteImmutableDocumentError::new((*document).into()).into()
}
}
Expand Down
Loading