Skip to content

Commit

Permalink
more state transitions
Browse files Browse the repository at this point in the history
  • Loading branch information
QuantumExplorer committed Jan 7, 2025
1 parent da5d87a commit e701008
Show file tree
Hide file tree
Showing 180 changed files with 2,398 additions and 468 deletions.
6 changes: 6 additions & 0 deletions packages/rs-dpp/src/state_transition/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,12 @@ impl StateTransition {
BatchedTransitionRef::Token(TokenTransition::Unfreeze(_)) => {
"TokenUnfreeze"
}
BatchedTransitionRef::Token(TokenTransition::DestroyFrozenFunds(_)) => {
"TokenDestroyFrozenFunds"
}
BatchedTransitionRef::Token(TokenTransition::EmergencyAction(_)) => {
"TokenEmergencyAction"
}
};
document_transition_types.push(document_transition_name);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use derive_more::{Display, From};
use serde::{Deserialize, Serialize};
use bincode::{Encode, Decode};
use crate::prelude::{IdentityNonce, Revision};
use crate::state_transition::batch_transition::{DocumentCreateTransition, DocumentDeleteTransition, DocumentReplaceTransition, TokenBurnTransition, TokenFreezeTransition, TokenMintTransition, TokenTransferTransition, TokenUnfreezeTransition};
use crate::state_transition::batch_transition::{DocumentCreateTransition, DocumentDeleteTransition, DocumentReplaceTransition, TokenBurnTransition, TokenDestroyFrozenFundsTransition, TokenEmergencyActionTransition, TokenFreezeTransition, TokenMintTransition, TokenTransferTransition, TokenUnfreezeTransition};
use crate::state_transition::batch_transition::batched_transition::{DocumentPurchaseTransition, DocumentTransferTransition, DocumentUpdatePriceTransition};
use crate::state_transition::batch_transition::batched_transition::document_purchase_transition::v0::v0_methods::DocumentPurchaseTransitionV0Methods;
use crate::state_transition::batch_transition::batched_transition::document_transfer_transition::v0::v0_methods::DocumentTransferTransitionV0Methods;
Expand Down Expand Up @@ -101,6 +101,16 @@ impl BatchTransitionResolversV0 for DocumentTransition {
fn as_transition_token_unfreeze(&self) -> Option<&TokenUnfreezeTransition> {
None
}

fn as_transition_token_destroy_frozen_funds(
&self,
) -> Option<&TokenDestroyFrozenFundsTransition> {
None
}

fn as_transition_token_emergency_action(&self) -> Option<&TokenEmergencyActionTransition> {
None
}
}

pub trait DocumentTransitionV0Methods {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ pub enum DocumentTransitionActionType {
IgnoreWhileBumpingRevision,
}

pub trait TransitionActionTypeGetter {
pub trait DocumentTransitionActionTypeGetter {
fn action_type(&self) -> DocumentTransitionActionType;
}

impl TransitionActionTypeGetter for DocumentTransition {
impl DocumentTransitionActionTypeGetter for DocumentTransition {
fn action_type(&self) -> DocumentTransitionActionType {
match self {
DocumentTransition::Create(_) => DocumentTransitionActionType::Create,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ pub mod multi_party_action;
mod resolvers;
pub mod token_base_transition;
pub mod token_burn_transition;
pub mod token_destroy_frozen_funds_transition;
pub mod token_emergency_action_transition;
pub mod token_freeze_transition;
pub mod token_mint_transition;
pub mod token_transfer_transition;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use crate::state_transition::batch_transition::batched_transition::{
use crate::state_transition::batch_transition::resolvers::v0::BatchTransitionResolversV0;
use crate::state_transition::batch_transition::{
DocumentCreateTransition, DocumentDeleteTransition, DocumentReplaceTransition,
TokenBurnTransition, TokenFreezeTransition, TokenMintTransition, TokenTransferTransition,
TokenUnfreezeTransition,
TokenBurnTransition, TokenDestroyFrozenFundsTransition, TokenEmergencyActionTransition,
TokenFreezeTransition, TokenMintTransition, TokenTransferTransition, TokenUnfreezeTransition,
};

impl BatchTransitionResolversV0 for BatchedTransition {
Expand Down Expand Up @@ -78,6 +78,22 @@ impl BatchTransitionResolversV0 for BatchedTransition {
BatchedTransition::Token(token) => token.as_transition_token_unfreeze(),
}
}

fn as_transition_token_destroy_frozen_funds(
&self,
) -> Option<&TokenDestroyFrozenFundsTransition> {
match self {
BatchedTransition::Document(_) => None,
BatchedTransition::Token(token) => token.as_transition_token_destroy_frozen_funds(),
}
}

fn as_transition_token_emergency_action(&self) -> Option<&TokenEmergencyActionTransition> {
match self {
BatchedTransition::Document(_) => None,
BatchedTransition::Token(token) => token.as_transition_token_emergency_action(),
}
}
}

impl<'a> BatchTransitionResolversV0 for BatchedTransitionRef<'a> {
Expand Down Expand Up @@ -150,4 +166,20 @@ impl<'a> BatchTransitionResolversV0 for BatchedTransitionRef<'a> {
BatchedTransitionRef::Token(token) => token.as_transition_token_unfreeze(),
}
}

fn as_transition_token_destroy_frozen_funds(
&self,
) -> Option<&TokenDestroyFrozenFundsTransition> {
match self {
BatchedTransitionRef::Document(_) => None,
BatchedTransitionRef::Token(token) => token.as_transition_token_destroy_frozen_funds(),
}
}

fn as_transition_token_emergency_action(&self) -> Option<&TokenEmergencyActionTransition> {
match self {
BatchedTransitionRef::Document(_) => None,
BatchedTransitionRef::Token(token) => token.as_transition_token_emergency_action(),
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
pub mod v0;
mod v0_methods;

use bincode::{Decode, Encode};
use derive_more::{Display, From};
#[cfg(feature = "state-transition-serde-conversion")]
use serde::{Deserialize, Serialize};
pub use v0::TokenDestroyFrozenFundsTransitionV0;

#[derive(Debug, Clone, Encode, Decode, PartialEq, Display, From)]
#[cfg_attr(
feature = "state-transition-serde-conversion",
derive(Serialize, Deserialize)
)]
pub enum TokenDestroyFrozenFundsTransition {
#[display("V0({})", "_0")]
V0(TokenDestroyFrozenFundsTransitionV0),
}

impl Default for TokenDestroyFrozenFundsTransition {
fn default() -> Self {
TokenDestroyFrozenFundsTransition::V0(TokenDestroyFrozenFundsTransitionV0::default())
// since only v0
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
pub mod v0_methods;

use crate::state_transition::batch_transition::token_base_transition::TokenBaseTransition;
use bincode::{Decode, Encode};
use derive_more::Display;
use platform_value::Identifier;
#[cfg(feature = "state-transition-serde-conversion")]
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Default, Encode, Decode, PartialEq, Display)]
#[cfg_attr(
feature = "state-transition-serde-conversion",
derive(Serialize, Deserialize),
serde(rename_all = "camelCase")
)]
#[display("Base: {base}, Destroyed Account Identity ID: {frozen_identity_id}")]
pub struct TokenDestroyFrozenFundsTransitionV0 {
/// Document Base Transition
#[cfg_attr(feature = "state-transition-serde-conversion", serde(flatten))]
pub base: TokenBaseTransition,
/// The identity id of the account whose balance should be destroyed
pub frozen_identity_id: Identifier,
/// The public note
pub public_note: Option<String>,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
use platform_value::Identifier;
use crate::state_transition::batch_transition::batched_transition::multi_party_action::AllowedAsMultiPartyAction;
use crate::state_transition::batch_transition::token_base_transition::token_base_transition_accessors::TokenBaseTransitionAccessors;
use crate::state_transition::batch_transition::token_base_transition::TokenBaseTransition;
use crate::state_transition::batch_transition::token_base_transition::v0::v0_methods::TokenBaseTransitionV0Methods;
use crate::state_transition::batch_transition::token_destroy_frozen_funds_transition::TokenDestroyFrozenFundsTransitionV0;
use crate::state_transition::batch_transition::TokenDestroyFrozenFundsTransition;

impl TokenBaseTransitionAccessors for TokenDestroyFrozenFundsTransitionV0 {
fn base(&self) -> &TokenBaseTransition {
&self.base
}

fn base_mut(&mut self) -> &mut TokenBaseTransition {
&mut self.base
}

fn set_base(&mut self, base: TokenBaseTransition) {
self.base = base;
}
}

pub trait TokenDestroyFrozenFundsTransitionV0Methods:
TokenBaseTransitionAccessors + AllowedAsMultiPartyAction
{
/// Returns the `public_note` field of the `TokenDestroyFrozenFundsTransitionV0`.
fn public_note(&self) -> Option<&String>;

/// Returns the owned `public_note` field of the `TokenDestroyFrozenFundsTransitionV0`.
fn public_note_owned(self) -> Option<String>;

/// Sets the `public_note` field in the `TokenDestroyFrozenFundsTransitionV0`.
fn set_public_note(&mut self, public_note: Option<String>);

/// Returns the `frozen_identity_id` field of the `TokenFreezeTransitionV0`.
fn frozen_identity_id(&self) -> Identifier;

/// Sets the value of the `frozen_identity_id` field in the `TokenFreezeTransitionV0`.
fn set_frozen_identity_id(&mut self, frozen_identity_id: Identifier);
}

impl TokenDestroyFrozenFundsTransitionV0Methods for TokenDestroyFrozenFundsTransitionV0 {
fn public_note(&self) -> Option<&String> {
self.public_note.as_ref()
}

fn public_note_owned(self) -> Option<String> {
self.public_note
}

fn set_public_note(&mut self, public_note: Option<String>) {
self.public_note = public_note;
}

fn frozen_identity_id(&self) -> Identifier {
self.frozen_identity_id
}
fn set_frozen_identity_id(&mut self, frozen_identity_id: Identifier) {
self.frozen_identity_id = frozen_identity_id;
}
}

impl AllowedAsMultiPartyAction for TokenDestroyFrozenFundsTransitionV0 {
fn calculate_action_id(&self, owner_id: Identifier) -> Identifier {
let TokenDestroyFrozenFundsTransitionV0 {
base,
frozen_identity_id,
..
} = self;

TokenDestroyFrozenFundsTransition::calculate_action_id_with_fields(
base.token_id().as_bytes(),
owner_id.as_bytes(),
frozen_identity_id.as_bytes(),
base.identity_contract_nonce(),
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
use platform_value::Identifier;
use crate::prelude::IdentityNonce;
use crate::state_transition::batch_transition::batched_transition::multi_party_action::AllowedAsMultiPartyAction;
use crate::state_transition::batch_transition::token_base_transition::token_base_transition_accessors::TokenBaseTransitionAccessors;
use crate::state_transition::batch_transition::token_base_transition::TokenBaseTransition;
use crate::state_transition::batch_transition::token_destroy_frozen_funds_transition::TokenDestroyFrozenFundsTransition;
use crate::state_transition::batch_transition::token_destroy_frozen_funds_transition::v0::v0_methods::TokenDestroyFrozenFundsTransitionV0Methods;
use crate::util::hash::hash_double;

impl TokenBaseTransitionAccessors for TokenDestroyFrozenFundsTransition {
fn base(&self) -> &TokenBaseTransition {
match self {
TokenDestroyFrozenFundsTransition::V0(v0) => &v0.base,
}
}

fn base_mut(&mut self) -> &mut TokenBaseTransition {
match self {
TokenDestroyFrozenFundsTransition::V0(v0) => &mut v0.base,
}
}

fn set_base(&mut self, base: TokenBaseTransition) {
match self {
TokenDestroyFrozenFundsTransition::V0(v0) => v0.base = base,
}
}
}

impl TokenDestroyFrozenFundsTransitionV0Methods for TokenDestroyFrozenFundsTransition {
fn public_note(&self) -> Option<&String> {
match self {
TokenDestroyFrozenFundsTransition::V0(v0) => v0.public_note(),
}
}

fn public_note_owned(self) -> Option<String> {
match self {
TokenDestroyFrozenFundsTransition::V0(v0) => v0.public_note_owned(),
}
}

fn set_public_note(&mut self, public_note: Option<String>) {
match self {
TokenDestroyFrozenFundsTransition::V0(v0) => v0.set_public_note(public_note),
}
}

fn frozen_identity_id(&self) -> Identifier {
match self {
TokenDestroyFrozenFundsTransition::V0(v0) => v0.frozen_identity_id(),
}
}

fn set_frozen_identity_id(&mut self, frozen_identity_id: Identifier) {
match self {
TokenDestroyFrozenFundsTransition::V0(v0) => {
v0.set_frozen_identity_id(frozen_identity_id)
}
}
}
}

impl AllowedAsMultiPartyAction for TokenDestroyFrozenFundsTransition {
fn calculate_action_id(&self, owner_id: Identifier) -> Identifier {
match self {
TokenDestroyFrozenFundsTransition::V0(v0) => v0.calculate_action_id(owner_id),
}
}
}

impl TokenDestroyFrozenFundsTransition {
pub fn calculate_action_id_with_fields(
token_id: &[u8; 32],
owner_id: &[u8; 32],
target_id: &[u8; 32],
identity_contract_nonce: IdentityNonce,
) -> Identifier {
let mut bytes = b"action_token_destroy".to_vec();
bytes.extend_from_slice(token_id);
bytes.extend_from_slice(owner_id);
bytes.extend_from_slice(target_id);
bytes.extend_from_slice(&identity_contract_nonce.to_be_bytes());

hash_double(bytes).into()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
pub mod v0;
mod v0_methods;

use bincode::{Decode, Encode};
use derive_more::{Display, From};
#[cfg(feature = "state-transition-serde-conversion")]
use serde::{Deserialize, Serialize};
pub use v0::TokenEmergencyActionTransitionV0;

#[derive(Debug, Clone, Encode, Decode, PartialEq, Display, From)]
#[cfg_attr(
feature = "state-transition-serde-conversion",
derive(Serialize, Deserialize)
)]
pub enum TokenEmergencyActionTransition {
#[display("V0({})", "_0")]
V0(TokenEmergencyActionTransitionV0),
}

impl Default for TokenEmergencyActionTransition {
fn default() -> Self {
TokenEmergencyActionTransition::V0(TokenEmergencyActionTransitionV0::default())
// since only v0
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
pub mod v0_methods;

use crate::state_transition::batch_transition::token_base_transition::TokenBaseTransition;
use crate::tokens::emergency_action::TokenEmergencyAction;
use bincode::{Decode, Encode};
#[cfg(feature = "state-transition-serde-conversion")]
use serde::{Deserialize, Serialize};
use std::fmt;

#[derive(Debug, Clone, Default, Encode, Decode, PartialEq)]
#[cfg_attr(
feature = "state-transition-serde-conversion",
derive(Serialize, Deserialize),
serde(rename_all = "camelCase")
)]
pub struct TokenEmergencyActionTransitionV0 {
/// Document Base Transition
#[cfg_attr(feature = "state-transition-serde-conversion", serde(flatten))]
pub base: TokenBaseTransition,
/// The emergency action
pub emergency_action: TokenEmergencyAction,
/// The public note
pub public_note: Option<String>,
}

impl fmt::Display for TokenEmergencyActionTransitionV0 {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Base: {}", self.base)
}
}
Loading

0 comments on commit e701008

Please sign in to comment.