-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dc8e575
commit b07e1a6
Showing
20 changed files
with
481 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 77 additions & 22 deletions
99
packages/rs-drive/src/drive/tokens/balance/add_to_previous_token_balance/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 126 additions & 19 deletions
145
packages/rs-drive/src/drive/tokens/balance/add_to_previous_token_balance/v0/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,143 @@ | ||
use crate::drive::Drive; | ||
use crate::error::identity::IdentityError; | ||
use crate::error::Error; | ||
use crate::fees::op::LowLevelDriveOperation; | ||
use dpp::balances::credits::TokenAmount; | ||
use dpp::block::block_info::BlockInfo; | ||
use dpp::fee::fee_result::FeeResult; | ||
use dpp::fee::Credits; | ||
|
||
use crate::drive::tokens::token_balances_path_vec; | ||
use dpp::fee::default_costs::CachedEpochIndexFeeVersions; | ||
use dpp::version::PlatformVersion; | ||
use grovedb::TransactionArg; | ||
use dpp::ProtocolError; | ||
use grovedb::batch::KeyInfoPath; | ||
use grovedb::{Element, EstimatedLayerInformation, TransactionArg}; | ||
use std::collections::HashMap; | ||
|
||
impl Drive { | ||
/// The method to add balance to the previous balance. This function is version controlled. | ||
pub(super) fn add_to_previous_token_balance_v0( | ||
/// Adds specified amount of credits to identity balance | ||
/// This function checks for overflows and does not exceed MAX_CREDITS | ||
/// | ||
/// Balances are stored in the balance tree under the identity's id | ||
pub(in crate::drive::tokens) fn add_to_identity_token_balance_v0( | ||
&self, | ||
token_id: [u8; 32], | ||
identity_id: [u8; 32], | ||
previous_balance: Credits, | ||
added_balance: Credits, | ||
balance_to_add: Credits, | ||
block_info: &BlockInfo, | ||
apply: bool, | ||
transaction: TransactionArg, | ||
drive_operations: &mut Vec<LowLevelDriveOperation>, | ||
platform_version: &PlatformVersion, | ||
) -> Result<AddToPreviousBalanceOutcome, Error> { | ||
// Deduct added balance from existing one | ||
let new_balance = previous_balance | ||
.checked_add(added_balance) | ||
.ok_or(Error::Identity(IdentityError::CriticalBalanceOverflow( | ||
"identity balance add overflow error", | ||
)))?; | ||
|
||
Ok(AddToPreviousBalanceOutcomeV0 { | ||
balance_modified: Some(new_balance), | ||
negative_credit_balance_modified: None, | ||
previous_fee_versions: Option<&CachedEpochIndexFeeVersions>, | ||
) -> Result<FeeResult, Error> { | ||
let mut estimated_costs_only_with_layer_info = if apply { | ||
None::<HashMap<KeyInfoPath, EstimatedLayerInformation>> | ||
} else { | ||
Some(HashMap::new()) | ||
}; | ||
|
||
let batch_operations = self.add_to_identity_token_balance_operations_v0( | ||
token_id, | ||
identity_id, | ||
balance_to_add, | ||
&mut estimated_costs_only_with_layer_info, | ||
transaction, | ||
platform_version, | ||
)?; | ||
|
||
let mut drive_operations: Vec<LowLevelDriveOperation> = vec![]; | ||
self.apply_batch_low_level_drive_operations( | ||
estimated_costs_only_with_layer_info, | ||
transaction, | ||
batch_operations, | ||
&mut drive_operations, | ||
&platform_version.drive, | ||
)?; | ||
let fees = Drive::calculate_fee( | ||
None, | ||
Some(drive_operations), | ||
&block_info.epoch, | ||
self.config.epochs_per_era, | ||
platform_version, | ||
previous_fee_versions, | ||
)?; | ||
Ok(fees) | ||
} | ||
|
||
/// Adds specified amount of credits to identity balance | ||
/// This function checks for overflows and does not exceed MAX_CREDITS | ||
/// | ||
/// Balances are stored in the identity under key 0 | ||
/// This gets operations based on apply flag (stateful vs stateless) | ||
pub(in crate::drive::tokens) fn add_to_identity_token_balance_operations_v0( | ||
&self, | ||
token_id: [u8; 32], | ||
identity_id: [u8; 32], | ||
balance_to_add: TokenAmount, | ||
estimated_costs_only_with_layer_info: &mut Option< | ||
HashMap<KeyInfoPath, EstimatedLayerInformation>, | ||
>, | ||
transaction: TransactionArg, | ||
platform_version: &PlatformVersion, | ||
) -> 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( | ||
estimated_costs_only_with_layer_info, | ||
&platform_version.drive, | ||
)?; | ||
} | ||
|
||
let apply = estimated_costs_only_with_layer_info.is_none(); | ||
|
||
let previous_balance = if apply { | ||
self.fetch_identity_token_balance_operations( | ||
token_id, | ||
identity_id, | ||
apply, | ||
transaction, | ||
&mut drive_operations, | ||
platform_version, | ||
)? | ||
} else { | ||
None // worse case is that we insert | ||
}; | ||
|
||
let balance_path = token_balances_path_vec(token_id); | ||
|
||
if let Some(previous_balance) = previous_balance { | ||
// 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(), | ||
)?; | ||
|
||
drive_operations.push(LowLevelDriveOperation::replace_for_known_path_key_element( | ||
balance_path, | ||
identity_id.to_vec(), | ||
Element::new_sum_item(new_balance), | ||
)); | ||
} else { | ||
if balance_to_add > i64::MAX as u64 { | ||
return Err( | ||
ProtocolError::CriticalCorruptedCreditsCodeExecution(format!( | ||
"Token balance to add over i64 max, is {}", | ||
balance_to_add | ||
)) | ||
.into(), | ||
); | ||
} | ||
drive_operations.push(LowLevelDriveOperation::insert_for_known_path_key_element( | ||
balance_path, | ||
identity_id.to_vec(), | ||
Element::new_sum_item(balance_to_add as i64), | ||
)); | ||
} | ||
.into()) | ||
|
||
Ok(drive_operations) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.