Skip to content

Commit

Permalink
Trimmed the proto file, fixed commitments. Ready for review. (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
adizere committed Jul 10, 2020
1 parent ae792c0 commit d332fd6
Show file tree
Hide file tree
Showing 12 changed files with 141 additions and 393 deletions.
4 changes: 2 additions & 2 deletions modules/build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
extern crate prost_build;

fn main() {
prost_build::compile_protos(&["src/proto/ibc/connection/connection.proto"], &["src/proto"]).unwrap();
}
prost_build::compile_protos(&["src/proto/connection.proto"], &["src/proto"]).unwrap();
}
46 changes: 46 additions & 0 deletions modules/src/ics03_connection/connection.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use super::exported::*;
use crate::ics03_connection::error::{Error, Kind};
use crate::ics03_connection::proto_connection;
use crate::ics23_commitment::CommitmentPrefix;
use crate::ics24_host::identifier::{ClientId, ConnectionId};
use serde_derive::{Deserialize, Serialize};

use anomaly::fail;
use std::str::FromStr;

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct ConnectionEnd {
state: State,
Expand All @@ -25,6 +29,34 @@ impl ConnectionEnd {
versions: validate_versions(versions).map_err(|e| Kind::InvalidVersion.context(e))?,
})
}

pub fn set_state(&mut self, new_state: State) {
self.state = new_state;
}

pub fn from_proto_connection(pc: proto_connection::ConnectionEnd) -> Result<Self, Error> {
// The Counterparty field is an Option, may be missing.
match pc.counterparty {
Some(cp) => {
let mut conn = ConnectionEnd::new(
ClientId::from_str(&pc.client_id).unwrap(),
Counterparty::from_proto_counterparty(cp).unwrap(),
pc.versions,
)
.unwrap();

// Set the state.
conn.set_state(State::from_i32(pc.state));
Ok(conn)
}

// If no counterparty was set, signal the error.
None => fail!(
Kind::MissingCounterparty,
"no counterparty in the given connection"
),
}
}
}

impl Connection for ConnectionEnd {
Expand Down Expand Up @@ -76,6 +108,20 @@ impl Counterparty {
prefix,
})
}

pub fn from_proto_counterparty(pc: proto_connection::Counterparty) -> Result<Self, Error> {
match pc.prefix {
Some(prefix) => Counterparty::new(
pc.client_id,
pc.connection_id,
CommitmentPrefix::new(prefix.key_prefix),
),
None => fail!(
Kind::MissingCounterpartyPrefix,
"no prefix in the given counterparty"
),
}
}
}

impl ConnectionCounterparty for Counterparty {
Expand Down
6 changes: 6 additions & 0 deletions modules/src/ics03_connection/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ pub enum Kind {

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

#[error("missing counterparty")]
MissingCounterparty,

#[error("missing counterparty prefix")]
MissingCounterpartyPrefix,
}

impl Kind {
Expand Down
9 changes: 9 additions & 0 deletions modules/src/ics03_connection/exported.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ impl State {
Self::Open => "OPEN",
}
}

pub fn from_i32(nr: i32) -> Self {
match nr {
1 => Self::Init,
2 => Self::TryOpen,
3 => Self::Open,
_ => Self::Uninitialized,
}
}
}

impl std::str::FromStr for State {
Expand Down
4 changes: 2 additions & 2 deletions modules/src/ics03_connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ pub mod msgs;
pub mod query;

// Use the generated proto file for the connection according to prost_build instructions
pub mod proto_ibc_connection {
include!(concat!(env!("OUT_DIR"), "/ibc.connection.rs"));
pub mod proto_connection {
include!(concat!(env!("OUT_DIR"), "/connection.rs"));
}
45 changes: 15 additions & 30 deletions modules/src/ics03_connection/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ use tendermint_rpc::endpoint::abci_query::AbciQuery;

use tendermint::abci;

use crate::ics23_commitment::{CommitmentPath, CommitmentProof, CommitmentPrefix};
use crate::ics24_host::identifier::{ConnectionId, ClientId};
use crate::ics23_commitment::{CommitmentPath, CommitmentProof};
use crate::ics24_host::identifier::ConnectionId;

use crate::error;
use crate::ics03_connection::connection::{ConnectionEnd, Counterparty};
use crate::ics03_connection::connection::ConnectionEnd;
use crate::path::{ConnectionPath, Path};
use crate::query::{IbcQuery, IbcResponse};
use crate::Height;

// Protobuf
use crate::ics03_connection::proto_ibc_connection;
use prost::Message;
use crate::ics03_connection::error::Error;
use crate::ics03_connection::proto_connection;
use bytes::Bytes;
use std::str::FromStr;
use prost::Message;

pub struct QueryConnection {
pub chain_height: Height,
Expand Down Expand Up @@ -85,17 +85,14 @@ impl IbcResponse<QueryConnection> for ConnectionResponse {
query: QueryConnection,
response: AbciQuery,
) -> Result<Self, error::Error> {

let connection = proto_unmarshal(response.value);
match connection {
Ok(conn) => {
Ok(ConnectionResponse::new(
query.connection_id,
conn,
response.proof,
response.height.into(),
))
},
Ok(conn) => Ok(ConnectionResponse::new(
query.connection_id,
conn,
response.proof,
response.height.into(),
)),
Err(e) => {
println!("Error proto un-marshall: {:?}", e);
todo!()
Expand All @@ -119,22 +116,10 @@ impl IdentifiedConnectionEnd {
}
}

fn proto_unmarshal(bytes: Vec<u8>) -> Result<ConnectionEnd, error::Error> {
fn proto_unmarshal(bytes: Vec<u8>) -> Result<ConnectionEnd, Error> {
let buf = Bytes::from(bytes);
let decoded = proto_ibc_connection::ConnectionEnd::decode(buf);
match decoded {
Ok(conn) => {
let client_id = ClientId::from_str(&conn.client_id).unwrap();
let prefix = CommitmentPrefix{};
let counterparty = Counterparty::new(client_id.to_string(), conn.id, prefix).unwrap();
let connection_end = ConnectionEnd::new(client_id, counterparty, conn.versions).unwrap();
Ok(connection_end)
},
Err(e) => {
println!("Error proto un-marshall: {:?}", e);
todo!()
}
}
let decoded = proto_connection::ConnectionEnd::decode(buf).unwrap();
ConnectionEnd::from_proto_connection(decoded)
}

fn amino_unmarshal_binary_length_prefixed<T>(_bytes: &[u8]) -> Result<T, error::Error> {
Expand Down
10 changes: 7 additions & 3 deletions modules/src/ics23_commitment/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl CommitmentPath {
where
P: Path,
{
CommitmentPath{}
CommitmentPath {}
}
}

Expand All @@ -32,6 +32,10 @@ impl CommitmentProof {
*/

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct CommitmentPrefix;
pub struct CommitmentPrefix(std::vec::Vec<u8>);

// TODO: impl CommitPrefix
impl CommitmentPrefix {
pub fn new(content: Vec<u8>) -> Self {
Self { 0: content }
}
}
54 changes: 54 additions & 0 deletions modules/src/proto/connection.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
syntax = "proto3";

package connection;

// ICS03 - Connection Data Structures as defined in
// https://github.com/cosmos/ics/tree/master/spec/ics-003-connection-semantics#data-structures

// ConnectionEnd defines a stateful object on a chain connected to another separate
// one.
// NOTE: there must only be 2 defined ConnectionEnds to establish a connection
// between two chains.
message ConnectionEnd {
// connection identifier.
string id = 1;
// client associated with this connection.
string client_id = 2;
// opaque string which can be utilised to determine encodings or protocols for
// channels or packets utilising this connection
repeated string versions = 3;
// current state of the connection end.
State state = 4;
// counterparty chain associated with this connection.
Counterparty counterparty = 5;
}

// State defines if a connection is in one of the following states:
// INIT, TRYOPEN, OPEN or UNINITIALIZED.
enum State {
// Default State
STATE_UNINITIALIZED_UNSPECIFIED = 0;
// A connection end has just started the opening handshake.
STATE_INIT = 1;
// A connection end has acknowledged the handshake step on the counterparty chain.
STATE_TRYOPEN = 2;
// A connection end has completed the handshake.
STATE_OPEN = 3;
}

// Counterparty defines the counterparty chain associated with a connection end.
message Counterparty {
// identifies the client on the counterparty chain associated with a given connection.
string client_id = 1;
// identifies the connection end on the counterparty chain associated with a given connection.
string connection_id = 2;
// commitment merkle prefix of the counterparty chain
MerklePrefix prefix = 3;
}


// MerklePrefix is merkle path prefixed to the key.
// The constructed key from the Path and the key will be append(Path.KeyPath, append(Path.KeyPrefix, key...))
message MerklePrefix {
bytes key_prefix = 1;
}
Loading

0 comments on commit d332fd6

Please sign in to comment.