Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(pb): change gen path and use message key as bytes instead of string #184

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func (dht *IpfsDHT) getValueOrPeers(ctx context.Context, p peer.ID, key string)
log.Debug("getValueOrPeers: got value")

// make sure record is valid.
err = dht.Validator.Validate(record.GetKey(), record.GetValue())
err = dht.Validator.Validate(string(record.GetKey()), record.GetValue())
if err != nil {
log.Info("Received invalid record! (discarded)")
// return a sentinal to signify an invalid record was received
Expand Down Expand Up @@ -236,10 +236,9 @@ func (dht *IpfsDHT) getLocal(key string) (*recpb.Record, error) {
}

// Double check the key. Can't hurt.
if rec != nil && rec.GetKey() != key {
log.Errorf("BUG getLocal: found a DHT record that didn't match it's key: %s != %s", rec.GetKey(), key)
if rec != nil && string(rec.GetKey()) != key {
log.Errorf("BUG getLocal: found a DHT record that didn't match it's key: %s != %s", string(rec.GetKey()), key)
return nil, nil

}
return rec, nil
}
Expand Down Expand Up @@ -327,7 +326,7 @@ func (dht *IpfsDHT) findProvidersSingle(ctx context.Context, p peer.ID, key *cid

// nearestPeersToQuery returns the routing tables closest peers.
func (dht *IpfsDHT) nearestPeersToQuery(pmes *pb.Message, count int) []peer.ID {
closer := dht.routingTable.NearestPeers(kb.ConvertKey(pmes.GetKey()), count)
closer := dht.routingTable.NearestPeers(kb.ConvertKey(string(pmes.GetKey())), count)
return closer
}

Expand Down
24 changes: 12 additions & 12 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ func (dht *IpfsDHT) handleGetValue(ctx context.Context, p peer.ID, pmes *pb.Mess
}
eip.Done()
}()
log.Debugf("%s handleGetValue for key: %s", dht.self, pmes.GetKey())
log.Debugf("%s handleGetValue for key: %s", dht.self, string(pmes.GetKey()))

// setup response
resp := pb.NewMessage(pmes.GetType(), pmes.GetKey(), pmes.GetClusterLevel())
resp := pb.NewMessage(pmes.GetType(), string(pmes.GetKey()), pmes.GetClusterLevel())

// first, is there even a key?
k := pmes.GetKey()
k := string(pmes.GetKey())
if k == "" {
return nil, errors.New("handleGetValue but no key was provided")
// TODO: send back an error response? could be bad, but the other node's hanging.
Expand Down Expand Up @@ -170,19 +170,19 @@ func (dht *IpfsDHT) handlePutValue(ctx context.Context, p peer.ID, pmes *pb.Mess
return nil, errors.New("nil record")
}

if pmes.GetKey() != rec.GetKey() {
if string(pmes.GetKey()) != string(rec.GetKey()) {
return nil, errors.New("put key doesn't match record key")
}

cleanRecord(rec)

// Make sure the record is valid (not expired, valid signature etc)
if err = dht.Validator.Validate(rec.GetKey(), rec.GetValue()); err != nil {
if err = dht.Validator.Validate(string(rec.GetKey()), rec.GetValue()); err != nil {
log.Warningf("Bad dht record in PUT from: %s. %s", p.Pretty(), err)
return nil, err
}

dskey := convertToDsKey(rec.GetKey())
dskey := convertToDsKey(string(rec.GetKey()))

// Make sure the new record is "better" than the record we have locally.
// This prevents a record with for example a lower sequence number from
Expand All @@ -194,7 +194,7 @@ func (dht *IpfsDHT) handlePutValue(ctx context.Context, p peer.ID, pmes *pb.Mess

if existing != nil {
recs := [][]byte{rec.GetValue(), existing.GetValue()}
i, err := dht.Validator.Select(rec.GetKey(), recs)
i, err := dht.Validator.Select(string(rec.GetKey()), recs)
if err != nil {
log.Warningf("Bad dht record in PUT from %s: %s", p.Pretty(), err)
return nil, err
Expand Down Expand Up @@ -245,7 +245,7 @@ func (dht *IpfsDHT) getRecordFromDatastore(dskey ds.Key) (*recpb.Record, error)
return nil, nil
}

err = dht.Validator.Validate(rec.GetKey(), rec.GetValue())
err = dht.Validator.Validate(string(rec.GetKey()), rec.GetValue())
if err != nil {
// Invalid record in datastore, probably expired but don't return an error,
// we'll just overwrite it
Expand All @@ -267,7 +267,7 @@ func (dht *IpfsDHT) handleFindPeer(ctx context.Context, p peer.ID, pmes *pb.Mess
var closest []peer.ID

// if looking for self... special case where we send it on CloserPeers.
targetPid := peer.ID(pmes.GetKey())
targetPid := peer.ID(string(pmes.GetKey()))
if targetPid == dht.self {
closest = []peer.ID{dht.self}
} else {
Expand Down Expand Up @@ -316,8 +316,8 @@ func (dht *IpfsDHT) handleGetProviders(ctx context.Context, p peer.ID, pmes *pb.
eip := log.EventBegin(ctx, "handleGetProviders", lm)
defer eip.Done()

resp := pb.NewMessage(pmes.GetType(), pmes.GetKey(), pmes.GetClusterLevel())
c, err := cid.Cast([]byte(pmes.GetKey()))
resp := pb.NewMessage(pmes.GetType(), string(pmes.GetKey()), pmes.GetClusterLevel())
c, err := cid.Cast(pmes.GetKey())
if err != nil {
eip.SetError(err)
return nil, err
Expand Down Expand Up @@ -367,7 +367,7 @@ func (dht *IpfsDHT) handleAddProvider(ctx context.Context, p peer.ID, pmes *pb.M
eip := log.EventBegin(ctx, "handleAddProvider", lm)
defer eip.Done()

c, err := cid.Cast([]byte(pmes.GetKey()))
c, err := cid.Cast(pmes.GetKey())
if err != nil {
eip.SetError(err)
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion pb/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ GO = $(PB:.proto=.pb.go)
all: $(GO)

%.pb.go: %.proto
protoc --gogo_out=. --proto_path=../../../../../../:/usr/local/opt/protobuf/include:. $<
protoc --gogo_out=. --proto_path=$(GOPATH)/src:/usr/local/opt/protobuf/include:. $<

clean:
rm -f *.pb.go
Expand Down
77 changes: 59 additions & 18 deletions pb/dht.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions pb/dht.proto
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
syntax = "proto2";
package dht.pb;

import "record.proto";
import "github.com/libp2p/go-libp2p-record/pb/record.proto";

message Message {
enum MessageType {
Expand Down Expand Up @@ -37,7 +37,7 @@ message Message {

message Peer {
// ID of a given peer.
optional string id = 1;
optional bytes id = 1;

// multiaddrs for a given peer
repeated bytes addrs = 2;
Expand All @@ -55,7 +55,7 @@ message Message {

// Used to specify the key associated with this message.
// PUT_VALUE, GET_VALUE, ADD_PROVIDER, GET_PROVIDERS
optional string key = 2;
optional bytes key = 2;

// Used to return a value
// PUT_VALUE, GET_VALUE
Expand Down
10 changes: 4 additions & 6 deletions pb/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type PeerRoutingInfo struct {
func NewMessage(typ Message_MessageType, key string, level int) *Message {
m := &Message{
Type: &typ,
Key: &key,
Key: []byte(key),
}
m.SetClusterLevel(level)
return m
Expand All @@ -33,8 +33,7 @@ func peerRoutingInfoToPBPeer(p PeerRoutingInfo) *Message_Peer {
for i, maddr := range p.Addrs {
pbp.Addrs[i] = maddr.Bytes() // Bytes, not String. Compressed.
}
s := string(p.ID)
pbp.Id = &s
pbp.Id = []byte(p.ID)
c := ConnectionType(p.Connectedness)
pbp.Connection = &c
return pbp
Expand All @@ -47,8 +46,7 @@ func peerInfoToPBPeer(p pstore.PeerInfo) *Message_Peer {
for i, maddr := range p.Addrs {
pbp.Addrs[i] = maddr.Bytes() // Bytes, not String. Compressed.
}
s := string(p.ID)
pbp.Id = &s
pbp.Id = []byte(p.ID)
return pbp
}

Expand Down Expand Up @@ -144,7 +142,7 @@ func (m *Message) Loggable() map[string]interface{} {
return map[string]interface{}{
"message": map[string]string{
"type": m.Type.String(),
"key": b58.Encode([]byte(m.GetKey())),
"key": b58.Encode(m.GetKey()),
},
}
}
Expand Down