-
Notifications
You must be signed in to change notification settings - Fork 329
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement
Capability{Reader,Keeper}
traits (#1826)
* Impl Display for Path types instead of enum variants * Fix no-std build * Add CapabilityKeeper, CapabilityReader and PortKeeper traits * Remove Ics26Context clone * Fix build * Add unclog entry * Add comments * Update Mock impl * Apply suggestions from code review Co-authored-by: Romain Ruetschi <romain@informal.systems> * Use .to_string() instead of format!() * Add CapabilityName newtype * Simplify mock PortReader impl * Fix validation check for CapabilityName Co-authored-by: Romain Ruetschi <romain@informal.systems>
- Loading branch information
Showing
10 changed files
with
198 additions
and
81 deletions.
There are no files selected for viewing
2 changes: 2 additions & 0 deletions
2
.changelog/unreleased/improvements/ibc/1769-cap-reader-keeper.md
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
- Define CapabilityReader and CapabilityKeeper traits | ||
([#1769](https://github.com/informalsystems/ibc-rs/issues/1769)) |
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
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,11 +1,74 @@ | ||
use crate::core::ics05_port::capabilities::Capability; | ||
use crate::core::ics05_port::capabilities::{Capability, CapabilityName}; | ||
use crate::core::ics05_port::error::Error; | ||
use crate::core::ics24_host::identifier::PortId; | ||
use crate::core::ics24_host::path::PortsPath; | ||
use crate::prelude::*; | ||
|
||
// A context supplying all the necessary read-only dependencies for processing any information regarding a port. | ||
pub trait PortReader { | ||
fn lookup_module_by_port(&self, port_id: &PortId) -> Result<Capability, Error>; | ||
fn authenticate(&self, key: &Capability, port_id: &PortId) -> bool; | ||
/// A context supplying all the necessary read-only dependencies for processing any information regarding a port. | ||
pub trait PortReader: CapabilityReader { | ||
/// Module Id type that can be mapped to an ICS26 router callback module | ||
type ModuleId; | ||
|
||
/// Return the module_id along with the capability associated with a given port_id | ||
fn lookup_module_by_port( | ||
&self, | ||
port_id: &PortId, | ||
) -> Result<(Self::ModuleId, Capability), Error>; | ||
|
||
/// Check if the specified port_id is already bounded | ||
fn is_bound(&self, port_id: PortId) -> bool { | ||
self.get_capability(&Self::port_capability_name(port_id)) | ||
.is_ok() | ||
} | ||
|
||
/// Authenticate a capability key against a port_id by checking if the capability was previously | ||
/// generated and bound to the specified port | ||
fn authenticate(&self, port_id: PortId, capability: &Capability) -> bool { | ||
self.authenticate_capability(&Self::port_capability_name(port_id), capability) | ||
.is_ok() | ||
} | ||
|
||
fn port_capability_name(port_id: PortId) -> CapabilityName { | ||
PortsPath(port_id) | ||
.to_string() | ||
.parse() | ||
.expect("PortsPath cannot be empty string") | ||
} | ||
} | ||
|
||
pub trait PortKeeper: CapabilityKeeper + PortReader { | ||
/// Binds to a port and returns the associated capability | ||
fn bind_port(&mut self, port_id: PortId) -> Result<Capability, Error> { | ||
if self.is_bound(port_id.clone()) { | ||
Err(Error::port_already_bound(port_id)) | ||
} else { | ||
self.new_capability(Self::port_capability_name(port_id)) | ||
} | ||
} | ||
} | ||
|
||
// Result<Capability, Error>//return Ok(Capability::new()); | ||
pub trait CapabilityKeeper { | ||
/// Create a new capability with the given name. | ||
/// Return an error if the capability was already taken. | ||
fn new_capability(&mut self, name: CapabilityName) -> Result<Capability, Error>; | ||
|
||
/// Claim the specified capability using the specified name. | ||
/// Return an error if the capability was already taken. | ||
fn claim_capability(&mut self, name: CapabilityName, capability: Capability); | ||
|
||
/// Release a previously claimed or created capability | ||
fn release_capability(&mut self, name: CapabilityName, capability: Capability); | ||
} | ||
|
||
pub trait CapabilityReader { | ||
/// Fetch a capability which was previously claimed by specified name | ||
fn get_capability(&self, name: &CapabilityName) -> Result<Capability, Error>; | ||
|
||
/// Authenticate a given capability and name. Lookup the capability from the internal store and | ||
/// check against the provided name. | ||
fn authenticate_capability( | ||
&self, | ||
name: &CapabilityName, | ||
capability: &Capability, | ||
) -> Result<(), Error>; | ||
} |
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
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 |
---|---|---|
|
@@ -16,6 +16,5 @@ pub trait Ics26Context: | |
+ ChannelReader | ||
+ PortReader | ||
+ Ics20Context | ||
+ Clone | ||
{ | ||
} |
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
Oops, something went wrong.