Skip to content
Snippets Groups Projects

Fix identities live tests

Merged Hugo Trentesaux requested to merge hugo-live-test into master
1 unresolved thread
Files
2
@@ -23,7 +23,7 @@ use sp_core::{blake2_128, ByteArray, H256};
use std::collections::HashMap;
use subxt::config::SubstrateConfig as GdevConfig;
const DEFAULT_ENDPOINT: &str = "wss://gdev.librelois.fr:443/ws";
const DEFAULT_ENDPOINT: &str = "ws://localhost:9944";
const TREASURY_ACCOUNT_ID: [u8; 32] =
hex!("6d6f646c70792f74727372790000000000000000000000000000000000000000");
@@ -90,7 +90,7 @@ async fn sanity_tests_at(client: Client, maybe_block_hash: Option<H256>) -> anyh
account_id_bytes.copy_from_slice(&key.0[48..]);
accounts.insert(AccountId32::new(account_id_bytes), account_info);
}
println!("accounts: {}.", accounts.len());
println!("accounts.len(): {}.", accounts.len());
// Collect identities
let mut identities = HashMap::new();
@@ -107,7 +107,7 @@ async fn sanity_tests_at(client: Client, maybe_block_hash: Option<H256>) -> anyh
idty_index_bytes.copy_from_slice(&key.0[40..]);
identities.insert(IdtyIndex::from_le_bytes(idty_index_bytes), idty_value);
}
println!("identities: {}.", identities.len());
println!("identities.len(): {}.", identities.len());
// Collect identity_index_of
let mut identity_index_of = HashMap::new();
@@ -124,7 +124,7 @@ async fn sanity_tests_at(client: Client, maybe_block_hash: Option<H256>) -> anyh
blake2_128_bytes.copy_from_slice(&key.0[32..]);
identity_index_of.insert(blake2_128_bytes, idty_index);
}
println!("identity_index_of: {}.", identities.len());
println!("identity_index_of.len(): {}.", identity_index_of.len());
let storage = Storage {
accounts,
@@ -149,12 +149,17 @@ mod verifier {
Self { errors: Vec::new() }
}
// FIXME why async functions when called with await?
/// method to run all storage tests
pub(super) async fn verify_storage(&mut self, storage: &Storage) -> anyhow::Result<()> {
self.verify_accounts(&storage.accounts).await;
self.verify_identities(&storage.accounts, &storage.identities)
.await;
self.verify_identity_index_of(&storage.identities, &storage.identity_index_of)
.await;
self.verify_identity_coherence(&storage.identities, &storage.identity_index_of)
.await;
if self.errors.is_empty() {
Ok(())
@@ -169,12 +174,14 @@ mod verifier {
}
}
/// assert method to collect errors
fn assert(&mut self, assertion: bool, error: String) {
if !assertion {
self.errors.push(error);
}
}
/// check accounts sufficients and consumers (specific to duniter-account pallet)
async fn verify_accounts(&mut self, accounts: &HashMap<AccountId32, AccountInfo>) {
for (account_id, account_info) in accounts {
if account_info.sufficients == 0 {
@@ -193,9 +200,9 @@ mod verifier {
);
}
// Rule 3: If the account have consumers, it shoul have at least one provider
// Rule 3: If the account have consumers, it should have at least one provider
if account_info.consumers > 0 {
// Rule 1: If the account is not s
// Rule 1: If the account is not sufficient [...]
self.assert(
account_info.providers > 0,
format!("Account {} has no providers nor sufficients.", account_id),
@@ -213,6 +220,7 @@ mod verifier {
}
}
/// check list of identities (account existence, sufficient)
async fn verify_identities(
&mut self,
accounts: &HashMap<AccountId32, AccountInfo>,
@@ -239,7 +247,7 @@ mod verifier {
match idty_value.status {
IdtyStatus::Validated => {
// Rule 3: If the identity is validated, removable_on shoud be zero
// Rule 3: If the identity is validated, removable_on should be zero
self.assert(
idty_value.removable_on == 0,
format!(
@@ -249,7 +257,7 @@ mod verifier {
);
}
_ => {
// Rule 4: If the identity is not validated, next_creatable_identity_on shoud be zero
// Rule 4: If the identity is not validated, next_creatable_identity_on should be zero
self.assert(
idty_value.next_creatable_identity_on == 0,
format!("Identity {} is corrupted: next_creatable_identity_on > 0 on non-validated idty",
@@ -260,6 +268,7 @@ mod verifier {
}
}
/// check the identity hashmap (length, identity existence, hash matches owner key)
async fn verify_identity_index_of(
&mut self,
identities: &HashMap<IdtyIndex, IdtyValue>,
@@ -268,7 +277,11 @@ mod verifier {
// Rule1: identity_index_of should have the same lenght as identities
self.assert(
identities.len() == identity_index_of.len(),
"identities.len() != identity_index_of.len().".to_owned(),
format!(
"identities.len({}) != identity_index_of.len({}).",
identities.len(),
identity_index_of.len()
),
);
for (blake2_128_owner_key, idty_index) in identity_index_of {
@@ -296,5 +309,32 @@ mod verifier {
}
}
}
/// check coherence between identity list and identity index hashmap
async fn verify_identity_coherence(
&mut self,
identities: &HashMap<IdtyIndex, IdtyValue>,
identity_index_of: &HashMap<[u8; 16], IdtyIndex>,
) {
// each identity should be correcly referenced in the hashmap
for (idty_index, idty_value) in identities {
// hash owner key to get key
let blake2_128_owner_key = &blake2_128(idty_value.owner_key.as_slice());
// get identity index from hashmap
if let Some(index_of) = identity_index_of.get(blake2_128_owner_key) {
self.assert(idty_index == index_of,
format!("identity number {idty_index} with owner key {0} is mapped to identity index {index_of}", idty_value.owner_key));
} else {
self.assert(
false,
format!(
"identity with owner key {} is not present in hashmap",
idty_value.owner_key
),
);
}
}
}
}
}
Loading