Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add sanity tests #512

Merged
merged 44 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
c1906eb
first draft
Ad96el Apr 26, 2023
f392dfa
did finished
Ad96el Apr 26, 2023
b7bbd01
did finished
Ad96el Apr 26, 2023
b8c3f41
attestation ctype
Ad96el Apr 27, 2023
f6e4eee
delegation
Ad96el Apr 27, 2023
e676a1a
did-lookup
Ad96el Apr 27, 2023
3f721be
web3name
Ad96el Apr 27, 2023
fba90dd
public credentials
Ad96el Apr 28, 2023
6d65710
bug
Ad96el Apr 28, 2023
0b3c281
comments
Ad96el Apr 28, 2023
ca72f47
public credentials
Ad96el Apr 28, 2023
8e78c9e
fmt
Ad96el May 2, 2023
1ff6584
fix: do_try_state
Ad96el May 2, 2023
cd2291f
fix test
Ad96el May 2, 2023
866f701
clippy
Ad96el May 2, 2023
4a3ef47
first draft for staking
Ad96el May 2, 2023
2ecc686
first draft for staking
Ad96el May 3, 2023
b6538d2
staking pallet
Ad96el May 3, 2023
5507a09
staking pallet
Ad96el May 4, 2023
be07f97
fmt
Ad96el May 4, 2023
b29ef02
fmt
Ad96el May 4, 2023
fa15fad
Merge branch 'develop' into chore/sanity-tests
Ad96el May 4, 2023
0ba1364
fix wrong test
Ad96el May 4, 2023
0a5dd8c
clippy
Ad96el May 4, 2023
98b079e
Merge branch 'chore/sanity-tests' of github.com:KILTprotocol/kilt-nod…
Ad96el May 4, 2023
da193f8
refactor
Ad96el May 5, 2023
ecd05bc
fix test
Ad96el May 8, 2023
0ed0971
first fixes
Ad96el May 9, 2023
f61632b
refactor
Ad96el May 10, 2023
a646f74
3 test are failing
Ad96el May 10, 2023
06da8c5
refactor
Ad96el May 10, 2023
33ef40c
neutral lang
Ad96el May 11, 2023
c4b8d05
refactor files
Ad96el May 11, 2023
afd1755
refactor
Ad96el May 12, 2023
0719f78
merge develop into chore/sanity-tests
Ad96el May 12, 2023
0aa08f9
remove debugging flags
Ad96el May 12, 2023
dc5fe26
draft for error messages
Ad96el May 12, 2023
5e5ae53
fmt error messages
Ad96el May 15, 2023
5662cb0
refactor
Ad96el May 15, 2023
85cb911
fixed tests
Ad96el May 16, 2023
3d49caa
refactor error messages
Ad96el May 16, 2023
7c4cf6d
comments
Ad96el May 16, 2023
3daa939
comments
Ad96el May 16, 2023
3c13e2c
Merge branch 'develop' into chore/sanity-tests
Ad96el May 16, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,306 changes: 741 additions & 565 deletions Cargo.lock

Large diffs are not rendered by default.

24 changes: 23 additions & 1 deletion pallets/attestation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,13 @@ pub mod pallet {
pub struct Pallet<T>(_);

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
#[cfg(feature = "try-runtime")]
fn try_state(_n: BlockNumberFor<T>) -> Result<(), &'static str> {
Self::do_try_state()?;
Ok(())
}
}

/// Attestations stored on chain.
///
Expand Down Expand Up @@ -465,6 +471,22 @@ pub mod pallet {
ExternalAttestations::<T>::remove(authorization_id, claim_hash);
}
}

#[cfg(any(feature = "try-runtime", test))]
pub fn do_try_state() -> DispatchResult {
Attestations::<T>::iter().try_for_each(|(claim_hash, attestation_details)| -> DispatchResult {
if let Some(authorization_id) = attestation_details.authorization_id {
ensure!(
ExternalAttestations::<T>::contains_key(authorization_id, claim_hash),
DispatchError::Other("Test")
);
} else {
return Ok(());
}
Ok(())
})?;
Ok(())
}
}

struct AttestationStorageDepositCollector<T: Config>(PhantomData<T>);
Expand Down
9 changes: 9 additions & 0 deletions pallets/attestation/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ pub(crate) mod runtime {
use ctype::{CtypeCreatorOf, CtypeEntryOf};
use kilt_support::mock::{mock_origin, SubjectId};

use crate::Pallet;

use super::*;

type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
Expand Down Expand Up @@ -385,6 +387,13 @@ pub(crate) mod runtime {
ext
}

pub fn build_and_execute_with_sanity_tests(self, test: impl FnOnce()) {
self.build().execute_with(|| {
test();
Pallet::<Test>::do_try_state().unwrap();
})
}

#[cfg(feature = "runtime-benchmarks")]
pub fn build_with_keystore(self) -> sp_io::TestExternalities {
let mut ext = self.build();
Expand Down
72 changes: 24 additions & 48 deletions pallets/attestation/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ fn test_attest_without_authorization() {
ExtBuilder::default()
.with_ctypes(vec![(ctype_hash, attester.clone())])
.with_balances(vec![(ACCOUNT_00, <Test as Config>::Deposit::get() * 100)])
.build()
.execute_with(|| {
.build_and_execute_with_sanity_tests(|| {
assert_ok!(Attestation::add(
DoubleOrigin(ACCOUNT_00, attester.clone()).into(),
claim_hash,
Expand Down Expand Up @@ -72,8 +71,7 @@ fn test_attest_authorized() {
ExtBuilder::default()
.with_ctypes(vec![(ctype, attester.clone())])
.with_balances(vec![(ACCOUNT_00, <Test as Config>::Deposit::get() * 100)])
.build()
.execute_with(|| {
.build_and_execute_with_sanity_tests(|| {
assert_ok!(Attestation::add(
DoubleOrigin(ACCOUNT_00, attester.clone()).into(),
claim_hash,
Expand Down Expand Up @@ -105,8 +103,7 @@ fn test_attest_unauthorized() {
ExtBuilder::default()
.with_ctypes(vec![(ctype, attester.clone())])
.with_balances(vec![(ACCOUNT_00, <Test as Config>::Deposit::get() * 100)])
.build()
.execute_with(|| {
.build_and_execute_with_sanity_tests(|| {
assert_eq!(
Attestation::add(
DoubleOrigin(ACCOUNT_00, attester.clone()).into(),
Expand All @@ -127,8 +124,7 @@ fn test_attest_ctype_not_found() {

ExtBuilder::default()
.with_balances(vec![(ACCOUNT_00, <Test as Config>::Deposit::get() * 100)])
.build()
.execute_with(|| {
.build_and_execute_with_sanity_tests(|| {
assert_noop!(
Attestation::add(
DoubleOrigin(ACCOUNT_00, attester.clone()).into(),
Expand All @@ -151,8 +147,7 @@ fn test_attest_already_exists() {
.with_balances(vec![(ACCOUNT_00, <Test as Config>::Deposit::get() * 100)])
.with_ctypes(vec![(attestation.ctype_hash, attester.clone())])
.with_attestations(vec![(claim_hash, attestation.clone())])
.build()
.execute_with(|| {
.build_and_execute_with_sanity_tests(|| {
assert_noop!(
Attestation::add(
DoubleOrigin(ACCOUNT_00, attester.clone()).into(),
Expand All @@ -178,8 +173,7 @@ fn test_revoke_remove() {
.with_balances(vec![(ACCOUNT_00, <Test as Config>::Deposit::get() * 100)])
.with_ctypes(vec![(attestation.ctype_hash, revoker.clone())])
.with_attestations(vec![(claim_hash, attestation)])
.build()
.execute_with(|| {
.build_and_execute_with_sanity_tests(|| {
assert_ok!(Attestation::revoke(
DoubleOrigin(ACCOUNT_00, revoker.clone()).into(),
claim_hash,
Expand Down Expand Up @@ -214,8 +208,7 @@ fn test_authorized_revoke() {
.with_balances(vec![(ACCOUNT_00, <Test as Config>::Deposit::get() * 100)])
.with_ctypes(vec![(attestation.ctype_hash, attester)])
.with_attestations(vec![(claim_hash, attestation)])
.build()
.execute_with(|| {
.build_and_execute_with_sanity_tests(|| {
assert_ok!(Attestation::revoke(
DoubleOrigin(ACCOUNT_00, revoker.clone()).into(),
claim_hash,
Expand Down Expand Up @@ -245,8 +238,7 @@ fn test_unauthorized_revoke() {
.with_balances(vec![(ACCOUNT_00, <Test as Config>::Deposit::get() * 100)])
.with_ctypes(vec![(attestation.ctype_hash, attester)])
.with_attestations(vec![(claim_hash, attestation)])
.build()
.execute_with(|| {
.build_and_execute_with_sanity_tests(|| {
assert_noop!(
Attestation::revoke(DoubleOrigin(ACCOUNT_00, evil).into(), claim_hash, authorization_info),
DispatchError::Other("Unauthorized")
Expand All @@ -264,8 +256,7 @@ fn test_revoke_not_found() {
ExtBuilder::default()
.with_balances(vec![(ACCOUNT_00, <Test as Config>::Deposit::get() * 100)])
.with_ctypes(vec![(attestation.ctype_hash, revoker.clone())])
.build()
.execute_with(|| {
.build_and_execute_with_sanity_tests(|| {
assert_noop!(
Attestation::revoke(
DoubleOrigin(ACCOUNT_00, revoker.clone()).into(),
Expand All @@ -291,8 +282,7 @@ fn test_already_revoked() {
.with_balances(vec![(ACCOUNT_00, <Test as Config>::Deposit::get() * 100)])
.with_ctypes(vec![(attestation.ctype_hash, revoker.clone())])
.with_attestations(vec![(claim_hash, attestation)])
.build()
.execute_with(|| {
.build_and_execute_with_sanity_tests(|| {
assert_noop!(
Attestation::revoke(
DoubleOrigin(ACCOUNT_00, revoker.clone()).into(),
Expand All @@ -318,8 +308,7 @@ fn test_remove() {
.with_balances(vec![(ACCOUNT_00, <Test as Config>::Deposit::get() * 100)])
.with_ctypes(vec![(attestation.ctype_hash, attester.clone())])
.with_attestations(vec![(claim_hash, attestation)])
.build()
.execute_with(|| {
.build_and_execute_with_sanity_tests(|| {
assert_ok!(Attestation::remove(
DoubleOrigin(ACCOUNT_00, attester.clone()).into(),
claim_hash,
Expand All @@ -342,8 +331,7 @@ fn test_remove_authorized() {
.with_balances(vec![(ACCOUNT_00, <Test as Config>::Deposit::get() * 100)])
.with_ctypes(vec![(attestation.ctype_hash, revoker.clone())])
.with_attestations(vec![(claim_hash, attestation)])
.build()
.execute_with(|| {
.build_and_execute_with_sanity_tests(|| {
assert_ok!(Attestation::remove(
DoubleOrigin(ACCOUNT_00, revoker.clone()).into(),
claim_hash,
Expand All @@ -366,8 +354,7 @@ fn test_remove_unauthorized() {
.with_balances(vec![(ACCOUNT_00, <Test as Config>::Deposit::get() * 100)])
.with_ctypes(vec![(attestation.ctype_hash, attester)])
.with_attestations(vec![(claim_hash, attestation)])
.build()
.execute_with(|| {
.build_and_execute_with_sanity_tests(|| {
assert_noop!(
Attestation::remove(
DoubleOrigin(ACCOUNT_00, evil.clone()).into(),
Expand All @@ -388,8 +375,7 @@ fn test_remove_not_found() {
ExtBuilder::default()
.with_balances(vec![(ACCOUNT_00, <Test as Config>::Deposit::get() * 100)])
.with_ctypes(vec![(attestation.ctype_hash, attester.clone())])
.build()
.execute_with(|| {
.build_and_execute_with_sanity_tests(|| {
assert!(Balances::reserved_balance(ACCOUNT_00).is_zero());
assert_noop!(
Attestation::remove(DoubleOrigin(ACCOUNT_00, attester.clone()).into(), claim_hash, None),
Expand All @@ -413,8 +399,7 @@ fn test_reclaim_deposit() {
.with_balances(vec![(ACCOUNT_00, <Test as Config>::Deposit::get() * 100)])
.with_ctypes(vec![(attestation.ctype_hash, attester)])
.with_attestations(vec![(claim_hash, attestation)])
.build()
.execute_with(|| {
.build_and_execute_with_sanity_tests(|| {
assert_eq!(Balances::reserved_balance(ACCOUNT_00), <Test as Config>::Deposit::get());
assert_ok!(Attestation::reclaim_deposit(
RuntimeOrigin::signed(ACCOUNT_00),
Expand All @@ -439,8 +424,7 @@ fn test_reclaim_deposit_authorization() {
.with_balances(vec![(ACCOUNT_00, <Test as Config>::Deposit::get() * 100)])
.with_ctypes(vec![(attestation.ctype_hash, attester)])
.with_attestations(vec![(claim_hash, attestation)])
.build()
.execute_with(|| {
.build_and_execute_with_sanity_tests(|| {
assert_eq!(Balances::reserved_balance(ACCOUNT_00), <Test as Config>::Deposit::get());
assert_ok!(Attestation::reclaim_deposit(
RuntimeOrigin::signed(ACCOUNT_00),
Expand All @@ -461,8 +445,7 @@ fn test_reclaim_unauthorized() {
.with_balances(vec![(ACCOUNT_00, <Test as Config>::Deposit::get() * 100)])
.with_ctypes(vec![(attestation.ctype_hash, attester)])
.with_attestations(vec![(claim_hash, attestation)])
.build()
.execute_with(|| {
.build_and_execute_with_sanity_tests(|| {
assert_noop!(
Attestation::reclaim_deposit(RuntimeOrigin::signed(ACCOUNT_01), claim_hash),
attestation::Error::<Test>::NotAuthorized,
Expand All @@ -479,8 +462,7 @@ fn test_reclaim_deposit_not_found() {
ExtBuilder::default()
.with_balances(vec![(ACCOUNT_00, <Test as Config>::Deposit::get() * 100)])
.with_ctypes(vec![(attestation.ctype_hash, attester)])
.build()
.execute_with(|| {
.build_and_execute_with_sanity_tests(|| {
assert_noop!(
Attestation::reclaim_deposit(RuntimeOrigin::signed(ACCOUNT_01), claim_hash),
attestation::Error::<Test>::NotFound,
Expand All @@ -506,8 +488,7 @@ fn test_change_deposit_owner() {
])
.with_ctypes(vec![(attestation.ctype_hash, attester.clone())])
.with_attestations(vec![(claim_hash, attestation)])
.build()
.execute_with(|| {
.build_and_execute_with_sanity_tests(|| {
assert_eq!(Balances::reserved_balance(ACCOUNT_00), <Test as Config>::Deposit::get());
assert_ok!(Attestation::change_deposit_owner(
DoubleOrigin(ACCOUNT_01, attester).into(),
Expand Down Expand Up @@ -539,8 +520,7 @@ fn test_change_deposit_owner_insufficient_balance() {
.with_balances(vec![(ACCOUNT_00, <Test as Config>::Deposit::get() * 100)])
.with_ctypes(vec![(attestation.ctype_hash, attester.clone())])
.with_attestations(vec![(claim_hash, attestation)])
.build()
.execute_with(|| {
.build_and_execute_with_sanity_tests(|| {
assert_eq!(Balances::reserved_balance(ACCOUNT_00), <Test as Config>::Deposit::get());
assert_noop!(
Attestation::change_deposit_owner(DoubleOrigin(ACCOUNT_01, attester).into(), claim_hash),
Expand All @@ -560,8 +540,7 @@ fn test_change_deposit_owner_unauthorized() {
.with_balances(vec![(ACCOUNT_00, <Test as Config>::Deposit::get() * 100)])
.with_ctypes(vec![(attestation.ctype_hash, attester)])
.with_attestations(vec![(claim_hash, attestation)])
.build()
.execute_with(|| {
.build_and_execute_with_sanity_tests(|| {
assert_noop!(
Attestation::change_deposit_owner(DoubleOrigin(ACCOUNT_00, evil_actor).into(), claim_hash),
attestation::Error::<Test>::NotAuthorized,
Expand All @@ -578,8 +557,7 @@ fn test_change_deposit_owner_not_found() {
ExtBuilder::default()
.with_balances(vec![(ACCOUNT_00, <Test as Config>::Deposit::get() * 100)])
.with_ctypes(vec![(attestation.ctype_hash, attester.clone())])
.build()
.execute_with(|| {
.build_and_execute_with_sanity_tests(|| {
assert_noop!(
Attestation::change_deposit_owner(DoubleOrigin(ACCOUNT_00, attester).into(), claim_hash),
attestation::Error::<Test>::NotFound,
Expand All @@ -602,8 +580,7 @@ fn test_update_deposit() {
.with_balances(vec![(ACCOUNT_00, <Test as Config>::Deposit::get() * 100)])
.with_ctypes(vec![(attestation.ctype_hash, attester)])
.with_attestations(vec![(claim_hash, attestation)])
.build()
.execute_with(|| {
.build_and_execute_with_sanity_tests(|| {
assert_eq!(
Balances::reserved_balance(ACCOUNT_00),
<Test as Config>::Deposit::get() * 2
Expand Down Expand Up @@ -642,8 +619,7 @@ fn test_update_deposit_unauthorized() {
.with_balances(vec![(ACCOUNT_00, <Test as Config>::Deposit::get() * 100)])
.with_ctypes(vec![(attestation.ctype_hash, attester)])
.with_attestations(vec![(claim_hash, attestation)])
.build()
.execute_with(|| {
.build_and_execute_with_sanity_tests(|| {
assert_eq!(
Balances::reserved_balance(ACCOUNT_00),
<Test as Config>::Deposit::get() * 2
Expand Down
63 changes: 62 additions & 1 deletion pallets/delegation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ use frame_support::{
use kilt_support::traits::StorageDepositCollector;
use parity_scale_codec::Encode;
use sp_runtime::{traits::Hash, DispatchError};
use sp_std::{marker::PhantomData, vec::Vec};
use sp_std::{marker::PhantomData, vec, vec::Vec};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This dependendency is only used in try-runtime, right? In this case it should not be pulled in regardless, and also it could either be used directly with sp_std::vec, or imported inside the function that needs it.


#[frame_support::pallet]
pub mod pallet {
Expand Down Expand Up @@ -286,6 +286,15 @@ pub mod pallet {
MaxChildrenExceeded,
}

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
#[cfg(feature = "try-runtime")]
fn try_state(_n: BlockNumberFor<T>) -> Result<(), &'static str> {
Self::do_try_state()?;
Ok(())
}
}

#[pallet::call]
impl<T: Config> Pallet<T> {
/// Create a new delegation root associated with a given CType hash.
Expand Down Expand Up @@ -965,6 +974,58 @@ pub mod pallet {
removals = removals.saturating_add(1);
Ok((removals, consumed_weight))
}

#[cfg(any(feature = "try-runtime", test))]
pub fn do_try_state() -> DispatchResult {
fn get_merged_subtree<T: Config>(node: DelegationNode<T>) -> Vec<DelegationNode<T>> {
let mut nodes_to_explore = vec![node];
let mut children: Vec<DelegationNode<T>> = vec![];
while !nodes_to_explore.is_empty() {
let current_node = nodes_to_explore.pop().unwrap();
let child_nodes: Vec<DelegationNode<T>> = current_node
.children
.iter()
.filter_map(DelegationNodes::<T>::get)
.collect();
nodes_to_explore.extend(child_nodes.clone());
children.extend(child_nodes);
}

children
}

DelegationNodes::<T>::iter().try_for_each(
|(delegation_node_id, delegation_details): (DelegationNodeIdOf<T>, DelegationNode<T>)| -> DispatchResult {
let hierachy_id = delegation_details.hierarchy_root_id;

// check if node is in part of a delegation hierachy.
ensure!(
DelegationHierarchies::<T>::contains_key(hierachy_id),
DispatchError::Other("Test1")
);

let children_count = DelegationNodes::<T>::iter_values()
.filter(|delegation_node : &DelegationNode<T> | delegation_node.children.contains(&delegation_node_id))
.count();

match delegation_details.parent {
// If node is a leaf or intermediate, check if it occures only once. Otherwise we have cylces.
Some(_) => ensure!(children_count <= 1 , DispatchError::Other("Test2")),
// if parent is None, check that the root is not the children
// from another node.
_ => ensure!(children_count == 0 , DispatchError::Other("Test3"))
};

// if a node is revoked, his subtree should be revoked as well.
if delegation_details.details.revoked {
let is_subtree_revoked = get_merged_subtree::<T>(delegation_details).iter().map(|child : &DelegationNode<T>| {child.details.revoked }).all(|x| x);
ensure!(is_subtree_revoked, DispatchError::Other("Test4"));
}
Ok(())
},
)?;
Ok(())
}
}

struct DelegationDepositCollector<T: Config>(PhantomData<T>);
Expand Down
Loading