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

fix(dot/digest): BABE NextEpochData and NextConfigData should be set on finalization #2339

Merged
merged 123 commits into from
Apr 1, 2022
Merged
Show file tree
Hide file tree
Changes from 83 commits
Commits
Show all changes
123 commits
Select commit Hold shift + click to select a range
6139712
feat: include substrate docker image
EclesioMeloJunior Jan 31, 2022
8c3fb1d
chore: add substrate docker images
EclesioMeloJunior Feb 2, 2022
c0be6dc
chore: formatting and add depends on yaml key
EclesioMeloJunior Feb 2, 2022
eda4e4a
chore: add datadog stuff again
EclesioMeloJunior Feb 2, 2022
dae59e2
chore: use alice peer id
EclesioMeloJunior Feb 2, 2022
6ccf8fb
chore: split substrate docker compose services into another file
EclesioMeloJunior Feb 2, 2022
1407b3a
chore: enable prometheus external and add datadog
EclesioMeloJunior Feb 2, 2022
0d59fac
Merge branch 'development' into eclesio/substrate-docker-image
EclesioMeloJunior Feb 4, 2022
5108412
chore: keep substrate ports same as gossamer node ports
EclesioMeloJunior Feb 4, 2022
9fa7ffb
chore: expose rpc and ws calls in substrate docker file
EclesioMeloJunior Feb 4, 2022
71a8f5e
chore: use same dns name and p2p id
EclesioMeloJunior Feb 4, 2022
f1fea2d
chore: use same key for both alice gossamer and substrate nodes
EclesioMeloJunior Feb 4, 2022
9b485f6
chore: fix dd golang cmd
EclesioMeloJunior Feb 4, 2022
cff9c8c
Update devnet/README.md
EclesioMeloJunior Feb 7, 2022
262b329
Update devnet/README.md
EclesioMeloJunior Feb 7, 2022
012b4b9
chore: update `substrate_alice` readme info
EclesioMeloJunior Feb 7, 2022
1d158a3
Update devnet/README.md
EclesioMeloJunior Feb 7, 2022
f0689ee
chore: add `/tcp` to expose ports
EclesioMeloJunior Feb 7, 2022
604ce4b
Merge branch 'eclesio/substrate-docker-image' of github.com:ChainSafe…
EclesioMeloJunior Feb 7, 2022
237647a
chore: adjust args and envs
EclesioMeloJunior Feb 7, 2022
ae0429e
chore: pin prometheus version, add read only volume and add trailing …
EclesioMeloJunior Feb 7, 2022
843d361
chore: improve README.md
EclesioMeloJunior Feb 7, 2022
bcb134e
Merge branch 'development' into eclesio/substrate-docker-image
EclesioMeloJunior Feb 7, 2022
579136c
chore: add port 7001 explicitly
EclesioMeloJunior Feb 7, 2022
c49bcf0
chore: remove prometheus `--config.file` flag
EclesioMeloJunior Feb 8, 2022
f03093a
chore: use env to chain
EclesioMeloJunior Feb 8, 2022
25946ca
update substrate_bob.Dockerfile
EclesioMeloJunior Feb 8, 2022
e8c1e9d
Merge branch 'development' into eclesio/substrate-docker-image
EclesioMeloJunior Feb 9, 2022
15f1377
chore: add datadog-agent start to gssmr alice node
EclesioMeloJunior Feb 9, 2022
94c6a87
Merge branch 'eclesio/substrate-docker-image' of github.com:ChainSafe…
EclesioMeloJunior Feb 9, 2022
17fd1f2
chore: add comment about different but same node keys
EclesioMeloJunior Feb 16, 2022
90529c8
chore: IncrementSetID to return the nextSetID
EclesioMeloJunior Feb 16, 2022
a3a0635
chore: digest HandleDigests check !ok first
EclesioMeloJunior Feb 16, 2022
6a55d07
feat: store babe next epoch data only at finalised blocks
EclesioMeloJunior Mar 1, 2022
68377a0
Merge branch 'development' into eclesio/babe-next-epoch
EclesioMeloJunior Mar 1, 2022
993dcf2
Merge branch 'development' into eclesio/babe-next-epoch
EclesioMeloJunior Mar 1, 2022
b251fc0
Merge branch 'eclesio/babe-next-epoch' of github.com:ChainSafe/gossam…
EclesioMeloJunior Mar 1, 2022
aac29ab
feat: include substrate docker image
EclesioMeloJunior Jan 31, 2022
4b68de7
chore: add substrate docker images
EclesioMeloJunior Feb 2, 2022
5cc4bb5
chore: formatting and add depends on yaml key
EclesioMeloJunior Feb 2, 2022
863e410
chore: add datadog stuff again
EclesioMeloJunior Feb 2, 2022
943a047
chore: use alice peer id
EclesioMeloJunior Feb 2, 2022
cb11c99
chore: split substrate docker compose services into another file
EclesioMeloJunior Feb 2, 2022
29bbcff
chore: enable prometheus external and add datadog
EclesioMeloJunior Feb 2, 2022
8d7b8cb
chore: keep substrate ports same as gossamer node ports
EclesioMeloJunior Feb 4, 2022
6e13e85
chore: expose rpc and ws calls in substrate docker file
EclesioMeloJunior Feb 4, 2022
c4a7c07
chore: use same dns name and p2p id
EclesioMeloJunior Feb 4, 2022
a9e2977
chore: use same key for both alice gossamer and substrate nodes
EclesioMeloJunior Feb 4, 2022
c3b530c
chore: fix dd golang cmd
EclesioMeloJunior Feb 4, 2022
ca48211
Update devnet/README.md
EclesioMeloJunior Feb 7, 2022
c7409d2
Update devnet/README.md
EclesioMeloJunior Feb 7, 2022
45a7987
chore: update `substrate_alice` readme info
EclesioMeloJunior Feb 7, 2022
f319ebf
chore: add `/tcp` to expose ports
EclesioMeloJunior Feb 7, 2022
ad9396c
Update devnet/README.md
EclesioMeloJunior Mar 1, 2022
d5433be
chore: adjust args and envs
EclesioMeloJunior Feb 7, 2022
a7163f9
chore: pin prometheus version, add read only volume and add trailing …
EclesioMeloJunior Feb 7, 2022
6a0930d
chore: improve README.md
EclesioMeloJunior Feb 7, 2022
d2e50d3
chore: add port 7001 explicitly
EclesioMeloJunior Feb 7, 2022
0efccc0
chore: add datadog-agent start to gssmr alice node
EclesioMeloJunior Feb 9, 2022
97cd61b
chore: remove prometheus `--config.file` flag
EclesioMeloJunior Feb 8, 2022
c17eeed
chore: use env to chain
EclesioMeloJunior Feb 8, 2022
5a23168
update substrate_bob.Dockerfile
EclesioMeloJunior Feb 8, 2022
630ad29
chore: add comment about different but same node keys
EclesioMeloJunior Feb 16, 2022
624fbb1
chore: IncrementSetID to return the nextSetID
EclesioMeloJunior Feb 16, 2022
d9f8627
chore: digest HandleDigests check !ok first
EclesioMeloJunior Feb 16, 2022
79cb4bf
feat: store babe next epoch data only at finalised blocks
EclesioMeloJunior Mar 1, 2022
799315c
Merge branch 'eclesio/babe-next-epoch' of github.com:ChainSafe/gossam…
EclesioMeloJunior Mar 1, 2022
f728564
chore: resolve conflicts
EclesioMeloJunior Mar 2, 2022
3b9566d
chore: remove unused
EclesioMeloJunior Mar 4, 2022
57bfff4
chore: rename function and change log message
EclesioMeloJunior Mar 4, 2022
de52526
chore: fix babe next epoch data/config while not finalized chain
EclesioMeloJunior Mar 8, 2022
9691a91
chore: allow forks validate blocks using the right epoch data
EclesioMeloJunior Mar 9, 2022
9437e35
Merge branch 'development' into eclesio/babe-next-epoch
EclesioMeloJunior Mar 9, 2022
849c9a8
chore: add exported comments and improve VerifyBlock comment
EclesioMeloJunior Mar 9, 2022
3c78b35
chore: fix lint, removing shadowing
EclesioMeloJunior Mar 9, 2022
409d2eb
chore: promising integration test to cover babe digest handler
EclesioMeloJunior Mar 11, 2022
63bcd2b
chore: integration test added to babe verify block
EclesioMeloJunior Mar 11, 2022
51eb04c
chore: needs improvent at chain finalization
EclesioMeloJunior Mar 11, 2022
2d5f3d1
chore: integration test done!
EclesioMeloJunior Mar 11, 2022
20137f5
chore: fix lint
EclesioMeloJunior Mar 11, 2022
a66e755
Merge branch 'development' into eclesio/babe-next-epoch
EclesioMeloJunior Mar 14, 2022
e6f78e2
chore: addressing comments at digest pkg
EclesioMeloJunior Mar 14, 2022
eece1fe
chore: wrap errors at granpda pkg and fix lll at verify_integration_test
EclesioMeloJunior Mar 14, 2022
8afbded
Merge branch 'development' into eclesio/babe-next-epoch
EclesioMeloJunior Mar 18, 2022
de47880
chore: improve comments
EclesioMeloJunior Mar 18, 2022
83b7328
chore: improving state/epoch tests
EclesioMeloJunior Mar 21, 2022
ada7efd
chore: return wrapped error at `babe digest finalization`
EclesioMeloJunior Mar 23, 2022
8f2b866
chore: improve testing
EclesioMeloJunior Mar 23, 2022
9f824b4
chore: improve exported comment
EclesioMeloJunior Mar 23, 2022
aace7da
chore: improve exported comment
EclesioMeloJunior Mar 23, 2022
5fc2508
chore: improve error decription
EclesioMeloJunior Mar 23, 2022
1764f20
chore: improve error description
EclesioMeloJunior Mar 23, 2022
fc9856f
Merge branch 'eclesio/babe-next-epoch' of github.com:ChainSafe/gossam…
EclesioMeloJunior Mar 23, 2022
ee1de46
chore: reorg `HasConfigData` and `HasEpochData`
EclesioMeloJunior Mar 23, 2022
51e11f8
chore: add two mutexes for protect `nextEpoch` and `nextConfig`
EclesioMeloJunior Mar 23, 2022
63484f9
chore: improve exported comment
EclesioMeloJunior Mar 23, 2022
cd4b675
Merge branch 'development' into eclesio/babe-next-epoch
EclesioMeloJunior Mar 23, 2022
c9e34df
chore: change `GetEpochData` to check in the memory if database doesn…
EclesioMeloJunior Mar 23, 2022
b968c2a
chore: scope specific variables
EclesioMeloJunior Mar 23, 2022
4338cba
unneeded `\n`
EclesioMeloJunior Mar 24, 2022
1e1f99d
chore: fix unit tests
EclesioMeloJunior Mar 24, 2022
dc91c1b
Merge branch 'development' into eclesio/babe-next-epoch
EclesioMeloJunior Mar 24, 2022
8f60c9f
Merge branch 'development' into eclesio/babe-next-epoch
EclesioMeloJunior Mar 24, 2022
6b7e6ec
chore: git add fix exported func name
EclesioMeloJunior Mar 24, 2022
649c224
Merge branch 'eclesio/babe-next-epoch' of github.com:ChainSafe/gossam…
EclesioMeloJunior Mar 24, 2022
cfed2f0
chore: fix tests
EclesioMeloJunior Mar 24, 2022
7621e2f
chore: remove DigestHandler from core pkg
EclesioMeloJunior Mar 25, 2022
e880e8a
Merge branch 'development' into eclesio/babe-next-epoch
EclesioMeloJunior Mar 28, 2022
2ac2752
chore: get current epoch at next epoch data and next config data
EclesioMeloJunior Mar 28, 2022
70a114e
chore: check `err != nil` before check `err is not ErrKeyNotFound`
EclesioMeloJunior Mar 28, 2022
1df169b
chore: rename error var name
EclesioMeloJunior Mar 30, 2022
e02015a
chore: flip babe digest finalization
EclesioMeloJunior Mar 30, 2022
eff747e
chore: improve exported comment
EclesioMeloJunior Mar 30, 2022
1694db0
chore: separate not related conditions
EclesioMeloJunior Mar 30, 2022
9b234c7
chore: remove DigestHandler from `dot/core` pkg
EclesioMeloJunior Mar 30, 2022
5193288
chore: changing `String()` method owner
EclesioMeloJunior Mar 30, 2022
c0665fc
chore: change function name to `persistBABEDigestsForNextEpoch`
EclesioMeloJunior Mar 30, 2022
56a1277
chore: fix typo
EclesioMeloJunior Mar 30, 2022
3715b00
chore: remove `nilnil` lint warn
EclesioMeloJunior Mar 30, 2022
094d32d
chore: remove unused core.DigestHandler mocks
EclesioMeloJunior Mar 30, 2022
054b942
chore: fix
EclesioMeloJunior Mar 30, 2022
42ec9e4
chore: unexport functions
EclesioMeloJunior Mar 31, 2022
5083234
chore: fix integration tests
EclesioMeloJunior Mar 31, 2022
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
3 changes: 0 additions & 3 deletions dot/core/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,6 @@ func (s *Service) handleBlock(block *types.Block, state *rtstorage.TrieState) er
logger.Debugf("imported block %s and stored state trie with root %s",
block.Header.Hash(), state.MustRoot())

// handle consensus digests
s.digestHandler.HandleDigests(&block.Header)

rt, err := s.blockState.GetRuntime(&block.Header.ParentHash)
if err != nil {
return err
Expand Down
115 changes: 63 additions & 52 deletions dot/digest/digest.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package digest
import (
"context"
"errors"
"fmt"

"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/internal/log"
Expand Down Expand Up @@ -118,12 +119,14 @@ func (h *Handler) NextGrandpaAuthorityChange() (next uint) {
func (h *Handler) HandleDigests(header *types.Header) {
for i, d := range header.Digest.Types {
val, ok := d.Value().(types.ConsensusDigest)
if ok {
err := h.handleConsensusDigest(&val, header)
if err != nil {
h.logger.Errorf("cannot handle digests for block number %d, index %d, digest %s: %s",
header.Number, i, d.Value(), err)
}
if !ok {
continue
}

err := h.handleConsensusDigest(&val, header)
if err != nil {
h.logger.Errorf("cannot handle digests for block number %d, index %d, digest %s: %s",
EclesioMeloJunior marked this conversation as resolved.
Show resolved Hide resolved
header.Number, i, d.Value(), err)
qdm12 marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand Down Expand Up @@ -175,15 +178,28 @@ func (h *Handler) handleGrandpaConsensusDigest(digest scale.VaryingDataType, hea
}

func (h *Handler) handleBabeConsensusDigest(digest scale.VaryingDataType, header *types.Header) error {
currEpoch, err := h.epochState.GetEpochForBlock(header)
if err != nil {
return fmt.Errorf("cannot get epoch for block %d (%s): %w",
header.Number, header.Hash(), err)
}

nextEpoch := currEpoch + 1
headerHash := header.Hash()

switch val := digest.Value().(type) {
case types.NextEpochData:
h.logger.Debugf("handling BABENextEpochData data: %v", digest)
return h.handleNextEpochData(val, header)
h.logger.Debugf("stored BABENextEpochData data: %v for hash: %s to epoch: %d", digest, headerHash, nextEpoch)
EclesioMeloJunior marked this conversation as resolved.
Show resolved Hide resolved
h.epochState.StoreBABENextEpochData(nextEpoch, headerHash, val)
return nil

case types.BABEOnDisabled:
return h.handleBABEOnDisabled(val, header)

case types.NextConfigData:
h.logger.Debugf("handling BABENextConfigData data: %v", digest)
return h.handleNextConfigData(val, header)
h.logger.Debugf("stored BABENextConfigData data: %v for hash: %s to epoch: %d", digest, headerHash, nextEpoch)
EclesioMeloJunior marked this conversation as resolved.
Show resolved Hide resolved
h.epochState.StoreBABENextConfigData(nextEpoch, headerHash, val)
return nil
}

return errors.New("invalid consensus digest data")
Expand All @@ -197,6 +213,7 @@ func (h *Handler) handleBlockImport(ctx context.Context) {
continue
}

h.HandleDigests(&block.Header)
err := h.handleGrandpaChangesOnImport(block.Header.Number)
if err != nil {
h.logger.Errorf("failed to handle grandpa changes on block import: %s", err)
Expand All @@ -215,7 +232,12 @@ func (h *Handler) handleBlockFinalisation(ctx context.Context) {
continue
}

err := h.handleGrandpaChangesOnFinalization(info.Header.Number)
err := h.setBABEDigestsOnFinalization(&info.Header)
if err != nil {
h.logger.Errorf("failed to store babe next epoch digest: %s", err)
}

err = h.handleGrandpaChangesOnFinalization(info.Header.Number)
if err != nil {
h.logger.Errorf("failed to handle grandpa changes on block finalisation: %s", err)
}
Expand All @@ -225,6 +247,34 @@ func (h *Handler) handleBlockFinalisation(ctx context.Context) {
}
}

// setBABEDigestsOnFinalization is called only when a block is finalised
// and defines the correct next epoch data and next config data
func (h *Handler) setBABEDigestsOnFinalization(finalizedHeader *types.Header) error {
kishansagathiya marked this conversation as resolved.
Show resolved Hide resolved
currEpoch, err := h.epochState.GetEpochForBlock(finalizedHeader)
if err != nil {
return fmt.Errorf("cannot get epoch for block %d (%s): %w",
finalizedHeader.Number, finalizedHeader.Hash(), err)
}

nextEpoch := currEpoch + 1

err = h.epochState.FinalizeBABENextEpochData(nextEpoch)
if err != nil {
h.logger.Debugf("failed to store BABENextEpochData for block number %d (%s): %w",
finalizedHeader.Number, finalizedHeader.Hash(), err)
return err
EclesioMeloJunior marked this conversation as resolved.
Show resolved Hide resolved
}

err = h.epochState.FinalizeBABENextConfigDataToFinalize(nextEpoch)
if err != nil {
h.logger.Debugf("failed to store BABENextConfigData for block number %d (%s): %w",
finalizedHeader.Number, finalizedHeader.Hash(), err)
return err
EclesioMeloJunior marked this conversation as resolved.
Show resolved Hide resolved
}

return nil
}

func (h *Handler) handleGrandpaChangesOnImport(num uint) error {
resume := h.grandpaResume
if resume != nil && num >= resume.atBlock {
Expand All @@ -233,17 +283,12 @@ func (h *Handler) handleGrandpaChangesOnImport(num uint) error {

fc := h.grandpaForcedChange
if fc != nil && num >= fc.atBlock {
err := h.grandpaState.IncrementSetID()
curr, err := h.grandpaState.IncrementSetID()
if err != nil {
return err
}

h.grandpaForcedChange = nil
curr, err := h.grandpaState.GetCurrentSetID()
if err != nil {
return err
}

h.logger.Debugf("incremented grandpa set id %d", curr)
}

Expand All @@ -258,17 +303,12 @@ func (h *Handler) handleGrandpaChangesOnFinalization(num uint) error {

sc := h.grandpaScheduledChange
if sc != nil && num >= sc.atBlock {
err := h.grandpaState.IncrementSetID()
curr, err := h.grandpaState.IncrementSetID()
if err != nil {
return err
}

h.grandpaScheduledChange = nil
curr, err := h.grandpaState.GetCurrentSetID()
if err != nil {
return err
}

h.logger.Debugf("incremented grandpa set id %d", curr)
}

Expand Down Expand Up @@ -381,32 +421,3 @@ func (h *Handler) handleBABEOnDisabled(_ types.BABEOnDisabled, _ *types.Header)
h.logger.Debug("handling BABEOnDisabled")
return nil
}

func (h *Handler) handleNextEpochData(act types.NextEpochData, header *types.Header) error {
currEpoch, err := h.epochState.GetEpochForBlock(header)
if err != nil {
return err
}

// set EpochState epoch data for upcoming epoch
data, err := act.ToEpochData()
if err != nil {
return err
}

h.logger.Debugf("setting data for block number %d and epoch %d with data: %v",
header.Number, currEpoch+1, data)
return h.epochState.SetEpochData(currEpoch+1, data)
}

func (h *Handler) handleNextConfigData(config types.NextConfigData, header *types.Header) error {
currEpoch, err := h.epochState.GetEpochForBlock(header)
if err != nil {
return err
}

h.logger.Debugf("setting BABE config data for block number %d and epoch %d with data: %v",
header.Number, currEpoch+1, config.ToConfigData())
// set EpochState config data for upcoming epoch
return h.epochState.SetConfigData(currEpoch+1, config.ToConfigData())
}
64 changes: 54 additions & 10 deletions dot/digest/digest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"testing"
"time"

"context"

"github.com/ChainSafe/gossamer/dot/state"
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/internal/log"
Expand Down Expand Up @@ -377,16 +379,13 @@ func createHeaderWithPreDigest(t *testing.T, slotNumber uint64) *types.Header {
require.NoError(t, err)

return &types.Header{
Number: 1,
Digest: digest,
}
}

func TestHandler_HandleNextEpochData(t *testing.T) {
expData := common.MustHexToBytes("0x0108d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d01000000000000008eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a4801000000000000004d58630000000000000000000000000000000000000000000000000000000000") //nolint:lll

handler := newTestHandler(t)
handler.Start()
defer handler.Stop()
expectedDigestBytes := common.MustHexToBytes("0x0108d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d01000000000000008eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a4801000000000000004d58630000000000000000000000000000000000000000000000000000000000") //nolint:lll

keyring, err := keystore.NewSr25519Keyring()
require.NoError(t, err)
Expand All @@ -411,7 +410,7 @@ func TestHandler_HandleNextEpochData(t *testing.T) {
data, err := scale.Marshal(digest)
require.NoError(t, err)

require.Equal(t, expData, data)
require.Equal(t, expectedDigestBytes, data)

d := &types.ConsensusDigest{
ConsensusEngineID: types.BabeEngineID,
Expand All @@ -420,9 +419,34 @@ func TestHandler_HandleNextEpochData(t *testing.T) {

header := createHeaderWithPreDigest(t, 10)

finalisedCh := make(chan *types.FinalisationInfo)

handler := newTestHandler(t)
handler.finalised = finalisedCh

ctx, cancel := context.WithCancel(context.Background())

doneCh := make(chan struct{})

go func() {
defer close(doneCh)
handler.handleBlockFinalisation(ctx)
}()
EclesioMeloJunior marked this conversation as resolved.
Show resolved Hide resolved

err = handler.handleConsensusDigest(d, header)
require.NoError(t, err)

finalisedCh <- &types.FinalisationInfo{
Header: *header,
Round: 1,
SetID: 1,
}

// Before check the epoch data was stored
// we need to wait for both handle functions to finish
cancel()
<-doneCh

stored, err := handler.epochState.(*state.EpochState).GetEpochData(1)
require.NoError(t, err)

Expand All @@ -437,10 +461,6 @@ func TestHandler_HandleNextEpochData(t *testing.T) {
}

func TestHandler_HandleNextConfigData(t *testing.T) {
handler := newTestHandler(t)
handler.Start()
defer handler.Stop()

var digest = types.NewBabeConsensusDigest()
err := digest.Set(types.NextConfigData{
C1: 1,
Expand All @@ -459,9 +479,33 @@ func TestHandler_HandleNextConfigData(t *testing.T) {

header := createHeaderWithPreDigest(t, 10)

finalisedCh := make(chan *types.FinalisationInfo)

handler := newTestHandler(t)
handler.finalised = finalisedCh

ctx, cancel := context.WithCancel(context.Background())

doneCh := make(chan struct{})
go func() {
defer close(doneCh)
handler.handleBlockFinalisation(ctx)
}()

err = handler.handleConsensusDigest(d, header)
require.NoError(t, err)

finalisedCh <- &types.FinalisationInfo{
Header: *header,
Round: 1,
SetID: 1,
}

// Before check the config data was stored
// we need to wait for both handle functions finish
cancel()
<-doneCh

act, ok := digest.Value().(types.NextConfigData)
if !ok {
t.Fatal()
Expand Down
8 changes: 7 additions & 1 deletion dot/digest/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package digest

import (
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/grandpa"
)

Expand All @@ -22,12 +23,17 @@ type EpochState interface {
GetEpochForBlock(header *types.Header) (uint64, error)
SetEpochData(epoch uint64, info *types.EpochData) error
SetConfigData(epoch uint64, info *types.ConfigData) error

StoreBABENextEpochData(epoch uint64, hash common.Hash, nextEpochData types.NextEpochData)
StoreBABENextConfigData(epoch uint64, hash common.Hash, nextEpochData types.NextConfigData)
FinalizeBABENextEpochData(epoch uint64) error
FinalizeBABENextConfigDataToFinalize(epoch uint64) error
}

// GrandpaState is the interface for the state.GrandpaState
type GrandpaState interface {
SetNextChange(authorities []grandpa.Voter, number uint) error
IncrementSetID() error
IncrementSetID() (newSetID uint64, err error)
SetNextPause(number uint) error
SetNextResume(number uint) error
GetCurrentSetID() (uint64, error)
Expand Down
8 changes: 7 additions & 1 deletion dot/state/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ func (bs *BlockState) GenesisHash() common.Hash {
return bs.genesisHash
}

// HasHeader returns if the db contains a header with the given hash
// HasHeader returns if the hash are stored in the memory unfinalised blocks or
// persisted in database
EclesioMeloJunior marked this conversation as resolved.
Show resolved Hide resolved
func (bs *BlockState) HasHeader(hash common.Hash) (bool, error) {
if bs.unfinalisedBlocks.getBlock(hash) != nil {
return true, nil
Expand All @@ -192,6 +193,11 @@ func (bs *BlockState) HasHeader(hash common.Hash) (bool, error) {
return bs.db.Has(headerKey(hash))
}

// HasHeaderInDatabase returns if only database contains a header with the given hash
EclesioMeloJunior marked this conversation as resolved.
Show resolved Hide resolved
func (bs *BlockState) HasHeaderInDatabase(hash common.Hash) (bool, error) {
return bs.db.Has(headerKey(hash))
}

// GetHeader returns a BlockHeader for a given hash
func (bs *BlockState) GetHeader(hash common.Hash) (header *types.Header, err error) {
header = bs.unfinalisedBlocks.getBlockHeader(hash)
Expand Down
Loading