Skip to content

Commit

Permalink
sbft: get rid of s.seq
Browse files Browse the repository at this point in the history
Suggested-by: Gabor Hosszu
Change-Id: I9cbb91bf0870a2c7cea9f6a54d2acb4b4e0c32f1
Signed-off-by: Simon Schubert <sis@zurich.ibm.com>
  • Loading branch information
corecode committed Nov 3, 2016
1 parent aa92b80 commit 0c12e56
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 44 deletions.
2 changes: 1 addition & 1 deletion consensus/simplebft/backlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (s *SBFT) testBacklog2(m *Msg, src uint64) bool {
if !s.activeView {
return true
}
if seq.Seq > s.cur.subject.Seq.Seq || seq.View > s.seq.View {
if seq.Seq > s.cur.subject.Seq.Seq || seq.View > s.view {
return true
}
return false
Expand Down
1 change: 0 additions & 1 deletion consensus/simplebft/checkpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ func (s *SBFT) handleCheckpoint(c *Checkpoint, src uint64) {
batch := *s.cur.preprep.Batch
batch.Signatures = cpset
s.sys.Deliver(&batch)
s.seq = *s.cur.subject.Seq

s.cur.timeout.Cancel()
log.Infof("request %s %s completed on %d", s.cur.subject.Seq, hash2str(s.cur.subject.Digest), s.id)
Expand Down
5 changes: 2 additions & 3 deletions consensus/simplebft/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ func (s *SBFT) handleHello(h *Hello, src uint64) {

if s.sys.LastBatch().DecodeHeader().Seq < bh.Seq {
s.sys.Deliver(h.Batch)
s.seq.Seq = bh.Seq
}

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

if s.seq.View <= h.NewView.View {
s.seq.View = h.NewView.View
if s.view < h.NewView.View {
s.view = h.NewView.View
}
s.activeView = true
}
Expand Down
10 changes: 5 additions & 5 deletions consensus/simplebft/newview.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ import (
)

func (s *SBFT) maybeSendNewView() {
if s.lastNewViewSent != nil && s.lastNewViewSent.View == s.seq.View {
if s.lastNewViewSent != nil && s.lastNewViewSent.View == s.view {
return
}

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

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

nv := &NewView{
View: s.seq.View,
View: s.view,
Vset: vset,
Xset: xset,
Batch: batch,
Expand Down Expand Up @@ -146,8 +146,8 @@ func (s *SBFT) processNewView() {
return
}

nv := s.replicaState[s.primaryIDView(s.seq.View)].newview
if nv == nil || nv.View != s.seq.View {
nv := s.replicaState[s.primaryIDView(s.view)].newview
if nv == nil || nv.View != s.view {
return
}

Expand Down
6 changes: 3 additions & 3 deletions consensus/simplebft/newview_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
)

func TestXsetNoByz(t *testing.T) {
s := &SBFT{config: Config{N: 4, F: 1}, seq: SeqView{3, 1}}
s := &SBFT{config: Config{N: 4, F: 1}, view: 3}
vcs := []*ViewChange{
&ViewChange{
View: 3,
Expand Down Expand Up @@ -57,7 +57,7 @@ func TestXsetNoByz(t *testing.T) {
}

func TestXsetByz0(t *testing.T) {
s := &SBFT{config: Config{N: 4, F: 1}, seq: SeqView{3, 1}}
s := &SBFT{config: Config{N: 4, F: 1}, view: 3}
vcs := []*ViewChange{
&ViewChange{
View: 3,
Expand Down Expand Up @@ -103,7 +103,7 @@ func TestXsetByz0(t *testing.T) {
}

func TestXsetByz2(t *testing.T) {
s := &SBFT{config: Config{N: 4, F: 1}, seq: SeqView{3, 1}}
s := &SBFT{config: Config{N: 4, F: 1}, view: 3}
vcs := []*ViewChange{
&ViewChange{
View: 3,
Expand Down
33 changes: 13 additions & 20 deletions consensus/simplebft/simplebft.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type SBFT struct {

config Config
id uint64
seq SeqView
view uint64
batch []*Request
batchTimer Canceller
cur reqInfo
Expand Down Expand Up @@ -109,26 +109,17 @@ func New(id uint64, config *Config, sys System) (*SBFT, error) {
}
s.sys.SetReceiver(s)

lastBatch := s.sys.LastBatch()
bh, err := s.checkBatch(lastBatch, false)
if err != nil {
panic(err)
}

s.seq.View = 0
s.seq.Seq = bh.Seq
s.cur.subject.Seq = &s.seq
s.view = 0
s.cur.subject.Seq = &SeqView{}
s.cur.sentCommit = true
s.cur.executed = true
s.cur.checkpointDone = true
s.cur.timeout = dummyCanceller{}

pp := &Preprepare{}
if s.sys.Restore("preprepare", pp) {
s.seq.View = pp.Seq.View
if pp.Seq.Seq > bh.Seq {
s.seq = *pp.Seq
s.seq.Seq -= 1
s.view = pp.Seq.View
if pp.Seq.Seq > s.seq() {
s.acceptPreprepare(pp)
}
}
Expand All @@ -141,7 +132,7 @@ func New(id uint64, config *Config, sys System) (*SBFT, error) {
s.cur.executed = true
}

if s.seq.Seq == 0 {
if s.seq() == 0 {
s.activeView = true
}

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

func (s *SBFT) primaryID() uint64 {
return s.primaryIDView(s.seq.View)
return s.primaryIDView(s.view)
}

func (s *SBFT) isPrimary() bool {
return s.primaryID() == s.id
}

func (s *SBFT) seq() uint64 {
return s.sys.LastBatch().DecodeHeader().Seq
}

func (s *SBFT) nextSeq() SeqView {
seq := s.seq
seq.Seq += 1
return seq
return SeqView{Seq: s.seq() + 1, View: s.view}
}

func (s *SBFT) nextView() uint64 {
return s.seq.View + 1
return s.view + 1
}

func (s *SBFT) noFaultyQuorum() int {
Expand Down
18 changes: 9 additions & 9 deletions consensus/simplebft/viewchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ package simplebft
import "time"

func (s *SBFT) sendViewChange() {
s.seq.View = s.nextView()
s.view = s.nextView()
s.cur.timeout.Cancel()
s.activeView = false
for src := range s.replicaState {
state := &s.replicaState[src]
if state.viewchange != nil && state.viewchange.View < s.seq.View {
if state.viewchange != nil && state.viewchange.View < s.view {
state.viewchange = nil
}
}
log.Noticef("sending viewchange for view %d", s.seq.View)
log.Noticef("sending viewchange for view %d", s.view)

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

vc := &ViewChange{
View: s.seq.View,
View: s.view,
Qset: q,
Pset: p,
Executed: s.seq.Seq,
Executed: s.seq(),
}
svc := s.sign(vc)
s.viewChangeTimer.Cancel()
Expand All @@ -68,8 +68,8 @@ func (s *SBFT) handleViewChange(svc *Signed, src uint64) {
log.Noticef("invalid viewchange: %s", err)
return
}
if vc.View < s.seq.View {
log.Debugf("old view change from %s for view %d, we are in view %d", src, vc.View, s.seq.View)
if vc.View < s.view {
log.Debugf("old view change from %s for view %d, we are in view %d", src, vc.View, s.view)
return
}
if ovc := s.replicaState[src].viewchange; ovc != nil && vc.View <= ovc.View {
Expand All @@ -94,9 +94,9 @@ func (s *SBFT) handleViewChange(svc *Signed, src uint64) {

if quorum == s.oneCorrectQuorum() {
// catch up to the minimum view
if s.seq.View < min {
if s.view < min {
log.Notice("we are behind on view change, resending for newer view")
s.seq.View = min - 1
s.view = min - 1
s.sendViewChange()
return
}
Expand Down
4 changes: 2 additions & 2 deletions consensus/simplebft/xset.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ nextm:

log.Debugf("selecting %d with %x", next, mtuple.Digest)
xset = &Subject{
Seq: &SeqView{Seq: next, View: s.seq.View},
Seq: &SeqView{Seq: next, View: s.view},
Digest: mtuple.Digest,
}
break nextm
Expand All @@ -141,7 +141,7 @@ nextm:
if emptycount >= s.noFaultyQuorum() {
log.Debugf("selecting null request for %d", next)
xset = &Subject{
Seq: &SeqView{Seq: next, View: s.seq.View},
Seq: &SeqView{Seq: next, View: s.view},
Digest: nil,
}
}
Expand Down

0 comments on commit 0c12e56

Please sign in to comment.