|
| 1 | +--- |
| 2 | +name: charon-guide |
| 3 | +description: Use when porting Charon components to understand Go codebase architecture, workflow, design patterns, or component responsibilities |
| 4 | +--- |
| 5 | + |
| 6 | +# Charon Architecture Guide |
| 7 | + |
| 8 | +Reference for understanding the Charon Go codebase when porting to Pluto (Rust). |
| 9 | + |
| 10 | +## What is Charon? |
| 11 | + |
| 12 | +Distributed validator middleware for Ethereum staking. Enables running a single validator across multiple independent nodes using threshold BLS signatures. Each validator duty flows through a multi-stage workflow with consensus and signature aggregation. |
| 13 | + |
| 14 | +## Knowledge Base |
| 15 | + |
| 16 | +When you need deeper understanding: |
| 17 | + |
| 18 | +| Topic | Location | |
| 19 | +|-------|----------| |
| 20 | +| High-level overview | `charon/README.md` | |
| 21 | +| Detailed architecture | `charon/docs/architecture.md` | |
| 22 | +| Go coding guidelines | `charon/docs/goguidelines.md` | |
| 23 | +| Configuration options | `charon/docs/configuration.md` | |
| 24 | +| DKG details | `charon/docs/dkg.md` | |
| 25 | +| Consensus (QBFT) | `charon/docs/consensus.md`, `charon/core/qbft/README.md` | |
| 26 | +| Metrics | `charon/docs/metrics.md` | |
| 27 | +| Error reason codes | `charon/docs/reasons.md` | |
| 28 | +| Package structure | `charon/docs/structure.md` | |
| 29 | +| Product docs | https://docs.obol.org/next | |
| 30 | + |
| 31 | +## Core Workflow |
| 32 | + |
| 33 | +Every validator duty (attestation, block proposal, etc.) flows through these components in order: |
| 34 | + |
| 35 | +``` |
| 36 | +Scheduler → Fetcher → Consensus → DutyDB → ValidatorAPI → ParSigDB → ParSigEx → SigAgg → AggSigDB → Bcast |
| 37 | +``` |
| 38 | + |
| 39 | +| Component | Responsibility | Go Package | |
| 40 | +|-----------|---------------|------------| |
| 41 | +| **Scheduler** | Triggers duties at optimal times based on beacon chain state | `core/scheduler/` | |
| 42 | +| **Fetcher** | Fetches unsigned duty data from beacon node | `core/fetcher/` | |
| 43 | +| **Consensus** | QBFT consensus to agree on duty data across all nodes | `core/consensus/`, `core/qbft/` | |
| 44 | +| **DutyDB** | Persists unsigned data, slashing protection | `core/dutydb/` | |
| 45 | +| **ValidatorAPI** | Serves data to validator clients, receives partial signatures | `core/validatorapi/` | |
| 46 | +| **ParSigDB** | Stores partial threshold BLS signatures from local/remote VCs | `core/parsigdb/` | |
| 47 | +| **ParSigEx** | Exchanges partial signatures with peers via p2p | `core/parsigex/` | |
| 48 | +| **SigAgg** | Aggregates partial signatures when threshold reached | `core/sigagg/` | |
| 49 | +| **AggSigDB** | Persists aggregated signatures | `core/aggsigdb/` | |
| 50 | +| **Bcast** | Broadcasts final signatures to beacon node | `core/bcast/` | |
| 51 | + |
| 52 | +**Porting note:** When porting a component, trace its inputs and outputs through this pipeline. |
| 53 | + |
| 54 | +## Key Abstractions |
| 55 | + |
| 56 | +| Concept | Definition | Go Type | |
| 57 | +|---------|------------|---------| |
| 58 | +| **Duty** | Unit of work (slot + duty type). Cluster-level, not per-validator. | `core.Duty` | |
| 59 | +| **PubKey** | DV root public key, validator identifier in workflow | `core.PubKey` | |
| 60 | +| **UnsignedData** | Abstract type for attestation data, blocks, etc. | `core.UnsignedData` (interface) | |
| 61 | +| **SignedData** | Fully signed duty data | `core.SignedData` (interface) | |
| 62 | +| **ParSignedData** | Partially signed data from single threshold BLS share | `core.ParSignedData` (struct) | |
| 63 | + |
| 64 | +**Type encoding:** Abstract types are encoded/decoded via `core/encode.go`. Always check this file when porting type serialization. |
| 65 | + |
| 66 | +## Design Patterns |
| 67 | + |
| 68 | +| Pattern | Description | Porting Impact | |
| 69 | +|---------|-------------|----------------| |
| 70 | +| **Immutable values** | Components consume and produce immutable values (actor-like) | Always `.Clone()` before sharing/caching in Rust | |
| 71 | +| **Callback subscriptions** | Components decoupled via subscriptions, not direct calls | Use channels or callbacks in Rust | |
| 72 | +| **Type-safe encoding** | Abstract types use custom encoding/decoding | Port `core/encode.go` logic carefully | |
| 73 | + |
| 74 | +## Consensus (QBFT) |
| 75 | + |
| 76 | +Charon uses **QBFT** (Istanbul BFT) for consensus. Each duty requires consensus to ensure all nodes sign identical data (required for BLS threshold signatures and slashing protection). |
| 77 | + |
| 78 | +**Key files:** |
| 79 | +- `core/qbft/` - QBFT implementation |
| 80 | +- `core/consensus/` - Consensus component integration |
| 81 | +- `charon/docs/consensus.md` - Design documentation |
| 82 | + |
| 83 | +**Porting note:** QBFT is complex. Port incrementally and verify against Go test vectors. |
| 84 | + |
| 85 | +## Package Structure |
| 86 | + |
| 87 | +| Go Package | Purpose | Rust Equivalent | |
| 88 | +|------------|---------|-----------------| |
| 89 | +| `app/` | Application entrypoint, wiring, infrastructure (log, errors, tracer, lifecycle) | `pluto/crates/app/` | |
| 90 | +| `cluster/` | Cluster config, lock files, DKG artifacts | `pluto/crates/cluster/` | |
| 91 | +| `cmd/` | CLI commands (run, dkg, create, test, etc.) | `pluto/crates/cli/` | |
| 92 | +| `core/` | Core workflow business logic and components | `pluto/crates/core/` | |
| 93 | +| `dkg/` | Distributed Key Generation logic | `pluto/crates/dkg/` | |
| 94 | +| `eth2util/` | ETH2 utilities (signing, deposits, keystores) | `pluto/crates/eth2util/` | |
| 95 | +| `p2p/` | libp2p networking and discv5 peer discovery | `pluto/crates/p2p/` | |
| 96 | +| `tbls/` | Threshold BLS signature scheme | `pluto/crates/crypto/` | |
| 97 | +| `testutil/` | Test utilities, mocks, golden files | `pluto/crates/testutil/` | |
| 98 | + |
| 99 | +## Common Porting Scenarios |
| 100 | + |
| 101 | +### Porting a Workflow Component |
| 102 | + |
| 103 | +1. Read `charon/docs/architecture.md` for component interfaces |
| 104 | +2. Identify inputs (what triggers this component?) |
| 105 | +3. Identify outputs (what does it produce? who consumes it?) |
| 106 | +4. Check `core/<component>/` for implementation |
| 107 | +5. Check subscriptions in `app/app.go` (wiring/stitching logic) |
| 108 | +6. Port logic, preserving immutability and callback patterns |
| 109 | + |
| 110 | +### Adding New Duty Types |
| 111 | + |
| 112 | +When porting code that adds duty types: |
| 113 | + |
| 114 | +1. Add to `core/types.go` (duty type constants) |
| 115 | +2. Add to `core/encode.go` (encoding/decoding logic) |
| 116 | +3. Update Scheduler, Fetcher, and relevant components |
| 117 | + |
| 118 | +### Understanding Error Handling |
| 119 | + |
| 120 | +Go style: |
| 121 | +- Just return errors, don't log and return |
| 122 | +- Wrap external errors: `errors.Wrap(err, "do something")` |
| 123 | +- Use `app/errors` for structured errors with fields |
| 124 | + |
| 125 | +Rust style: See `rust-style` skill and AGENTS.md. |
| 126 | + |
| 127 | +### Working with Cluster Lock Files |
| 128 | + |
| 129 | +- `cluster-definition.json`: Intended cluster config (operators, validators) |
| 130 | +- `cluster-lock.json`: Extends definition with DV public keys and shares (DKG output) |
| 131 | +- See `cluster/` package for parsing/validation |
| 132 | + |
| 133 | +## Dependency Notes |
| 134 | + |
| 135 | +Charon uses forked dependencies: |
| 136 | +- `github.com/ObolNetwork/kryptology` (security fixes) |
| 137 | +- `github.com/ObolNetwork/go-eth2-client` (kept up to date) |
| 138 | + |
| 139 | +When porting code using these, check if Rust equivalents exist or if porting is needed. |
| 140 | + |
| 141 | +## Version Compatibility |
| 142 | + |
| 143 | +Important for protocol compatibility: |
| 144 | + |
| 145 | +- **Compatible:** Same MAJOR version, different MINOR/PATCH |
| 146 | +- **Incompatible:** Different MAJOR version |
| 147 | +- **DKG:** Requires matching MAJOR and MINOR versions (PATCH can differ) |
| 148 | + |
| 149 | +## Quick Reference Commands |
| 150 | + |
| 151 | +When exploring Charon codebase: |
| 152 | + |
| 153 | +```bash |
| 154 | +# Find a function |
| 155 | +grep -r "func FunctionName" charon/ |
| 156 | + |
| 157 | +# See package structure |
| 158 | +ls -la charon/<directory>/ |
| 159 | + |
| 160 | +# Check imports |
| 161 | +grep "^import" charon/path/to/file.go |
| 162 | + |
| 163 | +# Run specific Go test (to understand behavior) |
| 164 | +cd charon && go test -run TestFunctionName ./path/to/package |
| 165 | +``` |
0 commit comments