forked from hyperledger-labs/mirbft
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmirbft.go
206 lines (180 loc) · 6.6 KB
/
mirbft.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
// Package mirbft is a consensus library, implementing the Mir BFT consensus protocol.
//
// This library can be used by applications which desire distributed, byzantine fault
// tolerant consensus on message order. Unlike many traditional consensus algorithms,
// Mir BFT is a multi-leader protocol, allowing throughput to scale with the number of
// nodes (even over WANs), where the performance of traditional consenus algorithms
// begin to degrade.
package mirbft
import (
"context"
"fmt"
"github.com/IBM/mirbft/pkg/pb/msgs"
"github.com/IBM/mirbft/pkg/statemachine"
"github.com/IBM/mirbft/pkg/status"
"github.com/pkg/errors"
)
var ErrStopped = fmt.Errorf("stopped at caller request")
// WALStorage gives the state machine access to the most recently persisted state as
// requested by a previous instance of the state machine.
type WALStorage interface {
// LoadAll will invoke the given function with the the persisted entry
// iteratively, until the entire write-ahead-log has been loaded.
// If an error is encountered reading the log, it is returned and iteration stops.
LoadAll(forEach func(index uint64, p *msgs.Persistent)) error
}
// Node is the local instance of the MirBFT state machine through which the calling application
// proposes new messages, receives delegated actions, and returns action results.
// The methods exposed on Node are all thread safe, though typically, a single loop handles
// reading Actions, writing results, and writing ticks, while other go routines Propose and Step.
type Node struct {
Config *Config
s *serializer
}
func StandardInitialNetworkState(nodeCount int, clientCount int) *msgs.NetworkState {
nodes := []uint64{}
for i := 0; i < nodeCount; i++ {
nodes = append(nodes, uint64(i))
}
numberOfBuckets := int32(nodeCount)
checkpointInterval := numberOfBuckets * 5
maxEpochLength := checkpointInterval * 10
clients := make([]*msgs.NetworkState_Client, clientCount)
for i := range clients {
clients[i] = &msgs.NetworkState_Client{
Id: uint64(i),
Width: 100,
LowWatermark: 0,
}
}
return &msgs.NetworkState{
Config: &msgs.NetworkState_Config{
Nodes: nodes,
F: int32((nodeCount - 1) / 3),
NumberOfBuckets: numberOfBuckets,
CheckpointInterval: checkpointInterval,
MaxEpochLength: uint64(maxEpochLength),
},
Clients: clients,
}
}
type dummyWAL struct {
initialNetworkState *msgs.NetworkState
initialCheckpointValue []byte
}
func (dw *dummyWAL) LoadAll(forEach func(uint64, *msgs.Persistent)) error {
forEach(1, &msgs.Persistent{
Type: &msgs.Persistent_CEntry{
CEntry: &msgs.CEntry{
SeqNo: 0,
CheckpointValue: dw.initialCheckpointValue,
NetworkState: dw.initialNetworkState,
},
},
})
forEach(2, &msgs.Persistent{
Type: &msgs.Persistent_FEntry{
FEntry: &msgs.FEntry{
EndsEpochConfig: &msgs.EpochConfig{
Number: 0,
Leaders: dw.initialNetworkState.Config.Nodes,
},
},
},
})
return nil
}
// StartNewNode creates a node to join a fresh network. The first actions returned
// by the node will be to persist the initial parameters to the WAL so that on subsequent
// starts RestartNode should be invoked instead. The initialCheckpointValue should reflect
// any initial state of the application as well as the initialNetworkState passed to the start.
func StartNewNode(
config *Config,
initialNetworkState *msgs.NetworkState,
initialCheckpointValue []byte,
) (*Node, error) {
return RestartNode(
config,
&dummyWAL{
initialNetworkState: initialNetworkState,
initialCheckpointValue: initialCheckpointValue,
},
)
}
// RestartNode should be invoked for any subsequent starts of the Node. It reads
// the supplied WAL and Request store to initialize the state machine and therefore
// does not require network parameters as they are embedded into the WAL.
func RestartNode(
config *Config,
walStorage WALStorage,
) (*Node, error) {
serializer, err := newSerializer(config, walStorage)
if err != nil {
return nil, errors.Errorf("failed to start new node: %s", err)
}
return &Node{
Config: config,
s: serializer,
}, nil
}
// Stop terminates the resources associated with the node
func (n *Node) Stop() {
n.s.stop()
}
// Status returns a static snapshot in time of the internal state of the state machine.
// This method necessarily exposes some of the internal architecture of the system, and
// especially while the library is in development, the data structures may change substantially.
// This method returns a nil status and an error if the context ends. If the serializer go routine
// exits for any other reason, then a best effort is made to return the last (and final) status.
// This final status may be relied upon if it is non-nil. If the serializer exited at the user's
// request (because the done channel was closed), then ErrStopped is returned.
func (n *Node) Status(ctx context.Context) (*status.StateMachine, error) {
statusC := make(chan *status.StateMachine, 1)
select {
case <-ctx.Done():
return nil, ctx.Err()
case n.s.statusC <- statusC:
select {
case status := <-statusC:
return status, nil
case <-n.s.errC:
}
case <-n.s.errC:
// Note, we do not check the doneC, as if doneC closes, errC eventually closes
}
// The serializer has exited
n.s.exitMutex.Lock()
defer n.s.exitMutex.Unlock()
return n.s.exitStatus, n.s.exitErr
}
// Ready returns a channel which will deliver Actions for the user to perform.
// See the documentation for Actions regarding the detailed responsibilities
// of the caller.
func (n *Node) Actions() <-chan *statemachine.ActionList {
return n.s.actionsC
}
// Err should never close unless the consumer has requested the library exit
// by closing the doneC supplied at construction time. However, if unforeseen
// programatic errors violate the safety of the state machine, rather than panic
// the library will close this channel, and set an exit status. The consumer may
// wish to call Status() to get the cause of the exit, and a best effort exit status.
// If the exit was caused gracefully (by closing the done channel), then ErrStopped
// is returned.
func (n *Node) Err() <-chan struct{} {
return n.s.errC
}
// InjectEvents is called by the consumer after processing actions, or because
// events such as network sends or client requests have occurred.
// If the node is stopped, it returns the exit error otherwise nil is returned.
func (n *Node) InjectEvents(events *statemachine.EventList) error {
select {
case n.s.eventsC <- events:
return nil
case <-n.s.errC:
return n.s.getExitErr()
}
}