Newer
Older
// Copyright 2021 Axiom-Team
//
// This file is part of Substrate-Libre-Currency.
//
// Substrate-Libre-Currency is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, version 3 of the License.
//
// Substrate-Libre-Currency 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with Substrate-Libre-Currency. If not, see <https://www.gnu.org/licenses/>.
use super::*;
use crate::{self as pallet_duniter_wot};
use frame_support::{parameter_types, traits::Everything};
use frame_system as system;
use sp_core::H256;
use sp_runtime::{
testing::{Header, TestSignature, UintAuthorityId},
traits::{BlakeTwo256, IdentityLookup},
};
use std::collections::BTreeMap;
type AccountId = u64;
type Block = frame_system::mocking::MockBlock<Test>;
type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
// Configure a mock runtime to test the pallet.
frame_support::construct_runtime!(
pub enum Test where
Block = Block,
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic,
{
System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
DuniterWot: pallet_duniter_wot::<Instance1>::{Pallet},
Identity: pallet_identity::{Pallet, Call, Config<T>, Storage, Event<T>},
Membership: pallet_membership::<Instance1>::{Pallet, Call, Config<T>, Storage, Event<T>},
Cert: pallet_certification::<Instance1>::{Pallet, Call, Config<T>, Storage, Event<T>},
}
);
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
parameter_types! {
pub const BlockHashCount: u64 = 250;
pub const SS58Prefix: u8 = 42;
}
impl system::Config for Test {
type BaseCallFilter = Everything;
type BlockWeights = ();
type BlockLength = ();
type DbWeight = ();
type Origin = Origin;
type Call = Call;
type Index = u64;
type BlockNumber = u64;
type Hash = H256;
type Hashing = BlakeTwo256;
type AccountId = AccountId;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header;
type Event = Event;
type BlockHashCount = BlockHashCount;
type Version = ();
type PalletInfo = PalletInfo;
type AccountData = ();
type OnNewAccount = ();
type OnKilledAccount = ();
type SystemWeightInfo = ();
type SS58Prefix = SS58Prefix;
type OnSetCode = ();
type MaxConsumers = frame_support::traits::ConstU32<16>;
}
// DuniterWot
parameter_types! {
pub const MinCertForMembership: u32 = 2;
pub const FirstIssuableOn: u64 = 2;
}
impl pallet_duniter_wot::Config<Instance1> for Test {
type IsSubWot = frame_support::traits::ConstBool<false>;
type MinCertForMembership = MinCertForMembership;
type MinCertForCreateIdtyRight = MinCertForCreateIdtyRigh;
type FirstIssuableOn = FirstIssuableOn;
}
// Identity
parameter_types! {
pub const ConfirmPeriod: u64 = 2;
pub const IdtyCreationPeriod: u64 = 3;
pub const ValidationPeriod: u64 = 2;
}
pub struct IdtyNameValidatorTestImpl;
impl pallet_identity::traits::IdtyNameValidator for IdtyNameValidatorTestImpl {
fn validate(idty_name: &pallet_identity::IdtyName) -> bool {
idty_name.0.len() < 16
}
}
impl pallet_identity::Config for Test {
type ConfirmPeriod = ConfirmPeriod;
type Event = Event;
type EnsureIdtyCallAllowed = DuniterWot;
type IdtyCreationPeriod = IdtyCreationPeriod;
type IdtyNameValidator = IdtyNameValidatorTestImpl;
type IdtyIndex = IdtyIndex;
type IdtyValidationOrigin = system::EnsureRoot<AccountId>;
type IsMember = Membership;
type OnIdtyChange = DuniterWot;
type RemoveIdentityConsumers = ();
type RevocationSigner = UintAuthorityId;
type RevocationSignature = TestSignature;
}
// Membership
parameter_types! {
pub const MembershipPeriod: u64 = 5;
pub const PendingMembershipPeriod: u64 = 3;
pub const RenewablePeriod: u64 = 2;
pub const RevocationPeriod: u64 = 4;
}
impl pallet_membership::Config<Instance1> for Test {
type IsIdtyAllowedToRenewMembership = DuniterWot;
type IsIdtyAllowedToRequestMembership = DuniterWot;
type Event = Event;
type IdtyId = IdtyIndex;
type MembershipPeriod = MembershipPeriod;
type PendingMembershipPeriod = PendingMembershipPeriod;
type RenewablePeriod = RenewablePeriod;
type RevocationPeriod = RevocationPeriod;
}
// Cert
parameter_types! {
pub const MinReceivedCertToBeAbleToIssueCert: u32 = 2;
pub const CertRenewablePeriod: u64 = 4;
pub const CertPeriod: u64 = 2;
pub const ValidityPeriod: u64 = 10;
}
impl pallet_certification::Config<Instance1> for Test {
type CertPeriod = CertPeriod;
type Event = Event;
type IdtyIndex = IdtyIndex;
type IdtyIndexOf = Identity;
type IsCertAllowed = DuniterWot;
type MaxByIssuer = MaxByIssuer;
type MinReceivedCertToBeAbleToIssueCert = MinReceivedCertToBeAbleToIssueCert;
type OnNewcert = DuniterWot;
type OnRemovedCert = DuniterWot;
type CertRenewablePeriod = CertRenewablePeriod;
type ValidityPeriod = ValidityPeriod;
}
pub const NAMES: [&str; 6] = ["Alice", "Bob", "Charlie", "Dave", "Eve", "Ferdie"];
// Build genesis storage according to the mock runtime.
pub fn new_test_ext(initial_identities_len: usize) -> sp_io::TestExternalities {
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
let mut t = frame_system::GenesisConfig::default()
.build_storage::<Test>()
.unwrap();
pallet_identity::GenesisConfig::<Test> {
identities: (1..=initial_identities_len)
.map(|i| pallet_identity::GenesisIdty {
index: i as u32,
name: pallet_identity::IdtyName::from(NAMES[i - 1]),
value: pallet_identity::IdtyValue {
next_creatable_identity_on: 0,
owner_key: i as u64,
removable_on: 0,
status: pallet_identity::IdtyStatus::Validated,
},
})
.collect(),
}
.assimilate_storage(&mut t)
.unwrap();
pallet_membership::GenesisConfig::<Test, Instance1> {
memberships: (1..=initial_identities_len)
.map(|i| {
(
i as u32,
sp_membership::MembershipData {
expire_on: MembershipPeriod::get(),
renewable_on: RenewablePeriod::get(),
)
})
.collect(),
}
.assimilate_storage(&mut t)
.unwrap();
pallet_certification::GenesisConfig::<Test, Instance1> {
certs_by_issuer: clique_wot(initial_identities_len, ValidityPeriod::get()),
apply_cert_period_at_genesis: true,
.assimilate_storage(&mut t)
.unwrap();
frame_support::BasicExternalities::execute_with_storage(&mut t, || {
// manually increment genesis identities sufficient counter
// In real world, this should be handle manually by genesis creator
for i in 1..=initial_identities_len {
frame_system::Pallet::<Test>::inc_sufficients(&(i as u64));
}
// Some dedicated test account
frame_system::Pallet::<Test>::inc_providers(&(initial_identities_len as u64));
frame_system::Pallet::<Test>::inc_providers(&(initial_identities_len as u64 + 1));
});
sp_io::TestExternalities::new(t)
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
}
pub fn run_to_block(n: u64) {
while System::block_number() < n {
DuniterWot::on_finalize(System::block_number());
Identity::on_finalize(System::block_number());
Membership::on_finalize(System::block_number());
Cert::on_finalize(System::block_number());
System::on_finalize(System::block_number());
System::reset_events();
System::set_block_number(System::block_number() + 1);
System::on_initialize(System::block_number());
DuniterWot::on_initialize(System::block_number());
Identity::on_initialize(System::block_number());
Membership::on_initialize(System::block_number());
Cert::on_initialize(System::block_number());
}
}
fn clique_wot(
initial_identities_len: usize,
cert_validity_period: u64,
) -> BTreeMap<IdtyIndex, BTreeMap<IdtyIndex, u64>> {
let mut certs_by_issuer = BTreeMap::new();
for i in 1..=initial_identities_len {
certs_by_issuer.insert(
i as IdtyIndex,
(1..=initial_identities_len)
.filter_map(|j| {
if i != j {
Some((j as IdtyIndex, cert_validity_period))
} else {
None
}
})
.collect(),
);
}
certs_by_issuer
}