Skip to content

Commit

Permalink
Merge pull request #5772 from onflow/leo/v0.33-check-events-hash-when…
Browse files Browse the repository at this point in the history
…-block-has-no-events

Backport check events hash when block has no events
  • Loading branch information
zhangchiqing authored Apr 25, 2024
2 parents c3ffacb + 2df36bc commit aa5ba4e
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 4 deletions.
41 changes: 38 additions & 3 deletions engine/execution/rpc/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,21 @@ func (h *handler) GetEventsForBlockIDs(
return nil, status.Errorf(codes.Internal, "state commitment for block ID %s could not be retrieved", bID)
}

// lookup events
blockEvents, err := h.events.ByBlockIDEventType(bID, flow.EventType(eType))
// lookup all events for the block
blockAllEvents, err := h.getEventsByBlockID(bID)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get events for block: %v", err)
}

// filter events by type
eventType := flow.EventType(eType)
blockEvents := make([]flow.Event, 0, len(blockAllEvents))
for _, event := range blockAllEvents {
if event.Type == eventType {
blockEvents = append(blockEvents, event)
}
}

result, err := h.eventResult(bID, blockEvents)
if err != nil {
return nil, err
Expand Down Expand Up @@ -442,7 +451,7 @@ func (h *handler) GetTransactionResultsByBlockID(
}

// get all events for a block
blockEvents, err := h.events.ByBlockID(blockID)
blockEvents, err := h.getEventsByBlockID(blockID)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get events for block: %v", err)
}
Expand Down Expand Up @@ -792,3 +801,29 @@ func (h *handler) blockHeaderResponse(header *flow.Header) (*execution.BlockHead
Block: msg,
}, nil
}

// additional check that when there is no event in the block, double check if the execution
// result has no events as well, otherwise return an error.
// we check the execution result has no event by checking if each chunk's EventCollection is
// the default hash for empty event collection.
func (h *handler) getEventsByBlockID(blockID flow.Identifier) ([]flow.Event, error) {
blockEvents, err := h.events.ByBlockID(blockID)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get events for block: %v", err)
}

if len(blockEvents) == 0 {
executionResult, err := h.exeResults.ByBlockID(blockID)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get execution result for block %v: %v", blockID, err)
}

for _, chunk := range executionResult.Chunks {
if chunk.EventCollection != flow.EmptyEventCollectionID {
return nil, status.Errorf(codes.Internal, "events not found for block %s, but chunk %d has events", blockID, chunk.Index)
}
}
}

return blockEvents, nil
}
2 changes: 1 addition & 1 deletion engine/execution/rpc/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func (suite *Suite) TestGetEventsForBlockIDs() {
suite.commits.On("ByBlockID", id).Return(nil, nil).Once()

// expect one call to lookup events for each block ID
suite.events.On("ByBlockIDEventType", id, flow.EventAccountCreated).Return(eventsForBlock, nil).Once()
suite.events.On("ByBlockID", id).Return(eventsForBlock, nil).Once()

// expect one call to lookup each block
suite.headers.On("ByBlockID", id).Return(block.Header, nil).Once()
Expand Down
14 changes: 14 additions & 0 deletions model/flow/chunk.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
package flow

import (
"log"

"github.com/ipfs/go-cid"
)

var EmptyEventCollectionID Identifier

func init() {
// Convert hexadecimal string to a byte slice.
var err error
emptyEventCollectionHex := "0e5751c026e543b2e8ab2eb06099daa1d1e5df47778f7787faab45cdf12fe3a8"
EmptyEventCollectionID, err = HexStringToIdentifier(emptyEventCollectionHex)
if err != nil {
log.Fatalf("Failed to decode hex: %v", err)
}
}

type ChunkBody struct {
CollectionIndex uint

Expand Down
10 changes: 10 additions & 0 deletions model/flow/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/onflow/flow-go/model/encoding/rlp"
"github.com/onflow/flow-go/model/fingerprint"
Expand Down Expand Up @@ -118,3 +119,12 @@ func TestEventsMerkleRootHash(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, expectedRootHashHex, ABHash.String())
}

// TestEmptyEventsMerkleRootHash validates that the `EmptyEventCollectionID` constant
// is equal to the hash of an empty events list. If this test fails, change the hex string constant
// populating `EmptyEventCollectionID` accordingly.
func TestEmptyEventsMerkleRootHash(t *testing.T) {
actualHash, err := flow.EventsMerkleRootHash([]flow.Event{})
require.NoError(t, err)
require.Equal(t, flow.EmptyEventCollectionID, actualHash)
}

0 comments on commit aa5ba4e

Please sign in to comment.