-
Notifications
You must be signed in to change notification settings - Fork 175
/
executionParameters.go
137 lines (124 loc) · 4.47 KB
/
executionParameters.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
package fvm
import (
"github.com/onflow/cadence"
"github.com/onflow/cadence/runtime"
"github.com/onflow/cadence/runtime/common"
"github.com/onflow/flow-go/fvm/blueprints"
"github.com/onflow/flow-go/fvm/errors"
"github.com/onflow/flow-go/fvm/meter/weighted"
"github.com/onflow/flow-go/fvm/utils"
)
// getExecutionWeights reads stored execution effort weights from the service account
func getExecutionEffortWeights(
env Environment,
service runtime.Address,
) (
computationWeights weighted.ExecutionEffortWeights,
err error,
) {
value, err := env.VM().Runtime.ReadStored(
service,
cadence.Path{
Domain: blueprints.TransactionExecutionParametersPathDomain,
Identifier: blueprints.TransactionFeesExecutionEffortWeightsPathIdentifier,
},
runtime.Context{Interface: env},
)
if err != nil {
// this might be fatal, return as is
return nil, err
}
computationWeightsRaw, ok := utils.CadenceValueToWeights(value)
if !ok {
// this is a non-fatal error. It is expected if the weights are not set up on the network yet.
return nil, errors.NewCouldNotGetExecutionParameterFromStateError(
service.Hex(),
blueprints.TransactionExecutionParametersPathDomain,
blueprints.TransactionFeesExecutionEffortWeightsPathIdentifier)
}
// Merge the default weights with the weights from the state.
// This allows for weights that are not set in the state, to be set by default.
// In case the network is stuck because of a transaction using an FVM feature that has 0 weight
// (or is not metered at all), the defaults can be changed and the network restarted
// instead of trying to change the weights with a transaction.
computationWeights = make(weighted.ExecutionEffortWeights, len(weighted.DefaultComputationWeights))
for k, v := range weighted.DefaultComputationWeights {
computationWeights[k] = v
}
for k, v := range computationWeightsRaw {
computationWeights[common.ComputationKind(k)] = v
}
return computationWeights, nil
}
// getExecutionMemoryWeights reads stored execution memory weights from the service account
func getExecutionMemoryWeights(
env Environment,
service runtime.Address,
) (
memoryWeights weighted.ExecutionMemoryWeights,
err error,
) {
value, err := env.VM().Runtime.ReadStored(
service,
cadence.Path{
Domain: blueprints.TransactionExecutionParametersPathDomain,
Identifier: blueprints.TransactionFeesExecutionMemoryWeightsPathIdentifier,
},
runtime.Context{Interface: env},
)
if err != nil {
// this might be fatal, return as is
return nil, err
}
memoryWeightsRaw, ok := utils.CadenceValueToWeights(value)
if !ok {
// this is a non-fatal error. It is expected if the weights are not set up on the network yet.
return nil, errors.NewCouldNotGetExecutionParameterFromStateError(
service.Hex(),
blueprints.TransactionExecutionParametersPathDomain,
blueprints.TransactionFeesExecutionMemoryWeightsPathIdentifier)
}
// Merge the default weights with the weights from the state.
// This allows for weights that are not set in the state, to be set by default.
// In case the network is stuck because of a transaction using an FVM feature that has 0 weight
// (or is not metered at all), the defaults can be changed and the network restarted
// instead of trying to change the weights with a transaction.
memoryWeights = make(weighted.ExecutionMemoryWeights, len(weighted.DefaultMemoryWeights))
for k, v := range weighted.DefaultMemoryWeights {
memoryWeights[k] = v
}
for k, v := range memoryWeightsRaw {
memoryWeights[common.MemoryKind(k)] = v
}
return memoryWeights, nil
}
// getExecutionMemoryLimit reads the stored execution memory limit from the service account
func getExecutionMemoryLimit(
env Environment,
service runtime.Address,
) (
memoryLimit uint64,
err error,
) {
value, err := env.VM().Runtime.ReadStored(
service,
cadence.Path{
Domain: blueprints.TransactionExecutionParametersPathDomain,
Identifier: blueprints.TransactionFeesExecutionMemoryLimitPathIdentifier,
},
runtime.Context{Interface: env},
)
if err != nil {
// this might be fatal, return as is
return 0, err
}
memoryLimitRaw, ok := value.(cadence.UInt64)
if value == nil || !ok {
// this is a non-fatal error. It is expected if the weights are not set up on the network yet.
return 0, errors.NewCouldNotGetExecutionParameterFromStateError(
service.Hex(),
blueprints.TransactionExecutionParametersPathDomain,
blueprints.TransactionFeesExecutionMemoryLimitPathIdentifier)
}
return memoryLimitRaw.ToGoValue().(uint64), nil
}