Skip to content

Commit

Permalink
update BlockResult tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanschalm committed Dec 2, 2024
1 parent defcdea commit 405626e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 41 deletions.
23 changes: 5 additions & 18 deletions engine/execution/block_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,10 @@ func (er *BlockExecutionResult) AllEvents() flow.EventsList {
return res
}

// ServiceEventIndicesForChunk returns the list of service event indices associated with the given chunk.
// Outputs are index ranges with no gaps, and index into the flow.ExecutionResult.ServiceEvents field.
func (er *BlockExecutionResult) ServiceEventIndicesForChunk(chunkIndex int) []uint32 {
nServiceEventsForChunk := len(er.collectionExecutionResults[chunkIndex].serviceEvents)
if nServiceEventsForChunk == 0 {
return []uint32{}
}

firstIndex := 0
for i := 0; i < chunkIndex; i++ {
firstIndex += len(er.collectionExecutionResults[i].serviceEvents)
}
indices := make([]uint32, 0, nServiceEventsForChunk)
for i := firstIndex; i < firstIndex+nServiceEventsForChunk; i++ {
indices = append(indices, uint32(i))
}
return indices
// ServiceEventCountForChunk returns the number of service events emitted in the given chunk.
func (er *BlockExecutionResult) ServiceEventCountForChunk(chunkIndex int) uint16 {
serviceEventCount := len(er.collectionExecutionResults[chunkIndex].serviceEvents)
return uint16(serviceEventCount)
}

func (er *BlockExecutionResult) AllServiceEvents() flow.EventsList {
Expand Down Expand Up @@ -218,7 +205,7 @@ func (ar *BlockAttestationResult) ChunkAt(index int) *flow.Chunk {
attestRes.startStateCommit,
len(execRes.TransactionResults()),
attestRes.eventCommit,
ar.ServiceEventIndicesForChunk(index),
ar.ServiceEventCountForChunk(index),
attestRes.endStateCommit,
execRes.executionSnapshot.TotalComputationUsed(),
)
Expand Down
41 changes: 18 additions & 23 deletions engine/execution/block_result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ func makeBlockExecutionResultFixture(serviceEventsPerChunk []int) *BlockExecutio
return fixture
}

// Tests that ServiceEventIndicesForChunk method works as expected under various circumstances:
// Tests that ServiceEventCountForChunk method works as expected under various circumstances:
func TestBlockExecutionResult_ServiceEventIndicesForChunk(t *testing.T) {
t.Run("no service events", func(t *testing.T) {
nChunks := rand.Intn(10) + 1
blockResult := makeBlockExecutionResultFixture(make([]int, nChunks))
// all chunks should yield an empty list of service event indices
// all chunks should have 0 service event count
for chunkIndex := 0; chunkIndex < nChunks; chunkIndex++ {
indices := blockResult.ServiceEventIndicesForChunk(chunkIndex)
assert.Equal(t, make([]uint32, 0), indices)
count := blockResult.ServiceEventCountForChunk(chunkIndex)
assert.Equal(t, uint16(0), count)
}
})
t.Run("service events only in system chunk", func(t *testing.T) {
Expand All @@ -43,14 +43,13 @@ func TestBlockExecutionResult_ServiceEventIndicesForChunk(t *testing.T) {
serviceEventAllocation[nChunks-1] = nServiceEvents

blockResult := makeBlockExecutionResultFixture(serviceEventAllocation)
// all non-system chunks should yield an empty list of service event indices
// all non-system chunks should have zero service event count
for chunkIndex := 0; chunkIndex < nChunks-1; chunkIndex++ {
indices := blockResult.ServiceEventIndicesForChunk(chunkIndex)
assert.Equal(t, make([]uint32, 0), indices)
count := blockResult.ServiceEventCountForChunk(chunkIndex)
assert.Equal(t, uint16(0), count)
}
// the system chunk should contain indices for all events
expected := slices.MakeRange[uint32](0, uint32(nServiceEvents))
assert.Equal(t, expected, blockResult.ServiceEventIndicesForChunk(nChunks-1))
// the system chunk should contain all service events
assert.Equal(t, uint16(nServiceEvents), blockResult.ServiceEventCountForChunk(nChunks-1))
})
t.Run("service events only outside system chunk", func(t *testing.T) {
nChunks := rand.Intn(10) + 2 // at least 2 chunks
Expand All @@ -59,28 +58,24 @@ func TestBlockExecutionResult_ServiceEventIndicesForChunk(t *testing.T) {
serviceEventAllocation[nChunks-1] = 0

blockResult := makeBlockExecutionResultFixture(serviceEventAllocation)
// all non-system chunks should yield a length-1 list of service event indices
// all non-system chunks should have 1 service event
for chunkIndex := 0; chunkIndex < nChunks-1; chunkIndex++ {
indices := blockResult.ServiceEventIndicesForChunk(chunkIndex)
// 1 service event per chunk => service event indices match chunk indices
expected := slices.Fill(uint32(chunkIndex), 1)
assert.Equal(t, expected, indices)
count := blockResult.ServiceEventCountForChunk(chunkIndex)
assert.Equal(t, uint16(1), count)
}
// the system chunk should contain indices for all events
assert.Equal(t, make([]uint32, 0), blockResult.ServiceEventIndicesForChunk(nChunks-1))
// the system chunk service event count should include all service events
assert.Equal(t, uint16(0), blockResult.ServiceEventCountForChunk(nChunks-1))
})
t.Run("service events in both system chunk and other chunks", func(t *testing.T) {
nChunks := rand.Intn(10) + 2 // at least 2 chunks
// add 1 service event to all chunks (including system chunk
// add 1 service event to all chunks (including system chunk)
serviceEventAllocation := slices.Fill(1, nChunks)

blockResult := makeBlockExecutionResultFixture(serviceEventAllocation)
// all chunks should yield a length-1 list of service event indices
// all chunks should have service event count of 1
for chunkIndex := 0; chunkIndex < nChunks; chunkIndex++ {
indices := blockResult.ServiceEventIndicesForChunk(chunkIndex)
// 1 service event per chunk => service event indices match chunk indices
expected := slices.Fill(uint32(chunkIndex), 1)
assert.Equal(t, expected, indices)
count := blockResult.ServiceEventCountForChunk(chunkIndex)
assert.Equal(t, uint16(1), count)
}
})
}
1 change: 1 addition & 0 deletions model/flow/execution_result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func TestExecutionResultGroupBy(t *testing.T) {
assert.Equal(t, 0, unknown.Size())
}

// TODO
func TestExecutionResult_FingerprintBackwardCompatibility(t *testing.T) {}

// Tests that [ExecutionResult.ServiceEventsByChunk] method works in a variety of circumstances.
Expand Down

0 comments on commit 405626e

Please sign in to comment.