Skip to content

Commit 43f825c

Browse files
inspection fixes
1 parent e063d22 commit 43f825c

File tree

8 files changed

+48
-156
lines changed

8 files changed

+48
-156
lines changed

engine/execution/computation/computer/computer_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -352,9 +352,6 @@ func TestBlockExecutor_ExecuteBlock(t *testing.T) {
352352
Return(noOpExecutor{}).
353353
Once() // just system chunk
354354

355-
vm.On("Inspect", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).
356-
Return(nil)
357-
358355
snapshot := storehouse.NewExecutingBlockSnapshot(
359356
snapshot.MapStorageSnapshot{},
360357
unittest.StateCommitmentFixture(),

engine/execution/computation/computer/transaction_coordinator.go

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -149,23 +149,10 @@ func (coordinator *transactionCoordinator) commit(txn *transaction) error {
149149
return err
150150
}
151151

152-
output := txn.Output()
153-
154-
// Run inspection on the transaction results.
155-
// This is done here rather than in vm.Run() because the block computer
156-
// uses NewExecutor for fine-grained transaction control.
157-
output.InspectionResults = coordinator.vm.Inspect(
158-
txn.request.ctx,
159-
txn.request.TransactionProcedure,
160-
coordinator.storageSnapshot,
161-
executionSnapshot,
162-
output,
163-
)
164-
165152
coordinator.writeBehindLog.AddTransactionResult(
166153
txn.request,
167154
executionSnapshot,
168-
output,
155+
txn.Output(),
169156
time.Since(txn.startedAt),
170157
txn.numConflictRetries)
171158

fvm/fvm.go

Lines changed: 24 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"github.com/onflow/cadence"
88
"github.com/onflow/cadence/common"
99
"github.com/rs/zerolog"
10-
"github.com/rs/zerolog/log"
1110

1211
"github.com/onflow/flow-go/fvm/inspection"
1312

@@ -72,6 +71,25 @@ func (output *ProcedureOutput) PopulateEnvironmentValues(
7271
return nil
7372
}
7473

74+
func (output *ProcedureOutput) PopulateInspectionResults(
75+
log zerolog.Logger,
76+
ctx Context,
77+
env environment.Environment,
78+
storageSnapshot snapshot.StorageSnapshot,
79+
executionSnapshot *snapshot.ExecutionSnapshot,
80+
) {
81+
82+
evts := make([]flow.Event, 0, len(output.Events)+len(output.ServiceEvents))
83+
evts = append(evts, output.Events...)
84+
evts = append(evts, output.ServiceEvents...)
85+
86+
log.Debug().Str("module", "tc-inspector").
87+
Int("inspectors", len(ctx.Inspectors)).
88+
Msg("populating environment values for procedure output")
89+
inspectionResults := inspectProcedureResults(log, ctx, storageSnapshot, executionSnapshot, evts)
90+
output.InspectionResults = inspectionResults
91+
}
92+
7593
type ProcedureExecutor interface {
7694
Preprocess() error
7795
Execute() error
@@ -124,17 +142,6 @@ type VM interface {
124142
ProcedureOutput,
125143
error,
126144
)
127-
128-
// Inspect runs configured inspectors on the procedure results.
129-
// This is used by the block computer which manages transaction execution
130-
// manually via NewExecutor rather than using Run.
131-
Inspect(
132-
ctx Context,
133-
proc Procedure,
134-
storageSnapshot snapshot.StorageSnapshot,
135-
executionSnapshot *snapshot.ExecutionSnapshot,
136-
output ProcedureOutput,
137-
) []inspection.Result
138145
}
139146

140147
var _ VM = (*VirtualMachine)(nil)
@@ -214,56 +221,22 @@ func (vm *VirtualMachine) Run(
214221
return nil, ProcedureOutput{}, err
215222
}
216223

217-
// This is of informative nature right now so this placement is ok
218-
// In the future we will need to move this inside the procedure if we want it to affect execution
219-
output := executor.Output()
220-
log.Debug().Str("module", "tc-inspector").
221-
Str("procedure-type", string(proc.Type())).
222-
Int("inspectors", len(ctx.Inspectors)).
223-
Msg("populating environment values for procedure output")
224-
inspectionResults := vm.inspectProcedureResults(ctx.Logger, ctx, proc, storageSnapshot, executionSnapshot, output)
225-
output.InspectionResults = inspectionResults
226-
227-
return executionSnapshot, output, nil
228-
}
229-
230-
// Inspect runs configured inspectors on the procedure results.
231-
// This is used by the block computer which manages transaction execution
232-
// manually via NewExecutor rather than using Run.
233-
func (vm *VirtualMachine) Inspect(
234-
ctx Context,
235-
proc Procedure,
236-
storageSnapshot snapshot.StorageSnapshot,
237-
executionSnapshot *snapshot.ExecutionSnapshot,
238-
output ProcedureOutput,
239-
) []inspection.Result {
240-
return vm.inspectProcedureResults(ctx.Logger, ctx, proc, storageSnapshot, executionSnapshot, output)
224+
return executionSnapshot, executor.Output(), nil
241225
}
242226

243-
func (vm *VirtualMachine) inspectProcedureResults(
227+
func inspectProcedureResults(
244228
logger zerolog.Logger,
245229
context Context,
246-
proc Procedure,
247230
storageSnapshot snapshot.StorageSnapshot,
248231
executionSnapshot *snapshot.ExecutionSnapshot,
249-
output ProcedureOutput,
232+
events []flow.Event,
250233
) []inspection.Result {
251-
// TODO(janezp): this should be decided by the inspector
252-
if proc.Type() != TransactionProcedureType {
253-
logger.Debug().Str("module", "tc-inspector").Msg("skipping inspection for non-transaction procedure")
254-
return nil
255-
}
256-
257-
// TODO(janezp): inspector should be able to receive ProcedureOutput directly
258-
evts := make([]flow.Event, 0, len(output.Events)+len(output.ServiceEvents))
259-
evts = append(evts, output.Events...)
260-
evts = append(evts, output.ServiceEvents...)
261-
262234
inspectionResults := make([]inspection.Result, len(context.Inspectors))
263235
logger.Debug().Str("module", "tc-inspector").Int("num_inspectors", len(context.Inspectors)).Msg("inspecting procedure results")
264236
var err error
265237
for i, inspector := range context.Inspectors {
266-
inspectionResults[i], err = inspector.Inspect(logger, storageSnapshot, executionSnapshot, evts)
238+
// TODO(janezp): inspector should be able to receive ProcedureOutput directly
239+
inspectionResults[i], err = inspector.Inspect(logger, storageSnapshot, executionSnapshot, events)
267240
if err != nil {
268241
logger.Warn().Str("module", "tc-inspector").Err(err).Msg("failed to inspect procedure results")
269242
}

fvm/fvm_test.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"testing"
1313

1414
"github.com/onflow/flow-core-contracts/lib/go/templates"
15-
"github.com/rs/zerolog"
1615
"github.com/stretchr/testify/assert"
1716
mockery "github.com/stretchr/testify/mock"
1817
"github.com/stretchr/testify/require"
@@ -4613,6 +4612,10 @@ func TestFlowTokenChangesInspector(t *testing.T) {
46134612

46144613
differ := inspection.NewTokenChangesInspector(tc.tokenDefinitions)
46154614

4615+
// Add the inspector to the context so inspection runs
4616+
// as part of the transaction execution pipeline.
4617+
ctx = fvm.NewContextFromParent(ctx, fvm.WithInspectors([]inspection.Inspector{differ}))
4618+
46164619
// Create an account private key.
46174620
privateKey, err := testutil.GenerateAccountPrivateKey()
46184621
require.NoError(t, err)
@@ -4628,7 +4631,7 @@ func TestFlowTokenChangesInspector(t *testing.T) {
46284631

46294632
txBody := tc.txBody(t, chain, accounts)
46304633

4631-
executionSnapshot, output, err := vm.Run(
4634+
_, output, err := vm.Run(
46324635
ctx,
46334636
fvm.Transaction(txBody, 0),
46344637
snapshotTree)
@@ -4640,14 +4643,8 @@ func TestFlowTokenChangesInspector(t *testing.T) {
46404643
require.NoError(t, output.Err)
46414644
}
46424645

4643-
evts := make([]flow.Event, 0, len(output.Events)+len(output.ServiceEvents))
4644-
evts = append(evts, output.Events...)
4645-
evts = append(evts, output.ServiceEvents...)
4646-
4647-
diff, err := differ.Inspect(zerolog.Nop(), snapshotTree, executionSnapshot, evts)
4648-
require.NoError(t, err)
4649-
4650-
tc.resultChecker(t, diff.(inspection.TokenDiffResult))
4646+
require.Len(t, output.InspectionResults, 1, "expected one inspection result")
4647+
tc.resultChecker(t, output.InspectionResults[0].(inspection.TokenDiffResult))
46514648
},
46524649
)
46534650
}

fvm/mock/vm.go

Lines changed: 0 additions & 78 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

fvm/storage/state/storage_state.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,9 @@ func (state *storageState) interimReadSet(
131131
accumulator[id] = struct{}{}
132132
}
133133
}
134+
135+
// BaseStorageSnapshot gives access to the storage snapshot as it was without changes
136+
// WARNING: this should not be read mid-transaction as reads to it are not recorded in the spocks
137+
func (state *storageState) BaseStorageSnapshot() snapshot.StorageSnapshot {
138+
return state.baseStorage
139+
}

fvm/storage/state/transaction_state.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ type NestedTransactionPreparer interface {
160160
Get(id flow.RegisterID) (flow.RegisterValue, error)
161161

162162
Set(id flow.RegisterID, value flow.RegisterValue) error
163+
164+
// BaseStorageSnapshot gives access to the storage snapshot as it was without changes
165+
// WARNING: this should not be read mid-transaction as reads to it are not recorded in the spocks
166+
BaseStorageSnapshot() snapshot.StorageSnapshot
163167
}
164168

165169
type nestedTransactionStackFrame struct {
@@ -450,6 +454,10 @@ func (txnState *transactionState) Set(
450454
return txnState.current().Set(id, value)
451455
}
452456

457+
func (txnState *transactionState) BaseStorageSnapshot() snapshot.StorageSnapshot {
458+
return txnState.nestedTransactions[0].ExecutionState.BaseStorageSnapshot()
459+
}
460+
453461
func (txnState *transactionState) MeterComputation(usage common.ComputationUsage) error {
454462
return txnState.current().MeterComputation(usage)
455463
}

fvm/transactionInvoker.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,5 +465,7 @@ func (executor *transactionExecutor) commit(
465465
commitErr)
466466
}
467467

468+
executor.output.PopulateInspectionResults(executor.ctx.Logger, executor.ctx, executor.env, executor.txnState, executor.executionStateRead)
469+
468470
return nil
469471
}

0 commit comments

Comments
 (0)