OSDN Git Service

modify field name (#1202)
[bytom/bytom-spv.git] / api / nodeinfo.go
index 40ef409..ccf4c8f 100644 (file)
@@ -1,13 +1,13 @@
 package api
 
 import (
-       "net"
        "context"
+       "net"
 
-       "github.com/bytom/version"
-       "github.com/bytom/netsync"
        "github.com/bytom/errors"
+       "github.com/bytom/netsync"
        "github.com/bytom/p2p"
+       "github.com/bytom/version"
 )
 
 // NetInfo indicate net information
@@ -26,24 +26,25 @@ type NetInfo struct {
 func (a *API) GetNodeInfo() *NetInfo {
        info := &NetInfo{
                Listening:    a.sync.Switch().IsListening(),
-               Syncing:      a.sync.BlockKeeper().IsCaughtUp(),
+               Syncing:      !a.sync.IsCaughtUp(),
                Mining:       a.cpuMiner.IsMining(),
                PeerCount:    len(a.sync.Switch().Peers().List()),
                CurrentBlock: a.chain.BestBlockHeight(),
                NetWorkID:    a.sync.NodeInfo().Network,
                Version:      version.Version,
        }
-       _, info.HighestBlock = a.sync.Peers().BestPeer()
+       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.Peers().GetPeerInfos()
+       peerInfos := a.sync.GetPeerInfos()
        for _, peerInfo := range peerInfos {
                if peerInfo.RemoteAddr == addr {
                        return peerInfo
@@ -53,13 +54,8 @@ func (a *API) getPeerInfoByAddr(addr string) *netsync.PeerInfo {
 }
 
 // disconnect peer by the peer id
-func (a *API) disconnectPeerById(peerId string) error {
-       if peer, ok := a.sync.Peers().Peer(peerId); ok {
-               swPeer := peer.GetPeer()
-               a.sync.Switch().StopPeerGracefully(swPeer)
-               return nil
-       }
-       return errors.New("peerId not exist")
+func (a *API) disconnectPeerById(peerID string) error {
+       return a.sync.StopPeer(peerID)
 }
 
 // connect peer b y net address
@@ -100,14 +96,14 @@ func (a *API) IsMining() bool {
 
 // return the peers of current node
 func (a *API) listPeers() Response {
-       return NewSuccessResponse(a.sync.Peers().GetPeerInfos())
+       return NewSuccessResponse(a.sync.GetPeerInfos())
 }
 
 // disconnect peer
 func (a *API) disconnectPeer(ctx context.Context, ins struct {
-       PeerId string `json:"peerId"`
+       PeerID string `json:"peer_id"`
 }) Response {
-       if err := a.disconnectPeerById(ins.PeerId); err != nil {
+       if err := a.disconnectPeerById(ins.PeerID); err != nil {
                return NewErrorResponse(err)
        }
        return NewSuccessResponse(nil)