forked from NebulousLabs/Sia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
explorer.go
62 lines (53 loc) · 1.78 KB
/
explorer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package api
import (
"fmt"
"net/http"
"github.com/NebulousLabs/Sia/types"
)
// Handles the call to get information about the blockchain. Returns
// several data points such as chain height, the current block, and
// file contract info
func (srv *Server) explorerStatusHandler(w http.ResponseWriter, req *http.Request) {
writeJSON(w, srv.exp.ExplorerStatus())
}
// Handles the call to get many data points on blocks
func (srv *Server) explorerBlockDataHandler(w http.ResponseWriter, req *http.Request) {
// Extract the start and end point from the request
var start, finish types.BlockHeight
_, err := fmt.Sscan(req.FormValue("start"), &start)
if err != nil {
writeError(w, "Malformed or no start height", http.StatusBadRequest)
return
}
// If a range end is not given, assume the range end to be one
// greater than the range start, returning one block
_, err = fmt.Sscan(req.FormValue("finish"), &finish)
if err != nil {
finish = start + 1
}
// Bounds checking is done inside BlockInfo
blockSummaries, err := srv.exp.BlockInfo(start, finish)
if err != nil {
writeError(w, err.Error(), http.StatusBadRequest)
return
}
writeJSON(w, blockSummaries)
}
// Handles the api call to retrieve data about a specific hash
func (srv *Server) explorerGetHashHandler(w http.ResponseWriter, req *http.Request) {
// Extract the hash from the request
var data []byte
_, err := fmt.Sscanf(req.FormValue("hash"), "%x", &data)
if err != nil {
writeError(w, "Malformed or no hash provided: "+err.Error(), http.StatusBadRequest)
return
}
// returnData will be a generic interface. The json encoder
// should still work though
returnData, err := srv.exp.GetHashInfo(data)
if err != nil {
writeError(w, err.Error(), http.StatusInternalServerError)
return
}
writeJSON(w, returnData)
}