github.com/johnathanhowell/sia@v0.5.1-beta.0.20160524050156-83dcc3d37c94/modules/gateway.go (about)

     1  package modules
     2  
     3  import (
     4  	"net"
     5  )
     6  
     7  const (
     8  	// GatewayDir is the name of the directory used to store the gateway's
     9  	// persistent data.
    10  	GatewayDir = "gateway"
    11  	// WellConnectedThreshold is the number of outbound connections at which the
    12  	// gateway will not attempt to make new outbound connections.
    13  	WellConnectedThreshold = 8
    14  )
    15  
    16  // TODO: Move this and it's functionality into the gateway package.
    17  var (
    18  	// BootstrapPeers is a list of peers that can be used to find other peers -
    19  	// when a client first connects to the network, the only options for
    20  	// finding peers are either manual entry of peers or to use a hardcoded
    21  	// bootstrap point. While the bootstrap point could be a central service,
    22  	// it can also be a list of peers that are known to be stable. We have
    23  	// chosen to hardcode known-stable peers.
    24  	BootstrapPeers = []NetAddress{
    25  		"23.239.14.98:9981",
    26  		"87.98.216.46:9981",
    27  		"45.79.132.35:9981",
    28  		"68.55.10.144:9981",
    29  		"109.206.33.225:9981",
    30  		"79.51.183.132:9981",
    31  		"176.9.59.110:9981",
    32  	}
    33  )
    34  
    35  type (
    36  	// Peer contains all the info necessary to Broadcast to a peer.
    37  	Peer struct {
    38  		NetAddress NetAddress `json:"netaddress"`
    39  		Version    string     `json:"version"`
    40  		Inbound    bool       `json:"inbound"`
    41  	}
    42  
    43  	// A PeerConn is the connection type used when communicating with peers during
    44  	// an RPC. For now it is identical to a net.Conn.
    45  	PeerConn interface {
    46  		net.Conn
    47  	}
    48  
    49  	// RPCFunc is the type signature of functions that handle RPCs. It is used for
    50  	// both the caller and the callee. RPCFuncs may perform locking. RPCFuncs may
    51  	// close the connection early, and it is recommended that they do so to avoid
    52  	// keeping the connection open after all necessary I/O has been performed.
    53  	RPCFunc func(PeerConn) error
    54  
    55  	// A Gateway facilitates the interactions between the local node and remote
    56  	// nodes (peers). It relays incoming blocks and transactions to local modules,
    57  	// and broadcasts outgoing blocks and transactions to peers. In a broad sense,
    58  	// it is responsible for ensuring that the local consensus set is consistent
    59  	// with the "network" consensus set.
    60  	Gateway interface {
    61  		// Connect establishes a persistent connection to a peer.
    62  		Connect(NetAddress) error
    63  
    64  		// Disconnect terminates a connection to a peer.
    65  		Disconnect(NetAddress) error
    66  
    67  		// Address returns the Gateway's address.
    68  		Address() NetAddress
    69  
    70  		// Peers returns the addresses that the Gateway is currently connected to.
    71  		Peers() []Peer
    72  
    73  		// RegisterRPC registers a function to handle incoming connections that
    74  		// supply the given RPC ID.
    75  		RegisterRPC(string, RPCFunc)
    76  
    77  		// RegisterConnectCall registers an RPC name and function to be called
    78  		// upon connecting to a peer.
    79  		RegisterConnectCall(string, RPCFunc)
    80  
    81  		// RPC calls an RPC on the given address. RPC cannot be called on an
    82  		// address that the Gateway is not connected to.
    83  		RPC(NetAddress, string, RPCFunc) error
    84  
    85  		// Broadcast transmits obj, prefaced by the RPC name, to all of the
    86  		// given peers in parallel.
    87  		Broadcast(name string, obj interface{}, peers []Peer)
    88  
    89  		// Close safely stops the Gateway's listener process.
    90  		Close() error
    91  	}
    92  )