-
Notifications
You must be signed in to change notification settings - Fork 3
/
handlers.go
280 lines (226 loc) · 8.01 KB
/
handlers.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package zikade
import (
"bytes"
"context"
"errors"
"fmt"
ds "github.com/ipfs/go-datastore"
record "github.com/libp2p/go-libp2p-record"
recpb "github.com/libp2p/go-libp2p-record/pb"
"github.com/libp2p/go-libp2p/core/peer"
"go.opentelemetry.io/otel/attribute"
otel "go.opentelemetry.io/otel/trace"
"golang.org/x/exp/slog"
"github.com/probe-lab/zikade/kadt"
"github.com/probe-lab/zikade/pb"
)
// handleFindPeer handles FIND_NODE requests from remote peers.
func (d *DHT) handleFindPeer(ctx context.Context, remote peer.ID, req *pb.Message) (*pb.Message, error) {
if len(req.GetKey()) == 0 {
return nil, fmt.Errorf("handleFindPeer with empty key")
}
// tell the coordinator that this peer supports finding closer nodes
d.kad.NotifyConnectivity(ctx, kadt.PeerID(remote))
// "parse" requested peer ID from the key field
target := peer.ID(req.GetKey())
// initialize the response message
resp := &pb.Message{
Type: pb.Message_FIND_NODE,
Key: req.GetKey(),
}
// get reference to peer store
pstore := d.host.Peerstore()
// if the remote is asking for us, short-circuit and return us only
if target == d.host.ID() {
resp.CloserPeers = []*pb.Message_Peer{pb.FromAddrInfo(pstore.PeerInfo(d.host.ID()))}
return resp, nil
}
// gather closer peers that we know
resp.CloserPeers = d.closerPeers(ctx, remote, kadt.PeerID(target).Key())
// if we happen to know the target peers addresses (e.g., although we are
// far away in the keyspace), we add the peer to the result set. This means
// we potentially return bucketSize + 1 peers. We won't add the peer to the
// response if it's already contained in CloserPeers.
targetInfo := pstore.PeerInfo(target)
if len(targetInfo.Addrs) > 0 && !resp.ContainsCloserPeer(target) && target != remote {
resp.CloserPeers = append(resp.CloserPeers, pb.FromAddrInfo(targetInfo))
}
return resp, nil
}
// handlePing handles PING requests from remote peers.
func (d *DHT) handlePing(ctx context.Context, remote peer.ID, req *pb.Message) (*pb.Message, error) {
d.log.LogAttrs(ctx, slog.LevelDebug, "Responding to ping", slog.String("remote", remote.String()))
return &pb.Message{Type: pb.Message_PING}, nil
}
// handleGetValue handles PUT_VALUE RPCs from remote peers.
func (d *DHT) handlePutValue(ctx context.Context, remote peer.ID, req *pb.Message) (*pb.Message, error) {
// validate incoming request -> key and record must not be empty/nil
k := string(req.GetKey())
if len(k) == 0 {
return nil, fmt.Errorf("no key was provided")
}
rec := req.GetRecord()
if rec == nil {
return nil, fmt.Errorf("nil record")
}
if !bytes.Equal(req.GetKey(), rec.GetKey()) {
return nil, fmt.Errorf("key doesn't match record key")
}
// TODO: use putValueLocal?
// key is /$namespace/$binary_id
ns, path, err := record.SplitKey(k) // get namespace (prefix of the key)
if err != nil || len(path) == 0 {
return nil, fmt.Errorf("invalid key %s: %w", k, err)
}
backend, found := d.backends[ns]
if !found {
return nil, fmt.Errorf("unsupported record type: %s", ns)
}
_, err = backend.Store(ctx, path, rec)
return nil, err
}
// handleGetValue handles GET_VALUE RPCs from remote peers.
func (d *DHT) handleGetValue(ctx context.Context, remote peer.ID, req *pb.Message) (*pb.Message, error) {
k := string(req.GetKey())
if len(k) == 0 {
return nil, fmt.Errorf("handleGetValue but no key in request")
}
// prepare the response message
resp := &pb.Message{
Type: pb.Message_GET_VALUE,
Key: req.GetKey(),
CloserPeers: d.closerPeers(ctx, remote, kadt.NewKey(req.GetKey())),
}
ns, path, err := record.SplitKey(k) // get namespace (prefix of the key)
if err != nil || path == "" {
return nil, fmt.Errorf("invalid key %s: %w", k, err)
}
backend, found := d.backends[ns]
if !found {
return nil, fmt.Errorf("unsupported record type: %s", ns)
}
fetched, err := backend.Fetch(ctx, path)
if err != nil {
if errors.Is(err, ds.ErrNotFound) {
return resp, nil
}
return nil, fmt.Errorf("fetch record for key %s: %w", k, err)
} else if fetched == nil {
return resp, nil
}
rec, ok := fetched.(*recpb.Record)
if ok {
resp.Record = rec
return resp, nil
}
// the returned value wasn't a record, which could be the case if the
// key was prefixed with "providers."
pset, ok := fetched.(*providerSet)
if ok {
resp.ProviderPeers = make([]*pb.Message_Peer, len(pset.providers))
for i, p := range pset.providers {
resp.ProviderPeers[i] = pb.FromAddrInfo(p)
}
return resp, nil
}
return nil, fmt.Errorf("expected *recpb.Record or *providerSet value type, got: %T", pset)
}
// handleAddProvider handles ADD_PROVIDER RPCs from remote peers.
func (d *DHT) handleAddProvider(ctx context.Context, remote peer.ID, req *pb.Message) (*pb.Message, error) {
k := string(req.GetKey())
if len(k) > 80 {
return nil, fmt.Errorf("key size too large")
} else if len(k) == 0 {
return nil, fmt.Errorf("key is empty")
} else if len(req.GetProviderPeers()) == 0 {
return nil, fmt.Errorf("no provider peers given")
}
var addrInfos []peer.AddrInfo
for _, addrInfo := range req.ProviderAddrInfos() {
addrInfo := addrInfo // TODO: remove after go.mod was updated to go 1.21
if addrInfo.ID != remote {
return nil, fmt.Errorf("attempted to store provider record for other peer %s", addrInfo.ID)
}
if len(addrInfo.Addrs) == 0 {
return nil, fmt.Errorf("no addresses for provider")
}
addrInfos = append(addrInfos, addrInfo)
}
backend, ok := d.backends[namespaceProviders]
if !ok {
return nil, fmt.Errorf("unsupported record type: %s", namespaceProviders)
}
for _, addrInfo := range addrInfos {
if _, err := backend.Store(ctx, k, addrInfo); err != nil {
return nil, fmt.Errorf("storing provider record: %w", err)
}
}
return nil, nil
}
// handleGetProviders handles GET_PROVIDERS RPCs from remote peers.
func (d *DHT) handleGetProviders(ctx context.Context, remote peer.ID, req *pb.Message) (*pb.Message, error) {
k := req.GetKey()
if len(k) > 80 {
return nil, fmt.Errorf("handleGetProviders key size too large")
} else if len(k) == 0 {
return nil, fmt.Errorf("handleGetProviders key is empty")
}
backend, ok := d.backends[namespaceProviders]
if !ok {
return nil, fmt.Errorf("unsupported record type: %s", namespaceProviders)
}
resp := &pb.Message{
Type: pb.Message_GET_PROVIDERS,
Key: k,
CloserPeers: d.closerPeers(ctx, remote, kadt.NewKey(k)),
}
fetched, err := backend.Fetch(ctx, string(req.GetKey()))
if err != nil {
if errors.Is(err, ds.ErrNotFound) {
return resp, nil
}
return nil, fmt.Errorf("fetch providers from datastore: %w", err)
}
pset, ok := fetched.(*providerSet)
if !ok {
return nil, fmt.Errorf("expected *providerSet value type, got: %T", pset)
}
pbProviders := make([]*pb.Message_Peer, len(pset.providers))
for i, p := range pset.providers {
pbProviders[i] = pb.FromAddrInfo(p)
}
resp.ProviderPeers = pbProviders
return resp, nil
}
// closerPeers returns the closest peers to the given target key this host knows
// about. It doesn't return 1) itself 2) the peer that asked for closer peers.
func (d *DHT) closerPeers(ctx context.Context, remote peer.ID, target kadt.Key) []*pb.Message_Peer {
_, span := d.tele.Tracer.Start(ctx, "DHT.closerPeers", otel.WithAttributes(attribute.String("remote", remote.String()), attribute.String("target", target.HexString())))
defer span.End()
peers := d.rt.NearestNodes(target, d.cfg.BucketSize)
if len(peers) == 0 {
return nil
}
// pre-allocated the result set slice.
filtered := make([]*pb.Message_Peer, 0, len(peers))
for _, p := range peers {
pid := peer.ID(p) // TODO: type cast
// check for own peer ID
if pid == d.host.ID() {
d.log.Warn("routing table NearestNodes returned our own ID")
continue
}
// Don't send a peer back themselves
if pid == remote {
continue
}
// extract peer information from peer store and only add it to the
// final list if we know any addresses of that peer.
addrInfo := d.host.Peerstore().PeerInfo(pid)
if len(addrInfo.Addrs) == 0 {
continue
}
filtered = append(filtered, pb.FromAddrInfo(addrInfo))
}
return filtered
}