Skip to content

Commit f2dba50

Browse files
more validation
1 parent 81b3cae commit f2dba50

File tree

6 files changed

+65
-11
lines changed

6 files changed

+65
-11
lines changed

packages/rs-dpp/src/data_contract/document_type/class_methods/try_from_schema/v1/mod.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use crate::consensus::basic::data_contract::ContestedUniqueIndexOnMutableDocumen
2828
use crate::consensus::basic::data_contract::ContestedUniqueIndexWithUniqueIndexError;
2929
#[cfg(any(test, feature = "validation"))]
3030
use crate::consensus::basic::data_contract::InvalidDocumentTypeNameError;
31+
use crate::consensus::basic::data_contract::RedundantDocumentPaidForByTokenWithContractId;
3132
#[cfg(feature = "validation")]
3233
use crate::consensus::basic::data_contract::TokenPaymentByBurningOnlyAllowedOnInternalTokenError;
3334
#[cfg(feature = "validation")]
@@ -564,7 +565,7 @@ impl DocumentTypeV1 {
564565
.transpose()?
565566
.map(|action_cost| {
566567
// Extract an optional contract_id. Adjust the key if necessary.
567-
let contract_id = action_cost.get_optional_identifier("contractId")?;
568+
let target_contract_id = action_cost.get_optional_identifier("contractId")?;
568569
// Extract token_contract_position as an integer, then convert it.
569570
let token_contract_position =
570571
action_cost.get_integer::<TokenContractPosition>("tokenPosition")?;
@@ -579,7 +580,8 @@ impl DocumentTypeV1 {
579580

580581
#[cfg(feature = "validation")]
581582
if full_validation {
582-
if !token_configurations.contains_key(&token_contract_position) {
583+
// contract id is none if we are on our own contract
584+
if target_contract_id.is_none() && !token_configurations.contains_key(&token_contract_position) {
583585
return Err(ProtocolError::ConsensusError(
584586
ConsensusError::BasicError(
585587
BasicError::InvalidTokenPositionError(
@@ -594,13 +596,22 @@ impl DocumentTypeV1 {
594596
}
595597

596598
// If contractId is present and user tries to burn, bail out:
597-
if let Some(contract_id) = contract_id {
599+
if let Some(target_contract_id) = target_contract_id {
600+
if target_contract_id == data_contract_id {
601+
// we are in the same contract, but we set the data contract id
602+
return Err(ProtocolError::ConsensusError(
603+
ConsensusError::BasicError(
604+
BasicError::RedundantDocumentPaidForByTokenWithContractId(RedundantDocumentPaidForByTokenWithContractId::new(target_contract_id))
605+
)
606+
.into(),
607+
));
608+
}
598609
if effect == DocumentActionTokenEffect::BurnToken {
599610
return Err(ProtocolError::ConsensusError(
600611
ConsensusError::BasicError(
601612
BasicError::TokenPaymentByBurningOnlyAllowedOnInternalTokenError(
602613
TokenPaymentByBurningOnlyAllowedOnInternalTokenError::new(
603-
contract_id,
614+
target_contract_id,
604615
token_contract_position,
605616
key.to_string(),
606617
),
@@ -620,7 +631,7 @@ impl DocumentTypeV1 {
620631
.unwrap_or(GasFeesPaidBy::DocumentOwner);
621632

622633
Ok(DocumentActionTokenCost {
623-
contract_id,
634+
contract_id: target_contract_id,
624635
token_contract_position,
625636
token_amount,
626637
effect,

packages/rs-dpp/src/errors/consensus/basic/basic_error.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ use crate::consensus::basic::data_contract::{
2323
InvalidTokenDistributionFunctionInvalidParameterTupleError, InvalidTokenLanguageCodeError,
2424
InvalidTokenNameCharacterError, InvalidTokenNameLengthError, MainGroupIsNotDefinedError,
2525
NewTokensDestinationIdentityOptionRequiredError, NonContiguousContractGroupPositionsError,
26-
NonContiguousContractTokenPositionsError, SystemPropertyIndexAlreadyPresentError,
27-
UndefinedIndexPropertyError, UniqueIndicesLimitReachedError,
28-
UnknownDocumentCreationRestrictionModeError, UnknownGasFeesPaidByError,
29-
UnknownSecurityLevelError, UnknownStorageKeyRequirementsError, UnknownTradeModeError,
30-
UnknownTransferableTypeError,
26+
NonContiguousContractTokenPositionsError, RedundantDocumentPaidForByTokenWithContractId,
27+
SystemPropertyIndexAlreadyPresentError, UndefinedIndexPropertyError,
28+
UniqueIndicesLimitReachedError, UnknownDocumentCreationRestrictionModeError,
29+
UnknownGasFeesPaidByError, UnknownSecurityLevelError, UnknownStorageKeyRequirementsError,
30+
UnknownTradeModeError, UnknownTransferableTypeError,
3131
};
3232
use crate::consensus::basic::data_contract::{
3333
InvalidJsonSchemaRefError, TokenPaymentByBurningOnlyAllowedOnInternalTokenError,
@@ -564,6 +564,9 @@ pub enum BasicError {
564564

565565
#[error(transparent)]
566566
TokenNoteOnlyAllowedWhenProposerError(TokenNoteOnlyAllowedWhenProposerError),
567+
568+
#[error(transparent)]
569+
RedundantDocumentPaidForByTokenWithContractId(RedundantDocumentPaidForByTokenWithContractId),
567570
}
568571

569572
impl From<BasicError> for ConsensusError {

packages/rs-dpp/src/errors/consensus/basic/data_contract/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ mod main_group_is_not_defined;
4444
mod new_tokens_destination_identity_option_required_error;
4545
mod non_contiguous_contract_group_positions_error;
4646
mod non_contiguous_contract_token_positions_error;
47+
mod redundant_document_paid_for_by_token_with_contract_id;
4748
mod system_property_index_already_present_error;
4849
mod token_decimals_over_limit_error;
4950
mod token_payment_by_burning_only_allowed_on_internal_token_error;
@@ -107,6 +108,7 @@ pub use main_group_is_not_defined::*;
107108
pub use new_tokens_destination_identity_option_required_error::*;
108109
pub use non_contiguous_contract_group_positions_error::*;
109110
pub use non_contiguous_contract_token_positions_error::*;
111+
pub use redundant_document_paid_for_by_token_with_contract_id::*;
110112
pub use token_decimals_over_limit_error::*;
111113
pub use token_payment_by_burning_only_allowed_on_internal_token_error::*;
112114
pub use unknown_document_action_token_effect_error::*;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use crate::consensus::basic::BasicError;
2+
use crate::consensus::ConsensusError;
3+
use crate::errors::ProtocolError;
4+
use bincode::{Decode, Encode};
5+
use platform_serialization_derive::{PlatformDeserialize, PlatformSerialize};
6+
use platform_value::Identifier;
7+
use thiserror::Error;
8+
9+
#[derive(
10+
Error, Debug, Clone, PartialEq, Eq, Encode, Decode, PlatformSerialize, PlatformDeserialize,
11+
)]
12+
#[error("Document paid for by a token has a contractId {contract_id} set, which is redundant because it is targeting the current contract")]
13+
#[platform_serialize(unversioned)]
14+
pub struct RedundantDocumentPaidForByTokenWithContractId {
15+
contract_id: Identifier,
16+
}
17+
18+
impl RedundantDocumentPaidForByTokenWithContractId {
19+
pub fn new(contract_id: Identifier) -> Self {
20+
Self { contract_id }
21+
}
22+
23+
pub fn contract_id(&self) -> Identifier {
24+
self.contract_id
25+
}
26+
}
27+
28+
impl From<RedundantDocumentPaidForByTokenWithContractId> for ConsensusError {
29+
fn from(err: RedundantDocumentPaidForByTokenWithContractId) -> Self {
30+
Self::BasicError(BasicError::RedundantDocumentPaidForByTokenWithContractId(
31+
err,
32+
))
33+
}
34+
}

packages/rs-dpp/src/errors/consensus/codes.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ impl ErrorWithCode for BasicError {
116116
Self::InvalidKeywordCharacterError(_) => 10269,
117117
Self::InvalidKeywordLengthError(_) => 10270,
118118
Self::DecimalsOverLimitError(_) => 10271,
119+
Self::RedundantDocumentPaidForByTokenWithContractId(_) => 10272,
119120

120121
// Group Errors: 10350-10399
121122
Self::GroupPositionDoesNotExistError(_) => 10350,

packages/wasm-dpp/src/errors/consensus/consensus_error.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ use dpp::consensus::state::data_trigger::DataTriggerError::{
6161
DataTriggerConditionError, DataTriggerExecutionError, DataTriggerInvalidResultError,
6262
};
6363
use wasm_bindgen::{JsError, JsValue};
64-
use dpp::consensus::basic::data_contract::{ContestedUniqueIndexOnMutableDocumentTypeError, ContestedUniqueIndexWithUniqueIndexError, DataContractTokenConfigurationUpdateError, DecimalsOverLimitError, DuplicateKeywordsError, GroupExceedsMaxMembersError, GroupMemberHasPowerOfZeroError, GroupMemberHasPowerOverLimitError, GroupNonUnilateralMemberPowerHasLessThanRequiredPowerError, GroupPositionDoesNotExistError, GroupRequiredPowerIsInvalidError, GroupTotalPowerLessThanRequiredError, InvalidDescriptionLengthError, InvalidDocumentTypeRequiredSecurityLevelError, InvalidKeywordCharacterError, InvalidKeywordLengthError, InvalidTokenBaseSupplyError, InvalidTokenDistributionFunctionDivideByZeroError, InvalidTokenDistributionFunctionIncoherenceError, InvalidTokenDistributionFunctionInvalidParameterError, InvalidTokenDistributionFunctionInvalidParameterTupleError, InvalidTokenLanguageCodeError, InvalidTokenNameCharacterError, InvalidTokenNameLengthError, MainGroupIsNotDefinedError, NewTokensDestinationIdentityOptionRequiredError, NonContiguousContractGroupPositionsError, NonContiguousContractTokenPositionsError, TokenPaymentByBurningOnlyAllowedOnInternalTokenError, TooManyKeywordsError, UnknownDocumentActionTokenEffectError, UnknownDocumentCreationRestrictionModeError, UnknownGasFeesPaidByError, UnknownSecurityLevelError, UnknownStorageKeyRequirementsError, UnknownTradeModeError, UnknownTransferableTypeError};
64+
use dpp::consensus::basic::data_contract::{ContestedUniqueIndexOnMutableDocumentTypeError, ContestedUniqueIndexWithUniqueIndexError, DataContractTokenConfigurationUpdateError, DecimalsOverLimitError, DuplicateKeywordsError, GroupExceedsMaxMembersError, GroupMemberHasPowerOfZeroError, GroupMemberHasPowerOverLimitError, GroupNonUnilateralMemberPowerHasLessThanRequiredPowerError, GroupPositionDoesNotExistError, GroupRequiredPowerIsInvalidError, GroupTotalPowerLessThanRequiredError, InvalidDescriptionLengthError, InvalidDocumentTypeRequiredSecurityLevelError, InvalidKeywordCharacterError, InvalidKeywordLengthError, InvalidTokenBaseSupplyError, InvalidTokenDistributionFunctionDivideByZeroError, InvalidTokenDistributionFunctionIncoherenceError, InvalidTokenDistributionFunctionInvalidParameterError, InvalidTokenDistributionFunctionInvalidParameterTupleError, InvalidTokenLanguageCodeError, InvalidTokenNameCharacterError, InvalidTokenNameLengthError, MainGroupIsNotDefinedError, NewTokensDestinationIdentityOptionRequiredError, NonContiguousContractGroupPositionsError, NonContiguousContractTokenPositionsError, RedundantDocumentPaidForByTokenWithContractId, TokenPaymentByBurningOnlyAllowedOnInternalTokenError, TooManyKeywordsError, UnknownDocumentActionTokenEffectError, UnknownDocumentCreationRestrictionModeError, UnknownGasFeesPaidByError, UnknownSecurityLevelError, UnknownStorageKeyRequirementsError, UnknownTradeModeError, UnknownTransferableTypeError};
6565
use dpp::consensus::basic::document::{ContestedDocumentsTemporarilyNotAllowedError, DocumentCreationNotAllowedError, DocumentFieldMaxSizeExceededError, MaxDocumentsTransitionsExceededError, MissingPositionsInDocumentTypePropertiesError};
6666
use dpp::consensus::basic::group::GroupActionNotAllowedOnTransitionError;
6767
use dpp::consensus::basic::identity::{DataContractBoundsNotPresentError, DisablingKeyIdAlsoBeingAddedInSameTransitionError, InvalidIdentityCreditWithdrawalTransitionAmountError, InvalidIdentityUpdateTransitionDisableKeysError, InvalidIdentityUpdateTransitionEmptyError, TooManyMasterPublicKeyError, WithdrawalOutputScriptNotAllowedWhenSigningWithOwnerKeyError};
@@ -824,6 +824,9 @@ fn from_basic_error(basic_error: &BasicError) -> JsValue {
824824
BasicError::TokenNoteOnlyAllowedWhenProposerError(e) => {
825825
generic_consensus_error!(TokenNoteOnlyAllowedWhenProposerError, e).into()
826826
}
827+
BasicError::RedundantDocumentPaidForByTokenWithContractId(e) => {
828+
generic_consensus_error!(RedundantDocumentPaidForByTokenWithContractId, e).into()
829+
}
827830
}
828831
}
829832

0 commit comments

Comments
 (0)