Skip to content

Commit

Permalink
Implements Connection and Channel messages (#86)
Browse files Browse the repository at this point in the history
* made Msg trait global

* MsgConnectionOpenTry implementation

* Connection msgs skeleton

* channel msgs skeleton

* validation fixes

* Added tests for connection and channel msgs

* fix clippy error

* fix further errors

* review suggestions
  • Loading branch information
Shivani912 committed Jun 16, 2020
1 parent ba810c3 commit f73d832
Show file tree
Hide file tree
Showing 10 changed files with 1,865 additions and 122 deletions.
56 changes: 33 additions & 23 deletions modules/src/ics03_connection/connection.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use super::exported::*;
use crate::ics03_connection::error;
use crate::ics03_connection::error::Kind;
use crate::ics03_connection::error::{Error, Kind};
use crate::ics23_commitment::CommitmentPrefix;
use crate::ics24_host::identifier::{ClientId, ConnectionId};
use serde_derive::{Deserialize, Serialize};
Expand All @@ -14,18 +13,22 @@ pub struct ConnectionEnd {
}

impl ConnectionEnd {
pub fn new(client_id: ClientId, counterparty: Counterparty, versions: Vec<String>) -> Self {
ConnectionEnd {
pub fn new(
client_id: ClientId,
counterparty: Counterparty,
versions: Vec<String>,
) -> Result<Self, Error> {
Ok(Self {
state: State::Uninitialized,
client_id,
counterparty,
versions,
}
versions: validate_versions(versions).map_err(|e| Kind::InvalidVersion.context(e))?,
})
}
}

impl Connection for ConnectionEnd {
type ValidationError = error::Error;
type ValidationError = Error;

fn state(&self) -> &State {
&self.state
Expand All @@ -46,19 +49,6 @@ impl Connection for ConnectionEnd {
}

fn validate_basic(&self) -> Result<(), Self::ValidationError> {
if self.versions.is_empty() {
return Err(error::Kind::InvalidVersion
.context("missing connection versions")
.into());
}

for v in self.versions().iter() {
if v.trim().is_empty() {
return Err(error::Kind::InvalidVersion
.context("empty version string")
.into());
}
}
self.counterparty().validate_basic()
}
}
Expand All @@ -75,7 +65,7 @@ impl Counterparty {
client_id: String,
connection_id: String,
prefix: CommitmentPrefix,
) -> Result<Self, error::Error> {
) -> Result<Self, Error> {
Ok(Self {
client_id: client_id
.parse()
Expand All @@ -89,7 +79,7 @@ impl Counterparty {
}

impl ConnectionCounterparty for Counterparty {
type ValidationError = error::Error;
type ValidationError = Error;

fn client_id(&self) -> String {
self.client_id.as_str().into()
Expand All @@ -104,6 +94,26 @@ impl ConnectionCounterparty for Counterparty {
}

fn validate_basic(&self) -> Result<(), Self::ValidationError> {
todo!()
// todo!()
Ok(())
}
}

pub fn validate_versions(versions: Vec<String>) -> Result<Vec<String>, String> {
let v: Vec<String> = versions.to_vec();
if v.is_empty() {
return Err("missing versions".to_string());
}

for v in versions.into_iter() {
validate_version(v)?;
}
Ok(v)
}

pub fn validate_version(version: String) -> Result<String, String> {
if version.trim().is_empty() {
return Err("empty version string".to_string());
}
Ok(version)
}
6 changes: 6 additions & 0 deletions modules/src/ics03_connection/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ pub enum Kind {

#[error("invalid version")]
InvalidVersion,

#[error("invalid address")]
InvalidAddress,

#[error("invalid proof")]
InvalidProof,
}

impl Kind {
Expand Down
Loading

0 comments on commit f73d832

Please sign in to comment.