forked from NebulousLabs/Sia
/
consensus.go
48 lines (39 loc) · 1.14 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
48
package api
import (
"net/http"
"github.com/NebulousLabs/Sia/build"
"github.com/NebulousLabs/Sia/types"
)
type ConsensusInfo struct {
Height types.BlockHeight
CurrentBlock types.BlockID
Target types.Target
}
// consensusStatusHandler handles the API call asking for the consensus status.
func (srv *Server) consensusStatusHandler(w http.ResponseWriter, req *http.Request) {
lockID := srv.mu.RLock()
defer srv.mu.RUnlock(lockID)
currentTarget, exists := srv.cs.ChildTarget(srv.currentBlock.ID())
if build.DEBUG {
if !exists {
panic("server has nonexistent current block")
}
}
writeJSON(w, ConsensusInfo{
srv.blockchainHeight,
srv.currentBlock.ID(),
currentTarget,
})
}
// consensusSynchronizeHandler handles the API call asking for the consensus to
// synchronize with other peers.
func (srv *Server) consensusSynchronizeHandler(w http.ResponseWriter, req *http.Request) {
peers := srv.gateway.Peers()
if len(peers) == 0 {
writeError(w, "No peers available for syncing", http.StatusInternalServerError)
return
}
// TODO: Do not select first peer every time.
go srv.cs.Synchronize(peers[0])
writeSuccess(w)
}