Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
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
203
204
205
206
207
208
209
210
211
212
213
214
// 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,
traits::{BlakeTwo256, IdentityLookup},
BuildStorage,
};
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::{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>},
}
);
// Sstem
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 MinCertForUdRight: u8 = 2;
pub const MinCertForCertRight: u8 = 3;
pub const MinCertForCreateIdtyRigh: u8 = 4;
pub const FirstIssuableOn: u64 = 2;
}
impl pallet_duniter_wot::Config for Test {
type MinCertForUdRight = MinCertForUdRight;
type MinCertForCertRight = MinCertForCertRight;
type MinCertForCreateIdtyRight = MinCertForCreateIdtyRigh;
type FirstIssuableOn = FirstIssuableOn;
}
// Identity
parameter_types! {
pub const ConfirmPeriod: u64 = 2;
pub const IdtyCreationPeriod: u64 = 3;
pub const MaxInactivityPeriod: u64 = 5;
pub const MaxNoRightPeriod: u64 = 4;
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 AddRightOrigin = system::EnsureRoot<AccountId>;
type DelRightOrigin = system::EnsureRoot<AccountId>;
type EnsureIdtyCallAllowed = DuniterWot;
type IdtyCreationPeriod = IdtyCreationPeriod;
type IdtyData = ();
type IdtyDataProvider = ();
type IdtyNameValidator = IdtyNameValidatorTestImpl;
type IdtyIndex = IdtyIndex;
type IdtyValidationOrigin = system::EnsureRoot<AccountId>;
type IdtyRight = IdtyRight;
type IsMember = Membership;
type OnIdtyChange = DuniterWot;
type OnRightKeyChange = ();
type MaxNoRightPeriod = MaxNoRightPeriod;
}
// Membership
parameter_types! {
pub const ExternalizeMembershipStorage: bool = false;
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 IsIdtyAllowedToClaimMembership = DuniterWot;
type IsIdtyAllowedToRenewMembership = DuniterWot;
type IsIdtyAllowedToRequestMembership = DuniterWot;
type IsOriginAllowedToUseIdty = DuniterWot;
type Event = Event;
type ExternalizeMembershipStorage = ExternalizeMembershipStorage;
type IdtyId = IdtyIndex;
type OnEvent = DuniterWot;
type MembershipExternalStorage = sp_membership::traits::NoExternalStorage;
type MembershipPeriod = MembershipPeriod;
type PendingMembershipPeriod = PendingMembershipPeriod;
type RenewablePeriod = RenewablePeriod;
type RevocationPeriod = RevocationPeriod;
}
// Cert
parameter_types! {
pub const MaxByIssuer: u8 = 3;
pub const CertRenewablePeriod: u64 = 4;
pub const CertPeriod: u64 = 2;
pub const ValidityPeriod: u64 = 10;
}
impl pallet_certification::Config<Instance1> for Test {
type AddCertOrigin = pallet_duniter_wot::AddStrongCertOrigin<Test>;
type CertPeriod = CertPeriod;
type DelCertOrigin = pallet_duniter_wot::DelStrongCertOrigin<Test>;
type Event = Event;
type IdtyIndex = IdtyIndex;
type MaxByIssuer = MaxByIssuer;
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 {
GenesisConfig {
system: SystemConfig::default(),
identity: IdentityConfig {
identities: (1..=initial_identities_len)
.map(|i| pallet_identity::IdtyValue {
data: (),
owner_key: i as u64,
name: pallet_identity::IdtyName::from(NAMES[i]),
next_creatable_identity_on: 0,
removable_on: 0,
rights: vec![
(IdtyRight::CreateIdty, None),
(IdtyRight::StrongCert, None),
(IdtyRight::Ud, None),
],
status: pallet_identity::IdtyStatus::Validated,
})
.collect(),
},
membership: MembershipConfig {
memberships: (1..=initial_identities_len)
.map(|i| {
(
i as u32,
sp_membership::MembershipData {
expire_on: MembershipPeriod::get(),
renewable_on: RenewablePeriod::get(),
},
)
})
.collect(),
},
cert: CertConfig {
certs_by_issuer: clique_wot(initial_identities_len, ValidityPeriod::get()),
216
217
218
219
220
221
222
223
224
225
226
227
228
229
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
},
}
.build_storage()
.unwrap()
.into()
}
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
}