Skip to content

Commit

Permalink
Add test cases for asserting the population of ProcedureOutput from s…
Browse files Browse the repository at this point in the history
…cript execution
  • Loading branch information
m-Peter committed Jul 20, 2023
1 parent 574fb22 commit 1aa6e3d
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 2 deletions.
81 changes: 81 additions & 0 deletions fvm/fvm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2023,6 +2023,87 @@ func TestScriptAccountKeyMutationsFailure(t *testing.T) {
)
}

func TestScriptExecutionLimit(t *testing.T) {

t.Parallel()

script := fvm.Script([]byte(`
pub fun main() {
var s: Int256 = 1024102410241024
var i: Int256 = 0
var a: Int256 = 7
var b: Int256 = 5
var c: Int256 = 2
while i < 150000 {
s = s * a
s = s / b
s = s / c
i = i + 1
}
}
`))

bootstrapProcedureOptions := []fvm.BootstrapProcedureOption{
fvm.WithTransactionFee(fvm.DefaultTransactionFees),
fvm.WithExecutionMemoryLimit(math.MaxUint32),
fvm.WithExecutionEffortWeights(map[common.ComputationKind]uint64{
common.ComputationKindStatement: 1569,
common.ComputationKindLoop: 1569,
common.ComputationKindFunctionInvocation: 1569,
environment.ComputationKindGetValue: 808,
environment.ComputationKindCreateAccount: 2837670,
environment.ComputationKindSetValue: 765,
}),
fvm.WithExecutionMemoryWeights(meter.DefaultMemoryWeights),
fvm.WithMinimumStorageReservation(fvm.DefaultMinimumStorageReservation),
fvm.WithAccountCreationFee(fvm.DefaultAccountCreationFee),
fvm.WithStorageMBPerFLOW(fvm.DefaultStorageMBPerFLOW),
}

t.Run("Exceeding computation limit",
newVMTest().withBootstrapProcedureOptions(
bootstrapProcedureOptions...,
).withContextOptions(
fvm.WithTransactionFeesEnabled(true),
fvm.WithAccountStorageLimit(true),
fvm.WithComputationLimit(10000),
).run(
func(t *testing.T, vm fvm.VM, chain flow.Chain, ctx fvm.Context, snapshotTree snapshot.SnapshotTree) {
scriptCtx := fvm.NewContextFromParent(ctx)

_, output, err := vm.Run(scriptCtx, script, snapshotTree)
require.NoError(t, err)
require.Error(t, output.Err)
require.True(t, errors.IsComputationLimitExceededError(output.Err))
require.ErrorContains(t, output.Err, "computation exceeds limit (10000)")
require.GreaterOrEqual(t, output.ComputationUsed, uint64(10000))
require.GreaterOrEqual(t, output.MemoryEstimate, uint64(548020260))
},
),
)

t.Run("Sufficient computation limit",
newVMTest().withBootstrapProcedureOptions(
bootstrapProcedureOptions...,
).withContextOptions(
fvm.WithTransactionFeesEnabled(true),
fvm.WithAccountStorageLimit(true),
fvm.WithComputationLimit(20000),
).run(
func(t *testing.T, vm fvm.VM, chain flow.Chain, ctx fvm.Context, snapshotTree snapshot.SnapshotTree) {
scriptCtx := fvm.NewContextFromParent(ctx)

_, output, err := vm.Run(scriptCtx, script, snapshotTree)
require.NoError(t, err)
require.NoError(t, output.Err)
require.GreaterOrEqual(t, output.ComputationUsed, uint64(17955))
require.GreaterOrEqual(t, output.MemoryEstimate, uint64(984017413))
},
),
)
}

func TestInteractionLimit(t *testing.T) {
type testCase struct {
name string
Expand Down
4 changes: 2 additions & 2 deletions fvm/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,9 @@ func (executor *scriptExecutor) executeScript() error {
)
populateErr := executor.output.PopulateEnvironmentValues(executor.env)
if err != nil {
return err
return multierror.Append(err, populateErr)
}

executor.output.Value = value
return multierror.Append(err, populateErr)
return populateErr
}

0 comments on commit 1aa6e3d

Please sign in to comment.