Skip to content

Commit

Permalink
Merge pull request #3378 from onflow/bastian/update-atree-register-in…
Browse files Browse the repository at this point in the history
…lining-v1.0-4
  • Loading branch information
turbolent authored May 28, 2024
2 parents 72e083b + 90d6bba commit a187365
Show file tree
Hide file tree
Showing 28 changed files with 2,054 additions and 570 deletions.
20 changes: 20 additions & 0 deletions encoding/json/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -1369,6 +1369,26 @@ func (d *Decoder) decodeTypeValue(valueJSON any) cadence.TypeValue {
func (d *Decoder) decodeCapability(valueJSON any) cadence.Capability {
obj := toObject(valueJSON)

if d.backwardsCompatible {
if _, hasKey := obj[idKey]; !hasKey {
path, ok := d.DecodeJSON(obj.Get(pathKey)).(cadence.Path)
if !ok {
panic(errors.NewDefaultUserError("invalid capability: missing or invalid path"))
}

return cadence.NewDeprecatedMeteredPathCapability(
d.gauge,
d.decodeAddress(obj.Get(addressKey)),
path,
d.decodeType(obj.Get(borrowTypeKey), typeDecodingResults{}),
)
}
} else {
if _, hasKey := obj[pathKey]; hasKey {
panic(errors.NewDefaultUserError("invalid capability: path is not supported"))
}
}

return cadence.NewMeteredCapability(
d.gauge,
d.decodeUInt64(obj.Get(idKey)),
Expand Down
147 changes: 124 additions & 23 deletions encoding/json/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2798,6 +2798,99 @@ func TestEncodeCapability(t *testing.T) {
)
}

func TestDecodeCapability(t *testing.T) {

t.Run("with backwards compatibility", func(t *testing.T) {
t.Parallel()

testDecode(
t,
// language=json
`
{
"type": "Capability",
"value": {
"borrowType": {
"kind": "Int"
},
"address": "0x0000000102030405",
"id": "6"
}
}
`,
cadence.NewCapability(
6,
cadence.BytesToAddress([]byte{1, 2, 3, 4, 5}),
cadence.IntType,
),
WithBackwardsCompatibility(),
)
})

t.Run("with backwards compatibility on a deprecated Path Capabliity", func(t *testing.T) {
t.Parallel()

testDecode(
t,
// language=json
`
{
"type": "Capability",
"value": {
"path": {
"type": "Path",
"value": {
"domain": "public",
"identifier": "foo"
}
},
"borrowType": {
"kind": "Int"
},
"address": "0x0000000102030405"
}
}
`,
cadence.NewDeprecatedPathCapability( //nolint:staticcheck
cadence.BytesToAddress([]byte{1, 2, 3, 4, 5}),
cadence.Path{
Domain: common.PathDomainPublic,
Identifier: "foo",
},
cadence.IntType,
),
WithBackwardsCompatibility(),
)
})

t.Run("deprecated Path Capability without backwards compatibility", func(t *testing.T) {
t.Parallel()

_, err := Decode(nil, []byte(
`
{
"type": "Capability",
"value": {
"path": {
"type": "Path",
"value": {
"domain": "public",
"identifier": "foo"
}
},
"borrowType": {
"kind": "Int"
},
"address": "0x0000000102030405"
}
}
`,
))
require.Error(t, err)

})
}

func TestDecodeFixedPoints(t *testing.T) {

t.Parallel()
Expand Down Expand Up @@ -3123,29 +3216,37 @@ func TestDecodeDeprecatedTypes(t *testing.T) {

t.Parallel()

// Decode with panic if restriction is not supported
require.Panics(t, func() {
_, err := Decode(nil, []byte(`
{
"type": "Type",
"value": {
"staticType": {
"kind": "Restriction",
"typeID": "Int{String}",
"type": {
"kind": "Int"
},
"restrictions": [
{
"kind": "String"
}
]
}
}
}
`))
require.NoError(t, err)
},
testDecode(
t,
// language=json
`
{
"type": "Type",
"value": {
"staticType": {
"kind": "Restriction",
"typeID": "Int{String}",
"type": {
"kind": "Int"
},
"restrictions": [
{
"kind": "String"
}
]
}
}
}
`,
cadence.TypeValue{
StaticType: &cadence.DeprecatedRestrictedType{
Restrictions: []cadence.Type{
cadence.StringType,
},
Type: cadence.IntType,
},
},
WithBackwardsCompatibility(),
)
})
}
Expand Down
66 changes: 0 additions & 66 deletions migrations/cache.go

This file was deleted.

25 changes: 2 additions & 23 deletions migrations/entitlements/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,14 @@ import (
)

type EntitlementsMigration struct {
Interpreter *interpreter.Interpreter
migratedTypeCache migrations.StaticTypeCache
Interpreter *interpreter.Interpreter
}

var _ migrations.ValueMigration = EntitlementsMigration{}

func NewEntitlementsMigration(inter *interpreter.Interpreter) EntitlementsMigration {
staticTypeCache := migrations.NewDefaultStaticTypeCache()
return NewEntitlementsMigrationWithCache(inter, staticTypeCache)
}

func NewEntitlementsMigrationWithCache(
inter *interpreter.Interpreter,
migratedTypeCache migrations.StaticTypeCache,
) EntitlementsMigration {
return EntitlementsMigration{
Interpreter: inter,
migratedTypeCache: migratedTypeCache,
Interpreter: inter,
}
}

Expand Down Expand Up @@ -84,17 +74,6 @@ func (m EntitlementsMigration) ConvertToEntitledType(
}

inter := m.Interpreter
migratedTypeCache := m.migratedTypeCache

staticTypeID := staticType.ID()

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

defer func() {
migratedTypeCache.Set(staticTypeID, resultType, err)
}()

switch t := staticType.(type) {
case *interpreter.ReferenceStaticType:
Expand Down
29 changes: 1 addition & 28 deletions migrations/statictypes/statictype_migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
type StaticTypeMigration struct {
compositeTypeConverter CompositeTypeConverterFunc
interfaceTypeConverter InterfaceTypeConverterFunc
migratedTypeCache migrations.StaticTypeCache
}

type CompositeTypeConverterFunc func(*interpreter.CompositeStaticType) interpreter.StaticType
Expand All @@ -40,14 +39,7 @@ type InterfaceTypeConverterFunc func(*interpreter.InterfaceStaticType) interpret
var _ migrations.ValueMigration = &StaticTypeMigration{}

func NewStaticTypeMigration() *StaticTypeMigration {
staticTypeCache := migrations.NewDefaultStaticTypeCache()
return NewStaticTypeMigrationWithCache(staticTypeCache)
}

func NewStaticTypeMigrationWithCache(migratedTypeCache migrations.StaticTypeCache) *StaticTypeMigration {
return &StaticTypeMigration{
migratedTypeCache: migratedTypeCache,
}
return &StaticTypeMigration{}
}

func (m *StaticTypeMigration) WithCompositeTypeConverter(converterFunc CompositeTypeConverterFunc) *StaticTypeMigration {
Expand Down Expand Up @@ -171,25 +163,6 @@ func (m *StaticTypeMigration) maybeConvertStaticType(
) (
resultType interpreter.StaticType,
) {
// Consult the cache and cache the result at the root of the migration,
// i.e. when the parent type is nil.
//
// Parse of the migration, e.g. the intersection type migration depends on the parent type.
// For example, `{Ts}` in `&{Ts}` is migrated differently from `{Ts}`.

if parentType == nil {
migratedTypeCache := m.migratedTypeCache
staticTypeID := staticType.ID()

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

defer func() {
migratedTypeCache.Set(staticTypeID, resultType, nil)
}()
}

switch staticType := staticType.(type) {
case *interpreter.ConstantSizedStaticType:
convertedType := m.maybeConvertStaticType(staticType.Type, staticType)
Expand Down

Large diffs are not rendered by default.

Loading

0 comments on commit a187365

Please sign in to comment.