https://github.com/mudler/go-libp2p-simple-raft
A simple raft wrapper for libp2p
https://github.com/mudler/go-libp2p-simple-raft
Last synced: 5 months ago
JSON representation
A simple raft wrapper for libp2p
- Host: GitHub
- URL: https://github.com/mudler/go-libp2p-simple-raft
- Owner: mudler
- License: apache-2.0
- Created: 2024-08-09T07:19:43.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2025-01-14T12:11:50.000Z (over 1 year ago)
- Last Synced: 2025-04-11T03:41:39.764Z (about 1 year ago)
- Language: Go
- Size: 24.4 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-libp2p-simple-raft
A simple raft wrapper for libp2p.
Motivation: I wanted to experiment and have a simple wrapper to work with raft and libp2p, hence why this.
### Usage
```go
import (
"github.com/mudler/go-libp2p-simple-raft"
)
// Structure nodes have to agree on
type raftState struct {
Value int
}
ctx := context.Background()
// start a libp2p node (not done here)
// NewSimpleRaft needs a libp2p host, a state to calculate consensus on, and a channel to listen for new peers that are discovered
w, err := simpleraft.NewSimpleRaft(ctx, h, &raftState{Value: 3}, peerChan)
if err != nil {
fmt.Println(err)
}
// Make sure states are syncronized
err = w.WaitForSync(ctx)
if err != nil {
fmt.Println(err)
}
// Check if we are leaders and update state
if !w.IsLeader() {
// we are not leaders, we can only read the state and/or know who is the leader
state, err:= w.GetState()
address, serverID := w.GetLeaderID()
return
}
// verify we are leaders
if err := w.VerifyLeader(); err != nil {
fmt.Println("error", err)
}
// try to write the state
nUpdates := 0
for {
if nUpdates >= 1000 {
break
}
newState := &raftState{nUpdates * 2}
// CommitState() blocks until the state has been
// agreed upon by everyone
agreedState, err := w.CommitState(newState)
if err != nil {
fmt.Println(err)
continue
}
if agreedState == nil {
fmt.Println("agreedState is nil: commited on a non-leader?")
continue
}
agreedRaftState := agreedState.(*raftState)
nUpdates++
if nUpdates%200 == 0 {
fmt.Printf("Performed %d updates. Current state value: %d\n",
nUpdates, agreedRaftState.Value)
}
}
```