-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
simplebft.go
293 lines (249 loc) · 7.73 KB
/
simplebft.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package simplebft
import (
"fmt"
"math"
"reflect"
"time"
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/orderer/common/filter"
"github.com/op/go-logging"
)
const preprepared string = "preprepared"
const prepared string = "prepared"
const committed string = "committed"
const viewchange string = "viewchange"
// Receiver defines the API that is exposed by SBFT to the system.
type Receiver interface {
Receive(msg *Msg, src uint64)
Request(req []byte)
Connection(replica uint64)
GetChainId() string
}
// System defines the API that needs to be provided for SBFT.
type System interface {
Send(chainId string, msg *Msg, dest uint64)
Timer(d time.Duration, f func()) Canceller
Deliver(chainId string, batch *Batch, committers []filter.Committer)
AddReceiver(chainId string, receiver Receiver)
Persist(chainId string, key string, data proto.Message)
Restore(chainId string, key string, out proto.Message) bool
LastBatch(chainId string) *Batch
Sign(data []byte) []byte
CheckSig(data []byte, src uint64, sig []byte) error
Reconnect(chainId string, replica uint64)
Ordered(chainID string, req *Request) ([][]*Request, [][]filter.Committer, bool)
Cut(chainID string) ([]*Request, []filter.Committer)
}
// Canceller allows cancelling of a scheduled timer event.
type Canceller interface {
Cancel()
}
// SBFT is a simplified PBFT implementation.
type SBFT struct {
sys System
config Config
id uint64
view uint64
batches [][]*Request
batchTimer Canceller
cur reqInfo
activeView bool
lastNewViewSent *NewView
viewChangeTimeout time.Duration
viewChangeTimer Canceller
replicaState []replicaInfo
pending map[string]*Request
passedToBC map[string]*Request
chainId string
primarycommitters [][]filter.Committer
}
type reqInfo struct {
subject Subject
timeout Canceller
preprep *Preprepare
prep map[uint64]*Subject
commit map[uint64]*Subject
checkpoint map[uint64]*Checkpoint
prepared bool
committed bool
checkpointDone bool
committers []filter.Committer
}
type replicaInfo struct {
backLog []*Msg
hello *Hello
signedViewchange *Signed
viewchange *ViewChange
}
var log = logging.MustGetLogger("sbft")
type dummyCanceller struct{}
func (d dummyCanceller) Cancel() {}
// New creates a new SBFT instance.
func New(id uint64, chainID string, config *Config, sys System) (*SBFT, error) {
if config.F*3+1 > config.N {
return nil, fmt.Errorf("invalid combination of N (%d) and F (%d)", config.N, config.F)
}
s := &SBFT{
config: *config,
sys: sys,
id: id,
chainId: chainID,
viewChangeTimer: dummyCanceller{},
replicaState: make([]replicaInfo, config.N),
pending: make(map[string]*Request),
passedToBC: make(map[string]*Request),
batches: make([][]*Request, 0, 3),
primarycommitters: make([][]filter.Committer, 0),
}
s.sys.AddReceiver(chainID, s)
s.view = 0
s.cur.subject.Seq = &SeqView{}
s.cur.prepared = true
s.cur.committed = true
s.cur.checkpointDone = true
s.cur.timeout = dummyCanceller{}
s.activeView = true
svc := &Signed{}
if s.sys.Restore(s.chainId, viewchange, svc) {
vc := &ViewChange{}
err := proto.Unmarshal(svc.Data, vc)
if err != nil {
return nil, err
}
fmt.Println(fmt.Sprintf("rep %d VIEW %d %d", s.id, s.view, vc.View))
s.view = vc.View
s.replicaState[s.id].signedViewchange = svc
s.activeView = false
}
pp := &Preprepare{}
if s.sys.Restore(s.chainId, preprepared, pp) && pp.Seq.View >= s.view {
s.view = pp.Seq.View
s.activeView = true
if pp.Seq.Seq > s.seq() {
// TODO double add to BC?
_, committers := s.getCommittersFromBlockCutter(pp.Batch)
s.acceptPreprepare(pp, committers)
}
}
c := &Subject{}
if s.sys.Restore(s.chainId, prepared, c) && reflect.DeepEqual(c, &s.cur.subject) && c.Seq.View >= s.view {
s.cur.prepared = true
}
ex := &Subject{}
if s.sys.Restore(s.chainId, committed, ex) && reflect.DeepEqual(c, &s.cur.subject) && ex.Seq.View >= s.view {
s.cur.committed = true
}
s.cancelViewChangeTimer()
return s, nil
}
////////////////////////////////////////////////
func (s *SBFT) GetChainId() string {
return s.chainId
}
func (s *SBFT) primaryIDView(v uint64) uint64 {
return v % s.config.N
}
func (s *SBFT) primaryID() uint64 {
return s.primaryIDView(s.view)
}
func (s *SBFT) isPrimary() bool {
return s.primaryID() == s.id
}
func (s *SBFT) seq() uint64 {
return s.sys.LastBatch(s.chainId).DecodeHeader().Seq
}
func (s *SBFT) nextSeq() SeqView {
return SeqView{Seq: s.seq() + 1, View: s.view}
}
func (s *SBFT) nextView() uint64 {
return s.view + 1
}
func (s *SBFT) commonCaseQuorum() int {
//When N=3F+1 this should be 2F+1 (N-F)
//More generally, we need every two common case quorums of size X to intersect in at least F+1 orderers,
//hence 2X>=N+F+1, or X is:
return int(math.Ceil(float64(s.config.N+s.config.F+1) / float64(2)))
}
func (s *SBFT) viewChangeQuorum() int {
//When N=3F+1 this should be 2F+1 (N-F)
//More generally, we need every view change quorum to intersect with every common case quorum at least F+1 orderers, hence:
//Y >= N-X+F+1
return int(s.config.N+s.config.F+1) - s.commonCaseQuorum()
}
func (s *SBFT) oneCorrectQuorum() int {
return int(s.config.F + 1)
}
func (s *SBFT) broadcast(m *Msg) {
for i := uint64(0); i < s.config.N; i++ {
s.sys.Send(s.chainId, m, i)
}
}
////////////////////////////////////////////////
// Receive is the ingress method for SBFT messages.
func (s *SBFT) Receive(m *Msg, src uint64) {
log.Debugf("replica %d: received message from %d: %s", s.id, src, m)
if h := m.GetHello(); h != nil {
s.handleHello(h, src)
return
} else if req := m.GetRequest(); req != nil {
s.handleRequest(req, src)
return
} else if vs := m.GetViewChange(); vs != nil {
s.handleViewChange(vs, src)
return
} else if nv := m.GetNewView(); nv != nil {
s.handleNewView(nv, src)
return
}
if s.testBacklogMessage(m, src) {
log.Debugf("replica %d: message for future seq, storing for later", s.id)
s.recordBacklogMsg(m, src)
return
}
s.handleQueueableMessage(m, src)
}
func (s *SBFT) handleQueueableMessage(m *Msg, src uint64) {
if pp := m.GetPreprepare(); pp != nil {
s.handlePreprepare(pp, src)
return
} else if p := m.GetPrepare(); p != nil {
s.handlePrepare(p, src)
return
} else if c := m.GetCommit(); c != nil {
s.handleCommit(c, src)
return
} else if c := m.GetCheckpoint(); c != nil {
s.handleCheckpoint(c, src)
return
}
log.Warningf("replica %d: received invalid message from %d", s.id, src)
}
func (s *SBFT) deliverBatch(batch *Batch, committers []filter.Committer) {
if committers == nil {
log.Warningf("replica %d: commiter is nil", s.id)
panic("Committer is nil.")
}
s.cur.checkpointDone = true
s.cur.timeout.Cancel()
// s.primarycommitters[0]
s.sys.Deliver(s.chainId, batch, committers)
// s.primarycommitters = s.primarycommitters[1:]
for _, req := range batch.Payloads {
key := hash2str(hash(req))
log.Infof("replica %d: attempting to remove %x from pending", s.id, key)
delete(s.pending, key)
delete(s.passedToBC, key)
}
}