-
Notifications
You must be signed in to change notification settings - Fork 9
/
p2p.go
263 lines (220 loc) · 7.22 KB
/
p2p.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// Package p2p provides the peer-to-peer abstractions used
// across different protocols in Aurora.
package p2p
import (
"context"
"io"
"net"
"time"
"github.com/gauss-project/aurorafs/pkg/aurora"
"github.com/gauss-project/aurorafs/pkg/boson"
"github.com/gauss-project/aurorafs/pkg/routetab/pb"
"github.com/libp2p/go-libp2p-core/network"
libp2ppeer "github.com/libp2p/go-libp2p-core/peer"
ma "github.com/multiformats/go-multiaddr"
)
// ReachabilityStatus represents the node reachability status.
type ReachabilityStatus network.Reachability
// String implements the fmt.Stringer interface.
func (rs ReachabilityStatus) String() string {
return network.Reachability(rs).String()
}
const (
ReachabilityStatusUnknown = ReachabilityStatus(network.ReachabilityUnknown)
ReachabilityStatusPublic = ReachabilityStatus(network.ReachabilityPublic)
ReachabilityStatusPrivate = ReachabilityStatus(network.ReachabilityPrivate)
)
// NetworkStatus represents the network availability status.
type NetworkStatus int
// String implements the fmt.Stringer interface.
func (ns NetworkStatus) String() string {
str := [...]string{
NetworkStatusUnknown: "Unknown",
NetworkStatusAvailable: "Available",
NetworkStatusUnavailable: "Unavailable",
}
if ns < 0 || int(ns) >= len(str) {
return "(unrecognized)"
}
return str[ns]
}
const (
NetworkStatusUnknown NetworkStatus = 0
NetworkStatusAvailable NetworkStatus = 1
NetworkStatusUnavailable NetworkStatus = 2
)
// Service provides methods to handle p2p Peers and Protocols.
type Service interface {
CallHandlerWithConnChain(ctx context.Context, last, src Peer, stream Stream, protocolName, protocolVersion, streamName string) error
CallHandler(ctx context.Context, last Peer, stream Stream) (relayData *pb.RouteRelayReq, w *WriterChan, r *ReaderChan, forward bool, err error)
AddProtocol(ProtocolSpec) error
Connect
Disconnecter
Peers() []Peer
PeerID(overlay boson.Address) (id libp2ppeer.ID, found bool)
ResourceManager() network.ResourceManager
BlocklistedPeers() ([]BlockPeers, error)
BlocklistRemove(overlay boson.Address) error
Addresses() ([]ma.Multiaddr, error)
NATAddresses() ([]net.Addr, error)
SetPickyNotifier(PickyNotifier)
Halter
NetworkStatuser
}
type Connect interface {
// Connect to a peer but do not notify topology about the established connection.
Connect(ctx context.Context, addr ma.Multiaddr) (peer *Peer, err error)
}
type Disconnecter interface {
Disconnect(overlay boson.Address, reason string) error
Blocklister
}
// NetworkStatuser handles bookkeeping of the network availability status.
type NetworkStatuser interface {
// NetworkStatus returns current network availability status.
NetworkStatus() NetworkStatus
}
type Blocklister interface {
NetworkStatuser
// Blocklist will disconnect a peer and put it on a blocklist (blocking in & out connections) for provided duration
// Duration 0 is treated as an infinite duration.
Blocklist(overlay boson.Address, duration time.Duration, reason string) error
}
type Halter interface {
// Halt new incoming connections while shutting down
Halt()
}
// PickyNotifier can decide whether a peer should be picked
type PickyNotifier interface {
Picker
Notifier
ReachabilityUpdater
ReachableNotifier
}
type Picker interface {
Pick(Peer) bool
}
type ReachableNotifier interface {
Reachable(boson.Address, ReachabilityStatus)
}
type Reacher interface {
Connected(boson.Address, ma.Multiaddr)
Disconnected(boson.Address)
}
type ReachabilityUpdater interface {
UpdateReachability(ReachabilityStatus)
}
type Notifier interface {
Connected(context.Context, Peer, bool) error
Disconnected(peer Peer, reason string)
Announce(ctx context.Context, peer boson.Address, fullnode bool) error
AnnounceTo(ctx context.Context, addressee, peer boson.Address, fullnode bool) error
NotifyPeerState(peer PeerInfo)
}
// DebugService extends the Service with method used for debugging.
type DebugService interface {
Service
SetWelcomeMessage(val string) error
GetWelcomeMessage() string
}
// Streamer is able to create a new Stream.
type Streamer interface {
NewStream(ctx context.Context, address boson.Address, h Headers, protocol, version, stream string) (Stream, error)
NewRelayStream(ctx context.Context, address boson.Address, h Headers, protocol, version, stream string, midCall bool) (Stream, error)
NewConnChainRelayStream(ctx context.Context, target boson.Address, h Headers, protocolName, protocolVersion, streamName string) (Stream, error)
}
type StreamerDisconnecter interface {
Streamer
Disconnecter
}
// Pinger interface is used to ping a underlay address which is not yet known to the aurora node.
// It uses libp2p's default ping protocol. This is different from the PingPong protocol as this
// is meant to be used before we know a particular underlay and we can consider it useful
type Pinger interface {
Ping(ctx context.Context, addr ma.Multiaddr) (rtt time.Duration, err error)
}
type StreamerPinger interface {
Streamer
Pinger
}
// Stream represent a bidirectional data Stream.
type Stream interface {
io.ReadWriter
io.Closer
ResponseHeaders() Headers
Headers() Headers
FullClose() error
Reset() error
}
type VirtualStream interface {
Stream
UpdateStatRealStreamClosed()
Reader() *ReaderChan
Writer() *WriterChan
Done() chan struct{}
RealStream() Stream
}
// ProtocolSpec defines a collection of Stream specifications with handlers.
type ProtocolSpec struct {
Name string
Version string
StreamSpecs []StreamSpec
ConnectIn func(context.Context, Peer) error
ConnectOut func(context.Context, Peer) error
DisconnectIn func(Peer) error
DisconnectOut func(Peer) error
}
// StreamSpec defines a Stream handling within the protocol.
type StreamSpec struct {
Name string
Handler HandlerFunc
Headler HeadlerFunc
}
type BlockPeers struct {
Address boson.Address `json:"address"`
Timestamp string `json:"timestamp"`
Duration float64 `json:"duration"`
}
// Peer holds information about a Peer.
type Peer struct {
Address boson.Address `json:"address"`
Mode aurora.Model `json:"mode"`
}
type PeerInfo struct {
Overlay boson.Address `json:"overlay"`
Mode []byte `json:"mode"`
State PeerState `json:"state"`
Reason string `json:"reason"`
}
type WriterChan struct {
W chan []byte
Err chan error
}
type ReaderChan struct {
R chan []byte
Err chan error
}
// HandlerFunc handles a received Stream from a Peer.
type HandlerFunc func(context.Context, Peer, Stream) error
// HandlerMiddleware decorates a HandlerFunc by returning a new one.
type HandlerMiddleware func(HandlerFunc) HandlerFunc
// HeadlerFunc is returning response headers based on the received request
// headers.
type HeadlerFunc func(Headers, boson.Address) Headers
// Headers represents a collection of p2p header key value pairs.
type Headers map[string][]byte
// Common header names.
const (
HeaderNameTracingSpanContext = "tracing-span-context"
)
// NewAuroraStreamName constructs a libp2p compatible stream name out of
// protocol name and version and stream name.
func NewAuroraStreamName(protocol, version, stream string) string {
return "/boson/" + protocol + "/" + version + "/" + stream
}
type PeerState int
const (
PeerStateConnectIn PeerState = iota + 1
PeerStateConnectOut
PeerStateDisconnect
)