OSDN Git Service

Merge pull request #606 from Bytom/dev
[bytom/bytom-spv.git] / api / nodeinfo.go
1 package api
2
3 type NetInfo struct {
4         Listening    bool   `json:"listening"`
5         Syncing      bool   `json:"syncing"`
6         Mining       bool   `json:"mining"`
7         PeerCount    int    `json:"peer_count"`
8         CurrentBlock uint64 `json:"current_block"`
9         HighestBlock uint64 `json:"highest_block"`
10 }
11
12 func (a *API) GetNodeInfo() *NetInfo {
13         info := &NetInfo{
14                 Listening:    a.sync.Switch().IsListening(),
15                 Syncing:      a.sync.BlockKeeper().IsCaughtUp(),
16                 Mining:       a.cpuMiner.IsMining(),
17                 PeerCount:    len(a.sync.Switch().Peers().List()),
18                 CurrentBlock: a.chain.BestBlockHeight(),
19         }
20         _, info.HighestBlock = a.sync.Peers().BestPeer()
21         if info.CurrentBlock > info.HighestBlock {
22                 info.HighestBlock = info.CurrentBlock
23         }
24         return info
25 }
26
27 // return network infomation
28 func (a *API) getNetInfo() Response {
29         return NewSuccessResponse(a.GetNodeInfo())
30 }
31
32 // return is in mining or not
33 func (a *API) isMining() Response {
34         IsMining := map[string]bool{"isMining": a.IsMining()}
35         return NewSuccessResponse(IsMining)
36 }
37
38 func (a *API) IsMining() bool {
39         return a.cpuMiner.IsMining()
40 }