mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-30 18:13:54 +08:00
return sentinel error for invalid records
License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>
This commit is contained in:
@ -150,6 +150,8 @@ func (dht *IpfsDHT) putProvider(ctx context.Context, p peer.ID, skey string) err
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var errInvalidRecord = errors.New("received invalid record")
|
||||||
|
|
||||||
// getValueOrPeers queries a particular peer p for the value for
|
// getValueOrPeers queries a particular peer p for the value for
|
||||||
// key. It returns either the value or a list of closer peers.
|
// key. It returns either the value or a list of closer peers.
|
||||||
// NOTE: it will update the dht's peerstore with any new addresses
|
// NOTE: it will update the dht's peerstore with any new addresses
|
||||||
@ -173,11 +175,11 @@ func (dht *IpfsDHT) getValueOrPeers(ctx context.Context, p peer.ID,
|
|||||||
err = dht.verifyRecordOnline(ctx, record)
|
err = dht.verifyRecordOnline(ctx, record)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Info("Received invalid record! (discarded)")
|
log.Info("Received invalid record! (discarded)")
|
||||||
// still return a non-nil record to signify that we received
|
// return a sentinal to signify an invalid record was received
|
||||||
// a bad record from this peer
|
err = errInvalidRecord
|
||||||
record = new(pb.Record)
|
record = new(pb.Record)
|
||||||
}
|
}
|
||||||
return record, peers, nil
|
return record, peers, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(peers) > 0 {
|
if len(peers) > 0 {
|
||||||
|
@ -171,21 +171,26 @@ func (dht *IpfsDHT) GetValues(ctx context.Context, key key.Key, nvals int) ([]ro
|
|||||||
})
|
})
|
||||||
|
|
||||||
rec, peers, err := dht.getValueOrPeers(ctx, p, key)
|
rec, peers, err := dht.getValueOrPeers(ctx, p, key)
|
||||||
if err != nil {
|
switch err {
|
||||||
if err == routing.ErrNotFound {
|
case routing.ErrNotFound:
|
||||||
// in this case, they responded with nothing,
|
// in this case, they responded with nothing,
|
||||||
// still send a notification
|
// still send a notification so listeners can know the
|
||||||
notif.PublishQueryEvent(parent, ¬if.QueryEvent{
|
// request has completed 'successfully'
|
||||||
Type: notif.PeerResponse,
|
notif.PublishQueryEvent(parent, ¬if.QueryEvent{
|
||||||
ID: p,
|
Type: notif.PeerResponse,
|
||||||
})
|
ID: p,
|
||||||
}
|
})
|
||||||
return nil, err
|
return nil, err
|
||||||
|
default:
|
||||||
|
return nil, err
|
||||||
|
|
||||||
|
case nil, errInvalidRecord:
|
||||||
|
// in either of these cases, we want to keep going
|
||||||
}
|
}
|
||||||
|
|
||||||
res := &dhtQueryResult{closerPeers: peers}
|
res := &dhtQueryResult{closerPeers: peers}
|
||||||
|
|
||||||
if rec.GetValue() != nil {
|
if rec.GetValue() != nil || err == errInvalidRecord {
|
||||||
rv := routing.RecvdVal{
|
rv := routing.RecvdVal{
|
||||||
Val: rec.GetValue(),
|
Val: rec.GetValue(),
|
||||||
From: p,
|
From: p,
|
||||||
|
Reference in New Issue
Block a user