Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@ use crate::prelude::TimestampMillis;
use super::validation_result::TimeWindowValidationResult;

pub const BLOCK_TIME_WINDOW_MINUTES: u64 = 5;
pub const BLOCK_TIME_WINDOW_MILLIS: u64 = BLOCK_TIME_WINDOW_MINUTES * 60 * 1000;
pub const BLOCK_TIME_WINDOW_MILLIS: TimestampMillis = BLOCK_TIME_WINDOW_MINUTES * 60 * 1000;

pub fn validate_time_in_block_time_window(
last_block_header_time_millis: TimestampMillis,
time_to_check_millis: TimestampMillis,
) -> TimeWindowValidationResult {
let time_window_start = last_block_header_time_millis - BLOCK_TIME_WINDOW_MILLIS;
let time_window_end = last_block_header_time_millis + BLOCK_TIME_WINDOW_MILLIS;
let maybe_time_window_start =
last_block_header_time_millis.checked_sub(BLOCK_TIME_WINDOW_MILLIS);
let maybe_time_window_end = last_block_header_time_millis.checked_add(BLOCK_TIME_WINDOW_MILLIS);

let valid =
time_to_check_millis >= time_window_start && time_to_check_millis <= time_window_end;
let time_window_start = maybe_time_window_start.unwrap_or(TimestampMillis::MIN);
let time_window_end = maybe_time_window_end.unwrap_or(TimestampMillis::MAX);

let valid = maybe_time_window_start.is_some()
&& maybe_time_window_end.is_some()
&& (time_to_check_millis >= time_window_start && time_to_check_millis <= time_window_end);

TimeWindowValidationResult {
time_window_start,
Expand Down
5 changes: 3 additions & 2 deletions packages/rs-dpp/src/dash_platform_protocol.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::prelude::ProtocolVersion;
use crate::BlsModule;
use std::sync::Arc;

Expand All @@ -8,7 +9,7 @@ use crate::version::{ProtocolVersionValidator, COMPATIBILITY_MAP, LATEST_VERSION

pub struct DashPlatformProtocol<SR, BLS: BlsModule> {
/// Version of protocol
pub protocol_version: u32,
pub protocol_version: ProtocolVersion,
/// Public facing facades to interact with the library
pub identities: IdentityFacade<BLS>,
/// State Repository provides the access to the stateful validation
Expand Down Expand Up @@ -51,5 +52,5 @@ impl<SR, BLS: BlsModule> DashPlatformProtocol<SR, BLS> {

#[derive(Default)]
pub struct DPPOptions {
pub current_protocol_version: Option<u32>,
pub current_protocol_version: Option<ProtocolVersion>,
}
5 changes: 3 additions & 2 deletions packages/rs-dpp/src/data_contract/data_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::data_contract::contract_config::{
};

use crate::data_contract::get_binary_properties_from_schema::get_binary_properties;
use crate::prelude::{DataContractVersion, ProtocolVersion};
use crate::util::cbor_value::CborCanonicalMap;
use crate::util::deserializer;
use crate::util::deserializer::SplitProtocolVersionOutcome;
Expand Down Expand Up @@ -80,12 +81,12 @@ impl Convertible for DataContract {
#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct DataContract {
pub protocol_version: u32,
pub protocol_version: ProtocolVersion,
#[serde(rename = "$id")]
pub id: Identifier,
#[serde(rename = "$schema")]
pub schema: String,
pub version: u32,
pub version: DataContractVersion,
pub owner_id: Identifier,

#[serde(rename = "documents")]
Expand Down
4 changes: 2 additions & 2 deletions packages/rs-dpp/src/data_contract/data_contract_facade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::data_contract::state_transition::{
use crate::data_contract::validation::data_contract_validator::DataContractValidator;
use crate::data_contract::{DataContract, DataContractFactory};
use crate::document::document_transition::document_base_transition::JsonValue;
use crate::prelude::{Identifier, ValidationResult};
use crate::prelude::{Identifier, ProtocolVersion, ValidationResult};
use crate::version::ProtocolVersionValidator;
use crate::ProtocolError;
use std::sync::Arc;
Expand All @@ -16,7 +16,7 @@ pub struct DataContractFacade {

impl DataContractFacade {
pub fn new(
protocol_version: u32,
protocol_version: ProtocolVersion,
protocol_version_validator: Arc<ProtocolVersionValidator>,
) -> Self {
let validator = Arc::new(DataContractValidator::new(protocol_version_validator));
Expand Down
10 changes: 7 additions & 3 deletions packages/rs-dpp/src/data_contract/data_contract_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use data_contract::state_transition::property_names as st_prop;

use crate::data_contract::errors::InvalidDataContractError;
use crate::data_contract::property_names;
use crate::prelude::ProtocolVersion;
use crate::util::serializer::value_to_cbor;
use crate::{
data_contract::{self, generate_data_contract_id},
Expand Down Expand Up @@ -35,13 +36,16 @@ impl EntropyGenerator for DefaultEntropyGenerator {
}

pub struct DataContractFactory {
protocol_version: u32,
protocol_version: ProtocolVersion,
validate_data_contract: Arc<DataContractValidator>,
entropy_generator: Box<dyn EntropyGenerator>,
}

impl DataContractFactory {
pub fn new(protocol_version: u32, validate_data_contract: Arc<DataContractValidator>) -> Self {
pub fn new(
protocol_version: ProtocolVersion,
validate_data_contract: Arc<DataContractValidator>,
) -> Self {
Self {
protocol_version,
validate_data_contract,
Expand All @@ -50,7 +54,7 @@ impl DataContractFactory {
}

pub fn new_with_entropy_generator(
protocol_version: u32,
protocol_version: ProtocolVersion,
validate_data_contract: Arc<DataContractValidator>,
entropy_generator: Box<dyn EntropyGenerator>,
) -> Self {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ use super::{
use crate::data_contract::document_type::{property_names, ArrayFieldType};
use crate::data_contract::errors::{DataContractError, StructureError};

use crate::prelude::ProtocolVersion;
use crate::ProtocolError;
use platform_value::btreemap_extensions::BTreeValueMapHelper;
use platform_value::Value;
use serde::{Deserialize, Serialize};

pub const PROTOCOL_VERSION: u32 = 1;
pub const PROTOCOL_VERSION: ProtocolVersion = 1;
pub const CONTRACT_DOCUMENTS_PATH_HEIGHT: u16 = 4;
pub const BASE_CONTRACT_ROOT_PATH_SIZE: usize = 33; // 1 + 32
pub const BASE_CONTRACT_KEEPING_HISTORY_STORAGE_PATH_SIZE: usize = 34; // 1 + 32 + 1
Expand Down
5 changes: 3 additions & 2 deletions packages/rs-dpp/src/data_contract/extra/common.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::data_contract::errors::StructureError;
use crate::prelude::ProtocolVersion;
use crate::util::cbor_value::cbor_value_into_json_value;
use crate::util::serializer::value_to_cbor;
use crate::ProtocolError;
Expand Down Expand Up @@ -273,14 +274,14 @@ pub fn json_document_to_value(path: impl AsRef<Path>) -> Result<serde_json::Valu
/// Reads a JSON file and converts it to CBOR.
pub fn json_document_to_cbor(
path: impl AsRef<Path>,
protocol_version: Option<u32>,
protocol_version: Option<ProtocolVersion>,
) -> Result<Vec<u8>, ProtocolError> {
let json = json_document_to_value(path)?;
value_to_cbor(json, protocol_version)
}

/// Make sure the protocol version is correct.
pub const fn check_protocol_version(_version: u32) -> bool {
pub const fn check_protocol_version(_version: ProtocolVersion) -> bool {
// Temporary disabled due protocol version is dynamic and goes from consensus params
true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use serde_json::Value as JsonValue;
use crate::{
data_contract::DataContract,
identity::KeyID,
prelude::Identifier,
prelude::{Identifier, ProtocolVersion},
state_transition::{
state_transition_execution_context::StateTransitionExecutionContext,
StateTransitionConvert, StateTransitionIdentitySigned, StateTransitionLike,
Expand All @@ -25,7 +25,7 @@ pub mod validation;
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DataContractCreateTransition {
pub protocol_version: u32,
pub protocol_version: ProtocolVersion,
#[serde(rename = "type")]
pub transition_type: StateTransitionType,
// we want to skip serialization of transitions, as we does it manually in `to_object()` and `to_json()`
Expand Down Expand Up @@ -57,7 +57,7 @@ impl DataContractCreateTransition {
mut raw_data_contract_update_transition: JsonValue,
) -> Result<DataContractCreateTransition, ProtocolError> {
Ok(DataContractCreateTransition {
protocol_version: raw_data_contract_update_transition.get_u64(PROTOCOL_VERSION)? as u32,
protocol_version: raw_data_contract_update_transition.get_u32(PROTOCOL_VERSION)?,
signature: raw_data_contract_update_transition
.remove_into(SIGNATURE)
.unwrap_or_default(),
Expand All @@ -80,7 +80,7 @@ impl DataContractCreateTransition {
&self.data_contract
}

pub fn get_protocol_version(&self) -> u32 {
pub fn get_protocol_version(&self) -> ProtocolVersion {
self.protocol_version
}

Expand Down Expand Up @@ -114,7 +114,7 @@ impl StateTransitionIdentitySigned for DataContractCreateTransition {
}

impl StateTransitionLike for DataContractCreateTransition {
fn get_protocol_version(&self) -> u32 {
fn get_protocol_version(&self) -> ProtocolVersion {
self.protocol_version
}
/// returns the type of State Transition
Expand Down Expand Up @@ -268,8 +268,8 @@ mod test {
assert_eq!(
version::LATEST_VERSION,
json_object
.get_u64(PROTOCOL_VERSION)
.expect("the protocol version should be present") as u32
.get_u32(PROTOCOL_VERSION)
.expect("the protocol version should be present")
);

assert_eq!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde_json::Value as JsonValue;
use crate::{
data_contract::DataContract,
identity::KeyID,
prelude::Identifier,
prelude::{Identifier, ProtocolVersion},
state_transition::{
state_transition_execution_context::StateTransitionExecutionContext,
StateTransitionConvert, StateTransitionIdentitySigned, StateTransitionLike,
Expand All @@ -22,7 +22,7 @@ pub mod validation;
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DataContractUpdateTransition {
pub protocol_version: u32,
pub protocol_version: ProtocolVersion,
#[serde(rename = "type")]
pub transition_type: StateTransitionType,
// we want to skip serialization of transitions, as we does it manually in `to_object()` and `to_json()`
Expand Down Expand Up @@ -52,7 +52,7 @@ impl DataContractUpdateTransition {
mut raw_data_contract_update_transition: JsonValue,
) -> Result<DataContractUpdateTransition, ProtocolError> {
Ok(DataContractUpdateTransition {
protocol_version: raw_data_contract_update_transition.get_u64(PROTOCOL_VERSION)? as u32,
protocol_version: raw_data_contract_update_transition.get_u32(PROTOCOL_VERSION)?,
signature: raw_data_contract_update_transition
.remove_into(SIGNATURE)
.unwrap_or_default(),
Expand Down Expand Up @@ -96,7 +96,7 @@ impl StateTransitionIdentitySigned for DataContractUpdateTransition {
}

impl StateTransitionLike for DataContractUpdateTransition {
fn get_protocol_version(&self) -> u32 {
fn get_protocol_version(&self) -> ProtocolVersion {
self.protocol_version
}
/// returns the type of State Transition
Expand Down Expand Up @@ -249,8 +249,8 @@ mod test {
assert_eq!(
version::LATEST_VERSION,
json_object
.get_u64(PROTOCOL_VERSION)
.expect("the protocol version should be present") as u32
.get_u32(PROTOCOL_VERSION)
.expect("the protocol version should be present")
);

assert_eq!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::consensus::basic::data_contract::{
use crate::consensus::basic::decode::ProtocolVersionParsingError;
use crate::consensus::basic::invalid_data_contract_version_error::InvalidDataContractVersionError;
use crate::consensus::ConsensusError;
use crate::prelude::ProtocolVersion;
use crate::state_transition::state_transition_execution_context::StateTransitionExecutionContext;
use crate::{
consensus::basic::BasicError,
Expand Down Expand Up @@ -128,9 +129,13 @@ where
}
};

let new_version = raw_data_contract.get_u64(contract_property_names::VERSION)? as u32;
let new_version = raw_data_contract.get_u32(contract_property_names::VERSION)?;
let old_version = existing_data_contract.version;
if (new_version - old_version) != 1 {
if (new_version
.checked_sub(old_version)
.unwrap_or(ProtocolVersion::MAX))
!= 1
{
validation_result.add_error(BasicError::InvalidDataContractVersionError(
InvalidDataContractVersionError::new(old_version + 1, new_version),
))
Expand Down
10 changes: 6 additions & 4 deletions packages/rs-dpp/src/document/document_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::consensus::basic::document::InvalidDocumentTypeError;
use crate::{
data_contract::{errors::DataContractError, DataContract},
decode_protocol_entity_factory::DecodeProtocolEntity,
prelude::Identifier,
prelude::{Identifier, ProtocolVersion},
state_repository::StateRepositoryLike,
util::entropy_generator,
util::{json_schema::JsonSchemaExt, json_value::JsonValueExt},
Expand Down Expand Up @@ -58,7 +58,7 @@ const DOCUMENT_REPLACE_KEYS_TO_STAY: [&str; 5] = [

/// Factory for creating documents
pub struct DocumentFactory<ST> {
protocol_version: u32,
protocol_version: ProtocolVersion,
document_validator: DocumentValidator,
data_contract_fetcher_and_validator: DataContractFetcherAndValidator<ST>,
}
Expand All @@ -77,7 +77,7 @@ where
ST: StateRepositoryLike,
{
pub fn new(
protocol_version: u32,
protocol_version: ProtocolVersion,
validate_document: DocumentValidator,
data_contract_fetcher_and_validator: DataContractFetcherAndValidator<ST>,
) -> Self {
Expand Down Expand Up @@ -333,7 +333,9 @@ where
PROPERTY_ACTION.to_string(),
serde_json::to_value(Action::Replace)?,
);
let new_revision = document_revision + 1;
let new_revision = document_revision
.checked_add(1)
.ok_or(ProtocolError::Overflow("max revision reached"))?;
map.insert(PROPERTY_REVISION.to_string(), json!(new_revision));

// If document have an originally set `updatedAt`
Expand Down
2 changes: 1 addition & 1 deletion packages/rs-dpp/src/document/document_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl DocumentValidator {
return Ok(result);
}

let protocol_version = raw_document.get_u64(PROPERTY_PROTOCOL_VERSION)? as u32;
let protocol_version = raw_document.get_u32(PROPERTY_PROTOCOL_VERSION)?;
result.merge(self.protocol_version_validator.validate(protocol_version)?);

Ok(result)
Expand Down
12 changes: 7 additions & 5 deletions packages/rs-dpp/src/document/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ use crate::data_contract::DataContract;
use crate::errors::ProtocolError;
use crate::identifier::Identifier;
use crate::metadata::Metadata;
use crate::prelude::ProtocolVersion;
use crate::prelude::Revision;
use crate::prelude::TimestampMillis;
use crate::util::cbor_value::CborCanonicalMap;
use crate::util::cbor_value::FieldType;
use crate::util::deserializer::SplitProtocolVersionOutcome;
Expand Down Expand Up @@ -52,23 +55,22 @@ pub const IDENTIFIER_FIELDS: [&str; 3] = [
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct Document {
#[serde(rename = "$protocolVersion")]
pub protocol_version: u32,
pub protocol_version: ProtocolVersion,
#[serde(rename = "$id")]
pub id: Identifier,
#[serde(rename = "$type")]
/// TODO: Why not &str?
pub document_type: String,
#[serde(rename = "$revision")]
pub revision: u32,
pub revision: Revision,
#[serde(rename = "$dataContractId")]
pub data_contract_id: Identifier,
#[serde(rename = "$ownerId")]
pub owner_id: Identifier,
#[serde(rename = "$createdAt", skip_serializing_if = "Option::is_none")]
// TODO: Must be TimestampMillis
pub created_at: Option<i64>,
pub created_at: Option<TimestampMillis>,
#[serde(rename = "$updatedAt", skip_serializing_if = "Option::is_none")]
pub updated_at: Option<i64>,
pub updated_at: Option<TimestampMillis>,
// the serde_json::Value preserves the order (see .toml file)
#[serde(flatten)]
pub data: JsonValue,
Expand Down
Loading