Skip to content

Commit

Permalink
a lot of grovedb support
Browse files Browse the repository at this point in the history
  • Loading branch information
QuantumExplorer committed Jan 10, 2025
1 parent ad720bf commit 29dadbf
Show file tree
Hide file tree
Showing 74 changed files with 1,039 additions and 298 deletions.
18 changes: 9 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions packages/rs-dpp/src/balances/credits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ pub type Credits = u64;
/// Token Amount type
pub type TokenAmount = u64;

/// Sum token amount
pub type SumTokenAmount = i128;

/// Signed Credits type is used for internal computations and total credits
/// balance verification
Expand Down
1 change: 1 addition & 0 deletions packages/rs-dpp/src/balances/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod total_credits_balance;

pub mod credits;
pub mod total_tokens_balance;
53 changes: 53 additions & 0 deletions packages/rs-dpp/src/balances/total_tokens_balance/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use crate::balances::credits::SumTokenAmount;
use crate::ProtocolError;
use std::fmt;

/// The outcome of verifying token balances
#[derive(Copy, Clone, Debug)]
pub struct TotalTokensBalance {
/// all the tokens in platform
pub total_tokens_in_platform: SumTokenAmount,
/// all the tokens in identity token balances
pub total_identity_token_balances: SumTokenAmount,
}

impl fmt::Display for TotalTokensBalance {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, "TotalTokensBalance {{")?;
writeln!(
f,
" total_tokens_in_platform: {},",
self.total_tokens_in_platform
)?;
writeln!(
f,
" total_identity_token_balances: {}",
self.total_identity_token_balances
)?;
write!(f, "}}")
}
}
impl TotalTokensBalance {
/// Is the outcome okay? basically do the values match up
/// Errors in case of overflow
pub fn ok(&self) -> Result<bool, ProtocolError> {
let TotalTokensBalance {
total_tokens_in_platform,
total_identity_token_balances,
} = *self;

if total_tokens_in_platform < 0 {
return Err(ProtocolError::CriticalCorruptedCreditsCodeExecution(
"Tokens in platform are less than 0".to_string(),
));
}

if total_identity_token_balances < 0 {
return Err(ProtocolError::CriticalCorruptedCreditsCodeExecution(
"Tokens in identity balances are less than 0".to_string(),
));
}

Ok(total_tokens_in_platform == total_identity_token_balances)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ use crate::state_transition::batch_transition::token_freeze_transition::TokenFre
use crate::state_transition::batch_transition::token_mint_transition::TokenMintTransitionV0;
use crate::state_transition::batch_transition::token_transfer_transition::TokenTransferTransitionV0;
use crate::state_transition::batch_transition::token_unfreeze_transition::TokenUnfreezeTransitionV0;
#[cfg(feature = "state-transition-signing")]
use crate::tokens::emergency_action::TokenEmergencyAction;

impl DocumentsBatchTransitionAccessorsV0 for BatchTransitionV1 {
Expand Down
8 changes: 8 additions & 0 deletions packages/rs-drive-abci/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ pub struct ExecutionConfig {
#[serde(default = "ExecutionConfig::default_verify_sum_trees")]
pub verify_sum_trees: bool,

/// Should we verify sum trees? Useful to set as `false` for tests
#[serde(default = "ExecutionConfig::default_verify_token_sum_trees")]
pub verify_token_sum_trees: bool,

/// How long in seconds should an epoch last
/// It might last a lot longer if the chain is halted
#[serde(
Expand Down Expand Up @@ -614,6 +618,10 @@ impl ExecutionConfig {
true
}

fn default_verify_token_sum_trees() -> bool {
true
}

fn default_use_document_triggers() -> bool {
true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub(in crate::execution) mod initialization;
pub(in crate::execution) mod protocol_upgrade;
/// State transition processing
pub(in crate::execution) mod state_transition_processing;
mod tokens;
/// Voting
pub(in crate::execution) mod voting;
/// Withdrawal methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ use drive::drive::identity::withdrawals::paths::{
WITHDRAWAL_TRANSACTIONS_SUM_AMOUNT_TREE_KEY,
};
use drive::drive::system::misc_path;
use drive::drive::tokens::{
tokens_root_path, TOKEN_BALANCES_KEY, TOKEN_IDENTITY_INFO_KEY, TOKEN_STATUS_INFO_KEY,
};
use drive::drive::RootTree;
use drive::grovedb::{Element, Transaction};
use drive::grovedb_path::SubtreePath;
Expand Down Expand Up @@ -113,11 +116,39 @@ impl<C> Platform<C> {
&platform_version.drive,
)?;

let path = tokens_root_path();
self.drive.grove_insert_if_not_exists(
(&path).into(),
&[TOKEN_BALANCES_KEY],
Element::empty_big_sum_tree(),
Some(transaction),
None,
&platform_version.drive,
)?;

self.drive.grove_insert_if_not_exists(
(&path).into(),
&[TOKEN_IDENTITY_INFO_KEY],
Element::empty_tree(),
Some(transaction),
None,
&platform_version.drive,
)?;

self.drive.grove_insert_if_not_exists(
(&path).into(),
&[TOKEN_STATUS_INFO_KEY],
Element::empty_tree(),
Some(transaction),
None,
&platform_version.drive,
)?;

let path = misc_path();
self.drive.grove_insert_if_not_exists(
(&path).into(),
TOTAL_TOKEN_SUPPLIES_STORAGE_KEY.as_slice(),
Element::empty_tree(),
Element::empty_big_sum_tree(),
Some(transaction),
None,
&platform_version.drive,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod validate_token_aggregated_balance;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod v0;
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use dpp::block::epoch::Epoch;
use drive::drive::Drive;
use drive::grovedb::Transaction;

use crate::error::execution::ExecutionError;
use crate::error::Error;
use crate::execution::types::block_execution_context::BlockExecutionContext;
use crate::platform_types::platform::Platform;
use platform_version::version::PlatformVersion;

impl<CoreRPCLike> Platform<CoreRPCLike> {
/// Adds operations to GroveDB op batch related to processing
/// and distributing the block fees from the previous block and applies the batch.
///
/// Returns `ProcessedBlockFeesOutcome`.
#[inline(always)]
pub(super) fn validate_token_aggregated_balance_v0(
&self,
block_execution_context: &BlockExecutionContext,
transaction: &Transaction,
platform_version: &PlatformVersion,
) -> Result<(), Error> {
if self.config.execution.verify_token_sum_trees {
// Verify sum trees
let credits_verified = self
.drive
.calculate_total_token_balance(Some(transaction), &platform_version.drive)
.map_err(Error::Drive)?;

if !credits_verified.ok()? {
return Err(Error::Execution(
ExecutionError::CorruptedCreditsNotBalanced(format!(
"credits are not balanced after block execution {:?} off by {}",
credits_verified,
credits_verified
.total_in_trees()
.unwrap()
.abs_diff(credits_verified.total_credits_in_platform)
)),
));
}
}

Ok(outcome)
}
}
12 changes: 6 additions & 6 deletions packages/rs-drive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ enum-map = { version = "2.0.3", optional = true }
intmap = { version = "2.0.0", features = ["serde"], optional = true }
chrono = { version = "0.4.35", optional = true }
itertools = { version = "0.13", optional = true }
grovedb = { git = "https://github.com/dashpay/grovedb", rev= "2417f7a72900cd3dca943ad52c979bc8abfdaa20", optional = true, default-features = false }
grovedb-costs = { git = "https://github.com/dashpay/grovedb", rev= "2417f7a72900cd3dca943ad52c979bc8abfdaa20", optional = true }
grovedb-path = { git = "https://github.com/dashpay/grovedb", rev= "2417f7a72900cd3dca943ad52c979bc8abfdaa20" }
grovedb-storage = { git = "https://github.com/dashpay/grovedb", rev= "2417f7a72900cd3dca943ad52c979bc8abfdaa20", optional = true }
grovedb-version = { git = "https://github.com/dashpay/grovedb", rev= "2417f7a72900cd3dca943ad52c979bc8abfdaa20" }
grovedb-epoch-based-storage-flags = { git = "https://github.com/dashpay/grovedb", rev= "2417f7a72900cd3dca943ad52c979bc8abfdaa20" }
grovedb = { git = "https://github.com/dashpay/grovedb", rev= "263dcd5164c7db597bf7de7b6de133ad7cd77d50", optional = true, default-features = false }
grovedb-costs = { git = "https://github.com/dashpay/grovedb", rev= "263dcd5164c7db597bf7de7b6de133ad7cd77d50", optional = true }
grovedb-path = { git = "https://github.com/dashpay/grovedb", rev= "263dcd5164c7db597bf7de7b6de133ad7cd77d50" }
grovedb-storage = { git = "https://github.com/dashpay/grovedb", rev= "263dcd5164c7db597bf7de7b6de133ad7cd77d50", optional = true }
grovedb-version = { git = "https://github.com/dashpay/grovedb", rev= "263dcd5164c7db597bf7de7b6de133ad7cd77d50" }
grovedb-epoch-based-storage-flags = { git = "https://github.com/dashpay/grovedb", rev= "263dcd5164c7db597bf7de7b6de133ad7cd77d50" }

[dev-dependencies]
criterion = "0.5"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ impl Drive {
12, // 32 + 1 + 1 / 3
SomeSumTrees {
sum_trees_weight: 1,
big_sum_trees_weight: 0,
count_trees_weight: 0,
count_sum_trees_weight: 0,
non_sum_trees_weight: 2,
},
None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl Drive {

self.batch_insert_empty_tree_if_not_exists(
key_info,
false,
TreeType::NormalTree,
storage_flags.as_ref().map(|flags| flags.as_ref()),
apply_type,
transaction,
Expand Down
Loading

0 comments on commit 29dadbf

Please sign in to comment.