|
| 1 | +# Architectural Overview: Zero.nix |
| 2 | + |
| 3 | +Zero.nix is a nix-based multi-chain simulation system designed to streamline the setup and testing of blockchain environments. It provides tooling for deploying various blockchain nodes, smart contracts, and inter-chain communication mechanisms i.e. relayers. |
| 4 | + |
| 5 | +## Core Components |
| 6 | + |
| 7 | +### 1. NixOS Modules |
| 8 | + |
| 9 | +The system provides NixOS modules that define how blockchain nodes and relayers are configured and deployed: |
| 10 | + |
| 11 | +- **Cosmos Node Module** (`nixosModules/cosmos-node/`): |
| 12 | + - Defines configuration options for running Cosmos SDK-based blockchain nodes |
| 13 | + - Handles node initialization, configuration, and service setup |
| 14 | + - Manages account creation, key management, and blockchain state |
| 15 | + - Supports contract deployment through a dedicated service |
| 16 | + |
| 17 | +- **Hermes Relayer Module** (`nixosModules/hermes.nix`): |
| 18 | + - Configures and deploys the Hermes IBC relayer |
| 19 | + - Establishes connections and channels between blockchain networks |
| 20 | + - Manages key importing and chain configuration |
| 21 | + - Automates channel setup between all configured chains |
| 22 | + |
| 23 | +### 2. Packages |
| 24 | + |
| 25 | +The system includes packages for various blockchain-related tools: |
| 26 | + |
| 27 | +- **Upload Contract Package** (`packages/upload-contract/`): |
| 28 | + - Provides utilities for uploading and instantiating smart contracts on Cosmos-based chains |
| 29 | + - Handles contract deployment workflow including storing, instantiating, and verification |
| 30 | + |
| 31 | +- **Valence Contracts** (`packages/valence-contracts.nix`): |
| 32 | + - Manages specific contract packages for the Valence Protocol |
| 33 | + - Supports multiple contract versions (v0.1.1, v0.1.2, and main) |
| 34 | + |
| 35 | +### 3. Flake Modules |
| 36 | + |
| 37 | +The flake modules extend the functionality of the system: |
| 38 | + |
| 39 | +- **Upload Valence Contracts** (`flakeModules/upload-valence-contracts.nix`): |
| 40 | + - Provides specialized tooling for deploying Valence Protocol contracts |
| 41 | + - Integrates with the general contract upload mechanism |
| 42 | + |
| 43 | +- **Upload Contracts Directory** (`flakeModules/upload-contracts/`): |
| 44 | + - Contains further utilities for contract deployment workflows |
| 45 | + |
| 46 | +### 4. Tools |
| 47 | + |
| 48 | +Additional tooling to support the blockchain simulation environment: |
| 49 | + |
| 50 | +- **Build Valence Contracts** (`tools/build-valence-contracts/`): |
| 51 | + - Tools for building Valence Protocol contracts from source |
| 52 | + - Integrates with the Rust toolchain via crane and rust-overlay |
| 53 | + |
| 54 | +## Architecture |
| 55 | + |
| 56 | +```mermaid |
| 57 | +graph TD |
| 58 | + %% Main Components |
| 59 | + ZeroNix["Zero.nix System"] |
| 60 | + NixModules["NixOS Modules"] |
| 61 | + Packages["Packages"] |
| 62 | + FlakeModules["Flake Modules"] |
| 63 | + Tools["Tools"] |
| 64 | + InfraRepo["Infra Repository"] |
| 65 | + Deployment["Blockchain Network Deployment"] |
| 66 | + |
| 67 | + %% NixOS Modules Subcomponents |
| 68 | + CosmosNode["Cosmos Node Module"] |
| 69 | + HermesModule["Hermes Relayer Module"] |
| 70 | + |
| 71 | + %% Packages Subcomponents |
| 72 | + UploadContract["Upload Contract Package"] |
| 73 | + ValenceContracts["Valence Contracts"] |
| 74 | + |
| 75 | + %% Flake Modules Subcomponents |
| 76 | + UploadValenceContracts["Upload Valence Contracts"] |
| 77 | + UploadContractsDir["Upload Contracts Directory"] |
| 78 | + |
| 79 | + %% Tools Subcomponents |
| 80 | + BuildValenceContracts["Build Valence Contracts"] |
| 81 | + |
| 82 | + %% External Dependencies |
| 83 | + CosmosNix["cosmos.nix"] |
| 84 | + HermesRelay["Hermes"] |
| 85 | + ValenceProtocol["Valence Protocol"] |
| 86 | + |
| 87 | + %% Nodes and Chains |
| 88 | + ChainA["Chain A (e.g. Gaia)"] |
| 89 | + ChainB["Chain B (e.g. Neutron)"] |
| 90 | + ChainC["Chain C (e.g. Osmosis)"] |
| 91 | + |
| 92 | + %% Relationships |
| 93 | + ZeroNix --> NixModules |
| 94 | + ZeroNix --> Packages |
| 95 | + ZeroNix --> FlakeModules |
| 96 | + ZeroNix --> Tools |
| 97 | + |
| 98 | + NixModules --> CosmosNode |
| 99 | + NixModules --> HermesModule |
| 100 | + |
| 101 | + Packages --> UploadContract |
| 102 | + Packages --> ValenceContracts |
| 103 | + |
| 104 | + FlakeModules --> UploadValenceContracts |
| 105 | + FlakeModules --> UploadContractsDir |
| 106 | + |
| 107 | + Tools --> BuildValenceContracts |
| 108 | + |
| 109 | + %% External Dependencies |
| 110 | + CosmosNix -.-> CosmosNode |
| 111 | + HermesRelay -.-> HermesModule |
| 112 | + ValenceProtocol -.-> ValenceContracts |
| 113 | + ValenceProtocol -.-> BuildValenceContracts |
| 114 | + |
| 115 | + %% Deployment Flow |
| 116 | + ZeroNix --> InfraRepo |
| 117 | + InfraRepo --> Deployment |
| 118 | + Deployment --> ChainA |
| 119 | + Deployment --> ChainB |
| 120 | + Deployment --> ChainC |
| 121 | + |
| 122 | + %% Inter-chain Communication |
| 123 | + HermesModule --> |"IBC Relayer"| ChainA |
| 124 | + HermesModule --> |"IBC Relayer"| ChainB |
| 125 | + HermesModule --> |"IBC Relayer"| ChainC |
| 126 | + |
| 127 | + %% Contract Deployment |
| 128 | + UploadContract --> ChainA |
| 129 | + UploadContract --> ChainB |
| 130 | + UploadContract --> ChainC |
| 131 | + |
| 132 | + %% Class Styling |
| 133 | + classDef core fill:#f9f,stroke:#333,stroke-width:2px |
| 134 | + classDef module fill:#bbf,stroke:#333,stroke-width:1px |
| 135 | + classDef package fill:#bfb,stroke:#333,stroke-width:1px |
| 136 | + classDef external fill:#ddd,stroke:#333,stroke-width:1px,stroke-dasharray: 5 5 |
| 137 | + classDef chain fill:#feb,stroke:#333,stroke-width:1px |
| 138 | + |
| 139 | + %% Apply Classes |
| 140 | + class ZeroNix core |
| 141 | + class NixModules,Packages,FlakeModules,Tools,InfraRepo module |
| 142 | + class CosmosNode,HermesModule,UploadContract,ValenceContracts,UploadValenceContracts,UploadContractsDir,BuildValenceContracts package |
| 143 | + class CosmosNix,HermesRelay,ValenceProtocol external |
| 144 | + class ChainA,ChainB,ChainC chain |
| 145 | +``` |
| 146 | + |
| 147 | +### Multi-Chain Environment |
| 148 | + |
| 149 | +Zero.nix enables the creation of complex multi-chain testing environments by: |
| 150 | + |
| 151 | +1. **Chain Configuration**: Allowing detailed configuration of blockchain nodes with sensible defaults |
| 152 | +2. **Network Topology**: Supporting the definition of inter-chain connections via IBC channels |
| 153 | +3. **Contract Deployment**: Providing automated contract deployment and initialization across chains |
| 154 | +4. **Message Passing**: Establishing relayer connections for cross-chain communication |
| 155 | + |
| 156 | +### Nix Integration |
| 157 | + |
| 158 | +The system leverages Nix/NixOS capabilities: |
| 159 | + |
| 160 | +1. **Reproducible Environments**: Ensuring consistent test environments through declarative configuration |
| 161 | +2. **Modular Design**: Using the flake-parts pattern for composable and reusable components |
| 162 | +3. **Cross-Platform Support**: Supporting multiple systems (aarch64/x86_64 on Linux/Darwin) |
| 163 | +4. **Binary Caching**: Configured with Cachix for faster setup (`timewave.cachix.org`) |
| 164 | + |
| 165 | +### Workflow |
| 166 | + |
| 167 | +A typical workflow in Zero.nix involves: |
| 168 | + |
| 169 | +1. Configuring chain nodes using the cosmos-node module |
| 170 | +2. Setting up inter-chain communication with the hermes module |
| 171 | +3. Building smart contracts with the provided tools |
| 172 | +4. Deploying contracts to the appropriate chains |
| 173 | +5. Establishing channels for cross-chain message passing |
| 174 | +6. Testing and simulating cross-chain interactions |
| 175 | + |
| 176 | +## Integration with Infra Repository |
| 177 | + |
| 178 | +Zero.nix is designed to be used in conjunction with a companion infrastructure repository (`infra`) that defines specific deployment configurations. The integration works as follows: |
| 179 | + |
| 180 | +### Repository Structure and Relationship |
| 181 | + |
| 182 | +- **zero.nix**: Core library providing modular components for blockchain simulation |
| 183 | +- **infra**: Deployment-specific configurations that import and utilize zero.nix modules |
| 184 | + |
| 185 | +### Integration Points |
| 186 | + |
| 187 | +1. **NixOS Module Import**: The infra repository imports Zero.nix modules: |
| 188 | + ```nix |
| 189 | + imports = [ |
| 190 | + inputs.zero-nix.nixosModules.hermes |
| 191 | + inputs.zero-nix.nixosModules.cosmos-nodes |
| 192 | + ]; |
| 193 | + ``` |
| 194 | + |
| 195 | +2. **Deployment Configuration**: The infra repository uses Colmena to deploy the NixOS configurations to target machines: |
| 196 | + ```nix |
| 197 | + flake.colmenaHive = inputs.colmena.lib.makeHive self.colmena; |
| 198 | + ``` |
| 199 | + |
| 200 | +3. **Blockchain Node Profiles**: The infra repository defines specific blockchain configurations as profiles: |
| 201 | + - `profiles/gaia.nix`: Cosmos Hub configuration |
| 202 | + - `profiles/neutron.nix`: Neutron chain configuration |
| 203 | + - `profiles/juno.nix`: Juno chain configuration |
| 204 | + - `profiles/osmosis.nix`: Osmosis chain configuration |
| 205 | + |
| 206 | +4. **Smart Contract Deployment**: The infra repository includes contract artifacts for deployment: |
| 207 | + ```nix |
| 208 | + services.cosmos.nodeDefaults.contracts = { |
| 209 | + cw_sp1_verifier-groth16.path = ./cw_sp1_verifier-groth16.wasm; |
| 210 | + cw_sp1_verifier-plonk.path = ./cw_sp1_verifier-plonk.wasm; |
| 211 | + }; |
| 212 | + ``` |
| 213 | + |
| 214 | +### Deployment Workflow |
| 215 | + |
| 216 | +1. **Setup Infrastructure Repository**: |
| 217 | + ```bash |
| 218 | + git clone https://github.com/timewave-computer/infra.git |
| 219 | + cd infra |
| 220 | + ``` |
| 221 | + |
| 222 | +2. **Configure Host Machine**: |
| 223 | + Edit or create host configuration files in the `hosts/` directory based on your requirements. |
| 224 | + |
| 225 | +3. **Add Chain Profiles**: |
| 226 | + Configure the blockchain nodes you want to deploy in the `profiles/` directory. |
| 227 | + |
| 228 | +4. **Deploy with [Colmena](https://colmena.cli.rs/)**: |
| 229 | + ```bash |
| 230 | + nix develop |
| 231 | + colmena apply --on zk-simulation-environment |
| 232 | + ``` |
| 233 | + |
| 234 | +5. **Access and Interact with Nodes**: |
| 235 | + Once deployed, you can interact with the blockchain nodes and relayers through their exposed RPC endpoints. |
| 236 | + |
| 237 | +### Customization Options |
| 238 | + |
| 239 | +1. **Adding New Chains**: Create a new profile in the `profiles/` directory following the pattern of existing profiles. |
| 240 | + |
| 241 | +2. **Custom Contract Deployment**: Add contract WASM files to a profile directory and reference them in the configuration. |
| 242 | + |
| 243 | +3. **Genesis Account Configuration**: Modify the `default-genesis-accounts.nix` file to configure initial account balances. |
| 244 | + |
| 245 | +4. **Node Settings Override**: Use the `default-node-settings.nix` file to configure global settings for all nodes. |
| 246 | + |
| 247 | +## External Dependencies |
| 248 | + |
| 249 | +The system integrates with several external projects: |
| 250 | + |
| 251 | +- [cosmos.nix](https://github.com/informalsystems/cosmos.nix): For Cosmos SDK blockchain node packages and utilities |
| 252 | +- [Hermes](https://github.com/informalsystems/hermes): For IBC relayer functionality |
| 253 | +- [Valence Protocol](https://github.com/timewave-computer/valence-protocol): For cross-chain smart contracts |
| 254 | + |
| 255 | +## Usage Examples |
| 256 | + |
| 257 | +Zero.nix can be used to simulate various multi-chain scenarios, such as: |
| 258 | + |
| 259 | +- Testing cross-chain token transfers |
| 260 | +- Validating IBC protocol implementations |
| 261 | +- Developing and testing cross-chain applications |
| 262 | +- Benchmarking relayer performance under different network conditions |
| 263 | +- Simulating complex multi-chain application workflows |
0 commit comments