diff --git a/packages/rs-drive/src/drive/document/insert/mod.rs b/packages/rs-drive/src/drive/document/insert/mod.rs index f0eb33b8a7..d439a05e99 100644 --- a/packages/rs-drive/src/drive/document/insert/mod.rs +++ b/packages/rs-drive/src/drive/document/insert/mod.rs @@ -315,78 +315,86 @@ mod tests { } #[test] - fn test_add_dashpay_profile_with_fee() { - let drive = setup_drive_with_initial_state_structure(None); + fn test_add_dashpay_profile_with_fee_first_version_apply() { + let platform_version = PlatformVersion::first(); + let expected_fee_result = FeeResult { + storage_fee: 1305 + * Epoch::new(0).unwrap().cost_for_known_cost_item( + &EPOCH_CHANGE_FEE_VERSION_TEST, + StorageDiskUsageCreditPerByte, + ), + processing_fee: 900400, + ..Default::default() + }; + + do_test_add_dashpay_profile_with_fee(true, platform_version, expected_fee_result); + } - let db_transaction = drive.grove.start_transaction(); + #[test] + fn test_add_dashpay_profile_with_fee_first_version_estimated() { + let platform_version = PlatformVersion::first(); + let expected_fee_result = FeeResult { + storage_fee: 1305 + * Epoch::new(0).unwrap().cost_for_known_cost_item( + &EPOCH_CHANGE_FEE_VERSION_TEST, + StorageDiskUsageCreditPerByte, + ), + processing_fee: 73253660, + ..Default::default() + }; + + do_test_add_dashpay_profile_with_fee(false, platform_version, expected_fee_result); + } + #[test] + fn test_add_dashpay_profile_with_fee_latest_version_apply() { let platform_version = PlatformVersion::latest(); - - let contract = setup_contract( - &drive, - "tests/supporting_files/contract/dashpay/dashpay-contract.json", - None, - Some(&db_transaction), - ); - - let document_type = contract - .document_type_for_name("profile") - .expect("expected to get document type"); - - let random_owner_id = rand::thread_rng().gen::<[u8; 32]>(); - - let dashpay_profile_document = json_document_to_document( - "tests/supporting_files/contract/dashpay/profile0.json", - Some(random_owner_id.into()), - document_type, - platform_version, - ) - .expect("expected to get cbor document"); - - let fee_result = drive - .add_document_for_contract( - DocumentAndContractInfo { - owned_document_info: OwnedDocumentInfo { - document_info: DocumentRefInfo(( - &dashpay_profile_document, - StorageFlags::optional_default_as_cow(), - )), - owner_id: Some(random_owner_id), - }, - contract: &contract, - document_type, - }, - false, - BlockInfo::default(), - true, - Some(&db_transaction), - platform_version, - None, - ) - .expect("expected to insert a document successfully"); - - assert_eq!( - fee_result, - FeeResult { - storage_fee: 1305 - * Epoch::new(0).unwrap().cost_for_known_cost_item( - &EPOCH_CHANGE_FEE_VERSION_TEST, - StorageDiskUsageCreditPerByte, - ), - processing_fee: 900400, - ..Default::default() - } - ); + let expected_fee_result = FeeResult { + storage_fee: 1305 + * Epoch::new(0).unwrap().cost_for_known_cost_item( + &EPOCH_CHANGE_FEE_VERSION_TEST, + StorageDiskUsageCreditPerByte, + ), + processing_fee: 900400, + ..Default::default() + }; + + do_test_add_dashpay_profile_with_fee(true, platform_version, expected_fee_result); } #[test] - fn test_add_dashpay_profile_average_case_cost_fee() { + fn test_add_dashpay_profile_with_fee_latest_version_estimated() { + let platform_version = PlatformVersion::latest(); + let expected_fee_result = FeeResult { + storage_fee: 1305 + * Epoch::new(0).unwrap().cost_for_known_cost_item( + &EPOCH_CHANGE_FEE_VERSION_TEST, + StorageDiskUsageCreditPerByte, + ), + processing_fee: 73253660, + ..Default::default() + }; + + do_test_add_dashpay_profile_with_fee(false, platform_version, expected_fee_result); + } + + /// This helper sets up the environment, adds a dashpay profile document, + /// and either applies or just estimates the cost. + /// + /// `apply`: if true, we commit the transaction (applying the changes). + /// if false, we do not commit, so changes are only estimated. + /// `platform_version`: which PlatformVersion to use. + /// `expected_fee_result`: the FeeResult we expect in the test assertion. + fn do_test_add_dashpay_profile_with_fee( + apply: bool, + platform_version: &PlatformVersion, + expected_fee_result: FeeResult, + ) { let drive = setup_drive_with_initial_state_structure(None); let db_transaction = drive.grove.start_transaction(); - let platform_version = PlatformVersion::latest(); - + // Setup contract let contract = setup_contract( &drive, "tests/supporting_files/contract/dashpay/dashpay-contract.json", @@ -398,8 +406,9 @@ mod tests { .document_type_for_name("profile") .expect("expected to get document type"); - let random_owner_id = rand::thread_rng().gen::<[u8; 32]>(); + let random_owner_id = random::<[u8; 32]>(); + // Build dashpay profile doc let dashpay_profile_document = json_document_to_document( "tests/supporting_files/contract/dashpay/profile0.json", Some(random_owner_id.into()), @@ -408,12 +417,8 @@ mod tests { ) .expect("expected to get cbor document"); - let FeeResult { - storage_fee, - processing_fee, - fee_refunds: _, - removed_bytes_from_system: _, - } = drive + // Perform the add operation, with either an apply or a dry run + let fee_result = drive .add_document_for_contract( DocumentAndContractInfo { owned_document_info: OwnedDocumentInfo { @@ -426,22 +431,16 @@ mod tests { contract: &contract, document_type, }, - false, + false, // override BlockInfo::default(), - false, + apply, Some(&db_transaction), platform_version, None, ) .expect("expected to insert a document successfully"); - let added_bytes = storage_fee - / Epoch::new(0).unwrap().cost_for_known_cost_item( - &EPOCH_CHANGE_FEE_VERSION_TEST, - StorageDiskUsageCreditPerByte, - ); - assert_eq!(1305, added_bytes); - assert_eq!(73253660, processing_fee); + assert_eq!(fee_result, expected_fee_result); } #[test] diff --git a/packages/rs-drive/src/drive/identity/balance/update.rs b/packages/rs-drive/src/drive/identity/balance/update.rs index dc7c94e3e7..aed30f9b24 100644 --- a/packages/rs-drive/src/drive/identity/balance/update.rs +++ b/packages/rs-drive/src/drive/identity/balance/update.rs @@ -21,10 +21,61 @@ mod tests { use super::*; #[test] - fn should_add_to_balance() { - let drive = setup_drive_with_initial_state_structure(None); + fn should_add_to_balance_first_version_apply() { + let platform_version = PlatformVersion::first(); + // Use the same fee result if you want, or adjust if needed: + let expected_fee_result = FeeResult { + processing_fee: 174660, + removed_bytes_from_system: 0, + ..Default::default() + }; + do_should_add_to_balance(true, platform_version, expected_fee_result); + } + + #[test] + fn should_add_to_balance_first_version_estimated() { + let platform_version = PlatformVersion::first(); + // Use the same fee result if you want, or adjust if needed: + let expected_fee_result = FeeResult { + processing_fee: 4278840, + removed_bytes_from_system: 0, + ..Default::default() + }; + + do_should_add_to_balance(false, platform_version, expected_fee_result); + } + + #[test] + fn should_add_to_balance_latest_version_apply() { let platform_version = PlatformVersion::latest(); + let expected_fee_result = FeeResult { + processing_fee: 174660, + removed_bytes_from_system: 0, + ..Default::default() + }; + + do_should_add_to_balance(true, platform_version, expected_fee_result); + } + + #[test] + fn should_add_to_balance_latest_version_estimated() { + let platform_version = PlatformVersion::latest(); + let expected_fee_result = FeeResult { + processing_fee: 4278840, + removed_bytes_from_system: 0, + ..Default::default() + }; + + do_should_add_to_balance(false, platform_version, expected_fee_result); + } + + fn do_should_add_to_balance( + apply: bool, + platform_version: &PlatformVersion, + expected_fee_result: FeeResult, + ) { + let drive = setup_drive_with_initial_state_structure(None); let identity = Identity::random_identity(5, Some(12345), platform_version) .expect("expected a random identity"); @@ -33,6 +84,7 @@ mod tests { let block_info = BlockInfo::default_with_epoch(Epoch::new(0).unwrap()); + // Add new identity (always apply here so it actually gets inserted) drive .add_new_identity( identity.clone(), @@ -48,43 +100,41 @@ mod tests { let amount = 300; + // Add to the identity balance, either applying or estimating let fee_result = drive .add_to_identity_balance( identity.id().to_buffer(), amount, &block_info, - true, + apply, Some(&db_transaction), platform_version, ) .expect("expected to add to identity balance"); - assert_eq!( - fee_result, - FeeResult { - processing_fee: 174660, - removed_bytes_from_system: 0, - ..Default::default() - } - ); - - drive - .grove - .commit_transaction(db_transaction) - .unwrap() - .expect("expected to be able to commit a transaction"); - - let (balance, _fee_cost) = drive - .fetch_identity_balance_with_costs( - identity.id().to_buffer(), - &block_info, - true, - None, - platform_version, - ) - .expect("expected to get balance"); - - assert_eq!(balance.unwrap(), old_balance + amount); + assert_eq!(fee_result, expected_fee_result); + + if apply { + // Commit if we are applying + drive + .grove + .commit_transaction(db_transaction) + .unwrap() + .expect("expected to be able to commit a transaction"); + + // Check the updated balance + let (balance, _fee_cost) = drive + .fetch_identity_balance_with_costs( + identity.id().to_buffer(), + &block_info, + true, + None, + platform_version, + ) + .expect("expected to get balance"); + + assert_eq!(balance.unwrap(), old_balance + amount); + } } #[test] @@ -110,9 +160,45 @@ mod tests { } #[test] - fn should_deduct_from_debt_if_balance_is_nil() { - let drive = setup_drive_with_initial_state_structure(None); + fn should_deduct_from_debt_if_balance_is_nil_first_version_apply() { + let platform_version = PlatformVersion::first(); + let expected_fee_result = FeeResult { + storage_fee: 0, + processing_fee: 385160, + removed_bytes_from_system: 0, + ..Default::default() + }; + + do_should_deduct_from_debt_if_balance_is_nil( + true, + platform_version, + expected_fee_result, + ); + } + + #[test] + fn should_deduct_from_debt_if_balance_is_nil_latest_version_apply() { let platform_version = PlatformVersion::latest(); + let expected_fee_result = FeeResult { + storage_fee: 0, + processing_fee: 385160, + removed_bytes_from_system: 0, + ..Default::default() + }; + + do_should_deduct_from_debt_if_balance_is_nil( + true, + platform_version, + expected_fee_result, + ); + } + + fn do_should_deduct_from_debt_if_balance_is_nil( + apply: bool, + platform_version: &PlatformVersion, + expected_fee_result: FeeResult, + ) { + let drive = setup_drive_with_initial_state_structure(None); let identity = create_test_identity(&drive, [0; 32], Some(1), None, platform_version) .expect("expected an identity"); @@ -147,55 +233,86 @@ mod tests { identity.id().to_buffer(), added_balance, &block_info, - true, + apply, None, platform_version, ) .expect("expected to add to identity balance"); - assert_eq!( - fee_result, - FeeResult { - storage_fee: 0, - processing_fee: 385160, - removed_bytes_from_system: 0, - ..Default::default() - } - ); + assert_eq!(fee_result, expected_fee_result); + + if apply { + let (updated_balance, _fee_cost) = drive + .fetch_identity_balance_with_costs( + identity.id().to_buffer(), + &block_info, + true, + None, + platform_version, + ) + .expect("expected to get balance"); + + assert_eq!( + updated_balance.expect("balance should present"), + added_balance - negative_amount + ); + + let updated_negative_balance = drive + .fetch_identity_negative_balance_operations( + identity.id().to_buffer(), + true, + None, + &mut drive_operations, + platform_version, + ) + .expect("expected to get negative balance") + .expect("negative balance should present"); + + assert_eq!(updated_negative_balance, 0); + } + } - let (updated_balance, _fee_cost) = drive - .fetch_identity_balance_with_costs( - identity.id().to_buffer(), - &block_info, - true, - None, - platform_version, - ) - .expect("expected to get balance"); + #[test] + fn should_keep_nil_balance_and_reduce_debt_if_added_balance_is_lower_first_version_apply() { + let platform_version = PlatformVersion::first(); + let expected_fee_result = FeeResult { + storage_fee: 0, + processing_fee: 260540, + removed_bytes_from_system: 0, + ..Default::default() + }; - assert_eq!( - updated_balance.expect("balance should present"), - added_balance - negative_amount + do_should_keep_nil_balance_and_reduce_debt_if_added_balance_is_lower( + true, + platform_version, + expected_fee_result, ); + } - let updated_negative_balance = drive - .fetch_identity_negative_balance_operations( - identity.id().to_buffer(), - true, - None, - &mut drive_operations, - platform_version, - ) - .expect("expected to get balance") - .expect("balance should present"); + #[test] + fn should_keep_nil_balance_and_reduce_debt_if_added_balance_is_lower_latest_version_apply() + { + let platform_version = PlatformVersion::latest(); + let expected_fee_result = FeeResult { + storage_fee: 0, + processing_fee: 260540, + removed_bytes_from_system: 0, + ..Default::default() + }; - assert_eq!(updated_negative_balance, 0) + do_should_keep_nil_balance_and_reduce_debt_if_added_balance_is_lower( + true, + platform_version, + expected_fee_result, + ); } - #[test] - fn should_keep_nil_balance_and_reduce_debt_if_added_balance_is_lower() { + fn do_should_keep_nil_balance_and_reduce_debt_if_added_balance_is_lower( + apply: bool, + platform_version: &PlatformVersion, + expected_fee_result: FeeResult, + ) { let drive = setup_drive_with_initial_state_structure(None); - let platform_version = PlatformVersion::latest(); let identity = create_test_identity(&drive, [0; 32], Some(1), None, platform_version) .expect("expected an identity"); @@ -229,130 +346,117 @@ mod tests { identity.id().to_buffer(), added_balance, &block_info, - true, + apply, None, platform_version, ) .expect("expected to add to identity balance"); - assert_eq!( - fee_result, - FeeResult { - storage_fee: 0, - processing_fee: 260540, - removed_bytes_from_system: 0, - ..Default::default() - } - ); - - let (updated_balance, _fee_cost) = drive - .fetch_identity_balance_with_costs( - identity.id().to_buffer(), - &block_info, - true, - None, - platform_version, - ) - .expect("expected to get balance"); + assert_eq!(fee_result, expected_fee_result); + + if apply { + let (updated_balance, _fee_cost) = drive + .fetch_identity_balance_with_costs( + identity.id().to_buffer(), + &block_info, + true, + None, + platform_version, + ) + .expect("expected to get balance"); + + assert_eq!(updated_balance.expect("balance should present"), 0); + + let updated_negative_balance = drive + .fetch_identity_negative_balance_operations( + identity.id().to_buffer(), + true, + None, + &mut drive_operations, + platform_version, + ) + .expect("expected to get balance") + .expect("balance should present"); + + assert_eq!(updated_negative_balance, negative_amount - added_balance); + } + } + } - assert_eq!(updated_balance.expect("balance should present"), 0); + mod remove_from_identity_balance { + use super::*; + use dpp::block::block_info::BlockInfo; + use dpp::fee::fee_result::FeeResult; + use dpp::version::PlatformVersion; - let updated_negative_balance = drive - .fetch_identity_negative_balance_operations( - identity.id().to_buffer(), - true, - None, - &mut drive_operations, - platform_version, - ) - .expect("expected to get balance") - .expect("balance should present"); + #[test] + fn should_remove_from_balance_first_version_apply() { + let platform_version = PlatformVersion::first(); + let expected_fee_result = FeeResult { + processing_fee: 174660, + removed_bytes_from_system: 0, + ..Default::default() + }; - assert_eq!(updated_negative_balance, negative_amount - added_balance) + do_should_remove_from_balance(true, platform_version, expected_fee_result); } #[test] - fn should_estimate_costs_without_state() { - let drive = setup_drive_with_initial_state_structure(None); - - let platform_version = PlatformVersion::latest(); - - let identity = Identity::random_identity(5, Some(12345), platform_version) - .expect("expected a random identity"); - - let block = BlockInfo::default_with_epoch(Epoch::new(0).unwrap()); - - let app_hash_before = drive - .grove - .root_hash(None, &platform_version.drive.grove_version) - .unwrap() - .expect("should return app hash"); - - let fee_result = drive - .add_to_identity_balance( - identity.id().to_buffer(), - 300, - &block, - false, - None, - platform_version, - ) - .expect("expected to get estimated costs to update an identity balance"); + fn should_remove_from_balance_first_version_estimated() { + let platform_version = PlatformVersion::first(); + let expected_fee_result = FeeResult { + processing_fee: 2476860, + removed_bytes_from_system: 0, + ..Default::default() + }; - assert_eq!( - fee_result, - FeeResult { - processing_fee: 4278840, - ..Default::default() - } - ); + do_should_remove_from_balance(false, platform_version, expected_fee_result); + } - let app_hash_after = drive - .grove - .root_hash(None, &platform_version.drive.grove_version) - .unwrap() - .expect("should return app hash"); + #[test] + fn should_remove_from_balance_latest_version_apply() { + let platform_version = PlatformVersion::latest(); + let expected_fee_result = FeeResult { + processing_fee: 174660, + removed_bytes_from_system: 0, + ..Default::default() + }; - assert_eq!(app_hash_after, app_hash_before); + do_should_remove_from_balance(true, platform_version, expected_fee_result); + } - let (balance, _fee_cost) = drive - .fetch_identity_balance_with_costs( - identity.id().to_buffer(), - &block, - true, - None, - platform_version, - ) - .expect("expected to get balance"); + #[test] + fn should_remove_from_balance_latest_version_estimated() { + let platform_version = PlatformVersion::latest(); + let expected_fee_result = FeeResult { + processing_fee: 2476860, + removed_bytes_from_system: 0, + ..Default::default() + }; - assert!(balance.is_none()); //shouldn't have changed + do_should_remove_from_balance(false, platform_version, expected_fee_result); } - } - mod remove_from_identity_balance { - use super::*; - use dpp::block::block_info::BlockInfo; - use dpp::fee::fee_result::FeeResult; - use dpp::version::PlatformVersion; - - #[test] - fn should_remove_from_balance() { + fn do_should_remove_from_balance( + apply: bool, + platform_version: &PlatformVersion, + expected_fee_result: FeeResult, + ) { let drive = setup_drive_with_initial_state_structure(None); - let platform_version = PlatformVersion::latest(); - let identity = Identity::random_identity(5, Some(12345), platform_version) .expect("expected a random identity"); let old_balance = identity.balance(); - let block = BlockInfo::default_with_epoch(Epoch::new(0).unwrap()); + let block_info = BlockInfo::default_with_epoch(Epoch::new(0).unwrap()); + // Insert the identity with apply=true so it's actually stored drive .add_new_identity( identity.clone(), false, - &block, + &block_info, true, None, platform_version, @@ -367,100 +471,35 @@ mod tests { .remove_from_identity_balance( identity.id().to_buffer(), amount, - &block, - true, + &block_info, + apply, Some(&db_transaction), platform_version, None, ) - .expect("expected to add to identity balance"); + .expect("expected to remove from identity balance"); - assert_eq!( - fee_result, - FeeResult { - processing_fee: 174660, - removed_bytes_from_system: 0, - ..Default::default() - } - ); + assert_eq!(fee_result, expected_fee_result); - drive - .grove - .commit_transaction(db_transaction) - .unwrap() - .expect("expected to be able to commit a transaction"); - - let (balance, _fee_cost) = drive - .fetch_identity_balance_with_costs( - identity.id().to_buffer(), - &block, - true, - None, - platform_version, - ) - .expect("expected to get balance"); - - assert_eq!(balance.unwrap(), old_balance - amount); - } - - #[test] - fn should_estimated_costs_without_state() { - let drive = setup_drive_with_initial_state_structure(None); - - let platform_version = PlatformVersion::latest(); - - let identity = Identity::random_identity(5, Some(12345), platform_version) - .expect("expected a random identity"); + if apply { + drive + .grove + .commit_transaction(db_transaction) + .unwrap() + .expect("expected to be able to commit a transaction"); - let block = BlockInfo::default_with_epoch(Epoch::new(0).unwrap()); - - let app_hash_before = drive - .grove - .root_hash(None, &platform_version.drive.grove_version) - .unwrap() - .expect("should return app hash"); - - let amount = 10; - - let fee_result = drive - .remove_from_identity_balance( - identity.id().to_buffer(), - amount, - &block, - false, - None, - platform_version, - None, - ) - .expect("expected to add to identity balance"); - - let app_hash_after = drive - .grove - .root_hash(None, &platform_version.drive.grove_version) - .unwrap() - .expect("should return app hash"); - - assert_eq!(app_hash_after, app_hash_before); - - assert_eq!( - fee_result, - FeeResult { - processing_fee: 2476860, - ..Default::default() - } - ); - - let (balance, _fee_cost) = drive - .fetch_identity_balance_with_costs( - identity.id().to_buffer(), - &block, - true, - None, - platform_version, - ) - .expect("expected to get balance"); + let (balance, _fee_cost) = drive + .fetch_identity_balance_with_costs( + identity.id().to_buffer(), + &block_info, + true, + None, + platform_version, + ) + .expect("expected to get balance"); - assert!(balance.is_none()); //shouldn't have changed + assert_eq!(balance.unwrap(), old_balance - amount); + } } } 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 bd03fbe38b..3494ceb37c 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 @@ -121,3 +121,104 @@ impl Drive { } } } + +#[cfg(test)] +mod tests { + use crate::util::test_helpers::setup::setup_drive; + use dpp::identity::Identity; + + use dpp::block::block_info::BlockInfo; + use dpp::fee::fee_result::FeeResult; + use dpp::identity::accessors::IdentityGettersV0; + + use dpp::version::PlatformVersion; + + #[test] + fn test_insert_and_fetch_identity_first_version() { + let platform_version = PlatformVersion::first(); + let expected_fee_result = FeeResult { + storage_fee: 128871000, + processing_fee: 2330320, + ..Default::default() + }; + + test_insert_and_fetch_identity(true, platform_version, expected_fee_result); + } + + #[test] + fn test_insert_and_fetch_identity_latest_version() { + let platform_version = PlatformVersion::latest(); + let expected_fee_result = FeeResult { + storage_fee: 128871000, + processing_fee: 2330320, + ..Default::default() + }; + + test_insert_and_fetch_identity(true, platform_version, expected_fee_result); + } + + #[test] + fn test_insert_identity_estimated_costs_first_version() { + let platform_version = PlatformVersion::first(); + let expected_fee_result = FeeResult { + storage_fee: 128871000, + processing_fee: 11764980, + ..Default::default() + }; + + test_insert_and_fetch_identity(false, platform_version, expected_fee_result); + } + + #[test] + fn test_insert_identity_estimated_costs_latest_version() { + let platform_version = PlatformVersion::latest(); + let expected_fee_result = FeeResult { + storage_fee: 128871000, + processing_fee: 11764980, + ..Default::default() + }; + + test_insert_and_fetch_identity(false, platform_version, expected_fee_result); + } + + fn test_insert_and_fetch_identity( + apply: bool, + platform_version: &PlatformVersion, + expected_fee_result: FeeResult, + ) { + let drive = setup_drive(None); + + let transaction = drive.grove.start_transaction(); + + drive + .create_initial_state_structure(Some(&transaction), platform_version) + .expect("expected to create root tree successfully"); + + let identity = Identity::random_identity(5, Some(12345), platform_version) + .expect("expected a random identity"); + + let fee_result = drive + .add_new_identity( + identity.clone(), + false, + &BlockInfo::default(), + apply, + Some(&transaction), + platform_version, + ) + .expect("expected to insert identity"); + + if apply { + let fetched_identity = drive + .fetch_full_identity( + identity.id().to_buffer(), + Some(&transaction), + platform_version, + ) + .expect("should fetch an identity") + .expect("should have an identity"); + assert_eq!(identity, fetched_identity); + } + assert_eq!(fee_result, expected_fee_result); + } +} 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 f756234431..536d680821 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 @@ -270,80 +270,3 @@ impl Drive { Ok(batch_operations) } } - -#[cfg(test)] -mod tests { - use crate::util::test_helpers::setup::{setup_drive, setup_drive_with_initial_state_structure}; - use dpp::identity::Identity; - - use dpp::block::block_info::BlockInfo; - use dpp::identity::accessors::IdentityGettersV0; - - use dpp::version::PlatformVersion; - - #[test] - fn test_insert_and_fetch_identity_v0() { - let drive = setup_drive(None); - let platform_version = PlatformVersion::first(); - - let transaction = drive.grove.start_transaction(); - - drive - .create_initial_state_structure(Some(&transaction), platform_version) - .expect("expected to create root tree successfully"); - - let identity = Identity::random_identity(5, Some(12345), platform_version) - .expect("expected a random identity"); - - drive - .add_new_identity_v0( - identity.clone(), - false, - &BlockInfo::default(), - true, - Some(&transaction), - platform_version, - ) - .expect("expected to insert identity"); - - let fetched_identity = drive - .fetch_full_identity( - identity.id().to_buffer(), - Some(&transaction), - platform_version, - ) - .expect("should fetch an identity") - .expect("should have an identity"); - - assert_eq!(identity, fetched_identity); - } - - #[test] - fn test_insert_identity_v0() { - let drive = setup_drive_with_initial_state_structure(None); - - let db_transaction = drive.grove.start_transaction(); - - let platform_version = PlatformVersion::latest(); - - let identity = Identity::random_identity(5, Some(12345), platform_version) - .expect("expected a random identity"); - - drive - .add_new_identity_v0( - identity, - false, - &BlockInfo::default(), - true, - Some(&db_transaction), - platform_version, - ) - .expect("expected to insert identity"); - - drive - .grove - .commit_transaction(db_transaction) - .unwrap() - .expect("expected to be able to commit a transaction"); - } -} diff --git a/packages/rs-drive/src/drive/identity/update/mod.rs b/packages/rs-drive/src/drive/identity/update/mod.rs index c846d8d43d..bae2413245 100644 --- a/packages/rs-drive/src/drive/identity/update/mod.rs +++ b/packages/rs-drive/src/drive/identity/update/mod.rs @@ -14,28 +14,87 @@ mod tests { mod add_new_keys_to_identity { use super::*; + use crate::drive::Drive; + use crate::fees::op::LowLevelDriveOperation; use dpp::block::block_info::BlockInfo; use dpp::block::epoch::Epoch; use dpp::fee::fee_result::FeeResult; use dpp::version::PlatformVersion; + use rand::prelude::StdRng; + use rand::{Rng, SeedableRng}; + // ------------------------------------------------- + // should_add_one_new_key_to_identity (4 tests) + // ------------------------------------------------- #[test] - fn should_add_one_new_key_to_identity() { - let drive = setup_drive_with_initial_state_structure(None); + fn should_add_one_new_key_to_identity_first_version_apply() { + let platform_version = PlatformVersion::first(); + let expected_fee_result = FeeResult { + storage_fee: 14202000, + processing_fee: 1098260, + ..Default::default() + }; + + do_should_add_one_new_key_to_identity(true, platform_version, expected_fee_result); + } + #[test] + fn should_add_one_new_key_to_identity_first_version_estimated() { let platform_version = PlatformVersion::first(); + // Adjust the expected processing_fee if it differs from "apply = true" + // or as needed for your scenario + let expected_fee_result = FeeResult { + storage_fee: 17145000, + processing_fee: 5483620, + ..Default::default() + }; + + do_should_add_one_new_key_to_identity(false, platform_version, expected_fee_result); + } + + #[test] + fn should_add_one_new_key_to_identity_latest_version_apply() { + let platform_version = PlatformVersion::latest(); + let expected_fee_result = FeeResult { + storage_fee: 14202000, + processing_fee: 1098260, + ..Default::default() + }; + + do_should_add_one_new_key_to_identity(true, platform_version, expected_fee_result); + } + + #[test] + fn should_add_one_new_key_to_identity_latest_version_estimated() { + let platform_version = PlatformVersion::latest(); + let expected_fee_result = FeeResult { + storage_fee: 17145000, + processing_fee: 5483620, + ..Default::default() + }; + + do_should_add_one_new_key_to_identity(false, platform_version, expected_fee_result); + } + + fn do_should_add_one_new_key_to_identity( + apply: bool, + platform_version: &PlatformVersion, + expected_fee_result: FeeResult, + ) { + let drive = setup_drive_with_initial_state_structure(None); let identity = Identity::random_identity(5, Some(12345), platform_version) .expect("expected a random identity"); let block = BlockInfo::default_with_epoch(Epoch::new(0).unwrap()); + // We assume the user wants to apply or just estimate for adding identity drive .add_new_identity( identity.clone(), false, &block, - true, + apply, None, platform_version, ) @@ -51,39 +110,90 @@ mod tests { identity.id().to_buffer(), new_keys_to_add, &block, - true, + apply, Some(&db_transaction), platform_version, ) .expect("expected to update identity with new keys"); - assert_eq!( - fee_result, - FeeResult { - storage_fee: 14202000, - processing_fee: 1098260, - ..Default::default() - } - ); + assert_eq!(fee_result, expected_fee_result); + + if apply { + drive + .grove + .commit_transaction(db_transaction) + .unwrap() + .expect("expected to be able to commit a transaction"); + + let identity_keys = drive + .fetch_all_identity_keys(identity.id().to_buffer(), None, platform_version) + .expect("expected to get keys"); + assert_eq!(identity_keys.len(), 6); // we had 5 keys and we added 1 + } else { + // Not applying -> no commit. We can check root hash if we want + let app_hash_after = drive + .grove + .root_hash(None, &platform_version.drive.grove_version) + .unwrap() + .expect("should return app hash"); + // Or any other logic to ensure no state changes actually took effect + let _ = app_hash_after; + } + } - drive - .grove - .commit_transaction(db_transaction) - .unwrap() - .expect("expected to be able to commit a transaction"); + // ------------------------------------------------- + // check_reference_below_tokens_cost (4 tests) + // ------------------------------------------------- + #[test] + fn check_reference_below_tokens_cost_first_version_apply() { + let platform_version = PlatformVersion::first(); + let expected_fee_result = FeeResult { + storage_fee: 9423000, + processing_fee: 406100, + ..Default::default() + }; + do_check_reference_below_tokens_cost(true, platform_version, expected_fee_result); + } - let identity_keys = drive - .fetch_all_identity_keys(identity.id().to_buffer(), None, platform_version) - .expect("expected to get balance"); + #[test] + fn check_reference_below_tokens_cost_first_version_estimated() { + let platform_version = PlatformVersion::first(); + let expected_fee_result = FeeResult { + storage_fee: 9423000, + processing_fee: 314560, + ..Default::default() + }; + do_check_reference_below_tokens_cost(false, platform_version, expected_fee_result); + } - assert_eq!(identity_keys.len(), 6); // we had 5 keys and we added 1 + #[test] + fn check_reference_below_tokens_cost_latest_version_apply() { + let platform_version = PlatformVersion::latest(); + let expected_fee_result = FeeResult { + storage_fee: 9423000, + processing_fee: 406100, + ..Default::default() + }; + do_check_reference_below_tokens_cost(true, platform_version, expected_fee_result); } #[test] - fn should_add_two_dozen_new_keys_to_identity() { - let drive = setup_drive_with_initial_state_structure(None); + fn check_reference_below_tokens_cost_latest_version_estimated() { + let platform_version = PlatformVersion::latest(); + let expected_fee_result = FeeResult { + storage_fee: 9423000, + processing_fee: 314560, + ..Default::default() + }; + do_check_reference_below_tokens_cost(false, platform_version, expected_fee_result); + } - let platform_version = PlatformVersion::first(); + fn do_check_reference_below_tokens_cost( + apply: bool, + platform_version: &PlatformVersion, + expected_fee_result: FeeResult, + ) { + let drive = setup_drive_with_initial_state_structure(None); let identity = Identity::random_identity(5, Some(12345), platform_version) .expect("expected a random identity"); @@ -95,97 +205,186 @@ mod tests { identity.clone(), false, &block, - true, + apply, None, platform_version, ) .expect("expected to insert identity"); - let new_keys_to_add = - IdentityPublicKey::random_authentication_keys(5, 24, Some(15), platform_version); + IdentityPublicKey::random_authentication_keys(5, 1, Some(15), platform_version); + + let mut rng = StdRng::seed_from_u64(23450); let db_transaction = drive.grove.start_transaction(); - let fee_result = drive - .add_new_unique_keys_to_identity( + let batch_operations = drive + .insert_non_unique_public_key_hash_reference_to_identity_operations( identity.id().to_buffer(), - new_keys_to_add, - &block, - true, + rng.gen(), + &mut None, Some(&db_transaction), - platform_version, + &platform_version.drive, ) .expect("expected to update identity with new keys"); - assert_eq!( - fee_result, - FeeResult { - storage_fee: 347382000, - processing_fee: 6819220, - ..Default::default() - } - ); - + let mut drive_operations: Vec = vec![]; drive - .grove - .commit_transaction(db_transaction) - .unwrap() - .expect("expected to be able to commit a transaction"); + .apply_batch_low_level_drive_operations( + None, + Some(&db_transaction), + batch_operations, + &mut drive_operations, + &platform_version.drive, + ) + .expect("expected to apply operations"); + + let fee_result = Drive::calculate_fee( + None, + Some(drive_operations), + &Epoch::new(0).unwrap(), + drive.config.epochs_per_era, + platform_version, + None, + ) + .expect("expected fee result"); + + assert_eq!(fee_result, expected_fee_result); + + if apply { + drive + .grove + .commit_transaction(db_transaction) + .unwrap() + .expect("expected to be able to commit a transaction"); + } else { + // Not applying -> no commit + } + } - let identity_keys = drive - .fetch_all_identity_keys(identity.id().to_buffer(), None, platform_version) - .expect("expected to get balance"); + // ------------------------------------------------- + // should_add_two_dozen_new_keys_to_identity (4 tests) + // ------------------------------------------------- + #[test] + fn should_add_two_dozen_new_keys_to_identity_first_version_apply() { + let platform_version = PlatformVersion::first(); + let expected_fee_result = FeeResult { + storage_fee: 347382000, + processing_fee: 6819220, + ..Default::default() + }; + + do_should_add_two_dozen_new_keys_to_identity( + true, + platform_version, + expected_fee_result, + ); + } - assert_eq!(identity_keys.len(), 29); // we had 5 keys and we added 24 + #[test] + fn should_add_two_dozen_new_keys_to_identity_first_version_estimated() { + let platform_version = PlatformVersion::first(); + // Possibly different processing fee if "estimated" differs + let expected_fee_result = FeeResult { + storage_fee: 356211000, + processing_fee: 11699520, + ..Default::default() + }; + + do_should_add_two_dozen_new_keys_to_identity( + false, + platform_version, + expected_fee_result, + ); } #[test] - fn should_estimated_costs_without_state() { - let drive = setup_drive_with_initial_state_structure(None); + fn should_add_two_dozen_new_keys_to_identity_latest_version_apply() { + let platform_version = PlatformVersion::latest(); + let expected_fee_result = FeeResult { + storage_fee: 347382000, + processing_fee: 6819220, + ..Default::default() + }; + + do_should_add_two_dozen_new_keys_to_identity( + true, + platform_version, + expected_fee_result, + ); + } - let platform_version = PlatformVersion::first(); + #[test] + fn should_add_two_dozen_new_keys_to_identity_latest_version_estimated() { + let platform_version = PlatformVersion::latest(); + let expected_fee_result = FeeResult { + storage_fee: 356211000, + processing_fee: 11699520, + ..Default::default() + }; + + do_should_add_two_dozen_new_keys_to_identity( + false, + platform_version, + expected_fee_result, + ); + } + + fn do_should_add_two_dozen_new_keys_to_identity( + apply: bool, + platform_version: &PlatformVersion, + expected_fee_result: FeeResult, + ) { + let drive = setup_drive_with_initial_state_structure(None); let identity = Identity::random_identity(5, Some(12345), platform_version) .expect("expected a random identity"); let block = BlockInfo::default_with_epoch(Epoch::new(0).unwrap()); + drive + .add_new_identity( + identity.clone(), + false, + &block, + apply, + None, + platform_version, + ) + .expect("expected to insert identity"); + let new_keys_to_add = - IdentityPublicKey::random_authentication_keys(5, 1, Some(15), platform_version); + IdentityPublicKey::random_authentication_keys(5, 24, Some(15), platform_version); - let app_hash_before = drive - .grove - .root_hash(None, &platform_version.drive.grove_version) - .unwrap() - .expect("should return app hash"); + let db_transaction = drive.grove.start_transaction(); let fee_result = drive .add_new_unique_keys_to_identity( identity.id().to_buffer(), new_keys_to_add, &block, - false, - None, + apply, + Some(&db_transaction), platform_version, ) .expect("expected to update identity with new keys"); - let app_hash_after = drive - .grove - .root_hash(None, &platform_version.drive.grove_version) - .unwrap() - .expect("should return app hash"); + assert_eq!(fee_result, expected_fee_result); - assert_eq!(app_hash_after, app_hash_before); + if apply { + drive + .grove + .commit_transaction(db_transaction) + .unwrap() + .expect("expected to be able to commit a transaction"); - assert_eq!( - fee_result, - FeeResult { - storage_fee: 17145000, - processing_fee: 5483620, - ..Default::default() - } - ); + let identity_keys = drive + .fetch_all_identity_keys(identity.id().to_buffer(), None, platform_version) + .expect("expected to get balance"); + + assert_eq!(identity_keys.len(), 29); // we had 5 keys, added 24 + } else { + // Not applying => no commit + } } } @@ -198,11 +397,59 @@ mod tests { use dpp::identity::identity_public_key::accessors::v0::IdentityPublicKeyGettersV0; use dpp::version::PlatformVersion; + // ------------------------------------------------- + // should_disable_a_few_keys (4 tests) + // ------------------------------------------------- #[test] - fn should_disable_a_few_keys() { - let drive = setup_drive_with_initial_state_structure(None); + fn should_disable_a_few_keys_first_version_apply() { + let platform_version = PlatformVersion::first(); + let expected_fee_result = FeeResult { + storage_fee: 513000, + processing_fee: 869380, + ..Default::default() + }; + do_should_disable_a_few_keys(true, platform_version, expected_fee_result); + } + #[test] + fn should_disable_a_few_keys_first_version_estimated() { let platform_version = PlatformVersion::first(); + let expected_fee_result = FeeResult { + storage_fee: 486000, + processing_fee: 3216860, + ..Default::default() + }; + do_should_disable_a_few_keys(false, platform_version, expected_fee_result); + } + + #[test] + fn should_disable_a_few_keys_latest_version_apply() { + let platform_version = PlatformVersion::latest(); + let expected_fee_result = FeeResult { + storage_fee: 513000, + processing_fee: 869380, + ..Default::default() + }; + do_should_disable_a_few_keys(true, platform_version, expected_fee_result); + } + + #[test] + fn should_disable_a_few_keys_latest_version_estimated() { + let platform_version = PlatformVersion::latest(); + let expected_fee_result = FeeResult { + storage_fee: 486000, + processing_fee: 3216860, + ..Default::default() + }; + do_should_disable_a_few_keys(false, platform_version, expected_fee_result); + } + + fn do_should_disable_a_few_keys( + apply: bool, + platform_version: &PlatformVersion, + expected_fee_result: FeeResult, + ) { + let drive = setup_drive_with_initial_state_structure(None); let identity = Identity::random_identity(5, Some(12345), platform_version) .expect("expected a random identity"); @@ -245,92 +492,78 @@ mod tests { key_ids, disable_at, &block_info, - true, + apply, Some(&db_transaction), platform_version, ) .expect("should disable a few keys"); - assert_eq!( - fee_result, - FeeResult { - storage_fee: 513000, - processing_fee: 869380, - ..Default::default() - } - ); - - drive - .grove - .commit_transaction(db_transaction) - .unwrap() - .expect("expected to be able to commit a transaction"); + assert_eq!(fee_result, expected_fee_result); - let identity_keys = drive - .fetch_all_identity_keys(identity.id().to_buffer(), None, platform_version) - .expect("expected to get balance"); + if apply { + drive + .grove + .commit_transaction(db_transaction) + .unwrap() + .expect("expected to commit transaction"); - assert_eq!(identity_keys.len(), 7); // we had 5 keys and we added 2 + let identity_keys = drive + .fetch_all_identity_keys(identity.id().to_buffer(), None, platform_version) + .expect("expected to get balance"); - for (_, key) in identity_keys.into_iter().skip(5) { - assert_eq!(key.disabled_at(), Some(disable_at)); + assert_eq!(identity_keys.len(), 7); // we had 5 keys and we added 2 + for (_, key) in identity_keys.into_iter().skip(5) { + assert_eq!(key.disabled_at(), Some(disable_at)); + } } } #[test] - fn should_estimated_costs_without_state() { - let drive = setup_drive_with_initial_state_structure(None); - + fn estimated_costs_should_have_same_storage_cost_first_version() { let platform_version = PlatformVersion::first(); - - let identity = Identity::random_identity(5, Some(12345), platform_version) - .expect("expected a random identity"); - - let block_info = BlockInfo::default_with_epoch(Epoch::new(0).unwrap()); - - let disable_at = Utc::now().timestamp_millis() as TimestampMillis; - - let app_hash_before = drive - .grove - .root_hash(None, &platform_version.drive.grove_version) - .unwrap() - .expect("should return app hash"); - - let fee_result = drive - .disable_identity_keys( - identity.id().to_buffer(), - vec![0, 1], - disable_at, - &block_info, - false, - None, - platform_version, - ) - .expect("should estimate the disabling of a few keys"); - - let app_hash_after = drive - .grove - .root_hash(None, &platform_version.drive.grove_version) - .unwrap() - .expect("should return app hash"); - - assert_eq!(app_hash_after, app_hash_before); - - assert_eq!( - fee_result, - FeeResult { - storage_fee: 486000, - processing_fee: 3216860, - ..Default::default() - } + let expected_estimated_fee_result = FeeResult { + storage_fee: 486000, + processing_fee: 3216860, + ..Default::default() + }; + let expected_fee_result = FeeResult { + storage_fee: 486000, + processing_fee: 794720, + ..Default::default() + }; + estimated_costs_should_have_same_storage_cost( + platform_version, + expected_estimated_fee_result, + expected_fee_result, ); } #[test] - fn estimated_costs_should_have_same_storage_cost() { - let drive = setup_drive_with_initial_state_structure(None); + fn estimated_costs_should_have_same_storage_cost_latest_version() { + let platform_version = PlatformVersion::latest(); + let expected_estimated_fee_result = FeeResult { + storage_fee: 486000, + processing_fee: 3216860, + ..Default::default() + }; + let expected_fee_result = FeeResult { + storage_fee: 486000, + processing_fee: 794720, + ..Default::default() + }; + estimated_costs_should_have_same_storage_cost( + platform_version, + expected_estimated_fee_result, + expected_fee_result, + ); + } - let platform_version = PlatformVersion::first(); + fn estimated_costs_should_have_same_storage_cost( + platform_version: &PlatformVersion, + expected_estimated_fee_result: FeeResult, + expected_fee_result: FeeResult, + ) { + let drive = setup_drive_with_initial_state_structure(None); let identity = Identity::random_identity(5, Some(12345), platform_version) .expect("expected a random identity"); @@ -350,7 +583,7 @@ mod tests { let disable_at = Utc::now().timestamp_millis() as TimestampMillis; - let expected_fee_result = drive + let estimated_fee_result = drive .disable_identity_keys( identity.id().to_buffer(), vec![0, 1], @@ -374,7 +607,9 @@ mod tests { ) .expect("should get the cost of the disabling a few keys"); - assert_eq!(expected_fee_result.storage_fee, fee_result.storage_fee); + assert_eq!(estimated_fee_result.storage_fee, fee_result.storage_fee); + assert_eq!(estimated_fee_result, expected_estimated_fee_result); + assert_eq!(fee_result, expected_fee_result); } } @@ -385,27 +620,87 @@ mod tests { use dpp::fee::fee_result::FeeResult; use dpp::version::PlatformVersion; + // ------------------------------------------------- + // should_update_revision (4 tests) + // ------------------------------------------------- #[test] - fn should_update_revision() { - let drive = setup_drive_with_initial_state_structure(None); + fn should_update_revision_first_version_apply() { + let platform_version = PlatformVersion::first(); + let expected_fee_result = FeeResult { + storage_fee: 0, + processing_fee: 238820, + removed_bytes_from_system: 0, + ..Default::default() + }; + + do_should_update_revision(true, platform_version, expected_fee_result); + } + #[test] + fn should_update_revision_first_version_estimated() { let platform_version = PlatformVersion::first(); + // Possibly different if your scenario's estimated cost differs + let expected_fee_result = FeeResult { + storage_fee: 0, + processing_fee: 1813560, + removed_bytes_from_system: 0, + ..Default::default() + }; + + do_should_update_revision(false, platform_version, expected_fee_result); + } + + #[test] + fn should_update_revision_latest_version_apply() { + let platform_version = PlatformVersion::latest(); + let expected_fee_result = FeeResult { + storage_fee: 0, + processing_fee: 238820, + removed_bytes_from_system: 0, + ..Default::default() + }; + + do_should_update_revision(true, platform_version, expected_fee_result); + } + + #[test] + fn should_update_revision_latest_version_estimated() { + let platform_version = PlatformVersion::latest(); + // Possibly different if your scenario's estimated cost differs + let expected_fee_result = FeeResult { + storage_fee: 0, + processing_fee: 1813560, + removed_bytes_from_system: 0, + ..Default::default() + }; + + do_should_update_revision(false, platform_version, expected_fee_result); + } + + fn do_should_update_revision( + apply: bool, + platform_version: &PlatformVersion, + expected_fee_result: FeeResult, + ) { + let drive = setup_drive_with_initial_state_structure(None); let identity = Identity::random_identity(5, Some(12345), platform_version) .expect("expected a random identity"); let block_info = BlockInfo::default_with_epoch(Epoch::new(0).unwrap()); + // Insert identity drive .add_new_identity( identity.clone(), false, &block_info, - true, + apply, None, platform_version, ) .expect("expected to insert identity"); + // We'll update the revision from 0 -> 2 let revision = 2; let db_transaction = drive.grove.start_transaction(); @@ -415,84 +710,35 @@ mod tests { identity.id().to_buffer(), revision, &block_info, - true, + apply, Some(&db_transaction), platform_version, None, ) .expect("should update revision"); - assert_eq!( - fee_result, - FeeResult { - storage_fee: 0, - processing_fee: 238820, - removed_bytes_from_system: 0, - ..Default::default() - } - ); - - drive - .grove - .commit_transaction(db_transaction) - .unwrap() - .expect("expected to be able to commit a transaction"); - - let updated_revision = drive - .fetch_identity_revision(identity.id().to_buffer(), true, None, platform_version) - .expect("expected to get revision"); - - assert_eq!(updated_revision, Some(revision)); - } - - #[test] - fn should_estimated_costs_without_state() { - let drive = setup_drive_with_initial_state_structure(None); - - let platform_version = PlatformVersion::first(); - - let identity = Identity::random_identity(5, Some(12345), platform_version) - .expect("expected a random identity"); - - let block_info = BlockInfo::default_with_epoch(Epoch::new(0).unwrap()); - - let revision = 2; - - let app_hash_before = drive - .grove - .root_hash(None, &platform_version.drive.grove_version) - .unwrap() - .expect("should return app hash"); - - let fee_result = drive - .update_identity_revision( - identity.id().to_buffer(), - revision, - &block_info, - false, - None, - platform_version, - None, - ) - .expect("should estimate the revision update"); - - let app_hash_after = drive - .grove - .root_hash(None, &platform_version.drive.grove_version) - .unwrap() - .expect("should return app hash"); - - assert_eq!(app_hash_after, app_hash_before); - - assert_eq!( - fee_result, - FeeResult { - storage_fee: 0, - processing_fee: 1813560, - removed_bytes_from_system: 0, - ..Default::default() - } - ); + assert_eq!(fee_result, expected_fee_result); + + if apply { + drive + .grove + .commit_transaction(db_transaction) + .unwrap() + .expect("expected to be able to commit a transaction"); + + // check new revision + let updated_revision = drive + .fetch_identity_revision( + identity.id().to_buffer(), + true, + None, + platform_version, + ) + .expect("expected to get revision"); + assert_eq!(updated_revision, Some(revision)); + } else { + // No commit => no changes + } } } }