Skip to content
Snippets Groups Projects
Commit a998d76e authored by Éloïs's avatar Éloïs
Browse files

feat(certs): allow to config certs expire_on at genesis

parent 43460507
No related branches found
No related tags found
No related merge requests found
...@@ -1403,6 +1403,7 @@ dependencies = [ ...@@ -1403,6 +1403,7 @@ dependencies = [
"jsonrpc-core", "jsonrpc-core",
"log", "log",
"maplit", "maplit",
"pallet-certification",
"pallet-grandpa", "pallet-grandpa",
"pallet-transaction-payment-rpc", "pallet-transaction-payment-rpc",
"pallet-transaction-payment-rpc-runtime-api", "pallet-transaction-payment-rpc-runtime-api",
......
...@@ -31,6 +31,7 @@ common-runtime = { path = '../runtime/common' } ...@@ -31,6 +31,7 @@ common-runtime = { path = '../runtime/common' }
g1-runtime = { path = '../runtime/g1' } g1-runtime = { path = '../runtime/g1' }
gdev-runtime = { path = '../runtime/gdev' } gdev-runtime = { path = '../runtime/gdev' }
gtest-runtime = { path = '../runtime/gtest' } gtest-runtime = { path = '../runtime/gtest' }
pallet-certification = { path = '../pallets/certification' }
# crates.io dependencies # crates.io dependencies
async-io = "1.3" async-io = "1.3"
......
...@@ -22,7 +22,7 @@ use common_runtime::IdtyIndex; ...@@ -22,7 +22,7 @@ use common_runtime::IdtyIndex;
use common_runtime::{entities::IdtyName, AccountId, Signature}; use common_runtime::{entities::IdtyName, AccountId, Signature};
use sp_core::{Pair, Public}; use sp_core::{Pair, Public};
use sp_runtime::traits::{IdentifyAccount, Verify}; use sp_runtime::traits::{IdentifyAccount, Verify};
use std::collections::{BTreeMap, BTreeSet}; use std::collections::BTreeMap;
pub type AccountPublic = <Signature as Verify>::Signer; pub type AccountPublic = <Signature as Verify>::Signer;
...@@ -49,13 +49,22 @@ where ...@@ -49,13 +49,22 @@ where
AccountPublic::from(get_from_seed::<TPublic>(seed)).into_account() AccountPublic::from(get_from_seed::<TPublic>(seed)).into_account()
} }
fn clique_wot(initial_identities_len: usize) -> BTreeMap<IdtyIndex, BTreeSet<IdtyIndex>> { fn clique_wot(
initial_identities_len: usize,
cert_validity_period: common_runtime::BlockNumber,
) -> BTreeMap<IdtyIndex, BTreeMap<IdtyIndex, common_runtime::BlockNumber>> {
let mut certs_by_issuer = BTreeMap::new(); let mut certs_by_issuer = BTreeMap::new();
for i in 1..=initial_identities_len { for i in 1..=initial_identities_len {
certs_by_issuer.insert( certs_by_issuer.insert(
i as IdtyIndex, i as IdtyIndex,
(1..=initial_identities_len) (1..=initial_identities_len)
.filter_map(|j| if i != j { Some(j as IdtyIndex) } else { None }) .filter_map(|j| {
if i != j {
Some((j as IdtyIndex, cert_validity_period))
} else {
None
}
})
.collect(), .collect(),
); );
} }
......
...@@ -133,7 +133,10 @@ fn devnet_genesis( ...@@ -133,7 +133,10 @@ fn devnet_genesis(
.collect(), .collect(),
}, },
strong_cert: StrongCertConfig { strong_cert: StrongCertConfig {
certs_by_issuer: clique_wot(initial_identities.len()), certs_by_issuer: clique_wot(
initial_identities.len(),
gdev_runtime::parameters::ValidityPeriod::get(),
),
phantom: std::marker::PhantomData, phantom: std::marker::PhantomData,
}, },
ud_accounts_storage: UdAccountsStorageConfig { ud_accounts_storage: UdAccountsStorageConfig {
......
...@@ -187,7 +187,10 @@ fn devnet_genesis( ...@@ -187,7 +187,10 @@ fn devnet_genesis(
.collect(), .collect(),
}, },
strong_cert: StrongCertConfig { strong_cert: StrongCertConfig {
certs_by_issuer: clique_wot(initial_identities.len()), certs_by_issuer: clique_wot(
initial_identities.len(),
gtest_runtime::parameters::ValidityPeriod::get(),
),
phantom: std::marker::PhantomData, phantom: std::marker::PhantomData,
}, },
ud_accounts_storage: UdAccountsStorageConfig { ud_accounts_storage: UdAccountsStorageConfig {
...@@ -249,7 +252,10 @@ fn testnet_genesis( ...@@ -249,7 +252,10 @@ fn testnet_genesis(
.collect(), .collect(),
}, },
strong_cert: StrongCertConfig { strong_cert: StrongCertConfig {
certs_by_issuer: clique_wot(initial_identities.len()), certs_by_issuer: clique_wot(
initial_identities.len(),
gdev_runtime::parameters::ValidityPeriod::get(),
),
phantom: std::marker::PhantomData, phantom: std::marker::PhantomData,
}, },
ud_accounts_storage: UdAccountsStorageConfig { ud_accounts_storage: UdAccountsStorageConfig {
......
...@@ -39,7 +39,7 @@ pub mod pallet { ...@@ -39,7 +39,7 @@ pub mod pallet {
use super::*; use super::*;
use frame_support::pallet_prelude::*; use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*; use frame_system::pallet_prelude::*;
use sp_std::collections::{btree_map::BTreeMap, btree_set::BTreeSet}; use sp_std::collections::btree_map::BTreeMap;
/// The current storage version. /// The current storage version.
const STORAGE_VERSION: StorageVersion = StorageVersion::new(1); const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);
...@@ -87,7 +87,7 @@ pub mod pallet { ...@@ -87,7 +87,7 @@ pub mod pallet {
#[pallet::genesis_config] #[pallet::genesis_config]
pub struct GenesisConfig<T: Config<I>, I: 'static = ()> { pub struct GenesisConfig<T: Config<I>, I: 'static = ()> {
pub certs_by_issuer: BTreeMap<T::IdtyIndex, BTreeSet<T::IdtyIndex>>, pub certs_by_issuer: BTreeMap<T::IdtyIndex, BTreeMap<T::IdtyIndex, T::BlockNumber>>,
pub phantom: PhantomData<I>, pub phantom: PhantomData<I>,
} }
...@@ -109,7 +109,7 @@ pub mod pallet { ...@@ -109,7 +109,7 @@ pub mod pallet {
let mut certs_by_receiver = BTreeMap::<T::IdtyIndex, Vec<T::IdtyIndex>>::new(); let mut certs_by_receiver = BTreeMap::<T::IdtyIndex, Vec<T::IdtyIndex>>::new();
for (issuer, receivers) in &self.certs_by_issuer { for (issuer, receivers) in &self.certs_by_issuer {
assert!( assert!(
!receivers.contains(issuer), !receivers.contains_key(issuer),
"Identity cannot certify it-self." "Identity cannot certify it-self."
); );
assert!( assert!(
...@@ -122,11 +122,11 @@ pub mod pallet { ...@@ -122,11 +122,11 @@ pub mod pallet {
*issuer, *issuer,
IdtyCertMeta { IdtyCertMeta {
issued_count: receivers.len() as u8, issued_count: receivers.len() as u8,
next_issuable_on: T::CertPeriod::get(), next_issuable_on: sp_runtime::traits::Zero::zero(),
received_count: 0, received_count: 0,
}, },
); );
for receiver in receivers { for receiver in receivers.keys() {
certs_by_receiver certs_by_receiver
.entry(*receiver) .entry(*receiver)
.or_default() .or_default()
...@@ -142,26 +142,47 @@ pub mod pallet { ...@@ -142,26 +142,47 @@ pub mod pallet {
issuers.sort(); issuers.sort();
<StorageCertsByReceiver<T, I>>::insert(receiver, issuers); <StorageCertsByReceiver<T, I>>::insert(receiver, issuers);
} }
// Write StorageIdtyCertMeta // Write StorageCertsByIssuer
for (issuer, cert_meta) in cert_meta_by_issuer { let mut certs_removable_on =
<StorageIdtyCertMeta<T, I>>::insert(issuer, cert_meta); BTreeMap::<T::BlockNumber, Vec<(T::IdtyIndex, T::IdtyIndex)>>::new();
}
// Write StorageCertsByIssuer && StorageCertsRemovableOn
let mut all_couples = Vec::new();
for (issuer, receivers) in &self.certs_by_issuer { for (issuer, receivers) in &self.certs_by_issuer {
for receiver in receivers { for (receiver, removable_on) in receivers {
all_couples.push((*issuer, *receiver)); certs_removable_on
.entry(*removable_on)
.or_default()
.push((*issuer, *receiver));
use sp_runtime::traits::Saturating as _;
let issuer_next_issuable_on = removable_on
.saturating_add(T::CertPeriod::get())
.saturating_sub(T::ValidityPeriod::get());
if let Some(cert_meta) = cert_meta_by_issuer.get_mut(issuer) {
if cert_meta.next_issuable_on < issuer_next_issuable_on {
cert_meta.next_issuable_on = issuer_next_issuable_on;
}
}
let renewable_on = removable_on.saturating_sub(
T::ValidityPeriod::get().saturating_sub(T::CertRenewablePeriod::get()),
);
<StorageCertsByIssuer<T, I>>::insert( <StorageCertsByIssuer<T, I>>::insert(
issuer, issuer,
receiver, receiver,
CertValue { CertValue {
renewable_on: T::CertRenewablePeriod::get(), renewable_on,
removable_on: T::ValidityPeriod::get(), removable_on: *removable_on,
}, },
); );
} }
} }
<StorageCertsRemovableOn<T, I>>::insert(T::ValidityPeriod::get(), all_couples); // Write StorageIdtyCertMeta
for (issuer, cert_meta) in cert_meta_by_issuer {
<StorageIdtyCertMeta<T, I>>::insert(issuer, cert_meta);
}
// Write storage StorageCertsRemovableOn
for (removable_on, certs) in certs_removable_on {
<StorageCertsRemovableOn<T, I>>::insert(removable_on, certs);
}
} }
} }
......
...@@ -14,11 +14,12 @@ ...@@ -14,11 +14,12 @@
// You should have received a copy of the GNU Affero General Public License // 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/>. // along with Substrate-Libre-Currency. If not, see <https://www.gnu.org/licenses/>.
use crate::mock::Event as RuntimeEvent;
use crate::mock::*; use crate::mock::*;
use crate::Error; use crate::{Error, Event};
use frame_support::assert_ok; use frame_support::assert_ok;
//use frame_system::{EventRecord, Phase}; //use frame_system::{EventRecord, Phase};
use maplit::{btreemap, btreeset}; use maplit::btreemap;
use sp_std::collections::btree_map::BTreeMap; use sp_std::collections::btree_map::BTreeMap;
#[test] #[test]
...@@ -35,10 +36,84 @@ fn test_must_receive_cert_before_can_issue() { ...@@ -35,10 +36,84 @@ fn test_must_receive_cert_before_can_issue() {
}); });
} }
#[test]
fn test_genesis_build() {
new_test_ext(DefaultCertificationConfig {
certs_by_issuer: btreemap![
0 => btreemap![
1 => 10,
2 => 5,
],
1 => btreemap![
0 => 7,
2 => 4,
],
2 => btreemap![
0 => 9,
1 => 3,
],
],
phantom: core::marker::PhantomData,
})
.execute_with(|| {
run_to_block(1);
// Verify state of idty 0
assert_eq!(
DefaultCertification::idty_cert_meta(0),
Some(crate::IdtyCertMeta {
issued_count: 2,
next_issuable_on: 2,
received_count: 2,
})
);
// Verify state of idty 1
assert_eq!(
DefaultCertification::idty_cert_meta(1),
Some(crate::IdtyCertMeta {
issued_count: 2,
next_issuable_on: 0,
received_count: 2,
})
);
// Verify state of idty 2
assert_eq!(
DefaultCertification::idty_cert_meta(2),
Some(crate::IdtyCertMeta {
issued_count: 2,
next_issuable_on: 1,
received_count: 2,
})
);
// Cert 2->1 must be removable at block #3
assert_eq!(
DefaultCertification::certs_removable_on(3),
Some(vec![(2, 1)]),
);
// Cert 2->0 cannot be renewed before #5
assert_eq!(
DefaultCertification::add_cert(Origin::root(), 2, 0),
Err(Error::<Test, _>::NotRespectRenewablePeriod.into())
);
run_to_block(3);
// Cert 2->1 must have expired
assert_eq!(
System::events()[0].event,
RuntimeEvent::DefaultCertification(Event::RemovedCert {
issuer: 2,
issuer_issued_count: 1,
receiver: 1,
receiver_received_count: 1,
expiration: true,
},)
);
});
}
#[test] #[test]
fn test_cert_period() { fn test_cert_period() {
new_test_ext(DefaultCertificationConfig { new_test_ext(DefaultCertificationConfig {
certs_by_issuer: btreemap![0 => btreeset![1]], certs_by_issuer: btreemap![0 => btreemap![1 => 10]],
phantom: core::marker::PhantomData, phantom: core::marker::PhantomData,
}) })
.execute_with(|| { .execute_with(|| {
...@@ -61,7 +136,7 @@ fn test_cert_period() { ...@@ -61,7 +136,7 @@ fn test_cert_period() {
#[test] #[test]
fn test_renewable_period() { fn test_renewable_period() {
new_test_ext(DefaultCertificationConfig { new_test_ext(DefaultCertificationConfig {
certs_by_issuer: btreemap![0 => btreeset![1]], certs_by_issuer: btreemap![0 => btreemap![1 => 10]],
phantom: core::marker::PhantomData, phantom: core::marker::PhantomData,
}) })
.execute_with(|| { .execute_with(|| {
......
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
#[cfg(feature = "std")] #[cfg(feature = "std")]
include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs")); include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));
mod parameters; pub mod parameters;
pub use self::parameters::*; pub use self::parameters::*;
pub use common_runtime::{ pub use common_runtime::{
......
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
#[cfg(feature = "std")] #[cfg(feature = "std")]
include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs")); include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));
mod parameters; pub mod parameters;
pub use self::parameters::*; pub use self::parameters::*;
pub use common_runtime::{ pub use common_runtime::{
......
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
#[cfg(feature = "std")] #[cfg(feature = "std")]
include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs")); include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));
mod parameters; pub mod parameters;
pub use self::parameters::*; pub use self::parameters::*;
pub use common_runtime::{ pub use common_runtime::{
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment