-
Notifications
You must be signed in to change notification settings - Fork 179
/
meter.go
168 lines (144 loc) · 4.31 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package environment
import (
"context"
"github.com/onflow/cadence/runtime/common"
"github.com/onflow/flow-go/fvm/errors"
"github.com/onflow/flow-go/fvm/meter"
"github.com/onflow/flow-go/fvm/storage/state"
)
const (
// [2_000, 3_000) reserved for the FVM
ComputationKindHash = 2001 + iota
ComputationKindVerifySignature
ComputationKindAddAccountKey
ComputationKindAddEncodedAccountKey
ComputationKindAllocateStorageIndex
ComputationKindCreateAccount
ComputationKindEmitEvent
ComputationKindGenerateUUID
ComputationKindGetAccountAvailableBalance
ComputationKindGetAccountBalance
ComputationKindGetAccountContractCode
ComputationKindGetAccountContractNames
ComputationKindGetAccountKey
ComputationKindGetBlockAtHeight
ComputationKindGetCode
ComputationKindGetCurrentBlockHeight
_
ComputationKindGetStorageCapacity
ComputationKindGetStorageUsed
ComputationKindGetValue
ComputationKindRemoveAccountContractCode
ComputationKindResolveLocation
ComputationKindRevokeAccountKey
ComputationKindRevokeEncodedAccountKey
_
ComputationKindSetValue
ComputationKindUpdateAccountContractCode
ComputationKindValidatePublicKey
ComputationKindValueExists
ComputationKindAccountKeysCount
ComputationKindBLSVerifyPOP
ComputationKindBLSAggregateSignatures
ComputationKindBLSAggregatePublicKeys
ComputationKindGetOrLoadProgram
ComputationKindGenerateAccountLocalID
ComputationKindGetRandomSourceHistory
ComputationKindEVMGasUsage
ComputationKindRLPEncoding
ComputationKindRLPDecoding
ComputationKindEncodeEvent
_
ComputationKindEVMEncodeABI
ComputationKindEVMDecodeABI
)
type Meter interface {
MeterComputation(common.ComputationKind, uint) error
ComputationUsed() (uint64, error)
ComputationIntensities() meter.MeteredComputationIntensities
ComputationAvailable(common.ComputationKind, uint) bool
MeterMemory(usage common.MemoryUsage) error
MemoryUsed() (uint64, error)
MeterEmittedEvent(byteSize uint64) error
TotalEmittedEventBytes() uint64
InteractionUsed() (uint64, error)
}
type meterImpl struct {
txnState state.NestedTransactionPreparer
}
func NewMeter(txnState state.NestedTransactionPreparer) Meter {
return &meterImpl{
txnState: txnState,
}
}
func (meter *meterImpl) MeterComputation(
kind common.ComputationKind,
intensity uint,
) error {
return meter.txnState.MeterComputation(kind, intensity)
}
func (meter *meterImpl) ComputationIntensities() meter.MeteredComputationIntensities {
return meter.txnState.ComputationIntensities()
}
func (meter *meterImpl) ComputationAvailable(
kind common.ComputationKind,
intensity uint,
) bool {
return meter.txnState.ComputationAvailable(kind, intensity)
}
func (meter *meterImpl) ComputationUsed() (uint64, error) {
return meter.txnState.TotalComputationUsed(), nil
}
func (meter *meterImpl) MeterMemory(usage common.MemoryUsage) error {
return meter.txnState.MeterMemory(usage.Kind, uint(usage.Amount))
}
func (meter *meterImpl) MemoryUsed() (uint64, error) {
return meter.txnState.TotalMemoryEstimate(), nil
}
func (meter *meterImpl) InteractionUsed() (uint64, error) {
return meter.txnState.InteractionUsed(), nil
}
func (meter *meterImpl) MeterEmittedEvent(byteSize uint64) error {
return meter.txnState.MeterEmittedEvent(byteSize)
}
func (meter *meterImpl) TotalEmittedEventBytes() uint64 {
return meter.txnState.TotalEmittedEventBytes()
}
type cancellableMeter struct {
meterImpl
ctx context.Context
}
func NewCancellableMeter(
ctx context.Context,
txnState state.NestedTransactionPreparer,
) 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)
}