Skip to content

Commit

Permalink
fix(platform)!: fix reference of items between epochs (#2064)
Browse files Browse the repository at this point in the history
Co-authored-by: Odysseas Gabrielides <[email protected]>
  • Loading branch information
QuantumExplorer and ogabrielides authored Aug 20, 2024
1 parent 00f0b6a commit d0fe6dd
Show file tree
Hide file tree
Showing 35 changed files with 2,286 additions and 469 deletions.
317 changes: 181 additions & 136 deletions Cargo.lock

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -113,21 +113,26 @@ impl StateTransitionStateValidationV0 for IdentityUpdateTransition {
#[cfg(test)]
mod tests {
use crate::config::{PlatformConfig, PlatformTestConfig};
use crate::execution::validation::state_transition::tests::setup_identity_return_master_key;
use crate::execution::validation::state_transition::tests::{
setup_add_key_to_identity, setup_identity_return_master_key,
};
use crate::test::helpers::setup::TestPlatformBuilder;
use dpp::block::block_info::BlockInfo;
use dpp::dash_to_credits;
use dpp::data_contract::accessors::v0::DataContractV0Getters;
use dpp::identity::accessors::IdentityGettersV0;
use dpp::identity::contract_bounds::ContractBounds;
use dpp::identity::identity_public_key::accessors::v0::IdentityPublicKeyGettersV0;
use dpp::identity::signer::Signer;
use dpp::identity::{KeyType, Purpose, SecurityLevel};
use dpp::serialization::{PlatformSerializable, Signable};
use dpp::state_transition::identity_update_transition::v0::IdentityUpdateTransitionV0;
use dpp::state_transition::identity_update_transition::IdentityUpdateTransition;
use dpp::state_transition::StateTransition;
use platform_version::version::PlatformVersion;

#[test]
fn test_identity_update_that_disables_a_key() {
fn test_identity_update_that_disables_an_authentication_key() {
let platform_config = PlatformConfig {
testing_configs: PlatformTestConfig {
disable_instant_lock_signature_verification: true,
Expand Down Expand Up @@ -202,9 +207,134 @@ mod tests {
let issues = platform
.drive
.grove
.visualize_verify_grovedb(true, &platform_version.drive.grove_version)
.visualize_verify_grovedb(None, true, false, &platform_version.drive.grove_version)
.expect("expected to have no issues");

assert_eq!(issues.len(), 0);
}

#[test]
fn test_identity_update_that_disables_an_encryption_key() {
let platform_config = PlatformConfig {
testing_configs: PlatformTestConfig {
disable_instant_lock_signature_verification: true,
..Default::default()
},
..Default::default()
};

let platform_version = PlatformVersion::latest();

let mut platform = TestPlatformBuilder::new()
.with_config(platform_config)
.build_with_mock_rpc()
.set_genesis_state();

let (mut identity, mut signer, master_key) =
setup_identity_return_master_key(&mut platform, 958, dash_to_credits!(0.1));

let dashpay = platform.drive.cache.system_data_contracts.load_dashpay();

let key = setup_add_key_to_identity(
&mut platform,
&mut identity,
&mut signer,
4,
2,
Purpose::ENCRYPTION,
SecurityLevel::MEDIUM,
KeyType::ECDSA_SECP256K1,
Some(ContractBounds::SingleContractDocumentType {
id: dashpay.id(),
document_type_name: "contactRequest".to_string(),
}),
);

let issues = platform
.drive
.grove
.visualize_verify_grovedb(None, true, false, &platform_version.drive.grove_version)
.expect("expected to have no issues");

assert_eq!(
issues.len(),
0,
"issues are {}",
issues
.iter()
.map(|(hash, (a, b, c))| format!("{}: {} {} {}", hash, a, b, c))
.collect::<Vec<_>>()
.join(" | ")
);

let platform_state = platform.state.load();

let update_transition: IdentityUpdateTransition = IdentityUpdateTransitionV0 {
identity_id: identity.id(),
revision: 1,
nonce: 1,
add_public_keys: vec![],
disable_public_keys: vec![key.id()],
user_fee_increase: 0,
signature_public_key_id: master_key.id(),
signature: Default::default(),
}
.into();

let mut update_transition: StateTransition = update_transition.into();

let data = update_transition
.signable_bytes()
.expect("expected signable bytes");
update_transition.set_signature(
signer
.sign(&master_key, data.as_slice())
.expect("expected to sign"),
);

let update_transition_bytes = update_transition
.serialize_to_bytes()
.expect("expected to serialize");

let transaction = platform.drive.grove.start_transaction();

let processing_result = platform
.platform
.process_raw_state_transitions(
&vec![update_transition_bytes.clone()],
&platform_state,
&BlockInfo::default(),
&transaction,
platform_version,
true,
None,
)
.expect("expected to process state transition");

assert_eq!(processing_result.valid_count(), 1);

platform
.drive
.grove
.commit_transaction(transaction)
.unwrap()
.expect("expected to commit");

let issues = platform
.drive
.grove
.visualize_verify_grovedb(None, true, false, &platform_version.drive.grove_version)
.expect("expected to have no issues");

assert_eq!(
issues.len(),
0,
"issues are {}",
issues
.iter()
.map(|(hash, (a, b, c))| format!("{}: {} {} {}", hash, a, b, c))
.collect::<Vec<_>>()
.join(" | ")
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub(crate) mod tests {
use crate::test::helpers::setup::TempPlatform;
use dpp::block::block_info::BlockInfo;
use dpp::fee::Credits;
use dpp::identity::{Identity, IdentityPublicKey, IdentityV0, KeyType, Purpose, SecurityLevel};
use dpp::identity::{Identity, IdentityPublicKey, IdentityV0, KeyID, KeyType, Purpose, SecurityLevel};
use dpp::prelude::{Identifier, IdentityNonce};
use platform_version::version::PlatformVersion;
use rand::prelude::StdRng;
Expand Down Expand Up @@ -86,6 +86,7 @@ pub(crate) mod tests {
use dpp::fee::fee_result::FeeResult;
use dpp::identifier::MasternodeIdentifiers;
use dpp::identity::accessors::IdentityGettersV0;
use dpp::identity::contract_bounds::ContractBounds;
use dpp::identity::hash::IdentityPublicKeyHashMethodsV0;
use dpp::platform_value::{Bytes32, Value};
use dpp::serialization::PlatformSerializable;
Expand Down Expand Up @@ -253,6 +254,51 @@ pub(crate) mod tests {
(identity, signer, master_key)
}

pub(crate) fn setup_add_key_to_identity(
platform: &mut TempPlatform<MockCoreRPCLike>,
identity: &mut Identity,
signer: &mut SimpleSigner,
seed: u64,
key_id: KeyID,
purpose: Purpose,
security_level: SecurityLevel,
key_type: KeyType,
contract_bounds: Option<ContractBounds>,
) -> IdentityPublicKey {
let platform_version = PlatformVersion::latest();

let mut rng = StdRng::seed_from_u64(seed);

let (key, private_key) = IdentityPublicKey::random_key_with_known_attributes(
key_id,
&mut rng,
purpose,
security_level,
key_type,
contract_bounds,
platform_version,
)
.expect("expected to get key pair");

signer.add_key(key.clone(), private_key.clone());

identity.add_public_key(key.clone());

platform
.drive
.add_new_unique_keys_to_identity(
identity.id().to_buffer(),
vec![key.clone()],
&BlockInfo::default(),
true,
None,
platform_version,
)
.expect("expected to add a new key");

key
}

pub(crate) fn setup_identity_with_withdrawal_key_and_system_credits(
platform: &mut TempPlatform<MockCoreRPCLike>,
seed: u64,
Expand Down
17 changes: 12 additions & 5 deletions packages/rs-drive-abci/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,12 @@ fn verify_grovedb(db_path: &PathBuf, force: bool) -> Result<(), String> {
let grovedb = drive::grovedb::GroveDb::open(db_path).expect("open grovedb");
//todo: get platform version instead of taking latest
let result = grovedb
.visualize_verify_grovedb(true, &PlatformVersion::latest().drive.grove_version)
.visualize_verify_grovedb(
None,
true,
true,
&PlatformVersion::latest().drive.grove_version,
)
.map_err(|e| e.to_string());

match result {
Expand Down Expand Up @@ -481,10 +486,9 @@ mod test {
let cf_handle = db.cf_handle(cf).unwrap();
let iter = db.iterator_cf(cf_handle, IteratorMode::Start);

// let iter = db.iterator(IteratorMode::Start);
for (i, item) in iter.enumerate() {
let (key, mut value) = item.unwrap();
// println!("{} = {}", hex::encode(&key), hex::encode(value));
// println!("{} = {}", hex::encode(&key), hex::encode(&value));
tracing::trace!(cf, key=?hex::encode(&key), value=hex::encode(&value),"found item in rocksdb");

if i == n {
Expand All @@ -509,8 +513,11 @@ mod test {

corrupt_rocksdb_item(&db_path, "roots", 0);

let result = super::verify_grovedb(&db_path, true);
assert!(result.is_err());
let result_error = super::verify_grovedb(&db_path, true).expect_err("expected an error");
assert_eq!(
result_error,
"data corruption error: expected merk to contain value at key 0x08 for tree"
);

println!("db path: {:?}", &db_path);
}
Expand Down
Loading

0 comments on commit d0fe6dd

Please sign in to comment.