fix validators and key prefix
Jeromy committed
Nov 10, 2014 at 14:22 UTC
cb23d5b2ae5578d2e75c7a6966df19f442c271ce
4 files changed
+26
-16
routing/dht/dht_test.go
+15
-6
@@ -33,6 +33,9 @@ func setupDHT(ctx context.Context, t *testing.T, p peer.Peer) *IpfsDHT {
33
34
d := NewDHT(ctx, p, peerstore, net, dhts, ds.NewMapDatastore())
35
dhts.SetHandler(d)
36
+ d.Validators["v"] = func(u.Key, []byte) error {
37
+ return nil
38
+ }
39
return d
40
}
41
@@ -136,6 +139,12 @@ func TestValueGetSet(t *testing.T) {
139
dhtA := setupDHT(ctx, t, peerA)
140
dhtB := setupDHT(ctx, t, peerB)
141
142
+ vf := func(u.Key, []byte) error {
143
+ return nil
144
+ }
145
+ dhtA.Validators["v"] = vf
146
+ dhtB.Validators["v"] = vf
147
+
148
defer dhtA.Close()
149
defer dhtB.Close()
150
defer dhtA.dialer.(inet.Network).Close()
@@ -147,10 +156,10 @@ func TestValueGetSet(t *testing.T) {
156
}
157
158
ctxT, _ := context.WithTimeout(ctx, time.Second)
150
- dhtA.PutValue(ctxT, "hello", []byte("world"))
159
+ dhtA.PutValue(ctxT, "/v/hello", []byte("world"))
160
161
ctxT, _ = context.WithTimeout(ctx, time.Second*2)
153
- val, err := dhtA.GetValue(ctxT, "hello")
162
+ val, err := dhtA.GetValue(ctxT, "/v/hello")
163
if err != nil {
164
t.Fatal(err)
165
}
@@ -160,7 +169,7 @@ func TestValueGetSet(t *testing.T) {
169
}
170
171
ctxT, _ = context.WithTimeout(ctx, time.Second*2)
163
- val, err = dhtB.GetValue(ctxT, "hello")
172
+ val, err = dhtB.GetValue(ctxT, "/v/hello")
173
if err != nil {
174
t.Fatal(err)
175
}
@@ -326,12 +335,12 @@ func TestLayeredGet(t *testing.T) {
335
t.Fatal(err)
336
}
337
329
- err = dhts[3].putLocal(u.Key("hello"), []byte("world"))
338
+ err = dhts[3].putLocal(u.Key("/v/hello"), []byte("world"))
339
if err != nil {
340
t.Fatal(err)
341
}
342
334
- err = dhts[3].Provide(ctx, u.Key("hello"))
343
+ err = dhts[3].Provide(ctx, u.Key("/v/hello"))
344
if err != nil {
345
t.Fatal(err)
346
}
@@ -339,7 +348,7 @@ func TestLayeredGet(t *testing.T) {
348
time.Sleep(time.Millisecond * 60)
349
350
ctxT, _ := context.WithTimeout(ctx, time.Second)
342
- val, err := dhts[0].GetValue(ctxT, u.Key("hello"))
351
+ val, err := dhts[0].GetValue(ctxT, u.Key("/v/hello"))
352
if err != nil {
353
t.Fatal(err)
354
}
routing/dht/ext_test.go
+1
-3
@@ -4,6 +4,7 @@ import (
4
"testing"
5
6
crand "crypto/rand"
7
+
8
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
9
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
10
@@ -199,9 +200,6 @@ func TestGetFailures(t *testing.T) {
200
if pmes.GetRecord() != nil {
201
t.Fatal("shouldnt have value")
202
}
202
- if len(pmes.GetCloserPeers()) > 0 {
203
- t.Fatal("shouldnt have closer peers")
204
- }
203
if pmes.GetProviderPeers() != nil {
204
t.Fatal("shouldnt have provider peers")
205
}
routing/dht/handlers.go
+1
@@ -114,6 +114,7 @@ func (dht *IpfsDHT) handlePutValue(p peer.Peer, pmes *pb.Message) (*pb.Message,
114
115
err := dht.verifyRecord(pmes.GetRecord())
116
if err != nil {
117
+ fmt.Println(u.Key(pmes.GetRecord().GetAuthor()))
118
log.Error("Bad dht record in put request")
119
return nil, err
120
}
routing/dht/records.go
+9
-7
@@ -20,7 +20,7 @@ var ErrInvalidRecordType = errors.New("invalid record keytype")
20
func (dht *IpfsDHT) makePutRecord(key u.Key, value []byte) (*pb.Record, error) {
21
record := new(pb.Record)
22
23
- record.Key = proto.String(key.String())
23
+ record.Key = proto.String(string(key))
24
record.Value = value
25
record.Author = proto.String(string(dht.self.ID()))
26
blob := bytes.Join([][]byte{[]byte(key), value, []byte(dht.self.ID())}, []byte{})
@@ -38,13 +38,15 @@ func (dht *IpfsDHT) verifyRecord(r *pb.Record) error {
38
if err != nil {
39
return err
40
}
41
+ k := u.Key(r.GetKey())
42
42
- blob := bytes.Join([][]byte{[]byte(r.GetKey()),
43
+ blob := bytes.Join([][]byte{[]byte(k),
44
r.GetValue(),
44
- []byte(r.GetKey())}, []byte{})
45
+ []byte(r.GetAuthor())}, []byte{})
46
47
ok, err := p.PubKey().Verify(blob, r.GetSignature())
48
if err != nil {
49
+ log.Error("Signature verify failed.")
50
return err
51
}
52
@@ -54,14 +56,14 @@ func (dht *IpfsDHT) verifyRecord(r *pb.Record) error {
56
57
// Now, check validity func
58
parts := strings.Split(r.GetKey(), "/")
57
- if len(parts) < 2 {
58
- log.Error("Record had bad key: %s", r.GetKey())
59
+ if len(parts) < 3 {
60
+ log.Errorf("Record had bad key: %s", u.Key(r.GetKey()))
61
return ErrBadRecord
62
}
63
62
- fnc, ok := dht.Validators[parts[0]]
64
+ fnc, ok := dht.Validators[parts[1]]
65
if !ok {
64
- log.Errorf("Unrecognized key prefix: %s", parts[0])
66
+ log.Errorf("Unrecognized key prefix: %s", parts[1])
67
return ErrInvalidRecordType
68
}
69