Skip to content

Commit

Permalink
add endpoint to get block by height
Browse files Browse the repository at this point in the history
  • Loading branch information
chris124567 committed Jan 4, 2024
1 parent bb8ebca commit e0d8501
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 6 deletions.
8 changes: 7 additions & 1 deletion api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,14 @@ func (c *Client) Tip() (resp types.ChainIndex, err error) {
return
}

// Block returns a block with the given block ID.
// Block returns the block with the specified ID.
func (c *Client) Block(id types.BlockID) (resp types.Block, err error) {
err = c.c.GET(fmt.Sprintf("/explorer/block/%s", id), &resp)

Check failure on line 72 in api/client.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.19)

Client references route not defined by server: GET /explorer/block/%s

Check failure on line 72 in api/client.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.19)

Analyzer warning in api

Client references route not defined by server: GET /explorer/block/%s
return
}

// BlockHeight returns the block with the specified height.
func (c *Client) BlockHeight(height uint64) (resp types.Block, err error) {
err = c.c.GET(fmt.Sprintf("/explorer/block/height/%d", height), &resp)
return
}
18 changes: 16 additions & 2 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type (
Explorer interface {
Tip() (types.ChainIndex, error)
Block(id types.BlockID) (types.Block, error)
BlockHeight(height uint64) (types.Block, error)
}
)

Expand Down Expand Up @@ -151,6 +152,18 @@ func (s *server) explorerBlockHandler(jc jape.Context) {
jc.Encode(block)
}

func (s *server) explorerBlockHeightHandler(jc jape.Context) {
var height uint64
if jc.DecodeParam("height", &height) != nil {
return
}
block, err := s.e.BlockHeight(height)
if jc.Check("failed to get block", err) != nil {
return
}
jc.Encode(block)
}

// NewServer returns an HTTP handler that serves the explored API.
func NewServer(e Explorer, cm ChainManager, s Syncer) http.Handler {
srv := server{
Expand All @@ -167,7 +180,8 @@ func NewServer(e Explorer, cm ChainManager, s Syncer) http.Handler {
"GET /txpool/fee": srv.txpoolFeeHandler,
"POST /txpool/broadcast": srv.txpoolBroadcastHandler,

"GET /explorer/tip": srv.explorerTipHandler,
"GET /explorer/block/:id": srv.explorerBlockHandler,
"GET /explorer/tip": srv.explorerTipHandler,
"GET /explorer/block/id/:id": srv.explorerBlockHandler,
"GET /explorer/block/height/:height": srv.explorerBlockHeightHandler,
})
}
2 changes: 1 addition & 1 deletion cmd/explored/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func newNode(addr, dir string, chainNetwork string, useUPNP bool, logger *zap.Lo
Start: func() func() {
ch := make(chan struct{})
go func() {
s.Run()
// s.Run()
close(ch)
}()
return func() {
Expand Down
6 changes: 6 additions & 0 deletions explorer/explorer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Store interface {

Tip() (types.ChainIndex, error)
Block(id types.BlockID) (types.Block, error)
BlockHeight(height uint64) (types.Block, error)
}

// Explorer implements a Sia explorer.
Expand All @@ -33,3 +34,8 @@ func (e *Explorer) Tip() (types.ChainIndex, error) {
func (e *Explorer) Block(id types.BlockID) (types.Block, error) {
return e.s.Block(id)
}

// BlockHeight returns the block with the specified height.
func (e *Explorer) BlockHeight(height uint64) (types.Block, error) {
return e.s.BlockHeight(height)
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ require (
github.com/mattn/go-sqlite3 v1.14.19
go.etcd.io/bbolt v1.3.7
go.sia.tech/core v0.1.12-0.20231021194448-f1e65eb9f0d0
go.sia.tech/jape v0.9.0
go.sia.tech/jape v0.11.1
go.uber.org/zap v1.26.0
golang.org/x/term v0.6.0
lukechampine.com/frand v1.4.2
lukechampine.com/upnp v0.3.0
Expand All @@ -17,7 +18,6 @@ require (
github.com/julienschmidt/httprouter v1.3.0 // indirect
go.sia.tech/mux v1.2.0 // indirect
go.uber.org/multierr v1.10.0 // indirect
go.uber.org/zap v1.26.0 // indirect
golang.org/x/crypto v0.0.0-20220507011949-2cf3adece122 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/tools v0.7.0 // indirect
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ go.sia.tech/core v0.1.12-0.20231021194448-f1e65eb9f0d0 h1:2nKOKa99g9h9m3hL5UortA
go.sia.tech/core v0.1.12-0.20231021194448-f1e65eb9f0d0/go.mod h1:3EoY+rR78w1/uGoXXVqcYdwSjSJKuEMI5bL7WROA27Q=
go.sia.tech/jape v0.9.0 h1:kWgMFqALYhLMJYOwWBgJda5ko/fi4iZzRxHRP7pp8NY=
go.sia.tech/jape v0.9.0/go.mod h1:4QqmBB+t3W7cNplXPj++ZqpoUb2PeiS66RLpXmEGap4=
go.sia.tech/jape v0.11.1 h1:M7IP+byXL7xOqzxcHUQuXW+q3sYMkYzmMlMw+q8ZZw0=
go.sia.tech/jape v0.11.1/go.mod h1:4QqmBB+t3W7cNplXPj++ZqpoUb2PeiS66RLpXmEGap4=
go.sia.tech/mux v1.2.0 h1:ofa1Us9mdymBbGMY2XH/lSpY8itFsKIo/Aq8zwe+GHU=
go.sia.tech/mux v1.2.0/go.mod h1:Yyo6wZelOYTyvrHmJZ6aQfRoer3o4xyKQ4NmQLJrBSo=
go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk=
go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo=
Expand Down
15 changes: 15 additions & 0 deletions persist/sqlite/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,18 @@ func (s *Store) Block(id types.BlockID) (result types.Block, err error) {

return
}

// BlockHeight implements explorer.Store.
func (s *Store) BlockHeight(height uint64) (result types.Block, err error) {
var data []byte
if err = s.queryRow("SELECT id FROM Blocks WHERE height = ?", height).Scan(&data); err != nil {
return
}

var bid types.BlockID
if err = decode(&bid, data); err != nil {
return
}
result, err = s.Block(bid)
return
}

0 comments on commit e0d8501

Please sign in to comment.