return sentinel error for invalid records
License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>
Jeromy committed
Nov 20, 2015 at 11:12 UTC
51d031c115e7fd69e860940dc9681cfaaa7933fd
2 files changed
+20
-13
routing/dht/dht.go
+5
-3
@@ -150,6 +150,8 @@ func (dht *IpfsDHT) putProvider(ctx context.Context, p peer.ID, skey string) err
150
return nil
151
}
152
153
+var errInvalidRecord = errors.New("received invalid record")
154
+
155
// getValueOrPeers queries a particular peer p for the value for
156
// key. It returns either the value or a list of closer peers.
157
// 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,
175
err = dht.verifyRecordOnline(ctx, record)
176
if err != nil {
177
log.Info("Received invalid record! (discarded)")
176
- // still return a non-nil record to signify that we received
177
- // a bad record from this peer
178
+ // return a sentinal to signify an invalid record was received
179
+ err = errInvalidRecord
180
record = new(pb.Record)
181
}
180
- return record, peers, nil
182
+ return record, peers, err
183
}
184
185
if len(peers) > 0 {
routing/dht/routing.go
+15
-10
@@ -171,21 +171,26 @@ func (dht *IpfsDHT) GetValues(ctx context.Context, key key.Key, nvals int) ([]ro
171
})
172
173
rec, peers, err := dht.getValueOrPeers(ctx, p, key)
174
- if err != nil {
175
- if err == routing.ErrNotFound {
176
- // in this case, they responded with nothing,
177
- // still send a notification
178
- notif.PublishQueryEvent(parent, ¬if.QueryEvent{
179
- Type: notif.PeerResponse,
180
- ID: p,
181
- })
182
- }
174
+ switch err {
175
+ case routing.ErrNotFound:
176
+ // in this case, they responded with nothing,
177
+ // still send a notification so listeners can know the
178
+ // request has completed 'successfully'
179
+ notif.PublishQueryEvent(parent, ¬if.QueryEvent{
180
+ Type: notif.PeerResponse,
181
+ ID: p,
182
+ })
183
+ return nil, err
184
+ default:
185
return nil, err
186
+
187
+ case nil, errInvalidRecord:
188
+ // in either of these cases, we want to keep going
189
}
190
191
res := &dhtQueryResult{closerPeers: peers}
192
188
- if rec.GetValue() != nil {
193
+ if rec.GetValue() != nil || err == errInvalidRecord {
194
rv := routing.RecvdVal{
195
Val: rec.GetValue(),
196
From: p,