This repository has been archived by the owner on Mar 21, 2024. It is now read-only.
/
nodeinfo.go
131 lines (115 loc) · 3.41 KB
/
nodeinfo.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package api
import (
"context"
"net"
"github.com/bytom/bytom/errors"
"github.com/bytom/bytom/netsync"
"github.com/bytom/bytom/p2p"
"github.com/bytom/bytom/version"
)
type VersionInfo struct {
Version string `json:"version"`
Update uint16 `json:"update"` // 0: no update; 1: small update; 2: significant update
NewVer string `json:"new_version"`
}
// NetInfo indicate net information
type NetInfo struct {
Listening bool `json:"listening"`
Syncing bool `json:"syncing"`
Mining bool `json:"mining"`
PeerCount int `json:"peer_count"`
CurrentBlock uint64 `json:"current_block"`
HighestBlock uint64 `json:"highest_block"`
NetWorkID string `json:"network_id"`
Version *VersionInfo `json:"version_info"`
}
// GetNodeInfo return net information
func (a *API) GetNodeInfo() *NetInfo {
info := &NetInfo{
Listening: a.sync.IsListening(),
Syncing: !a.sync.IsCaughtUp(),
Mining: a.cpuMiner.IsMining(),
PeerCount: a.sync.PeerCount(),
CurrentBlock: a.chain.BestBlockHeight(),
NetWorkID: a.sync.GetNetwork(),
Version: &VersionInfo{
Version: version.Version,
Update: version.Status.VersionStatus(),
NewVer: version.Status.MaxVerSeen(),
},
}
if bestPeer := a.sync.BestPeer(); bestPeer != nil {
info.HighestBlock = bestPeer.Height
}
if info.CurrentBlock > info.HighestBlock {
info.HighestBlock = info.CurrentBlock
}
return info
}
// return the currently connected peers with net address
func (a *API) getPeerInfoByAddr(addr string) *netsync.PeerInfo {
peerInfos := a.sync.GetPeerInfos()
for _, peerInfo := range peerInfos {
if peerInfo.RemoteAddr == addr {
return peerInfo
}
}
return nil
}
// disconnect peer by the peer id
func (a *API) disconnectPeerById(peerID string) error {
return a.sync.StopPeer(peerID)
}
// connect peer b y net address
func (a *API) connectPeerByIpAndPort(ip string, port uint16) (*netsync.PeerInfo, error) {
netIp := net.ParseIP(ip)
if netIp == nil {
return nil, errors.New("invalid ip address")
}
addr := p2p.NewNetAddressIPPort(netIp, port)
if err := a.sync.DialPeerWithAddress(addr); err != nil {
return nil, errors.Wrap(err, "can not connect to the address")
}
peer := a.getPeerInfoByAddr(addr.String())
if peer == nil {
return nil, errors.New("the peer is disconnected again")
}
return peer, nil
}
// getNetInfo return network information
func (a *API) getNetInfo() Response {
return NewSuccessResponse(a.GetNodeInfo())
}
// isMining return is in mining or not
func (a *API) isMining() Response {
IsMining := map[string]bool{"is_mining": a.IsMining()}
return NewSuccessResponse(IsMining)
}
// IsMining return mining status
func (a *API) IsMining() bool {
return a.cpuMiner.IsMining()
}
// return the peers of current node
func (a *API) listPeers() Response {
return NewSuccessResponse(a.sync.GetPeerInfos())
}
// disconnect peer
func (a *API) disconnectPeer(ctx context.Context, ins struct {
PeerID string `json:"peer_id"`
}) Response {
if err := a.disconnectPeerById(ins.PeerID); err != nil {
return NewErrorResponse(err)
}
return NewSuccessResponse(nil)
}
// connect peer by ip and port
func (a *API) connectPeer(ctx context.Context, ins struct {
Ip string `json:"ip"`
Port uint16 `json:"port"`
}) Response {
if peer, err := a.connectPeerByIpAndPort(ins.Ip, ins.Port); err != nil {
return NewErrorResponse(err)
} else {
return NewSuccessResponse(peer)
}
}