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