Skip to content

Commit 0c12e56

Browse files
committed
sbft: get rid of s.seq
Suggested-by: Gabor Hosszu Change-Id: I9cbb91bf0870a2c7cea9f6a54d2acb4b4e0c32f1 Signed-off-by: Simon Schubert <[email protected]>
1 parent aa92b80 commit 0c12e56

File tree

8 files changed

+35
-44
lines changed

8 files changed

+35
-44
lines changed

consensus/simplebft/backlog.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func (s *SBFT) testBacklog2(m *Msg, src uint64) bool {
3232
if !s.activeView {
3333
return true
3434
}
35-
if seq.Seq > s.cur.subject.Seq.Seq || seq.View > s.seq.View {
35+
if seq.Seq > s.cur.subject.Seq.Seq || seq.View > s.view {
3636
return true
3737
}
3838
return false

consensus/simplebft/checkpoint.go

-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ func (s *SBFT) handleCheckpoint(c *Checkpoint, src uint64) {
9898
batch := *s.cur.preprep.Batch
9999
batch.Signatures = cpset
100100
s.sys.Deliver(&batch)
101-
s.seq = *s.cur.subject.Seq
102101

103102
s.cur.timeout.Cancel()
104103
log.Infof("request %s %s completed on %d", s.cur.subject.Seq, hash2str(s.cur.subject.Digest), s.id)

consensus/simplebft/connection.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ func (s *SBFT) handleHello(h *Hello, src uint64) {
6868

6969
if s.sys.LastBatch().DecodeHeader().Seq < bh.Seq {
7070
s.sys.Deliver(h.Batch)
71-
s.seq.Seq = bh.Seq
7271
}
7372

7473
if h.NewView != nil {
@@ -89,8 +88,8 @@ func (s *SBFT) handleHello(h *Hello, src uint64) {
8988
return
9089
}
9190

92-
if s.seq.View <= h.NewView.View {
93-
s.seq.View = h.NewView.View
91+
if s.view < h.NewView.View {
92+
s.view = h.NewView.View
9493
}
9594
s.activeView = true
9695
}

consensus/simplebft/newview.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ import (
2222
)
2323

2424
func (s *SBFT) maybeSendNewView() {
25-
if s.lastNewViewSent != nil && s.lastNewViewSent.View == s.seq.View {
25+
if s.lastNewViewSent != nil && s.lastNewViewSent.View == s.view {
2626
return
2727
}
2828

2929
vset := make(map[uint64]*Signed)
3030
var vcs []*ViewChange
3131

3232
for src, state := range s.replicaState {
33-
if state.viewchange != nil && state.viewchange.View == s.seq.View {
33+
if state.viewchange != nil && state.viewchange.View == s.view {
3434
vset[uint64(src)] = state.signedViewchange
3535
vcs = append(vcs, state.viewchange)
3636
}
@@ -56,7 +56,7 @@ func (s *SBFT) maybeSendNewView() {
5656
}
5757

5858
nv := &NewView{
59-
View: s.seq.View,
59+
View: s.view,
6060
Vset: vset,
6161
Xset: xset,
6262
Batch: batch,
@@ -146,8 +146,8 @@ func (s *SBFT) processNewView() {
146146
return
147147
}
148148

149-
nv := s.replicaState[s.primaryIDView(s.seq.View)].newview
150-
if nv == nil || nv.View != s.seq.View {
149+
nv := s.replicaState[s.primaryIDView(s.view)].newview
150+
if nv == nil || nv.View != s.view {
151151
return
152152
}
153153

consensus/simplebft/newview_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
)
2323

2424
func TestXsetNoByz(t *testing.T) {
25-
s := &SBFT{config: Config{N: 4, F: 1}, seq: SeqView{3, 1}}
25+
s := &SBFT{config: Config{N: 4, F: 1}, view: 3}
2626
vcs := []*ViewChange{
2727
&ViewChange{
2828
View: 3,
@@ -57,7 +57,7 @@ func TestXsetNoByz(t *testing.T) {
5757
}
5858

5959
func TestXsetByz0(t *testing.T) {
60-
s := &SBFT{config: Config{N: 4, F: 1}, seq: SeqView{3, 1}}
60+
s := &SBFT{config: Config{N: 4, F: 1}, view: 3}
6161
vcs := []*ViewChange{
6262
&ViewChange{
6363
View: 3,
@@ -103,7 +103,7 @@ func TestXsetByz0(t *testing.T) {
103103
}
104104

105105
func TestXsetByz2(t *testing.T) {
106-
s := &SBFT{config: Config{N: 4, F: 1}, seq: SeqView{3, 1}}
106+
s := &SBFT{config: Config{N: 4, F: 1}, view: 3}
107107
vcs := []*ViewChange{
108108
&ViewChange{
109109
View: 3,

consensus/simplebft/simplebft.go

+13-20
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ type SBFT struct {
5757

5858
config Config
5959
id uint64
60-
seq SeqView
60+
view uint64
6161
batch []*Request
6262
batchTimer Canceller
6363
cur reqInfo
@@ -109,26 +109,17 @@ func New(id uint64, config *Config, sys System) (*SBFT, error) {
109109
}
110110
s.sys.SetReceiver(s)
111111

112-
lastBatch := s.sys.LastBatch()
113-
bh, err := s.checkBatch(lastBatch, false)
114-
if err != nil {
115-
panic(err)
116-
}
117-
118-
s.seq.View = 0
119-
s.seq.Seq = bh.Seq
120-
s.cur.subject.Seq = &s.seq
112+
s.view = 0
113+
s.cur.subject.Seq = &SeqView{}
121114
s.cur.sentCommit = true
122115
s.cur.executed = true
123116
s.cur.checkpointDone = true
124117
s.cur.timeout = dummyCanceller{}
125118

126119
pp := &Preprepare{}
127120
if s.sys.Restore("preprepare", pp) {
128-
s.seq.View = pp.Seq.View
129-
if pp.Seq.Seq > bh.Seq {
130-
s.seq = *pp.Seq
131-
s.seq.Seq -= 1
121+
s.view = pp.Seq.View
122+
if pp.Seq.Seq > s.seq() {
132123
s.acceptPreprepare(pp)
133124
}
134125
}
@@ -141,7 +132,7 @@ func New(id uint64, config *Config, sys System) (*SBFT, error) {
141132
s.cur.executed = true
142133
}
143134

144-
if s.seq.Seq == 0 {
135+
if s.seq() == 0 {
145136
s.activeView = true
146137
}
147138

@@ -156,21 +147,23 @@ func (s *SBFT) primaryIDView(v uint64) uint64 {
156147
}
157148

158149
func (s *SBFT) primaryID() uint64 {
159-
return s.primaryIDView(s.seq.View)
150+
return s.primaryIDView(s.view)
160151
}
161152

162153
func (s *SBFT) isPrimary() bool {
163154
return s.primaryID() == s.id
164155
}
165156

157+
func (s *SBFT) seq() uint64 {
158+
return s.sys.LastBatch().DecodeHeader().Seq
159+
}
160+
166161
func (s *SBFT) nextSeq() SeqView {
167-
seq := s.seq
168-
seq.Seq += 1
169-
return seq
162+
return SeqView{Seq: s.seq() + 1, View: s.view}
170163
}
171164

172165
func (s *SBFT) nextView() uint64 {
173-
return s.seq.View + 1
166+
return s.view + 1
174167
}
175168

176169
func (s *SBFT) noFaultyQuorum() int {

consensus/simplebft/viewchange.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@ package simplebft
1919
import "time"
2020

2121
func (s *SBFT) sendViewChange() {
22-
s.seq.View = s.nextView()
22+
s.view = s.nextView()
2323
s.cur.timeout.Cancel()
2424
s.activeView = false
2525
for src := range s.replicaState {
2626
state := &s.replicaState[src]
27-
if state.viewchange != nil && state.viewchange.View < s.seq.View {
27+
if state.viewchange != nil && state.viewchange.View < s.view {
2828
state.viewchange = nil
2929
}
3030
}
31-
log.Noticef("sending viewchange for view %d", s.seq.View)
31+
log.Noticef("sending viewchange for view %d", s.view)
3232

3333
var q, p []*Subject
3434
if s.cur.sentCommit {
@@ -39,10 +39,10 @@ func (s *SBFT) sendViewChange() {
3939
}
4040

4141
vc := &ViewChange{
42-
View: s.seq.View,
42+
View: s.view,
4343
Qset: q,
4444
Pset: p,
45-
Executed: s.seq.Seq,
45+
Executed: s.seq(),
4646
}
4747
svc := s.sign(vc)
4848
s.viewChangeTimer.Cancel()
@@ -68,8 +68,8 @@ func (s *SBFT) handleViewChange(svc *Signed, src uint64) {
6868
log.Noticef("invalid viewchange: %s", err)
6969
return
7070
}
71-
if vc.View < s.seq.View {
72-
log.Debugf("old view change from %s for view %d, we are in view %d", src, vc.View, s.seq.View)
71+
if vc.View < s.view {
72+
log.Debugf("old view change from %s for view %d, we are in view %d", src, vc.View, s.view)
7373
return
7474
}
7575
if ovc := s.replicaState[src].viewchange; ovc != nil && vc.View <= ovc.View {
@@ -94,9 +94,9 @@ func (s *SBFT) handleViewChange(svc *Signed, src uint64) {
9494

9595
if quorum == s.oneCorrectQuorum() {
9696
// catch up to the minimum view
97-
if s.seq.View < min {
97+
if s.view < min {
9898
log.Notice("we are behind on view change, resending for newer view")
99-
s.seq.View = min - 1
99+
s.view = min - 1
100100
s.sendViewChange()
101101
return
102102
}

consensus/simplebft/xset.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ nextm:
128128

129129
log.Debugf("selecting %d with %x", next, mtuple.Digest)
130130
xset = &Subject{
131-
Seq: &SeqView{Seq: next, View: s.seq.View},
131+
Seq: &SeqView{Seq: next, View: s.view},
132132
Digest: mtuple.Digest,
133133
}
134134
break nextm
@@ -141,7 +141,7 @@ nextm:
141141
if emptycount >= s.noFaultyQuorum() {
142142
log.Debugf("selecting null request for %d", next)
143143
xset = &Subject{
144-
Seq: &SeqView{Seq: next, View: s.seq.View},
144+
Seq: &SeqView{Seq: next, View: s.view},
145145
Digest: nil,
146146
}
147147
}

0 commit comments

Comments
 (0)