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

404 in /consensus/tip/:height if height is too high #130

Merged
merged 3 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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
Loading