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/state): fix a bug in IsDescendantOf #3125

Merged
merged 16 commits into from
Feb 27, 2023
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
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
6 changes: 5 additions & 1 deletion dot/state/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -773,10 +773,14 @@ func (bs *BlockState) IsDescendantOf(ancestor, descendant common.Hash) (bool, er
return false, fmt.Errorf("getting header: %w", err2)
}

kishansagathiya marked this conversation as resolved.
Show resolved Hide resolved
for current := descendantHeader; descendantHeader.Number < ancestorHeader.Number; {
for current := descendantHeader; current.Number > ancestorHeader.Number; {
if current.ParentHash == ancestor {
return true, nil
}
current, err2 = bs.GetHeader(current.ParentHash)
kishansagathiya marked this conversation as resolved.
Show resolved Hide resolved
if err2 != nil {
return false, fmt.Errorf("getting header: %w", err2)
}
}

return false, nil
Expand Down
3 changes: 1 addition & 2 deletions dot/state/epoch.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

"github.com/ChainSafe/chaindb"
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/blocktree"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/pkg/scale"
)
Expand Down Expand Up @@ -399,7 +398,7 @@ func (nem nextEpochMap[T]) Retrieve(blockState *BlockState, epoch uint64, header
// sometimes while moving to the next epoch is possible the header
// is not fully imported by the blocktree, in this case we will use
// its parent header which migth be already imported.
if errors.Is(err, blocktree.ErrEndNodeNotFound) {
if errors.Is(err, chaindb.ErrKeyNotFound) {
parentHeader, err := blockState.GetHeader(header.ParentHash)
if err != nil {
return nil, fmt.Errorf("cannot get parent header: %w", err)
Expand Down
4 changes: 2 additions & 2 deletions dot/sync/chain_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ func (s *chainProcessor) processReadyBlocks() {

if err := s.processBlockData(*bd); err != nil {
// depending on the error, we might want to save this block for later
if !errors.Is(err, errFailedToGetParent) {
logger.Errorf("block data processing for block with hash %s failed: %s", bd.Hash, err)
if !errors.Is(err, errFailedToGetParent) && !errors.Is(err, blocktree.ErrParentNotFound) {
logger.Tracef("block data processing for block with hash %s failed: %s", bd.Hash, err)
continue
}

Expand Down
1 change: 1 addition & 0 deletions lib/blocktree/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var (
// ErrEndNodeNotFound is returned if the end of a subchain does not exist
ErrEndNodeNotFound = errors.New("end node does not exist")

ErrKeyNotFound = errors.New("key not found")
// ErrNilDescendant is returned if calling subchain with a nil node
ErrNilDescendant = errors.New("descendant node is nil")

Expand Down
4 changes: 2 additions & 2 deletions lib/blocktree/leaves.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package blocktree

import (
"bytes"
"errors"
"fmt"
"sync"

"github.com/ChainSafe/gossamer/lib/common"
Expand Down Expand Up @@ -41,7 +41,7 @@ func (lm *leafMap) store(key Hash, value *node) {
func (lm *leafMap) load(key Hash) (*node, error) {
v, ok := lm.smap.Load(key)
if !ok {
return nil, errors.New("key not found")
return nil, fmt.Errorf("%w: %s", ErrKeyNotFound, key)
}

return v.(*node), nil
Expand Down