Skip to content

Commit

Permalink
Merge pull request #3351 from onflow/fxamacker/sync-atree-register-in…
Browse files Browse the repository at this point in the history
…lining-v1.0

Sync feature/atree-register-inlining-v1.0 with master
  • Loading branch information
fxamacker authored May 15, 2024
2 parents ab00c49 + 986cb08 commit 72871ab
Show file tree
Hide file tree
Showing 118 changed files with 4,768 additions and 2,754 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/downstream.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ jobs:
uses: actions/checkout@v3
with:
repository: 'onflow/flow-go'
ref: ${{ github.base_ref == 'master' && 'feature/stable-cadence' || 'master' }}

- name: Setup Go
uses: actions/setup-go@v3
Expand All @@ -53,7 +52,6 @@ jobs:
uses: actions/checkout@v3
with:
repository: 'onflow/flow-emulator'
ref: ${{ github.base_ref == 'master' && 'master' || 'release/v0' }}

- name: Setup Go
uses: actions/setup-go@v3
Expand Down
30 changes: 23 additions & 7 deletions migrations/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,18 @@ import (
"github.com/onflow/cadence/runtime/interpreter"
)

type CachedStaticType struct {
StaticType interpreter.StaticType
Error error
}

type StaticTypeCache interface {
Get(typeID interpreter.TypeID) (interpreter.StaticType, bool)
Set(typeID interpreter.TypeID, ty interpreter.StaticType)
Get(typeID interpreter.TypeID) (CachedStaticType, bool)
Set(
typeID interpreter.TypeID,
staticType interpreter.StaticType,
err error,
)
}

type DefaultStaticTypeCache struct {
Expand All @@ -37,14 +46,21 @@ func NewDefaultStaticTypeCache() *DefaultStaticTypeCache {
return &DefaultStaticTypeCache{}
}

func (c *DefaultStaticTypeCache) Get(typeID interpreter.TypeID) (interpreter.StaticType, bool) {
func (c *DefaultStaticTypeCache) Get(typeID interpreter.TypeID) (CachedStaticType, bool) {
v, ok := c.entries.Load(typeID)
if !ok {
return nil, false
return CachedStaticType{}, false
}
return v.(interpreter.StaticType), true
return v.(CachedStaticType), true
}

func (c *DefaultStaticTypeCache) Set(typeID interpreter.TypeID, ty interpreter.StaticType) {
c.entries.Store(typeID, ty)
func (c *DefaultStaticTypeCache) Set(
typeID interpreter.TypeID,
staticType interpreter.StaticType,
err error,
) {
c.entries.Store(typeID, CachedStaticType{
StaticType: staticType,
Error: err,
})
}
67 changes: 31 additions & 36 deletions migrations/entitlements/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (m EntitlementsMigration) ConvertToEntitledType(
staticType interpreter.StaticType,
) (
resultType interpreter.StaticType,
conversionErr error,
err error,
) {
if staticType == nil {
return nil, nil
Expand All @@ -89,13 +89,11 @@ func (m EntitlementsMigration) ConvertToEntitledType(
staticTypeID := staticType.ID()

if migratedType, exists := migratedTypeCache.Get(staticTypeID); exists {
return migratedType, nil
return migratedType.StaticType, migratedType.Error
}

defer func() {
if resultType != nil && conversionErr == nil {
migratedTypeCache.Set(staticTypeID, resultType)
}
migratedTypeCache.Set(staticTypeID, resultType, err)
}()

switch t := staticType.(type) {
Expand All @@ -105,8 +103,7 @@ func (m EntitlementsMigration) ConvertToEntitledType(

convertedReferencedType, err := m.ConvertToEntitledType(referencedType)
if err != nil {
conversionErr = err
return
return nil, err
}

var returnNew bool
Expand Down Expand Up @@ -164,96 +161,94 @@ func (m EntitlementsMigration) ConvertToEntitledType(
}

if returnNew {
resultType = interpreter.NewReferenceStaticType(nil, auth, referencedType)
return
return interpreter.NewReferenceStaticType(nil, auth, referencedType), nil
}

case *interpreter.CapabilityStaticType:
convertedBorrowType, err := m.ConvertToEntitledType(t.BorrowType)
if err != nil {
conversionErr = err
return
return nil, err
}

if convertedBorrowType != nil {
resultType = interpreter.NewCapabilityStaticType(nil, convertedBorrowType)
return
return interpreter.NewCapabilityStaticType(nil, convertedBorrowType), nil
}

case *interpreter.VariableSizedStaticType:
elementType := t.Type

convertedElementType, err := m.ConvertToEntitledType(elementType)
if err != nil {
conversionErr = err
return
return nil, err
}

if convertedElementType != nil {
resultType = interpreter.NewVariableSizedStaticType(nil, convertedElementType)
return
return interpreter.NewVariableSizedStaticType(nil, convertedElementType), nil
}

case *interpreter.ConstantSizedStaticType:
elementType := t.Type

convertedElementType, err := m.ConvertToEntitledType(elementType)
if err != nil {
conversionErr = err
return
return nil, err
}

if convertedElementType != nil {
resultType = interpreter.NewConstantSizedStaticType(nil, convertedElementType, t.Size)
return
return interpreter.NewConstantSizedStaticType(nil, convertedElementType, t.Size), nil
}

case *interpreter.DictionaryStaticType:
keyType := t.KeyType

convertedKeyType, err := m.ConvertToEntitledType(keyType)
if err != nil {
conversionErr = err
return
return nil, err
}

valueType := t.ValueType

convertedValueType, err := m.ConvertToEntitledType(valueType)
if err != nil {
conversionErr = err
return
return nil, err
}

if convertedKeyType != nil {
if convertedValueType != nil {
resultType = interpreter.NewDictionaryStaticType(nil, convertedKeyType, convertedValueType)
return
return interpreter.NewDictionaryStaticType(
nil,
convertedKeyType,
convertedValueType,
), nil
} else {
resultType = interpreter.NewDictionaryStaticType(nil, convertedKeyType, valueType)
return
return interpreter.NewDictionaryStaticType(
nil,
convertedKeyType,
valueType,
), nil
}
} else if convertedValueType != nil {
resultType = interpreter.NewDictionaryStaticType(nil, keyType, convertedValueType)
return
return interpreter.NewDictionaryStaticType(
nil,
keyType,
convertedValueType,
), nil
}

case *interpreter.OptionalStaticType:
innerType := t.Type

convertedInnerType, err := m.ConvertToEntitledType(innerType)
if err != nil {
conversionErr = err
return
return nil, err
}

if convertedInnerType != nil {
resultType = interpreter.NewOptionalStaticType(nil, convertedInnerType)
return
return interpreter.NewOptionalStaticType(nil, convertedInnerType), nil
}
}

return
return nil, nil
}

// ConvertValueToEntitlements converts the input value into a version compatible with the new entitlements feature,
Expand Down
22 changes: 19 additions & 3 deletions migrations/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type StorageMigration struct {
name string
address common.Address
dictionaryKeyConflicts int
stacktraceEnabled bool
}

func NewStorageMigration(
Expand All @@ -79,8 +80,13 @@ func NewStorageMigration(
}, nil
}

func (m *StorageMigration) WithErrorStacktrace(stacktraceEnabled bool) *StorageMigration {
m.stacktraceEnabled = stacktraceEnabled
return m
}

func (m *StorageMigration) Commit() error {
return m.storage.Commit(m.interpreter, false)
return m.storage.NondeterministicCommit(m.interpreter, false)
}

func (m *StorageMigration) Migrate(migrator StorageMapKeyMigrator) {
Expand Down Expand Up @@ -185,12 +191,17 @@ func (m *StorageMigration) MigrateNestedValue(
err = fmt.Errorf("%v", r)
}

var stack []byte
if m.stacktraceEnabled {
stack = debug.Stack()
}

err = StorageMigrationError{
StorageKey: storageKey,
StorageMapKey: storageMapKey,
Migration: m.name,
Err: err,
Stack: debug.Stack(),
Stack: stack,
}

if reporter != nil {
Expand Down Expand Up @@ -777,12 +788,17 @@ func (m *StorageMigration) migrate(
err = fmt.Errorf("%v", r)
}

var stack []byte
if m.stacktraceEnabled {
stack = debug.Stack()
}

err = StorageMigrationError{
StorageKey: storageKey,
StorageMapKey: storageMapKey,
Migration: migration.Name(),
Err: err,
Stack: debug.Stack(),
Stack: stack,
}
}
}()
Expand Down
2 changes: 2 additions & 0 deletions migrations/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1703,6 +1703,8 @@ func TestMigrationPanic(t *testing.T) {

reporter := newTestReporter()

migration = migration.WithErrorStacktrace(true)

migration.Migrate(
migration.NewValueMigrationsPathMigrator(
reporter,
Expand Down
5 changes: 3 additions & 2 deletions migrations/statictypes/account_type_migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,14 +506,15 @@ func TestAccountTypeInTypeValueMigration(t *testing.T) {
StorageMapKey: storageMapKey,
}: {},
},
reporter.migrated)
reporter.migrated,
)
}

// Assert the migrated values.

storageMap := storage.GetStorageMap(account, pathDomain.Identifier(), false)
require.NotNil(t, storageMap)
require.Equal(t, storageMap.Count(), uint64(1))
require.Equal(t, uint64(1), storageMap.Count())

value := storageMap.ReadValue(nil, storageMapKey)

Expand Down
Loading

0 comments on commit 72871ab

Please sign in to comment.