github.com/adoriasoft/tendermint@v0.34.0-dev1.0.20200722151356-96d84601a75a/rpc/core/net.go (about) 1 package core 2 3 import ( 4 "errors" 5 "fmt" 6 7 "github.com/tendermint/tendermint/p2p" 8 ctypes "github.com/tendermint/tendermint/rpc/core/types" 9 rpctypes "github.com/tendermint/tendermint/rpc/jsonrpc/types" 10 ) 11 12 // NetInfo returns network info. 13 // More: https://docs.tendermint.com/master/rpc/#/Info/net_info 14 func NetInfo(ctx *rpctypes.Context) (*ctypes.ResultNetInfo, error) { 15 peersList := env.P2PPeers.Peers().List() 16 peers := make([]ctypes.Peer, 0, len(peersList)) 17 for _, peer := range peersList { 18 nodeInfo, ok := peer.NodeInfo().(p2p.DefaultNodeInfo) 19 if !ok { 20 return nil, fmt.Errorf("peer.NodeInfo() is not DefaultNodeInfo") 21 } 22 peers = append(peers, ctypes.Peer{ 23 NodeInfo: nodeInfo, 24 IsOutbound: peer.IsOutbound(), 25 ConnectionStatus: peer.Status(), 26 RemoteIP: peer.RemoteIP().String(), 27 }) 28 } 29 // TODO: Should we include PersistentPeers and Seeds in here? 30 // PRO: useful info 31 // CON: privacy 32 return &ctypes.ResultNetInfo{ 33 Listening: env.P2PTransport.IsListening(), 34 Listeners: env.P2PTransport.Listeners(), 35 NPeers: len(peers), 36 Peers: peers, 37 }, nil 38 } 39 40 // UnsafeDialSeeds dials the given seeds (comma-separated id@IP:PORT). 41 func UnsafeDialSeeds(ctx *rpctypes.Context, seeds []string) (*ctypes.ResultDialSeeds, error) { 42 if len(seeds) == 0 { 43 return &ctypes.ResultDialSeeds{}, errors.New("no seeds provided") 44 } 45 env.Logger.Info("DialSeeds", "seeds", seeds) 46 if err := env.P2PPeers.DialPeersAsync(seeds); err != nil { 47 return &ctypes.ResultDialSeeds{}, err 48 } 49 return &ctypes.ResultDialSeeds{Log: "Dialing seeds in progress. See /net_info for details"}, nil 50 } 51 52 // UnsafeDialPeers dials the given peers (comma-separated id@IP:PORT), 53 // optionally making them persistent. 54 func UnsafeDialPeers(ctx *rpctypes.Context, peers []string, persistent bool) (*ctypes.ResultDialPeers, error) { 55 if len(peers) == 0 { 56 return &ctypes.ResultDialPeers{}, errors.New("no peers provided") 57 } 58 env.Logger.Info("DialPeers", "peers", peers, "persistent", persistent) 59 if persistent { 60 if err := env.P2PPeers.AddPersistentPeers(peers); err != nil { 61 return &ctypes.ResultDialPeers{}, err 62 } 63 } 64 if err := env.P2PPeers.DialPeersAsync(peers); err != nil { 65 return &ctypes.ResultDialPeers{}, err 66 } 67 return &ctypes.ResultDialPeers{Log: "Dialing peers in progress. See /net_info for details"}, nil 68 } 69 70 // Genesis returns genesis file. 71 // More: https://docs.tendermint.com/master/rpc/#/Info/genesis 72 func Genesis(ctx *rpctypes.Context) (*ctypes.ResultGenesis, error) { 73 return &ctypes.ResultGenesis{Genesis: env.GenDoc}, nil 74 }