forked from onflow/flow-go
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request onflow#5444 from onflow/petera/backport-fix-event-…
…tx-order [Access] Sort events returned from index in tx index order - backport
- Loading branch information
Showing
5 changed files
with
164 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package backend | ||
|
||
import ( | ||
"bytes" | ||
"math" | ||
"sort" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/onflow/flow-go/model/flow" | ||
storagemock "github.com/onflow/flow-go/storage/mock" | ||
"github.com/onflow/flow-go/utils/unittest" | ||
) | ||
|
||
// TestGetEvents tests that GetEvents returns the events in the correct order | ||
func TestGetEvents(t *testing.T) { | ||
expectedEvents := make(flow.EventsList, 0, 6) | ||
expectedEvents = append(expectedEvents, generateTxEvents(unittest.IdentifierFixture(), 0, 1)...) | ||
expectedEvents = append(expectedEvents, generateTxEvents(unittest.IdentifierFixture(), 1, 3)...) | ||
expectedEvents = append(expectedEvents, generateTxEvents(unittest.IdentifierFixture(), 2, 2)...) | ||
|
||
storedEvents := make([]flow.Event, len(expectedEvents)) | ||
copy(storedEvents, expectedEvents) | ||
|
||
// sort events in storage order (by tx ID) | ||
sort.Slice(storedEvents, func(i, j int) bool { | ||
cmp := bytes.Compare(storedEvents[i].TransactionID[:], storedEvents[j].TransactionID[:]) | ||
if cmp == 0 { | ||
if storedEvents[i].TransactionIndex == storedEvents[j].TransactionIndex { | ||
return storedEvents[i].EventIndex < storedEvents[j].EventIndex | ||
} | ||
return storedEvents[i].TransactionIndex < storedEvents[j].TransactionIndex | ||
} | ||
return cmp < 0 | ||
}) | ||
|
||
events := storagemock.NewEvents(t) | ||
header := unittest.BlockHeaderFixture() | ||
|
||
events.On("ByBlockID", mock.Anything).Return(func(blockID flow.Identifier) ([]flow.Event, error) { | ||
return storedEvents, nil | ||
}) | ||
|
||
eventsIndex := NewEventsIndex(events) | ||
err := eventsIndex.Initialize(&mockIndexReporter{}) | ||
require.NoError(t, err) | ||
|
||
actualEvents, err := eventsIndex.GetEvents(header.ID(), header.Height) | ||
require.NoError(t, err) | ||
|
||
// output events should be in the same order as the expected events | ||
assert.Len(t, actualEvents, len(expectedEvents)) | ||
for i, event := range actualEvents { | ||
assert.Equal(t, expectedEvents[i], event) | ||
} | ||
} | ||
|
||
func generateTxEvents(txID flow.Identifier, txIndex uint32, count int) flow.EventsList { | ||
events := make(flow.EventsList, count) | ||
for i := 0; i < count; i++ { | ||
events[i] = flow.Event{ | ||
Type: unittest.EventTypeFixture(flow.Localnet), | ||
TransactionID: txID, | ||
TransactionIndex: txIndex, | ||
EventIndex: uint32(i), | ||
} | ||
} | ||
return events | ||
} | ||
|
||
type mockIndexReporter struct{} | ||
|
||
func (r *mockIndexReporter) LowestIndexedHeight() (uint64, error) { | ||
return 0, nil | ||
} | ||
|
||
func (r *mockIndexReporter) HighestIndexedHeight() (uint64, error) { | ||
return math.MaxUint64, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters