Skip to content

Commit

Permalink
fix(SPV-769): check sql.select results length
Browse files Browse the repository at this point in the history
  • Loading branch information
arkadiuszos4chain committed Apr 26, 2024
1 parent b7ad9a1 commit 20924b3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
15 changes: 8 additions & 7 deletions database/sql/headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,19 +335,20 @@ func (h *HeadersDb) GetTip(ctx context.Context) (*dto.DbBlockHeader, error) {
h.log.Error().Msgf("sql error: %v", err)
return nil, errors.Wrap(err, "failed to get tip")
}
if len(tip) == 0 {
return nil, errors.New("could not find tip")
}

return &tip[0], nil
}

// GetAncestorOnHeight provides ancestor for a hash on a specified height.
func (h *HeadersDb) GetAncestorOnHeight(hash string, height int32) (*dto.DbBlockHeader, error) {
var bh []*dto.DbBlockHeader
if err := h.db.Select(&bh, h.db.Rebind(sqlSelectAncestorOnHeight), hash, int(height), int(height)); err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, errors.New("could not find ancestors for a providen hash")
}
return nil, errors.Wrapf(err, "failed to get ancestors using given hash: %s ", hash)
}
if bh == nil {
if len(bh) == 0 {
return nil, errors.New("could not find ancestors for a providen hash")
}
return bh[0], nil
Expand All @@ -366,11 +367,11 @@ func (h *HeadersDb) GetAllTips() ([]*dto.DbBlockHeader, error) {
func (h *HeadersDb) GetChainBetweenTwoHashes(low string, high string) ([]*dto.DbBlockHeader, error) {
var bh []*dto.DbBlockHeader
if err := h.db.Select(&bh, h.db.Rebind(sqlChainBetweenTwoHashes), high, low, low); err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, errors.New("could not find headers in given range")
}
return nil, errors.Wrapf(err, "failed to get headers using given range from: %s to: %s", low, high)
}
if len(bh) == 0 {
return nil, errors.New("could not find headers in given range")
}
return bh, nil
}

Expand Down
4 changes: 2 additions & 2 deletions service/header_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ func (hs *HeaderService) IsCurrent() bool {
// Not current if the latest main (best) chain height is before the
// latest known good checkpoint (when checkpoints are enabled).
checkpoints := hs.checkpoints
checkpoint := &checkpoints[len(checkpoints)-1]
checkpoint := checkpoints[len(checkpoints)-1]
tip := hs.GetTip()
if tip == nil {
return true
}
if checkpoint != nil && tip.Height < checkpoint.Height {
if tip.Height < checkpoint.Height {
return false
}

Expand Down

0 comments on commit 20924b3

Please sign in to comment.