-
Notifications
You must be signed in to change notification settings - Fork 0
/
gateway.go
119 lines (98 loc) · 4.36 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
109
110
111
112
113
114
115
116
117
118
119
package modules
import (
"net"
"SiaPrime/build"
)
const (
// GatewayDir is the name of the directory used to store the gateway's
// persistent data.
GatewayDir = "gateway"
)
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 = build.Select(build.Var{
Standard: []NetAddress{
"50.116.9.206:4281",
"45.79.100.217:4281",
},
Dev: []NetAddress(nil),
Testing: []NetAddress(nil),
}).([]NetAddress)
)
type (
// Peer contains all the info necessary to Broadcast to a peer.
Peer struct {
Inbound bool `json:"inbound"`
Local bool `json:"local"`
NetAddress NetAddress `json:"netaddress"`
Version string `json:"version"`
}
// 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
// DiscoverAddress discovers and returns the current public IP address
// of the gateway. Contrary to Address, DiscoverAddress is blocking and
// might take multiple minutes to return. A channel to cancel the
// discovery can be supplied optionally.
DiscoverAddress(cancel <-chan struct{}) (NetAddress, error)
// ForwardPort adds a port mapping to the router. It will block until
// the mapping is established or until it is interrupted by a shutdown.
ForwardPort(port string) 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)
// Online returns true if the gateway is connected to remote hosts
Online() bool
// Close safely stops the Gateway's listener process.
Close() error
}
)