Skip to content

Commit

Permalink
DID try-state and toolchain defaults -2
Browse files Browse the repository at this point in the history
  • Loading branch information
smohan-dw committed Dec 27, 2023
1 parent 8064fa4 commit eeec361
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 0 deletions.
91 changes: 91 additions & 0 deletions pallets/did/src/try_state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// This file is part of CORD – https://cord.network

// Copyright (C) 2019-2023 BOTLabs GmbH.
// Copyright (C) Dhiway Networks Pvt. Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later
// Adapted to meet the requirements of the CORD project.

// CORD is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// CORD is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with CORD. If not, see <https://www.gnu.org/licenses/>.

use cord_utilities::test_utils::log_and_return_error_message;
use frame_support::ensure;
use scale_info::prelude::format;
use sp_core::Get;
use sp_runtime::{SaturatedConversion, TryRuntimeError};

use crate::{
did_details::DidDetails, Config, Did, DidBlacklist, DidEndpointsCount, DidIdentifierOf,
ServiceEndpoints,
};

pub(crate) fn do_try_state<T: Config>() -> Result<(), TryRuntimeError> {
Did::<T>::iter().try_for_each(
|(did_subject, did_details): (DidIdentifierOf<T>, DidDetails<T>)| -> Result<(), TryRuntimeError> {
let service_endpoints_count = ServiceEndpoints::<T>::iter_prefix(&did_subject).count();

ensure!(
service_endpoints_count == DidEndpointsCount::<T>::get(&did_subject).saturated_into::<usize>(),
log_and_return_error_message(format!(
"Did {:?} has not matching service endpoints. In [ServiceEndpoints]: {:?} in [DidEndpointsCount]: {:?}",
did_subject,
service_endpoints_count,
DidEndpointsCount::<T>::get(&did_subject)
))
);

ensure!(
did_details.key_agreement_keys.len()
<= (<T as Config>::MaxTotalKeyAgreementKeys::get()).saturated_into::<usize>(),
log_and_return_error_message(format!(
"Did {:?} has to many key agreement keys. Allowed: {:?} found: {:?}",
did_subject,
<T as Config>::MaxTotalKeyAgreementKeys::get(),
did_details.key_agreement_keys.len()
))
);

ensure!(
service_endpoints_count <= <T as Config>::MaxNumberOfServicesPerDid::get().saturated_into::<usize>(),
log_and_return_error_message(format!(
"Did {:?} has to many service endpoints. Allowed: {:?} found: {:?}",
did_subject,
<T as Config>::MaxNumberOfServicesPerDid::get(),
service_endpoints_count
))
);

ensure!(
!DidBlacklist::<T>::contains_key(&did_subject),
log_and_return_error_message(format!("Did {:?} is blacklisted.", did_subject))
);

Ok(())
},
)?;

DidBlacklist::<T>::iter_keys().try_for_each(
|deleted_did_subject| -> Result<(), TryRuntimeError> {
let service_endpoints_count =
ServiceEndpoints::<T>::iter_prefix(&deleted_did_subject).count();
ensure!(
service_endpoints_count == 0,
log_and_return_error_message(format!(
"Blacklisted did {:?} has service endpoints.",
deleted_did_subject,
))
);
Ok(())
},
)
}
13 changes: 13 additions & 0 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[toolchain]
channel = "stable"
components = [
"cargo",
"clippy",
"rust-analyzer",
"rust-src",
"rust-std",
"rustc",
"rustfmt",
]
targets = ["wasm32-unknown-unknown"]
profile = "minimal"
28 changes: 28 additions & 0 deletions utilities/src/test_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// This file is part of CORD – https://cord.network

// Copyright (C) 2019-2023 BOTLabs GmbH.
// Copyright (C) Dhiway Networks Pvt. Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later
// Adapted to meet the requirements of the CORD project.

// CORD is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// CORD is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with CORD. If not, see <https://www.gnu.org/licenses/>.

use scale_info::prelude::string::String;
use sp_runtime::TryRuntimeError;

/// Logs the error message and returns "Sanity test error"
pub fn log_and_return_error_message(error_message: String) -> TryRuntimeError {
log::error!("{}", error_message);
TryRuntimeError::Other("Test")
}

0 comments on commit eeec361

Please sign in to comment.