-
Notifications
You must be signed in to change notification settings - Fork 58
feat(drive)!: count credit inflows against the daily withdrawal limit #4486
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
Merged
QuantumExplorer
merged 13 commits into
v4.2-dev
from
claude/record-credits-history-cause-e91105
Aug 26, 2026
Merged
Changes from 4 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
17cf382
feat(drive)!: count credit inflows against the daily withdrawal limit
QuantumExplorer 6f12fca
Merge remote-tracking branch 'origin/v4.2-dev' into claude/record-cre…
QuantumExplorer 4e82f6b
fix(drive): model all three withdrawal sum trees in the inflow estima…
QuantumExplorer c9e65c9
fix(drive): count only inflows younger than the day-old base snapshot
QuantumExplorer d75fe42
refactor(drive)!: record credit inflows once per block as a system event
QuantumExplorer 4ddf90b
fix(drive): stop subtracting reservations the day-old base already re…
QuantumExplorer c73d2cd
fix(drive-abci): thread the mint accumulator through the remaining ex…
QuantumExplorer 79f3f86
fix(drive): let inflows extend past the capped base and rewind mints …
QuantumExplorer fb704e6
chore(drive-abci): show credit_mints in the block fees outcome display
QuantumExplorer 504e35a
test(drive-abci): pin the epoch Core-reward mint in the block fees ou…
QuantumExplorer dcf295b
docs(drive): describe the net withdrawal limit in the v14 notes, iter…
QuantumExplorer 2b39a2c
Merge remote-tracking branch 'origin/v4.2-dev' into claude/record-cre…
QuantumExplorer b940512
Merge remote-tracking branch 'origin/v4.2-dev' into claude/record-cre…
QuantumExplorer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
176 changes: 176 additions & 0 deletions
176
...ecution/platform_events/withdrawals/cleanup_expired_locks_of_withdrawal_amounts/v1/mod.rs
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| use crate::error::Error; | ||
| use crate::platform_types::platform::Platform; | ||
|
|
||
| use crate::rpc::core::CoreRPCLike; | ||
| use dpp::block::block_info::BlockInfo; | ||
|
|
||
| use dpp::version::PlatformVersion; | ||
| use drive::drive::identity::withdrawals::paths::{ | ||
| get_withdrawal_credit_inflows_sum_tree_path_vec, get_withdrawal_transactions_sum_tree_path_vec, | ||
| }; | ||
| use drive::grovedb::{MaybeTree, PathQuery, QueryItem, Transaction}; | ||
| use drive::util::grove_operations::BatchDeleteApplyType; | ||
|
|
||
| impl<C> Platform<C> | ||
| where | ||
| C: CoreRPCLike, | ||
| { | ||
| /// Version 1 differs from version 0 in also pruning the expired entries of the credit | ||
| /// inflows sum tree, which exists from protocol version 14: both trees are keyed by the | ||
| /// block time their entries stop counting toward the daily withdrawal limit, on the same | ||
| /// 25 hour schedule, and both are pruned with the same per-block limit. | ||
| pub(super) fn cleanup_expired_locks_of_withdrawal_amounts_v1( | ||
| &self, | ||
| block_info: &BlockInfo, | ||
| transaction: &Transaction, | ||
| platform_version: &PlatformVersion, | ||
| ) -> Result<(), Error> { | ||
| let limit = platform_version | ||
| .drive_abci | ||
| .withdrawal_constants | ||
| .cleanup_expired_locks_of_withdrawal_amounts_limit; | ||
|
|
||
| if limit == 0 { | ||
| // No clean up | ||
| return Ok(()); | ||
| } | ||
|
|
||
| let mut batch_operations = vec![]; | ||
|
|
||
| for path in [ | ||
| get_withdrawal_transactions_sum_tree_path_vec(), | ||
| get_withdrawal_credit_inflows_sum_tree_path_vec(), | ||
| ] { | ||
| let mut path_query = PathQuery::new_single_query_item( | ||
| path, | ||
| QueryItem::RangeTo(..block_info.time_ms.to_be_bytes().to_vec()), | ||
| ); | ||
|
|
||
| path_query.query.limit = Some(limit); | ||
|
|
||
| self.drive.batch_delete_items_in_path_query( | ||
| &path_query, | ||
| true, | ||
| // we know that we are not deleting a subtree | ||
| BatchDeleteApplyType::StatefulBatchDelete { | ||
| is_known_to_be_subtree_with_sum: Some(MaybeTree::NotTree), | ||
| }, | ||
| Some(transaction), | ||
| &mut batch_operations, | ||
| &platform_version.drive, | ||
| )?; | ||
| } | ||
|
|
||
| self.drive.apply_batch_low_level_drive_operations( | ||
| None, | ||
| Some(transaction), | ||
| batch_operations, | ||
| &mut vec![], | ||
| &platform_version.drive, | ||
| )?; | ||
|
|
||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use crate::test::helpers::setup::TestPlatformBuilder; | ||
| use dpp::block::block_info::BlockInfo; | ||
| use dpp::block::epoch::Epoch; | ||
| use dpp::version::PlatformVersion; | ||
| use drive::drive::identity::withdrawals::paths::{ | ||
| get_withdrawal_credit_inflows_sum_tree_path_vec, | ||
| get_withdrawal_transactions_sum_tree_path_vec, | ||
| }; | ||
| use drive::grovedb::{Element, PathQuery, Query, SizedQuery}; | ||
| use drive::util::grove_operations::BatchInsertApplyType; | ||
| use drive::util::object_size_info::PathKeyElementInfo; | ||
|
|
||
| /// Both the reserved withdrawal amounts and the credit inflows expire on the same | ||
| /// schedule; the v1 cleanup must prune the entries of both trees whose key is before the | ||
| /// block time and leave the rest. | ||
| #[test] | ||
| fn should_prune_expired_entries_of_both_sum_trees() { | ||
| let platform_version = PlatformVersion::latest(); | ||
| let platform = TestPlatformBuilder::new() | ||
| .with_latest_protocol_version() | ||
| .build_with_mock_rpc() | ||
| .set_initial_state_structure(); | ||
|
|
||
| let transaction = platform.drive.grove.start_transaction(); | ||
|
|
||
| let now_ms: u64 = 1_000_000; | ||
|
|
||
| for path in [ | ||
| get_withdrawal_transactions_sum_tree_path_vec(), | ||
| get_withdrawal_credit_inflows_sum_tree_path_vec(), | ||
| ] { | ||
| for (key_time_ms, amount) in [(now_ms - 1, 100i64), (now_ms, 250i64)] { | ||
| let mut drive_operations = vec![]; | ||
| platform | ||
| .drive | ||
| .batch_insert_sum_item_or_add_to_if_already_exists( | ||
| PathKeyElementInfo::PathKeyElement::<0>(( | ||
| path.clone(), | ||
| key_time_ms.to_be_bytes().to_vec(), | ||
| Element::new_sum_item(amount), | ||
| )), | ||
| BatchInsertApplyType::StatefulBatchInsert, | ||
| Some(&transaction), | ||
| &mut drive_operations, | ||
| &platform_version.drive, | ||
| ) | ||
| .expect("expected to insert the entry"); | ||
| platform | ||
| .drive | ||
| .apply_batch_low_level_drive_operations( | ||
| None, | ||
| Some(&transaction), | ||
| drive_operations, | ||
| &mut vec![], | ||
| &platform_version.drive, | ||
| ) | ||
| .expect("expected to apply the entry"); | ||
| } | ||
| } | ||
|
|
||
| platform | ||
| .cleanup_expired_locks_of_withdrawal_amounts_v1( | ||
| &BlockInfo { | ||
| time_ms: now_ms, | ||
| height: 100, | ||
| core_height: 10, | ||
| epoch: Epoch::default(), | ||
| }, | ||
| &transaction, | ||
| platform_version, | ||
| ) | ||
| .expect("expected the cleanup to succeed"); | ||
|
|
||
| for path in [ | ||
| get_withdrawal_transactions_sum_tree_path_vec(), | ||
| get_withdrawal_credit_inflows_sum_tree_path_vec(), | ||
| ] { | ||
| let mut query = Query::new(); | ||
| query.insert_all(); | ||
| let (results, _) = platform | ||
| .drive | ||
| .grove_get_raw_path_query( | ||
| &PathQuery::new(path, SizedQuery::new(query, None, None)), | ||
| Some(&transaction), | ||
| drive::grovedb::query_result_type::QueryResultType::QueryKeyElementPairResultType, | ||
| &mut vec![], | ||
| &platform_version.drive, | ||
| ) | ||
| .expect("expected to query the tree"); | ||
| let keys: Vec<_> = results | ||
| .to_key_elements() | ||
| .into_iter() | ||
| .map(|(key, _)| key) | ||
| .collect(); | ||
| // The entry exactly at the block time is not expired yet (strict `<`). | ||
| assert_eq!(keys, vec![now_ms.to_be_bytes().to_vec()]); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.