Skip to content

Commit

Permalink
Merge pull request #130 from SiaFoundation/consensus-block-not-exist-404
Browse files Browse the repository at this point in the history
404 in /consensus/tip/:height if height is too high
  • Loading branch information
n8maninger authored Oct 30, 2024
2 parents 0917e7b + b28ed04 commit 4536c20
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
7 changes: 6 additions & 1 deletion api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,15 @@ func (s *server) consensusTipHeightHandler(jc jape.Context) {
if jc.DecodeParam("height", &height) != nil {
return
}

tip, err := s.e.BestTip(height)
if jc.Check("failed to get block", err) != nil {
if errors.Is(err, explorer.ErrNoTip) {
jc.Error(explorer.ErrNoTip, http.StatusNotFound)
return
} else if jc.Check("failed to get tip", err) != nil {
return
}

jc.Encode(tip)
}

Expand Down
5 changes: 2 additions & 3 deletions explorer/explorer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ import (
)

var (
// ErrNoTip is returned when Tip() is unable to find any blocks in the
// database and thus there is no tip. It does not mean there was an
// error in the underlying database.
// ErrNoTip is returned when we are unable to find the tip in the
// database or there is no tips at all.
ErrNoTip = errors.New("no tip found")

// ErrContractNotFound is returned when ContractRevisions is unable to find
Expand Down
6 changes: 5 additions & 1 deletion persist/sqlite/blocks.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package sqlite

import (
"database/sql"
"errors"
"fmt"

"go.sia.tech/core/types"
Expand Down Expand Up @@ -58,7 +60,9 @@ func (s *Store) Block(id types.BlockID) (result explorer.Block, err error) {
func (s *Store) BestTip(height uint64) (result types.ChainIndex, err error) {
err = s.transaction(func(tx *txn) error {
err = tx.QueryRow(`SELECT id, height FROM blocks WHERE height=?`, height).Scan(decode(&result.ID), decode(&result.Height))
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return explorer.ErrNoTip
} else if err != nil {
return err
}

Expand Down

0 comments on commit 4536c20

Please sign in to comment.