Skip to content

Commit

Permalink
handle the case when epoch data is nil (#2590)
Browse files Browse the repository at this point in the history
Fixes #2584
  • Loading branch information
kishansagathiya authored Jun 20, 2022
1 parent 7e1014b commit 1fa1c65
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
35 changes: 25 additions & 10 deletions dot/state/epoch.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ import (
)

var (
ErrEpochNotInMemory = errors.New("epoch not found in memory map")
errHashNotInMemory = errors.New("hash not found in memory map")
errHashNotPersisted = errors.New("hash with next epoch not found in database")
ErrEpochNotInMemory = errors.New("epoch not found in memory map")
errHashNotInMemory = errors.New("hash not found in memory map")
errEpochDataNotFound = errors.New("epoch data not found in the database")
errHashNotPersisted = errors.New("hash with next epoch not found in database")
errNoPreRuntimeDigest = errors.New("header does not contain pre-runtime digest")
)

var (
Expand Down Expand Up @@ -225,7 +227,7 @@ func (s *EpochState) GetEpochForBlock(header *types.Header) (uint64, error) {
return (slotNumber - firstSlot) / s.epochLength, nil
}

return 0, errors.New("header does not contain pre-runtime digest")
return 0, errNoPreRuntimeDigest
}

// SetEpochData sets the epoch data for a given epoch
Expand All @@ -251,14 +253,19 @@ func (s *EpochState) GetEpochData(epoch uint64, header *types.Header) (*types.Ep

if err != nil && !errors.Is(err, chaindb.ErrKeyNotFound) {
return nil, fmt.Errorf("failed to get epoch data from database: %w", err)
} else if header == nil {
// if no header is given then skip the lookup in-memory
return epochData, nil
}

epochData, err = s.getEpochDataFromMemory(epoch, header)
if err != nil {
return nil, fmt.Errorf("failed to get epoch data from memory: %w", err)
// lookup in-memory only if header is given
if header != nil && errors.Is(err, chaindb.ErrKeyNotFound) {
epochData, err = s.getEpochDataFromMemory(epoch, header)
if err != nil {
return nil, fmt.Errorf("failed to get epoch data from memory: %w", err)
}
}

if epochData == nil {
return nil, fmt.Errorf("%w: for epoch %d and header with hash %s",
errEpochDataNotFound, epoch, header.Hash())
}

return epochData, nil
Expand Down Expand Up @@ -543,6 +550,10 @@ func (s *EpochState) StoreBABENextConfigData(epoch uint64, hash common.Hash, nex
// check if the header is in the database then it's been finalized and
// thus we can also set the corresponding EpochData in the database
func (s *EpochState) FinalizeBABENextEpochData(finalizedHeader *types.Header) error {
if finalizedHeader.Number == 0 {
return nil
}

s.nextEpochDataLock.Lock()
defer s.nextEpochDataLock.Unlock()

Expand Down Expand Up @@ -600,6 +611,10 @@ func (s *EpochState) FinalizeBABENextEpochData(finalizedHeader *types.Header) er
// check if the header is in the database then it's been finalized and
// thus we can also set the corresponding NextConfigData in the database
func (s *EpochState) FinalizeBABENextConfigData(finalizedHeader *types.Header) error {
if finalizedHeader.Number == 0 {
return nil
}

s.nextConfigDataLock.Lock()
defer s.nextConfigDataLock.Unlock()

Expand Down
2 changes: 2 additions & 0 deletions lib/babe/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ func (b *verifier) verifyAuthorshipRight(header *types.Header) error {
}

// check if the producer has equivocated, ie. have they produced a conflicting block?
// hashes is hashes of all blocks with same block number as header.Number
hashes := b.blockState.GetAllBlocksAtDepth(header.ParentHash)

for _, hash := range hashes {
Expand All @@ -364,6 +365,7 @@ func (b *verifier) verifyAuthorshipRight(header *types.Header) error {
existingBlockProducerIndex = d.AuthorityIndex
}

// same authority won't produce two different blocks at the same block number
if currentBlockProducerIndex == existingBlockProducerIndex && hash != header.Hash() {
return ErrProducerEquivocated
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/scale/varying_data_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type VaryingDataTypeValue interface {
Index() uint
}

// VaryingDataTypeSlice is used to represent []VaryingDataType. SCALE requires knowledge
// VaryingDataTypeSlice is used to represent []VaryingDataType. SCALE requires knowledge
// of the underlying data, so it is required to have the VaryingDataType required for decoding
type VaryingDataTypeSlice struct {
VaryingDataType
Expand Down

0 comments on commit 1fa1c65

Please sign in to comment.