提交 be598ad1 作者: Jeromy Johnson

Merge pull request #987 from ipfs/feat/pubkey-cache

cache public keys and use better method for fetching
...@@ -7,7 +7,6 @@ import ( ...@@ -7,7 +7,6 @@ import (
mh "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash" mh "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context" "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
pb "github.com/ipfs/go-ipfs/namesys/internal/pb" pb "github.com/ipfs/go-ipfs/namesys/internal/pb"
ci "github.com/ipfs/go-ipfs/p2p/crypto"
routing "github.com/ipfs/go-ipfs/routing" routing "github.com/ipfs/go-ipfs/routing"
u "github.com/ipfs/go-ipfs/util" u "github.com/ipfs/go-ipfs/util"
) )
...@@ -64,25 +63,17 @@ func (r *routingResolver) Resolve(ctx context.Context, name string) (u.Key, erro ...@@ -64,25 +63,17 @@ func (r *routingResolver) Resolve(ctx context.Context, name string) (u.Key, erro
} }
// name should be a public key retrievable from ipfs // name should be a public key retrievable from ipfs
// /ipfs/<name> pubkey, err := routing.GetPublicKey(r.routing, ctx, hash)
key := u.Key("/pk/" + string(hash))
pkval, err := r.routing.GetValue(ctx, key)
if err != nil { if err != nil {
log.Warning("RoutingResolve PubKey Get failed.")
return "", err return "", err
} }
// get PublicKey from node.Data hsh, _ := pubkey.Hash()
pk, err := ci.UnmarshalPublicKey(pkval)
if err != nil {
return "", err
}
hsh, _ := pk.Hash()
log.Debugf("pk hash = %s", u.Key(hsh)) log.Debugf("pk hash = %s", u.Key(hsh))
// check sig with pk // check sig with pk
if ok, err := pk.Verify(ipnsEntryDataForSig(entry), entry.GetSignature()); err != nil || !ok { if ok, err := pubkey.Verify(ipnsEntryDataForSig(entry), entry.GetSignature()); err != nil || !ok {
return "", fmt.Errorf("Invalid value. Not signed by PrivateKey corresponding to %v", pk) return "", fmt.Errorf("Invalid value. Not signed by PrivateKey corresponding to %v", pubkey)
} }
// ok sig checks out. this is a valid name. // ok sig checks out. this is a valid name.
......
...@@ -6,19 +6,13 @@ import ( ...@@ -6,19 +6,13 @@ import (
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context" "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
ci "github.com/ipfs/go-ipfs/p2p/crypto" ci "github.com/ipfs/go-ipfs/p2p/crypto"
peer "github.com/ipfs/go-ipfs/p2p/peer" peer "github.com/ipfs/go-ipfs/p2p/peer"
routing "github.com/ipfs/go-ipfs/routing"
pb "github.com/ipfs/go-ipfs/routing/dht/pb" pb "github.com/ipfs/go-ipfs/routing/dht/pb"
record "github.com/ipfs/go-ipfs/routing/record" record "github.com/ipfs/go-ipfs/routing/record"
u "github.com/ipfs/go-ipfs/util"
ctxutil "github.com/ipfs/go-ipfs/util/ctx" ctxutil "github.com/ipfs/go-ipfs/util/ctx"
) )
// KeyForPublicKey returns the key used to retrieve public keys func (dht *IpfsDHT) GetPublicKey(ctx context.Context, p peer.ID) (ci.PubKey, error) {
// from the dht.
func KeyForPublicKey(id peer.ID) u.Key {
return u.Key("/pk/" + string(id))
}
func (dht *IpfsDHT) getPublicKeyOnline(ctx context.Context, p peer.ID) (ci.PubKey, error) {
log.Debugf("getPublicKey for: %s", p) log.Debugf("getPublicKey for: %s", p)
// check locally. // check locally.
...@@ -40,9 +34,8 @@ func (dht *IpfsDHT) getPublicKeyOnline(ctx context.Context, p peer.ID) (ci.PubKe ...@@ -40,9 +34,8 @@ func (dht *IpfsDHT) getPublicKeyOnline(ctx context.Context, p peer.ID) (ci.PubKe
// last ditch effort: let's try the dht. // last ditch effort: let's try the dht.
log.Debugf("pk for %s not in peerstore, and peer failed. trying dht.", p) log.Debugf("pk for %s not in peerstore, and peer failed. trying dht.", p)
pkkey := KeyForPublicKey(p) pkkey := routing.KeyForPublicKey(p)
// ok, now try the dht. Anyone who has previously fetched the key should have it
val, err := dht.GetValue(ctxT, pkkey) val, err := dht.GetValue(ctxT, pkkey)
if err != nil { if err != nil {
log.Warning("Failed to find requested public key.") log.Warning("Failed to find requested public key.")
...@@ -66,7 +59,7 @@ func (dht *IpfsDHT) getPublicKeyFromNode(ctx context.Context, p peer.ID) (ci.Pub ...@@ -66,7 +59,7 @@ func (dht *IpfsDHT) getPublicKeyFromNode(ctx context.Context, p peer.ID) (ci.Pub
return pk, nil return pk, nil
} }
pkkey := KeyForPublicKey(p) pkkey := routing.KeyForPublicKey(p)
pmes, err := dht.getValueSingle(ctx, p, pkkey) pmes, err := dht.getValueSingle(ctx, p, pkkey)
if err != nil { if err != nil {
return nil, err return nil, err
...@@ -132,7 +125,7 @@ func (dht *IpfsDHT) verifyRecordOnline(ctx context.Context, r *pb.Record) error ...@@ -132,7 +125,7 @@ func (dht *IpfsDHT) verifyRecordOnline(ctx context.Context, r *pb.Record) error
if len(r.Signature) > 0 { if len(r.Signature) > 0 {
// get the public key, search for it if necessary. // get the public key, search for it if necessary.
p := peer.ID(r.GetAuthor()) p := peer.ID(r.GetAuthor())
pk, err := dht.getPublicKeyOnline(ctx, p) pk, err := dht.GetPublicKey(ctx, p)
if err != nil { if err != nil {
return err return err
} }
......
...@@ -6,6 +6,7 @@ import ( ...@@ -6,6 +6,7 @@ import (
"time" "time"
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context" context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
ci "github.com/ipfs/go-ipfs/p2p/crypto"
peer "github.com/ipfs/go-ipfs/p2p/peer" peer "github.com/ipfs/go-ipfs/p2p/peer"
u "github.com/ipfs/go-ipfs/util" u "github.com/ipfs/go-ipfs/util"
) )
...@@ -46,3 +47,29 @@ type IpfsRouting interface { ...@@ -46,3 +47,29 @@ type IpfsRouting interface {
// TODO expose io.Closer or plain-old Close error // TODO expose io.Closer or plain-old Close error
} }
type PubKeyFetcher interface {
GetPublicKey(context.Context, peer.ID) (ci.PubKey, error)
}
// KeyForPublicKey returns the key used to retrieve public keys
// from the dht.
func KeyForPublicKey(id peer.ID) u.Key {
return u.Key("/pk/" + string(id))
}
func GetPublicKey(r IpfsRouting, ctx context.Context, pkhash []byte) (ci.PubKey, error) {
if dht, ok := r.(PubKeyFetcher); ok {
// If we have a DHT as our routing system, use optimized fetcher
return dht.GetPublicKey(ctx, peer.ID(pkhash))
} else {
key := u.Key("/pk/" + string(pkhash))
pkval, err := r.GetValue(ctx, key)
if err != nil {
return nil, err
}
// get PublicKey from node.Data
return ci.UnmarshalPublicKey(pkval)
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论