From 425df6fc408d387c769c43d755e209bc75f44110 Mon Sep 17 00:00:00 2001 From: Christopher Tarry Date: Tue, 29 Oct 2024 11:29:18 -0400 Subject: [PATCH 1/3] 404 in /consensus/tip if height is too high --- api/server.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/api/server.go b/api/server.go index d2a47042..b0a28be2 100644 --- a/api/server.go +++ b/api/server.go @@ -96,6 +96,10 @@ var ( // ErrTooManyIDs is returned by the batch transaction and contract // endpoints when more than maxIDs IDs are specified. ErrTooManyIDs = fmt.Errorf("too many IDs provided (provide less than %d)", maxIDs) + + // ErrInvalidTip is returned by /consensus/tip/:height when a block at that + // height does not exist yet. + ErrInvalidTip = errors.New("invalid tip") ) type server struct { @@ -200,6 +204,17 @@ func (s *server) consensusTipHeightHandler(jc jape.Context) { if jc.DecodeParam("height", &height) != nil { return } + + maxTip, err := s.e.Tip() + if jc.Check("failed to get tip", err) != nil { + return + } + + if height > maxTip.Height { + jc.Error(ErrInvalidTip, http.StatusNotFound) + return + } + tip, err := s.e.BestTip(height) if jc.Check("failed to get block", err) != nil { return From e6f13f96948d6fbaee0a57109b00c15ac88f63d5 Mon Sep 17 00:00:00 2001 From: Christopher Tarry Date: Tue, 29 Oct 2024 12:44:22 -0400 Subject: [PATCH 2/3] use sentinel error --- api/server.go | 18 ++++-------------- explorer/explorer.go | 5 ++--- persist/sqlite/blocks.go | 6 +++++- 3 files changed, 11 insertions(+), 18 deletions(-) diff --git a/api/server.go b/api/server.go index b0a28be2..699c5046 100644 --- a/api/server.go +++ b/api/server.go @@ -96,10 +96,6 @@ var ( // ErrTooManyIDs is returned by the batch transaction and contract // endpoints when more than maxIDs IDs are specified. ErrTooManyIDs = fmt.Errorf("too many IDs provided (provide less than %d)", maxIDs) - - // ErrInvalidTip is returned by /consensus/tip/:height when a block at that - // height does not exist yet. - ErrInvalidTip = errors.New("invalid tip") ) type server struct { @@ -205,20 +201,14 @@ func (s *server) consensusTipHeightHandler(jc jape.Context) { return } - maxTip, err := s.e.Tip() - if jc.Check("failed to get tip", err) != nil { + tip, err := s.e.BestTip(height) + if errors.Is(err, explorer.ErrNoTip) { + jc.Error(explorer.ErrNoTip, http.StatusNotFound) return - } - - if height > maxTip.Height { - jc.Error(ErrInvalidTip, http.StatusNotFound) + } else if jc.Check("failed to get tip", err) != nil { return } - tip, err := s.e.BestTip(height) - if jc.Check("failed to get block", err) != nil { - return - } jc.Encode(tip) } diff --git a/explorer/explorer.go b/explorer/explorer.go index 292eaca5..52bc70ac 100644 --- a/explorer/explorer.go +++ b/explorer/explorer.go @@ -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 diff --git a/persist/sqlite/blocks.go b/persist/sqlite/blocks.go index 15ece33b..889a60e9 100644 --- a/persist/sqlite/blocks.go +++ b/persist/sqlite/blocks.go @@ -1,6 +1,8 @@ package sqlite import ( + "database/sql" + "errors" "fmt" "go.sia.tech/core/types" @@ -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 } From e5e42218e334cee3f0120752a359b72cf3837bc0 Mon Sep 17 00:00:00 2001 From: Nate Maninger Date: Wed, 30 Oct 2024 07:47:54 -0700 Subject: [PATCH 3/3] cmd: increase inbound peers --- cmd/explored/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/explored/main.go b/cmd/explored/main.go index 1b271e04..996cfec5 100644 --- a/cmd/explored/main.go +++ b/cmd/explored/main.go @@ -250,7 +250,7 @@ func runRootCmd(ctx context.Context, log *zap.Logger) error { UniqueID: gateway.GenerateUniqueID(), NetAddress: syncerAddr, } - s := syncer.New(syncerListener, cm, ps, header, syncer.WithLogger(log.Named("syncer"))) + s := syncer.New(syncerListener, cm, ps, header, syncer.WithLogger(log.Named("syncer")), syncer.WithMaxInboundPeers(256)) defer s.Close() go s.Run(ctx)