Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6fabee3

Browse files
committedNov 20, 2015
return sentinel error for invalid records
License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>
1 parent 81645b9 commit 6fabee3

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed
 

‎routing/dht/dht.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ func (dht *IpfsDHT) putProvider(ctx context.Context, p peer.ID, skey string) err
150150
return nil
151151
}
152152

153+
var errInvalidRecord = errors.New("received invalid record")
154+
153155
// getValueOrPeers queries a particular peer p for the value for
154156
// key. It returns either the value or a list of closer peers.
155157
// 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,
173175
err = dht.verifyRecordOnline(ctx, record)
174176
if err != nil {
175177
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
178180
record = new(pb.Record)
179181
}
180-
return record, peers, nil
182+
return record, peers, err
181183
}
182184

183185
if len(peers) > 0 {

‎routing/dht/routing.go

+15-10
Original file line numberDiff line numberDiff line change
@@ -171,21 +171,26 @@ func (dht *IpfsDHT) GetValues(ctx context.Context, key key.Key, nvals int) ([]ro
171171
})
172172

173173
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, &notif.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, &notif.QueryEvent{
180+
Type: notif.PeerResponse,
181+
ID: p,
182+
})
183+
return nil, err
184+
default:
183185
return nil, err
186+
187+
case nil, errInvalidRecord:
188+
// in either of these cases, we want to keep going
184189
}
185190

186191
res := &dhtQueryResult{closerPeers: peers}
187192

188-
if rec.GetValue() != nil {
193+
if rec.GetValue() != nil || err == errInvalidRecord {
189194
rv := routing.RecvdVal{
190195
Val: rec.GetValue(),
191196
From: p,

0 commit comments

Comments
 (0)
Please sign in to comment.