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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions packages/rs-dpp/src/data_contract/accessors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ impl DataContractV0Getters for DataContract {
}
}

fn system_version_type(&self) -> u16 {
match self {
DataContract::V0(_) => 0,
DataContract::V1(_) => 1,
}
}

fn version(&self) -> u32 {
match self {
DataContract::V0(v0) => v0.version(),
Expand Down
1 change: 1 addition & 0 deletions packages/rs-dpp/src/data_contract/accessors/v0/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub trait DataContractV0Getters {
fn id(&self) -> Identifier;

fn id_ref(&self) -> &Identifier;
fn system_version_type(&self) -> u16;

/// Returns the version of this data contract.
fn version(&self) -> u32;
Expand Down
7 changes: 7 additions & 0 deletions packages/rs-dpp/src/data_contract/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ pub enum DataContractConfig {
}

impl DataContractConfig {
pub fn version(&self) -> u16 {
match self {
DataContractConfig::V0(_) => 0,
DataContractConfig::V1(_) => 1,
}
}

pub fn default_for_version(
platform_version: &PlatformVersion,
) -> Result<DataContractConfig, ProtocolError> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ impl DocumentType {
/// # Parameters
///
/// * `data_contract_id`: Identifier for the data contract.
/// * `data_contract_system_version`: The version of the data contract
/// * `contract_document_types_raw`: Vector representing the raw contract document types.
/// * `definition_references`: BTreeMap representing the definition references.
/// * `documents_keep_history_contract_default`: A boolean flag that specifies the document's keep history contract default.
Expand All @@ -37,6 +38,8 @@ impl DocumentType {
#[allow(clippy::too_many_arguments)]
pub fn create_document_types_from_document_schemas(
data_contract_id: Identifier,
data_contract_system_version: u16,
contract_config_version: u16,
document_schemas: BTreeMap<DocumentName, Value>,
schema_defs: Option<&BTreeMap<String, Value>>,
token_configurations: &BTreeMap<TokenContractPosition, TokenConfiguration>,
Expand All @@ -55,6 +58,8 @@ impl DocumentType {
{
0 => DocumentType::create_document_types_from_document_schemas_v0(
data_contract_id,
data_contract_system_version,
contract_config_version,
document_schemas,
schema_defs,
token_configurations,
Expand All @@ -66,6 +71,8 @@ impl DocumentType {
// in v1 we add the ability to have contracts without documents and just tokens
1 => DocumentType::create_document_types_from_document_schemas_v1(
data_contract_id,
data_contract_system_version,
contract_config_version,
document_schemas,
schema_defs,
token_configurations,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ impl DocumentType {
#[allow(clippy::too_many_arguments)]
pub(in crate::data_contract) fn create_document_types_from_document_schemas_v0(
data_contract_id: Identifier,
data_contract_system_version: u16,
contract_config_version: u16,
document_schemas: BTreeMap<DocumentName, Value>,
schema_defs: Option<&BTreeMap<String, Value>>,
token_configurations: &BTreeMap<TokenContractPosition, TokenConfiguration>,
Expand All @@ -38,6 +40,8 @@ impl DocumentType {
{
0 => DocumentType::try_from_schema(
data_contract_id,
data_contract_system_version,
contract_config_version,
&name,
schema,
schema_defs,
Expand Down Expand Up @@ -84,6 +88,8 @@ mod tests {

let result = DocumentType::create_document_types_from_document_schemas(
id,
1,
config.version(),
Default::default(),
None,
&BTreeMap::new(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ impl DocumentType {
#[allow(clippy::too_many_arguments)]
pub(in crate::data_contract) fn create_document_types_from_document_schemas_v1(
data_contract_id: Identifier,
data_contract_system_version: u16,
contract_config_version: u16,
document_schemas: BTreeMap<DocumentName, Value>,
schema_defs: Option<&BTreeMap<String, Value>>,
token_configurations: &BTreeMap<TokenContractPosition, TokenConfiguration>,
Expand All @@ -39,6 +41,8 @@ impl DocumentType {
{
0 => DocumentType::try_from_schema(
data_contract_id,
data_contract_system_version,
contract_config_version,
&name,
schema,
schema_defs,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use crate::data_contract::errors::DataContractError;
use crate::ProtocolError;

mod create_document_types_from_document_schemas;
mod should_use_creator_id;
mod system_properties;
mod try_from_schema;

#[inline]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
use crate::data_contract::document_type::accessors::DocumentTypeV0Getters;
use crate::data_contract::document_type::{DocumentType, DocumentTypeMutRef, DocumentTypeRef};
use crate::document::transfer::Transferable;
use crate::nft::TradeMode;
use crate::ProtocolError;
use platform_version::version::PlatformVersion;

impl DocumentType {
/// A convenience method on if we should add the creator id
/// Contracts on version 0 never use creator ids
pub fn should_use_creator_id(
&self,
contract_version_type: u16,
contract_config_version_type: u16,
platform_version: &PlatformVersion,
) -> Result<bool, ProtocolError> {
should_use_creator_id_class_method(
contract_version_type,
contract_config_version_type,
self.documents_transferable(),
self.trade_mode(),
platform_version,
)
}
}

impl DocumentTypeRef<'_> {
/// A convenience method on if we should add the creator id
/// Contracts on version 0 never use creator ids
pub fn should_use_creator_id(
&self,
contract_version_type: u16,
contract_config_version_type: u16,
platform_version: &PlatformVersion,
) -> Result<bool, ProtocolError> {
should_use_creator_id_class_method(
contract_version_type,
contract_config_version_type,
self.documents_transferable(),
self.trade_mode(),
platform_version,
)
}
}

impl DocumentTypeMutRef<'_> {
/// A convenience method on if we should add the creator id
/// Contracts on version 0 never use creator ids
pub fn should_use_creator_id(
&self,
contract_version_type: u16,
contract_config_version_type: u16,
platform_version: &PlatformVersion,
) -> Result<bool, ProtocolError> {
should_use_creator_id_class_method(
contract_version_type,
contract_config_version_type,
self.documents_transferable(),
self.trade_mode(),
platform_version,
)
}
}

/// A convenience method on if we should add the creator id
fn should_use_creator_id_class_method(
contract_version_type: u16,
contract_config_version_type: u16,
transferable: Transferable,
trade_mode: TradeMode,
platform_version: &PlatformVersion,
) -> Result<bool, ProtocolError> {
match platform_version
.dpp
.contract_versions
.document_type_versions
.schema
.should_add_creator_id
{
0 => Ok(false),
1 => Ok(contract_version_type > 0
&& contract_config_version_type > 0
&& (transferable.is_transferable() || trade_mode != TradeMode::None)),
version => Err(ProtocolError::UnknownVersionMismatch {
method: "DocumentType::should_use_creator_id".to_string(),
known_versions: vec![0, 1],
received: version,
}),
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use crate::data_contract::document_type::DocumentType;
use crate::document::property_names::CREATOR_ID;
use crate::document::transfer::Transferable;
use crate::nft::TradeMode;
use crate::ProtocolError;
use platform_version::version::PlatformVersion;

const SYSTEM_PROPERTIES: [&str; 11] = [
"$id",
"$ownerId",
"$createdAt",
"$updatedAt",
"$transferredAt",
"$createdAtBlockHeight",
"$updatedAtBlockHeight",
"$transferredAtBlockHeight",
"$createdAtCoreBlockHeight",
"$updatedAtCoreBlockHeight",
"$transferredAtCoreBlockHeight",
];

impl DocumentType {
pub fn system_properties_contains(
contract_system_version: u16,
contract_config_version: u16,
transferable: Transferable,
trade_mode: TradeMode,
property_name: &str,
platform_version: &PlatformVersion,
) -> Result<bool, ProtocolError> {
match platform_version
.dpp
.contract_versions
.document_type_versions
.schema
.should_add_creator_id
{
0 => Ok(SYSTEM_PROPERTIES.contains(&property_name)),
1 => {
if property_name == CREATOR_ID
&& contract_system_version > 0
&& contract_config_version > 0
&& (transferable.is_transferable() || trade_mode != TradeMode::None)
{
Ok(true)
} else {
Ok(SYSTEM_PROPERTIES.contains(&property_name))
}
}
version => Err(ProtocolError::UnknownVersionMismatch {
method: "DocumentType::system_properties_contains".to_string(),
known_versions: vec![0, 1],
received: version,
}),
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,7 @@ mod v0;
mod v1;

const NOT_ALLOWED_SYSTEM_PROPERTIES: [&str; 1] = ["$id"];
const SYSTEM_PROPERTIES: [&str; 11] = [
"$id",
"$ownerId",
"$createdAt",
"$updatedAt",
"$transferredAt",
"$createdAtBlockHeight",
"$updatedAtBlockHeight",
"$transferredAtBlockHeight",
"$createdAtCoreBlockHeight",
"$updatedAtCoreBlockHeight",
"$transferredAtCoreBlockHeight",
];

const MAX_INDEXED_STRING_PROPERTY_LENGTH: u16 = 63;
const MAX_INDEXED_BYTE_ARRAY_PROPERTY_LENGTH: u16 = 255;
const MAX_INDEXED_ARRAY_ITEMS: usize = 1024;
Expand All @@ -40,6 +28,8 @@ impl DocumentType {
#[allow(clippy::too_many_arguments)]
pub fn try_from_schema(
data_contract_id: Identifier,
data_contract_system_version: u16,
contract_config_version: u16,
name: &str,
schema: Value,
schema_defs: Option<&BTreeMap<String, Value>>,
Expand All @@ -58,6 +48,8 @@ impl DocumentType {
{
0 => DocumentTypeV0::try_from_schema(
data_contract_id,
data_contract_system_version,
contract_config_version,
name,
schema,
schema_defs,
Expand All @@ -69,6 +61,8 @@ impl DocumentType {
.map(|document_type| document_type.into()),
1 => DocumentTypeV1::try_from_schema(
data_contract_id,
data_contract_system_version,
contract_config_version,
name,
schema,
schema_defs,
Expand Down
Loading
Loading