Skip to content

Commit

Permalink
tokens compiling
Browse files Browse the repository at this point in the history
  • Loading branch information
QuantumExplorer committed Dec 20, 2024
1 parent b07e1a6 commit a5a16c4
Show file tree
Hide file tree
Showing 34 changed files with 393 additions and 218 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::fmt;

mod accessors;
pub mod accessors;
mod methods;
mod v0;

Expand Down
60 changes: 59 additions & 1 deletion packages/rs-drive/src/drive/balances/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ use crate::drive::RootTree;
use crate::query::Query;
use grovedb::{PathQuery, SizedQuery};

/// Storage fee pool key
/// Total system credits storage
#[cfg(any(feature = "server", feature = "verify"))]
pub const TOTAL_SYSTEM_CREDITS_STORAGE_KEY: &[u8; 1] = b"D";

/// Total token supplies storage
#[cfg(any(feature = "server", feature = "verify"))]
pub const TOTAL_TOKEN_SUPPLIES_STORAGE_KEY: &[u8; 1] = b"T";

/// The path for all the credits in the system
#[cfg(any(feature = "server", feature = "verify"))]
pub fn total_credits_path() -> [&'static [u8]; 2] {
Expand Down Expand Up @@ -57,6 +61,60 @@ pub fn total_credits_on_platform_path_query() -> PathQuery {
}
}

/// The path for the root of all token supplies
#[cfg(any(feature = "server", feature = "verify"))]
pub fn total_tokens_root_supply_path() -> [&'static [u8]; 2] {
[
Into::<&[u8; 1]>::into(RootTree::Misc),
TOTAL_TOKEN_SUPPLIES_STORAGE_KEY,
]
}

/// The path as a vec for the root of all token supplies
#[cfg(any(feature = "server", feature = "verify"))]
pub fn total_tokens_root_supply_path_vec() -> Vec<Vec<u8>> {
vec![
vec![RootTree::Misc as u8],
TOTAL_TOKEN_SUPPLIES_STORAGE_KEY.to_vec(),
]
}

/// The path for the token supply for a given token
#[cfg(any(feature = "server", feature = "verify"))]
pub fn total_token_supply_path(token_id: &[u8; 32]) -> [&[u8]; 3] {
[
Into::<&[u8; 1]>::into(RootTree::Misc),
TOTAL_TOKEN_SUPPLIES_STORAGE_KEY,
token_id,
]
}

/// The path as a vec for the token supply for a given token
#[cfg(any(feature = "server", feature = "verify"))]
pub fn total_token_supply_path_vec(token_id: [u8; 32]) -> Vec<Vec<u8>> {
vec![
vec![RootTree::Misc as u8],
TOTAL_TOKEN_SUPPLIES_STORAGE_KEY.to_vec(),
token_id.to_vec(),
]
}

/// A path query helper to get the total token supply for a given token on Platform
#[cfg(any(feature = "server", feature = "verify"))]
pub fn total_supply_for_token_on_platform_path_query(token_id: [u8; 32]) -> PathQuery {
PathQuery {
path: vec![
vec![RootTree::Misc as u8],
TOTAL_TOKEN_SUPPLIES_STORAGE_KEY.to_vec(),
],
query: SizedQuery {
query: Query::new_single_key(token_id.to_vec()),
limit: Some(1),
offset: None,
},
}
}

/// The path for the balances tree
#[cfg(any(feature = "server", feature = "verify"))]
pub(crate) fn balance_path() -> [&'static [u8]; 1] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ impl Drive {
}

if let Some(estimated_costs_only_with_layer_info) = estimated_costs_only_with_layer_info {
Self::add_estimation_costs_for_token_balances(
Self::add_estimation_costs_for_balances(
estimated_costs_only_with_layer_info,
&platform_version.drive,
)?;
Expand All @@ -257,7 +257,7 @@ impl Drive {
let mut create_tree_keys_operations = self.create_key_tree_with_keys_operations(
id.to_buffer(),
public_keys.into_values().collect(),
// if we are a masternode identity, we want to register all keys as non unique
// if we are a masternode identity, we want to register all keys as non-unique
is_masternode_identity,
&block_info.epoch,
estimated_costs_only_with_layer_info,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl Drive {
let mut drive_operations = vec![];
let drive_version = &platform_version.drive;
if let Some(estimated_costs_only_with_layer_info) = estimated_costs_only_with_layer_info {
Self::add_estimation_costs_for_token_balances(
Self::add_estimation_costs_for_balances(
estimated_costs_only_with_layer_info,
drive_version,
)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl Drive {
) -> Result<Vec<LowLevelDriveOperation>, Error> {
let mut drive_operations = vec![];
if let Some(estimated_costs_only_with_layer_info) = estimated_costs_only_with_layer_info {
Self::add_estimation_costs_for_token_balances(
Self::add_estimation_costs_for_balances(
estimated_costs_only_with_layer_info,
&platform_version.drive,
)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ impl Drive {
let mut drive_operations = vec![];
if let Some(estimated_costs_only_with_layer_info) = estimated_costs_only_with_layer_info {
Self::add_estimation_costs_for_token_balances(
token_id,
false,
estimated_costs_only_with_layer_info,
&platform_version.drive,
)?;
Expand All @@ -109,12 +111,9 @@ impl Drive {
// Check for overflow
let new_balance = (previous_balance as i64)
.checked_add(balance_to_add as i64)
.ok_or(
ProtocolError::CriticalCorruptedCreditsCodeExecution(
"Overflow of total token balance".to_string(),
)
.into(),
)?;
.ok_or(ProtocolError::CriticalCorruptedCreditsCodeExecution(
"Overflow of total token balance".to_string(),
))?;

drive_operations.push(LowLevelDriveOperation::replace_for_known_path_key_element(
balance_path,
Expand Down
2 changes: 1 addition & 1 deletion packages/rs-drive/src/drive/tokens/balance/fetch/v0/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::drive::tokens::{token_balances_path, token_balances_path_vec};
use crate::drive::tokens::token_balances_path;
use crate::drive::Drive;
use crate::error::drive::DriveError;
use crate::error::Error;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ impl Drive {
let mut drive_operations = vec![];
if let Some(estimated_costs_only_with_layer_info) = estimated_costs_only_with_layer_info {
Self::add_estimation_costs_for_token_balances(
token_id,
false,
estimated_costs_only_with_layer_info,
&platform_version.drive,
)?;
Expand Down
4 changes: 0 additions & 4 deletions packages/rs-drive/src/drive/tokens/burn/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ impl Drive {
identity_id: [u8; 32],
burn_amount: u64,
apply: bool,
previous_batch_operations: &mut Option<&mut Vec<LowLevelDriveOperation>>,
transaction: TransactionArg,
drive_operations: &mut Vec<LowLevelDriveOperation>,
platform_version: &PlatformVersion,
Expand All @@ -58,7 +57,6 @@ impl Drive {
identity_id,
burn_amount,
apply,
previous_batch_operations,
transaction,
drive_operations,
platform_version,
Expand All @@ -77,7 +75,6 @@ impl Drive {
token_id: [u8; 32],
identity_id: [u8; 32],
burn_amount: u64,
previous_batch_operations: &mut Option<&mut Vec<LowLevelDriveOperation>>,
estimated_costs_only_with_layer_info: &mut Option<
HashMap<KeyInfoPath, EstimatedLayerInformation>,
>,
Expand All @@ -89,7 +86,6 @@ impl Drive {
token_id,
identity_id,
burn_amount,
previous_batch_operations,
estimated_costs_only_with_layer_info,
transaction,
platform_version,
Expand Down
76 changes: 15 additions & 61 deletions packages/rs-drive/src/drive/tokens/burn/v0/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::drive::Drive;
use crate::error::drive::DriveError;
use crate::error::Error;
use crate::fees::op::LowLevelDriveOperation;
use dpp::block::block_info::BlockInfo;
Expand All @@ -26,7 +25,6 @@ impl Drive {
identity_id,
burn_amount,
apply,
&mut None,
transaction,
&mut drive_operations,
platform_version,
Expand All @@ -50,7 +48,6 @@ impl Drive {
identity_id: [u8; 32],
burn_amount: u64,
apply: bool,
previous_batch_operations: &mut Option<&mut Vec<LowLevelDriveOperation>>,
transaction: TransactionArg,
drive_operations: &mut Vec<LowLevelDriveOperation>,
platform_version: &PlatformVersion,
Expand All @@ -62,7 +59,6 @@ impl Drive {
token_id,
identity_id,
burn_amount,
previous_batch_operations,
&mut estimated_costs_only_with_layer_info,
transaction,
platform_version,
Expand All @@ -82,7 +78,6 @@ impl Drive {
token_id: [u8; 32],
identity_id: [u8; 32],
burn_amount: u64,
_previous_batch_operations: &mut Option<&mut Vec<LowLevelDriveOperation>>,
estimated_costs_only_with_layer_info: &mut Option<
HashMap<KeyInfoPath, EstimatedLayerInformation>,
>,
Expand All @@ -91,63 +86,22 @@ impl Drive {
) -> Result<Vec<LowLevelDriveOperation>, Error> {
let mut drive_operations = vec![];

// Add estimation info if needed
if let Some(esti) = estimated_costs_only_with_layer_info {
Self::add_estimation_costs_for_token_balances(esti, &platform_version.drive)?;
Self::add_estimation_costs_for_negative_credit(
identity_id,
esti,
&platform_version.drive,
)?;
}

// Fetch current balance
let current_balance = self
.fetch_identity_token_balance_operations(
token_id,
identity_id,
estimated_costs_only_with_layer_info.is_none(),
transaction,
&mut drive_operations,
platform_version,
)?
.ok_or(Error::Drive(DriveError::CorruptedDriveState(
"there should be a balance when burning tokens".to_string(),
)))?;

if current_balance < burn_amount {
return Err(Error::Drive(DriveError::CorruptedDriveState(
"cannot burn more tokens than currently owned".to_string(),
)));
}

let new_balance = current_balance - burn_amount;

// Update identity balance
drive_operations
.push(self.update_identity_token_balance_operation_v0(identity_id, new_balance)?);

// Update total supply for the token (subtract burn_amount)
let current_supply = self
.fetch_token_total_supply_operations(
token_id,
estimated_costs_only_with_layer_info.is_none(),
transaction,
&mut drive_operations,
platform_version,
)?
.ok_or(Error::Drive(DriveError::CorruptedDriveState(
"token should have a total supply".to_string(),
)))?;

if current_supply < burn_amount {
return Err(Error::Drive(DriveError::CorruptedDriveState(
"cannot burn more tokens than total supply".to_string(),
)));
}
drive_operations.extend(self.remove_from_identity_token_balance_operations(
token_id,
identity_id,
burn_amount,
estimated_costs_only_with_layer_info,
transaction,
platform_version,
)?);

let new_supply = current_supply - burn_amount;
drive_operations.push(self.update_token_total_supply_operation_v0(token_id, new_supply)?);
drive_operations.extend(self.remove_from_token_total_supply_operations(
token_id,
burn_amount,
estimated_costs_only_with_layer_info,
transaction,
platform_version,
)?);

Ok(drive_operations)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use grovedb::EstimatedLayerInformation;
use std::collections::HashMap;

impl Drive {
/// Adds estimation costs for balances.
/// Adds estimation costs for token balance changes.
///
/// It operates on the provided HashMap, `estimated_costs_only_with_layer_info`, and adds
/// new entries to it, representing the estimated costs for different layers of the balance tree.
Expand All @@ -25,12 +25,21 @@ impl Drive {
/// # Errors
/// This function will return an error if the method version doesn't match any known versions.
pub(crate) fn add_estimation_costs_for_token_balances(
token_id: [u8; 32],
with_info_tree: bool,
estimated_costs_only_with_layer_info: &mut HashMap<KeyInfoPath, EstimatedLayerInformation>,
drive_version: &DriveVersion,
) -> Result<(), Error> {
match drive_version.methods.identity.cost_estimation.for_balances {
match drive_version
.methods
.identity
.cost_estimation
.for_token_balances
{
0 => {
Self::add_estimation_costs_for_token_balances_v0(
token_id,
with_info_tree,
estimated_costs_only_with_layer_info,
);
Ok(())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::util::type_constants::DEFAULT_HASH_SIZE_U8;
use grovedb::EstimatedSumTrees::{AllSumTrees, NoSumTrees, SomeSumTrees};
use std::collections::HashMap;

pub const ESTIMATED_TOKEN_INFO_SIZE_BYTES: u16 = 256;
pub const ESTIMATED_TOKEN_INFO_SIZE_BYTES: u32 = 256;

impl Drive {
/// Adds estimation costs for token balances in Drive for version 0.
Expand Down Expand Up @@ -52,8 +52,8 @@ impl Drive {
/// ```
pub(super) fn add_estimation_costs_for_token_balances_v0(
token_id: [u8; 32],
with_info_tree: bool,
estimated_costs_only_with_layer_info: &mut HashMap<KeyInfoPath, EstimatedLayerInformation>,
with_info: bool,
) {
// we have constructed the top layer so contract/documents tree are at the top
// since balance will be on layer 3 (level 2 on left then left)
Expand Down Expand Up @@ -81,7 +81,7 @@ impl Drive {
},
);

if with_info {
if with_info_tree {
estimated_costs_only_with_layer_info.insert(
KeyInfoPath::from_known_path(token_path(&token_id)),
EstimatedLayerInformation {
Expand All @@ -108,7 +108,7 @@ impl Drive {
);
}

if with_info {
if with_info_tree {
// there is one tree for the root path
estimated_costs_only_with_layer_info.insert(
KeyInfoPath::from_known_path(token_identity_infos_path(&token_id)),
Expand Down
Loading

0 comments on commit a5a16c4

Please sign in to comment.