mirror of
https://github.com/tendermint/tendermint.git
synced 2026-02-04 02:52:07 +00:00
No need to duplicate information in this case. It a) requires extra efforts to keep both in sync b) nobody reads godoc documentation anyways.
76 lines
2.5 KiB
Go
76 lines
2.5 KiB
Go
package core
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/tendermint/tendermint/p2p"
|
|
ctypes "github.com/tendermint/tendermint/rpc/core/types"
|
|
rpctypes "github.com/tendermint/tendermint/rpc/lib/types"
|
|
)
|
|
|
|
// NetInfo returns network info.
|
|
// More: https://tendermint.com/rpc/#/Info/net_info
|
|
func NetInfo(ctx *rpctypes.Context) (*ctypes.ResultNetInfo, error) {
|
|
out, in, _ := p2pPeers.NumPeers()
|
|
peers := make([]ctypes.Peer, 0, out+in)
|
|
for _, peer := range p2pPeers.Peers().List() {
|
|
nodeInfo, ok := peer.NodeInfo().(p2p.DefaultNodeInfo)
|
|
if !ok {
|
|
return nil, fmt.Errorf("peer.NodeInfo() is not DefaultNodeInfo")
|
|
}
|
|
peers = append(peers, ctypes.Peer{
|
|
NodeInfo: nodeInfo,
|
|
IsOutbound: peer.IsOutbound(),
|
|
ConnectionStatus: peer.Status(),
|
|
RemoteIP: peer.RemoteIP().String(),
|
|
})
|
|
}
|
|
// TODO: Should we include PersistentPeers and Seeds in here?
|
|
// PRO: useful info
|
|
// CON: privacy
|
|
return &ctypes.ResultNetInfo{
|
|
Listening: p2pTransport.IsListening(),
|
|
Listeners: p2pTransport.Listeners(),
|
|
NPeers: len(peers),
|
|
Peers: peers,
|
|
}, nil
|
|
}
|
|
|
|
// UnsafeDialSeeds dials the given seeds (comma-separated id@IP:PORT).
|
|
func UnsafeDialSeeds(ctx *rpctypes.Context, seeds []string) (*ctypes.ResultDialSeeds, error) {
|
|
if len(seeds) == 0 {
|
|
return &ctypes.ResultDialSeeds{}, errors.New("No seeds provided")
|
|
}
|
|
logger.Info("DialSeeds", "seeds", seeds)
|
|
if err := p2pPeers.DialPeersAsync(seeds); err != nil {
|
|
return &ctypes.ResultDialSeeds{}, err
|
|
}
|
|
return &ctypes.ResultDialSeeds{Log: "Dialing seeds in progress. See /net_info for details"}, nil
|
|
}
|
|
|
|
// UnsafeDialPeers dials the given peers (comma-separated id@IP:PORT),
|
|
// optionally making them persistent.
|
|
func UnsafeDialPeers(ctx *rpctypes.Context, peers []string, persistent bool) (*ctypes.ResultDialPeers, error) {
|
|
if len(peers) == 0 {
|
|
return &ctypes.ResultDialPeers{}, errors.New("No peers provided")
|
|
}
|
|
logger.Info("DialPeers", "peers", peers, "persistent", persistent)
|
|
if persistent {
|
|
if err := p2pPeers.AddPersistentPeers(peers); err != nil {
|
|
return &ctypes.ResultDialPeers{}, err
|
|
}
|
|
}
|
|
if err := p2pPeers.DialPeersAsync(peers); err != nil {
|
|
return &ctypes.ResultDialPeers{}, err
|
|
}
|
|
return &ctypes.ResultDialPeers{Log: "Dialing peers in progress. See /net_info for details"}, nil
|
|
}
|
|
|
|
// Genesis returns genesis file.
|
|
// More: https://tendermint.com/rpc/#/Info/genesis
|
|
func Genesis(ctx *rpctypes.Context) (*ctypes.ResultGenesis, error) {
|
|
return &ctypes.ResultGenesis{Genesis: genDoc}, nil
|
|
}
|