forked from NebulousLabs/Sia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
consensus.go
47 lines (40 loc) · 1.31 KB
/
consensus.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
package api
import (
"fmt"
"net/http"
"github.com/NebulousLabs/Sia/build"
"github.com/NebulousLabs/Sia/types"
)
// The ConsensusGET struct contains general information about the consensus
// set, with tags to support idiomatic json encodings.
type ConsensusGET struct {
Height types.BlockHeight `json:"height"`
CurrentBlock types.BlockID `json:"currentblock"`
Target types.Target `json:"target"`
}
// consensusHandlerGET handles a GET request to /consensus.
func (srv *Server) consensusHandlerGET(w http.ResponseWriter, req *http.Request) {
id := srv.mu.RLock()
defer srv.mu.RUnlock(id)
curblockID := srv.currentBlock.ID()
currentTarget, exists := srv.cs.ChildTarget(curblockID)
if build.DEBUG {
if !exists {
fmt.Printf("Could not find block %s\n", curblockID)
panic("server has nonexistent current block")
}
}
writeJSON(w, ConsensusGET{
Height: types.BlockHeight(srv.blockchainHeight),
CurrentBlock: srv.currentBlock.ID(),
Target: currentTarget,
})
}
// consensusHandler handles the API calls to /consensus.
func (srv *Server) consensusHandler(w http.ResponseWriter, req *http.Request) {
if req.Method == "" || req.Method == "GET" {
srv.consensusHandlerGET(w, req)
return
}
writeError(w, "unrecognized method when calling /consensus", http.StatusBadRequest)
}