gitlab.com/SiaPrime/SiaPrime@v1.4.1/modules/gateway.go (about)

     1  package modules
     2  
     3  import (
     4  	"net"
     5  
     6  	"gitlab.com/SiaPrime/SiaPrime/build"
     7  )
     8  
     9  const (
    10  	// GatewayDir is the name of the directory used to store the gateway's
    11  	// persistent data.
    12  	GatewayDir = "gateway"
    13  )
    14  
    15  var (
    16  	// BootstrapPeers is a list of peers that can be used to find other peers -
    17  	// when a client first connects to the network, the only options for
    18  	// finding peers are either manual entry of peers or to use a hardcoded
    19  	// bootstrap point. While the bootstrap point could be a central service,
    20  	// it can also be a list of peers that are known to be stable. We have
    21  	// chosen to hardcode known-stable peers.
    22  	//
    23  	// These peers have been verified to be v1.3.7 or higher
    24  	BootstrapPeers = build.Select(build.Var{
    25  		Standard: []NetAddress{
    26  			"50.116.9.206:4281",
    27  			"45.79.100.217:4281",
    28  			"52.39.226.32:4281",
    29  			"68.183.100.181:4281",
    30  			"59.167.191.60:4281",
    31  			"81.149.127.41:4281",
    32  			"107.2.170.129:4281",
    33  		},
    34  		Dev:     []NetAddress(nil),
    35  		Testing: []NetAddress(nil),
    36  	}).([]NetAddress)
    37  )
    38  
    39  type (
    40  	// Peer contains all the info necessary to Broadcast to a peer.
    41  	Peer struct {
    42  		Inbound    bool       `json:"inbound"`
    43  		Local      bool       `json:"local"`
    44  		NetAddress NetAddress `json:"netaddress"`
    45  		Version    string     `json:"version"`
    46  	}
    47  
    48  	// A PeerConn is the connection type used when communicating with peers during
    49  	// an RPC. It is identical to a net.Conn with the additional RPCAddr method.
    50  	// This method acts as an identifier for peers and is the address that the
    51  	// peer can be dialed on. It is also the address that should be used when
    52  	// calling an RPC on the peer.
    53  	PeerConn interface {
    54  		net.Conn
    55  		RPCAddr() NetAddress
    56  	}
    57  
    58  	// RPCFunc is the type signature of functions that handle RPCs. It is used for
    59  	// both the caller and the callee. RPCFuncs may perform locking. RPCFuncs may
    60  	// close the connection early, and it is recommended that they do so to avoid
    61  	// keeping the connection open after all necessary I/O has been performed.
    62  	RPCFunc func(PeerConn) error
    63  
    64  	// A Gateway facilitates the interactions between the local node and remote
    65  	// nodes (peers). It relays incoming blocks and transactions to local modules,
    66  	// and broadcasts outgoing blocks and transactions to peers. In a broad sense,
    67  	// it is responsible for ensuring that the local consensus set is consistent
    68  	// with the "network" consensus set.
    69  	Gateway interface {
    70  		// Connect establishes a persistent connection to a peer.
    71  		Connect(NetAddress) error
    72  
    73  		// ConnectManual is a Connect wrapper for a user-initiated Connect
    74  		ConnectManual(NetAddress) error
    75  
    76  		// Disconnect terminates a connection to a peer.
    77  		Disconnect(NetAddress) error
    78  
    79  		// DiscoverAddress discovers and returns the current public IP address
    80  		// of the gateway. Contrary to Address, DiscoverAddress is blocking and
    81  		// might take multiple minutes to return. A channel to cancel the
    82  		// discovery can be supplied optionally.
    83  		DiscoverAddress(cancel <-chan struct{}) (net.IP, error)
    84  
    85  		// ForwardPort adds a port mapping to the router. It will block until
    86  		// the mapping is established or until it is interrupted by a shutdown.
    87  		ForwardPort(port string) error
    88  
    89  		// DisconnectManual is a Disconnect wrapper for a user-initiated disconnect
    90  		DisconnectManual(NetAddress) error
    91  
    92  		// Address returns the Gateway's address.
    93  		Address() NetAddress
    94  
    95  		// Peers returns the addresses that the Gateway is currently connected to.
    96  		Peers() []Peer
    97  
    98  		// RegisterRPC registers a function to handle incoming connections that
    99  		// supply the given RPC ID.
   100  		RegisterRPC(string, RPCFunc)
   101  
   102  		// RateLimits returns the currently set bandwidth limits of the gateway.
   103  		RateLimits() (int64, int64)
   104  
   105  		// SetRateLimits changes the rate limits for the peer-connections of the
   106  		// gateway.
   107  		SetRateLimits(downloadSpeed, uploadSpeed int64) error
   108  
   109  		// UnregisterRPC unregisters an RPC and removes all references to the RPCFunc
   110  		// supplied in the corresponding RegisterRPC call. References to RPCFuncs
   111  		// registered with RegisterConnectCall are not removed and should be removed
   112  		// with UnregisterConnectCall. If the RPC does not exist no action is taken.
   113  		UnregisterRPC(string)
   114  
   115  		// RegisterConnectCall registers an RPC name and function to be called
   116  		// upon connecting to a peer.
   117  		RegisterConnectCall(string, RPCFunc)
   118  
   119  		// UnregisterConnectCall unregisters an RPC and removes all references to the
   120  		// RPCFunc supplied in the corresponding RegisterConnectCall call. References
   121  		// to RPCFuncs registered with RegisterRPC are not removed and should be
   122  		// removed with UnregisterRPC. If the RPC does not exist no action is taken.
   123  		UnregisterConnectCall(string)
   124  
   125  		// RPC calls an RPC on the given address. RPC cannot be called on an
   126  		// address that the Gateway is not connected to.
   127  		RPC(NetAddress, string, RPCFunc) error
   128  
   129  		// Broadcast transmits obj, prefaced by the RPC name, to all of the
   130  		// given peers in parallel.
   131  		Broadcast(name string, obj interface{}, peers []Peer)
   132  
   133  		// Online returns true if the gateway is connected to remote hosts
   134  		Online() bool
   135  
   136  		// Close safely stops the Gateway's listener process.
   137  		Close() error
   138  	}
   139  )