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

fix bug introduced in #835 #849

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
21 changes: 19 additions & 2 deletions consensus/consensusfsm/fsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,14 @@ func NewConsensusFSM(cfg Config, ctx Context, clock clock.Clock) (*ConsensusFSM,
sAcceptProposalEndorsement, // not enough endorsements
sAcceptLockEndorsement, // enough endorsements
}).
AddTransition(
sAcceptProposalEndorsement,
eReceivePreCommitEndorsement,
cm.onReceiveProposalEndorsement,
[]fsm.State{
sAcceptProposalEndorsement, // not enough endorsements
sAcceptLockEndorsement, // enough endorsements
}).
AddTransition(
sAcceptProposalEndorsement,
eStopReceivingProposalEndorsement,
Expand All @@ -161,6 +169,14 @@ func NewConsensusFSM(cfg Config, ctx Context, clock clock.Clock) (*ConsensusFSM,
sAcceptLockEndorsement, // not enough endorsements
sAcceptPreCommitEndorsement, // reach commit agreement, jump to next step
}).
AddTransition(
sAcceptLockEndorsement,
eReceivePreCommitEndorsement,
cm.onReceiveLockEndorsement,
[]fsm.State{
sAcceptLockEndorsement, // not enough endorsements
sAcceptPreCommitEndorsement, // reach commit agreement, jump to next step
}).
AddTransition(
sAcceptPreCommitEndorsement,
eBroadcastPreCommitEndorsement,
Expand Down Expand Up @@ -297,11 +313,11 @@ func (m *ConsensusFSM) produce(evt *ConsensusEvent, delay time.Duration) {

func (m *ConsensusFSM) handle(evt *ConsensusEvent) error {
if m.ctx.IsStaleEvent(evt) {
m.ctx.Logger().Debug("stale event", zap.Any("event", evt))
m.ctx.Logger().Debug("stale event", zap.Any("event", evt.Type()))
return nil
}
if m.ctx.IsFutureEvent(evt) {
m.ctx.Logger().Debug("future event", zap.Any("event", evt))
m.ctx.Logger().Debug("future event", zap.Any("event", evt.Type()))
// TODO: find a more appropriate delay
m.produce(evt, m.cfg.UnmatchedEventInterval)
return nil
Expand Down Expand Up @@ -472,6 +488,7 @@ func (m *ConsensusFSM) onBroadcastPreCommitEndorsement(evt fsm.Event) (fsm.State
if !ok {
return sAcceptPreCommitEndorsement, errors.Wrap(ErrEvtCast, "failed to cast to consensus event")
}
m.ctx.Logger().Debug("broadcast pre-commit endorsement")
m.ctx.Broadcast(cEvt.Data())
m.produce(cEvt, m.cfg.CommitTTL)

Expand Down
37 changes: 30 additions & 7 deletions consensus/consensusfsm/fsm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestBackdoorEvt(t *testing.T) {
}
}

func TestStateTransitions(t *testing.T) {
func TestStateTransitionFunctions(t *testing.T) {
t.Parallel()
require := require.New(t)
ctrl := gomock.NewController(t)
Expand Down Expand Up @@ -231,18 +231,23 @@ func TestStateTransitions(t *testing.T) {
require.Equal(sAcceptProposalEndorsement, state)
})
t.Run("is-not-locked", func(t *testing.T) {
mockCtx.EXPECT().NewLockEndorsement(gomock.Any()).Return(nil, nil).Times(1)
mockEndorsement := NewMockEndorsement(ctrl)
mockCtx.EXPECT().NewLockEndorsement(gomock.Any()).Return(nil, nil).Times(2)
state, err := cfsm.onReceiveProposalEndorsement(&ConsensusEvent{
eventType: eReceiveProposalEndorsement,
data: mockEndorsement,
data: NewMockEndorsement(ctrl),
})
require.NoError(err)
require.Equal(sAcceptProposalEndorsement, state)
state, err = cfsm.onReceiveProposalEndorsement(&ConsensusEvent{
eventType: eReceivePreCommitEndorsement,
data: NewMockEndorsement(ctrl),
})
require.NoError(err)
require.Equal(sAcceptProposalEndorsement, state)
})
t.Run("is-locked", func(t *testing.T) {
mockCtx.EXPECT().NewLockEndorsement(gomock.Any()).Return(NewMockEndorsement(ctrl), nil).Times(1)
mockCtx.EXPECT().Broadcast(gomock.Any()).Return().Times(1)
mockCtx.EXPECT().NewLockEndorsement(gomock.Any()).Return(NewMockEndorsement(ctrl), nil).Times(2)
mockCtx.EXPECT().Broadcast(gomock.Any()).Return().Times(2)
state, err := cfsm.onReceiveProposalEndorsement(&ConsensusEvent{
eventType: eReceiveProposalEndorsement,
data: NewMockEndorsement(ctrl),
Expand All @@ -251,6 +256,14 @@ func TestStateTransitions(t *testing.T) {
require.Equal(sAcceptLockEndorsement, state)
evt := <-cfsm.evtq
require.Equal(eReceiveLockEndorsement, evt.Type())
state, err = cfsm.onReceiveProposalEndorsement(&ConsensusEvent{
eventType: eReceivePreCommitEndorsement,
data: NewMockEndorsement(ctrl),
})
require.NoError(err)
require.Equal(sAcceptLockEndorsement, state)
evt = <-cfsm.evtq
require.Equal(eReceiveLockEndorsement, evt.Type())
})
})
t.Run("onStopReceivingProposalEndorsement", func(t *testing.T) {
Expand All @@ -275,13 +288,19 @@ func TestStateTransitions(t *testing.T) {
require.Equal(sAcceptLockEndorsement, state)
})
t.Run("not-ready-to-pre-commit", func(t *testing.T) {
mockCtx.EXPECT().NewPreCommitEndorsement(gomock.Any()).Return(nil, nil).Times(1)
mockCtx.EXPECT().NewPreCommitEndorsement(gomock.Any()).Return(nil, nil).Times(2)
state, err := cfsm.onReceiveLockEndorsement(&ConsensusEvent{
eventType: eReceiveLockEndorsement,
data: NewMockEndorsement(ctrl),
})
require.NoError(err)
require.Equal(sAcceptLockEndorsement, state)
state, err = cfsm.onReceiveLockEndorsement(&ConsensusEvent{
eventType: eReceivePreCommitEndorsement,
data: NewMockEndorsement(ctrl),
})
require.NoError(err)
require.Equal(sAcceptLockEndorsement, state)
})
t.Run("ready-to-pre-commit", func(t *testing.T) {
mockCtx.EXPECT().NewPreCommitEndorsement(gomock.Any()).Return(NewMockEndorsement(ctrl), nil).Times(1)
Expand Down Expand Up @@ -421,3 +440,7 @@ func TestStateTransitions(t *testing.T) {
})
})
}

func TestStateTransitionEvents(t *testing.T) {
// TODO (zhi): add event tests
}