fix routing resolver

This commit is contained in:
Jeromy 2014-11-11 16:28:20 -08:00 committed by Juan Batiz-Benet
parent 1cf7a1824a
commit 01451c214e
5 changed files with 55 additions and 7 deletions

View File

@ -33,34 +33,40 @@ func (p *ipnsPublisher) Publish(k ci.PrivKey, value string) error {
// validate `value` is a ref (multihash)
_, err := mh.FromB58String(value)
if err != nil {
log.Errorf("hash cast failed: %s", value)
return fmt.Errorf("publish value must be str multihash. %v", err)
}
ctx := context.TODO()
data, err := createRoutingEntryData(k, value)
if err != nil {
log.Error("entry creation failed.")
return err
}
pubkey := k.GetPublic()
pkbytes, err := pubkey.Bytes()
if err != nil {
return nil
log.Error("pubkey getbytes failed.")
return err
}
nameb := u.Hash(pkbytes)
namekey := u.Key(nameb).Pretty()
ipnskey := []byte("/ipns/" + namekey)
namekey := u.Key("/pk/" + string(nameb))
log.Debugf("Storing pubkey at: %s", namekey)
// Store associated public key
timectx, _ := context.WithDeadline(ctx, time.Now().Add(time.Second*4))
err = p.routing.PutValue(timectx, u.Key("/pk/"+string(nameb)), pkbytes)
err = p.routing.PutValue(timectx, namekey, pkbytes)
if err != nil {
return err
}
ipnskey := u.Key("/ipns/" + string(nameb))
log.Debugf("Storing ipns entry at: %s", ipnskey)
// Store ipns entry at "/ipns/"+b58(h(pubkey))
timectx, _ = context.WithDeadline(ctx, time.Now().Add(time.Second*4))
err = p.routing.PutValue(timectx, u.Key(ipnskey), data)
err = p.routing.PutValue(timectx, ipnskey, data)
if err != nil {
return err
}

View File

@ -46,7 +46,7 @@ func (r *routingResolver) Resolve(name string) (string, error) {
// use the routing system to get the name.
// /ipns/<name>
h := []byte("/ipns/" + name)
h := []byte("/ipns/" + string(hash))
ipnsKey := u.Key(h)
val, err := r.routing.GetValue(ctx, ipnsKey)

View File

@ -355,10 +355,12 @@ func (dht *IpfsDHT) getFromPeerList(ctx context.Context, key u.Key,
func (dht *IpfsDHT) getLocal(key u.Key) ([]byte, error) {
dht.dslock.Lock()
defer dht.dslock.Unlock()
log.Debug("getLocal %s", key)
v, err := dht.datastore.Get(key.DsKey())
if err != nil {
return nil, err
}
log.Debug("found in db")
byt, ok := v.([]byte)
if !ok {
@ -374,6 +376,7 @@ func (dht *IpfsDHT) getLocal(key u.Key) ([]byte, error) {
if u.Debug {
err = dht.verifyRecord(rec)
if err != nil {
log.Errorf("local record verify failed: %s", err)
return nil, err
}
}

View File

@ -4,8 +4,11 @@ import (
"bytes"
"errors"
"strings"
"time"
"code.google.com/p/go.net/context"
"code.google.com/p/goprotobuf/proto"
ci "github.com/jbenet/go-ipfs/crypto"
"github.com/jbenet/go-ipfs/peer"
pb "github.com/jbenet/go-ipfs/routing/dht/pb"
u "github.com/jbenet/go-ipfs/util"
@ -32,6 +35,29 @@ func (dht *IpfsDHT) makePutRecord(key u.Key, value []byte) (*pb.Record, error) {
return record, nil
}
func (dht *IpfsDHT) getPublicKey(pid peer.ID) (ci.PubKey, error) {
log.Debug("getPublicKey for: %s", pid)
p, err := dht.peerstore.Get(pid)
if err == nil {
return p.PubKey(), nil
}
log.Debug("not in peerstore, searching dht.")
ctxT, _ := context.WithTimeout(dht.ContextCloser.Context(), time.Second*5)
val, err := dht.GetValue(ctxT, u.Key("/pk/"+string(pid)))
if err != nil {
log.Warning("Failed to find requested public key.")
return nil, err
}
pubkey, err := ci.UnmarshalPublicKey(val)
if err != nil {
log.Errorf("Failed to unmarshal public key: %s", err)
return nil, err
}
return pubkey, nil
}
func (dht *IpfsDHT) verifyRecord(r *pb.Record) error {
// First, validate the signature
p, err := dht.peerstore.Get(peer.ID(r.GetAuthor()))
@ -76,6 +102,14 @@ func ValidateIpnsRecord(k u.Key, val []byte) error {
}
func ValidatePublicKeyRecord(k u.Key, val []byte) error {
// TODO:
keyparts := bytes.Split([]byte(k), []byte("/"))
if len(keyparts) < 3 {
return errors.New("invalid key")
}
pkh := u.Hash(val)
if !bytes.Equal(keyparts[2], pkh) {
return errors.New("public key does not match storage key")
}
return nil
}

View File

@ -12,6 +12,8 @@ import (
u "github.com/jbenet/go-ipfs/util"
)
var log = u.Logger("mockrouter")
var _ routing.IpfsRouting = &MockRouter{}
type MockRouter struct {
@ -33,10 +35,12 @@ func (mr *MockRouter) SetRoutingServer(rs RoutingServer) {
}
func (mr *MockRouter) PutValue(ctx context.Context, key u.Key, val []byte) error {
log.Debugf("PutValue: %s", key)
return mr.datastore.Put(key.DsKey(), val)
}
func (mr *MockRouter) GetValue(ctx context.Context, key u.Key) ([]byte, error) {
log.Debugf("GetValue: %s", key)
v, err := mr.datastore.Get(key.DsKey())
if err != nil {
return nil, err
@ -55,6 +59,7 @@ func (mr *MockRouter) FindProviders(ctx context.Context, key u.Key) ([]peer.Peer
}
func (mr *MockRouter) FindPeer(ctx context.Context, pid peer.ID) (peer.Peer, error) {
log.Debug("FindPeer: %s", pid)
return nil, nil
}