Skip to content

Commit

Permalink
Introduce sequence cancelled callback to the Backend interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan-Ethernal committed Apr 26, 2024
1 parent 00f7637 commit eaf2fd3
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 17 deletions.
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
15 changes: 11 additions & 4 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
validatorBackend
Notifier

// BuildProposal builds a new proposal for the given view (height and round)
BuildProposal(view *proto.View) []byte
Expand Down
2 changes: 1 addition & 1 deletion core/byzantine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/stretchr/testify/assert"
)

func TestByzantineBehaviour(t *testing.T) {
func TestByzanthineBehaviour(t *testing.T) {
t.Parallel()

//nolint:dupl
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
8 changes: 4 additions & 4 deletions core/validator_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ var (
errVotingPowerNotCorrect = errors.New("total voting power is zero or less")
)

// ValidatorBackend defines interface that has GetVotingPower
type ValidatorBackend interface {
// validatorBackend defines interface that has GetVotingPower
type validatorBackend interface {
// GetVotingPowers returns map of validators addresses and their voting powers for the specified height.
GetVotingPowers(height uint64) (map[string]*big.Int, error)
}
Expand All @@ -30,13 +30,13 @@ type ValidatorManager struct {
// the height specified in the current View
validatorsVotingPower map[string]*big.Int

backend ValidatorBackend
backend validatorBackend

log Logger
}

// NewValidatorManager creates new ValidatorManager
func NewValidatorManager(backend ValidatorBackend, log Logger) *ValidatorManager {
func NewValidatorManager(backend validatorBackend, log Logger) *ValidatorManager {
return &ValidatorManager{
quorumSize: big.NewInt(0),
backend: backend,
Expand Down

0 comments on commit eaf2fd3

Please sign in to comment.