gitlab.com/gpdionisio/tendermint@v0.34.19-dev2/rpc/core/net.go (about)

     1  package core
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"strings"
     7  
     8  	"github.com/tendermint/tendermint/p2p"
     9  	ctypes "github.com/tendermint/tendermint/rpc/core/types"
    10  	rpctypes "github.com/tendermint/tendermint/rpc/jsonrpc/types"
    11  )
    12  
    13  // NetInfo returns network info.
    14  // More: https://docs.tendermint.com/master/rpc/#/Info/net_info
    15  func NetInfo(ctx *rpctypes.Context) (*ctypes.ResultNetInfo, error) {
    16  	peersList := env.P2PPeers.Peers().List()
    17  	peers := make([]ctypes.Peer, 0, len(peersList))
    18  	for _, peer := range peersList {
    19  		nodeInfo, ok := peer.NodeInfo().(p2p.DefaultNodeInfo)
    20  		if !ok {
    21  			return nil, fmt.Errorf("peer.NodeInfo() is not DefaultNodeInfo")
    22  		}
    23  		peers = append(peers, ctypes.Peer{
    24  			NodeInfo:         nodeInfo,
    25  			IsOutbound:       peer.IsOutbound(),
    26  			ConnectionStatus: peer.Status(),
    27  			RemoteIP:         peer.RemoteIP().String(),
    28  		})
    29  	}
    30  	// TODO: Should we include PersistentPeers and Seeds in here?
    31  	// PRO: useful info
    32  	// CON: privacy
    33  	return &ctypes.ResultNetInfo{
    34  		Listening: env.P2PTransport.IsListening(),
    35  		Listeners: env.P2PTransport.Listeners(),
    36  		NPeers:    len(peers),
    37  		Peers:     peers,
    38  	}, nil
    39  }
    40  
    41  // UnsafeDialSeeds dials the given seeds (comma-separated id@IP:PORT).
    42  func UnsafeDialSeeds(ctx *rpctypes.Context, seeds []string) (*ctypes.ResultDialSeeds, error) {
    43  	if len(seeds) == 0 {
    44  		return &ctypes.ResultDialSeeds{}, errors.New("no seeds provided")
    45  	}
    46  	env.Logger.Info("DialSeeds", "seeds", seeds)
    47  	if err := env.P2PPeers.DialPeersAsync(seeds); err != nil {
    48  		return &ctypes.ResultDialSeeds{}, err
    49  	}
    50  	return &ctypes.ResultDialSeeds{Log: "Dialing seeds in progress. See /net_info for details"}, nil
    51  }
    52  
    53  // UnsafeDialPeers dials the given peers (comma-separated id@IP:PORT),
    54  // optionally making them persistent.
    55  func UnsafeDialPeers(ctx *rpctypes.Context, peers []string, persistent, unconditional, private bool) (
    56  	*ctypes.ResultDialPeers, error) {
    57  	if len(peers) == 0 {
    58  		return &ctypes.ResultDialPeers{}, errors.New("no peers provided")
    59  	}
    60  
    61  	ids, err := getIDs(peers)
    62  	if err != nil {
    63  		return &ctypes.ResultDialPeers{}, err
    64  	}
    65  
    66  	env.Logger.Info("DialPeers", "peers", peers, "persistent",
    67  		persistent, "unconditional", unconditional, "private", private)
    68  
    69  	if persistent {
    70  		if err := env.P2PPeers.AddPersistentPeers(peers); err != nil {
    71  			return &ctypes.ResultDialPeers{}, err
    72  		}
    73  	}
    74  
    75  	if private {
    76  		if err := env.P2PPeers.AddPrivatePeerIDs(ids); err != nil {
    77  			return &ctypes.ResultDialPeers{}, err
    78  		}
    79  	}
    80  
    81  	if unconditional {
    82  		if err := env.P2PPeers.AddUnconditionalPeerIDs(ids); err != nil {
    83  			return &ctypes.ResultDialPeers{}, err
    84  		}
    85  	}
    86  
    87  	if err := env.P2PPeers.DialPeersAsync(peers); err != nil {
    88  		return &ctypes.ResultDialPeers{}, err
    89  	}
    90  
    91  	return &ctypes.ResultDialPeers{Log: "Dialing peers in progress. See /net_info for details"}, nil
    92  }
    93  
    94  // Genesis returns genesis file.
    95  // More: https://docs.tendermint.com/master/rpc/#/Info/genesis
    96  func Genesis(ctx *rpctypes.Context) (*ctypes.ResultGenesis, error) {
    97  	if len(env.genChunks) > 1 {
    98  		return nil, errors.New("genesis response is large, please use the genesis_chunked API instead")
    99  	}
   100  
   101  	return &ctypes.ResultGenesis{Genesis: env.GenDoc}, nil
   102  }
   103  
   104  func GenesisChunked(ctx *rpctypes.Context, chunk uint) (*ctypes.ResultGenesisChunk, error) {
   105  	if env.genChunks == nil {
   106  		return nil, fmt.Errorf("service configuration error, genesis chunks are not initialized")
   107  	}
   108  
   109  	if len(env.genChunks) == 0 {
   110  		return nil, fmt.Errorf("service configuration error, there are no chunks")
   111  	}
   112  
   113  	id := int(chunk)
   114  
   115  	if id > len(env.genChunks)-1 {
   116  		return nil, fmt.Errorf("there are %d chunks, %d is invalid", len(env.genChunks)-1, id)
   117  	}
   118  
   119  	return &ctypes.ResultGenesisChunk{
   120  		TotalChunks: len(env.genChunks),
   121  		ChunkNumber: id,
   122  		Data:        env.genChunks[id],
   123  	}, nil
   124  }
   125  
   126  func getIDs(peers []string) ([]string, error) {
   127  	ids := make([]string, 0, len(peers))
   128  
   129  	for _, peer := range peers {
   130  
   131  		spl := strings.Split(peer, "@")
   132  		if len(spl) != 2 {
   133  			return nil, p2p.ErrNetAddressNoID{Addr: peer}
   134  		}
   135  		ids = append(ids, spl[0])
   136  
   137  	}
   138  	return ids, nil
   139  }