hotfix: duplicate blocks werent being counted properly, deduped key list before requesting
Jeromy committed
Feb 19, 2015 at 08:42 UTC
b04b23e675c800a65b8130551647f4f9a3719e6a
1 file changed
+22
-2
merkledag/merkledag.go
+22
-2
@@ -180,17 +180,24 @@ func (ds *dagService) GetDAG(ctx context.Context, root *Node) []NodeGetter {
180
// GetNodes returns an array of 'NodeGetter' promises, with each corresponding
181
// to the key with the same index as the passed in keys
182
func (ds *dagService) GetNodes(ctx context.Context, keys []u.Key) []NodeGetter {
183
+
184
+ // Early out if no work to do
185
+ if len(keys) == 0 {
186
+ return nil
187
+ }
188
+
189
promises := make([]NodeGetter, len(keys))
190
sendChans := make([]chan<- *Node, len(keys))
191
for i, _ := range keys {
192
promises[i], sendChans[i] = newNodePromise(ctx)
193
}
194
195
+ dedupedKeys := dedupeKeys(keys)
196
go func() {
197
ctx, cancel := context.WithCancel(ctx)
198
defer cancel()
199
193
- blkchan := ds.Blocks.GetBlocks(ctx, keys)
200
+ blkchan := ds.Blocks.GetBlocks(ctx, dedupedKeys)
201
202
for count := 0; count < len(keys); {
203
select {
@@ -207,8 +214,8 @@ func (ds *dagService) GetNodes(ctx context.Context, keys []u.Key) []NodeGetter {
214
}
215
is := FindLinks(keys, blk.Key(), 0)
216
for _, i := range is {
210
- sendChans[i] <- nd
217
count++
218
+ sendChans[i] <- nd
219
}
220
case <-ctx.Done():
221
return
@@ -218,6 +225,19 @@ func (ds *dagService) GetNodes(ctx context.Context, keys []u.Key) []NodeGetter {
225
return promises
226
}
227
228
+// Remove duplicates from a list of keys
229
+func dedupeKeys(ks []u.Key) []u.Key {
230
+ kmap := make(map[u.Key]struct{})
231
+ var out []u.Key
232
+ for _, k := range ks {
233
+ if _, ok := kmap[k]; !ok {
234
+ kmap[k] = struct{}{}
235
+ out = append(out, k)
236
+ }
237
+ }
238
+ return out
239
+}
240
+
241
func newNodePromise(ctx context.Context) (NodeGetter, chan<- *Node) {
242
ch := make(chan *Node, 1)
243
return &nodePromise{