Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

feat: use contract identifier instead of cbor #180

Merged
merged 2 commits into from
Nov 13, 2022
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
44 changes: 44 additions & 0 deletions drive/src/drive/document/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ use crate::drive::object_size_info::DriveKeyInfo;
use crate::drive::object_size_info::DriveKeyInfo::Key;
use crate::drive::object_size_info::KeyValueInfo::KeyRefRequest;
use crate::drive::Drive;
use crate::error::document::DocumentError;
use crate::error::drive::DriveError;
use crate::error::Error;
use crate::fee::op::DriveOperation;
Expand Down Expand Up @@ -101,6 +102,49 @@ impl Drive {
)
}

/// Deletes a document and returns the associated fee.
/// The contract CBOR is given instead of the contract itself.
pub fn delete_document_for_contract_id(
&self,
document_id: &[u8],
contract_id: &[u8],
document_type_name: &str,
owner_id: Option<&[u8]>,
block_info: BlockInfo,
apply: bool,
transaction: TransactionArg,
) -> Result<FeeResult, Error> {
let mut drive_operations: Vec<DriveOperation> = vec![];

let contract_id_sized = <[u8; 32]>::try_from(contract_id)
.map_err(|_| Error::Document(DocumentError::InvalidContractIdSize()))?;

let contract_fetch_info = self
.get_contract_with_fetch_info(
contract_id_sized,
Some(&block_info.epoch),
transaction,
&mut drive_operations,
)?
.ok_or(Error::Document(DocumentError::ContractNotFound()))?;

let contract = &contract_fetch_info.contract;

self.delete_document_for_contract_apply_and_add_to_operations(
document_id,
contract,
document_type_name,
owner_id,
apply,
transaction,
&mut drive_operations,
)?;

let fees = calculate_fee(None, Some(drive_operations), &block_info.epoch)?;

Ok(fees)
}

/// Deletes a document.
pub fn delete_document_for_contract_apply_and_add_to_operations(
&self,
Expand Down
59 changes: 58 additions & 1 deletion drive/src/drive/document/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ use grovedb::{Element, TransactionArg};
use std::collections::HashSet;
use std::option::Option::None;

use crate::contract::document::Document;
use crate::contract::Contract;
use crate::drive::defaults::{DEFAULT_HASH_SIZE, STORAGE_FLAGS_SIZE};
use crate::drive::document::{
Expand Down Expand Up @@ -69,7 +68,10 @@ use crate::fee::{calculate_fee, FeeResult};
use dpp::data_contract::extra::DocumentType;

use crate::common::encode::encode_unsigned_integer;
use crate::contract::document::Document;
use crate::drive::block_info::BlockInfo;
use crate::error::document::DocumentError;
use crate::fee_pools::epochs::Epoch;
use dpp::data_contract::extra::encode_float;
use dpp::data_contract::extra::DriveContractExt;

Expand Down Expand Up @@ -392,6 +394,61 @@ impl Drive {
)
}

/// Deserializes a document and adds it to a contract by id.
pub fn add_serialized_document_for_contract_id(
&self,
serialized_document: &[u8],
contract_id: &[u8],
document_type_name: &str,
owner_id: Option<&[u8]>,
override_document: bool,
block_info: BlockInfo,
apply: bool,
storage_flags: Option<&StorageFlags>,
transaction: TransactionArg,
) -> Result<FeeResult, Error> {
let mut drive_operations: Vec<DriveOperation> = vec![];

let contract_id_sized = <[u8; 32]>::try_from(contract_id)
.map_err(|_| Error::Document(DocumentError::InvalidContractIdSize()))?;

let contract_fetch_info = self
.get_contract_with_fetch_info(
contract_id_sized,
Some(&block_info.epoch),
transaction,
&mut drive_operations,
)?
.ok_or(Error::Document(DocumentError::ContractNotFound()))?;

let contract = &contract_fetch_info.contract;

let document = Document::from_cbor(serialized_document, None, owner_id)?;

let document_info =
DocumentRefAndSerialization((&document, serialized_document, storage_flags));

let document_type = contract.document_type_for_name(document_type_name)?;

self.add_document_for_contract_apply_and_add_to_operations(
DocumentAndContractInfo {
document_info,
contract,
document_type,
owner_id,
},
override_document,
&block_info,
apply,
transaction,
&mut drive_operations,
)?;

let fees = calculate_fee(None, Some(drive_operations), &block_info.epoch)?;

Ok(fees)
}

/// Adds a document to a contract.
pub fn add_document_for_contract(
&self,
Expand Down
54 changes: 54 additions & 0 deletions drive/src/drive/document/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ use crate::fee::op::DriveOperation;
use crate::fee::{calculate_fee, FeeResult};

use crate::drive::block_info::BlockInfo;
use crate::error::document::DocumentError;
use dpp::data_contract::extra::DriveContractExt;

impl Drive {
Expand Down Expand Up @@ -90,6 +91,59 @@ impl Drive {
)
}

/// Updates a serialized document given a contract id and returns the associated fee.
pub fn update_document_for_contract_id(
&self,
serialized_document: &[u8],
contract_id: &[u8],
document_type: &str,
owner_id: Option<&[u8]>,
block_info: BlockInfo,
apply: bool,
storage_flags: Option<&StorageFlags>,
transaction: TransactionArg,
) -> Result<FeeResult, Error> {
let mut drive_operations: Vec<DriveOperation> = vec![];

let contract_id_sized = <[u8; 32]>::try_from(contract_id)
.map_err(|_| Error::Document(DocumentError::InvalidContractIdSize()))?;

let contract_fetch_info = self
.get_contract_with_fetch_info(
contract_id_sized,
Some(&block_info.epoch),
transaction,
&mut drive_operations,
)?
.ok_or(Error::Document(DocumentError::ContractNotFound()))?;

let contract = &contract_fetch_info.contract;

let document = Document::from_cbor(serialized_document, None, owner_id)?;

let document_info =
DocumentRefAndSerialization((&document, serialized_document, storage_flags));

let document_type = contract.document_type_for_name(document_type)?;

self.update_document_for_contract_apply_and_add_to_operations(
DocumentAndContractInfo {
document_info,
contract,
document_type,
owner_id,
},
&block_info,
apply,
transaction,
&mut drive_operations,
)?;

let fees = calculate_fee(None, Some(drive_operations), &block_info.epoch)?;

Ok(fees)
}

/// Updates a serialized document and returns the associated fee.
pub fn update_serialized_document_for_contract(
&self,
Expand Down
6 changes: 6 additions & 0 deletions drive/src/error/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,10 @@ pub enum DocumentError {
/// Error
#[error("invalid document propoerty type error: {0}")]
InvalidDocumentPropertyType(&'static str),
/// Error
#[error("invalid contract identifier size error")]
InvalidContractIdSize(),
/// Error
#[error("contact with specified identifier is not found")]
ContractNotFound(),
}
10 changes: 5 additions & 5 deletions node/Drive.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class Drive {
return driveCreateDocumentAsync.call(
this.drive,
document.toBuffer(),
document.getDataContract().toBuffer(),
document.getDataContractId().toBuffer(),
document.getType(),
document.getOwnerId().toBuffer(),
true,
Expand All @@ -139,7 +139,7 @@ class Drive {
return driveUpdateDocumentAsync.call(
this.drive,
document.toBuffer(),
document.getDataContract().toBuffer(),
document.getDataContractId().toBuffer(),
document.getType(),
document.getOwnerId().toBuffer(),
blockInfo,
Expand All @@ -149,7 +149,7 @@ class Drive {
}

/**
* @param {DataContract} dataContract
* @param {Buffer|Identifier} dataContractId
* @param {string} documentType
* @param {Identifier} documentId
* @param {BlockInfo} blockInfo
Expand All @@ -159,7 +159,7 @@ class Drive {
* @returns {Promise<FeeResult>}
*/
async deleteDocument(
dataContract,
dataContractId,
documentType,
documentId,
blockInfo,
Expand All @@ -169,7 +169,7 @@ class Drive {
return driveDeleteDocumentAsync.call(
this.drive,
documentId.toBuffer(),
dataContract.toBuffer(),
dataContractId,
documentType,
blockInfo,
!dryRun,
Expand Down
Loading