github.com/number571/tendermint@v0.34.11-gost/rpc/core/net.go (about)

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