OCaml reference tracking
This document describes the system for tracking correspondence between Rust code in this repository and the original OCaml implementation in the Mina Protocol repository.
Overview
As mina-rust is a reimplementation of the Mina OCaml client, we maintain inline comments that reference the corresponding OCaml code. This helps developers:
- Understand the original implementation context
- Verify that the Rust implementation matches the OCaml behavior
- Track changes in the OCaml codebase that may require updates in Rust
Comment format
OCaml references are added as doc comments directly above the Rust type or function:
/// OCaml reference: src/lib/mina_base/transaction_status.ml L:9-113
/// Commit: 55582d249cdb225f722dbbb3b1420ce7570d501f
/// Last verified: 2025-10-08
pub enum TransactionFailure {
// ...
}
Format specification
- Line 1:
/// OCaml reference: <path> L:<start>-<end><path>: Path to the OCaml file relative to the Mina repository rootL:<start>-<end>: Line range in the OCaml file (optional but recommended)
- Line 2:
/// Commit: <commit-hash>- Full commit hash from the Mina repository
- Line 3:
/// Last verified: <YYYY-MM-DD>- Date when the reference was last verified to be accurate
Validation script
The .github/scripts/check-ocaml-refs.sh script validates all OCaml references:
# Validate against compatible branch (default)
./.github/scripts/check-ocaml-refs.sh
# Validate against a specific branch
./.github/scripts/check-ocaml-refs.sh --branch develop
# Validate against a specific repository
./.github/scripts/check-ocaml-refs.sh --repo https://github.com/MinaProtocol/mina.git --branch develop
# Automatically update stale commit hashes
./.github/scripts/check-ocaml-refs.sh --update
What the script checks
- File existence: Verifies the OCaml file exists at the specified path
- Line ranges: Validates that line ranges don't exceed the file length
- Code consistency: Verifies that the code at the referenced commit matches the code on the current branch (ensures the reference is still accurate)
- Commit staleness: Checks if the commit hash matches the current HEAD
Exit codes
0: All references are valid or only stale commits (warning)1: Invalid references found (missing files or invalid line ranges)
Automated verification
A GitHub Actions workflow runs weekly to:
- Validate all OCaml references against the latest
compatiblebranch - Automatically update stale commit hashes and verification dates
- Create a pull request with the updates
The workflow can also be triggered manually via the Actions tab.
Adding new references
When implementing new features from the OCaml codebase:
- Add the OCaml reference comment above your Rust type/function
- Use the current commit hash from the Mina repository
- Set the verification date to today
- Include line ranges to make it easy to find the exact code
Example:
/// OCaml reference: src/lib/mina_base/fee_transfer.ml L:19-45
/// Commit: 55582d249cdb225f722dbbb3b1420ce7570d501f
/// Last verified: 2025-10-08
#[derive(Debug, Clone, PartialEq)]
pub struct SingleFeeTransfer {
pub receiver_pk: CompressedPubKey,
pub fee: Fee,
pub fee_token: TokenId,
}
Finding the correct line range
To find the line range for an OCaml reference:
- Navigate to the file in the Mina repository
- Find the relevant type or function definition
- Note the starting and ending line numbers
- Use format
L:<start>-<end>
For single-line references, use the same number: L:42-42
Best practices
- Be specific: Include line ranges to point to exact definitions
- Verify regularly: Run the validation script before committing
- Update when needed: If you update Rust code based on OCaml changes, update the commit hash and date
- Document differences: If the Rust implementation intentionally differs, add a note explaining why
Example references
See
ledger/src/scan_state/transaction_logic/mod.rs
for examples of properly formatted OCaml references.