cosmos / ibc-rs

Rust implementation of the Inter-Blockchain Communication (IBC) protocol.
Apache License 2.0
182 stars 74 forks source link

Proposal for redesigning parts of the ibc-rs modules' API #25

Closed hu55a1n1 closed 1 year ago

hu55a1n1 commented 2 years ago

Crate

ibc

Proposal

Following is a list of ideas for redesigning parts of our ibc-rs modules' API accompanied with some reasoning for why these changes are required. (Some of these have already been implemented in PR informalsystems/hermes#1583.) This is meant to serve as a meta issue for tracking these design changes. Might make sense to defer this until we start implementing another client (i.e. substrate, etc.) ->

// to something like this -> pub enum Proofs { ChannelOpenTry(ChannelOpenTryProof), // ... }

// or maybe this -> pub struct SimpleProof(CommitmentProofBytes); pub struct ComplexProof { pub proof: CommitmentProofBytes, pub consensus_proof: ConsensusProof, // ... }

pub enum Proofs { ConnectionOpenTry(ComplexProof), ChannelOpenTry(SimpleProof), // ... }

- [x] `ics24_host::Path` variants as types - Since Rust doesn't treat enum variants as types, it might be a good idea to extract these variants as distinct types and use those types as the actual enum variants. This will allow us to enforce that certain functions accept only a certain type of path. eg. `verify_client_consensus_state()` should only accept a `Path::ClientConsensusState`. (https://github.com/informalsystems/ibc-rs/pull/1760)
- [x] Follow [RFC-356-no-module-prefixes](https://github.com/rust-lang/rfcs/blob/master/text/0356-no-module-prefixes.md). This basically means we rename `ibc::core::ics04_channel::msgs::MsgChannelCloseConfirm` to `ibc::core::ics04_channel::msgs::ChannelCloseConfirm`, and either re-export it as `MsgChannelCloseConfirm` in `msgs.rs` or add it to the prelude.
- [ ] Public `struct`s should either have public fields or setters/getters (if required), but not both. e.g.
```rust
pub struct MsgChannelCloseInit {
    pub port_id: PortId, // <- public
    // ...
}

impl MsgChannelCloseInit {
    pub fn port_id(&self) -> &PortId {
        &self.port_id
    } // <- also has a getter
}

PS: Thanks to @romac for his valuable insights and suggestions!


For Admin Use

mzabaluev commented 2 years ago
pub enum Proofs {
    ChannelOpenTry(ChannelOpenTryProof), 
    // ...
}

Bikeshedding: Does this type mean "all proofs supplied with (necessary for?) this request"? The variant names suggest that each case can be thought of a singular "proof", and in this case the enum should probably be named Proof.


// or maybe this -> 
pub struct SimpleProof(CommitmentProofBytes);
pub struct ComplexProof {
    pub proof: CommitmentProofBytes, 
    pub consensus_proof: ConsensusProof,
    // ...
}

pub enum Proofs {
    ConnectionOpenTry(ComplexProof), 
    ChannelOpenTry(SimpleProof), 
    // ...
}

Could be, but then a convenience accessor may be needed to get at the commitment proof in all variants where it's available:

impl Proofs {
    pub fn commitment(&self) -> Option<&CommitmentProofBytes> {
        todo!()
    }
}

If there may be benefit in type-checking the proof variants in particular places where only such a variant is meaningful, they each should get their own dedicated type, to be used instead of matching a dynamically multiplexed Proofs value and asserting. This is similar to @hu55a1n1's suggestion for ics24_host::Path

Farhad-Shabani commented 1 year ago

For the remained tasks in this ticket, issues that still make sense are opened (#532, #534, #537). This is done as a pre-work before preparing the "Road to IBC-rs V1". @plafer Can you please recheck the list to make sure I didn't miss anything before closing the issue?

plafer commented 1 year ago

LGTM