-
Notifications
You must be signed in to change notification settings - Fork 178
/
value_store.go
216 lines (185 loc) · 4.36 KB
/
value_store.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package environment
import (
"fmt"
"github.com/onflow/atree"
"github.com/onflow/flow-go/fvm/errors"
"github.com/onflow/flow-go/fvm/storage/state"
"github.com/onflow/flow-go/fvm/tracing"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module/trace"
)
// ValueStore provides read/write access to the account storage.
type ValueStore interface {
GetValue(owner []byte, key []byte) ([]byte, error)
SetValue(owner, key, value []byte) error
ValueExists(owner []byte, key []byte) (bool, error)
AllocateStorageIndex(owner []byte) (atree.StorageIndex, error)
}
type ParseRestrictedValueStore struct {
txnState state.NestedTransactionPreparer
impl ValueStore
}
func NewParseRestrictedValueStore(
txnState state.NestedTransactionPreparer,
impl ValueStore,
) ValueStore {
return ParseRestrictedValueStore{
txnState: txnState,
impl: impl,
}
}
func (store ParseRestrictedValueStore) GetValue(
owner []byte,
key []byte,
) (
[]byte,
error,
) {
return parseRestrict2Arg1Ret(
store.txnState,
trace.FVMEnvGetValue,
store.impl.GetValue,
owner,
key)
}
func (store ParseRestrictedValueStore) SetValue(
owner []byte,
key []byte,
value []byte,
) error {
return parseRestrict3Arg(
store.txnState,
trace.FVMEnvSetValue,
store.impl.SetValue,
owner,
key,
value)
}
func (store ParseRestrictedValueStore) ValueExists(
owner []byte,
key []byte,
) (
bool,
error,
) {
return parseRestrict2Arg1Ret(
store.txnState,
trace.FVMEnvValueExists,
store.impl.ValueExists,
owner,
key)
}
func (store ParseRestrictedValueStore) AllocateStorageIndex(
owner []byte,
) (
atree.StorageIndex,
error,
) {
return parseRestrict1Arg1Ret(
store.txnState,
trace.FVMEnvAllocateStorageIndex,
store.impl.AllocateStorageIndex,
owner)
}
type valueStore struct {
tracer tracing.TracerSpan
meter Meter
accounts Accounts
}
func NewValueStore(
tracer tracing.TracerSpan,
meter Meter,
accounts Accounts,
) ValueStore {
return &valueStore{
tracer: tracer,
meter: meter,
accounts: accounts,
}
}
func (store *valueStore) GetValue(
owner []byte,
keyBytes []byte,
) (
[]byte,
error,
) {
defer store.tracer.StartChildSpan(trace.FVMEnvGetValue).End()
id := flow.CadenceRegisterID(owner, keyBytes)
if id.IsInternalState() {
return nil, errors.NewInvalidInternalStateAccessError(id, "read")
}
v, err := store.accounts.GetValue(id)
if err != nil {
return nil, fmt.Errorf("get value failed: %w", err)
}
err = store.meter.MeterComputation(ComputationKindGetValue, uint(len(v)))
if err != nil {
return nil, fmt.Errorf("get value failed: %w", err)
}
return v, nil
}
// TODO disable SetValue for scripts, right now the view changes are discarded
func (store *valueStore) SetValue(
owner []byte,
keyBytes []byte,
value []byte,
) error {
defer store.tracer.StartChildSpan(trace.FVMEnvSetValue).End()
id := flow.CadenceRegisterID(owner, keyBytes)
if id.IsInternalState() {
return errors.NewInvalidInternalStateAccessError(id, "modify")
}
err := store.meter.MeterComputation(
ComputationKindSetValue,
uint(len(value)))
if err != nil {
return fmt.Errorf("set value failed: %w", err)
}
err = store.accounts.SetValue(id, value)
if err != nil {
return fmt.Errorf("set value failed: %w", err)
}
return nil
}
func (store *valueStore) ValueExists(
owner []byte,
key []byte,
) (
exists bool,
err error,
) {
defer store.tracer.StartChildSpan(trace.FVMEnvValueExists).End()
err = store.meter.MeterComputation(ComputationKindValueExists, 1)
if err != nil {
return false, fmt.Errorf("check value existence failed: %w", err)
}
v, err := store.GetValue(owner, key)
if err != nil {
return false, fmt.Errorf("check value existence failed: %w", err)
}
return len(v) > 0, nil
}
// AllocateStorageIndex allocates new storage index under the owner accounts
// to store a new register.
func (store *valueStore) AllocateStorageIndex(
owner []byte,
) (
atree.StorageIndex,
error,
) {
defer store.tracer.StartChildSpan(trace.FVMEnvAllocateStorageIndex).End()
err := store.meter.MeterComputation(ComputationKindAllocateStorageIndex, 1)
if err != nil {
return atree.StorageIndex{}, fmt.Errorf(
"allocate storage index failed: %w",
err)
}
v, err := store.accounts.AllocateStorageIndex(flow.BytesToAddress(owner))
if err != nil {
return atree.StorageIndex{}, fmt.Errorf(
"storage address allocation failed: %w",
err)
}
return v, nil
}