Skip to content

Commit

Permalink
add basic explorer API endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
chris124567 committed Jan 3, 2024
1 parent 6ad34fa commit 60bc47d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
6 changes: 6 additions & 0 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,9 @@ func (c *Client) SyncerBroadcastBlock(b types.Block) (err error) {
err = c.c.POST("/syncer/broadcast/block", b, nil)
return
}

// Tip returns the current tip of the explorer.
func (c *Client) Tip() (resp types.ChainIndex, err error) {
err = c.c.GET("/explorer/tip", &resp)
return
}
17 changes: 16 additions & 1 deletion api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,16 @@ type (
BroadcastV2TransactionSet(txns []types.V2Transaction)
BroadcastV2BlockOutline(bo gateway.V2BlockOutline)
}

// Explorer implements a Sia explorer.
Explorer interface {
Tip() (types.ChainIndex, error)
}
)

type server struct {
cm ChainManager
e Explorer
s Syncer

mu sync.Mutex
Expand Down Expand Up @@ -124,10 +130,17 @@ func (s *server) txpoolBroadcastHandler(jc jape.Context) {
}
}

func (s *server) explorerTipHandler(jc jape.Context) {
tip, err := s.e.Tip()
jc.Check("failed to get tip", err)
jc.Encode(tip)
}

// NewServer returns an HTTP handler that serves the explored API.
func NewServer(cm ChainManager, s Syncer) http.Handler {
func NewServer(e Explorer, cm ChainManager, s Syncer) http.Handler {
srv := server{
cm: cm,
e: e,
s: s,
}
return jape.Mux(map[string]jape.Handler{
Expand All @@ -138,5 +151,7 @@ func NewServer(cm ChainManager, s Syncer) http.Handler {
"GET /txpool/transactions": srv.txpoolTransactionsHandler,
"GET /txpool/fee": srv.txpoolFeeHandler,
"POST /txpool/broadcast": srv.txpoolBroadcastHandler,

"GET /explorer/tip": srv.explorerTipHandler,
})
}
2 changes: 1 addition & 1 deletion cmd/explored/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func startWeb(l net.Listener, node *node, password string) error {
renter := api.NewServer(node.cm, node.s)
renter := api.NewServer(node.e, node.cm, node.s)
api := jape.BasicAuth(password)(renter)
return http.Serve(l, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.URL.Path, "/api") {
Expand Down
4 changes: 4 additions & 0 deletions explorer/explorer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ type Explorer struct {
func NewExplorer(s Store) *Explorer {
return &Explorer{s: s}
}

func (e *Explorer) Tip() (types.ChainIndex, error) {
return e.s.Tip()
}

0 comments on commit 60bc47d

Please sign in to comment.