1
1
package dht
2
2
3
3
import (
4
+ "bytes"
4
5
"context"
5
6
"errors"
6
7
"fmt"
@@ -59,7 +60,7 @@ func (dht *IpfsDHT) handleGetValue(ctx context.Context, p peer.ID, pmes *pb.Mess
59
60
60
61
// first, is there even a key?
61
62
k := pmes .GetKey ()
62
- if k == "" {
63
+ if len ( k ) == 0 {
63
64
return nil , errors .New ("handleGetValue but no key was provided" )
64
65
// TODO: send back an error response? could be bad, but the other node's hanging.
65
66
}
@@ -90,7 +91,7 @@ func (dht *IpfsDHT) handleGetValue(ctx context.Context, p peer.ID, pmes *pb.Mess
90
91
return resp , nil
91
92
}
92
93
93
- func (dht * IpfsDHT ) checkLocalDatastore (k string ) (* recpb.Record , error ) {
94
+ func (dht * IpfsDHT ) checkLocalDatastore (k [] byte ) (* recpb.Record , error ) {
94
95
log .Debugf ("%s handleGetValue looking into ds" , dht .self )
95
96
dskey := convertToDsKey (k )
96
97
iVal , err := dht .datastore .Get (dskey )
@@ -150,8 +151,7 @@ func (dht *IpfsDHT) checkLocalDatastore(k string) (*recpb.Record, error) {
150
151
151
152
// Cleans the record (to avoid storing arbitrary data).
152
153
func cleanRecord (rec * recpb.Record ) {
153
- rec .XXX_unrecognized = nil
154
- rec .TimeReceived = nil
154
+ rec .TimeReceived = ""
155
155
}
156
156
157
157
// Store a value in this peer local storage
@@ -170,14 +170,14 @@ func (dht *IpfsDHT) handlePutValue(ctx context.Context, p peer.ID, pmes *pb.Mess
170
170
return nil , errors .New ("nil record" )
171
171
}
172
172
173
- if pmes .GetKey () != rec .GetKey () {
173
+ if ! bytes . Equal ( pmes .GetKey (), rec .GetKey () ) {
174
174
return nil , errors .New ("put key doesn't match record key" )
175
175
}
176
176
177
177
cleanRecord (rec )
178
178
179
179
// Make sure the record is valid (not expired, valid signature etc)
180
- if err = dht .Validator .Validate (rec .GetKey (), rec .GetValue ()); err != nil {
180
+ if err = dht .Validator .Validate (string ( rec .GetKey () ), rec .GetValue ()); err != nil {
181
181
log .Warningf ("Bad dht record in PUT from: %s. %s" , p .Pretty (), err )
182
182
return nil , err
183
183
}
@@ -194,7 +194,7 @@ func (dht *IpfsDHT) handlePutValue(ctx context.Context, p peer.ID, pmes *pb.Mess
194
194
195
195
if existing != nil {
196
196
recs := [][]byte {rec .GetValue (), existing .GetValue ()}
197
- i , err := dht .Validator .Select (rec .GetKey (), recs )
197
+ i , err := dht .Validator .Select (string ( rec .GetKey () ), recs )
198
198
if err != nil {
199
199
log .Warningf ("Bad dht record in PUT from %s: %s" , p .Pretty (), err )
200
200
return nil , err
@@ -206,7 +206,7 @@ func (dht *IpfsDHT) handlePutValue(ctx context.Context, p peer.ID, pmes *pb.Mess
206
206
}
207
207
208
208
// record the time we receive every record
209
- rec .TimeReceived = proto . String ( u .FormatRFC3339 (time .Now () ))
209
+ rec .TimeReceived = u .FormatRFC3339 (time .Now ())
210
210
211
211
data , err := proto .Marshal (rec )
212
212
if err != nil {
@@ -245,7 +245,7 @@ func (dht *IpfsDHT) getRecordFromDatastore(dskey ds.Key) (*recpb.Record, error)
245
245
return nil , nil
246
246
}
247
247
248
- err = dht .Validator .Validate (rec .GetKey (), rec .GetValue ())
248
+ err = dht .Validator .Validate (string ( rec .GetKey () ), rec .GetValue ())
249
249
if err != nil {
250
250
// Invalid record in datastore, probably expired but don't return an error,
251
251
// we'll just overwrite it
@@ -263,7 +263,7 @@ func (dht *IpfsDHT) handlePing(_ context.Context, p peer.ID, pmes *pb.Message) (
263
263
264
264
func (dht * IpfsDHT ) handleFindPeer (ctx context.Context , p peer.ID , pmes * pb.Message ) (* pb.Message , error ) {
265
265
defer log .EventBegin (ctx , "handleFindPeer" , p ).Done ()
266
- resp := pb .NewMessage (pmes .GetType (), "" , pmes .GetClusterLevel ())
266
+ resp := pb .NewMessage (pmes .GetType (), nil , pmes .GetClusterLevel ())
267
267
var closest []peer.ID
268
268
269
269
// if looking for self... special case where we send it on CloserPeers.
@@ -331,7 +331,7 @@ func (dht *IpfsDHT) handleGetProviders(ctx context.Context, p peer.ID, pmes *pb.
331
331
defer log .Debugf ("%s end" , reqDesc )
332
332
333
333
// check if we have this value, to add ourselves as provider.
334
- has , err := dht .datastore .Has (convertToDsKey (c .KeyString ()))
334
+ has , err := dht .datastore .Has (convertToDsKey (c .Bytes ()))
335
335
if err != nil && err != ds .ErrNotFound {
336
336
log .Debugf ("unexpected datastore error: %v\n " , err )
337
337
has = false
@@ -403,6 +403,6 @@ func (dht *IpfsDHT) handleAddProvider(ctx context.Context, p peer.ID, pmes *pb.M
403
403
return nil , nil
404
404
}
405
405
406
- func convertToDsKey (s string ) ds.Key {
407
- return ds .NewKey (base32 .RawStdEncoding .EncodeToString ([] byte ( s ) ))
406
+ func convertToDsKey (s [] byte ) ds.Key {
407
+ return ds .NewKey (base32 .RawStdEncoding .EncodeToString (s ))
408
408
}
0 commit comments