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

Enable service events for all collection context #5845

Merged
merged 8 commits into from
May 11, 2024
1 change: 0 additions & 1 deletion engine/execution/computation/computer/computer.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ func SystemChunkContext(vmCtx fvm.Context) fvm.Context {
fvm.WithAuthorizationChecksEnabled(false),
fvm.WithSequenceNumberCheckAndIncrementEnabled(false),
fvm.WithTransactionFeesEnabled(false),
fvm.WithServiceEventCollectionEnabled(),
fvm.WithEventCollectionSizeLimit(SystemChunkEventCollectionMaxSize),
fvm.WithMemoryAndInteractionLimitsDisabled(),
// only the system transaction is allowed to call the block entropy provider
Expand Down
1 change: 0 additions & 1 deletion engine/execution/computation/computer/computer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,6 @@ func TestBlockExecutor_ExecuteBlock(t *testing.T) {
t.Run(
"service events are emitted", func(t *testing.T) {
execCtx := fvm.NewContext(
fvm.WithServiceEventCollectionEnabled(),
fvm.WithAuthorizationChecksEnabled(false),
fvm.WithSequenceNumberCheckAndIncrementEnabled(false),
)
Expand Down
11 changes: 2 additions & 9 deletions fvm/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func newContext(ctx Context, opts ...Option) Context {
}

func defaultContext() Context {
return Context{
ctx := Context{
DisableMemoryAndInteractionLimits: false,
ComputationLimit: DefaultComputationLimit,
MemoryLimit: DefaultMemoryLimit,
Expand All @@ -77,6 +77,7 @@ func defaultContext() Context {
TransactionExecutorParams: DefaultTransactionExecutorParams(),
EnvironmentParams: environment.DefaultEnvironmentParams(),
}
return ctx
}

// An Option sets a configuration parameter for a virtual machine context.
Expand Down Expand Up @@ -185,14 +186,6 @@ func WithBlockHeader(header *flow.Header) Option {
}
}

// WithServiceEventCollectionEnabled enables service event collection
func WithServiceEventCollectionEnabled() Option {
return func(ctx Context) Context {
ctx.ServiceEventCollectionEnabled = true
return ctx
}
}

// WithBlocks sets the block storage provider for a virtual machine context.
//
// The VM uses the block storage provider to provide historical block information to
Expand Down
50 changes: 24 additions & 26 deletions fvm/environment/event_emitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,14 @@
)

type EventEmitterParams struct {
ServiceEventCollectionEnabled bool
EventCollectionByteSizeLimit uint64
EventEncoder EventEncoder
EventCollectionByteSizeLimit uint64
EventEncoder EventEncoder
}

func DefaultEventEmitterParams() EventEmitterParams {
return EventEmitterParams{
ServiceEventCollectionEnabled: false,
EventCollectionByteSizeLimit: DefaultEventCollectionByteSizeLimit,
EventEncoder: NewCadenceEventEncoder(),
EventCollectionByteSizeLimit: DefaultEventCollectionByteSizeLimit,
EventEncoder: NewCadenceEventEncoder(),
}
}

Expand Down Expand Up @@ -191,27 +189,24 @@
// TODO: to set limit to maximum when it is service account and get rid of this flag
isServiceAccount := emitter.payer == emitter.chain.ServiceAddress()

if emitter.ServiceEventCollectionEnabled {
ok, err := IsServiceEvent(eventType, emitter.chain.ChainID())
if err != nil {
return fmt.Errorf("unable to check service event: %w", err)
}
if ok {
eventEmitError := emitter.eventCollection.AppendServiceEvent(
emitter.chain,
flowEvent,
uint64(payloadSize))

// skip limit if payer is service account
// TODO skip only limit-related errors
if !isServiceAccount && eventEmitError != nil {
return eventEmitError
}
isServiceEvent, err := IsServiceEvent(eventType, emitter.chain.ChainID())
if err != nil {
return fmt.Errorf("unable to check service event: %w", err)
}
if isServiceEvent {
eventEmitError := emitter.eventCollection.AppendServiceEvent(
emitter.chain,
flowEvent,
uint64(payloadSize))

// skip limit if payer is service account
// TODO skip only limit-related errors
if !isServiceAccount && eventEmitError != nil {
return eventEmitError
}
// We don't return and append the service event into event collection
// as well.
}

// Regardless of whether it is a service event, add to eventCollection
eventEmitError := emitter.eventCollection.AppendEvent(flowEvent, uint64(payloadSize))
// skip limit if payer is service account
if !isServiceAccount {
Expand Down Expand Up @@ -295,8 +290,11 @@
return collection.eventCounter
}

// IsServiceEvent determines whether or not an emitted Cadence event is
// considered a service event for the given chain.
// IsServiceEvent determines whether an emitted Cadence event is considered a service event for the given chain.
// An event is a service event if it is defined in the `systemcontracts` package allow-list.
jordanschalm marked this conversation as resolved.
Show resolved Hide resolved
// Note that we have *removed* the prior constraint that service events can only be

Check failure on line 295 in fvm/environment/event_emitter.go

View workflow job for this annotation

GitHub Actions / Lint (./)

File is not `goimports`-ed with -local github.com/onflow/flow-go (goimports)
// emitted in the system chunk. Now a system smart contract can emit service events
// as part of any transaction.
jordanschalm marked this conversation as resolved.
Show resolved Hide resolved
func IsServiceEvent(eventType flow.EventType, chain flow.ChainID) (bool, error) {

// retrieve the service event information for this chain
Expand Down
5 changes: 2 additions & 3 deletions fvm/environment/event_emitter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,8 @@ func createTestEventEmitterWithLimit(chain flow.ChainID, address flow.Address, e
},
},
environment.EventEmitterParams{
ServiceEventCollectionEnabled: false,
EventCollectionByteSizeLimit: eventEmitLimit,
EventEncoder: environment.NewCadenceEventEncoder(),
EventCollectionByteSizeLimit: eventEmitLimit,
EventEncoder: environment.NewCadenceEventEncoder(),
},
)
}
Expand Down
Loading