Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates PBFT to allow a leader catchup #215

Merged
merged 2 commits into from
Jul 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/ordering/cosipbft/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ func (s *Service) doRound(ctx context.Context) error {
for resp := range resps {
_, err = resp.GetMessageOrError()
if err != nil {
s.logger.Warn().Err(err).Msg("view propagation failure")
s.logger.Warn().Err(err).Str("to", resp.GetFrom().String()).Msg("view propagation failure")
}
}

Expand Down
22 changes: 22 additions & 0 deletions core/ordering/cosipbft/pbft/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ type round struct {
committed bool
prevViews map[mino.Address]View
views map[mino.Address]View

// allows a node to catch up on a new leader
tentativeRound types.Digest
tentativeLeader uint16
}

// AuthorityReader is a function to help the state machine to read the current
Expand Down Expand Up @@ -303,6 +307,11 @@ func (m *pbftsm) Prepare(from mino.Address, block types.Block) (types.Digest, er
_, index := roster.GetPublicKey(from)

if uint16(index) != m.round.leader {
// Allows the node to catchup on the leader. It rejects the proposal,
// but will accept this leader later if the block is finalized and
// synced to us.
m.round.tentativeRound = block.GetHash()
m.round.tentativeLeader = uint16(index)
return id, xerrors.Errorf("'%v' is not the leader", from)
}

Expand Down Expand Up @@ -378,6 +387,8 @@ func (m *pbftsm) Finalize(id types.Digest, sig crypto.Signature) error {
return err
}

dela.Logger.Info().Msgf("finalize round with leader: %d", m.round.leader)

m.round.prevViews = nil
m.round.views = nil
m.round.committed = false
Expand Down Expand Up @@ -511,6 +522,8 @@ func (m *pbftsm) Expire(addr mino.Address) (View, error) {
m.Lock()
defer m.Unlock()

m.logger.Info().Msgf("expire: current leader is %d", m.round.leader)

roster, err := m.init()
if err != nil {
return View{}, xerrors.Errorf("init: %v", err)
Expand Down Expand Up @@ -581,6 +594,11 @@ func (m *pbftsm) CatchUp(link types.BlockLink) error {
return xerrors.Errorf("finalize failed: %v", err)
}

if link.GetTo() == m.round.tentativeRound {
m.round.leader = m.round.tentativeLeader
dela.Logger.Info().Msgf("accepting to set leader to: %d", m.round.leader)
}

m.round.views = nil
m.round.prevViews = nil
m.setState(InitialState)
Expand Down Expand Up @@ -831,5 +849,9 @@ func (obs observer) NotifyCallback(event interface{}) {
// be found with n = 3*f+1 where n is the number of participants.
func calculateThreshold(n int) int {
f := (n - 1) / 3
if f == 0 {
return n
}

return 2 * f
Comment on lines 851 to 856
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
f := (n - 1) / 3
if f == 0 {
return n
}
return 2 * f
if n <= 3 {
return n
}
f := (n - 1) / 3
return 2 * f

But it's the same thing...

}
48 changes: 48 additions & 0 deletions core/ordering/cosipbft/pbft/mod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,54 @@ func TestStateMachine_CatchUp(t *testing.T) {
require.EqualError(t, err, fake.Err("finalize failed: couldn't marshal signature"))
}

// checks that the tentative leader is set in case the tentative round is equal
// to the proposed block.
func TestStateMachine_CatchUp_Tentative_Leader_Accept(t *testing.T) {
tree, db, clean := makeTree(t)
defer clean()

ro := authority.FromAuthority(fake.NewAuthority(3, fake.NewSigner))

param := StateMachineParam{
Validation: simple.NewService(fakeExec{}, nil),
VerifierFactory: fake.VerifierFactory{},
Blocks: blockstore.NewInMemory(),
Genesis: blockstore.NewGenesisStore(),
Tree: blockstore.NewTreeCache(tree),
AuthorityReader: func(hashtree.Tree) (authority.Authority, error) {
return ro, nil
},
DB: db,
}

param.Genesis.Set(types.Genesis{})

root := types.Digest{}
copy(root[:], tree.GetRoot())

block, err := types.NewBlock(simple.NewResult(nil), types.WithTreeRoot(root), types.WithIndex(0))
require.NoError(t, err)

sm := NewStateMachine(param).(*pbftsm)

opts := []types.LinkOption{
types.WithSignatures(fake.Signature{}, fake.Signature{}),
types.WithChangeSet(authority.NewChangeSet()),
}

link, err := types.NewBlockLink(types.Digest{}, block, opts...)
require.NoError(t, err)

tentativeLeader := uint16(9)
sm.round.tentativeRound = link.GetTo()
sm.round.tentativeLeader = tentativeLeader

err = sm.CatchUp(link)
require.NoError(t, err)

require.Equal(t, tentativeLeader, sm.round.leader)
}

func TestStateMachine_Watch(t *testing.T) {
sm := &pbftsm{
watcher: core.NewWatcher(),
Expand Down