OSDN Git Service

Frontend (#1259)
[bytom/bytom.git] / api / nodeinfo.go
1 package api
2
3 import (
4         "context"
5         "net"
6
7         "github.com/bytom/errors"
8         "github.com/bytom/netsync"
9         "github.com/bytom/p2p"
10         "github.com/bytom/version"
11 )
12
13 // NetInfo indicate net information
14 type NetInfo struct {
15         Listening    bool   `json:"listening"`
16         Syncing      bool   `json:"syncing"`
17         Mining       bool   `json:"mining"`
18         PeerCount    int    `json:"peer_count"`
19         CurrentBlock uint64 `json:"current_block"`
20         HighestBlock uint64 `json:"highest_block"`
21         NetWorkID    string `json:"network_id"`
22         Version      string `json:"version"`
23 }
24
25 // GetNodeInfo return net information
26 func (a *API) GetNodeInfo() *NetInfo {
27         info := &NetInfo{
28                 Listening:    a.sync.Switch().IsListening(),
29                 Syncing:      !a.sync.IsCaughtUp(),
30                 Mining:       a.cpuMiner.IsMining(),
31                 PeerCount:    len(a.sync.Switch().Peers().List()),
32                 CurrentBlock: a.chain.BestBlockHeight(),
33                 NetWorkID:    a.sync.NodeInfo().Network,
34                 Version:      version.Version,
35         }
36         if bestPeer := a.sync.BestPeer(); bestPeer != nil {
37                 info.HighestBlock = bestPeer.Height
38         }
39         if info.CurrentBlock > info.HighestBlock {
40                 info.HighestBlock = info.CurrentBlock
41         }
42         return info
43 }
44
45 // return the currently connected peers with net address
46 func (a *API) getPeerInfoByAddr(addr string) *netsync.PeerInfo {
47         peerInfos := a.sync.GetPeerInfos()
48         for _, peerInfo := range peerInfos {
49                 if peerInfo.RemoteAddr == addr {
50                         return peerInfo
51                 }
52         }
53         return nil
54 }
55
56 // disconnect peer by the peer id
57 func (a *API) disconnectPeerById(peerID string) error {
58         return a.sync.StopPeer(peerID)
59 }
60
61 // connect peer b y net address
62 func (a *API) connectPeerByIpAndPort(ip string, port uint16) (*netsync.PeerInfo, error) {
63         netIp := net.ParseIP(ip)
64         if netIp == nil {
65                 return nil, errors.New("invalid ip address")
66         }
67
68         addr := p2p.NewNetAddressIPPort(netIp, port)
69         sw := a.sync.Switch()
70
71         if err := sw.DialPeerWithAddress(addr); err != nil {
72                 return nil, errors.Wrap(err, "can not connect to the address")
73         }
74         peer := a.getPeerInfoByAddr(addr.String())
75         if peer == nil {
76                 return nil, errors.New("the peer is disconnected again")
77         }
78         return peer, nil
79 }
80
81 // getNetInfo return network information
82 func (a *API) getNetInfo() Response {
83         return NewSuccessResponse(a.GetNodeInfo())
84 }
85
86 // isMining return is in mining or not
87 func (a *API) isMining() Response {
88         IsMining := map[string]bool{"is_mining": a.IsMining()}
89         return NewSuccessResponse(IsMining)
90 }
91
92 // IsMining return mining status
93 func (a *API) IsMining() bool {
94         return a.cpuMiner.IsMining()
95 }
96
97 // return the peers of current node
98 func (a *API) listPeers() Response {
99         return NewSuccessResponse(a.sync.GetPeerInfos())
100 }
101
102 // disconnect peer
103 func (a *API) disconnectPeer(ctx context.Context, ins struct {
104         PeerID string `json:"peer_id"`
105 }) Response {
106         if err := a.disconnectPeerById(ins.PeerID); err != nil {
107                 return NewErrorResponse(err)
108         }
109         return NewSuccessResponse(nil)
110 }
111
112 // connect peer by ip and port
113 func (a *API) connectPeer(ctx context.Context, ins struct {
114         Ip   string `json:"ip"`
115         Port uint16 `json:"port"`
116 }) Response {
117         if peer, err := a.connectPeerByIpAndPort(ins.Ip, ins.Port); err != nil {
118                 return NewErrorResponse(err)
119         } else {
120                 return NewSuccessResponse(peer)
121         }
122 }