|
| 1 | +--- |
| 2 | +sidebar_position: 11 |
| 3 | +title: OCaml reference tracking |
| 4 | +description: |
| 5 | + System for tracking correspondence between Rust and OCaml implementations |
| 6 | +slug: /developers/ocaml-reference-tracking |
| 7 | +--- |
| 8 | + |
| 9 | +# OCaml reference tracking |
| 10 | + |
| 11 | +This document describes the system for tracking correspondence between Rust code |
| 12 | +in this repository and the original OCaml implementation in the |
| 13 | +[Mina Protocol repository](https://github.com/MinaProtocol/mina). |
| 14 | + |
| 15 | +## Overview |
| 16 | + |
| 17 | +As mina-rust is a reimplementation of the Mina OCaml client, we maintain inline |
| 18 | +comments that reference the corresponding OCaml code. This helps developers: |
| 19 | + |
| 20 | +1. Understand the original implementation context |
| 21 | +2. Verify that the Rust implementation matches the OCaml behavior |
| 22 | +3. Track changes in the OCaml codebase that may require updates in Rust |
| 23 | + |
| 24 | +## Comment format |
| 25 | + |
| 26 | +OCaml references are added as doc comments directly above the Rust type or |
| 27 | +function: |
| 28 | + |
| 29 | +```rust |
| 30 | +/// OCaml reference: src/lib/mina_base/transaction_status.ml L:9-113 |
| 31 | +/// Commit: 55582d249cdb225f722dbbb3b1420ce7570d501f |
| 32 | +/// Last verified: 2025-10-08 |
| 33 | +pub enum TransactionFailure { |
| 34 | + // ... |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +### Format specification |
| 39 | + |
| 40 | +- **Line 1**: `/// OCaml reference: <path> L:<start>-<end>` |
| 41 | + - `<path>`: Path to the OCaml file relative to the Mina repository root |
| 42 | + - `L:<start>-<end>`: Line range in the OCaml file (optional but recommended) |
| 43 | +- **Line 2**: `/// Commit: <commit-hash>` |
| 44 | + - Full commit hash from the Mina repository |
| 45 | +- **Line 3**: `/// Last verified: <YYYY-MM-DD>` |
| 46 | + - Date when the reference was last verified to be accurate |
| 47 | + |
| 48 | +## Validation script |
| 49 | + |
| 50 | +The `.github/scripts/check-ocaml-refs.sh` script validates all OCaml references: |
| 51 | + |
| 52 | +```bash |
| 53 | +# Validate against compatible branch (default) |
| 54 | +./.github/scripts/check-ocaml-refs.sh |
| 55 | + |
| 56 | +# Validate against a specific branch |
| 57 | +./.github/scripts/check-ocaml-refs.sh --branch develop |
| 58 | + |
| 59 | +# Validate against a specific repository |
| 60 | +./.github/scripts/check-ocaml-refs.sh --repo https://github.com/MinaProtocol/mina.git --branch develop |
| 61 | + |
| 62 | +# Automatically update stale commit hashes |
| 63 | +./.github/scripts/check-ocaml-refs.sh --update |
| 64 | +``` |
| 65 | + |
| 66 | +### What the script checks |
| 67 | + |
| 68 | +1. **File existence**: Verifies the OCaml file exists at the specified path |
| 69 | +2. **Line ranges**: Validates that line ranges don't exceed the file length |
| 70 | +3. **Code consistency**: Verifies that the code at the referenced commit matches |
| 71 | + the code on the current branch (ensures the reference is still accurate) |
| 72 | +4. **Commit staleness**: Checks if the commit hash matches the current HEAD |
| 73 | + |
| 74 | +### Exit codes |
| 75 | + |
| 76 | +- `0`: All references are valid or only stale commits (warning) |
| 77 | +- `1`: Invalid references found (missing files or invalid line ranges) |
| 78 | + |
| 79 | +## Automated verification |
| 80 | + |
| 81 | +A GitHub Actions workflow runs weekly to: |
| 82 | + |
| 83 | +1. Validate all OCaml references against the latest `compatible` branch |
| 84 | +2. Automatically update stale commit hashes and verification dates |
| 85 | +3. Create a pull request with the updates |
| 86 | + |
| 87 | +The workflow can also be triggered manually via the Actions tab. |
| 88 | + |
| 89 | +## Adding new references |
| 90 | + |
| 91 | +When implementing new features from the OCaml codebase: |
| 92 | + |
| 93 | +1. Add the OCaml reference comment above your Rust type/function |
| 94 | +2. Use the current commit hash from the Mina repository |
| 95 | +3. Set the verification date to today |
| 96 | +4. Include line ranges to make it easy to find the exact code |
| 97 | + |
| 98 | +Example: |
| 99 | + |
| 100 | +```rust |
| 101 | +/// OCaml reference: src/lib/mina_base/fee_transfer.ml L:19-45 |
| 102 | +/// Commit: 55582d249cdb225f722dbbb3b1420ce7570d501f |
| 103 | +/// Last verified: 2025-10-08 |
| 104 | +#[derive(Debug, Clone, PartialEq)] |
| 105 | +pub struct SingleFeeTransfer { |
| 106 | + pub receiver_pk: CompressedPubKey, |
| 107 | + pub fee: Fee, |
| 108 | + pub fee_token: TokenId, |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +## Finding the correct line range |
| 113 | + |
| 114 | +To find the line range for an OCaml reference: |
| 115 | + |
| 116 | +1. Navigate to the file in the Mina repository |
| 117 | +2. Find the relevant type or function definition |
| 118 | +3. Note the starting and ending line numbers |
| 119 | +4. Use format `L:<start>-<end>` |
| 120 | + |
| 121 | +For single-line references, use the same number: `L:42-42` |
| 122 | + |
| 123 | +## Best practices |
| 124 | + |
| 125 | +1. **Be specific**: Include line ranges to point to exact definitions |
| 126 | +2. **Verify regularly**: Run the validation script before committing |
| 127 | +3. **Update when needed**: If you update Rust code based on OCaml changes, |
| 128 | + update the commit hash and date |
| 129 | +4. **Document differences**: If the Rust implementation intentionally differs, |
| 130 | + add a note explaining why |
| 131 | + |
| 132 | +## Example references |
| 133 | + |
| 134 | +See `ledger/src/scan_state/transaction_logic/mod.rs` for examples of properly |
| 135 | +formatted OCaml references. |
0 commit comments