Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migration to Substrate V3 #15

Merged
merged 3 commits into from
Mar 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
**/*.rs.bk

Cargo.lock

*.iml

.idea
20 changes: 10 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pallet-did"
version = '2.0.0'
version = '3.0.0'
description = 'Substrate Decentralized ID Pallet'
edition = '2018'
authors = ['Substrate DevHub <https://github.com/substrate-developer-hub>']
Expand All @@ -9,16 +9,16 @@ repository = 'https://github.com/paritytech/substrate/'
license = 'Unlicense'

[dependencies]
codec = { default-features = false, features = ['derive'], package = 'parity-scale-codec', version = '1.3.4' }
serde = { features = ['derive'], optional = true, version = '1.0.114' }
codec = { default-features = false, features = ['derive'], package = 'parity-scale-codec', version = '2.0.0' }
serde = { features = ['derive'], optional = true, version = '1.0.119' }

frame-support = { default-features = false, version = '2.0.0' }
frame-system = { default-features = false, version = '2.0.0' }
pallet-timestamp = { default_features = false, version = '2.0.0' }
sp-core = { default-features = false, version = '2.0.0' }
sp-io = { default-features = false, version = '2.0.0' }
sp-runtime = { default-features = false, version = '2.0.0' }
sp-std = { default-features = false, version = '2.0.0' }
frame-support = { default-features = false, version = '3.0.0' }
frame-system = { default-features = false, version = '3.0.0' }
pallet-timestamp = { default_features = false, version = '3.0.0' }
sp-core = { default-features = false, version = '3.0.0' }
sp-io = { default-features = false, version = '3.0.0' }
sp-runtime = { default-features = false, version = '3.0.0' }
sp-std = { default-features = false, version = '3.0.0' }

[features]
default = ['std']
Expand Down
2 changes: 1 addition & 1 deletion HOWTO.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Next we will update `my-node/runtime/src/lib.rs` to actually use our new runtime

``` rust
// add the following code block
impl pallet_did::Trait for Runtime {
impl pallet_did::Config for Runtime {
type Event = Event;
type Public = sp_runtime::MultiSigner;
type Signature = Signature;
Expand Down
27 changes: 14 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,32 +99,32 @@ mod mock;
#[cfg(test)]
mod tests;

pub trait Trait: frame_system::Trait {
type Event: From<Event<Self>> + Into<<Self as frame_system::Trait>::Event>;
pub trait Config: frame_system::Config {
type Event: From<Event<Self>> + Into<<Self as frame_system::Config>::Event>;
type Public: IdentifyAccount<AccountId = Self::AccountId>;
type Signature: Verify<Signer = Self::Public> + Member + Decode + Encode;
type Time: Time;
}

decl_storage! {
trait Store for Module<T: Trait> as DID {
trait Store for Module<T: Config> as DID {
/// Identity delegates stored by type.
/// Delegates are only valid for a specific period defined as blocks number.
pub DelegateOf get(fn delegate_of): map hasher(blake2_128_concat) (T::AccountId, Vec<u8>, T::AccountId) => Option<T::BlockNumber>;
/// The attributes that belong to an identity.
/// Attributes are only valid for a specific period defined as blocks number.
pub AttributeOf get(fn attribute_of): map hasher(blake2_128_concat) (T::AccountId, [u8; 32]) => Attribute<T::BlockNumber, <<T as Trait>::Time as Time>::Moment>;
pub AttributeOf get(fn attribute_of): map hasher(blake2_128_concat) (T::AccountId, [u8; 32]) => Attribute<T::BlockNumber, <<T as Config>::Time as Time>::Moment>;
/// Attribute nonce used to generate a unique hash even if the attribute is deleted and recreated.
pub AttributeNonce get(fn nonce_of): map hasher(twox_64_concat) (T::AccountId, Vec<u8>) => u64;
/// Identity owner.
pub OwnerOf get(fn owner_of): map hasher(blake2_128_concat) T::AccountId => Option<T::AccountId>;
/// Tracking the latest identity update.
pub UpdatedBy get(fn updated_by): map hasher(blake2_128_concat) T::AccountId => (T::AccountId, T::BlockNumber, <<T as Trait>::Time as Time>::Moment);
pub UpdatedBy get(fn updated_by): map hasher(blake2_128_concat) T::AccountId => (T::AccountId, T::BlockNumber, <<T as Config>::Time as Time>::Moment);
}
}

decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
pub struct Module<T: Config> for enum Call where origin: T::Origin {
type Error = Error<T>;

fn deposit_event() = default;
Expand Down Expand Up @@ -295,9 +295,9 @@ decl_module! {
decl_event!(
pub enum Event<T>
where
<T as frame_system::Trait>::AccountId,
<T as frame_system::Trait>::BlockNumber,
<T as Trait>::Signature
<T as frame_system::Config>::AccountId,
<T as frame_system::Config>::BlockNumber,
<T as Config>::Signature
{
OwnerChanged(AccountId, AccountId, AccountId, BlockNumber),
DelegateAdded(AccountId, Vec<u8>, AccountId, Option<BlockNumber>),
Expand All @@ -310,7 +310,7 @@ decl_event!(
);

decl_error! {
pub enum Error for Module<T: Trait> {
pub enum Error for Module<T: Config> {
NotOwner,
InvalidDelegate,
BadSignature,
Expand All @@ -323,7 +323,8 @@ decl_error! {
}
}

impl<T: Trait> Did<T::AccountId, T::BlockNumber, <<T as Trait>::Time as Time>::Moment, T::Signature>
impl<T: Config>
Did<T::AccountId, T::BlockNumber, <<T as Config>::Time as Time>::Moment, T::Signature>
for Module<T>
{
/// Validates if the AccountId 'actual_owner' owns the identity.
Expand Down Expand Up @@ -515,7 +516,7 @@ impl<T: Trait> Did<T::AccountId, T::BlockNumber, <<T as Trait>::Time as Time>::M
fn attribute_and_id(
identity: &T::AccountId,
name: &[u8],
) -> Option<AttributedId<T::BlockNumber, <<T as Trait>::Time as Time>::Moment>> {
) -> Option<AttributedId<T::BlockNumber, <<T as Config>::Time as Time>::Moment>> {
let nonce = Self::nonce_of((&identity, name.to_vec()));

// Used for first time attribute creation
Expand All @@ -536,7 +537,7 @@ impl<T: Trait> Did<T::AccountId, T::BlockNumber, <<T as Trait>::Time as Time>::M
}
}

impl<T: Trait> Module<T> {
impl<T: Config> Module<T> {
/// Creates a new attribute from a off-chain transaction.
fn signed_attribute(
who: T::AccountId,
Expand Down
100 changes: 64 additions & 36 deletions src/mock.rs
Original file line number Diff line number Diff line change
@@ -1,77 +1,105 @@
use crate::{Module, Trait};
use frame_support::{impl_outer_origin, parameter_types, weights::Weight};
use crate as pallet_did;

use frame_support::parameter_types;
use frame_system as system;
use pallet_timestamp as timestamp;
use sp_core::{sr25519, Pair, H256};
use sp_runtime::{
testing::Header,
traits::{BlakeTwo256, IdentityLookup},
Perbill,
traits::{BlakeTwo256, Extrinsic as ExtrinsicT, IdentifyAccount, IdentityLookup, Verify},
};

impl_outer_origin! {
pub enum Origin for Test {}
}
type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
type Block = frame_system::mocking::MockBlock<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::{Module, Call, Config, Storage, Event<T>},
DID: pallet_did::{Module, Call, Storage, Event<T>},
Timestamp: pallet_timestamp::{Module, Call, Storage, Inherent},
}
);

// For testing the pallet, we construct most of a mock runtime. This means
// first constructing a configuration type (`Test`) which `impl`s each of the
// configuration traits of modules we want to use.
#[derive(Clone, Eq, PartialEq)]
pub struct Test;
parameter_types! {
pub const BlockHashCount: u64 = 250;
pub const MaximumBlockWeight: Weight = 1024;
pub const MaximumBlockLength: u32 = 2 * 1024;
pub const AvailableBlockRatio: Perbill = Perbill::from_percent(75);
pub const BlockHashCount: u64 = 250;
pub const SS58Prefix: u8 = 42;
}

impl system::Trait for Test {
impl system::Config for Test {
type BaseCallFilter = ();
type BlockWeights = ();
type BlockLength = ();
type DbWeight = ();
type Origin = Origin;
type Call = ();
type Call = Call;
type Index = u64;
type BlockNumber = u64;
type Hash = H256;
type Hashing = BlakeTwo256;
type AccountId = sr25519::Public;
type AccountId = sp_core::sr25519::Public;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header;
type Event = ();
type Event = Event;
type BlockHashCount = BlockHashCount;
type MaximumBlockWeight = MaximumBlockWeight;
type DbWeight = ();
type BlockExecutionWeight = ();
type ExtrinsicBaseWeight = ();
type MaximumExtrinsicWeight = MaximumBlockWeight;
type MaximumBlockLength = MaximumBlockLength;
type AvailableBlockRatio = AvailableBlockRatio;
type Version = ();
type PalletInfo = ();
type PalletInfo = PalletInfo;
type AccountData = ();
type OnNewAccount = ();
type OnKilledAccount = ();
type SystemWeightInfo = ();
type SS58Prefix = SS58Prefix;
}

impl frame_system::offchain::SigningTypes for Test {
type Public = <sr25519::Signature as Verify>::Signer;
type Signature = sr25519::Signature;
}

type Extrinsic = sp_runtime::testing::TestXt<Call, ()>;
type AccountId = <<sp_core::sr25519::Signature as Verify>::Signer as IdentifyAccount>::AccountId;

impl<LocalCall> frame_system::offchain::SendTransactionTypes<LocalCall> for Test
where
Call: From<LocalCall>,
{
type OverarchingCall = Call;
type Extrinsic = Extrinsic;
}

impl timestamp::Trait for Test {
impl<LocalCall> frame_system::offchain::CreateSignedTransaction<LocalCall> for Test
where
Call: From<LocalCall>,
{
fn create_transaction<C: frame_system::offchain::AppCrypto<Self::Public, Self::Signature>>(
call: Call,
_public: <sr25519::Signature as Verify>::Signer,
_account: AccountId,
nonce: u64,
) -> Option<(Call, <Extrinsic as ExtrinsicT>::SignaturePayload)> {
Some((call, (nonce, ())))
}
}

impl timestamp::Config for Test {
type Moment = u64;
type OnTimestampSet = ();
type MinimumPeriod = ();
type WeightInfo = ();
}

impl Trait for Test {
type Event = ();
impl pallet_did::Config for Test {
type Event = Event;
type Public = sr25519::Public;
type Signature = sr25519::Signature;
type Time = pallet_timestamp::Module<Test>;
}

pub type DID = Module<Test>;
pub type System = system::Module<Test>;

// This function basically just builds a genesis storage key/value store according to
// our desired mockup.
// Build genesis storage according to the mock runtime.
pub fn new_test_ext() -> sp_io::TestExternalities {
system::GenesisConfig::default()
.build_storage::<Test>()
Expand Down
3 changes: 2 additions & 1 deletion src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{did::Did, mock::*, AttributeTransaction, Error};
use crate::did::Did;
use crate::{mock::*, AttributeTransaction, Error};
use codec::Encode;
use frame_support::{assert_noop, assert_ok};
use sp_core::Pair;
Expand Down