Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
feat: chain events and ctype storage
  • Loading branch information
bekolb committed Apr 2, 2019
commit 75c29a7ab1f76c17c88dd1432c04b304caef89bc
22 changes: 21 additions & 1 deletion runtime/src/attestation.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@

use rstd::result;
use rstd::prelude::*;
use support::{dispatch::Result, StorageMap, decl_module, decl_storage};
use support::{dispatch::Result, StorageMap, decl_module, decl_storage, decl_event};
use {system, super::delegation, super::ctype, system::ensure_signed};

pub trait Trait: system::Trait + delegation::Trait {
type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;
}

decl_event!(
pub enum Event<T> where <T as system::Trait>::AccountId, <T as system::Trait>::Hash,
<T as delegation::Trait>::DelegationNodeId {
/// An attestation has been added
AttestationCreated(AccountId, Hash, Hash, Option<DelegationNodeId>),
/// An attestation has been revoked
AttestationRevoked(AccountId, Hash),
}
);

decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {

fn deposit_event<T>() = default;

pub fn add(origin, claim_hash: T::Hash, ctype_hash: T::Hash, delegation_id: Option<T::DelegationNodeId>) -> Result {
let sender = ensure_signed(origin)?;
if !<ctype::CTYPEs<T>>::exists(ctype_hash) {
Expand Down Expand Up @@ -52,6 +65,9 @@ decl_module! {
},
None => {}
}

Self::deposit_event(RawEvent::AttestationCreated(sender.clone(), claim_hash.clone(),
ctype_hash.clone(), delegation_id.clone()));
Ok(())
}

Expand Down Expand Up @@ -83,6 +99,7 @@ decl_module! {
::runtime_io::print("revoking Attestation");
existing_attestation.3 = true;
<Attestations<T>>::insert(claim_hash.clone(), existing_attestation.clone());
Self::deposit_event(RawEvent::AttestationRevoked(sender.clone(), claim_hash.clone()));
Ok(())
}
}
Expand Down Expand Up @@ -139,9 +156,11 @@ mod tests {
}

impl ctype::Trait for Test {
type Event = ();
}

impl delegation::Trait for Test {
type Event = ();
type Signature = x25519::Signature;
type Signer = <x25519::Signature as Verify>::Signer;
type DelegationNodeId = H256;
Expand All @@ -151,6 +170,7 @@ mod tests {
}

impl Trait for Test {
type Event = ();
}

type Attestation = Module<Test>;
Expand Down
30 changes: 23 additions & 7 deletions runtime/src/ctype.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,34 @@
// initialise with:
// post({sender: runtime.balances.ss58Decode('F7Gh'), call: calls.demo.setPayment(1000)}).tie(console.log)

use support::{dispatch::Result, StorageMap, decl_module, decl_storage};
use support::{dispatch::Result, StorageMap, decl_module, decl_storage, decl_event};
use {system, system::ensure_signed};

pub trait Trait: system::Trait {
type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;
}

decl_event!(
pub enum Event<T> where <T as system::Trait>::AccountId, <T as system::Trait>::Hash {
/// A CTYPE has been added
CTypeCreated(AccountId, Hash),
}
);

decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {

fn deposit_event<T>() = default;

pub fn add(origin, hash: T::Hash) -> Result {
if <CTYPEs<T>>::exists(hash) {
return Err("CTYPE already exists")
}

let sender = ensure_signed(origin)?;
::runtime_io::print("insert CTYPE");
<CTYPEs<T>>::insert(hash.clone(), (hash.clone(), sender.clone()));
<CTYPEs<T>>::insert(hash.clone(), sender.clone());
Self::deposit_event(RawEvent::CTypeCreated(sender.clone(), hash.clone()));
Ok(())
}

Expand All @@ -26,7 +37,7 @@ decl_module! {

decl_storage! {
trait Store for Module<T: Trait> as Ctype {
pub CTYPEs get(ctypes): map T::Hash => (T::Hash,T::AccountId);
pub CTYPEs get(ctypes): map T::Hash => T::AccountId;
}
}

Expand Down Expand Up @@ -65,6 +76,7 @@ mod tests {
}

impl Trait for Test {
type Event = ();
}
type CType = Module<Test>;

Expand All @@ -75,16 +87,20 @@ mod tests {
#[test]
fn it_works_for_default_value() {
with_externalities(&mut new_test_ext(), || {
let account = H256::from_low_u64_be(1);
let ctype_hash = H256::from_low_u64_be(2);
assert_ok!(
CType::add(
Origin::signed(H256::from_low_u64_be(1)),
H256::from_low_u64_be(2)
Origin::signed(account.clone()),
ctype_hash.clone()
)
);
assert_eq!(<CTYPEs<Test>>::exists(ctype_hash), true);
assert_eq!(CType::ctypes(ctype_hash.clone()), account.clone());
assert_err!(
CType::add(
Origin::signed(H256::from_low_u64_be(1)),
H256::from_low_u64_be(2)
Origin::signed(account.clone()),
ctype_hash.clone()
),
"CTYPE already exists"
);
Expand Down
49 changes: 40 additions & 9 deletions runtime/src/delegation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use rstd::result;
use rstd::prelude::*;
use runtime_primitives::traits::{Hash, CheckEqual, SimpleBitOps, Member, Verify, MaybeDisplay};
use support::{dispatch::Result, StorageMap, Parameter, decl_module, decl_storage};
use support::{dispatch::Result, StorageMap, Parameter, decl_module, decl_storage, decl_event};
use parity_codec::{Encode, Decode};
use core::default::Default;

Expand Down Expand Up @@ -38,6 +38,8 @@ impl Default for Permissions {
}

pub trait Trait: ctype::Trait + system::Trait {
type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;

type Signer: From<Self::AccountId> + Member + Codec;
type Signature: Verify<Signer = Self::Signer> + Member + Codec + Default;
type DelegationNodeId: Parameter + Member + Codec + MaybeDisplay + SimpleBitOps
Expand All @@ -46,8 +48,27 @@ pub trait Trait: ctype::Trait + system::Trait {
fn print_hash(hash: Self::Hash);
}


decl_event!(
pub enum Event<T> where <T as system::Trait>::Hash, <T as system::Trait>::AccountId,
<T as Trait>::DelegationNodeId {
/// A new root has been created
RootCreated(AccountId, DelegationNodeId, Hash),
/// A root has been revoked
RootRevoked(AccountId, DelegationNodeId),
/// A new delegation has been created
DelegationCreated(AccountId, DelegationNodeId, DelegationNodeId, Option<DelegationNodeId>,
AccountId, Permissions),
/// A delegation has been revoked
DelegationRevoked(AccountId, DelegationNodeId),
}
);

decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {

fn deposit_event<T>() = default;

pub fn create_root(origin, root_id: T::DelegationNodeId, ctype_hash: T::Hash) -> Result {
let sender = ensure_signed(origin)?;
if <Root<T>>::exists(root_id) {
Expand All @@ -59,6 +80,7 @@ decl_module! {

::runtime_io::print("insert Delegation Root");
<Root<T>>::insert(root_id.clone(), (ctype_hash.clone(), sender.clone(), false));
Self::deposit_event(RawEvent::RootCreated(sender.clone(), root_id.clone(), ctype_hash.clone()));
return Ok(());
}

Expand Down Expand Up @@ -91,7 +113,8 @@ decl_module! {
} else {
// TODO: check for cycles?
::runtime_io::print("insert Delegation with parent");
<Delegations<T>>::insert(delegation_id.clone(), (root_id.clone(), Some(p.clone()), delegate, permissions, false));
<Delegations<T>>::insert(delegation_id.clone(), (root_id.clone(),
Some(p.clone()), delegate.clone(), permissions, false));
Self::add_child(delegation_id.clone(), p.clone());
}
} else {
Expand All @@ -103,13 +126,16 @@ decl_module! {
return Err("not owner of root")
}
::runtime_io::print("insert Delegation without parent");
<Delegations<T>>::insert(delegation_id.clone(), (root_id.clone(), None, delegate, permissions, false));
<Delegations<T>>::insert(delegation_id.clone(), (root_id.clone(),
None, delegate.clone(), permissions, false));
Self::add_child(delegation_id.clone(), root_id.clone());
}
}
} else {
return Err("root not found")
}
Self::deposit_event(RawEvent::DelegationCreated(sender.clone(), delegation_id.clone(),
root_id.clone(), parent_id.clone(), delegate.clone(), permissions.clone()));
return Ok(());
}

Expand All @@ -125,9 +151,10 @@ decl_module! {
if !r.2 {
r.2 = true;
<Root<T>>::insert(root_id.clone(), r);
Self::revoke_children(&root_id);
Self::revoke_children(&root_id, &sender);
}

Self::deposit_event(RawEvent::RootRevoked(sender.clone(), root_id.clone()));
return Ok(());
}

Expand All @@ -139,7 +166,7 @@ decl_module! {
if !Self::is_delegating(&sender, &delegation_id)? {
return Err("not permitted to revoke")
}
Self::revoke(&delegation_id);
Self::revoke(&delegation_id, &sender);
return Ok(());
}
}
Expand Down Expand Up @@ -181,20 +208,22 @@ impl<T: Trait> Module<T> {
}
}

fn revoke(delegation: &T::DelegationNodeId) {
fn revoke(delegation: &T::DelegationNodeId, sender: &T::AccountId) {
let mut d = <Delegations<T>>::get(delegation.clone());
if !d.4 {
d.4 = true;
<Delegations<T>>::insert(delegation.clone(), d);
Self::revoke_children(delegation);
Self::deposit_event(RawEvent::DelegationRevoked(sender.clone(), delegation.clone()));

Self::revoke_children(delegation, sender);
}
}

fn revoke_children(delegation: &T::DelegationNodeId) {
fn revoke_children(delegation: &T::DelegationNodeId, sender: &T::AccountId) {
if <Children<T>>::exists(delegation) {
let children = <Children<T>>::get(delegation);
for child in children {
Self::revoke(&child);
Self::revoke(&child, sender);
}
}
}
Expand Down Expand Up @@ -254,9 +283,11 @@ mod tests {
}

impl ctype::Trait for Test {
type Event = ();
}

impl Trait for Test {
type Event = ();
type Signature = x25519::Signature;
type Signer = <x25519::Signature as Verify>::Signer;
type DelegationNodeId = H256;
Expand Down
17 changes: 16 additions & 1 deletion runtime/src/did.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,41 @@

use rstd::prelude::*;
use runtime_primitives::traits::{Member};
use support::{dispatch::Result, StorageMap, Parameter, decl_module, decl_storage};
use support::{dispatch::Result, StorageMap, Parameter, decl_module, decl_storage, decl_event};
use runtime_primitives::codec::Codec;
use {system, system::ensure_signed};

pub trait Trait: system::Trait {
type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;
type PublicSigningKey : Parameter + Member + Codec + Default;
type PublicBoxKey : Parameter + Member + Codec + Default;
}

decl_event!(
pub enum Event<T> where <T as system::Trait>::AccountId {
/// A did has been created
DidCreated(AccountId),
/// A did has been removed
DidRemoved(AccountId),
}
);

decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {

fn deposit_event<T>() = default;

pub fn add(origin, sign_key: T::PublicSigningKey, box_key: T::PublicBoxKey, doc_ref: Option<Vec<u8>>) -> Result {
let sender = ensure_signed(origin)?;
<DIDs<T>>::insert(sender.clone(), (sign_key, box_key, doc_ref));
Self::deposit_event(RawEvent::DidCreated(sender.clone()));
Ok(())
}

pub fn remove(origin) -> Result {
let sender = ensure_signed(origin)?;
<DIDs<T>>::remove(sender.clone());
Self::deposit_event(RawEvent::DidRemoved(sender.clone()));
Ok(())
}
}
Expand Down Expand Up @@ -70,6 +84,7 @@ mod tests {
}

impl Trait for Test {
type Event = ();
type PublicSigningKey = H256;
type PublicBoxKey = H256;
}
Expand Down
13 changes: 9 additions & 4 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,15 @@ impl sudo::Trait for Runtime {
}

impl attestation::Trait for Runtime {
type Event = Event;
}

impl ctype::Trait for Runtime {
type Event = Event;
}

impl delegation::Trait for Runtime {
type Event = Event;
type Signer = AccountId;
type Signature = AccountSignature;
type DelegationNodeId = Hash;
Expand All @@ -204,6 +207,8 @@ impl delegation::Trait for Runtime {
}

impl did::Trait for Runtime {
/// The uniquitous event type.
type Event = Event;
type PublicSigningKey = Hash;
type PublicBoxKey = Hash;
}
Expand All @@ -221,10 +226,10 @@ construct_runtime!(
Indices: indices,
Balances: balances,
Sudo: sudo,
Ctype: ctype::{Module, Call, Storage},
Attestation: attestation::{Module, Call, Storage},
Delegation: delegation::{Module, Call, Storage},
Did: did::{Module, Call, Storage},
Ctype: ctype::{Module, Call, Storage, Event<T>},
Attestation: attestation::{Module, Call, Storage, Event<T>},
Delegation: delegation::{Module, Call, Storage, Event<T>},
Did: did::{Module, Call, Storage, Event<T>},
}
);

Expand Down