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

Introduce sequence cancelled callback to the Backend interface #96

Merged
merged 3 commits into from
Apr 29, 2024
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 .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 10 additions & 3 deletions core/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions core/ibft.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
19 changes: 14 additions & 5 deletions core/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}

Expand All @@ -84,7 +84,8 @@ type mockBackend struct {
insertProposalFn insertProposalDelegate
idFn idDelegate
getVotingPowerFn getVotingPowerDelegate
startRoundFn startRoundDelegate
roundStartsFn viewNotificationDelegate
sequenceCancelledFn viewNotificationDelegate
}

func (m mockBackend) ID() []byte {
Expand Down Expand Up @@ -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
Expand Down
Loading