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) (
// getLocal attempts to retrieve the value from the datastore
func (dht *IpfsDHT) getLocal(key string) (*recpb.Record, error) {
log.Debugf("getLocal %s", key)
v, err := dht.datastore.Get(mkDsKey(key))
rec, err := dht.getRecordFromDatastore(mkDsKey(key))
if err != nil {
return nil, err
}
log.Debugf("found %s in local datastore")
byt, ok := v.([]byte)
if !ok {
return nil, errors.New("value stored in datastore not []byte")
}
rec := new(recpb.Record)
err = proto.Unmarshal(byt, rec)
if err != nil {
return nil, err
}
// Double check the key. Can't hurt.
if rec != nil && rec.GetKey() != key {
log.Errorf("BUG: found a DHT record that didn't match it's key: %s != %s", rec.GetKey(), key)
return nil, routing.ErrNotFound
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
}
......
......@@ -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!
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
// what if the authoritative source updated it?
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