Repository navigation
feat(drive): add shielded pool storage, actions, and verification (Medusa part 2) #3198
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
Merged
Changes from 11 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
855d593
feat(drive): add shielded pool storage, actions, and verification
QuantumExplorer 2c74098
fix(drive): use RootTree enum instead of magic number 72u8 (#3205)
QuantumExplorer c93ae0e
fix(drive): guard against max_elements=0 in encrypted notes verificat…
QuantumExplorer 73c058a
fix(drive): replace expect/unwrap with error propagation in productio…
QuantumExplorer d1c1a61
fix(drive): import nullifier constants instead of duplicating them (#…
QuantumExplorer 357e9b0
fix(drive): replace unchecked integer casts with try_from (#3201)
QuantumExplorer 5218ddf
fix(drive): correct tree types in shielded pool estimated costs (#3200)
QuantumExplorer 743faeb
fix(drive): match min_depth between prove and verify for trunk query …
QuantumExplorer 95f8e04
Merge branch 'v3.1-dev' into feat/zk-drive
QuantumExplorer 77615a6
fix(drive): resolve clippy lints in shielded module
QuantumExplorer 2543341
fix(drive): fix CI failures for shielded module
QuantumExplorer 0432425
fix(drive): fix bugs found in CodeRabbit review of shielded module
QuantumExplorer 7221772
refactor(dpp): replace ProtocolError::Generic with specific error types
QuantumExplorer 7ca50a6
build(drive): update grovedb to latest develop (99a2ab01)
QuantumExplorer ea9e63c
refactor(drive): move per-block nullifier paths to shielded credit pool
QuantumExplorer f2b5338
refactor(drive): extract shielded pool ops into versioned functions a…
QuantumExplorer 0b82b6b
refactor(drive): derive notes and anchor from transition in try_from_…
QuantumExplorer 295f777
fix(drive): harden bincode decoding and fix limit==0 edge case in shi…
QuantumExplorer f483b62
Merge branch 'v3.1-dev' into feat/zk-drive
QuantumExplorer 3fb7801
refactor(drive): address review feedback on shielded nullifiers
QuantumExplorer a8de751
fix(drive): adapt proof verification for shielded pool tree structure…
QuantumExplorer 4d13397
refactor(drive): upgrade grovedb, fix estimated costs, and clean up d…
QuantumExplorer 0f5acd5
refactor(drive): replace nullifier tuple type aliases with structs
QuantumExplorer acf830a
Merge remote-tracking branch 'origin/v3.1-dev' into feat/zk-drive
QuantumExplorer b78d53e
Merge remote-tracking branch 'origin/v3.1-dev' into feat/zk-drive
QuantumExplorer 04c537e
feat(drive): adapt to grovedb SubelementsDeletionBehavior per-op enum…
QuantumExplorer 83ea01f
chore: update grovedb to b191ff27 with DontCheckWithNoCleanup
QuantumExplorer 160749d
fix(drive-abci): update version upgrade test for proper DeleteChildre…
QuantumExplorer 3220e43
Merge remote-tracking branch 'origin/v3.1-dev' into feat/zk-drive
QuantumExplorer 82eddb8
chore: update grovedb to dd99ed1d
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,23 +1,140 @@ | ||
| //! Drive Initialization v3 — adds shielded pool trees. | ||
| //! Drive Initialization | ||
|
|
||
| use crate::drive::Drive; | ||
| use crate::drive::saved_block_transactions::{ | ||
| COMPACTED_NULLIFIERS_KEY_U8, NULLIFIERS_EXPIRATION_TIME_KEY_U8, NULLIFIERS_KEY_U8, | ||
| }; | ||
| use crate::drive::shielded::paths::*; | ||
| use crate::drive::{Drive, RootTree}; | ||
| use crate::error::Error; | ||
| use crate::util::batch::grovedb_op_batch::GroveDbOpBatchV0Methods; | ||
| use crate::util::batch::GroveDbOpBatch; | ||
| use dpp::version::PlatformVersion; | ||
| use grovedb::TransactionArg; | ||
| use grovedb::{Element, TransactionArg, TreeType}; | ||
| use grovedb_path::SubtreePath; | ||
|
|
||
| impl Drive { | ||
| /// Creates the initial state structure (v3). | ||
| /// | ||
| /// Extends v2 by initializing shielded pool trees (commitment tree, | ||
| /// nullifier set, anchor history). | ||
| /// Creates the initial state structure. | ||
| pub(super) fn create_initial_state_structure_v3( | ||
| &self, | ||
| transaction: TransactionArg, | ||
| platform_version: &PlatformVersion, | ||
| ) -> Result<(), Error> { | ||
| // v3 currently delegates to v2. | ||
| // Shielded pool tree initialization will be added here once the | ||
| // RootTree variants for the shielded pool are defined. | ||
| self.create_initial_state_structure_v2(transaction, platform_version) | ||
| let drive_version = &platform_version.drive; | ||
| self.create_initial_state_structure_top_level_0(transaction, platform_version)?; | ||
|
|
||
| self.grove_insert_empty_tree( | ||
| SubtreePath::empty(), | ||
| &[RootTree::GroupActions as u8], | ||
| TreeType::NormalTree, | ||
| transaction, | ||
| None, | ||
| &mut vec![], | ||
| drive_version, | ||
| )?; | ||
|
|
||
| // AddressBalances top-level sum tree (introduced in v2) | ||
| self.grove_insert_empty_tree( | ||
| SubtreePath::empty(), | ||
| &[RootTree::AddressBalances as u8], | ||
| TreeType::SumTree, | ||
| transaction, | ||
| None, | ||
| &mut vec![], | ||
| drive_version, | ||
| )?; | ||
|
|
||
| // SavedBlockTransactions for address-based transaction sync | ||
| self.grove_insert_empty_tree( | ||
| SubtreePath::empty(), | ||
| &[RootTree::SavedBlockTransactions as u8], | ||
| TreeType::NormalTree, | ||
| transaction, | ||
| None, | ||
| &mut vec![], | ||
| drive_version, | ||
| )?; | ||
|
|
||
| // On lower layers we can use batching | ||
|
|
||
| let mut batch = | ||
| self.create_initial_state_structure_lower_layers_operations_0(platform_version)?; | ||
|
|
||
| self.initial_state_structure_lower_layers_add_operations_2(&mut batch, platform_version)?; | ||
|
|
||
| // Add shielded pool structures | ||
| self.initial_state_structure_shielded_pool_operations(&mut batch)?; | ||
|
|
||
| self.grove_apply_batch(batch, false, transaction, drive_version)?; | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| /// Adds shielded pool batch operations for initialization. | ||
| pub(in crate::drive::initialization) fn initial_state_structure_shielded_pool_operations( | ||
| &self, | ||
| batch: &mut GroveDbOpBatch, | ||
| ) -> Result<(), Error> { | ||
| // 1. Shielded credit pool SumTree under AddressBalances | ||
| batch.add_insert( | ||
| vec![vec![RootTree::AddressBalances as u8]], | ||
| vec![SHIELDED_CREDIT_POOL_KEY_U8], | ||
| Element::empty_sum_tree(), | ||
| ); | ||
|
|
||
| // 2. Notes tree (CommitmentTree = CountTree items + Sinsemilla Frontier) | ||
| batch.add_insert( | ||
| shielded_credit_pool_path_vec(), | ||
| vec![SHIELDED_NOTES_KEY], | ||
| Element::empty_commitment_tree(SHIELDED_NOTES_CHUNK_POWER), | ||
| ); | ||
|
|
||
| // 3. Nullifiers tree (ProvableCountTree) | ||
| batch.add_insert( | ||
| shielded_credit_pool_path_vec(), | ||
| vec![SHIELDED_NULLIFIERS_KEY], | ||
| Element::empty_provable_count_tree(), | ||
| ); | ||
|
|
||
| // 4. Total balance SumItem(0) | ||
| batch.add_insert( | ||
| shielded_credit_pool_path_vec(), | ||
| vec![SHIELDED_TOTAL_BALANCE_KEY], | ||
| Element::new_sum_item(0), | ||
| ); | ||
|
|
||
| // 5. Anchors tree (NormalTree) inside pool: block_height_be → anchor_bytes | ||
| batch.add_insert( | ||
| shielded_credit_pool_path_vec(), | ||
| vec![SHIELDED_ANCHORS_IN_POOL_KEY], | ||
| Element::empty_tree(), | ||
| ); | ||
|
|
||
| // 6. Nullifiers CountSumTree under SavedBlockTransactions for storing | ||
| // per-block nullifier lists. Each item is an ItemWithSumItem | ||
| // (serialized Vec<[u8;32]> + nullifier count as sum). | ||
| batch.add_insert( | ||
| Self::saved_block_transactions_path(), | ||
| vec![NULLIFIERS_KEY_U8], | ||
| Element::empty_count_sum_tree(), | ||
| ); | ||
|
|
||
| // 7. Compacted nullifiers NormalTree under SavedBlockTransactions for storing | ||
| // compacted/aggregated nullifier lists spanning multiple blocks. | ||
| // Key: (start_block, end_block) as 16 bytes, Value: serialized Vec<[u8;32]> | ||
| batch.add_insert( | ||
| Self::saved_block_transactions_path(), | ||
| vec![COMPACTED_NULLIFIERS_KEY_U8], | ||
| Element::empty_tree(), | ||
| ); | ||
|
|
||
| // 8. Nullifiers expiration time NormalTree under SavedBlockTransactions for storing | ||
| // expiration timestamps for compacted nullifier ranges. | ||
| batch.add_insert( | ||
| Self::saved_block_transactions_path(), | ||
| vec![NULLIFIERS_EXPIRATION_TIME_KEY_U8], | ||
| Element::empty_tree(), | ||
| ); | ||
|
|
||
| Ok(()) | ||
| } | ||
| } |
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
49 changes: 49 additions & 0 deletions
49
packages/rs-drive/src/drive/saved_block_transactions/cleanup_expired_nullifiers/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,49 @@ | ||
| mod v0; | ||
|
|
||
| use crate::drive::Drive; | ||
| use crate::error::drive::DriveError; | ||
| use crate::error::Error; | ||
| use grovedb::TransactionArg; | ||
| use platform_version::version::PlatformVersion; | ||
|
|
||
| impl Drive { | ||
| /// Cleans up expired compacted nullifier entries. | ||
| /// | ||
| /// This function queries the nullifier expiration time tree for entries with | ||
| /// expiration time <= current_block_time_ms, then deletes: | ||
| /// 1. The corresponding compacted nullifier entries | ||
| /// 2. The expiration time entries themselves | ||
| /// | ||
| /// # Arguments | ||
| /// * `current_block_time_ms` - The current block time in milliseconds | ||
| /// * `transaction` - Optional database transaction | ||
| /// * `platform_version` - The platform version | ||
| /// | ||
| /// # Returns | ||
| /// * `Ok(usize)` - The number of compacted entries that were cleaned up | ||
| /// * `Err` - An error occurred | ||
| pub fn cleanup_expired_nullifiers( | ||
| &self, | ||
| current_block_time_ms: u64, | ||
| transaction: TransactionArg, | ||
| platform_version: &PlatformVersion, | ||
| ) -> Result<usize, Error> { | ||
| match platform_version | ||
| .drive | ||
| .methods | ||
| .saved_block_transactions | ||
| .cleanup_expired_nullifiers | ||
| { | ||
| 0 => self.cleanup_expired_nullifiers_v0( | ||
| current_block_time_ms, | ||
| transaction, | ||
| platform_version, | ||
| ), | ||
| version => Err(Error::Drive(DriveError::UnknownVersionMismatch { | ||
| method: "cleanup_expired_nullifiers".to_string(), | ||
| known_versions: vec![0], | ||
| received: version, | ||
| })), | ||
| } | ||
| } | ||
| } |
95 changes: 95 additions & 0 deletions
95
packages/rs-drive/src/drive/saved_block_transactions/cleanup_expired_nullifiers/v0/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,95 @@ | ||
| use crate::drive::Drive; | ||
| use crate::error::Error; | ||
| use crate::util::batch::grovedb_op_batch::GroveDbOpBatchV0Methods; | ||
| use crate::util::batch::GroveDbOpBatch; | ||
| use dpp::ProtocolError; | ||
| use grovedb::query_result_type::QueryResultType; | ||
| use grovedb::{PathQuery, Query, QueryItem, SizedQuery, TransactionArg}; | ||
| use platform_version::version::PlatformVersion; | ||
|
|
||
| impl Drive { | ||
| /// Version 0 implementation of cleaning up expired compacted nullifier entries. | ||
| /// | ||
| /// Queries for all expiration entries with time <= current_block_time_ms, | ||
| /// then deletes the corresponding compacted entries and the expiration entries. | ||
| pub(super) fn cleanup_expired_nullifiers_v0( | ||
| &self, | ||
| current_block_time_ms: u64, | ||
| transaction: TransactionArg, | ||
| platform_version: &PlatformVersion, | ||
| ) -> Result<usize, Error> { | ||
| let expiration_path = Self::saved_nullifiers_expiration_time_path_vec(); | ||
|
|
||
| // Query all entries with expiration time <= current_block_time_ms | ||
| let mut query = Query::new(); | ||
| // Range from 0 to current_block_time_ms (inclusive) | ||
| query.insert_item(QueryItem::RangeToInclusive( | ||
| ..=current_block_time_ms.to_be_bytes().to_vec(), | ||
| )); | ||
|
|
||
| let path_query = | ||
| PathQuery::new(expiration_path.clone(), SizedQuery::new(query, None, None)); | ||
|
|
||
| let (results, _) = self.grove_get_path_query( | ||
| &path_query, | ||
| transaction, | ||
| QueryResultType::QueryKeyElementPairResultType, | ||
| &mut vec![], | ||
| &platform_version.drive, | ||
| )?; | ||
|
|
||
| let key_elements = results.to_key_elements(); | ||
|
|
||
| if key_elements.is_empty() { | ||
| return Ok(0); | ||
| } | ||
|
|
||
| let config = bincode::config::standard() | ||
| .with_big_endian() | ||
| .with_no_limit(); | ||
|
|
||
| let mut batch = GroveDbOpBatch::new(); | ||
| let mut total_cleaned = 0usize; | ||
|
|
||
| let compacted_path = Self::saved_compacted_block_transactions_nullifiers_path_vec(); | ||
|
|
||
| for (expiration_key, element) in key_elements { | ||
| // Get the vec of block ranges from the element | ||
| let grovedb::Element::Item(serialized_ranges, _) = element else { | ||
| return Err(Error::Protocol(Box::new( | ||
| ProtocolError::CorruptedSerialization( | ||
| "expected item element for expiration block ranges".to_string(), | ||
| ), | ||
| ))); | ||
| }; | ||
|
|
||
| // Deserialize the vec of block ranges | ||
| let (ranges, _): (Vec<(u64, u64)>, usize) = | ||
| bincode::decode_from_slice(&serialized_ranges, config).map_err(|e| { | ||
| Error::Protocol(Box::new(ProtocolError::CorruptedSerialization(format!( | ||
| "cannot decode expiration block ranges: {}", | ||
| e | ||
| )))) | ||
| })?; | ||
|
|
||
| // Delete each compacted nullifier entry | ||
| for (start_block, end_block) in &ranges { | ||
| let mut compacted_key = Vec::with_capacity(16); | ||
| compacted_key.extend_from_slice(&start_block.to_be_bytes()); | ||
| compacted_key.extend_from_slice(&end_block.to_be_bytes()); | ||
|
|
||
| batch.add_delete(compacted_path.clone(), compacted_key); | ||
| total_cleaned += 1; | ||
| } | ||
|
|
||
| // Delete the expiration entry itself | ||
| batch.add_delete(expiration_path.clone(), expiration_key); | ||
| } | ||
|
|
||
| if !batch.is_empty() { | ||
| self.grove_apply_batch(batch, false, transaction, &platform_version.drive)?; | ||
| } | ||
|
|
||
| Ok(total_cleaned) | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't silently skip proof assertions for shielded transitions.
These no-op arms let every new shielded action pass this helper without checking execution outcome, proof contents, or even the returned root hash. That means a broken shielded proof path can still go green here. Please verify these variants explicitly, or make the test fail until shielded verification is implemented.
🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is just temporary
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@QuantumExplorer, understood — thanks for the clarification. Since these are intentional placeholders pending a future PR, it would be helpful to add a brief comment (e.g.// TODO: implement shielded proof verification) to each arm so that it's clear to anyone reading the code that this is a known gap rather than an oversight. This also makes it easier to grep for when the follow-up implementation comes around.✏️ Learnings added
🧠 Learnings used