Skip to content

Commit 23b5153

Browse files
Merge branch 'master' into mpeter/add-file-system-test-contract
2 parents 1cb140e + 67daa85 commit 23b5153

12 files changed

+38
-74
lines changed

fvm/environment/account_creator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ func (creator *accountCreator) CreateAccount(
265265

266266
// don't enforce limit during account creation
267267
var address flow.Address
268-
creator.txnState.RunWithAllLimitsDisabled(func() {
268+
creator.txnState.RunWithMeteringDisabled(func() {
269269
address, err = creator.createAccount(flow.ConvertAddress(runtimePayer))
270270
})
271271

fvm/environment/accounts.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func (a *StatefulAccounts) AllocateSlabIndex(
7474
// compute storage size changes)
7575
// this way the getValue would load this value from deltas
7676
key := atree.SlabIndexToLedgerKey(index)
77-
a.txnState.RunWithAllLimitsDisabled(func() {
77+
a.txnState.RunWithMeteringDisabled(func() {
7878
err = a.txnState.Set(
7979
flow.NewRegisterID(address, string(key)),
8080
[]byte{})

fvm/environment/meter.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,6 @@ func (meter *meterImpl) ComputationAvailable(usage common.ComputationUsage) bool
104104
return meter.txnState.ComputationAvailable(usage)
105105
}
106106

107-
func (meter *meterImpl) ComputationRemaining(kind common.ComputationKind) uint64 {
108-
return meter.txnState.ComputationRemaining(kind)
109-
}
110-
111107
func (meter *meterImpl) ComputationUsed() (uint64, error) {
112108
return meter.txnState.TotalComputationUsed(), nil
113109
}

fvm/executionParameters.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func (computer ExecutionParametersComputer) Compute(
127127
) {
128128
var overrides derived.StateExecutionParameters
129129
var err error
130-
computer.txnState.RunWithAllLimitsDisabled(func() {
130+
computer.txnState.RunWithMeteringDisabled(func() {
131131
overrides, err = computer.getExecutionParameters()
132132
})
133133

fvm/meter/computation_meter.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -118,23 +118,6 @@ func (m *ComputationMeter) ComputationAvailable(usage common.ComputationUsage) b
118118
return potentialComputationUsage <= m.params.computationLimit
119119
}
120120

121-
// ComputationRemaining returns the remaining computation (intensity) left in the transaction for the given type
122-
func (m *ComputationMeter) ComputationRemaining(kind common.ComputationKind) uint64 {
123-
w, ok := m.params.computationWeights[kind]
124-
// if not found return has capacity
125-
// given the behaviour of MeterComputation is ignoring intensities without a set weight
126-
if !ok {
127-
return math.MaxUint64
128-
}
129-
130-
remainingComputationUsage := m.params.computationLimit - m.computationUsed
131-
if remainingComputationUsage <= 0 {
132-
return 0
133-
}
134-
135-
return remainingComputationUsage / w
136-
}
137-
138121
// ComputationIntensities returns all the measured computational intensities
139122
func (m *ComputationMeter) ComputationIntensities() MeteredComputationIntensities {
140123
return m.computationIntensities

fvm/meter/meter_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -214,16 +214,16 @@ func TestWeightedComputationMetering(t *testing.T) {
214214
))
215215
})
216216

217-
t.Run("check computation remaining", func(t *testing.T) {
217+
t.Run("check computation available", func(t *testing.T) {
218218
m := meter.NewMeter(
219219
meter.DefaultParameters().
220220
WithComputationLimit(10).
221221
WithComputationWeights(
222222
map[common.ComputationKind]uint64{0: 1 << meter.MeterExecutionInternalPrecisionBytes}),
223223
)
224224

225-
remaining := m.ComputationRemaining(0)
226-
require.Equal(t, uint64(10), remaining)
225+
available := m.ComputationAvailable(common.ComputationUsage{Kind: 0, Intensity: 10})
226+
require.True(t, available)
227227

228228
err := m.MeterComputation(
229229
common.ComputationUsage{
@@ -234,10 +234,10 @@ func TestWeightedComputationMetering(t *testing.T) {
234234
require.NoError(t, err)
235235
require.Equal(t, uint64(1), m.TotalComputationUsed())
236236

237-
require.Equal(t, uint64(9), m.ComputationRemaining(0))
237+
require.False(t, m.ComputationAvailable(common.ComputationUsage{Kind: 0, Intensity: 10}))
238238

239-
// test a type without a weight (default zero)
240-
require.Equal(t, uint64(math.MaxUint64), m.ComputationRemaining(1))
239+
// test a type without a weight (default MaxUint64)
240+
require.True(t, m.ComputationAvailable(common.ComputationUsage{Kind: 1, Intensity: math.MaxUint64}))
241241
})
242242

243243
t.Run("merge meters", func(t *testing.T) {

fvm/storage/state/execution_state.go

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package state
22

33
import (
44
"fmt"
5-
"math"
65

76
"github.com/onflow/cadence/common"
87
"github.com/onflow/crypto/hash"
@@ -81,27 +80,27 @@ func (params StateParameters) WithMaxValueSizeAllowed(
8180
}
8281

8382
type limitsController struct {
84-
enforceLimits bool
83+
meteringEnabled bool
8584
maxKeySizeAllowed uint64
8685
maxValueSizeAllowed uint64
8786
}
8887

8988
func newLimitsController(params StateParameters) *limitsController {
9089
return &limitsController{
91-
enforceLimits: true,
90+
meteringEnabled: true,
9291
maxKeySizeAllowed: params.maxKeySizeAllowed,
9392
maxValueSizeAllowed: params.maxValueSizeAllowed,
9493
}
9594
}
9695

97-
func (controller *limitsController) RunWithAllLimitsDisabled(f func()) {
96+
func (controller *limitsController) RunWithMeteringDisabled(f func()) {
9897
if f == nil {
9998
return
10099
}
101-
current := controller.enforceLimits
102-
controller.enforceLimits = false
100+
current := controller.meteringEnabled
101+
controller.meteringEnabled = false
103102
f()
104-
controller.enforceLimits = current
103+
controller.meteringEnabled = current
105104
}
106105

107106
// NewExecutionState constructs a new state
@@ -176,7 +175,7 @@ func (state *ExecutionState) Get(id flow.RegisterID) (flow.RegisterValue, error)
176175
var value []byte
177176
var err error
178177

179-
if state.enforceLimits {
178+
if state.meteringEnabled {
180179
if err = state.checkSize(id, []byte{}); err != nil {
181180
return nil, err
182181
}
@@ -189,7 +188,7 @@ func (state *ExecutionState) Get(id flow.RegisterID) (flow.RegisterValue, error)
189188
return nil, fmt.Errorf("failed to read %s: %w", id, getError)
190189
}
191190

192-
err = state.meter.MeterStorageRead(id, value, state.enforceLimits)
191+
err = state.meter.MeterStorageRead(id, value, state.meteringEnabled)
193192
return value, err
194193
}
195194

@@ -199,7 +198,7 @@ func (state *ExecutionState) Set(id flow.RegisterID, value flow.RegisterValue) e
199198
return fmt.Errorf("cannot Set on a finalized state")
200199
}
201200

202-
if state.enforceLimits {
201+
if state.meteringEnabled {
203202
if err := state.checkSize(id, value); err != nil {
204203
return err
205204
}
@@ -212,7 +211,7 @@ func (state *ExecutionState) Set(id flow.RegisterID, value flow.RegisterValue) e
212211
return fmt.Errorf("failed to update %s: %w", id, setError)
213212
}
214213

215-
return state.meter.MeterStorageWrite(id, value, state.enforceLimits)
214+
return state.meter.MeterStorageWrite(id, value, state.meteringEnabled)
216215
}
217216

218217
// MeterComputation meters computation usage
@@ -221,7 +220,7 @@ func (state *ExecutionState) MeterComputation(usage common.ComputationUsage) err
221220
return fmt.Errorf("cannot MeterComputation on a finalized state")
222221
}
223222

224-
if state.enforceLimits {
223+
if state.meteringEnabled {
225224
return state.meter.MeterComputation(usage)
226225
}
227226
return nil
@@ -234,26 +233,12 @@ func (state *ExecutionState) ComputationAvailable(usage common.ComputationUsage)
234233
return false
235234
}
236235

237-
if state.enforceLimits {
236+
if state.meteringEnabled {
238237
return state.meter.ComputationAvailable(usage)
239238
}
240239
return true
241240
}
242241

243-
// ComputationRemaining returns the available computation capacity without metering
244-
func (state *ExecutionState) ComputationRemaining(kind common.ComputationKind) uint64 {
245-
if state.finalized {
246-
// if state is finalized return 0
247-
return 0
248-
}
249-
250-
if state.enforceLimits {
251-
return state.meter.ComputationRemaining(kind)
252-
}
253-
254-
return math.MaxUint64
255-
}
256-
257242
// TotalComputationUsed returns total computation used
258243
func (state *ExecutionState) TotalComputationUsed() uint64 {
259244
return state.meter.TotalComputationUsed()
@@ -275,7 +260,7 @@ func (state *ExecutionState) MeterMemory(usage common.MemoryUsage) error {
275260
return fmt.Errorf("cannot MeterMemory on a finalized state")
276261
}
277262

278-
if state.enforceLimits {
263+
if state.meteringEnabled {
279264
return state.meter.MeterMemory(usage)
280265
}
281266

@@ -302,7 +287,7 @@ func (state *ExecutionState) MeterEmittedEvent(byteSize uint64) error {
302287
return fmt.Errorf("cannot MeterEmittedEvent on a finalized state")
303288
}
304289

305-
if state.enforceLimits {
290+
if state.meteringEnabled {
306291
return state.meter.MeterEmittedEvent(byteSize)
307292
}
308293

fvm/storage/state/transaction_state.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ func (id NestedTransactionId) StateForTestingOnly() *ExecutionState {
2222
type Meter interface {
2323
MeterComputation(usage common.ComputationUsage) error
2424
ComputationAvailable(usage common.ComputationUsage) bool
25-
ComputationRemaining(kind common.ComputationKind) uint64
2625
ComputationIntensities() meter.MeteredComputationIntensities
2726
TotalComputationLimit() uint64
2827
TotalComputationUsed() uint64
@@ -36,8 +35,13 @@ type Meter interface {
3635
MeterEmittedEvent(byteSize uint64) error
3736
TotalEmittedEventBytes() uint64
3837

39-
// RunWithAllLimitsDisabled runs f with limits disabled
40-
RunWithAllLimitsDisabled(f func())
38+
// RunWithMeteringDisabled runs f with limits disabled
39+
// This function can be used to run a function that fits one of these cases:
40+
// - the function should not fail due to metering limits
41+
// - the function is not invokable by the user and has a constant execution time (e.g. fee deduction)
42+
// - the function is metered once before calling `RunWithMeteringDisabled` with a special weight (e.g. create account)
43+
// and doesn't need additional metering inside the function
44+
RunWithMeteringDisabled(f func())
4145
}
4246

4347
// NestedTransactionPreparer provides active transaction states and facilitates
@@ -453,10 +457,6 @@ func (txnState *transactionState) ComputationAvailable(usage common.ComputationU
453457
return txnState.current().ComputationAvailable(usage)
454458
}
455459

456-
func (txnState *transactionState) ComputationRemaining(kind common.ComputationKind) uint64 {
457-
return txnState.current().ComputationRemaining(kind)
458-
}
459-
460460
func (txnState *transactionState) MeterMemory(usage common.MemoryUsage) error {
461461
return txnState.current().MeterMemory(usage)
462462
}
@@ -493,6 +493,6 @@ func (txnState *transactionState) TotalEmittedEventBytes() uint64 {
493493
return txnState.current().TotalEmittedEventBytes()
494494
}
495495

496-
func (txnState *transactionState) RunWithAllLimitsDisabled(f func()) {
497-
txnState.current().RunWithAllLimitsDisabled(f)
496+
func (txnState *transactionState) RunWithMeteringDisabled(f func()) {
497+
txnState.current().RunWithMeteringDisabled(f)
498498
}

fvm/transactionInvoker.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ func (executor *transactionExecutor) ExecuteTransactionBody() error {
281281

282282
if executor.errs.CollectedError() {
283283
invalidator = nil
284-
executor.txnState.RunWithAllLimitsDisabled(executor.errorExecution)
284+
executor.txnState.RunWithMeteringDisabled(executor.errorExecution)
285285
if executor.errs.CollectedFailure() {
286286
return executor.errs.ErrorOrNil()
287287
}
@@ -360,7 +360,7 @@ func (executor *transactionExecutor) normalExecution() (
360360
var maxTxFees uint64
361361
// run with limits disabled since this is a static cost check
362362
// and should be accounted for in the inclusion cost.
363-
executor.txnState.RunWithAllLimitsDisabled(func() {
363+
executor.txnState.RunWithMeteringDisabled(func() {
364364
maxTxFees, err = executor.CheckPayerBalanceAndReturnMaxFees(
365365
executor.proc,
366366
executor.txnState,
@@ -424,7 +424,7 @@ func (executor *transactionExecutor) normalExecution() (
424424
return
425425
}
426426

427-
executor.txnState.RunWithAllLimitsDisabled(func() {
427+
executor.txnState.RunWithMeteringDisabled(func() {
428428
err = executor.deductTransactionFees()
429429
})
430430

fvm/transactionPayerBalanceChecker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func (_ TransactionPayerBalanceChecker) CheckPayerBalanceAndReturnMaxFees(
7373

7474
var resultValue cadence.Value
7575
var err error
76-
txnState.RunWithAllLimitsDisabled(func() {
76+
txnState.RunWithMeteringDisabled(func() {
7777
// Don't meter the payer balance check.
7878
// It has a static cost, and its cost should be part of the inclusion fees, not part of execution fees.
7979
resultValue, err = env.CheckPayerBalanceAndGetMaxTxFees(

0 commit comments

Comments
 (0)