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

feat(platform)!: enhance token configuration and validation mechanisms #2439

Merged
merged 6 commits into from
Jan 23, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl TokenConfigurationV0Getters for TokenConfiguration {
}

/// Returns the maximum supply.
fn max_supply(&self) -> Option<u64> {
fn max_supply(&self) -> Option<TokenAmount> {
match self {
TokenConfiguration::V0(v0) => v0.max_supply(),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub enum AuthorizedActionTakers {
#[default]
NoOne,
ContractOwner,
Identity(Identifier),
MainGroup,
Group(GroupContractPosition),
}
Expand All @@ -26,6 +27,7 @@ impl fmt::Display for AuthorizedActionTakers {
AuthorizedActionTakers::ContractOwner => write!(f, "ContractOwner"),
AuthorizedActionTakers::MainGroup => write!(f, "MainGroup"),
AuthorizedActionTakers::Group(position) => write!(f, "Group(Position: {})", position),
AuthorizedActionTakers::Identity(identifier) => write!(f, "Identity({})", identifier),
}
}
}
Expand All @@ -51,6 +53,12 @@ impl AuthorizedActionTakers {
}
},

// Only an identity is allowed
AuthorizedActionTakers::Identity(identity) => match action_taker {
ActionTaker::SingleIdentity(action_taker) => action_taker == identity,
ActionTaker::SpecifiedIdentities(action_takers) => action_takers.contains(identity),
},

// MainGroup allows multiparty actions with specific power requirements
AuthorizedActionTakers::MainGroup => {
if let Some(main_group_contract_position) = &main_group {
Expand Down
7 changes: 2 additions & 5 deletions packages/rs-dpp/src/errors/consensus/basic/basic_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ use crate::consensus::basic::group::GroupActionNotAllowedOnTransitionError;
use crate::consensus::basic::overflow_error::OverflowError;
use crate::consensus::basic::token::{
ChoosingTokenMintRecipientNotAllowedError, ContractHasNoTokensError,
DestinationIdentityForTokenMintingNotSetError, InvalidActionIdError, InvalidGroupPositionError,
InvalidTokenIdError, InvalidTokenPositionError, TokenTransferToOurselfError,
DestinationIdentityForTokenMintingNotSetError, InvalidActionIdError, InvalidTokenIdError,
InvalidTokenPositionError, TokenTransferToOurselfError,
};
use crate::consensus::basic::unsupported_version_error::UnsupportedVersionError;
use crate::consensus::basic::value_error::ValueError;
Expand Down Expand Up @@ -438,9 +438,6 @@ pub enum BasicError {
#[error(transparent)]
ContractHasNoTokensError(ContractHasNoTokensError),

#[error(transparent)]
InvalidGroupPositionError(InvalidGroupPositionError),

#[error(transparent)]
GroupPositionDoesNotExistError(GroupPositionDoesNotExistError),

Expand Down
2 changes: 0 additions & 2 deletions packages/rs-dpp/src/errors/consensus/basic/token/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ mod choosing_token_mint_recipient_not_allowed_error;
mod contract_has_no_tokens_error;
mod destination_identity_for_token_minting_not_set_error;
mod invalid_action_id_error;
mod invalid_group_position_error;
mod invalid_token_id_error;
mod invalid_token_position_error;
mod token_transfer_to_ourselves_error;
Expand All @@ -11,7 +10,6 @@ pub use choosing_token_mint_recipient_not_allowed_error::*;
pub use contract_has_no_tokens_error::*;
pub use destination_identity_for_token_minting_not_set_error::*;
pub use invalid_action_id_error::*;
pub use invalid_group_position_error::*;
pub use invalid_token_id_error::*;
pub use invalid_token_position_error::*;
pub use token_transfer_to_ourselves_error::*;
22 changes: 14 additions & 8 deletions packages/rs-dpp/src/errors/consensus/codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,13 @@ impl ErrorWithCode for BasicError {
Self::NonContiguousContractTokenPositionsError(_) => 10253,

// Group Errors: 10350-10399
Self::InvalidGroupPositionError(_) => 10350,
Self::GroupPositionDoesNotExistError(_) => 10351,
Self::GroupActionNotAllowedOnTransitionError(_) => 10352,
Self::GroupTotalPowerLessThanRequiredError(_) => 10353,
Self::GroupNonUnilateralMemberPowerHasLessThanRequiredPowerError(_) => 10354,
Self::GroupExceedsMaxMembersError(_) => 10355,
Self::GroupMemberHasPowerOfZeroError(_) => 10356,
Self::GroupMemberHasPowerOverLimitError(_) => 10357,
Self::GroupPositionDoesNotExistError(_) => 10350,
Self::GroupActionNotAllowedOnTransitionError(_) => 10351,
Self::GroupTotalPowerLessThanRequiredError(_) => 10352,
Self::GroupNonUnilateralMemberPowerHasLessThanRequiredPowerError(_) => 10353,
Self::GroupExceedsMaxMembersError(_) => 10354,
Self::GroupMemberHasPowerOfZeroError(_) => 10355,
Self::GroupMemberHasPowerOverLimitError(_) => 10356,

// Document Errors: 10400-10449
Self::DataContractNotPresentError { .. } => 10400,
Expand Down Expand Up @@ -248,6 +247,13 @@ impl ErrorWithCode for StateError {
Self::UnauthorizedTokenActionError(_) => 40151,
Self::IdentityTokenAccountFrozenError(_) => 40152,
Self::IdentityTokenAccountNotFrozenError(_) => 40153,
Self::TokenSettingMaxSupplyToLessThanCurrentSupplyError(_) => 40154,
Self::TokenMintPastMaxSupplyError(_) => 40155,
Self::NewTokensDestinationIdentityDoesNotExistError(_) => 40156,
Self::NewAuthorizedActionTakerIdentityDoesNotExistError(_) => 40157,
Self::NewAuthorizedActionTakerGroupDoesNotExistError(_) => 40158,
Self::NewAuthorizedActionTakerMainGroupNotSetError(_) => 40159,
Self::InvalidGroupPositionError(_) => 40160,

// Identity Errors: 40200-40299
Self::IdentityAlreadyExistsError(_) => 40200,
Expand Down
27 changes: 26 additions & 1 deletion packages/rs-dpp/src/errors/consensus/state/state_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use crate::consensus::state::identity::missing_transfer_key_error::MissingTransf
use crate::consensus::state::identity::no_transfer_key_for_core_withdrawal_available_error::NoTransferKeyForCoreWithdrawalAvailableError;
use crate::consensus::state::prefunded_specialized_balances::prefunded_specialized_balance_insufficient_error::PrefundedSpecializedBalanceInsufficientError;
use crate::consensus::state::prefunded_specialized_balances::prefunded_specialized_balance_not_found_error::PrefundedSpecializedBalanceNotFoundError;
use crate::consensus::state::token::{IdentityDoesNotHaveEnoughTokenBalanceError, IdentityTokenAccountFrozenError, IdentityTokenAccountNotFrozenError, UnauthorizedTokenActionError};
use crate::consensus::state::token::{IdentityDoesNotHaveEnoughTokenBalanceError, IdentityTokenAccountFrozenError, IdentityTokenAccountNotFrozenError, InvalidGroupPositionError, NewAuthorizedActionTakerGroupDoesNotExistError, NewAuthorizedActionTakerIdentityDoesNotExistError, NewAuthorizedActionTakerMainGroupNotSetError, NewTokensDestinationIdentityDoesNotExistError, TokenMintPastMaxSupplyError, TokenSettingMaxSupplyToLessThanCurrentSupplyError, UnauthorizedTokenActionError};
use crate::consensus::state::voting::masternode_incorrect_voter_identity_id_error::MasternodeIncorrectVoterIdentityIdError;
use crate::consensus::state::voting::masternode_incorrect_voting_address_error::MasternodeIncorrectVotingAddressError;
use crate::consensus::state::voting::masternode_not_found_error::MasternodeNotFoundError;
Expand Down Expand Up @@ -230,6 +230,31 @@ pub enum StateError {

#[error(transparent)]
DataContractUpdateActionNotAllowedError(DataContractUpdateActionNotAllowedError),

#[error(transparent)]
TokenSettingMaxSupplyToLessThanCurrentSupplyError(
TokenSettingMaxSupplyToLessThanCurrentSupplyError,
),

#[error(transparent)]
TokenMintPastMaxSupplyError(TokenMintPastMaxSupplyError),

#[error(transparent)]
NewTokensDestinationIdentityDoesNotExistError(NewTokensDestinationIdentityDoesNotExistError),

#[error(transparent)]
NewAuthorizedActionTakerIdentityDoesNotExistError(
NewAuthorizedActionTakerIdentityDoesNotExistError,
),

#[error(transparent)]
NewAuthorizedActionTakerGroupDoesNotExistError(NewAuthorizedActionTakerGroupDoesNotExistError),

#[error(transparent)]
NewAuthorizedActionTakerMainGroupNotSetError(NewAuthorizedActionTakerMainGroupNotSetError),

#[error(transparent)]
InvalidGroupPositionError(InvalidGroupPositionError),
}

impl From<StateError> for ConsensusError {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::consensus::basic::BasicError;
use crate::consensus::state::state_error::StateError;
use crate::consensus::ConsensusError;
use crate::data_contract::GroupContractPosition;
use crate::ProtocolError;
Expand All @@ -10,29 +10,32 @@ use thiserror::Error;
Error, Debug, Clone, PartialEq, Eq, Encode, Decode, PlatformSerialize, PlatformDeserialize,
)]
#[error(
"Invalid group position {}, expected {}",
invalid_group_position,
expected_group_position
"Invalid group position: {invalid_group_position}. {max_group_message}",
max_group_message = if let Some(max) = self.max_group_position {
format!("The maximum allowed group position is {}", max)
} else {
"No maximum group position limit is set.".to_string()
}
)]
#[platform_serialize(unversioned)]
pub struct InvalidGroupPositionError {
expected_group_position: GroupContractPosition,
max_group_position: Option<GroupContractPosition>,
invalid_group_position: GroupContractPosition,
}

impl InvalidGroupPositionError {
pub fn new(
expected_group_position: GroupContractPosition,
max_group_position: Option<GroupContractPosition>,
invalid_group_position: GroupContractPosition,
) -> Self {
Self {
expected_group_position,
max_group_position,
invalid_group_position,
}
}

pub fn expected_group_position(&self) -> GroupContractPosition {
self.expected_group_position
pub fn max_group_position(&self) -> Option<GroupContractPosition> {
self.max_group_position
}

pub fn invalid_group_position(&self) -> GroupContractPosition {
Expand All @@ -42,6 +45,6 @@ impl InvalidGroupPositionError {

impl From<InvalidGroupPositionError> for ConsensusError {
fn from(err: InvalidGroupPositionError) -> Self {
Self::BasicError(BasicError::InvalidGroupPositionError(err))
Self::StateError(StateError::InvalidGroupPositionError(err))
}
}
14 changes: 14 additions & 0 deletions packages/rs-dpp/src/errors/consensus/state/token/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
mod identity_does_not_have_enough_token_balance_error;
mod identity_token_account_frozen_error;
mod identity_token_account_not_frozen_error;
mod invalid_group_position_error;
mod new_authorized_action_taker_group_does_not_exist_error;
mod new_authorized_action_taker_identity_does_not_exist_error;
mod new_authorized_action_taker_main_group_not_set_error;
mod new_tokens_destination_identity_does_not_exist_error;
mod token_mint_past_max_supply_error;
mod token_setting_max_supply_to_less_than_current_supply_error;
mod unauthorized_token_action_error;

pub use identity_does_not_have_enough_token_balance_error::*;
pub use identity_token_account_frozen_error::*;
pub use identity_token_account_not_frozen_error::*;
pub use invalid_group_position_error::*;
pub use new_authorized_action_taker_group_does_not_exist_error::*;
pub use new_authorized_action_taker_identity_does_not_exist_error::*;
pub use new_authorized_action_taker_main_group_not_set_error::*;
pub use new_tokens_destination_identity_does_not_exist_error::*;
pub use token_mint_past_max_supply_error::*;
pub use token_setting_max_supply_to_less_than_current_supply_error::*;
QuantumExplorer marked this conversation as resolved.
Show resolved Hide resolved
pub use unauthorized_token_action_error::*;
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use crate::consensus::state::state_error::StateError;
use crate::consensus::ConsensusError;
use crate::data_contract::GroupContractPosition;
use crate::ProtocolError;
use bincode::{Decode, Encode};
use platform_serialization_derive::{PlatformDeserialize, PlatformSerialize};
use thiserror::Error;

#[derive(
Error, Debug, Clone, PartialEq, Eq, Encode, Decode, PlatformSerialize, PlatformDeserialize,
)]
#[error("The specified new authorized action taker group {group_contract_position} does not exist")]
#[platform_serialize(unversioned)]
pub struct NewAuthorizedActionTakerGroupDoesNotExistError {
group_contract_position: GroupContractPosition,
}

impl NewAuthorizedActionTakerGroupDoesNotExistError {
pub fn new(group_contract_position: GroupContractPosition) -> Self {
Self {
group_contract_position,
}
}

pub fn group_contract_position(&self) -> GroupContractPosition {
self.group_contract_position
}
}

impl From<NewAuthorizedActionTakerGroupDoesNotExistError> for ConsensusError {
fn from(err: NewAuthorizedActionTakerGroupDoesNotExistError) -> Self {
Self::StateError(StateError::NewAuthorizedActionTakerGroupDoesNotExistError(
err,
))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use crate::consensus::state::state_error::StateError;
use crate::consensus::ConsensusError;
use crate::ProtocolError;
use bincode::{Decode, Encode};
use platform_serialization_derive::{PlatformDeserialize, PlatformSerialize};
use platform_value::Identifier;
use thiserror::Error;

#[derive(
Error, Debug, Clone, PartialEq, Eq, Encode, Decode, PlatformSerialize, PlatformDeserialize,
)]
#[error("The specified new authorized action taker identity {identity_id} does not exist")]
#[platform_serialize(unversioned)]
pub struct NewAuthorizedActionTakerIdentityDoesNotExistError {
identity_id: Identifier,
}

impl NewAuthorizedActionTakerIdentityDoesNotExistError {
pub fn new(identity_id: Identifier) -> Self {
Self { identity_id }
}

pub fn identity_id(&self) -> &Identifier {
&self.identity_id
}
}

impl From<NewAuthorizedActionTakerIdentityDoesNotExistError> for ConsensusError {
fn from(err: NewAuthorizedActionTakerIdentityDoesNotExistError) -> Self {
Self::StateError(StateError::NewAuthorizedActionTakerIdentityDoesNotExistError(err))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use crate::consensus::state::state_error::StateError;
use crate::consensus::ConsensusError;
use crate::ProtocolError;
use bincode::{Decode, Encode};
use platform_serialization_derive::{PlatformDeserialize, PlatformSerialize};
use thiserror::Error;

#[derive(
Error, Debug, Clone, PartialEq, Eq, Encode, Decode, PlatformSerialize, PlatformDeserialize,
)]
#[error(
"The specified new authorized action taker main group is not set in the token configuration"
)]
#[platform_serialize(unversioned)]
pub struct NewAuthorizedActionTakerMainGroupNotSetError {}

impl NewAuthorizedActionTakerMainGroupNotSetError {
pub fn new() -> Self {
Self {}
}
}

impl From<NewAuthorizedActionTakerMainGroupNotSetError> for ConsensusError {
fn from(err: NewAuthorizedActionTakerMainGroupNotSetError) -> Self {
Self::StateError(StateError::NewAuthorizedActionTakerMainGroupNotSetError(
err,
))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use crate::consensus::state::state_error::StateError;
use crate::consensus::ConsensusError;
use crate::ProtocolError;
use bincode::{Decode, Encode};
use platform_serialization_derive::{PlatformDeserialize, PlatformSerialize};
use platform_value::Identifier;
use thiserror::Error;

#[derive(
Error, Debug, Clone, PartialEq, Eq, Encode, Decode, PlatformSerialize, PlatformDeserialize,
)]
#[error("The specified new tokens destination identity {identity_id} does not exist")]
#[platform_serialize(unversioned)]
pub struct NewTokensDestinationIdentityDoesNotExistError {
identity_id: Identifier,
}

impl NewTokensDestinationIdentityDoesNotExistError {
pub fn new(identity_id: Identifier) -> Self {
Self { identity_id }
}

pub fn identity_id(&self) -> &Identifier {
&self.identity_id
}
}

impl From<NewTokensDestinationIdentityDoesNotExistError> for ConsensusError {
fn from(err: NewTokensDestinationIdentityDoesNotExistError) -> Self {
Self::StateError(StateError::NewTokensDestinationIdentityDoesNotExistError(
err,
))
}
}
Loading