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

Make capability ID mapping thread-safe #3085

Merged
merged 1 commit into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions migrations/capcons/capabilitymigration.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type CapabilityMigrationReporter interface {
// CapabilityValueMigration migrates all path capabilities to ID capabilities,
// using the path to ID capability controller mapping generated by LinkValueMigration.
type CapabilityValueMigration struct {
CapabilityIDs map[interpreter.AddressPath]interpreter.UInt64Value
CapabilityIDs *CapabilityIDMapping
Reporter CapabilityMigrationReporter
}

Expand Down Expand Up @@ -75,7 +75,7 @@ func (m *CapabilityValueMigration) Migrate(
oldCapability := value

capabilityAddressPath := oldCapability.AddressPath()
capabilityID, ok := m.CapabilityIDs[capabilityAddressPath]
capabilityID, ok := m.CapabilityIDs.Get(capabilityAddressPath)
if !ok {
if reporter != nil {
reporter.MissingCapabilityID(
Expand Down
5 changes: 2 additions & 3 deletions migrations/capcons/linkmigration.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type LinkMigrationReporter interface {

// LinkValueMigration migrates all links to capability controllers.
type LinkValueMigration struct {
CapabilityIDs map[interpreter.AddressPath]interpreter.UInt64Value
CapabilityIDs *CapabilityIDMapping
AccountIDGenerator stdlib.AccountIDGenerator
Reporter LinkMigrationReporter
}
Expand Down Expand Up @@ -182,8 +182,7 @@ func (m *LinkValueMigration) Migrate(
// Record new capability ID in source path mapping.
// The mapping is used later for migrating path capabilities to ID capabilities,
// see CapabilityMigration.

m.CapabilityIDs[addressPath] = capabilityID
m.CapabilityIDs.Record(addressPath, capabilityID)

if reporter != nil {
reporter.MigratedLink(addressPath, capabilityID)
Expand Down
52 changes: 52 additions & 0 deletions migrations/capcons/mapping.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Cadence - The resource-oriented smart contract programming language
*
* Copyright Dapper Labs, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package capcons

import (
"sync"

"github.com/onflow/cadence/runtime/interpreter"
)

type CapabilityIDMapping struct {
// CapabilityIDs' maps common.Address to map[interpreter.PathValue]interpreter.UInt64Value
capabilityIDs sync.Map
}

func (m *CapabilityIDMapping) Record(addressPath interpreter.AddressPath, capabilityID interpreter.UInt64Value) {
var accountCapabilityIDs map[interpreter.PathValue]interpreter.UInt64Value
rawAccountCapabilityIDs, ok := m.capabilityIDs.Load(addressPath.Address)
if ok {
accountCapabilityIDs = rawAccountCapabilityIDs.(map[interpreter.PathValue]interpreter.UInt64Value)
} else {
accountCapabilityIDs = map[interpreter.PathValue]interpreter.UInt64Value{}
m.capabilityIDs.Store(addressPath.Address, accountCapabilityIDs)
}
accountCapabilityIDs[addressPath.Path] = capabilityID
}

func (m *CapabilityIDMapping) Get(addressPath interpreter.AddressPath) (interpreter.UInt64Value, bool) {
rawAccountCapabilityIDs, ok := m.capabilityIDs.Load(addressPath.Address)
if !ok {
return 0, false
}
accountCapabilityIDs := rawAccountCapabilityIDs.(map[interpreter.PathValue]interpreter.UInt64Value)
capabilityID, ok := accountCapabilityIDs[addressPath.Path]
return capabilityID, ok
}
4 changes: 2 additions & 2 deletions migrations/capcons/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ func testPathCapabilityValueMigration(

migration := migrations.NewStorageMigration(inter, storage)

capabilityIDs := map[interpreter.AddressPath]interpreter.UInt64Value{}
capabilityIDs := &CapabilityIDMapping{}

reporter := &testMigrationReporter{}

Expand Down Expand Up @@ -1325,7 +1325,7 @@ func testLinkMigration(

migration := migrations.NewStorageMigration(inter, storage)

capabilityIDs := map[interpreter.AddressPath]interpreter.UInt64Value{}
capabilityIDs := &CapabilityIDMapping{}

reporter := &testMigrationReporter{}

Expand Down
Loading