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

Poe #1

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
29 changes: 16 additions & 13 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ resolver = "2"
members = [
"node",
"pallets/template",
"pallets/poe",
"runtime",

]
Expand Down
58 changes: 58 additions & 0 deletions pallets/poe/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
[package]
name = "pallet-poe"
description = "FRAME pallet to implement Proof of Existence."
version = "0.0.1"
license = "MIT-0"
authors.workspace = true
homepage.workspace = true
repository.workspace = true
edition.workspace = true
publish = false

[lints]
workspace = true

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
codec = { package = "parity-scale-codec", version = "3.6.1", default-features = false, features = [
"derive",
] }
scale-info = { version = "2.10.0", default-features = false, features = [
"derive",
] }

# frame deps
frame-benchmarking = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-v1.10.0", default-features = false, optional = true }
frame-support = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-v1.10.0", default-features = false }
frame-system = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-v1.10.0", default-features = false }

[dev-dependencies]
sp-core = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-v1.10.0" }
sp-io = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-v1.10.0" }
sp-runtime = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-v1.10.0" }

[features]
default = ["std"]
std = [
"codec/std",
"frame-benchmarking?/std",
"frame-support/std",
"frame-system/std",
"scale-info/std",
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
]
try-runtime = [
"frame-support/try-runtime",
"frame-system/try-runtime",
"sp-runtime/try-runtime",
]
108 changes: 108 additions & 0 deletions pallets/poe/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#![cfg_attr(not(feature = "std"), no_std)]

/// A module for proof of existence
pub use pallet::*;

#[cfg(test)]
mod mock;

#[cfg(test)]
mod tests;

#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;

#[pallet::config]
pub trait Config: frame_system::Config {
/// The maximum length of claim that can be added.
#[pallet::constant]
type MaxClaimLength: Get<u32>;
/// The runtime event
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
}

#[pallet::pallet]
pub struct Pallet<T>(_);

#[pallet::storage]
pub type Proofs<T: Config> = StorageMap<
_,
Blake2_128Concat,
BoundedVec<u8, T::MaxClaimLength>,
(T::AccountId, BlockNumberFor<T>),
>;

#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
ClaimCreated(T::AccountId, BoundedVec<u8, T::MaxClaimLength>),
ClaimRevoked(T::AccountId, BoundedVec<u8, T::MaxClaimLength>),
}

#[pallet::error]
pub enum Error<T> {
ProofAlreadyExist,
ClaimTooLong,
ClaimNotExist,
NotClaimOwner,
}

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}

#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
#[pallet::weight({0})]
pub fn create_claim(origin: OriginFor<T>, claim: BoundedVec<u8, T::MaxClaimLength>) -> DispatchResultWithPostInfo {
let sender = ensure_signed(origin)?;

ensure!(!Proofs::<T>::contains_key(&claim), Error::<T>::ProofAlreadyExist);

Proofs::<T>::insert(
&claim,
(sender.clone(), frame_system::Pallet::<T>::block_number()),
);

Self::deposit_event(Event::ClaimCreated(sender, claim));

Ok(().into())
}

#[pallet::call_index(1)]
#[pallet::weight({0})]
pub fn revoke_claim(origin: OriginFor<T>, claim: BoundedVec<u8, T::MaxClaimLength>) -> DispatchResultWithPostInfo {
let sender = ensure_signed(origin)?;

let (owner, _) = Proofs::<T>::get(&claim).ok_or(Error::<T>::ClaimNotExist)?;
ensure!(owner == sender, Error::<T>::NotClaimOwner);

Proofs::<T>::remove(&claim);

Self::deposit_event(Event::ClaimRevoked(sender, claim));

Ok(().into())
}

#[pallet::call_index(2)]
#[pallet::weight({0})]
pub fn transfer_claim(
origin: OriginFor<T>,
claim: BoundedVec<u8, T::MaxClaimLength>,
dest: T::AccountId,
) -> DispatchResultWithPostInfo {
let sender = ensure_signed(origin)?;

let (owner, _block_number) =
Proofs::<T>::get(&claim).ok_or(Error::<T>::ClaimNotExist)?;
ensure!(owner == sender, Error::<T>::NotClaimOwner);

Proofs::<T>::insert(&claim, (dest, frame_system::Pallet::<T>::block_number()));

Ok(().into())
}
}
}
61 changes: 61 additions & 0 deletions pallets/poe/src/mock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use crate as pallet_poe;
use frame_support::{
derive_impl,
traits::{ConstU16, ConstU32, ConstU64},
};
use sp_core::H256;
use sp_runtime::{
traits::{BlakeTwo256, IdentityLookup},
BuildStorage,
};

type Block = frame_system::mocking::MockBlock<Test>;

// Configure a mock runtime to test the pallet.
frame_support::construct_runtime!(
pub enum Test
{
System: frame_system,
PoeModule: pallet_poe,
}
);

#[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
impl frame_system::Config for Test {
type BaseCallFilter = frame_support::traits::Everything;
type BlockWeights = ();
type BlockLength = ();
type DbWeight = ();
type RuntimeOrigin = RuntimeOrigin;
type RuntimeCall = RuntimeCall;
type Nonce = u64;
type Hash = H256;
type Hashing = BlakeTwo256;
type AccountId = u64;
type Lookup = IdentityLookup<Self::AccountId>;
type Block = Block;
type RuntimeEvent = RuntimeEvent;
type BlockHashCount = ConstU64<250>;
type Version = ();
type PalletInfo = PalletInfo;
type AccountData = ();
type OnNewAccount = ();
type OnKilledAccount = ();
type SystemWeightInfo = ();
type SS58Prefix = ConstU16<42>;
type OnSetCode = ();
type MaxConsumers = frame_support::traits::ConstU32<16>;
}

impl pallet_poe::Config for Test {
type RuntimeEvent = RuntimeEvent;
type MaxClaimLength = ConstU32<10>;
}

// Build genesis storage according to the mock runtime.
pub fn new_test_ext() -> sp_io::TestExternalities {
frame_system::GenesisConfig::<Test>::default()
.build_storage()
.unwrap()
.into()
}
107 changes: 107 additions & 0 deletions pallets/poe/src/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
use super::*;
use crate::{mock::*, Error};
use frame_support::{assert_noop, assert_ok, BoundedVec, pallet_prelude::Get};

#[test]
fn create_claim_works() {
new_test_ext().execute_with(|| {
let claim = BoundedVec::try_from(vec![0, 1]).unwrap();
assert_ok!(PoeModule::create_claim(RuntimeOrigin::signed(1), claim.clone()));

assert_eq!(
Proofs::<Test>::get(&claim),
Some((1, frame_system::Pallet::<Test>::block_number()))
);
assert_eq!(<<Test as Config>::MaxClaimLength as Get<u32>>::get(), 10);
})
}

#[test]
fn create_claim_failed_when_claim_already_exist() {
new_test_ext().execute_with(|| {
let claim = BoundedVec::try_from(vec![0, 1]).unwrap();
let _ = PoeModule::create_claim(RuntimeOrigin::signed(1), claim.clone());

assert_noop!(
PoeModule::create_claim(RuntimeOrigin::signed(1), claim.clone()),
Error::<Test>::ProofAlreadyExist
);
})
}

#[test]
fn revoke_claim_works() {
new_test_ext().execute_with(|| {
let claim = BoundedVec::try_from(vec![0, 1]).unwrap();
let _ = PoeModule::create_claim(RuntimeOrigin::signed(1), claim.clone());

assert_ok!(PoeModule::revoke_claim(RuntimeOrigin::signed(1), claim.clone()));
})
}

#[test]
fn revoke_claim_failed_when_claim_is_not_exist() {
new_test_ext().execute_with(|| {
let claim = BoundedVec::try_from(vec![0, 1]).unwrap();

assert_noop!(
PoeModule::revoke_claim(RuntimeOrigin::signed(1), claim.clone()),
Error::<Test>::ClaimNotExist
);
})
}

#[test]
fn revoke_claim_failed_with_wrong_owner() {
new_test_ext().execute_with(|| {
let claim = BoundedVec::try_from(vec![0, 1]).unwrap();
let _ = PoeModule::create_claim(RuntimeOrigin::signed(1), claim.clone());

assert_noop!(
PoeModule::revoke_claim(RuntimeOrigin::signed(2), claim.clone()),
Error::<Test>::NotClaimOwner
);
})
}

#[test]
fn transfer_claim_works() {
new_test_ext().execute_with(|| {
let claim = BoundedVec::try_from(vec![0, 1]).unwrap();
let _ = PoeModule::create_claim(RuntimeOrigin::signed(1), claim.clone());

assert_ok!(PoeModule::transfer_claim(RuntimeOrigin::signed(1), claim.clone(), 2));

let bounded_claim =
BoundedVec::<u8, <Test as Config>::MaxClaimLength>::try_from(claim.clone()).unwrap();
assert_eq!(
Proofs::<Test>::get(&bounded_claim),
Some((2, frame_system::Pallet::<Test>::block_number()))
);
})
}

#[test]
fn transfer_claim_failed_when_claim_is_not_exist() {
new_test_ext().execute_with(|| {
let claim = BoundedVec::try_from(vec![0, 1]).unwrap();

assert_noop!(
PoeModule::transfer_claim(RuntimeOrigin::signed(1), claim.clone(), 2),
Error::<Test>::ClaimNotExist
);
})
}

#[test]
fn transfer_claim_failed_with_wrong_owner() {
new_test_ext().execute_with(|| {
let claim = BoundedVec::try_from(vec![0, 1]).unwrap();
let _ = PoeModule::create_claim(RuntimeOrigin::signed(1), claim.clone());

assert_noop!(
PoeModule::transfer_claim(RuntimeOrigin::signed(2), claim.clone(), 3),
Error::<Test>::NotClaimOwner
);
})
}
Loading