Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Access] Use local event for event streaming API #4853

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/access/node_builder/access_node_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,7 @@ func (builder *FlowAccessNodeBuilder) BuildExecutionSyncComponents() *FlowAccess
builder.stateStreamConf,
node.State,
node.Storage.Headers,
node.Storage.Events,
node.Storage.Seals,
node.Storage.Results,
builder.ExecutionDataStore,
Expand Down
3 changes: 3 additions & 0 deletions engine/access/integration_unsecure_grpc_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ type SameGRPCPortTestSuite struct {
// storage
blocks *storagemock.Blocks
headers *storagemock.Headers
events *storagemock.Events
collections *storagemock.Collections
transactions *storagemock.Transactions
receipts *storagemock.ExecutionReceipts
Expand Down Expand Up @@ -101,6 +102,7 @@ func (suite *SameGRPCPortTestSuite) SetupTest() {
suite.snapshot.On("Epochs").Return(suite.epochQuery).Maybe()
suite.blocks = new(storagemock.Blocks)
suite.headers = new(storagemock.Headers)
suite.events = new(storagemock.Events)
suite.transactions = new(storagemock.Transactions)
suite.collections = new(storagemock.Collections)
suite.receipts = new(storagemock.ExecutionReceipts)
Expand Down Expand Up @@ -241,6 +243,7 @@ func (suite *SameGRPCPortTestSuite) SetupTest() {
conf,
suite.state,
suite.headers,
suite.events,
suite.seals,
suite.results,
nil,
Expand Down
3 changes: 3 additions & 0 deletions engine/access/state_stream/backend/backend.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package backend

Check failure on line 1 in engine/access/state_stream/backend/backend.go

View workflow job for this annotation

GitHub Actions / Lint (./)

: # github.com/onflow/flow-go/engine/access/state_stream/backend [github.com/onflow/flow-go/engine/access/state_stream/backend.test]

import (
"context"
Expand Down Expand Up @@ -90,6 +90,7 @@
config Config,
state protocol.State,
headers storage.Headers,
events storage.Events,
seals storage.Seals,
results storage.ExecutionResults,
execDataStore execution_data.ExecutionDataStore,
Expand Down Expand Up @@ -137,6 +138,8 @@
b.EventsBackend = EventsBackend{
log: logger,
broadcaster: broadcaster,
headers: headers,
events: events,
sendTimeout: config.ClientSendTimeout,
responseLimit: config.ResponseLimit,
sendBufferSize: int(config.ClientSendBufferSize),
Expand Down
44 changes: 42 additions & 2 deletions engine/access/state_stream/backend/backend_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/onflow/flow-go/engine"
"github.com/onflow/flow-go/engine/access/state_stream"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/storage"
"github.com/onflow/flow-go/utils/logging"
)

Expand All @@ -25,9 +26,13 @@ type EventsBackend struct {
sendTimeout time.Duration
responseLimit float64
sendBufferSize int
events storage.Events
headers storage.Headers

getExecutionData GetExecutionDataFunc
getStartHeight GetStartHeightFunc

useIndex bool
}

func (b EventsBackend) SubscribeEvents(ctx context.Context, startBlockID flow.Identifier, startHeight uint64, filter state_stream.EventFilter) state_stream.Subscription {
Expand All @@ -36,14 +41,21 @@ func (b EventsBackend) SubscribeEvents(ctx context.Context, startBlockID flow.Id
return NewFailedSubscription(err, "could not get start height")
}

sub := NewHeightBasedSubscription(b.sendBufferSize, nextHeight, b.getResponseFactory(filter))
var responseFactory GetDataByHeightFunc
if b.useIndex {
responseFactory = b.getStorageResponseFactory(filter)
} else {
responseFactory = b.getExecutionDataResponseFactory(filter)
}

sub := NewHeightBasedSubscription(b.sendBufferSize, nextHeight, responseFactory)

go NewStreamer(b.log, b.broadcaster, b.sendTimeout, b.responseLimit, sub).Stream(ctx)

return sub
}

func (b EventsBackend) getResponseFactory(filter state_stream.EventFilter) GetDataByHeightFunc {
func (b EventsBackend) getExecutionDataResponseFactory(filter state_stream.EventFilter) GetDataByHeightFunc {
return func(ctx context.Context, height uint64) (interface{}, error) {
executionData, err := b.getExecutionData(ctx, height)
if err != nil {
Expand All @@ -67,3 +79,31 @@ func (b EventsBackend) getResponseFactory(filter state_stream.EventFilter) GetDa
}, nil
}
}

func (b EventsBackend) getStorageResponseFactory(filter state_stream.EventFilter) GetDataByHeightFunc {
return func(ctx context.Context, height uint64) (interface{}, error) {
header, err := b.headers.ByHeight(height)
if err != nil {
return nil, fmt.Errorf("could not get header for height %d: %w", height, err)
}
blockID := header.ID()

events, err := b.events.ByBlockID(blockID)
if err != nil {
return nil, fmt.Errorf("could not get events for block %d: %w", height, err)
}

events = filter.Filter(events)

b.log.Trace().
Hex("block_id", logging.ID(blockID)).
Uint64("height", height).
Msgf("sending %d events", len(events))

return &EventsResponse{
BlockID: blockID,
Height: height,
Events: events,
}, nil
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
s.snapshot = protocolmock.NewSnapshot(s.T())
s.params = protocolmock.NewParams(s.T())
s.headers = storagemock.NewHeaders(s.T())
s.events = storagemock.NewEvents(s.T())

Check failure on line 78 in engine/access/state_stream/backend/backend_executiondata_test.go

View workflow job for this annotation

GitHub Actions / Lint (./)

s.events undefined (type *BackendExecutionDataSuite has no field or method events)

Check failure on line 78 in engine/access/state_stream/backend/backend_executiondata_test.go

View workflow job for this annotation

GitHub Actions / Lint (./)

s.events undefined (type *BackendExecutionDataSuite has no field or method events)

Check failure on line 78 in engine/access/state_stream/backend/backend_executiondata_test.go

View workflow job for this annotation

GitHub Actions / Unit Tests (engine/access)

s.events undefined (type *BackendExecutionDataSuite has no field or method events)

Check failure on line 78 in engine/access/state_stream/backend/backend_executiondata_test.go

View workflow job for this annotation

GitHub Actions / Unit Tests (engine/access)

s.events undefined (type *BackendExecutionDataSuite has no field or method events)

Check failure on line 78 in engine/access/state_stream/backend/backend_executiondata_test.go

View workflow job for this annotation

GitHub Actions / Unit Tests (engine/access)

s.events undefined (type *BackendExecutionDataSuite has no field or method events)

Check failure on line 78 in engine/access/state_stream/backend/backend_executiondata_test.go

View workflow job for this annotation

GitHub Actions / Unit Tests (engine/access)

s.events undefined (type *BackendExecutionDataSuite has no field or method events)

Check failure on line 78 in engine/access/state_stream/backend/backend_executiondata_test.go

View workflow job for this annotation

GitHub Actions / Unit Tests (engine/access)

s.events undefined (type *BackendExecutionDataSuite has no field or method events)
s.seals = storagemock.NewSeals(s.T())
s.results = storagemock.NewExecutionResults(s.T())

Expand Down Expand Up @@ -253,6 +254,7 @@
conf,
s.state,
s.headers,
s.events,

Check failure on line 257 in engine/access/state_stream/backend/backend_executiondata_test.go

View workflow job for this annotation

GitHub Actions / Lint (./)

s.events undefined (type *BackendExecutionDataSuite has no field or method events) (typecheck)

Check failure on line 257 in engine/access/state_stream/backend/backend_executiondata_test.go

View workflow job for this annotation

GitHub Actions / Lint (./)

s.events undefined (type *BackendExecutionDataSuite has no field or method events)) (typecheck)

Check failure on line 257 in engine/access/state_stream/backend/backend_executiondata_test.go

View workflow job for this annotation

GitHub Actions / Unit Tests (engine/access)

s.events undefined (type *BackendExecutionDataSuite has no field or method events)

Check failure on line 257 in engine/access/state_stream/backend/backend_executiondata_test.go

View workflow job for this annotation

GitHub Actions / Unit Tests (engine/access)

s.events undefined (type *BackendExecutionDataSuite has no field or method events)

Check failure on line 257 in engine/access/state_stream/backend/backend_executiondata_test.go

View workflow job for this annotation

GitHub Actions / Unit Tests (engine/access)

s.events undefined (type *BackendExecutionDataSuite has no field or method events)

Check failure on line 257 in engine/access/state_stream/backend/backend_executiondata_test.go

View workflow job for this annotation

GitHub Actions / Unit Tests (engine/access)

s.events undefined (type *BackendExecutionDataSuite has no field or method events)

Check failure on line 257 in engine/access/state_stream/backend/backend_executiondata_test.go

View workflow job for this annotation

GitHub Actions / Unit Tests (engine/access)

s.events undefined (type *BackendExecutionDataSuite has no field or method events)
s.seals,
s.results,
s.eds,
Expand Down
Loading