-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DID try-state and toolchain defaults -2
- Loading branch information
Showing
3 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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(()) | ||
}, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[toolchain] | ||
channel = "stable" | ||
components = [ | ||
"cargo", | ||
"clippy", | ||
"rust-analyzer", | ||
"rust-src", | ||
"rust-std", | ||
"rustc", | ||
"rustfmt", | ||
] | ||
targets = ["wasm32-unknown-unknown"] | ||
profile = "minimal" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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") | ||
} |