Skip to content

Commit c93afae

Browse files
committed
Decouple GetMember and SetMember methods from the interpreter
1 parent 2bb2f45 commit c93afae

File tree

85 files changed

+1024
-921
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+1024
-921
lines changed

cmd/cmd.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ func (*StandardLibraryHandler) GetAccountAvailableBalance(_ common.Address) (uin
261261
return 0, goerrors.New("accounts are not supported in this environment")
262262
}
263263

264-
func (*StandardLibraryHandler) CommitStorageTemporarily(_ *interpreter.Interpreter) error {
264+
func (*StandardLibraryHandler) CommitStorageTemporarily(_ interpreter.ValueTransferContext) error {
265265
// NO-OP
266266
return nil
267267
}

cmd/decode-state-values/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ type interpreterStorage struct {
235235
var _ interpreter.Storage = &interpreterStorage{}
236236

237237
func (i interpreterStorage) GetDomainStorageMap(
238-
_ *interpreter.Interpreter,
238+
_ interpreter.StorageMutationTracker,
239239
_ common.Address,
240240
_ common.StorageDomain,
241241
_ bool,

interpreter/account_storagemap.go

+19-19
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func (s *AccountStorageMap) DomainExists(domain common.StorageDomain) bool {
9595
// is created and inserted into account storage map with given domain as key.
9696
func (s *AccountStorageMap) GetDomain(
9797
gauge common.MemoryGauge,
98-
interpreter *Interpreter,
98+
storageMutationTracker StorageMutationTracker,
9999
domain common.StorageDomain,
100100
createIfNotExists bool,
101101
) *DomainStorageMap {
@@ -112,7 +112,7 @@ func (s *AccountStorageMap) GetDomain(
112112
// Create domain storage map if needed.
113113

114114
if createIfNotExists {
115-
return s.NewDomain(gauge, interpreter, domain)
115+
return s.NewDomain(gauge, storageMutationTracker, domain)
116116
}
117117

118118
return nil
@@ -128,10 +128,10 @@ func (s *AccountStorageMap) GetDomain(
128128
// NewDomain creates new domain storage map and inserts it to AccountStorageMap with given domain as key.
129129
func (s *AccountStorageMap) NewDomain(
130130
gauge common.MemoryGauge,
131-
interpreter *Interpreter,
131+
storageMutationTracker StorageMutationTracker,
132132
domain common.StorageDomain,
133133
) *DomainStorageMap {
134-
interpreter.recordStorageMutation()
134+
storageMutationTracker.RecordStorageMutation()
135135

136136
domainStorageMap := NewDomainStorageMap(gauge, s.orderedMap.Storage, s.orderedMap.Address())
137137

@@ -162,24 +162,24 @@ func (s *AccountStorageMap) NewDomain(
162162
// If the given storage map is non-nil, domain is added/updated.
163163
// Returns true if domain storage map previously existed at the given domain.
164164
func (s *AccountStorageMap) WriteDomain(
165-
interpreter *Interpreter,
165+
context ValueTransferContext,
166166
domain common.StorageDomain,
167167
domainStorageMap *DomainStorageMap,
168168
) (existed bool) {
169169
if domainStorageMap == nil {
170-
return s.removeDomain(interpreter, domain)
170+
return s.removeDomain(context, domain)
171171
}
172-
return s.setDomain(interpreter, domain, domainStorageMap)
172+
return s.setDomain(context, domain, domainStorageMap)
173173
}
174174

175175
// setDomain sets domain storage map in the account storage map and returns true if domain previously existed.
176176
// If the given domain already stores a domain storage map, it is overwritten.
177177
func (s *AccountStorageMap) setDomain(
178-
interpreter *Interpreter,
178+
context ValueTransferContext,
179179
domain common.StorageDomain,
180180
newDomainStorageMap *DomainStorageMap,
181181
) (existed bool) {
182-
interpreter.recordStorageMutation()
182+
context.RecordStorageMutation()
183183

184184
key := Uint64StorageMapKey(domain)
185185

@@ -199,13 +199,13 @@ func (s *AccountStorageMap) setDomain(
199199
existingDomainStorageMap := newDomainStorageMapWithAtreeStorable(s.orderedMap.Storage, existingValueStorable)
200200

201201
// Deep remove elements in domain storage map
202-
existingDomainStorageMap.DeepRemove(interpreter, true)
202+
existingDomainStorageMap.DeepRemove(context, true)
203203

204204
// Remove domain storage map slab
205-
interpreter.RemoveReferencedSlab(existingValueStorable)
205+
RemoveReferencedSlab(context, existingValueStorable)
206206
}
207207

208-
interpreter.MaybeValidateAtreeValue(s.orderedMap)
208+
context.MaybeValidateAtreeValue(s.orderedMap)
209209

210210
// NOTE: Don't call maybeValidateAtreeStorage() here because it is possible
211211
// that domain storage map is in the process of being migrated to account
@@ -215,8 +215,8 @@ func (s *AccountStorageMap) setDomain(
215215
}
216216

217217
// removeDomain removes domain storage map with given domain in account storage map, if it exists.
218-
func (s *AccountStorageMap) removeDomain(interpreter *Interpreter, domain common.StorageDomain) (existed bool) {
219-
interpreter.recordStorageMutation()
218+
func (s *AccountStorageMap) removeDomain(context ValueTransferContext, domain common.StorageDomain) (existed bool) {
219+
context.RecordStorageMutation()
220220

221221
key := Uint64StorageMapKey(domain)
222222

@@ -238,7 +238,7 @@ func (s *AccountStorageMap) removeDomain(interpreter *Interpreter, domain common
238238

239239
// NOTE: Key is just an atree.Value (Uint64AtreeValue), not an interpreter.Value,
240240
// so do not need (can) convert and not need to deep remove
241-
interpreter.RemoveReferencedSlab(existingKeyStorable)
241+
RemoveReferencedSlab(context, existingKeyStorable)
242242

243243
// Value
244244

@@ -248,14 +248,14 @@ func (s *AccountStorageMap) removeDomain(interpreter *Interpreter, domain common
248248
domainStorageMap := newDomainStorageMapWithAtreeStorable(s.orderedMap.Storage, existingValueStorable)
249249

250250
// Deep remove elements in domain storage map
251-
domainStorageMap.DeepRemove(interpreter, true)
251+
domainStorageMap.DeepRemove(context, true)
252252

253253
// Remove domain storage map slab
254-
interpreter.RemoveReferencedSlab(existingValueStorable)
254+
RemoveReferencedSlab(context, existingValueStorable)
255255
}
256256

257-
interpreter.MaybeValidateAtreeValue(s.orderedMap)
258-
interpreter.MaybeValidateAtreeStorage()
257+
context.MaybeValidateAtreeValue(s.orderedMap)
258+
context.MaybeValidateAtreeStorage()
259259

260260
return
261261
}

interpreter/account_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ type testAccountHandler struct {
7171
generateAccountID func(address common.Address) (uint64, error)
7272
getAccountBalance func(address common.Address) (uint64, error)
7373
getAccountAvailableBalance func(address common.Address) (uint64, error)
74-
commitStorageTemporarily func(inter *interpreter.Interpreter) error
74+
commitStorageTemporarily func(context interpreter.ValueTransferContext) error
7575
getStorageUsed func(address common.Address) (uint64, error)
7676
getStorageCapacity func(address common.Address) (uint64, error)
7777
validatePublicKey func(key *stdlib.PublicKey) error
@@ -160,11 +160,11 @@ func (t *testAccountHandler) GetAccountAvailableBalance(address common.Address)
160160
return t.getAccountAvailableBalance(address)
161161
}
162162

163-
func (t *testAccountHandler) CommitStorageTemporarily(inter *interpreter.Interpreter) error {
163+
func (t *testAccountHandler) CommitStorageTemporarily(context interpreter.ValueTransferContext) error {
164164
if t.commitStorageTemporarily == nil {
165165
panic(errors.NewUnexpectedError("unexpected call to CommitStorageTemporarily"))
166166
}
167-
return t.commitStorageTemporarily(inter)
167+
return t.commitStorageTemporarily(context)
168168
}
169169

170170
func (t *testAccountHandler) GetStorageUsed(address common.Address) (uint64, error) {
@@ -471,8 +471,8 @@ func testAccountWithErrorHandler(
471471
return baseActivation
472472
},
473473
ContractValueHandler: makeContractValueHandler(nil, nil, nil),
474-
AccountHandler: func(inter *interpreter.Interpreter, address interpreter.AddressValue) interpreter.Value {
475-
return stdlib.NewAccountValue(inter, nil, address)
474+
AccountHandler: func(context interpreter.FunctionCreationContext, address interpreter.AddressValue) interpreter.Value {
475+
return stdlib.NewAccountValue(context, nil, address)
476476
},
477477
},
478478
HandleCheckerError: checkerErrorHandler,
@@ -1333,7 +1333,7 @@ func TestInterpretAccountStorageFields(t *testing.T) {
13331333
const storageCapacity = 43
13341334

13351335
handler := &testAccountHandler{
1336-
commitStorageTemporarily: func(_ *interpreter.Interpreter) error {
1336+
commitStorageTemporarily: func(_ interpreter.ValueTransferContext) error {
13371337
return nil
13381338
},
13391339
getStorageUsed: func(_ common.Address) (uint64, error) {

interpreter/composite_value_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func testCompositeValue(t *testing.T, code string) *interpreter.Interpreter {
110110
map[string]interpreter.Value{
111111
"name": interpreter.NewUnmeteredStringValue("Apple"),
112112
},
113-
func(name string, _ *interpreter.Interpreter, _ interpreter.LocationRange) interpreter.Value {
113+
func(name string, _ interpreter.MemberAccessibleContext, _ interpreter.LocationRange) interpreter.Value {
114114
if name == "color" {
115115
return interpreter.NewUnmeteredStringValue("Red")
116116
}

interpreter/conversion.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ func ByteValueToByte(memoryGauge common.MemoryGauge, element Value, locationRang
9595
return b, nil
9696
}
9797

98-
func ByteSliceToByteArrayValue(interpreter *Interpreter, buf []byte) *ArrayValue {
98+
func ByteSliceToByteArrayValue(context ArrayCreationContext, buf []byte) *ArrayValue {
9999

100-
common.UseMemory(interpreter, common.NewBytesMemoryUsage(len(buf)))
100+
common.UseMemory(context, common.NewBytesMemoryUsage(len(buf)))
101101

102102
var values []Value
103103

@@ -110,7 +110,7 @@ func ByteSliceToByteArrayValue(interpreter *Interpreter, buf []byte) *ArrayValue
110110
}
111111

112112
return NewArrayValue(
113-
interpreter,
113+
context,
114114
EmptyLocationRange,
115115
ByteArrayStaticType,
116116
common.ZeroAddress,

interpreter/domain_storagemap.go

+20-20
Original file line numberDiff line numberDiff line change
@@ -146,19 +146,19 @@ func (s *DomainStorageMap) ReadValue(gauge common.MemoryGauge, key StorageMapKey
146146
// If the given value is nil, the key is removed.
147147
// If the given value is non-nil, the key is added/updated.
148148
// Returns true if a value previously existed at the given key.
149-
func (s *DomainStorageMap) WriteValue(interpreter *Interpreter, key StorageMapKey, value atree.Value) (existed bool) {
149+
func (s *DomainStorageMap) WriteValue(context ValueTransferContext, key StorageMapKey, value atree.Value) (existed bool) {
150150
if value == nil {
151-
return s.RemoveValue(interpreter, key)
151+
return s.RemoveValue(context, key)
152152
} else {
153-
return s.SetValue(interpreter, key, value)
153+
return s.SetValue(context, key, value)
154154
}
155155
}
156156

157157
// SetValue sets a value in the storage map.
158158
// If the given key already stores a value, it is overwritten.
159159
// Returns true if given key already exists and existing value is overwritten.
160-
func (s *DomainStorageMap) SetValue(interpreter *Interpreter, key StorageMapKey, value atree.Value) (existed bool) {
161-
interpreter.recordStorageMutation()
160+
func (s *DomainStorageMap) SetValue(context ValueTransferContext, key StorageMapKey, value atree.Value) (existed bool) {
161+
context.RecordStorageMutation()
162162

163163
existingStorable, err := s.orderedMap.Set(
164164
key.AtreeValueCompare,
@@ -172,20 +172,20 @@ func (s *DomainStorageMap) SetValue(interpreter *Interpreter, key StorageMapKey,
172172

173173
existed = existingStorable != nil
174174
if existed {
175-
existingValue := StoredValue(interpreter, existingStorable, interpreter.Storage())
176-
existingValue.DeepRemove(interpreter, true) // existingValue is standalone because it was overwritten in parent container.
177-
interpreter.RemoveReferencedSlab(existingStorable)
175+
existingValue := StoredValue(context, existingStorable, context.Storage())
176+
existingValue.DeepRemove(context, true) // existingValue is standalone because it was overwritten in parent container.
177+
RemoveReferencedSlab(context, existingStorable)
178178
}
179179

180-
interpreter.MaybeValidateAtreeValue(s.orderedMap)
181-
interpreter.MaybeValidateAtreeStorage()
180+
context.MaybeValidateAtreeValue(s.orderedMap)
181+
context.MaybeValidateAtreeStorage()
182182

183183
return
184184
}
185185

186186
// RemoveValue removes a value in the storage map, if it exists.
187-
func (s *DomainStorageMap) RemoveValue(interpreter *Interpreter, key StorageMapKey) (existed bool) {
188-
interpreter.recordStorageMutation()
187+
func (s *DomainStorageMap) RemoveValue(context ValueRemoveContext, key StorageMapKey) (existed bool) {
188+
context.RecordStorageMutation()
189189

190190
existingKeyStorable, existingValueStorable, err := s.orderedMap.Remove(
191191
key.AtreeValueCompare,
@@ -204,19 +204,19 @@ func (s *DomainStorageMap) RemoveValue(interpreter *Interpreter, key StorageMapK
204204

205205
// NOTE: Key is just an atree.Value, not an interpreter.Value,
206206
// so do not need (can) convert and not need to deep remove
207-
interpreter.RemoveReferencedSlab(existingKeyStorable)
207+
RemoveReferencedSlab(context, existingKeyStorable)
208208

209209
// Value
210210

211211
existed = existingValueStorable != nil
212212
if existed {
213-
existingValue := StoredValue(interpreter, existingValueStorable, interpreter.Storage())
214-
existingValue.DeepRemove(interpreter, true) // existingValue is standalone because it was removed from parent container.
215-
interpreter.RemoveReferencedSlab(existingValueStorable)
213+
existingValue := StoredValue(context, existingValueStorable, context.Storage())
214+
existingValue.DeepRemove(context, true) // existingValue is standalone because it was removed from parent container.
215+
RemoveReferencedSlab(context, existingValueStorable)
216216
}
217217

218-
interpreter.MaybeValidateAtreeValue(s.orderedMap)
219-
interpreter.MaybeValidateAtreeStorage()
218+
context.MaybeValidateAtreeValue(s.orderedMap)
219+
context.MaybeValidateAtreeStorage()
220220

221221
return
222222
}
@@ -250,13 +250,13 @@ func (s *DomainStorageMap) DeepRemove(context ValueRemoveContext, hasNoParentCon
250250

251251
// NOTE: Key is just an atree.Value, not an interpreter.Value,
252252
// so do not need (can) convert and not need to deep remove
253-
context.RemoveReferencedSlab(keyStorable)
253+
RemoveReferencedSlab(context, keyStorable)
254254

255255
// Value
256256

257257
value := StoredValue(context, valueStorable, storage)
258258
value.DeepRemove(context, false) // value is an element of v.dictionary because it is from PopIterate() callback.
259-
context.RemoveReferencedSlab(valueStorable)
259+
RemoveReferencedSlab(context, valueStorable)
260260
})
261261
if err != nil {
262262
panic(errors.NewExternalError(err))

0 commit comments

Comments
 (0)