Skip to content

Commit

Permalink
fix(indexer): Ineffective or missing break statements in kv package. …
Browse files Browse the repository at this point in the history
…(backport cometbft#3557) (cometbft#3564)

Closes cometbft#3544.

### Changes
- Adds missing `for` loop labels in the kv indexer 
- Previously ineffective `break` statements now point to their enclosing
`for` loop labels to exit upon reception on the `ctx.Done()` channel.

---

#### PR checklist

~- [ ] Tests written/updated~
- [x] Changelog entry added in `.changelog` (we use
[unclog](https://github.com/informalsystems/unclog) to manage our
changelog)
~- [ ] Updated relevant documentation (`docs/` or `spec/`) and code
comments~
- [x] Title follows the [Conventional
Commits](https://www.conventionalcommits.org/en/v1.0.0/) spec
<hr>This is an automatic backport of pull request cometbft#3557 done by
[Mergify](https://mergify.com).

---------

Co-authored-by: Alessandro <[email protected]>
  • Loading branch information
mergify[bot] and alesforz authored Jul 26, 2024
1 parent 8f36ed9 commit 50ddaa8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- `[indexer]` Fixed ineffective select break statements; they now
point to their enclosing for loop label to exit
([\#3544](https://github.com/cometbft/cometbft/issues/3544))
20 changes: 12 additions & 8 deletions state/indexer/block/kv/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ func (idx *BlockerIndexer) Search(ctx context.Context, q *query.Query) ([]int64,
// fetch matching heights
results = make([]int64, 0, len(filteredHeights))
resultMap := make(map[int64]struct{})

FOR_LOOP:
for _, hBz := range filteredHeights {
h := int64FromBytes(hBz)

Expand All @@ -238,7 +240,7 @@ func (idx *BlockerIndexer) Search(ctx context.Context, q *query.Query) ([]int64,

select {
case <-ctx.Done():
break
break FOR_LOOP

default:
}
Expand Down Expand Up @@ -314,8 +316,7 @@ LOOP:

select {
case <-ctx.Done():
break

break LOOP
default:
}
}
Expand All @@ -337,6 +338,7 @@ LOOP:

// Remove/reduce matches in filteredHashes that were not found in this
// match (tmpHashes).
FOR_LOOP:
for k, v := range filteredHeights {
tmpHeight := tmpHeights[k]

Expand All @@ -347,8 +349,7 @@ LOOP:

select {
case <-ctx.Done():
break

break FOR_LOOP
default:
}
}
Expand Down Expand Up @@ -442,6 +443,7 @@ func (idx *BlockerIndexer) match(
}
defer it.Close()

LOOP_EXISTS:
for ; it.Valid(); it.Next() {
keyHeight, err := parseHeightFromEventKey(it.Key())
if err != nil || !checkHeightConditions(heightInfo, keyHeight) {
Expand All @@ -451,7 +453,7 @@ func (idx *BlockerIndexer) match(

select {
case <-ctx.Done():
break
break LOOP_EXISTS

default:
}
Expand All @@ -473,6 +475,7 @@ func (idx *BlockerIndexer) match(
}
defer it.Close()

LOOP_CONTAINS:
for ; it.Valid(); it.Next() {
eventValue, err := parseValueFromEventKey(it.Key())
if err != nil {
Expand All @@ -489,7 +492,7 @@ func (idx *BlockerIndexer) match(

select {
case <-ctx.Done():
break
break LOOP_CONTAINS

default:
}
Expand All @@ -515,14 +518,15 @@ func (idx *BlockerIndexer) match(

// Remove/reduce matches in filteredHeights that were not found in this
// match (tmpHeights).
FOR_LOOP:
for k, v := range filteredHeights {
tmpHeight := tmpHeights[k]
if tmpHeight == nil || !bytes.Equal(tmpHeight, v) {
delete(filteredHeights, k)

select {
case <-ctx.Done():
break
break FOR_LOOP

default:
}
Expand Down

0 comments on commit 50ddaa8

Please sign in to comment.