Skip to content

Commit

Permalink
spike: fix for root node with only one link
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Cihelka committed Oct 9, 2023
1 parent 36729f8 commit fa83b78
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions pkg/spade-spike/spade-spike.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,13 +232,21 @@ func SpadeTraversal(ctx context.Context, startingCid goCid.Cid, p peer.AddrInfo,
nextIndex := 0
rand.Seed(time.Now().UnixNano())
if i == 0 {
if len(links) < 2 {
return false, errors.New("starting node contains less than 2 links, will not traverse any further")
// Special logic for when we are at the root node
if len(links) == 0 {
return false, fmt.Errorf("root node contains no links, will not traverse any further")

Check failure on line 237 in pkg/spade-spike/spade-spike.go

View workflow job for this annotation

GitHub Actions / build

err113: do not define dynamic errors, use wrapped static errors instead: "fmt.Errorf(\"root node contains no links, will not traverse any further\")" (goerr113)
}
if len(links) == 1 {
// Generally this should not happen as the root node should contain at least one AggregateManifest and other links
// however, there may be a different construction in the future where this is still valid, so we will attempt to retrieve

Check failure on line 241 in pkg/spade-spike/spade-spike.go

View workflow job for this annotation

GitHub Actions / build

line is 125 characters (lll)
logger.Debugf("starting node contains only 1 link - will still attempt retrieval test")
nextIndex = 0
} else {
// To be safe, never grab the first link off the root as it may refer to the AggregateManifest
nextIndex = 1 + rand.Intn(len(links)-1)

Check failure on line 246 in pkg/spade-spike/spade-spike.go

View workflow job for this annotation

GitHub Actions / build

G404: Use of weak random number generator (math/rand instead of crypto/rand) (gosec)
}

// from the starting node's children, never grab the first link as it refers to the AggregateManifest
nextIndex = 1 + rand.Intn(len(links)-1)
} else {
// Logic for all other non-root nodes
if len(links) < 1 {
return false, fmt.Errorf("node at depth %d contains no links, will not traverse any further", i)

Check failure on line 251 in pkg/spade-spike/spade-spike.go

View workflow job for this annotation

GitHub Actions / build

err113: do not define dynamic errors, use wrapped static errors instead: "fmt.Errorf(\"node at depth %d contains no links, will not traverse any further\", i)" (goerr113)
}
Expand Down

0 comments on commit fa83b78

Please sign in to comment.