7 "github.com/bytom/errors"
8 "github.com/bytom/netsync"
10 "github.com/bytom/version"
13 type VersionInfo struct {
14 Version string `json:"version"`
15 Update uint16 `json:"update"` // 0: no update; 1: small update; 2: significant update
16 NewVer string `json:"new_version"`
19 // NetInfo indicate net information
21 Listening bool `json:"listening"`
22 Syncing bool `json:"syncing"`
23 Mining bool `json:"mining"`
24 PeerCount int `json:"peer_count"`
25 CurrentBlock uint64 `json:"current_block"`
26 HighestBlock uint64 `json:"highest_block"`
27 NetWorkID string `json:"network_id"`
28 Version *VersionInfo `json:"version_info"`
31 // GetNodeInfo return net information
32 func (a *API) GetNodeInfo() *NetInfo {
34 Listening: a.sync.IsListening(),
35 Syncing: !a.sync.IsCaughtUp(),
36 Mining: a.cpuMiner.IsMining(),
37 PeerCount: a.sync.PeerCount(),
38 CurrentBlock: a.chain.BestBlockHeight(),
39 NetWorkID: a.sync.GetNetwork(),
40 Version: &VersionInfo{
41 Version: version.Version,
42 Update: version.Status.VersionStatus(),
43 NewVer: version.Status.MaxVerSeen(),
46 if bestPeer := a.sync.BestPeer(); bestPeer != nil {
47 info.HighestBlock = bestPeer.Height
49 if info.CurrentBlock > info.HighestBlock {
50 info.HighestBlock = info.CurrentBlock
55 // return the currently connected peers with net address
56 func (a *API) getPeerInfoByAddr(addr string) *netsync.PeerInfo {
57 peerInfos := a.sync.GetPeerInfos()
58 for _, peerInfo := range peerInfos {
59 if peerInfo.RemoteAddr == addr {
66 // disconnect peer by the peer id
67 func (a *API) disconnectPeerById(peerID string) error {
68 return a.sync.StopPeer(peerID)
71 // connect peer b y net address
72 func (a *API) connectPeerByIpAndPort(ip string, port uint16) (*netsync.PeerInfo, error) {
73 netIp := net.ParseIP(ip)
75 return nil, errors.New("invalid ip address")
78 addr := p2p.NewNetAddressIPPort(netIp, port)
80 if err := a.sync.DialPeerWithAddress(addr); err != nil {
81 return nil, errors.Wrap(err, "can not connect to the address")
83 peer := a.getPeerInfoByAddr(addr.String())
85 return nil, errors.New("the peer is disconnected again")
90 // getNetInfo return network information
91 func (a *API) getNetInfo() Response {
92 return NewSuccessResponse(a.GetNodeInfo())
95 // isMining return is in mining or not
96 func (a *API) isMining() Response {
97 IsMining := map[string]bool{"is_mining": a.IsMining()}
98 return NewSuccessResponse(IsMining)
101 // IsMining return mining status
102 func (a *API) IsMining() bool {
103 return a.cpuMiner.IsMining()
106 // return the peers of current node
107 func (a *API) listPeers() Response {
108 return NewSuccessResponse(a.sync.GetPeerInfos())
112 func (a *API) disconnectPeer(ctx context.Context, ins struct {
113 PeerID string `json:"peer_id"`
115 if err := a.disconnectPeerById(ins.PeerID); err != nil {
116 return NewErrorResponse(err)
118 return NewSuccessResponse(nil)
121 // connect peer by ip and port
122 func (a *API) connectPeer(ctx context.Context, ins struct {
123 Ip string `json:"ip"`
124 Port uint16 `json:"port"`
126 if peer, err := a.connectPeerByIpAndPort(ins.Ip, ins.Port); err != nil {
127 return NewErrorResponse(err)
129 return NewSuccessResponse(peer)