|
| 1 | +# ADR 008: RESTRUCTURE `ibc` CRATE |
| 2 | + |
| 3 | +## Context |
| 4 | + |
| 5 | +The current state of the **`ibc`** crate exhibits a mix of different |
| 6 | +implementation layers. From a module-based perspective, it encompasses essential |
| 7 | +elements like ibc core, clients, applications, along with testing facilities. |
| 8 | +However, an architectural view reveals a fusion of diverse layers of type |
| 9 | +definitions, interfaces (APIs), and handler functions, resulting in a mix of |
| 10 | +dependencies and features that may lead to potential conflicts or unnecessary |
| 11 | +imports for users. |
| 12 | + |
| 13 | +As of [this pull request](https://github.com/cosmos/ibc-rs/pull/954), we've |
| 14 | +separated our mock testing kit into the standalone **`ibc-testkit`** library. |
| 15 | +This decoupling from the main **`ibc`** crate sets the stage for the objectives |
| 16 | +of this ADR. |
| 17 | + |
| 18 | +The primary goals here are twofold: firstly, to reduce interdependence within |
| 19 | +the codebase among various components such as ibc core, clients, and |
| 20 | +applications, and secondly, to improve the overall usability of the `ibc-rs` |
| 21 | +implementation. The overarching aim is to empower users to selectively import |
| 22 | +specific IBC layers, mitigating potential conflicts related to dependencies or |
| 23 | +features that may arise in the context of a monolithic library and letting |
| 24 | +`ibc-rs` be used in the following scenarios: |
| 25 | + |
| 26 | +1. **Selective Module Import** |
| 27 | + - Users cannot import only the necessary components/modules for their |
| 28 | + projects. For instance, importing only the **`ics07_tendermint`** |
| 29 | + implementation is impractical. |
| 30 | +2. **Selective Types Import** |
| 31 | + - Relayers, like Hermes, or any off-chain consumers cannot import their |
| 32 | + desired layer of implementation like ibc types without pulling in |
| 33 | + unnecessary dependencies into their project. |
| 34 | +3. **Smoother IBC Core Integration with Hosts** |
| 35 | + - Integrating ibc core with host chains without introducing light client or |
| 36 | + app dependencies is currently not straightforward, impeding smooth |
| 37 | + integration. |
| 38 | +4. **Easier Development of CosmWasm Contracts** |
| 39 | + - For developing a CosmWasm tendermint light client, we ideally should only |
| 40 | + be dependent on implementation under the **`ics07_tendermint`** and also |
| 41 | + be importing relevant parts from the **`ibc-core-client`** layer without |
| 42 | + pulling in all the ibc codebase and dependencies. |
| 43 | + |
| 44 | +This ADR aims to enhance both the usability and practicality of `ibc-rs` by |
| 45 | +restructuring the codebase and organizing it under multiple sub-libraries, as |
| 46 | +stated in the [decision](#decision) section. This will make different parts of |
| 47 | +`ibc-rs` accessible to users, positioning it as a more comprehensive, one-stop |
| 48 | +solution catering to diverse user groups, whether for on-chain or off-chain use |
| 49 | +cases. |
| 50 | + |
| 51 | +## Decision |
| 52 | + |
| 53 | +For the library organization, the first stage of separation is to split the |
| 54 | +codebase so that each IBC application, client, and core implementation is |
| 55 | +decoupled from one another. The top-level libraries and the naming schema would |
| 56 | +look as follows: |
| 57 | + |
| 58 | +```markdown |
| 59 | +. |
| 60 | +├── ibc -> Primarily re-exports sub-libraries |
| 61 | +├── ibc-core |
| 62 | +│ ├── ibc-core-client (contains the implementation + Re-exports types) |
| 63 | +│ ├── ibc-core-connection |
| 64 | +│ ├── ibc-core-channel |
| 65 | +│ ├── ibc-core-commitment |
| 66 | +│ └── ibc-core-host |
| 67 | +│ └── . |
| 68 | +├── ibc-clients |
| 69 | +│ ├── ibc-client-tendermint |
| 70 | +│ ├── ibc-client-tendermint-cw |
| 71 | +│ └── . |
| 72 | +├── ibc-apps |
| 73 | +│ ├── ibc-app-transfer |
| 74 | +│ ├── ibc-app-ica |
| 75 | +│ │ └── . |
| 76 | +│ └── . |
| 77 | +├── ibc-primitives |
| 78 | +├── ibc-testkit (previously mock module + `test-utils` feature) |
| 79 | +├── ibc-query |
| 80 | +└── ibc-derive |
| 81 | +``` |
| 82 | + |
| 83 | +With this restructure, the main `ibc` crate primarily re-exports types, |
| 84 | +interfaces, and implementations of all the sub-libraries. Therefore, if someone |
| 85 | +only wants to depend on the `ibc` crate without caring about this granularity, |
| 86 | +they can do so. |
| 87 | + |
| 88 | +Afterward, we split off data structure (domain types) of each IBC layer into a |
| 89 | +separate sub-library under a `types` folder, still maintained under the |
| 90 | +directory of that relevant component/module. As an example, the |
| 91 | +`ibc-core-client` crate’s tree and the naming schema would look like this: |
| 92 | + |
| 93 | +```markdown |
| 94 | +ibc-core |
| 95 | +└── ibc-core-client (dir: ibc-core/ics02-client) |
| 96 | + └── ibc-core-client-types (dir: ibc-core/ics02-client/types) |
| 97 | + ├── msgs |
| 98 | + ├── events |
| 99 | + └── . |
| 100 | +``` |
| 101 | + |
| 102 | +This way, the main crate of each IBC module contains all the necessary APIs and |
| 103 | +implementations to integrate with host chains, along with re-exporting the |
| 104 | +sub-library types. This allows projects to selectively import types (e.g. |
| 105 | +`ibc-core-client-types`), often required by off-chain users such as relayers. Or |
| 106 | +to pick the library containing the entire implementation of that particular |
| 107 | +module (e.g. `ibc-core-client`), typically more convenient for host chains or |
| 108 | +smart contract developers to integrate with on their end. |
| 109 | + |
| 110 | +Once the restructuring is complete, the **directory tree** of the repo would |
| 111 | +look as follows: |
| 112 | + |
| 113 | +```markdown |
| 114 | +ibc |
| 115 | +ibc-core |
| 116 | +├── ics02-client |
| 117 | +| ├── src |
| 118 | +| ├── types |
| 119 | +| | ├── src |
| 120 | +| | └── Cargo.toml |
| 121 | +| └── Cargo.toml |
| 122 | +├── ics03-connection |
| 123 | +| └── . |
| 124 | +├── ics04-channel |
| 125 | +| └── . |
| 126 | +├── ics23-commitment |
| 127 | +| └── . |
| 128 | +├── ics24-host |
| 129 | +| └── . |
| 130 | +├── src |
| 131 | +├── Cargo.toml |
| 132 | +└── README.md |
| 133 | +ibc-clients |
| 134 | +├── ics07-tendermint |
| 135 | +├── ics08-wasm |
| 136 | +└── . |
| 137 | +ibc-apps |
| 138 | +├── ics20-transfer |
| 139 | +├── ics27-ica |
| 140 | +└── . |
| 141 | +ibc-primitives |
| 142 | +ibc-testkit |
| 143 | +ibc-query |
| 144 | +ibc-derive |
| 145 | +``` |
| 146 | + |
| 147 | +In the refactored codebase, there will be several `*-types` crates that rely on |
| 148 | +the `ibc-proto` library. This library acts as an upstream crate, facilitating |
| 149 | +the conversion to and from proto types. Each `*-types` crate also re-exports |
| 150 | +crucial proto types for added user convenience. This approach ensures a seamless |
| 151 | +experience for users downstream, sparing them the necessity of directly |
| 152 | +including the `ibc-proto` dependency in their projects. Consequently, the |
| 153 | +`*-types` crates serve as comprehensive, battery-included libraries. |
| 154 | + |
| 155 | +To implement this ADR efficiently and for more organization, we use the |
| 156 | +workspace inheritance feature and will add a top-level README for main library |
| 157 | +groups like `ibc-core`, `ibc-clients`, etc serving as a guide for users to |
| 158 | +understand the purpose and structure of their sub libraries. |
| 159 | + |
| 160 | +Later, it is crucial to come up with a Github action to automate and simplify |
| 161 | +the release process as well. |
| 162 | + |
| 163 | +## **Status** |
| 164 | + |
| 165 | +Proposed |
| 166 | + |
| 167 | +## **Consequences** |
| 168 | + |
| 169 | +We should acknowledge this restructuring, while a significant step forward, will |
| 170 | +not completely address all existing design couplings. Subsequent improvements in |
| 171 | +implementation logic will be necessary to completely decouple ibc core, clients, |
| 172 | +and applications from each other and make the entire logic as chain-agnostic as |
| 173 | +possible. For instance, currently, our `IbcEvent` type depends on the Tendermint |
| 174 | +events in their conversion, which can only be addressed once this restructuring |
| 175 | +is complete. There may be other mix-ups as well, but the new repository |
| 176 | +structure significantly simplifies their handling and ensures `ibc-rs` evolves |
| 177 | +into a more adaptable, modular, and composable implementation that can serve |
| 178 | +various use cases. |
| 179 | + |
| 180 | +### **Positive** |
| 181 | + |
| 182 | +- Opens up a range of new use cases for `ibc-rs` |
| 183 | +- Facilitates moving toward more chain-agnostic and flexible design and interfaces |
| 184 | +- Simplifies development on top of each layer of `ibc-rs` implementation |
| 185 | + |
| 186 | +### **Negative** |
| 187 | + |
| 188 | +- Multiple libraries are more challenging to maintain |
| 189 | +- Enforces current users to update a large number of their import paths from |
| 190 | + `ibc` crates |
0 commit comments