Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Track resources with atree.ID instead of StorageID #2627

Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/fxamacker/cbor/v2 v2.4.1-0.20230228173756-c0c9f774e40c
github.com/go-test/deep v1.1.0
github.com/leanovate/gopter v0.2.9
github.com/onflow/atree v0.6.1-0.20230629205511-5b7b45a566a9
github.com/onflow/atree v0.6.1-0.20230629202201-116c95986e54
github.com/rivo/uniseg v0.4.4
github.com/schollz/progressbar/v3 v3.13.1
github.com/stretchr/testify v1.8.4
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ github.com/mattn/go-tty v0.0.4/go.mod h1:u5GGXBtZU6RQoKV8gY5W6UhMudbR5vXnUe7j3px
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ=
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
github.com/onflow/atree v0.6.1-0.20230629205511-5b7b45a566a9 h1:mffWKRKGrBq5NhCWplOox33eW+gf2lcgjvdI8aeCjGk=
github.com/onflow/atree v0.6.1-0.20230629205511-5b7b45a566a9/go.mod h1:7YNAyCd5JENq+NzH+fR1ABUZVzbSq9dkt0+5fZH3L2A=
github.com/onflow/atree v0.6.1-0.20230629202201-116c95986e54 h1:sYpk0UK7vHH907N3c/OIjrUq2PKtU7CcTi2O9YvKIYs=
github.com/onflow/atree v0.6.1-0.20230629202201-116c95986e54/go.mod h1:7YNAyCd5JENq+NzH+fR1ABUZVzbSq9dkt0+5fZH3L2A=
github.com/pkg/term v1.2.0-beta.2 h1:L3y/h2jkuBVFdWiJvNfYfKmzcCnILw7mJWm2JQuMppw=
github.com/pkg/term v1.2.0-beta.2/go.mod h1:E25nymQcrSllhX42Ok8MRm1+hyBdHY0dCeiKZ9jpNGw=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
38 changes: 19 additions & 19 deletions runtime/interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ type Storage interface {
CheckHealth() error
}

type ReferencedResourceKindedValues map[atree.StorageID]map[ReferenceTrackedResourceKindedValue]struct{}
type ReferencedResourceKindedValues map[atree.ID]map[ReferenceTrackedResourceKindedValue]struct{}

type Interpreter struct {
Location common.Location
Expand Down Expand Up @@ -5100,12 +5100,12 @@ func (interpreter *Interpreter) ValidateAtreeValue(value atree.Value) {

func (interpreter *Interpreter) maybeTrackReferencedResourceKindedValue(value Value) {
if value, ok := value.(ReferenceTrackedResourceKindedValue); ok {
interpreter.trackReferencedResourceKindedValue(value.StorageID(), value)
interpreter.trackReferencedResourceKindedValue(value.ID(), value)
}
}

func (interpreter *Interpreter) trackReferencedResourceKindedValue(
id atree.StorageID,
id atree.ID,
value ReferenceTrackedResourceKindedValue,
) {
values := interpreter.SharedState.referencedResourceKindedValues[id]
Expand All @@ -5117,20 +5117,20 @@ func (interpreter *Interpreter) trackReferencedResourceKindedValue(
}

func (interpreter *Interpreter) updateReferencedResource(
currentStorageID atree.StorageID,
newStorageID atree.StorageID,
currentID atree.ID,
newID atree.ID,
updateFunc func(value ReferenceTrackedResourceKindedValue),
) {
values := interpreter.SharedState.referencedResourceKindedValues[currentStorageID]
values := interpreter.SharedState.referencedResourceKindedValues[currentID]
if values == nil {
return
}
for value := range values { //nolint:maprange
updateFunc(value)
}
if newStorageID != currentStorageID {
interpreter.SharedState.referencedResourceKindedValues[newStorageID] = values
interpreter.SharedState.referencedResourceKindedValues[currentStorageID] = nil
if newID != currentID {
interpreter.SharedState.referencedResourceKindedValues[newID] = values
interpreter.SharedState.referencedResourceKindedValues[currentID] = nil
}
}

Expand Down Expand Up @@ -5377,8 +5377,8 @@ func (interpreter *Interpreter) idCapabilityCheckFunction(
)
}

func (interpreter *Interpreter) validateMutation(storageID atree.StorageID, locationRange LocationRange) {
_, present := interpreter.SharedState.containerValueIteration[storageID]
func (interpreter *Interpreter) validateMutation(id atree.ID, locationRange LocationRange) {
_, present := interpreter.SharedState.containerValueIteration[id]
if !present {
return
}
Expand All @@ -5387,32 +5387,32 @@ func (interpreter *Interpreter) validateMutation(storageID atree.StorageID, loca
})
}

func (interpreter *Interpreter) withMutationPrevention(storageID atree.StorageID, f func()) {
oldIteration, present := interpreter.SharedState.containerValueIteration[storageID]
interpreter.SharedState.containerValueIteration[storageID] = struct{}{}
func (interpreter *Interpreter) withMutationPrevention(id atree.ID, f func()) {
oldIteration, present := interpreter.SharedState.containerValueIteration[id]
interpreter.SharedState.containerValueIteration[id] = struct{}{}

f()

if !present {
delete(interpreter.SharedState.containerValueIteration, storageID)
delete(interpreter.SharedState.containerValueIteration, id)
} else {
interpreter.SharedState.containerValueIteration[storageID] = oldIteration
interpreter.SharedState.containerValueIteration[id] = oldIteration
}
}

func (interpreter *Interpreter) withResourceDestruction(
storageID atree.StorageID,
id atree.ID,
locationRange LocationRange,
f func(),
) {
_, exists := interpreter.SharedState.destroyedResources[storageID]
_, exists := interpreter.SharedState.destroyedResources[id]
if exists {
panic(DestroyedResourceError{
LocationRange: locationRange,
})
}

interpreter.SharedState.destroyedResources[storageID] = struct{}{}
interpreter.SharedState.destroyedResources[id] = struct{}{}

f()
}
6 changes: 3 additions & 3 deletions runtime/interpreter/interpreter_expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -937,7 +937,7 @@ func (interpreter *Interpreter) visitInvocationExpressionWithImplicitArgument(in
if boundFunction, ok := function.(BoundFunctionValue); ok && boundFunction.Self != nil {
self := *boundFunction.Self
if resource, ok := self.(ReferenceTrackedResourceKindedValue); ok {
storageID := resource.StorageID()
storageID := resource.ID()
interpreter.trackReferencedResourceKindedValue(storageID, resource)
}
}
Expand Down Expand Up @@ -1290,7 +1290,7 @@ func (interpreter *Interpreter) VisitAttachExpression(attachExpression *ast.Atta
base,
interpreter.MustSemaTypeOfValue(base).(*sema.CompositeType),
)
interpreter.trackReferencedResourceKindedValue(base.StorageID(), base)
interpreter.trackReferencedResourceKindedValue(base.ID(), base)

attachment, ok := interpreter.visitInvocationExpressionWithImplicitArgument(
attachExpression.Attachment,
Expand All @@ -1302,7 +1302,7 @@ func (interpreter *Interpreter) VisitAttachExpression(attachExpression *ast.Atta
}

// Because `self` in attachments is a reference, we need to track the attachment if it's a resource
interpreter.trackReferencedResourceKindedValue(attachment.StorageID(), attachment)
interpreter.trackReferencedResourceKindedValue(attachment.ID(), attachment)

base = base.Transfer(
interpreter,
Expand Down
10 changes: 5 additions & 5 deletions runtime/interpreter/sharedstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ type SharedState struct {
storageMutatedDuringIteration bool
CapabilityControllerIterations map[AddressPath]int
MutationDuringCapabilityControllerIteration bool
containerValueIteration map[atree.StorageID]struct{}
destroyedResources map[atree.StorageID]struct{}
containerValueIteration map[atree.ID]struct{}
destroyedResources map[atree.ID]struct{}
}

func NewSharedState(config *Config) *SharedState {
Expand All @@ -59,11 +59,11 @@ func NewSharedState(config *Config) *SharedState {
},
inStorageIteration: false,
storageMutatedDuringIteration: false,
referencedResourceKindedValues: map[atree.StorageID]map[ReferenceTrackedResourceKindedValue]struct{}{},
referencedResourceKindedValues: map[atree.ID]map[ReferenceTrackedResourceKindedValue]struct{}{},
resourceVariables: map[ResourceKindedValue]*Variable{},
CapabilityControllerIterations: map[AddressPath]int{},
containerValueIteration: map[atree.StorageID]struct{}{},
destroyedResources: map[atree.StorageID]struct{}{},
containerValueIteration: map[atree.ID]struct{}{},
destroyedResources: map[atree.ID]struct{}{},
}
}

Expand Down
Loading