Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanschalm committed Nov 29, 2024
1 parent 13aba21 commit defcdea
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 27 deletions.
22 changes: 13 additions & 9 deletions model/flow/execution_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,22 @@ func (er ExecutionResult) SystemChunk() *Chunk {

// ServiceEventsByChunk returns the list of service events emitted during the given chunk.
func (er ExecutionResult) ServiceEventsByChunk(chunkIndex uint64) ServiceEventList {
indices := er.Chunks[chunkIndex].ServiceEventIndices
// CASE 1: Service event indices are specified (non-nil)
if indices != nil {
serviceEventsForChunk := make(ServiceEventList, 0, len(indices))
for _, eventIndex := range indices {
serviceEventsForChunk = append(serviceEventsForChunk, er.ServiceEvents[eventIndex])
serviceEventCount := er.Chunks[chunkIndex].ServiceEventCount
// CASE 1: Service event count is specified (non-nil)
if serviceEventCount != nil {
if *serviceEventCount == 0 {
return nil
}
return serviceEventsForChunk

startIndex := 0
for i := uint64(0); i < chunkIndex; i++ {
startIndex += int(*er.Chunks[i].ServiceEventCount)
}
return er.ServiceEvents[startIndex : startIndex+int(*serviceEventCount)]
}
// CASE 2: Service event indices are omitted (nil)
// CASE 2: Service event count omitted (nil)
// This indicates the chunk was generated in an older data model version.
// In this case, any service events associated with the result are assumed
// In this case, all service events associated with the result are assumed
// to have been emitted within the system chunk (last chunk)
if chunkIndex == er.SystemChunk().Index {
return er.ServiceEvents
Expand Down
37 changes: 19 additions & 18 deletions model/flow/execution_result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/stretchr/testify/assert"

"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/utils/slices"
"github.com/onflow/flow-go/utils/unittest"
)

Expand Down Expand Up @@ -44,26 +43,28 @@ func TestExecutionResultGroupBy(t *testing.T) {
assert.Equal(t, 0, unknown.Size())
}

func TestExecutionResult_FingerprintBackwardCompatibility(t *testing.T) {}

// Tests that [ExecutionResult.ServiceEventsByChunk] method works in a variety of circumstances.
// It also tests the method against an ExecutionResult instance backed by both the
// current and old data model version (with and with ServiceEventIndices field)
// current and old data model version (with and with ServiceEventCount field)
func TestExecutionResult_ServiceEventsByChunk(t *testing.T) {
t.Run("no service events", func(t *testing.T) {
t.Run("nil ServiceEventIndices field (old model)", func(t *testing.T) {
t.Run("nil ServiceEventCount field (old model)", func(t *testing.T) {
result := unittest.ExecutionResultFixture()
for _, chunk := range result.Chunks {
chunk.ServiceEventIndices = nil
chunk.ServiceEventCount = nil
}
// should return empty list for all chunks
for chunkIndex := 0; chunkIndex < result.Chunks.Len(); chunkIndex++ {
serviceEvents := result.ServiceEventsByChunk(uint64(chunkIndex))
assert.Len(t, serviceEvents, 0)
}
})
t.Run("populated ServiceEventIndices field", func(t *testing.T) {
t.Run("populated ServiceEventCount field", func(t *testing.T) {
result := unittest.ExecutionResultFixture()
for _, chunk := range result.Chunks {
chunk.ServiceEventIndices = make([]uint32, 0)
chunk.ServiceEventCount = unittest.PtrTo[uint16](0)
}
// should return empty list for all chunks
for chunkIndex := 0; chunkIndex < result.Chunks.Len(); chunkIndex++ {
Expand All @@ -74,10 +75,10 @@ func TestExecutionResult_ServiceEventsByChunk(t *testing.T) {
})

t.Run("service events only in system chunk", func(t *testing.T) {
t.Run("nil ServiceEventIndices field (old model)", func(t *testing.T) {
t.Run("nil ServiceEventCount field (old model)", func(t *testing.T) {
result := unittest.ExecutionResultFixture()
for _, chunk := range result.Chunks {
chunk.ServiceEventIndices = nil
chunk.ServiceEventCount = nil
}

// should return empty list for all chunks
Expand All @@ -86,13 +87,13 @@ func TestExecutionResult_ServiceEventsByChunk(t *testing.T) {
assert.Len(t, serviceEvents, 0)
}
})
t.Run("populated ServiceEventIndices field", func(t *testing.T) {
t.Run("populated ServiceEventCount field", func(t *testing.T) {
nServiceEvents := rand.Intn(10) + 1
result := unittest.ExecutionResultFixture(unittest.WithServiceEvents(nServiceEvents))
for _, chunk := range result.Chunks[:result.Chunks.Len()-1] {
chunk.ServiceEventIndices = make([]uint32, 0)
chunk.ServiceEventCount = unittest.PtrTo[uint16](0)
}
result.SystemChunk().ServiceEventIndices = slices.MakeRange(0, uint32(nServiceEvents))
result.SystemChunk().ServiceEventCount = unittest.PtrTo(uint16(nServiceEvents))

// should return empty list for all non-system chunks
for chunkIndex := 0; chunkIndex < result.Chunks.Len()-1; chunkIndex++ {
Expand All @@ -109,11 +110,11 @@ func TestExecutionResult_ServiceEventsByChunk(t *testing.T) {
result := unittest.ExecutionResultFixture()
unittest.WithServiceEvents(result.Chunks.Len() - 1)(result) // one service event per non-system chunk

for chunkIndex, chunk := range result.Chunks {
// 1 service event per chunk => service event indices match chunk indices
chunk.ServiceEventIndices = []uint32{uint32(chunkIndex)}
for _, chunk := range result.Chunks {
// 1 service event per chunk
chunk.ServiceEventCount = unittest.PtrTo(uint16(1))
}
result.SystemChunk().ServiceEventIndices = make([]uint32, 0) // none in system chunk
result.SystemChunk().ServiceEventCount = unittest.PtrTo(uint16(0))

// should return one service event per non-system chunk
for chunkIndex := 0; chunkIndex < result.Chunks.Len()-1; chunkIndex++ {
Expand All @@ -129,9 +130,9 @@ func TestExecutionResult_ServiceEventsByChunk(t *testing.T) {
result := unittest.ExecutionResultFixture()
unittest.WithServiceEvents(result.Chunks.Len())(result) // one service event per chunk

for chunkIndex, chunk := range result.Chunks {
// 1 service event per chunk => service event indices match chunk indices
chunk.ServiceEventIndices = []uint32{uint32(chunkIndex)}
for _, chunk := range result.Chunks {
// 1 service event per chunk
chunk.ServiceEventCount = unittest.PtrTo(uint16(1))
}

// should return one service event per chunk
Expand Down

0 comments on commit defcdea

Please sign in to comment.