-
Notifications
You must be signed in to change notification settings - Fork 178
/
meter.go
144 lines (124 loc) · 3.47 KB
/
meter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package environment
import (
"context"
"github.com/onflow/cadence/runtime/common"
"github.com/onflow/flow-go/fvm/errors"
"github.com/onflow/flow-go/fvm/state"
)
const (
// [2_000, 3_000) reserved for the FVM
_ common.ComputationKind = iota + 2_000
ComputationKindHash
ComputationKindVerifySignature
ComputationKindAddAccountKey
ComputationKindAddEncodedAccountKey
ComputationKindAllocateStorageIndex
ComputationKindCreateAccount
ComputationKindEmitEvent
ComputationKindGenerateUUID
ComputationKindGetAccountAvailableBalance
ComputationKindGetAccountBalance
ComputationKindGetAccountContractCode
ComputationKindGetAccountContractNames
ComputationKindGetAccountKey
ComputationKindGetBlockAtHeight
ComputationKindGetCode
ComputationKindGetCurrentBlockHeight
ComputationKindGetProgram
ComputationKindGetStorageCapacity
ComputationKindGetStorageUsed
ComputationKindGetValue
ComputationKindRemoveAccountContractCode
ComputationKindResolveLocation
ComputationKindRevokeAccountKey
ComputationKindRevokeEncodedAccountKey
ComputationKindSetProgram
ComputationKindSetValue
ComputationKindUpdateAccountContractCode
ComputationKindValidatePublicKey
ComputationKindValueExists
)
type Meter interface {
MeterComputation(common.ComputationKind, uint) error
ComputationUsed() uint64
MeterMemory(usage common.MemoryUsage) error
MemoryEstimate() uint64
MeterEmittedEvent(byteSize uint64) error
TotalEmittedEventBytes() uint64
}
type meterImpl struct {
txnState *state.TransactionState
}
func NewMeter(txnState *state.TransactionState) Meter {
return &meterImpl{
txnState: txnState,
}
}
func (meter *meterImpl) MeterComputation(
kind common.ComputationKind,
intensity uint,
) error {
if meter.txnState.EnforceLimits() {
return meter.txnState.MeterComputation(kind, intensity)
}
return nil
}
func (meter *meterImpl) ComputationUsed() uint64 {
return uint64(meter.txnState.TotalComputationUsed())
}
func (meter *meterImpl) MeterMemory(usage common.MemoryUsage) error {
if meter.txnState.EnforceLimits() {
return meter.txnState.MeterMemory(usage.Kind, uint(usage.Amount))
}
return nil
}
func (meter *meterImpl) MemoryEstimate() uint64 {
return meter.txnState.TotalMemoryEstimate()
}
func (meter *meterImpl) MeterEmittedEvent(byteSize uint64) error {
if meter.txnState.EnforceLimits() {
return meter.txnState.MeterEmittedEvent(byteSize)
}
return nil
}
func (meter *meterImpl) TotalEmittedEventBytes() uint64 {
return meter.txnState.TotalEmittedEventBytes()
}
type cancellableMeter struct {
meterImpl
ctx context.Context
}
func NewCancellableMeter(
ctx context.Context,
txnState *state.TransactionState,
) Meter {
return &cancellableMeter{
meterImpl: meterImpl{
txnState: txnState,
},
ctx: ctx,
}
}
func (meter *cancellableMeter) MeterComputation(
kind common.ComputationKind,
intensity uint,
) error {
// this method is called on every unit of operation, so
// checking the context here is the most likely would capture
// timeouts or cancellation as soon as they happen, though
// we might revisit this when optimizing script execution
// by only checking on specific kind of Meter calls.
//
// in the future this context check should be done inside the cadence
select {
case <-meter.ctx.Done():
err := meter.ctx.Err()
if errors.Is(err, context.DeadlineExceeded) {
return errors.NewScriptExecutionTimedOutError()
}
return errors.NewScriptExecutionCancelledError(err)
default:
// do nothing
}
return meter.meterImpl.MeterComputation(kind, intensity)
}