Skip to content

Commit

Permalink
fix(service/share): make GetSharesByNamespace deterministic
Browse files Browse the repository at this point in the history
This change ensures that shares are always returned in same/correct order.
  • Loading branch information
tzdybal committed Apr 14, 2022
1 parent f611dd6 commit 324b4e2
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions service/share/share.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,37 +161,37 @@ func (s *service) GetSharesByNamespace(ctx context.Context, root *Root, nID name
return nil, ipld.ErrNotFoundInRange
}

type res struct {
nodes []format.Node
err error
}
resultCh := make(chan *res)

ctx, cancel := context.WithCancel(ctx)
defer cancel()

for _, rootCID := range rowRootCIDs {
go func(rootCID cid.Cid) {
nodes, err := ipld.GetLeavesByNamespace(ctx, s.dag, rootCID, nID)
resultCh <- &res{nodes: nodes, err: err}
}(rootCID)
errCh := make(chan error)
nodes := make([][]format.Node, len(rowRootCIDs))
for i, rootCID := range rowRootCIDs {
go func(i int, rootCID cid.Cid) {
var err error
nodes[i], err = ipld.GetLeavesByNamespace(ctx, s.dag, rootCID, nID)
errCh <- err
}(i, rootCID)
}

namespacedShares := make([]Share, 0)
for i := 0; i < len(rowRootCIDs); i++ {
select {
case result := <-resultCh:
if result.err != nil {
return nil, result.err
}
for _, node := range result.nodes {
namespacedShares = append(namespacedShares, node.RawData()[1:])
case err := <-errCh:
if err != nil {
return nil, err
}
case <-ctx.Done():
return nil, ctx.Err()
}
}

namespacedShares := make([]Share, 0)
for i := 0; i < len(rowRootCIDs); i++ {
for _, node := range nodes[i] {
namespacedShares = append(namespacedShares, node.RawData()[1:])
}
}

return namespacedShares, nil
}

Expand Down

0 comments on commit 324b4e2

Please sign in to comment.