Skip to content

Commit

Permalink
fix routing resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
whyrusleeping authored and jbenet committed Nov 16, 2014
1 parent 1cf7a18 commit 01451c2
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 7 deletions.
16 changes: 11 additions & 5 deletions namesys/publisher.go
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion namesys/routing.go
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions routing/dht/dht.go
Expand Up @@ -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 {
Expand All @@ -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
}
}
Expand Down
36 changes: 35 additions & 1 deletion routing/dht/records.go
Expand Up @@ -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"
Expand All @@ -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()))
Expand Down Expand Up @@ -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
}
5 changes: 5 additions & 0 deletions routing/mock/routing.go
Expand Up @@ -12,6 +12,8 @@ import (
u "github.com/jbenet/go-ipfs/util"
)

var log = u.Logger("mockrouter")

var _ routing.IpfsRouting = &MockRouter{}

type MockRouter struct {
Expand All @@ -33,10 +35,12 @@ func (mr *MockRouter) SetRoutingServer(rs RoutingServer) {
}

func (mr *MockRouter) PutValue(ctx context.Context, key u.Key, val []byte) error {

This comment has been minimized.

Copy link
@btc

btc Dec 13, 2014

Contributor

@whyrusleeping
does this simulate Putting the value into the network?

log.Debugf("PutValue: %s", key)
return mr.datastore.Put(key.DsKey(), val)
}

func (mr *MockRouter) GetValue(ctx context.Context, key u.Key) ([]byte, error) {

This comment has been minimized.

Copy link
@btc

btc Dec 13, 2014

Contributor

@whyrusleeping Does this simulate getting the value from the network?

log.Debugf("GetValue: %s", key)
v, err := mr.datastore.Get(key.DsKey())
if err != nil {
return nil, err
Expand All @@ -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
}

Expand Down

0 comments on commit 01451c2

Please sign in to comment.