Raft consensus algorithm implemented in MoonBit.
moon add Lfan-ke/raft-moonbit// Drive a five-node cluster through the deterministic simulator.
let cluster = @raft.Cluster::new(["a", "b", "c", "d", "e"], seed=1)
let leader = cluster.run_until_leader(200) // elect a leader
let _ = cluster.propose(b"set x = 1") // replicate a command
let _ = cluster.run_until_committed(2, 200) // wait for commit
cluster.crash(leader.unwrap()) // inject a fault
let _ = cluster.run_until_leader(400) // a new leader takes over
assert_true(cluster.one_leader_per_term()) // safety still holds
assert_true(cluster.committed_agrees())git clone https://github.com/Lfan-ke/raft-moonbit && cd raft-moonbit
moon run cmd/examplecluster of 5 nodes, seed 1
elected leader: b
committed 'set x = 1' on a majority
crashed the leader
new leader: c
one leader per term : true
committed prefixes agree : true
safety invariants hold : truemoon run examples/00-helloworld # elect a leader
moon run examples/01-replicate # propose commands, confirm every node commits
moon run examples/02-fault-tolerance # crash, partition, drop packets - safety holds
moon run examples/03-operations # progress, leadership transfer, compaction
moon run examples/04-kvstore # a replicated key-value store on a StateMachine| Method | What it does |
|---|---|
| Transliteration | The 258 upstream tests ported over. 723 tests pass on the wasm / wasm-gc / js backends, with 100% line and branch coverage (3094/3094 points) and zero warnings under moon check --deny-warn — CI fails the build if either coverage number regresses. |
| Adversarial audit | An audit whose sole instruction is to falsify — to find implemented-but-unwired code: a field nobody fills, a parameter forever default, a method with no caller, an ADT variant never constructed. |
| Differential trace (difftest) | The same scenarios drive etcd's RawNode and this port, compared event-by-event with upstream pinned as a git submodule. Directory restructuring and idiomatic cleanup are held to zero trace drift. |
moon build --target wasm --release # -> _build/wasm/release/build/demo/demo.wasm
cp _build/wasm/release/build/demo/demo.wasm docs/raft-moonbit.wasm
python3 -m http.server 8099 --directory docs # then open http://localhost:8099/| Package | Responsibility |
|---|---|
| quorum/ | Majority and joint-configuration vote counting |
| tracker/ | Per-follower Progress (Probe / Replicate / Snapshot) and the Inflights window |
| raftpb/ | On-the-wire types: Entry, Message, RPCs, HardState, Snapshot, ConfState, entry sizing |
| confchange/ | Configurations, joint consensus, ConfChange and the config Changer |
| storage/ | MemoryStorage, the write-ahead log, and the LogStore / RaftStorage traits |
| log/ | RaftLog, the unstable tail, term lookup and bounded slices |
| core/ | The consensus engine: RaftNode / Node step & dispatch, election, replication, snapshots, ReadIndex, leader lease, check-quorum, RawNode / Ready, Config, and the simulator |
| demo/ | The browser bridge: a flat wasm API over Cluster, one Web Worker per node |
The differential-testing harness lives on the difftest branch.
fn demo_run(ids : Array[String], seed : UInt64, elect_ticks : Int, commit_ticks : Int, reelect_ticks : Int) -> DemoReportlet no_limit : UInt64Raft consensus algorithm implemented in MoonBit.