Skip to content

Commit

Permalink
Merge pull request #246 from bitcoin-sv/fix-769-empty-select-fix
Browse files Browse the repository at this point in the history
fix(SPV-769): check sql.select result length
  • Loading branch information
wregulski authored May 13, 2024
2 parents 9da8e48 + 3b18f9d commit 74bb09f
Showing 1 changed file with 8 additions and 7 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

0 comments on commit 74bb09f

Please sign in to comment.