|
| 1 | +# Dynamic Dispatch in Leo |
| 2 | + |
| 3 | +This example demonstrates three related Leo 4.0 features: **interfaces**, **dynamic dispatch**, and **dynamic records**. |
| 4 | + |
| 5 | +## The Scenario |
| 6 | + |
| 7 | +Different governance systems call for different voting-power formulas. A _linear_ strategy grants one vote per token (simple majority), while a _quadratic_ strategy grants floor(√tokens) votes (reducing whale influence). We want a single governance contract that can apply **either** formula — or any future formula — without being redeployed. |
| 8 | + |
| 9 | +Dynamic dispatch makes this possible: the caller names the target strategy at runtime using the `identifier` type, and the AVM routes the call to whichever deployed program is named. |
| 10 | + |
| 11 | +## Program Architecture |
| 12 | + |
| 13 | +``` |
| 14 | +voting_power.aleo quadratic_power.aleo |
| 15 | + │ declares VotingStrategy │ implements VotingStrategy |
| 16 | + │ implements (linear) │ (floor √balance votes) |
| 17 | + │ showcases dyn record │ |
| 18 | + └──────────┬────────────────-┘ |
| 19 | + │ governance.aleo imports voting_power.aleo |
| 20 | + │ to resolve the VotingStrategy interface, |
| 21 | + ▼ then dispatches to either program at runtime |
| 22 | + governance.aleo |
| 23 | + get_voting_power(strategy, balance) |
| 24 | + proposal_passes(strategy, for_bal, against_bal) |
| 25 | + compare_strategies(balance) |
| 26 | +``` |
| 27 | + |
| 28 | +## Features Showcased |
| 29 | + |
| 30 | +### Interfaces |
| 31 | + |
| 32 | +An `interface` specifies the functions a program must expose. It is a _compile-time_ concept: Leo verifies the implementing program satisfies the contract; no interface bytecode appears on-chain. |
| 33 | + |
| 34 | +```leo |
| 35 | +// In voting_power.aleo |
| 36 | +interface VotingStrategy { |
| 37 | + fn compute_power(balance: u64) -> u64; |
| 38 | +} |
| 39 | +
|
| 40 | +program voting_power.aleo : VotingStrategy { |
| 41 | + fn compute_power(balance: u64) -> u64 { return balance; } |
| 42 | + ... |
| 43 | +} |
| 44 | +
|
| 45 | +// In quadratic_power.aleo — same interface, different implementation |
| 46 | +program quadratic_power.aleo : VotingStrategy { |
| 47 | + fn compute_power(balance: u64) -> u64 { /* floor(√balance) */ ... } |
| 48 | + ... |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +### Dynamic Dispatch |
| 53 | + |
| 54 | +The `identifier` type holds a program name resolved at runtime. The call syntax is: |
| 55 | + |
| 56 | +``` |
| 57 | +qualifier.aleo::Interface@(target)/function(args) |
| 58 | +│ │ |
| 59 | +│ └─ `identifier` value — resolved at runtime |
| 60 | +└─ program that declared the interface — fixed at compile time |
| 61 | +``` |
| 62 | + |
| 63 | +`governance.aleo` accepts `strategy: identifier` as a parameter and routes to whichever program the caller names: |
| 64 | + |
| 65 | +```leo |
| 66 | +fn get_voting_power(strategy: identifier, balance: u64) -> u64 { |
| 67 | + return voting_power.aleo::VotingStrategy@(strategy)/compute_power(balance); |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +Identifier literals use single quotes and can appear inline when the target is known at compile time: |
| 72 | + |
| 73 | +```leo |
| 74 | +fn compare_strategies(balance: u64) -> (u64, u64) { |
| 75 | + let linear: u64 = voting_power.aleo::VotingStrategy@('voting_power')/compute_power(balance); |
| 76 | + let quadratic: u64 = voting_power.aleo::VotingStrategy@('quadratic_power')/compute_power(balance); |
| 77 | + return (linear, quadratic); |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +### Dynamic Records |
| 82 | + |
| 83 | +A `dyn record` erases the concrete record type, allowing generic field access without knowing the full schema at the call site. Fields are read with a required type annotation; the operation fails at runtime if the field is missing or has the wrong type. |
| 84 | + |
| 85 | +```leo |
| 86 | +// In voting_power.aleo |
| 87 | +record VoterRecord { |
| 88 | + owner: address, |
| 89 | + balance: u64, |
| 90 | + epoch: u32, |
| 91 | +} |
| 92 | +
|
| 93 | +fn inspect_balance(vr: VoterRecord) -> u64 { |
| 94 | + let dr: dyn record = vr as dyn record; |
| 95 | + let bal: u64 = dr.balance; // type annotation required |
| 96 | + return bal; |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +This pattern is already useful within a single program. Future Leo versions will extend it to cross-program record passing, so governance.aleo can accept `VoterRecord`s from any token program — even ones deployed after governance itself — without recompilation. |
| 101 | + |
| 102 | +## Project Structure |
| 103 | + |
| 104 | +``` |
| 105 | +dynamic_dispatch/ |
| 106 | +├── run.sh |
| 107 | +├── voting_power/ # VotingStrategy interface + linear implementation |
| 108 | +│ ├── program.json |
| 109 | +│ └── src/ |
| 110 | +│ └── main.leo |
| 111 | +├── quadratic_power/ # Quadratic implementation of VotingStrategy |
| 112 | +│ ├── program.json |
| 113 | +│ └── src/ |
| 114 | +│ └── main.leo |
| 115 | +└── governance/ # Dynamic dispatch hub |
| 116 | + ├── program.json # lists voting_power.aleo + quadratic_power.aleo as network deps |
| 117 | + └── src/ |
| 118 | + └── main.leo |
| 119 | +``` |
| 120 | + |
| 121 | +**Note on interface scoping:** The `VotingStrategy` interface is declared at the top level of each program's `.leo` file (outside the `program {}` block). An interface defined in one program is not automatically exported to programs that depend on it via `program.json`; each program re-declares the interface with the matching signature, and Leo's type checker verifies the contract at compile time. |
| 122 | + |
| 123 | +**Note on network dependencies:** `governance/program.json` lists `voting_power.aleo` and `quadratic_power.aleo` with `"location": "network"`. When you run `leo build` (or any command that triggers a build), Leo fetches the deployed bytecode from the devnode endpoint into `build/imports/`, making it available to the local VM for proof generation. |
| 124 | + |
| 125 | +## Running the Example |
| 126 | + |
| 127 | +### Part 1 — Local execution (no devnode) |
| 128 | + |
| 129 | +Individual programs can be tested locally with `leo run`: |
| 130 | + |
| 131 | +```bash |
| 132 | +cd voting_power |
| 133 | +leo run compute_power 10000u64 # → 10000 (linear: 1:1) |
| 134 | +leo run compute_power 100u64 # → 100 |
| 135 | + |
| 136 | +cd ../quadratic_power |
| 137 | +leo run compute_power 10000u64 # → 100 (quadratic: √10000) |
| 138 | +leo run compute_power 100u64 # → 10 (quadratic: √100) |
| 139 | +``` |
| 140 | + |
| 141 | +### Part 2 — Dynamic dispatch (requires `leo devnode`) |
| 142 | + |
| 143 | +Dynamic calls require all three programs to be deployed. Start a local devnode in a **separate terminal** first: |
| 144 | + |
| 145 | +```bash |
| 146 | +leo devnode start --private-key APrivateKey1zkp8CZNn3yeCseEtxuVPbDCwSyhGW6yZKUYKfgXmcpoGPWH |
| 147 | +``` |
| 148 | + |
| 149 | +Then run the full demo from the `dynamic_dispatch/` directory: |
| 150 | + |
| 151 | +```bash |
| 152 | +./run.sh |
| 153 | +``` |
| 154 | + |
| 155 | +The script: |
| 156 | +1. Deploys `voting_power.aleo` — establishes the interface on-chain |
| 157 | +2. Deploys `quadratic_power.aleo` — registers a second implementation |
| 158 | +3. Deploys `governance.aleo` — the dispatch hub (holds no logic itself) |
| 159 | +4. Calls `get_voting_power` with `'voting_power'` → routes to linear |
| 160 | +5. Calls `get_voting_power` with `'quadratic_power'` → routes to quadratic |
| 161 | +6. Calls `proposal_passes` to show diverging outcomes under each strategy |
| 162 | +7. Calls `compare_strategies` to see both results side-by-side |
| 163 | + |
| 164 | +### Expected outputs |
| 165 | + |
| 166 | +| Function | Strategy | Balance | Result | |
| 167 | +|---|---|---|---| |
| 168 | +| `compute_power` | linear | 10 000 | 10 000 | |
| 169 | +| `compute_power` | quadratic | 10 000 | 100 | |
| 170 | +| `proposal_passes` | linear | 1 000 000 vs 10 000 | true (10 000× margin) | |
| 171 | +| `proposal_passes` | quadratic | 1 000 000 vs 10 000 | true (10× margin) | |
| 172 | +| `compare_strategies` | both | 10 000 | (10 000, 100) | |
| 173 | + |
| 174 | +## Key Takeaways |
| 175 | + |
| 176 | +- **Interfaces** are compile-time contracts — they enforce structure without on-chain overhead. |
| 177 | +- **Dynamic dispatch** lets a single contract target any compliant program at runtime; adding a new strategy requires only a new deployment, not a governance upgrade. |
| 178 | +- **`identifier` literals** (`'program_name'`) give you dynamic dispatch with a compile-time-known target when you want both flexibility and readability. |
| 179 | +- **`dyn record`** enables generic record inspection today and will enable cross-program record passing once support is complete. |
0 commit comments