Address comments from PR
Jeromy committed
Mar 31, 2015 at 14:41 UTC
bb1b0d50e0975d437c15c92ce2ea324d0680333b
3 files changed
+28
-36
namesys/routing.go
+3
-27
@@ -7,8 +7,6 @@ import (
7
mh "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
8
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
9
pb "github.com/ipfs/go-ipfs/namesys/internal/pb"
10
- ci "github.com/ipfs/go-ipfs/p2p/crypto"
11
- peer "github.com/ipfs/go-ipfs/p2p/peer"
10
routing "github.com/ipfs/go-ipfs/routing"
11
u "github.com/ipfs/go-ipfs/util"
12
)
@@ -65,31 +63,9 @@ func (r *routingResolver) Resolve(ctx context.Context, name string) (u.Key, erro
63
}
64
65
// name should be a public key retrievable from ipfs
68
- // /ipfs/<name>
69
- var pubkey ci.PubKey
70
- if dht, ok := r.routing.(routing.PubKeyFetcher); ok {
71
- // If we have a DHT as our routing system, use optimized fetcher
72
- pk, err := dht.GetPublicKey(ctx, peer.ID(hash))
73
- if err != nil {
74
- log.Warning("RoutingResolve PubKey Get failed.")
75
- return "", err
76
- }
77
- pubkey = pk
78
- } else {
79
- key := u.Key("/pk/" + string(hash))
80
- pkval, err := r.routing.GetValue(ctx, key)
81
- if err != nil {
82
- log.Warning("RoutingResolve PubKey Get failed.")
83
- return "", err
84
- }
85
-
86
- // get PublicKey from node.Data
87
- pk, err := ci.UnmarshalPublicKey(pkval)
88
- if err != nil {
89
- return "", err
90
- }
91
-
92
- pubkey = pk
66
+ pubkey, err := routing.GetPublicKey(r.routing, ctx, hash)
67
+ if err != nil {
68
+ return "", err
69
}
70
71
hsh, _ := pubkey.Hash()
routing/dht/records.go
+3
-9
@@ -6,18 +6,12 @@ import (
6
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
7
ci "github.com/ipfs/go-ipfs/p2p/crypto"
8
peer "github.com/ipfs/go-ipfs/p2p/peer"
9
+ routing "github.com/ipfs/go-ipfs/routing"
10
pb "github.com/ipfs/go-ipfs/routing/dht/pb"
11
record "github.com/ipfs/go-ipfs/routing/record"
11
- u "github.com/ipfs/go-ipfs/util"
12
ctxutil "github.com/ipfs/go-ipfs/util/ctx"
13
)
14
15
-// KeyForPublicKey returns the key used to retrieve public keys
16
-// from the dht.
17
-func KeyForPublicKey(id peer.ID) u.Key {
18
- return u.Key("/pk/" + string(id))
19
-}
20
-
15
func (dht *IpfsDHT) GetPublicKey(ctx context.Context, p peer.ID) (ci.PubKey, error) {
16
log.Debugf("getPublicKey for: %s", p)
17
@@ -40,7 +34,7 @@ func (dht *IpfsDHT) GetPublicKey(ctx context.Context, p peer.ID) (ci.PubKey, err
34
35
// last ditch effort: let's try the dht.
36
log.Debugf("pk for %s not in peerstore, and peer failed. trying dht.", p)
43
- pkkey := KeyForPublicKey(p)
37
+ pkkey := routing.KeyForPublicKey(p)
38
39
val, err := dht.GetValue(ctxT, pkkey)
40
if err != nil {
@@ -65,7 +59,7 @@ func (dht *IpfsDHT) getPublicKeyFromNode(ctx context.Context, p peer.ID) (ci.Pub
59
return pk, nil
60
}
61
68
- pkkey := KeyForPublicKey(p)
62
+ pkkey := routing.KeyForPublicKey(p)
63
pmes, err := dht.getValueSingle(ctx, p, pkkey)
64
if err != nil {
65
return nil, err
routing/routing.go
+22
@@ -51,3 +51,25 @@ type IpfsRouting interface {
51
type PubKeyFetcher interface {
52
GetPublicKey(context.Context, peer.ID) (ci.PubKey, error)
53
}
54
+
55
+// KeyForPublicKey returns the key used to retrieve public keys
56
+// from the dht.
57
+func KeyForPublicKey(id peer.ID) u.Key {
58
+ return u.Key("/pk/" + string(id))
59
+}
60
+
61
+func GetPublicKey(r IpfsRouting, ctx context.Context, pkhash []byte) (ci.PubKey, error) {
62
+ if dht, ok := r.(PubKeyFetcher); ok {
63
+ // If we have a DHT as our routing system, use optimized fetcher
64
+ return dht.GetPublicKey(ctx, peer.ID(pkhash))
65
+ } else {
66
+ key := u.Key("/pk/" + string(pkhash))
67
+ pkval, err := r.GetValue(ctx, key)
68
+ if err != nil {
69
+ return nil, err
70
+ }
71
+
72
+ // get PublicKey from node.Data
73
+ return ci.UnmarshalPublicKey(pkval)
74
+ }
75
+}