Skip to content

Commit a2ac6c2

Browse files
fix(dot/digest): verify if next epoch already contains some definition (#2472)
* fix: verify if next epoch already contains some definition * chore: upgrade mockery version * chore: remove duplicated code with generics * chore: improve test comment Co-authored-by: Quentin McGaw <[email protected]> * chore: improve test comment Co-authored-by: Quentin McGaw <[email protected]> * chore: improve test comment Co-authored-by: Quentin McGaw <[email protected]> * chore: improve test comment Co-authored-by: Quentin McGaw <[email protected]> * chore: remove the go:generate from the `EpochState`'s comment Co-authored-by: Quentin McGaw <[email protected]> * chore: use functions to define mocks in the test cases * chore: split into two different functions and check for existence before persit * chore: fix the ci lint warns * chore: use a more descriptive name * chore: unexport `Data` field on epoch_test.go * chore: addressing last nits * chore: rename to a better name * chore: fix comments * chore: addressing comments * chore: update mockery verison * chore: update devnet mocks as well * chore: improve comments Co-authored-by: Quentin McGaw <[email protected]> * chore: improve comments Co-authored-by: Quentin McGaw <[email protected]> Co-authored-by: Quentin McGaw <[email protected]>
1 parent 6b0b146 commit a2ac6c2

30 files changed

+341
-190
lines changed

devnet/cmd/scale-down-ecs-service/mocks/ecsapi.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dot/digest/digest.go

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"errors"
99
"fmt"
1010

11-
"github.com/ChainSafe/gossamer/dot/state"
1211
"github.com/ChainSafe/gossamer/dot/types"
1312
"github.com/ChainSafe/gossamer/internal/log"
1413
"github.com/ChainSafe/gossamer/lib/services"
@@ -17,8 +16,6 @@ import (
1716

1817
var (
1918
_ services.Service = &Handler{}
20-
21-
ErrDefineNextEpoch = errors.New("cannot define next epoch data and config")
2219
)
2320

2421
// Handler is used to handle consensus messages and relevant authority updates to BABE and GRANDPA
@@ -242,9 +239,14 @@ func (h *Handler) handleBlockFinalisation(ctx context.Context) {
242239
continue
243240
}
244241

245-
err := h.persistBABEDigestsForNextEpoch(&info.Header)
242+
err := h.epochState.FinalizeBABENextEpochData(&info.Header)
243+
if err != nil {
244+
h.logger.Errorf("failed to persist babe next epoch data: %s", err)
245+
}
246+
247+
err = h.epochState.FinalizeBABENextConfigData(&info.Header)
246248
if err != nil {
247-
h.logger.Errorf("failed to store babe next epoch digest: %s", err)
249+
h.logger.Errorf("failed to persist babe next epoch config: %s", err)
248250
}
249251

250252
err = h.handleGrandpaChangesOnFinalization(info.Header.Number)
@@ -257,35 +259,6 @@ func (h *Handler) handleBlockFinalisation(ctx context.Context) {
257259
}
258260
}
259261

260-
// persistBABEDigestsForNextEpoch is called only when a block is finalised
261-
// and defines the correct next epoch data and next config data.
262-
func (h *Handler) persistBABEDigestsForNextEpoch(finalizedHeader *types.Header) error {
263-
currEpoch, err := h.epochState.GetEpochForBlock(finalizedHeader)
264-
if err != nil {
265-
return fmt.Errorf("cannot get epoch for block %d (%s): %w",
266-
finalizedHeader.Number, finalizedHeader.Hash(), err)
267-
}
268-
269-
nextEpoch := currEpoch + 1
270-
err = h.epochState.FinalizeBABENextEpochData(nextEpoch)
271-
if err != nil && !errors.Is(err, state.ErrEpochNotInMemory) {
272-
return fmt.Errorf("cannot finalize babe next epoch data for block number %d (%s): %w",
273-
finalizedHeader.Number, finalizedHeader.Hash(), err)
274-
}
275-
276-
err = h.epochState.FinalizeBABENextConfigData(nextEpoch)
277-
if err == nil {
278-
return nil
279-
} else if errors.Is(err, state.ErrEpochNotInMemory) {
280-
return fmt.Errorf("%w: %s", ErrDefineNextEpoch, err)
281-
}
282-
283-
// the epoch state does not contains any information about the next epoch
284-
return fmt.Errorf("cannot finalize babe next config data for block number %d (%s): %w",
285-
finalizedHeader.Number, finalizedHeader.Hash(), err)
286-
287-
}
288-
289262
func (h *Handler) handleGrandpaChangesOnImport(num uint) error {
290263
resume := h.grandpaResume
291264
if resume != nil && num >= resume.atBlock {

dot/digest/interface.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ type BlockState interface {
1818
FreeFinalisedNotifierChannel(ch chan *types.FinalisationInfo)
1919
}
2020

21+
//go:generate mockgen -destination=mock_epoch_state_test.go -package $GOPACKAGE . EpochState
22+
2123
// EpochState is the interface for state.EpochState
2224
type EpochState interface {
2325
GetEpochForBlock(header *types.Header) (uint64, error)
@@ -26,8 +28,8 @@ type EpochState interface {
2628

2729
StoreBABENextEpochData(epoch uint64, hash common.Hash, nextEpochData types.NextEpochData)
2830
StoreBABENextConfigData(epoch uint64, hash common.Hash, nextEpochData types.NextConfigData)
29-
FinalizeBABENextEpochData(epoch uint64) error
30-
FinalizeBABENextConfigData(epoch uint64) error
31+
FinalizeBABENextEpochData(finalizedHeader *types.Header) error
32+
FinalizeBABENextConfigData(finalizedHeader *types.Header) error
3133
}
3234

3335
// GrandpaState is the interface for the state.GrandpaState

dot/digest/mock_epoch_state_test.go

Lines changed: 131 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dot/rpc/modules/mocks/block_api.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dot/rpc/modules/mocks/block_finality_api.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dot/rpc/modules/mocks/block_producer_api.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dot/rpc/modules/mocks/core_api.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dot/rpc/modules/mocks/network_api.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dot/rpc/modules/mocks/rpcapi.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dot/rpc/modules/mocks/runtime_storage_api.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dot/rpc/modules/mocks/storage_api.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dot/rpc/modules/mocks/sync_state_api.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dot/rpc/modules/mocks/system_api.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dot/rpc/modules/mocks/transaction_state_api.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)