-
Notifications
You must be signed in to change notification settings - Fork 47
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
feat: revoke delegations #15
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
|
||
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}; | ||
|
@@ -89,32 +90,107 @@ decl_module! { | |
Some(p) => { | ||
if <Delegations<T>>::exists(p) { | ||
let parent = <Delegations<T>>::get(p.clone()); | ||
if parent.2 != sender { | ||
if !parent.2.eq(&sender) { | ||
return Err("not owner of parent") | ||
} else if parent.3 & Permissions::DELEGATE != Permissions::DELEGATE { | ||
} else if (parent.3 & Permissions::DELEGATE) != Permissions::DELEGATE { | ||
return Err("not authorized to delegate") | ||
} else { | ||
// TODO: check for cycles | ||
// 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)); | ||
Self::add_child(delegation_id.clone(), p.clone()); | ||
} | ||
} else { | ||
return Err("parent not found") | ||
} | ||
}, | ||
None => { | ||
if root.1 != sender { | ||
if !root.1.eq(&sender) { | ||
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)); | ||
Self::add_child(delegation_id.clone(), root_id.clone()); | ||
} | ||
} | ||
} else { | ||
return Err("root not found") | ||
} | ||
return Ok(()); | ||
} | ||
|
||
pub fn revoke_root(origin, root_id: T::DelegationNodeId) -> Result { | ||
let sender = ensure_signed(origin)?; | ||
if !<Root<T>>::exists(root_id) { | ||
return Err("root not found") | ||
} | ||
let mut r = <Root<T>>::get(root_id.clone()); | ||
if !r.1.eq(&sender) { | ||
return Err("not permitted to revoke") | ||
} | ||
if !r.2 { | ||
r.2 = false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
<Root<T>>::insert(root_id.clone(), r); | ||
Self::revoke_children(&root_id); | ||
} | ||
|
||
return Ok(()); | ||
} | ||
|
||
pub fn revoke_delegation(origin, delegation_id: T::DelegationNodeId) -> Result { | ||
let sender = ensure_signed(origin)?; | ||
if !Self::is_delegating(&sender, &delegation_id)? { | ||
return Err("not permitted to revoke") | ||
} | ||
Self::revoke(&delegation_id); | ||
return Ok(()); | ||
} | ||
} | ||
} | ||
|
||
impl<T: Trait> Module<T> { | ||
pub fn is_delegating(account: &T::AccountId, delegation: &T::DelegationNodeId) -> result::Result<bool, &'static str> { | ||
if !<Delegations<T>>::exists(delegation) { | ||
return Err("delegation not found") | ||
} | ||
let d = <Delegations<T>>::get(delegation); | ||
if d.2.eq(account) { | ||
Ok(true) | ||
} else { | ||
match d.1 { | ||
None => { | ||
let r = <Root<T>>::get(d.0.clone()); | ||
Ok(r.1.eq(account)) | ||
}, | ||
Some(p) => { | ||
return Self::is_delegating(account, &p) | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn revoke(delegation: &T::DelegationNodeId) { | ||
let mut d = <Delegations<T>>::get(delegation.clone()); | ||
if !d.4 { | ||
d.4 = false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
<Delegations<T>>::insert(delegation.clone(), d); | ||
Self::revoke_children(delegation); | ||
} | ||
} | ||
|
||
fn revoke_children(delegation: &T::DelegationNodeId) { | ||
if <Children<T>>::exists(delegation) { | ||
let children = <Children<T>>::get(delegation); | ||
for child in children { | ||
Self::revoke(&child); | ||
} | ||
} | ||
} | ||
|
||
fn add_child(child: T::DelegationNodeId, parent: T::DelegationNodeId) { | ||
let mut children = <Children<T>>::get(parent.clone()); | ||
children.push(child); | ||
<Children<T>>::insert(parent, children); | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
&& !v.3
redundant. Already checked.