Commit 39907a76 authored by Steven Allen's avatar Steven Allen

reuse getRecordFromDatastore in getValue

Also, double check the key (should be impossible but the check is cheep).
parent 8dd81748
...@@ -228,29 +228,17 @@ func (dht *IpfsDHT) getValueSingle(ctx context.Context, p peer.ID, key string) ( ...@@ -228,29 +228,17 @@ func (dht *IpfsDHT) getValueSingle(ctx context.Context, p peer.ID, key string) (
// getLocal attempts to retrieve the value from the datastore // getLocal attempts to retrieve the value from the datastore
func (dht *IpfsDHT) getLocal(key string) (*recpb.Record, error) { func (dht *IpfsDHT) getLocal(key string) (*recpb.Record, error) {
log.Debugf("getLocal %s", key) log.Debugf("getLocal %s", key)
rec, err := dht.getRecordFromDatastore(mkDsKey(key))
v, err := dht.datastore.Get(mkDsKey(key))
if err != nil { if err != nil {
return nil, err return nil, err
} }
log.Debugf("found %s in local datastore")
byt, ok := v.([]byte) // Double check the key. Can't hurt.
if !ok { if rec != nil && rec.GetKey() != key {
return nil, errors.New("value stored in datastore not []byte") log.Errorf("BUG: found a DHT record that didn't match it's key: %s != %s", rec.GetKey(), key)
} return nil, routing.ErrNotFound
rec := new(recpb.Record)
err = proto.Unmarshal(byt, rec)
if err != nil {
return nil, err
}
err = dht.Validator.Validate(rec.GetKey(), rec.GetValue())
if err != nil {
log.Debugf("local record verify failed: %s (discarded)", err)
return nil, err
} }
return rec, nil return rec, nil
} }
......
...@@ -177,7 +177,11 @@ func (dht *IpfsDHT) GetValues(ctx context.Context, key string, nvals int) (_ []R ...@@ -177,7 +177,11 @@ func (dht *IpfsDHT) GetValues(ctx context.Context, key string, nvals int) (_ []R
// If we have it local, don't bother doing an RPC! // If we have it local, don't bother doing an RPC!
lrec, err := dht.getLocal(key) lrec, err := dht.getLocal(key)
if err == nil { if err != nil {
// something is wrong with the datastore.
return nil, err
}
if lrec != nil {
// TODO: this is tricky, we don't always want to trust our own value // TODO: this is tricky, we don't always want to trust our own value
// what if the authoritative source updated it? // what if the authoritative source updated it?
log.Debug("have it locally") log.Debug("have it locally")
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment