Skip to content

Commit 4d76fd2

Browse files
committed
upgrade protobuf and switch to bytes keys
fixes ipfs#177
1 parent 15cc53b commit 4d76fd2

11 files changed

+830
-124
lines changed

dht.go

+9-10
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,13 @@ func makeDHT(ctx context.Context, h host.Host, dstore ds.Batching, protocols []p
143143
}
144144

145145
// putValueToPeer stores the given key/value pair at the peer 'p'
146-
func (dht *IpfsDHT) putValueToPeer(ctx context.Context, p peer.ID,
147-
key string, rec *recpb.Record) error {
146+
func (dht *IpfsDHT) putValueToPeer(ctx context.Context, p peer.ID, rec *recpb.Record) error {
148147

149-
pmes := pb.NewMessage(pb.Message_PUT_VALUE, key, 0)
148+
pmes := pb.NewMessage(pb.Message_PUT_VALUE, rec.Key, 0)
150149
pmes.Record = rec
151150
rpmes, err := dht.sendRequest(ctx, p, pmes)
152151
if err != nil {
153-
log.Debugf("putValueToPeer: %v. (peer: %s, key: %s)", err, p.Pretty(), key)
152+
log.Debugf("putValueToPeer: %v. (peer: %s, key: %s)", err, p.Pretty(), loggableKey(string(rec.Key)))
154153
return err
155154
}
156155

@@ -183,7 +182,7 @@ func (dht *IpfsDHT) getValueOrPeers(ctx context.Context, p peer.ID, key string)
183182
log.Debug("getValueOrPeers: got value")
184183

185184
// make sure record is valid.
186-
err = dht.Validator.Validate(record.GetKey(), record.GetValue())
185+
err = dht.Validator.Validate(string(record.GetKey()), record.GetValue())
187186
if err != nil {
188187
log.Info("Received invalid record! (discarded)")
189188
// return a sentinal to signify an invalid record was received
@@ -212,7 +211,7 @@ func (dht *IpfsDHT) getValueSingle(ctx context.Context, p peer.ID, key string) (
212211
eip := log.EventBegin(ctx, "getValueSingle", meta)
213212
defer eip.Done()
214213

215-
pmes := pb.NewMessage(pb.Message_GET_VALUE, key, 0)
214+
pmes := pb.NewMessage(pb.Message_GET_VALUE, []byte(key), 0)
216215
resp, err := dht.sendRequest(ctx, p, pmes)
217216
switch err {
218217
case nil:
@@ -236,7 +235,7 @@ func (dht *IpfsDHT) getLocal(key string) (*recpb.Record, error) {
236235
}
237236

238237
// Double check the key. Can't hurt.
239-
if rec != nil && rec.GetKey() != key {
238+
if rec != nil && string(rec.GetKey()) != key {
240239
log.Errorf("BUG getLocal: found a DHT record that didn't match it's key: %s != %s", rec.GetKey(), key)
241240
return nil, nil
242241

@@ -293,7 +292,7 @@ func (dht *IpfsDHT) findPeerSingle(ctx context.Context, p peer.ID, id peer.ID) (
293292
})
294293
defer eip.Done()
295294

296-
pmes := pb.NewMessage(pb.Message_FIND_NODE, string(id), 0)
295+
pmes := pb.NewMessage(pb.Message_FIND_NODE, []byte(id), 0)
297296
resp, err := dht.sendRequest(ctx, p, pmes)
298297
switch err {
299298
case nil:
@@ -311,7 +310,7 @@ func (dht *IpfsDHT) findProvidersSingle(ctx context.Context, p peer.ID, key *cid
311310
eip := log.EventBegin(ctx, "findProvidersSingle", p, key)
312311
defer eip.Done()
313312

314-
pmes := pb.NewMessage(pb.Message_GET_PROVIDERS, key.KeyString(), 0)
313+
pmes := pb.NewMessage(pb.Message_GET_PROVIDERS, key.Bytes(), 0)
315314
resp, err := dht.sendRequest(ctx, p, pmes)
316315
switch err {
317316
case nil:
@@ -327,7 +326,7 @@ func (dht *IpfsDHT) findProvidersSingle(ctx context.Context, p peer.ID, key *cid
327326

328327
// nearestPeersToQuery returns the routing tables closest peers.
329328
func (dht *IpfsDHT) nearestPeersToQuery(pmes *pb.Message, count int) []peer.ID {
330-
closer := dht.routingTable.NearestPeers(kb.ConvertKey(pmes.GetKey()), count)
329+
closer := dht.routingTable.NearestPeers(kb.ConvertKey(string(pmes.GetKey())), count)
331330
return closer
332331
}
333332

ext_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ func TestGetFailures(t *testing.T) {
105105

106106
rec := record.MakePutRecord(str, []byte("blah"))
107107
req := pb.Message{
108-
Type: &typ,
109-
Key: &str,
108+
Type: typ,
109+
Key: []byte(str),
110110
Record: rec,
111111
}
112112

handlers.go

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dht
22

33
import (
4+
"bytes"
45
"context"
56
"errors"
67
"fmt"
@@ -59,7 +60,7 @@ func (dht *IpfsDHT) handleGetValue(ctx context.Context, p peer.ID, pmes *pb.Mess
5960

6061
// first, is there even a key?
6162
k := pmes.GetKey()
62-
if k == "" {
63+
if len(k) == 0 {
6364
return nil, errors.New("handleGetValue but no key was provided")
6465
// TODO: send back an error response? could be bad, but the other node's hanging.
6566
}
@@ -90,7 +91,7 @@ func (dht *IpfsDHT) handleGetValue(ctx context.Context, p peer.ID, pmes *pb.Mess
9091
return resp, nil
9192
}
9293

93-
func (dht *IpfsDHT) checkLocalDatastore(k string) (*recpb.Record, error) {
94+
func (dht *IpfsDHT) checkLocalDatastore(k []byte) (*recpb.Record, error) {
9495
log.Debugf("%s handleGetValue looking into ds", dht.self)
9596
dskey := convertToDsKey(k)
9697
iVal, err := dht.datastore.Get(dskey)
@@ -150,8 +151,7 @@ func (dht *IpfsDHT) checkLocalDatastore(k string) (*recpb.Record, error) {
150151

151152
// Cleans the record (to avoid storing arbitrary data).
152153
func cleanRecord(rec *recpb.Record) {
153-
rec.XXX_unrecognized = nil
154-
rec.TimeReceived = nil
154+
rec.TimeReceived = ""
155155
}
156156

157157
// 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
170170
return nil, errors.New("nil record")
171171
}
172172

173-
if pmes.GetKey() != rec.GetKey() {
173+
if !bytes.Equal(pmes.GetKey(), rec.GetKey()) {
174174
return nil, errors.New("put key doesn't match record key")
175175
}
176176

177177
cleanRecord(rec)
178178

179179
// 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 {
181181
log.Warningf("Bad dht record in PUT from: %s. %s", p.Pretty(), err)
182182
return nil, err
183183
}
@@ -194,7 +194,7 @@ func (dht *IpfsDHT) handlePutValue(ctx context.Context, p peer.ID, pmes *pb.Mess
194194

195195
if existing != nil {
196196
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)
198198
if err != nil {
199199
log.Warningf("Bad dht record in PUT from %s: %s", p.Pretty(), err)
200200
return nil, err
@@ -206,7 +206,7 @@ func (dht *IpfsDHT) handlePutValue(ctx context.Context, p peer.ID, pmes *pb.Mess
206206
}
207207

208208
// record the time we receive every record
209-
rec.TimeReceived = proto.String(u.FormatRFC3339(time.Now()))
209+
rec.TimeReceived = u.FormatRFC3339(time.Now())
210210

211211
data, err := proto.Marshal(rec)
212212
if err != nil {
@@ -245,7 +245,7 @@ func (dht *IpfsDHT) getRecordFromDatastore(dskey ds.Key) (*recpb.Record, error)
245245
return nil, nil
246246
}
247247

248-
err = dht.Validator.Validate(rec.GetKey(), rec.GetValue())
248+
err = dht.Validator.Validate(string(rec.GetKey()), rec.GetValue())
249249
if err != nil {
250250
// Invalid record in datastore, probably expired but don't return an error,
251251
// we'll just overwrite it
@@ -263,7 +263,7 @@ func (dht *IpfsDHT) handlePing(_ context.Context, p peer.ID, pmes *pb.Message) (
263263

264264
func (dht *IpfsDHT) handleFindPeer(ctx context.Context, p peer.ID, pmes *pb.Message) (*pb.Message, error) {
265265
defer log.EventBegin(ctx, "handleFindPeer", p).Done()
266-
resp := pb.NewMessage(pmes.GetType(), "", pmes.GetClusterLevel())
266+
resp := pb.NewMessage(pmes.GetType(), nil, pmes.GetClusterLevel())
267267
var closest []peer.ID
268268

269269
// 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.
331331
defer log.Debugf("%s end", reqDesc)
332332

333333
// 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()))
335335
if err != nil && err != ds.ErrNotFound {
336336
log.Debugf("unexpected datastore error: %v\n", err)
337337
has = false
@@ -403,6 +403,6 @@ func (dht *IpfsDHT) handleAddProvider(ctx context.Context, p peer.ID, pmes *pb.M
403403
return nil, nil
404404
}
405405

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))
408408
}

handlers_test.go

+6-8
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@ import (
1010

1111
func TestCleanRecordSigned(t *testing.T) {
1212
actual := new(recpb.Record)
13-
actual.TimeReceived = proto.String("time")
14-
actual.XXX_unrecognized = []byte("extra data")
13+
actual.TimeReceived = "time"
1514
actual.Value = []byte("value")
16-
actual.Key = proto.String("key")
15+
actual.Key = []byte("key")
1716

1817
cleanRecord(actual)
1918
actualBytes, err := proto.Marshal(actual)
@@ -23,7 +22,7 @@ func TestCleanRecordSigned(t *testing.T) {
2322

2423
expected := new(recpb.Record)
2524
expected.Value = []byte("value")
26-
expected.Key = proto.String("key")
25+
expected.Key = []byte("key")
2726
expectedBytes, err := proto.Marshal(expected)
2827
if err != nil {
2928
t.Fatal(err)
@@ -36,9 +35,8 @@ func TestCleanRecordSigned(t *testing.T) {
3635

3736
func TestCleanRecord(t *testing.T) {
3837
actual := new(recpb.Record)
39-
actual.TimeReceived = proto.String("time")
40-
actual.XXX_unrecognized = []byte("extra data")
41-
actual.Key = proto.String("key")
38+
actual.TimeReceived = "time"
39+
actual.Key = []byte("key")
4240
actual.Value = []byte("value")
4341

4442
cleanRecord(actual)
@@ -48,7 +46,7 @@ func TestCleanRecord(t *testing.T) {
4846
}
4947

5048
expected := new(recpb.Record)
51-
expected.Key = proto.String("key")
49+
expected.Key = []byte("key")
5250
expected.Value = []byte("value")
5351
expectedBytes, err := proto.Marshal(expected)
5452
if err != nil {

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"version": "1.0.0"
3131
},
3232
{
33-
"hash": "QmZ4Qi3GaRbjcx28Sme5eMH7RQjGkt8wHxt2a65oLaeFEV",
33+
"hash": "QmdxUuburamoF6zF9qjeQC4WYcWGbWuRmdLacMEsW8ioD8",
3434
"name": "gogo-protobuf",
3535
"version": "0.0.0"
3636
},
@@ -72,9 +72,9 @@
7272
},
7373
{
7474
"author": "whyrusleeping",
75-
"hash": "QmVsp2KdPYE6M8ryzCk5KHLo3zprcY5hBDaYx6uPCFUdxA",
75+
"hash": "QmUKGC4P3FT4y3ThT6sesshDt4HQofKdee3C9oJknQ4s6p",
7676
"name": "go-libp2p-record",
77-
"version": "4.1.3"
77+
"version": "4.1.4"
7878
},
7979
{
8080
"author": "whyrusleeping",

pb/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ GO = $(PB:.proto=.pb.go)
44
all: $(GO)
55

66
%.pb.go: %.proto
7-
protoc --gogo_out=. --proto_path=../../../../../../:/usr/local/opt/protobuf/include:. $<
7+
protoc --proto_path=$(GOPATH)/src:. --gogofast_out=. $<
88

99
clean:
1010
rm -f *.pb.go

0 commit comments

Comments
 (0)