OSDN Git Service

Merge pull request #770 from Bytom/p2p_test
[bytom/bytom.git] / api / nodeinfo.go
1 package api
2
3 import (
4         "github.com/bytom/version"
5 )
6
7 // NetInfo indicate net information
8 type NetInfo struct {
9         Listening    bool   `json:"listening"`
10         Syncing      bool   `json:"syncing"`
11         Mining       bool   `json:"mining"`
12         PeerCount    int    `json:"peer_count"`
13         CurrentBlock uint64 `json:"current_block"`
14         HighestBlock uint64 `json:"highest_block"`
15         NetWorkID    string `json:"network_id"`
16         Version      string `json:"version"`
17 }
18
19 // GetNodeInfo return net information
20 func (a *API) GetNodeInfo() *NetInfo {
21         info := &NetInfo{
22                 Listening:    a.sync.Switch().IsListening(),
23                 Syncing:      a.sync.BlockKeeper().IsCaughtUp(),
24                 Mining:       a.cpuMiner.IsMining(),
25                 PeerCount:    len(a.sync.Switch().Peers().List()),
26                 CurrentBlock: a.chain.BestBlockHeight(),
27                 NetWorkID:    a.sync.NodeInfo().Network,
28                 Version:      version.Version,
29         }
30         _, info.HighestBlock = a.sync.Peers().BestPeer()
31         if info.CurrentBlock > info.HighestBlock {
32                 info.HighestBlock = info.CurrentBlock
33         }
34         return info
35 }
36
37 // getNetInfo return network infomation
38 func (a *API) getNetInfo() Response {
39         return NewSuccessResponse(a.GetNodeInfo())
40 }
41
42 // isMining return is in mining or not
43 func (a *API) isMining() Response {
44         IsMining := map[string]bool{"is_mining": a.IsMining()}
45         return NewSuccessResponse(IsMining)
46 }
47
48 // IsMining return mining status
49 func (a *API) IsMining() bool {
50         return a.cpuMiner.IsMining()
51 }