Introduce sequence cancelled callback to the Backend interface (#96)

pull/91/merge
Stefan Negovanović 6 months ago committed by GitHub
parent 00f7637226
commit e2032115c7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 2
      .golangci.yml
  2. 13
      core/backend.go
  3. 9
      core/ibft.go
  4. 19
      core/mock_test.go

@ -352,7 +352,7 @@ linters-settings:
- name: max-public-structs - name: max-public-structs
severity: warning severity: warning
disabled: false disabled: false
arguments: [ 3 ] arguments: [ 8 ]
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#modifies-parameter # https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#modifies-parameter
- name: modifies-parameter - name: modifies-parameter
severity: warning severity: warning

@ -55,15 +55,22 @@ type Verifier interface {
IsValidCommittedSeal(proposalHash []byte, committedSeal *messages.CommittedSeal) bool 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 // Backend defines an interface all backend implementations
// need to implement // need to implement
type Backend interface { type Backend interface {
MessageConstructor MessageConstructor
Verifier Verifier
ValidatorBackend ValidatorBackend
Notifier
// StartRound notifies the backend implementation whenever new round is about to start
StartRound(view *proto.View) error
// BuildProposal builds a new proposal for the given view (height and round) // BuildProposal builds a new proposal for the given view (height and round)
BuildProposal(view *proto.View) []byte BuildProposal(view *proto.View) []byte

@ -323,8 +323,8 @@ func (i *IBFT) RunSequence(ctx context.Context, h uint64) {
for { for {
view := i.state.getView() view := i.state.getView()
if err := i.backend.StartRound(view); err != nil { if err := i.backend.RoundStarts(view); err != nil {
i.log.Error("failed to handle start round callback on backend", "round", view.Round, "err", err) i.log.Error("failed to handle start round callback on backend", "view", view, "err", err)
} }
i.log.Info("round started", "round", view.Round) i.log.Info("round started", "round", view.Round)
@ -382,6 +382,11 @@ func (i *IBFT) RunSequence(ctx context.Context, h uint64) {
return return
case <-ctxRound.Done(): case <-ctxRound.Done():
teardown() 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") i.log.Debug("sequence cancelled")
return return

@ -64,7 +64,7 @@ type buildRoundChangeMessageDelegate func(
type insertProposalDelegate func(*proto.Proposal, []*messages.CommittedSeal) type insertProposalDelegate func(*proto.Proposal, []*messages.CommittedSeal)
type idDelegate func() []byte type idDelegate func() []byte
type getVotingPowerDelegate func(uint64) (map[string]*big.Int, error) type getVotingPowerDelegate func(uint64) (map[string]*big.Int, error)
type startRoundDelegate func(*proto.View) error type viewNotificationDelegate func(*proto.View) error
var _ Backend = &mockBackend{} var _ Backend = &mockBackend{}
@ -84,7 +84,8 @@ type mockBackend struct {
insertProposalFn insertProposalDelegate insertProposalFn insertProposalDelegate
idFn idDelegate idFn idDelegate
getVotingPowerFn getVotingPowerDelegate getVotingPowerFn getVotingPowerDelegate
startRoundFn startRoundDelegate roundStartsFn viewNotificationDelegate
sequenceCancelledFn viewNotificationDelegate
} }
func (m mockBackend) ID() []byte { 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 return map[string]*big.Int{}, nil
} }
func (m mockBackend) StartRound(view *proto.View) error { func (m mockBackend) RoundStarts(view *proto.View) error {
if m.startRoundFn != nil { if m.roundStartsFn != nil {
return m.startRoundFn(view) return m.roundStartsFn(view)
}
return nil
}
func (m mockBackend) SequenceCancelled(view *proto.View) error {
if m.sequenceCancelledFn != nil {
return m.sequenceCancelledFn(view)
} }
return nil return nil

Loading…
Cancel
Save