github.com/fozzysec/SiaPrime@v0.0.0-20190612043147-66c8e8d11fe3/modules/gateway.go (about)

     1  package modules
     2  
     3  import (
     4  	"net"
     5  
     6  	"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  	BootstrapPeers = build.Select(build.Var{
    23  		Standard: []NetAddress{
    24  			"50.116.9.206:4281",
    25  			"45.79.100.217:4281",
    26  		},
    27  		Dev:     []NetAddress(nil),
    28  		Testing: []NetAddress(nil),
    29  	}).([]NetAddress)
    30  )
    31  
    32  type (
    33  	// Peer contains all the info necessary to Broadcast to a peer.
    34  	Peer struct {
    35  		Inbound    bool       `json:"inbound"`
    36  		Local      bool       `json:"local"`
    37  		NetAddress NetAddress `json:"netaddress"`
    38  		Version    string     `json:"version"`
    39  	}
    40  
    41  	// A PeerConn is the connection type used when communicating with peers during
    42  	// an RPC. It is identical to a net.Conn with the additional RPCAddr method.
    43  	// This method acts as an identifier for peers and is the address that the
    44  	// peer can be dialed on. It is also the address that should be used when
    45  	// calling an RPC on the peer.
    46  	PeerConn interface {
    47  		net.Conn
    48  		RPCAddr() NetAddress
    49  	}
    50  
    51  	// RPCFunc is the type signature of functions that handle RPCs. It is used for
    52  	// both the caller and the callee. RPCFuncs may perform locking. RPCFuncs may
    53  	// close the connection early, and it is recommended that they do so to avoid
    54  	// keeping the connection open after all necessary I/O has been performed.
    55  	RPCFunc func(PeerConn) error
    56  
    57  	// A Gateway facilitates the interactions between the local node and remote
    58  	// nodes (peers). It relays incoming blocks and transactions to local modules,
    59  	// and broadcasts outgoing blocks and transactions to peers. In a broad sense,
    60  	// it is responsible for ensuring that the local consensus set is consistent
    61  	// with the "network" consensus set.
    62  	Gateway interface {
    63  		// Connect establishes a persistent connection to a peer.
    64  		Connect(NetAddress) error
    65  
    66  		// Disconnect terminates a connection to a peer.
    67  		Disconnect(NetAddress) error
    68  
    69  		// DiscoverAddress discovers and returns the current public IP address
    70  		// of the gateway. Contrary to Address, DiscoverAddress is blocking and
    71  		// might take multiple minutes to return. A channel to cancel the
    72  		// discovery can be supplied optionally.
    73  		DiscoverAddress(cancel <-chan struct{}) (NetAddress, error)
    74  
    75  		// ForwardPort adds a port mapping to the router. It will block until
    76  		// the mapping is established or until it is interrupted by a shutdown.
    77  		ForwardPort(port string) error
    78  
    79  		// Address returns the Gateway's address.
    80  		Address() NetAddress
    81  
    82  		// Peers returns the addresses that the Gateway is currently connected to.
    83  		Peers() []Peer
    84  
    85  		// RegisterRPC registers a function to handle incoming connections that
    86  		// supply the given RPC ID.
    87  		RegisterRPC(string, RPCFunc)
    88  
    89  		// UnregisterRPC unregisters an RPC and removes all references to the RPCFunc
    90  		// supplied in the corresponding RegisterRPC call. References to RPCFuncs
    91  		// registered with RegisterConnectCall are not removed and should be removed
    92  		// with UnregisterConnectCall. If the RPC does not exist no action is taken.
    93  		UnregisterRPC(string)
    94  
    95  		// RegisterConnectCall registers an RPC name and function to be called
    96  		// upon connecting to a peer.
    97  		RegisterConnectCall(string, RPCFunc)
    98  
    99  		// UnregisterConnectCall unregisters an RPC and removes all references to the
   100  		// RPCFunc supplied in the corresponding RegisterConnectCall call. References
   101  		// to RPCFuncs registered with RegisterRPC are not removed and should be
   102  		// removed with UnregisterRPC. If the RPC does not exist no action is taken.
   103  		UnregisterConnectCall(string)
   104  
   105  		// RPC calls an RPC on the given address. RPC cannot be called on an
   106  		// address that the Gateway is not connected to.
   107  		RPC(NetAddress, string, RPCFunc) error
   108  
   109  		// Broadcast transmits obj, prefaced by the RPC name, to all of the
   110  		// given peers in parallel.
   111  		Broadcast(name string, obj interface{}, peers []Peer)
   112  
   113  		// Online returns true if the gateway is connected to remote hosts
   114  		Online() bool
   115  
   116  		// Close safely stops the Gateway's listener process.
   117  		Close() error
   118  	}
   119  )