Skip to content

Commit

Permalink
feat: genesis config for linking pallet (#476)
Browse files Browse the repository at this point in the history
## No Ticket

* adds accounts for doc tests to endowed accounts
* Linking Pallet
* MigrationState is Done in genesis (fix
https://github.com/KILTprotocol/ticket/issues/2475)
    * allow to configure pallet in genesis


I added the option to configure the DidLookupPallet in genesis, so that
we can start a test chain with already populated DID-Account links. We
could start adding genesis configs to the other pallets as well. Some
tests require well known DIDs or w3n to exist. We could add them in the
genesis config, so that the tests work out of the box with dev chains.
  • Loading branch information
weichweich authored Feb 23, 2023
1 parent ea5c315 commit a9d23d5
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions nodes/parachain/src/chain_spec/peregrine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,5 +219,6 @@ fn testnet_genesis(
polkadot_xcm: PolkadotXcmConfig {
safe_xcm_version: Some(SAFE_XCM_VERSION),
},
did_lookup: Default::default(),
}
}
1 change: 1 addition & 0 deletions nodes/parachain/src/chain_spec/spiritnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,5 +323,6 @@ fn testnet_genesis(
polkadot_xcm: PolkadotXcmConfig {
safe_xcm_version: Some(SAFE_XCM_VERSION),
},
did_lookup: Default::default(),
}
}
1 change: 1 addition & 0 deletions nodes/standalone/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ fn devnet_genesis(
aura: Default::default(),
grandpa: Default::default(),
sudo: SudoConfig { key: Some(root_key) },
did_lookup: Default::default(),
}
}

Expand Down
5 changes: 5 additions & 0 deletions pallets/pallet-did-lookup/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ sp-std.workspace = true
# Benchmarking dependencies
frame-benchmarking = {workspace = true, optional = true}

# std dependencies
serde = {workspace = true, optional = true}

[features]
default = ["std"]
runtime-benchmarks = [
Expand All @@ -61,10 +64,12 @@ std = [
"frame-support/std",
"frame-system/std",
"hex/std",
"kilt-support/std",
"log/std",
"libsecp256k1/std",
"scale-info/std",
"sha3/std",
"serde",
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
Expand Down
1 change: 1 addition & 0 deletions pallets/pallet-did-lookup/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use sp_core::{ecdsa, H160, H256};

/// The AccountId20 type.
/// It is a 20-byte Ethereum address.
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
#[derive(
Eq, PartialEq, Copy, Clone, Encode, Decode, TypeInfo, MaxEncodedLen, Default, PartialOrd, Ord, RuntimeDebug,
)]
Expand Down
1 change: 1 addition & 0 deletions pallets/pallet-did-lookup/src/connection_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use kilt_support::deposit::Deposit;
use scale_info::TypeInfo;

/// A record in the ConnectedDid map.
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Decode, Debug, Encode, TypeInfo, Eq, PartialEq, MaxEncodedLen)]
pub struct ConnectionRecord<DidIdentifier, Account, Balance> {
/// The did that is connected to the key under which the record was stored.
Expand Down
30 changes: 29 additions & 1 deletion pallets/pallet-did-lookup/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub mod pallet {
type OriginSuccess: CallSources<AccountIdOf<Self>, DidIdentifierOf<Self>>;

/// The identifier to which accounts can get associated.
type DidIdentifier: Parameter + MaxEncodedLen;
type DidIdentifier: Parameter + MaxEncodedLen + MaybeSerializeDeserialize;

/// The currency that is used to reserve funds for each did.
type Currency: ReservableCurrency<AccountIdOf<Self>>;
Expand Down Expand Up @@ -170,6 +170,34 @@ pub mod pallet {
Migration,
}

#[pallet::genesis_config]
pub struct GenesisConfig<T: Config> {
pub links: Vec<(LinkableAccountId, ConnectionRecordOf<T>)>,
}

#[cfg(feature = "std")]
impl<T: Config> Default for GenesisConfig<T> {
fn default() -> Self {
Self {
links: Default::default(),
}
}
}

#[pallet::genesis_build]
impl<T: Config> GenesisBuild<T> for GenesisConfig<T> {
fn build(&self) {
// populate link records
for (acc, connection) in &self.links {
ConnectedDids::<T>::insert(acc, connection);
ConnectedAccounts::<T>::insert(&connection.did, acc, ());
}

// set migration state to done
MigrationStateStore::<T>::set(MigrationState::Done);
}
}

#[pallet::call]
impl<T: Config> Pallet<T>
where
Expand Down
1 change: 1 addition & 0 deletions pallets/pallet-did-lookup/src/linkable_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use sp_runtime::AccountId32;

use crate::account::AccountId20;

#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Encode, Decode, MaxEncodedLen, TypeInfo)]
pub enum LinkableAccountId {
AccountId20(AccountId20),
Expand Down
8 changes: 6 additions & 2 deletions support/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
[package]
authors.workspace = true
description = "Shared traits and structs used across the KILT pallets"
documentation.workspace = true
edition.workspace = true
homepage.workspace = true
license-file.workspace = true
name = "kilt-support"
readme.workspace = true
repository.workspace = true
version.workspace = true
name = "kilt-support"
description = "Shared traits and structs used across the KILT pallets"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
Expand All @@ -25,6 +25,9 @@ sp-core.workspace = true
sp-runtime.workspace = true
sp-std.workspace = true

# std dependencies
serde = {workspace = true, optional = true}

[features]
default = ["std"]
mock = []
Expand All @@ -38,6 +41,7 @@ std = [
"frame-support/std",
"frame-system/std",
"scale-info/std",
"serde",
"sp-core/std",
"sp-runtime/std",
"sp-std/std",
Expand Down
1 change: 1 addition & 0 deletions support/src/deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use scale_info::TypeInfo;
use sp_runtime::{traits::Zero, DispatchError};

/// An amount of balance reserved by the specified address.
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Debug, Encode, Decode, Eq, PartialEq, Ord, PartialOrd, TypeInfo, MaxEncodedLen)]
pub struct Deposit<Account, Balance> {
pub owner: Account,
Expand Down
1 change: 1 addition & 0 deletions support/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ pub mod mock_origin {
}
}

#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, TypeInfo, MaxEncodedLen)]
pub struct SubjectId(pub AccountId32);

Expand Down

0 comments on commit a9d23d5

Please sign in to comment.