Skip to content

Commit

Permalink
chore: improve DX (#577)
Browse files Browse the repository at this point in the history
Fixes KILTprotocol/ticket#2974 and based on
top of #574.

This is my take at trying to improve the DX of integrating DIP
components. The following is a list of the main changes introduced by
this PR:

- Definitions of traits that are only ever used and useful inside a
pallet have been changed to take a generic `Runtime` type that must
implement the pallet config, reducing the clutter when implementing them
- Additional bounds have been added to the associated types of those
traits, so that they don't need to be specified in the pallet config so
that we can have trait bounds of those types. So basically what used to
be the trait bounds on the pallet config, is not a trait bound on the
trait return type. I think this is also the "Rusty" way to go.
- The generic consumer types exported in `kilt-dip-support` crate have
been renamed with the `Generic` prefix, because I introduced two new
types, starting with `Kilt`, that make it easier to integrate any KILT
runtime as an identity provider, with the downside of having to depend
on the whole runtime definition, but there's also ways around that
(e.g., by having an intermediate crate that only implements the required
`Config` traits). Example 1 below shows the change.
- Better re-exports (feedback is welcome on this one). For instance, for
types that implement a basic version of a pallet's trait definition, the
type is exported directly from the pallet crate root, while the trait is
still namespaced behind the `mod traits`. Example 2 below.
- Change of the origin check for the `pallet_dip_consumer::dispatch_as`
extrinsic from `ensure_signed` to a generic
`T::DispatchOriginCheck::ensure_origin`, as long as the
`DispatchOriginCheck` returns an `AccountId` upon successful
verification.

## Example 1: old vs new way of integrating a consumer into a runtime
where KILT is a provider

### Old

```rust
pub type ProofVerifier = VersionedDipSiblingProviderStateProofVerifier<
	RelayStateRootsViaRelayStorePallet<Runtime>,
	ConstU32<2_000>,
	ProviderParachainStateInfoViaProviderPallet<ProviderRuntime>,
	AccountId,
	BlakeTwo256,
	KeyIdOf<ProviderRuntime>,
	ProviderAccountId,
	Web3Name,
	LinkableAccountId,
	10,
	10,
	u128,
	// Signatures are valid for 50 blocks
	FrameSystemDidSignatureContext<Runtime, 50>,
	DipCallFilter,
>;
```

### New (100% equivalent to the old one)

```rust
pub type ProofVerifier = KiltVersionedSiblingProviderVerifier<
	ProviderRuntime,    // KILT runtime definition
	ConstU32<2_000>,
	RelayStateRootsViaRelayStorePallet<Runtime>,    // Local runtime definition
	BlakeTwo256,
	DipCallFilter,
	10,
	10,
	50,
>;
```

## Example 2: example of type implementing a pallet's trait with a basic
implementation

For the trait

```rust
	pub trait IdentityCommitmentGenerator<Runtime>
	where
		Runtime: Config,
		Runtime::IdentityProvider: IdentityProvider<Runtime>,
	{
		type Error: Into<u16>;
		type Output: Clone + Eq + Debug + TypeInfo + FullCodec + MaxEncodedLen;

		fn generate_commitment(
			identifier: &Runtime::Identifier,
			identity: &IdentityOf<Runtime>,
			version: IdentityCommitmentVersion,
		) -> Result<Self::Output, Self::Error>;
	}
```

a basic implementation (mostly useful for tests) is provided

```rust
	pub struct DefaultIdentityCommitmentGenerator<Output>(PhantomData<Output>);

	impl<Runtime, Output> IdentityCommitmentGenerator<Runtime> for DefaultIdentityCommitmentGenerator<Output>
	where
		Runtime: Config,
		Output: Default + Clone + Eq + Debug + TypeInfo + FullCodec + MaxEncodedLen,
	{
		type Error = u16;
		type Output = Output;

		fn generate_commitment(
			_identifier: &Runtime::Identifier,
			_identity: &IdentityOf<Runtime>,
			_version: IdentityCommitmentVersion,
		) -> Result<Self::Output, Self::Error> {
			Ok(Output::default())
		}
	}
```

The pallet's crate will then export the trait with `pub mod traits`,
while the type is re-exported directly _also_ from the root with `pub
use traits::DefaultIdentityCommitmentGenerator`.
  • Loading branch information
ntn-x2 authored Nov 15, 2023
1 parent 6d2481e commit 3ce3da6
Show file tree
Hide file tree
Showing 21 changed files with 695 additions and 607 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

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

8 changes: 8 additions & 0 deletions crates/kilt-dip-support/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ log.workspace = true

# Internal dependencies
did.workspace = true
pallet-did-lookup.workspace = true
pallet-dip-consumer.workspace = true
pallet-dip-provider.workspace = true
pallet-relay-store.workspace = true
pallet-web3-names.workspace = true

# Parity dependencies
parity-scale-codec = {workspace = true, features = ["derive"]}
Expand All @@ -42,6 +44,8 @@ xcm-executor.workspace = true

# Cumulus dependencies
cumulus-pallet-parachain-system.workspace = true
cumulus-primitives-core.workspace = true
parachain-info.workspace = true

[dev-dependencies]
hex-literal.workspace = true
Expand All @@ -53,9 +57,11 @@ std = [
"hash-db/std",
"log/std",
"did/std",
"pallet-did-lookup/std",
"pallet-dip-consumer/std",
"pallet-dip-provider/std",
"pallet-relay-store/std",
"pallet-web3-names/std",
"parity-scale-codec/std",
"scale-info/std",
"frame-system/std",
Expand All @@ -70,6 +76,8 @@ std = [
"xcm/std",
"xcm-executor/std",
"cumulus-pallet-parachain-system/std",
"cumulus-primitives-core/std",
"parachain-info/std",
]
runtime-benchmarks = [
"pallet-dip-consumer/runtime-benchmarks",
Expand Down
Loading

0 comments on commit 3ce3da6

Please sign in to comment.