From eaf2fd3360ce5113e57bad10ca41dc34031a3274 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Negovanovi=C4=87?= Date: Fri, 26 Apr 2024 10:32:37 +0200 Subject: [PATCH 1/3] Introduce sequence cancelled callback to the Backend interface --- .golangci.yml | 2 +- core/backend.go | 15 +++++++++++---- core/byzantine_test.go | 2 +- core/ibft.go | 9 +++++++-- core/mock_test.go | 19 ++++++++++++++----- core/validator_manager.go | 8 ++++---- 6 files changed, 38 insertions(+), 17 deletions(-) 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..1fa348b 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 + validatorBackend + Notifier // BuildProposal builds a new proposal for the given view (height and round) BuildProposal(view *proto.View) []byte diff --git a/core/byzantine_test.go b/core/byzantine_test.go index 8ceba06..e2f58d3 100644 --- a/core/byzantine_test.go +++ b/core/byzantine_test.go @@ -10,7 +10,7 @@ import ( "github.com/stretchr/testify/assert" ) -func TestByzantineBehaviour(t *testing.T) { +func TestByzanthineBehaviour(t *testing.T) { t.Parallel() //nolint:dupl 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 diff --git a/core/validator_manager.go b/core/validator_manager.go index 139f9fb..894ab48 100644 --- a/core/validator_manager.go +++ b/core/validator_manager.go @@ -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) } @@ -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, From 563d9098cba5689f334bf66a1d89cbd2a9746008 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Negovanovi=C4=87?= Date: Fri, 26 Apr 2024 10:35:33 +0200 Subject: [PATCH 2/3] Revert test rename --- core/byzantine_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/byzantine_test.go b/core/byzantine_test.go index e2f58d3..8ceba06 100644 --- a/core/byzantine_test.go +++ b/core/byzantine_test.go @@ -10,7 +10,7 @@ import ( "github.com/stretchr/testify/assert" ) -func TestByzanthineBehaviour(t *testing.T) { +func TestByzantineBehaviour(t *testing.T) { t.Parallel() //nolint:dupl From 6063d03ee210336e19a3d813e3dbe1d3ec7a1492 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Negovanovi=C4=87?= Date: Fri, 26 Apr 2024 10:50:32 +0200 Subject: [PATCH 3/3] Revert making ValidatorBackend internal interface --- core/backend.go | 2 +- core/validator_manager.go | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/core/backend.go b/core/backend.go index 1fa348b..8be7b82 100644 --- a/core/backend.go +++ b/core/backend.go @@ -69,7 +69,7 @@ type Notifier interface { type Backend interface { MessageConstructor Verifier - validatorBackend + ValidatorBackend Notifier // BuildProposal builds a new proposal for the given view (height and round) diff --git a/core/validator_manager.go b/core/validator_manager.go index 894ab48..139f9fb 100644 --- a/core/validator_manager.go +++ b/core/validator_manager.go @@ -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) } @@ -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,