From 1430d4af49a114e6f9f610cebf5606b995ee879d Mon Sep 17 00:00:00 2001 From: Ivan Shumkov Date: Fri, 6 Oct 2023 12:15:58 +0700 Subject: [PATCH 1/4] chore: add a breaking changes checkbox to the PR template (#1455) --- .github/PULL_REQUEST_TEMPLATE.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index 1d8af7bf58..cda6858ce4 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -26,7 +26,8 @@ - [ ] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have added or updated relevant unit/integration/functional/e2e tests -- [ ] I have made corresponding changes to the documentation +- [ ] I have added "!" to the title and described breaking changes in the corresponding section if my code contains any +- [ ] I have made corresponding changes to the documentation if needed **For repository code-owners and collaborators only** - [ ] I have assigned this pull request to a milestone From 32043b8e7a5db0ed734ab0b0a0c1af91d13d7baf Mon Sep 17 00:00:00 2001 From: Quantum Explorer Date: Sat, 7 Oct 2023 20:37:43 +0700 Subject: [PATCH 2/4] fix: a lot of fixes for masternode updates --- .../src/execution/check_tx/v0/mod.rs | 2 + .../update_masternode_identities/v0/mod.rs | 3 + .../update_operator_identity/v0/mod.rs | 5 +- .../update_voter_identity/v0/mod.rs | 1 + .../create_genesis_state/v0/mod.rs | 5 +- .../data_triggers/triggers/dashpay/v0/mod.rs | 1 + .../triggers/reward_share/v0/mod.rs | 5 + packages/rs-drive/src/common/identities.rs | 1 + .../drive/batch/drive_op_batch/identity.rs | 25 ++-- .../identity/identity_create_transition.rs | 5 +- packages/rs-drive/src/drive/contract/mod.rs | 2 + .../src/drive/identity/balance/prove.rs | 2 + .../src/drive/identity/balance/update.rs | 18 ++- .../fetch/fetch_by_public_key_hashes/mod.rs | 1 + .../drive/identity/fetch/full_identity/mod.rs | 2 + .../prove/prove_full_identities/v0/mod.rs | 2 + .../v0/mod.rs | 1 + .../fetch/prove/prove_full_identity/v0/mod.rs | 1 + .../v0/mod.rs | 1 + .../v0/mod.rs | 1 + .../v0/mod.rs | 1 + .../identity/insert/add_new_identity/mod.rs | 16 ++- .../insert/add_new_identity/v0/mod.rs | 115 +++++++++++++++++- .../src/drive/identity/key/fetch/mod.rs | 4 + .../insert/create_key_tree_with_keys/mod.rs | 2 + .../create_key_tree_with_keys/v0/mod.rs | 13 +- .../insert/insert_new_non_unique_key/mod.rs | 9 +- .../insert_new_non_unique_key/v0/mod.rs | 21 ++-- .../key/insert/insert_new_unique_key/mod.rs | 4 +- .../insert/insert_new_unique_key/v0/mod.rs | 4 +- .../add_new_keys_to_identity/v0/mod.rs | 8 +- .../rs-drive/src/drive/identity/update/mod.rs | 37 +++++- 32 files changed, 269 insertions(+), 49 deletions(-) diff --git a/packages/rs-drive-abci/src/execution/check_tx/v0/mod.rs b/packages/rs-drive-abci/src/execution/check_tx/v0/mod.rs index 98cfb18b42..26395620ff 100644 --- a/packages/rs-drive-abci/src/execution/check_tx/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/check_tx/v0/mod.rs @@ -226,6 +226,7 @@ mod tests { .drive .add_new_identity( identity, + false, &BlockInfo::default(), true, None, @@ -294,6 +295,7 @@ mod tests { .drive .add_new_identity( identity, + false, &BlockInfo::default(), true, None, diff --git a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/update_masternode_identities/v0/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/update_masternode_identities/v0/mod.rs index 1432289d4c..938804b9a4 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/update_masternode_identities/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/update_masternode_identities/v0/mod.rs @@ -80,14 +80,17 @@ where drive_operations.push(IdentityOperation(AddNewIdentity { identity: owner_identity, + is_masternode_identity: true, })); drive_operations.push(IdentityOperation(AddNewIdentity { identity: voter_identity, + is_masternode_identity: true, })); drive_operations.push(IdentityOperation(AddNewIdentity { identity: operator_identity, + is_masternode_identity: true, })); } diff --git a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/update_operator_identity/v0/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/update_operator_identity/v0/mod.rs index a30cbfba86..98c1b95edb 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/update_operator_identity/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/update_operator_identity/v0/mod.rs @@ -263,7 +263,10 @@ where new_platform_node_id, platform_version, )?); - drive_operations.push(IdentityOperation(AddNewIdentity { identity })); + drive_operations.push(IdentityOperation(AddNewIdentity { + identity, + is_masternode_identity: true, + })); } Ok(()) } diff --git a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/update_voter_identity/v0/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/update_voter_identity/v0/mod.rs index 48576a7913..87ee96806d 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/update_voter_identity/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/update_voter_identity/v0/mod.rs @@ -140,6 +140,7 @@ where // other is that the drive_operations.push(IdentityOperation(AddNewIdentity { identity: new_voter_identity, + is_masternode_identity: true, })); } Ok(()) diff --git a/packages/rs-drive-abci/src/execution/platform_events/initialization/create_genesis_state/v0/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/initialization/create_genesis_state/v0/mod.rs index ba2e76354b..d345052133 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/initialization/create_genesis_state/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/initialization/create_genesis_state/v0/mod.rs @@ -223,7 +223,10 @@ impl Platform { operations: &mut Vec, ) { operations.push(DriveOperation::IdentityOperation( - IdentityOperationType::AddNewIdentity { identity }, + IdentityOperationType::AddNewIdentity { + identity, + is_masternode_identity: false, + }, )) } diff --git a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/documents_batch/data_triggers/triggers/dashpay/v0/mod.rs b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/documents_batch/data_triggers/triggers/dashpay/v0/mod.rs index 3214753b13..42b0751659 100644 --- a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/documents_batch/data_triggers/triggers/dashpay/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/documents_batch/data_triggers/triggers/dashpay/v0/mod.rs @@ -284,6 +284,7 @@ mod test { .drive .add_new_identity( identity_fixture, + false, &BlockInfo::default(), true, None, diff --git a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/documents_batch/data_triggers/triggers/reward_share/v0/mod.rs b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/documents_batch/data_triggers/triggers/reward_share/v0/mod.rs index 77c267a8f8..78c48f1e90 100644 --- a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/documents_batch/data_triggers/triggers/reward_share/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/documents_batch/data_triggers/triggers/reward_share/v0/mod.rs @@ -379,6 +379,7 @@ mod test { .drive .add_new_identity( identity, + false, &BlockInfo::default(), true, None, @@ -399,6 +400,7 @@ mod test { .drive .add_new_identity( identity, + false, &BlockInfo::default(), true, None, @@ -597,6 +599,7 @@ mod test { .drive .add_new_identity( identity, + false, &BlockInfo::default(), true, None, @@ -677,6 +680,7 @@ mod test { .drive .add_new_identity( main_identity, + false, &BlockInfo::default(), true, None, @@ -704,6 +708,7 @@ mod test { .drive .add_new_identity( identity, + false, &BlockInfo::default(), true, None, diff --git a/packages/rs-drive/src/common/identities.rs b/packages/rs-drive/src/common/identities.rs index c030896ead..27f3bcc2b8 100644 --- a/packages/rs-drive/src/common/identities.rs +++ b/packages/rs-drive/src/common/identities.rs @@ -127,6 +127,7 @@ pub fn create_test_identity_with_rng( drive .add_new_identity( identity.clone(), + false, &BlockInfo::default(), true, transaction, diff --git a/packages/rs-drive/src/drive/batch/drive_op_batch/identity.rs b/packages/rs-drive/src/drive/batch/drive_op_batch/identity.rs index d9c04521c6..80373003d4 100644 --- a/packages/rs-drive/src/drive/batch/drive_op_batch/identity.rs +++ b/packages/rs-drive/src/drive/batch/drive_op_batch/identity.rs @@ -15,9 +15,13 @@ use std::collections::HashMap; #[derive(Clone, Debug)] pub enum IdentityOperationType { /// Inserts a new identity to the `Identities` subtree. + /// A masternode identity is an identity, but can not have unique keys. + /// It also will skip testing for unique keys when adding non unique keys, so no one will + /// take a key, then add it to a masternode AddNewIdentity { /// The identity we wish to insert identity: Identity, + is_masternode_identity: bool, }, /// Adds balance to an identity AddToIdentityBalance { @@ -84,15 +88,18 @@ impl DriveLowLevelOperationConverter for IdentityOperationType { ) -> Result, Error> { let _drive_version = &platform_version.drive; match self { - IdentityOperationType::AddNewIdentity { identity } => drive - .add_new_identity_operations( - identity, - block_info, - &mut None, - estimated_costs_only_with_layer_info, - transaction, - platform_version, - ), + IdentityOperationType::AddNewIdentity { + identity, + is_masternode_identity, + } => drive.add_new_identity_operations( + identity, + is_masternode_identity, + block_info, + &mut None, + estimated_costs_only_with_layer_info, + transaction, + platform_version, + ), IdentityOperationType::AddToIdentityBalance { identity_id, added_balance, diff --git a/packages/rs-drive/src/drive/batch/transitions/identity/identity_create_transition.rs b/packages/rs-drive/src/drive/batch/transitions/identity/identity_create_transition.rs index ef347963a7..47c315ec2f 100644 --- a/packages/rs-drive/src/drive/batch/transitions/identity/identity_create_transition.rs +++ b/packages/rs-drive/src/drive/batch/transitions/identity/identity_create_transition.rs @@ -22,7 +22,10 @@ impl DriveHighLevelOperationConverter for IdentityCreateTransitionAction { Identity::try_from_identity_create_transition_action(self, platform_version)?; let drive_operations = vec![ - IdentityOperation(IdentityOperationType::AddNewIdentity { identity }), + IdentityOperation(IdentityOperationType::AddNewIdentity { + identity, + is_masternode_identity: false, + }), SystemOperation(SystemOperationType::AddToSystemCredits { amount: initial_balance_amount, }), diff --git a/packages/rs-drive/src/drive/contract/mod.rs b/packages/rs-drive/src/drive/contract/mod.rs index 01ba8c6093..fdca8354fe 100644 --- a/packages/rs-drive/src/drive/contract/mod.rs +++ b/packages/rs-drive/src/drive/contract/mod.rs @@ -379,6 +379,7 @@ mod tests { drive .add_new_identity( identity.clone(), + false, &BlockInfo::default(), true, Some(&db_transaction), @@ -440,6 +441,7 @@ mod tests { drive .add_new_identity( identity.clone(), + false, &BlockInfo::default(), true, Some(&db_transaction), diff --git a/packages/rs-drive/src/drive/identity/balance/prove.rs b/packages/rs-drive/src/drive/identity/balance/prove.rs index bbc048d8cc..4e47feebc6 100644 --- a/packages/rs-drive/src/drive/identity/balance/prove.rs +++ b/packages/rs-drive/src/drive/identity/balance/prove.rs @@ -83,6 +83,7 @@ mod tests { drive .add_new_identity( identity.clone(), + false, &BlockInfo::default(), true, None, @@ -127,6 +128,7 @@ mod tests { drive .add_new_identity( identity.clone(), + false, &BlockInfo::default(), true, None, diff --git a/packages/rs-drive/src/drive/identity/balance/update.rs b/packages/rs-drive/src/drive/identity/balance/update.rs index 2006091a56..6500ad8050 100644 --- a/packages/rs-drive/src/drive/identity/balance/update.rs +++ b/packages/rs-drive/src/drive/identity/balance/update.rs @@ -34,7 +34,14 @@ mod tests { let block_info = BlockInfo::default_with_epoch(Epoch::new(0).unwrap()); drive - .add_new_identity(identity.clone(), &block_info, true, None, platform_version) + .add_new_identity( + identity.clone(), + false, + &block_info, + true, + None, + platform_version, + ) .expect("expected to insert identity"); let db_transaction = drive.grove.start_transaction(); @@ -342,7 +349,14 @@ mod tests { let block = BlockInfo::default_with_epoch(Epoch::new(0).unwrap()); drive - .add_new_identity(identity.clone(), &block, true, None, platform_version) + .add_new_identity( + identity.clone(), + false, + &block, + true, + None, + platform_version, + ) .expect("expected to insert identity"); let db_transaction = drive.grove.start_transaction(); diff --git a/packages/rs-drive/src/drive/identity/fetch/fetch_by_public_key_hashes/mod.rs b/packages/rs-drive/src/drive/identity/fetch/fetch_by_public_key_hashes/mod.rs index c0293e8212..b41b9587d2 100644 --- a/packages/rs-drive/src/drive/identity/fetch/fetch_by_public_key_hashes/mod.rs +++ b/packages/rs-drive/src/drive/identity/fetch/fetch_by_public_key_hashes/mod.rs @@ -39,6 +39,7 @@ mod tests { drive .add_new_identity( identity.clone(), + false, &BlockInfo::default(), true, Some(&transaction), diff --git a/packages/rs-drive/src/drive/identity/fetch/full_identity/mod.rs b/packages/rs-drive/src/drive/identity/fetch/full_identity/mod.rs index ffc75fa8a8..1726990c31 100644 --- a/packages/rs-drive/src/drive/identity/fetch/full_identity/mod.rs +++ b/packages/rs-drive/src/drive/identity/fetch/full_identity/mod.rs @@ -31,6 +31,7 @@ mod tests { drive .add_new_identity( identity.as_ref().unwrap().clone(), + false, &BlockInfo::default(), true, None, @@ -89,6 +90,7 @@ mod tests { drive .add_new_identity( identity.clone(), + false, &BlockInfo::default(), true, None, diff --git a/packages/rs-drive/src/drive/identity/fetch/prove/prove_full_identities/v0/mod.rs b/packages/rs-drive/src/drive/identity/fetch/prove/prove_full_identities/v0/mod.rs index 0da697d3aa..51870f67d8 100644 --- a/packages/rs-drive/src/drive/identity/fetch/prove/prove_full_identities/v0/mod.rs +++ b/packages/rs-drive/src/drive/identity/fetch/prove/prove_full_identities/v0/mod.rs @@ -58,6 +58,7 @@ mod tests { drive .add_new_identity( identity.as_ref().unwrap().clone(), + false, &BlockInfo::default(), true, None, @@ -122,6 +123,7 @@ mod tests { drive .add_new_identity( identity.as_ref().unwrap().clone(), + false, &BlockInfo::default(), true, None, diff --git a/packages/rs-drive/src/drive/identity/fetch/prove/prove_full_identities_by_unique_public_key_hashes/v0/mod.rs b/packages/rs-drive/src/drive/identity/fetch/prove/prove_full_identities_by_unique_public_key_hashes/v0/mod.rs index 5406fd17e7..6938f3012f 100644 --- a/packages/rs-drive/src/drive/identity/fetch/prove/prove_full_identities_by_unique_public_key_hashes/v0/mod.rs +++ b/packages/rs-drive/src/drive/identity/fetch/prove/prove_full_identities_by_unique_public_key_hashes/v0/mod.rs @@ -71,6 +71,7 @@ mod tests { drive .add_new_identity( identity.clone(), + false, &BlockInfo::default(), true, None, diff --git a/packages/rs-drive/src/drive/identity/fetch/prove/prove_full_identity/v0/mod.rs b/packages/rs-drive/src/drive/identity/fetch/prove/prove_full_identity/v0/mod.rs index 6e0f89946a..4d815ce159 100644 --- a/packages/rs-drive/src/drive/identity/fetch/prove/prove_full_identity/v0/mod.rs +++ b/packages/rs-drive/src/drive/identity/fetch/prove/prove_full_identity/v0/mod.rs @@ -50,6 +50,7 @@ mod tests { drive .add_new_identity( identity.clone(), + false, &BlockInfo::default(), true, None, diff --git a/packages/rs-drive/src/drive/identity/fetch/prove/prove_full_identity_by_unique_public_key_hash/v0/mod.rs b/packages/rs-drive/src/drive/identity/fetch/prove/prove_full_identity_by_unique_public_key_hash/v0/mod.rs index c3d8e32e6e..1c0362898c 100644 --- a/packages/rs-drive/src/drive/identity/fetch/prove/prove_full_identity_by_unique_public_key_hash/v0/mod.rs +++ b/packages/rs-drive/src/drive/identity/fetch/prove/prove_full_identity_by_unique_public_key_hash/v0/mod.rs @@ -66,6 +66,7 @@ mod tests { drive .add_new_identity( identity.clone(), + false, &BlockInfo::default(), true, None, diff --git a/packages/rs-drive/src/drive/identity/fetch/prove/prove_identity_id_by_unique_public_key_hash/v0/mod.rs b/packages/rs-drive/src/drive/identity/fetch/prove/prove_identity_id_by_unique_public_key_hash/v0/mod.rs index 168b35bb76..34d5e219be 100644 --- a/packages/rs-drive/src/drive/identity/fetch/prove/prove_identity_id_by_unique_public_key_hash/v0/mod.rs +++ b/packages/rs-drive/src/drive/identity/fetch/prove/prove_identity_id_by_unique_public_key_hash/v0/mod.rs @@ -47,6 +47,7 @@ mod tests { drive .add_new_identity( identity.clone(), + false, &BlockInfo::default(), true, None, diff --git a/packages/rs-drive/src/drive/identity/fetch/prove/prove_identity_ids_by_unique_public_key_hashes/v0/mod.rs b/packages/rs-drive/src/drive/identity/fetch/prove/prove_identity_ids_by_unique_public_key_hashes/v0/mod.rs index 20e9c69d97..8b0fa4efd5 100644 --- a/packages/rs-drive/src/drive/identity/fetch/prove/prove_identity_ids_by_unique_public_key_hashes/v0/mod.rs +++ b/packages/rs-drive/src/drive/identity/fetch/prove/prove_identity_ids_by_unique_public_key_hashes/v0/mod.rs @@ -53,6 +53,7 @@ mod tests { drive .add_new_identity( identity.clone(), + false, &BlockInfo::default(), true, None, diff --git a/packages/rs-drive/src/drive/identity/insert/add_new_identity/mod.rs b/packages/rs-drive/src/drive/identity/insert/add_new_identity/mod.rs index 842f9ee04c..b34078456a 100644 --- a/packages/rs-drive/src/drive/identity/insert/add_new_identity/mod.rs +++ b/packages/rs-drive/src/drive/identity/insert/add_new_identity/mod.rs @@ -18,6 +18,7 @@ impl Drive { pub fn add_new_identity( &self, identity: Identity, + is_masternode_identity: bool, block_info: &BlockInfo, apply: bool, transaction: TransactionArg, @@ -30,9 +31,14 @@ impl Drive { .insert .add_new_identity { - 0 => { - self.add_new_identity_v0(identity, block_info, apply, transaction, platform_version) - } + 0 => self.add_new_identity_v0( + identity, + is_masternode_identity, + block_info, + apply, + transaction, + platform_version, + ), version => Err(Error::Drive(DriveError::UnknownVersionMismatch { method: "add_new_identity".to_string(), known_versions: vec![0], @@ -45,6 +51,7 @@ impl Drive { pub fn add_new_identity_add_to_operations( &self, identity: Identity, + is_masternode_identity: bool, block_info: &BlockInfo, apply: bool, previous_batch_operations: &mut Option<&mut Vec>, @@ -61,6 +68,7 @@ impl Drive { { 0 => self.add_new_identity_add_to_operations_v0( identity, + is_masternode_identity, block_info, apply, previous_batch_operations, @@ -80,6 +88,7 @@ impl Drive { pub fn add_new_identity_operations( &self, identity: Identity, + is_masternode_identity: bool, block_info: &BlockInfo, previous_batch_operations: &mut Option<&mut Vec>, estimated_costs_only_with_layer_info: &mut Option< @@ -97,6 +106,7 @@ impl Drive { { 0 => self.add_new_identity_operations_v0( identity, + is_masternode_identity, block_info, previous_batch_operations, estimated_costs_only_with_layer_info, diff --git a/packages/rs-drive/src/drive/identity/insert/add_new_identity/v0/mod.rs b/packages/rs-drive/src/drive/identity/insert/add_new_identity/v0/mod.rs index ab374e1dc4..c469efa581 100644 --- a/packages/rs-drive/src/drive/identity/insert/add_new_identity/v0/mod.rs +++ b/packages/rs-drive/src/drive/identity/insert/add_new_identity/v0/mod.rs @@ -8,18 +8,27 @@ use crate::fee::op::LowLevelDriveOperation; use dpp::block::block_info::BlockInfo; use dpp::fee::fee_result::FeeResult; use dpp::identity::accessors::IdentityGettersV0; -use dpp::identity::Identity; - +use dpp::identity::{Identity, IdentityPublicKey}; + +use crate::drive::identity::key::fetch::{ + IdentityKeysRequest, KeyIDIdentityPublicKeyPairBTreeMap, KeyIDVec, KeyRequestType, KeyVec, +}; +use crate::error::drive::DriveError; +use dpp::identity::identity_public_key::accessors::v0::{ + IdentityPublicKeyGettersV0, IdentityPublicKeySettersV0, +}; use dpp::version::PlatformVersion; use grovedb::batch::KeyInfoPath; use grovedb::{EstimatedLayerInformation, TransactionArg}; -use std::collections::HashMap; +use itertools::Itertools; +use std::collections::{BTreeSet, HashMap, HashSet}; impl Drive { /// Adds a identity by inserting a new identity subtree structure to the `Identities` subtree. pub(super) fn add_new_identity_v0( &self, identity: Identity, + is_masternode_identity: bool, block_info: &BlockInfo, apply: bool, transaction: TransactionArg, @@ -28,6 +37,7 @@ impl Drive { let mut drive_operations: Vec = vec![]; self.add_new_identity_add_to_operations_v0( identity, + is_masternode_identity, block_info, apply, &mut None, @@ -48,6 +58,7 @@ impl Drive { pub(super) fn add_new_identity_add_to_operations_v0( &self, identity: Identity, + is_masternode_identity: bool, block_info: &BlockInfo, apply: bool, previous_batch_operations: &mut Option<&mut Vec>, @@ -63,6 +74,7 @@ impl Drive { let batch_operations = self.add_new_identity_operations( identity, + is_masternode_identity, block_info, previous_batch_operations, &mut estimated_costs_only_with_layer_info, @@ -83,6 +95,7 @@ impl Drive { pub(super) fn add_new_identity_operations_v0( &self, identity: Identity, + is_masternode_identity: bool, block_info: &BlockInfo, previous_batch_operations: &mut Option<&mut Vec>, estimated_costs_only_with_layer_info: &mut Option< @@ -125,9 +138,95 @@ impl Drive { )?; if !inserted { - return Err(Error::Identity(IdentityError::IdentityAlreadyExists( - "trying to insert an identity that already exists", - ))); + return if is_masternode_identity { + // there could be a situation where we are trying to reenable a masternode identity that already existed + // In this case we should reenable it's keys + + //we need to know what keys existed before + let key_request = IdentityKeysRequest { + identity_id: id.to_buffer(), + request_type: KeyRequestType::AllKeys, + limit: None, + offset: None, + }; + + let old_masternode_identity_keys = self + .fetch_identity_keys::( + key_request, + transaction, + platform_version, + )?; + + let mut last_key_id = *old_masternode_identity_keys.keys().max().unwrap(); + + if old_masternode_identity_keys.is_empty() { + return Err(Error::Drive(DriveError::CorruptedDriveState(format!( + "expected old keys to exist if the masternode identity {} used to exist", + id + )))); + } + + // we need the old key ids, this is why we do things like this + + let mut old_masternode_identity_keys_to_reenable = + old_masternode_identity_keys.values().collect::>(); + + old_masternode_identity_keys_to_reenable.retain(|old_public_key| { + public_keys + .iter() + .map(|(_, key)| key.data()) + .contains(old_public_key.data()) + }); + + // we should find what keys should be re-enabled first + batch_operations.append( + &mut self.re_enable_identity_keys_operations( + id.to_buffer(), + old_masternode_identity_keys_to_reenable + .iter() + .map(|identity_public_key| identity_public_key.id()) + .collect(), + estimated_costs_only_with_layer_info, + transaction, + platform_version, + )?, + ); + + let old_masternode_identity_keys_to_reenable_data = + old_masternode_identity_keys_to_reenable + .iter() + .map(|key| key.data()) + .collect::>(); + + //we might also need to add new keys (in the case of an operator) + + for mut identity_public_key in public_keys.into_iter().map(|(_, key)| key) { + if old_masternode_identity_keys_to_reenable_data + .contains(identity_public_key.data()) + { + // this was reenabled + continue; + } + last_key_id += 1; + identity_public_key.set_id(last_key_id); + self.insert_new_non_unique_key_operations( + id.to_buffer(), + identity_public_key, + false, + true, + &block_info.epoch, + estimated_costs_only_with_layer_info, + transaction, + &mut batch_operations, + platform_version, + )?; + } + Ok(batch_operations) + } else { + Err(Error::Identity(IdentityError::IdentityAlreadyExists( + "trying to insert an identity that already exists", + ))) + }; } if let Some(estimated_costs_only_with_layer_info) = estimated_costs_only_with_layer_info { @@ -151,6 +250,8 @@ 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 + is_masternode_identity, &block_info.epoch, estimated_costs_only_with_layer_info, transaction, @@ -193,6 +294,7 @@ mod tests { drive .add_new_identity_v0( identity.clone(), + false, &BlockInfo::default(), true, Some(&transaction), @@ -229,6 +331,7 @@ mod tests { drive .add_new_identity_v0( identity, + false, &BlockInfo::default(), true, Some(&db_transaction), diff --git a/packages/rs-drive/src/drive/identity/key/fetch/mod.rs b/packages/rs-drive/src/drive/identity/key/fetch/mod.rs index d653eaf86a..66c86d8a4b 100644 --- a/packages/rs-drive/src/drive/identity/key/fetch/mod.rs +++ b/packages/rs-drive/src/drive/identity/key/fetch/mod.rs @@ -1029,6 +1029,7 @@ mod tests { drive .add_new_identity( identity.clone(), + false, &BlockInfo::default(), true, Some(&transaction), @@ -1066,6 +1067,7 @@ mod tests { drive .add_new_identity( identity.clone(), + false, &BlockInfo::default(), true, Some(&transaction), @@ -1106,6 +1108,7 @@ mod tests { drive .add_new_identity( identity.clone(), + false, &BlockInfo::default(), true, Some(&transaction), @@ -1146,6 +1149,7 @@ mod tests { drive .add_new_identity( identity.clone(), + false, &BlockInfo::default(), true, Some(&transaction), diff --git a/packages/rs-drive/src/drive/identity/key/insert/create_key_tree_with_keys/mod.rs b/packages/rs-drive/src/drive/identity/key/insert/create_key_tree_with_keys/mod.rs index c89d8bd20e..07212bbf68 100644 --- a/packages/rs-drive/src/drive/identity/key/insert/create_key_tree_with_keys/mod.rs +++ b/packages/rs-drive/src/drive/identity/key/insert/create_key_tree_with_keys/mod.rs @@ -34,6 +34,7 @@ impl Drive { &self, identity_id: [u8; 32], keys: Vec, + register_all_keys_as_non_unique: bool, epoch: &Epoch, estimated_costs_only_with_layer_info: &mut Option< HashMap, @@ -52,6 +53,7 @@ impl Drive { 0 => self.create_key_tree_with_keys_operations_v0( identity_id, keys, + register_all_keys_as_non_unique, epoch, estimated_costs_only_with_layer_info, transaction, diff --git a/packages/rs-drive/src/drive/identity/key/insert/create_key_tree_with_keys/v0/mod.rs b/packages/rs-drive/src/drive/identity/key/insert/create_key_tree_with_keys/v0/mod.rs index 09189b8f57..b17b44f5a0 100644 --- a/packages/rs-drive/src/drive/identity/key/insert/create_key_tree_with_keys/v0/mod.rs +++ b/packages/rs-drive/src/drive/identity/key/insert/create_key_tree_with_keys/v0/mod.rs @@ -44,6 +44,7 @@ impl Drive { &self, identity_id: [u8; 32], keys: Vec, + register_all_keys_as_non_unique_for_masternode: bool, epoch: &Epoch, estimated_costs_only_with_layer_info: &mut Option< HashMap, @@ -85,9 +86,13 @@ impl Drive { drive_version, )?; - let (unique_keys, non_unique_keys): (Vec, Vec) = keys - .into_iter() - .partition(|key| key.key_type().is_unique_key_type()); + let (unique_keys, non_unique_keys): (Vec, Vec) = + if register_all_keys_as_non_unique_for_masternode { + (vec![], keys) + } else { + keys.into_iter() + .partition(|key| key.key_type().is_unique_key_type()) + }; for key in unique_keys.into_iter() { self.insert_new_unique_key_operations( @@ -106,6 +111,8 @@ impl Drive { self.insert_new_non_unique_key_operations( identity_id, key, + // if we are a masternode this then we should have false here + !register_all_keys_as_non_unique_for_masternode, true, epoch, estimated_costs_only_with_layer_info, diff --git a/packages/rs-drive/src/drive/identity/key/insert/insert_new_non_unique_key/mod.rs b/packages/rs-drive/src/drive/identity/key/insert/insert_new_non_unique_key/mod.rs index dfe476fa8e..4b4123e4a0 100644 --- a/packages/rs-drive/src/drive/identity/key/insert/insert_new_non_unique_key/mod.rs +++ b/packages/rs-drive/src/drive/identity/key/insert/insert_new_non_unique_key/mod.rs @@ -20,7 +20,8 @@ impl Drive { /// /// * `identity_id` - An array of bytes representing the identity id. /// * `identity_key` - The `IdentityPublicKey` to be inserted. - /// * `with_references` - A boolean value indicating whether to include references in the operations. + /// * `with_reference_to_non_unique_key` - A boolean value indicating whether to add to the non unique key tree lookup. Only should be true for masternodes. + /// * `with_searchable_inner_references` - A boolean value indicating whether to build the search tree, allowing to query for the key based on key type and purpose and security level (todo verify this statement). /// * `epoch` - The current epoch. /// * `estimated_costs_only_with_layer_info` - A mutable reference to an optional `HashMap` that may contain estimated layer information. /// * `transaction` - The transaction arguments. @@ -38,7 +39,8 @@ impl Drive { &self, identity_id: [u8; 32], identity_key: IdentityPublicKey, - with_references: bool, + with_reference_to_non_unique_key: bool, + with_searchable_inner_references: bool, epoch: &Epoch, estimated_costs_only_with_layer_info: &mut Option< HashMap, @@ -58,7 +60,8 @@ impl Drive { 0 => self.insert_new_non_unique_key_operations_v0( identity_id, identity_key, - with_references, + with_reference_to_non_unique_key, + with_searchable_inner_references, epoch, estimated_costs_only_with_layer_info, transaction, diff --git a/packages/rs-drive/src/drive/identity/key/insert/insert_new_non_unique_key/v0/mod.rs b/packages/rs-drive/src/drive/identity/key/insert/insert_new_non_unique_key/v0/mod.rs index 3ba6b6ff9a..95e2869bd1 100644 --- a/packages/rs-drive/src/drive/identity/key/insert/insert_new_non_unique_key/v0/mod.rs +++ b/packages/rs-drive/src/drive/identity/key/insert/insert_new_non_unique_key/v0/mod.rs @@ -17,7 +17,8 @@ impl Drive { &self, identity_id: [u8; 32], identity_key: IdentityPublicKey, - with_references: bool, + with_reference_to_non_unique_key: bool, + with_searchable_inner_references: bool, epoch: &Epoch, estimated_costs_only_with_layer_info: &mut Option< HashMap, @@ -26,13 +27,15 @@ impl Drive { drive_operations: &mut Vec, platform_version: &PlatformVersion, ) -> Result<(), Error> { - drive_operations.append(&mut self.insert_reference_to_non_unique_key_operations( - identity_id, - &identity_key, - estimated_costs_only_with_layer_info, - transaction, - &platform_version.drive, - )?); + if with_reference_to_non_unique_key { + drive_operations.append(&mut self.insert_reference_to_non_unique_key_operations( + identity_id, + &identity_key, + estimated_costs_only_with_layer_info, + transaction, + &platform_version.drive, + )?); + } let key_id_bytes = identity_key.id().encode_var_vec(); @@ -57,7 +60,7 @@ impl Drive { // if we set that we wanted to add references we should construct those - if with_references + if with_searchable_inner_references && matches!( identity_key.purpose(), Purpose::AUTHENTICATION | Purpose::WITHDRAW diff --git a/packages/rs-drive/src/drive/identity/key/insert/insert_new_unique_key/mod.rs b/packages/rs-drive/src/drive/identity/key/insert/insert_new_unique_key/mod.rs index e9f14267fe..3bbf64178e 100644 --- a/packages/rs-drive/src/drive/identity/key/insert/insert_new_unique_key/mod.rs +++ b/packages/rs-drive/src/drive/identity/key/insert/insert_new_unique_key/mod.rs @@ -38,7 +38,7 @@ impl Drive { &self, identity_id: [u8; 32], identity_key: IdentityPublicKey, - with_references: bool, + with_searchable_inner_references: bool, epoch: &Epoch, estimated_costs_only_with_layer_info: &mut Option< HashMap, @@ -58,7 +58,7 @@ impl Drive { 0 => self.insert_new_unique_key_operations_v0( identity_id, identity_key, - with_references, + with_searchable_inner_references, epoch, estimated_costs_only_with_layer_info, transaction, diff --git a/packages/rs-drive/src/drive/identity/key/insert/insert_new_unique_key/v0/mod.rs b/packages/rs-drive/src/drive/identity/key/insert/insert_new_unique_key/v0/mod.rs index 574d318ea4..20933bde0c 100644 --- a/packages/rs-drive/src/drive/identity/key/insert/insert_new_unique_key/v0/mod.rs +++ b/packages/rs-drive/src/drive/identity/key/insert/insert_new_unique_key/v0/mod.rs @@ -17,7 +17,7 @@ impl Drive { &self, identity_id: [u8; 32], identity_key: IdentityPublicKey, - with_references: bool, + with_searchable_inner_references: bool, epoch: &Epoch, estimated_costs_only_with_layer_info: &mut Option< HashMap, @@ -55,7 +55,7 @@ impl Drive { platform_version, )?; - if with_references + if with_searchable_inner_references && matches!( identity_key.purpose(), Purpose::AUTHENTICATION | Purpose::WITHDRAW diff --git a/packages/rs-drive/src/drive/identity/update/methods/add_new_keys_to_identity/v0/mod.rs b/packages/rs-drive/src/drive/identity/update/methods/add_new_keys_to_identity/v0/mod.rs index 7499501c96..f33d3b5f09 100644 --- a/packages/rs-drive/src/drive/identity/update/methods/add_new_keys_to_identity/v0/mod.rs +++ b/packages/rs-drive/src/drive/identity/update/methods/add_new_keys_to_identity/v0/mod.rs @@ -11,12 +11,13 @@ use std::collections::HashMap; impl Drive { /// The operations for adding new keys to an identity + /// This should not be called for adding new keys to a masternode pub fn add_new_keys_to_identity_operations_v0( &self, identity_id: [u8; 32], unique_keys_to_add: Vec, non_unique_keys_to_add: Vec, - with_references: bool, + with_searchable_inner_references: bool, epoch: &Epoch, estimated_costs_only_with_layer_info: &mut Option< HashMap, @@ -37,7 +38,7 @@ impl Drive { self.insert_new_unique_key_operations( identity_id, key, - with_references, + with_searchable_inner_references, epoch, estimated_costs_only_with_layer_info, transaction, @@ -50,7 +51,8 @@ impl Drive { self.insert_new_non_unique_key_operations( identity_id, key, - with_references, + true, + with_searchable_inner_references, epoch, estimated_costs_only_with_layer_info, transaction, diff --git a/packages/rs-drive/src/drive/identity/update/mod.rs b/packages/rs-drive/src/drive/identity/update/mod.rs index 2120d143db..fcdee4602d 100644 --- a/packages/rs-drive/src/drive/identity/update/mod.rs +++ b/packages/rs-drive/src/drive/identity/update/mod.rs @@ -31,7 +31,14 @@ mod tests { let block = BlockInfo::default_with_epoch(Epoch::new(0).unwrap()); drive - .add_new_identity(identity.clone(), &block, true, None, platform_version) + .add_new_identity( + identity.clone(), + false, + &block, + true, + None, + platform_version, + ) .expect("expected to insert identity"); let new_keys_to_add = @@ -84,7 +91,14 @@ mod tests { let block = BlockInfo::default_with_epoch(Epoch::new(0).unwrap()); drive - .add_new_identity(identity.clone(), &block, true, None, platform_version) + .add_new_identity( + identity.clone(), + false, + &block, + true, + None, + platform_version, + ) .expect("expected to insert identity"); let new_keys_to_add = @@ -196,7 +210,14 @@ mod tests { let block_info = BlockInfo::default_with_epoch(Epoch::new(0).unwrap()); drive - .add_new_identity(identity.clone(), &block_info, true, None, platform_version) + .add_new_identity( + identity.clone(), + false, + &block_info, + true, + None, + platform_version, + ) .expect("expected to insert identity"); let new_keys_to_add = IdentityPublicKey::random_keys(5, 2, Some(15), platform_version); @@ -317,6 +338,7 @@ mod tests { drive .add_new_identity( identity.clone(), + false, &BlockInfo::default(), true, None, @@ -374,7 +396,14 @@ mod tests { let block_info = BlockInfo::default_with_epoch(Epoch::new(0).unwrap()); drive - .add_new_identity(identity.clone(), &block_info, true, None, platform_version) + .add_new_identity( + identity.clone(), + false, + &block_info, + true, + None, + platform_version, + ) .expect("expected to insert identity"); let revision = 2; From 1800cb8a345ba665f85fe61f5d4e60c2249ae3e3 Mon Sep 17 00:00:00 2001 From: Quantum Explorer Date: Sat, 7 Oct 2023 20:58:49 +0700 Subject: [PATCH 3/4] fix --- packages/rs-drive-abci/tests/strategy_tests/main.rs | 6 +++--- .../rs-drive/src/drive/batch/drive_op_batch/identity.rs | 2 ++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/rs-drive-abci/tests/strategy_tests/main.rs b/packages/rs-drive-abci/tests/strategy_tests/main.rs index 84927ce534..f28867a5bb 100644 --- a/packages/rs-drive-abci/tests/strategy_tests/main.rs +++ b/packages/rs-drive-abci/tests/strategy_tests/main.rs @@ -393,7 +393,7 @@ mod tests { .expect("expected to fetch balances") .expect("expected to have an identity to get balance from"); - assert_eq!(balance, 99865284600) + assert_eq!(balance, 99868433120) } #[test] @@ -920,7 +920,7 @@ mod tests { .unwrap() .unwrap() ), - "590a89878629f4eacf70f7c74dda45aeb2625607284fe336ead7c55690f7fd71".to_string() + "a6a988b614f03261b695fd35e8a62bb7d964b407c573c574b90a6a2db02aca4b".to_string() ) } @@ -1468,7 +1468,7 @@ mod tests { .unwrap() .unwrap() ), - "d1797227cd4454b80254983a1cb437aa112556d7f19d87fff62c0c61b5fa2c5b".to_string() + "bd7d749fe03ae50129d52d2524f9a6140b1eb8518df099811dac5b17c33ff871".to_string() ) } diff --git a/packages/rs-drive/src/drive/batch/drive_op_batch/identity.rs b/packages/rs-drive/src/drive/batch/drive_op_batch/identity.rs index 80373003d4..a05cf0940f 100644 --- a/packages/rs-drive/src/drive/batch/drive_op_batch/identity.rs +++ b/packages/rs-drive/src/drive/batch/drive_op_batch/identity.rs @@ -21,6 +21,8 @@ pub enum IdentityOperationType { AddNewIdentity { /// The identity we wish to insert identity: Identity, + /// Is this identity a masternode identity + /// On Masternode identities we do not add lookup key hashes is_masternode_identity: bool, }, /// Adds balance to an identity From 5bdd930dd2b92ece52e1641dc083ec333648a642 Mon Sep 17 00:00:00 2001 From: Quantum Explorer Date: Mon, 9 Oct 2023 19:37:04 +0700 Subject: [PATCH 4/4] fixed some clippy warnings --- .../v0/mod.rs | 12 ++++-------- .../drive/identity/insert/add_new_identity/v0/mod.rs | 12 ++++++------ 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/packages/rs-drive/src/drive/identity/estimation_costs/for_identity_contract_info_group_key_purpose/v0/mod.rs b/packages/rs-drive/src/drive/identity/estimation_costs/for_identity_contract_info_group_key_purpose/v0/mod.rs index 079c87152f..2a2d2405d7 100644 --- a/packages/rs-drive/src/drive/identity/estimation_costs/for_identity_contract_info_group_key_purpose/v0/mod.rs +++ b/packages/rs-drive/src/drive/identity/estimation_costs/for_identity_contract_info_group_key_purpose/v0/mod.rs @@ -1,17 +1,13 @@ -use crate::drive::{identity_tree_path, Drive}; +use crate::drive::Drive; use grovedb::batch::KeyInfoPath; -use grovedb::EstimatedLayerCount::{ApproximateElements, EstimatedLevel, PotentiallyAtMaxElements}; +use grovedb::EstimatedLayerCount::ApproximateElements; use grovedb::EstimatedLayerInformation; -use grovedb::EstimatedLayerSizes::{AllItems, AllReference, AllSubtrees}; +use grovedb::EstimatedLayerSizes::AllReference; use crate::drive::identity::estimation_costs::KEY_REFERENCE_SIZE; -use crate::drive::identity::{ - identity_contract_info_group_path_key_purpose_vec, identity_contract_info_group_path_vec, - identity_contract_info_root_path, identity_contract_info_root_path_vec, -}; +use crate::drive::identity::identity_contract_info_group_path_key_purpose_vec; use dpp::identity::Purpose; -use grovedb::EstimatedSumTrees::{NoSumTrees, SomeSumTrees}; use std::collections::HashMap; impl Drive { diff --git a/packages/rs-drive/src/drive/identity/insert/add_new_identity/v0/mod.rs b/packages/rs-drive/src/drive/identity/insert/add_new_identity/v0/mod.rs index c469efa581..62228868ff 100644 --- a/packages/rs-drive/src/drive/identity/insert/add_new_identity/v0/mod.rs +++ b/packages/rs-drive/src/drive/identity/insert/add_new_identity/v0/mod.rs @@ -8,10 +8,10 @@ use crate::fee::op::LowLevelDriveOperation; use dpp::block::block_info::BlockInfo; use dpp::fee::fee_result::FeeResult; use dpp::identity::accessors::IdentityGettersV0; -use dpp::identity::{Identity, IdentityPublicKey}; +use dpp::identity::Identity; use crate::drive::identity::key::fetch::{ - IdentityKeysRequest, KeyIDIdentityPublicKeyPairBTreeMap, KeyIDVec, KeyRequestType, KeyVec, + IdentityKeysRequest, KeyIDIdentityPublicKeyPairBTreeMap, KeyRequestType, }; use crate::error::drive::DriveError; use dpp::identity::identity_public_key::accessors::v0::{ @@ -21,7 +21,7 @@ use dpp::version::PlatformVersion; use grovedb::batch::KeyInfoPath; use grovedb::{EstimatedLayerInformation, TransactionArg}; use itertools::Itertools; -use std::collections::{BTreeSet, HashMap, HashSet}; +use std::collections::{BTreeSet, HashMap}; impl Drive { /// Adds a identity by inserting a new identity subtree structure to the `Identities` subtree. @@ -173,8 +173,8 @@ impl Drive { old_masternode_identity_keys_to_reenable.retain(|old_public_key| { public_keys - .iter() - .map(|(_, key)| key.data()) + .values() + .map(|key| key.data()) .contains(old_public_key.data()) }); @@ -200,7 +200,7 @@ impl Drive { //we might also need to add new keys (in the case of an operator) - for mut identity_public_key in public_keys.into_iter().map(|(_, key)| key) { + for mut identity_public_key in public_keys.into_values() { if old_masternode_identity_keys_to_reenable_data .contains(identity_public_key.data()) {