diff --git a/.golangci.yml b/.golangci.yml index 337a935..9031b14 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -352,7 +352,7 @@ linters-settings: - name: max-public-structs severity: warning disabled: false - arguments: [ 3 ] + arguments: [ 8 ] # https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#modifies-parameter - name: modifies-parameter severity: warning diff --git a/core/backend.go b/core/backend.go index 3b86088..8be7b82 100644 --- a/core/backend.go +++ b/core/backend.go @@ -55,15 +55,22 @@ type Verifier interface { IsValidCommittedSeal(proposalHash []byte, committedSeal *messages.CommittedSeal) bool } +// Notifier contains callback functions that notifies about consensus execution +type Notifier interface { + // RoundStarts notifies the backend implementation whenever new round is about to start + RoundStarts(view *proto.View) error + + // SequenceCancelled notifies the backend implementation whenever a sequence is cancelled + SequenceCancelled(view *proto.View) error +} + // Backend defines an interface all backend implementations // need to implement type Backend interface { MessageConstructor Verifier ValidatorBackend - - // StartRound notifies the backend implementation whenever new round is about to start - StartRound(view *proto.View) error + Notifier // BuildProposal builds a new proposal for the given view (height and round) BuildProposal(view *proto.View) []byte diff --git a/core/ibft.go b/core/ibft.go index 63d14c9..87f4743 100644 --- a/core/ibft.go +++ b/core/ibft.go @@ -323,8 +323,8 @@ func (i *IBFT) RunSequence(ctx context.Context, h uint64) { for { view := i.state.getView() - if err := i.backend.StartRound(view); err != nil { - i.log.Error("failed to handle start round callback on backend", "round", view.Round, "err", err) + if err := i.backend.RoundStarts(view); err != nil { + i.log.Error("failed to handle start round callback on backend", "view", view, "err", err) } i.log.Info("round started", "round", view.Round) @@ -382,6 +382,11 @@ func (i *IBFT) RunSequence(ctx context.Context, h uint64) { return case <-ctxRound.Done(): teardown() + + if err := i.backend.SequenceCancelled(view); err != nil { + i.log.Error("failed to handle sequence cancelled callback on backend", "view", view, "err", err) + } + i.log.Debug("sequence cancelled") return diff --git a/core/mock_test.go b/core/mock_test.go index 90adef7..853eeb5 100644 --- a/core/mock_test.go +++ b/core/mock_test.go @@ -64,7 +64,7 @@ type buildRoundChangeMessageDelegate func( type insertProposalDelegate func(*proto.Proposal, []*messages.CommittedSeal) type idDelegate func() []byte type getVotingPowerDelegate func(uint64) (map[string]*big.Int, error) -type startRoundDelegate func(*proto.View) error +type viewNotificationDelegate func(*proto.View) error var _ Backend = &mockBackend{} @@ -84,7 +84,8 @@ type mockBackend struct { insertProposalFn insertProposalDelegate idFn idDelegate getVotingPowerFn getVotingPowerDelegate - startRoundFn startRoundDelegate + roundStartsFn viewNotificationDelegate + sequenceCancelledFn viewNotificationDelegate } func (m mockBackend) ID() []byte { @@ -204,9 +205,17 @@ func (m mockBackend) GetVotingPowers(height uint64) (map[string]*big.Int, error) return map[string]*big.Int{}, nil } -func (m mockBackend) StartRound(view *proto.View) error { - if m.startRoundFn != nil { - return m.startRoundFn(view) +func (m mockBackend) RoundStarts(view *proto.View) error { + if m.roundStartsFn != nil { + return m.roundStartsFn(view) + } + + return nil +} + +func (m mockBackend) SequenceCancelled(view *proto.View) error { + if m.sequenceCancelledFn != nil { + return m.sequenceCancelledFn(view) } return nil