From 6daa89aad39f70b2cbcebf0944ba3e7c8ef765a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20M=C3=BCller?= Date: Fri, 8 Mar 2024 08:49:19 -0800 Subject: [PATCH 1/3] update to latest version of atree --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index bb40c08980..829b7027e2 100644 --- a/go.mod +++ b/go.mod @@ -13,7 +13,7 @@ require ( github.com/kr/pretty v0.3.1 github.com/leanovate/gopter v0.2.9 github.com/logrusorgru/aurora/v4 v4.0.0 - github.com/onflow/atree v0.6.1-0.20230711151834-86040b30171f + github.com/onflow/atree v0.6.1-0.20240308163425-dc825c20b1a2 github.com/rivo/uniseg v0.4.4 github.com/schollz/progressbar/v3 v3.13.1 github.com/stretchr/testify v1.8.4 diff --git a/go.sum b/go.sum index 7155d2f2ff..f4d2868aff 100644 --- a/go.sum +++ b/go.sum @@ -68,8 +68,8 @@ github.com/mattn/go-tty v0.0.3/go.mod h1:ihxohKRERHTVzN+aSVRwACLCeqIoZAWpoICkkvr 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.20230711151834-86040b30171f h1:Z8/PgTqOgOg02MTRpTBYO2k16FE6z4wEOtaC2WBR9Xo= -github.com/onflow/atree v0.6.1-0.20230711151834-86040b30171f/go.mod h1:xvP61FoOs95K7IYdIYRnNcYQGf4nbF/uuJ0tHf4DRuM= +github.com/onflow/atree v0.6.1-0.20240308163425-dc825c20b1a2 h1:jJLDswfAVB0bHCu1y1FPdKukPcTNmN+jYEX9S9phbv0= +github.com/onflow/atree v0.6.1-0.20240308163425-dc825c20b1a2/go.mod h1:xvP61FoOs95K7IYdIYRnNcYQGf4nbF/uuJ0tHf4DRuM= github.com/onflow/crypto v0.25.0 h1:BeWbLsh3ZD13Ej+Uky6kg1PL1ZIVBDVX+2MVBNwqddg= github.com/onflow/crypto v0.25.0/go.mod h1:C8FbaX0x8y+FxWjbkHy0Q4EASCDR9bSPWZqlpCLYyVI= github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= From abd685992c90cfbf4ad384b004c4338dda09a685 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20M=C3=BCller?= Date: Fri, 8 Mar 2024 08:50:16 -0800 Subject: [PATCH 2/3] replace ArrayValue/DictionaryValue NewWithType with SetType --- runtime/interpreter/value.go | 120 ++++-------------------------- runtime/interpreter/value_test.go | 83 --------------------- 2 files changed, 13 insertions(+), 190 deletions(-) diff --git a/runtime/interpreter/value.go b/runtime/interpreter/value.go index 32ec72332f..4c1e3907d3 100644 --- a/runtime/interpreter/value.go +++ b/runtime/interpreter/value.go @@ -3518,55 +3518,12 @@ func (v *ArrayValue) ToConstantSized( ) } -func (v *ArrayValue) NewWithType( - inter *Interpreter, - locationRange LocationRange, - newType ArrayStaticType, -) *ArrayValue { - - newArray := NewArrayValue( - inter, - locationRange, - newType, - v.GetOwner(), - ) - - storage := inter.Storage() - - count := v.Count() - - for index := 0; index < count; index++ { - - storable := v.RemoveWithoutTransfer( - inter, - locationRange, - // NOTE: always removing first element, - // until original array is empty - 0, - ) - - if storable == nil { - panic(errors.NewUnreachableError()) - } - - if v.Count() != count-index-1 { - panic(errors.NewUnreachableError()) - } - - newValue, err := storable.StoredValue(storage) - if err != nil { - panic(err) - } - - newArray.InsertWithoutTransfer( - inter, - locationRange, - index, - newValue, - ) +func (v *ArrayValue) SetType(staticType ArrayStaticType) { + v.Type = staticType + err := v.array.SetType(staticType) + if err != nil { + panic(errors.NewExternalError(err)) } - - return newArray } // NumberValue @@ -18667,65 +18624,6 @@ func newDictionaryValueFromAtreeMap( } } -func (v *DictionaryValue) NewWithType( - inter *Interpreter, - locationRange LocationRange, - newType *DictionaryStaticType, -) *DictionaryValue { - - newDictionary := NewDictionaryValueWithAddress( - inter, - locationRange, - newType, - v.GetOwner(), - ) - - var keys []atree.Value - - iterator := v.Iterator() - - for { - key := iterator.NextKeyUnconverted() - if key == nil { - break - } - - keys = append(keys, key) - } - - storage := inter.Storage() - - for _, key := range keys { - existingKeyStorable, existingValueStorable := v.RemoveWithoutTransfer( - inter, - locationRange, - key, - ) - if existingKeyStorable == nil || existingValueStorable == nil { - panic(errors.NewUnreachableError()) - } - - newKey, err := existingKeyStorable.StoredValue(storage) - if err != nil { - panic(err) - } - - newValue, err := existingValueStorable.StoredValue(storage) - if err != nil { - panic(err) - } - - newDictionary.InsertWithoutTransfer( - inter, - locationRange, - newKey, - newValue, - ) - } - - return newDictionary -} - var _ Value = &DictionaryValue{} var _ atree.Value = &DictionaryValue{} var _ EquatableValue = &DictionaryValue{} @@ -19903,6 +19801,14 @@ func (v *DictionaryValue) IsResourceKinded(interpreter *Interpreter) bool { return *v.isResourceKinded } +func (v *DictionaryValue) SetType(staticType *DictionaryStaticType) { + v.Type = staticType + err := v.dictionary.SetType(staticType) + if err != nil { + panic(errors.NewExternalError(err)) + } +} + // OptionalValue type OptionalValue interface { diff --git a/runtime/interpreter/value_test.go b/runtime/interpreter/value_test.go index 38fd3b5355..2fdb809b6d 100644 --- a/runtime/interpreter/value_test.go +++ b/runtime/interpreter/value_test.go @@ -4187,86 +4187,3 @@ func TestValue_ConformsToStaticType(t *testing.T) { }) } - -func TestDictionaryValue_NewWithType(t *testing.T) { - - t.Parallel() - - inter := newTestInterpreter(t) - - address := common.Address{0x1} - - newDictionaryValue := func(dictionaryType *DictionaryStaticType) *DictionaryValue { - return NewDictionaryValueWithAddress( - inter, - EmptyLocationRange, - dictionaryType, - address, - NewUnmeteredStringValue("a"), - NewUnmeteredIntValueFromInt64(1), - NewUnmeteredStringValue("b"), - NewUnmeteredIntValueFromInt64(2), - NewUnmeteredStringValue("c"), - NewUnmeteredIntValueFromInt64(3), - ) - } - - oldType := &DictionaryStaticType{ - KeyType: PrimitiveStaticTypeString, - ValueType: PrimitiveStaticTypeAnyStruct, - } - - newType := &DictionaryStaticType{ - KeyType: PrimitiveStaticTypeString, - ValueType: PrimitiveStaticTypeInt, - } - - require.True(t, - newDictionaryValue(oldType). - NewWithType(inter, EmptyLocationRange, newType). - Equal( - inter, - EmptyLocationRange, - newDictionaryValue(newType), - ), - ) -} - -func TestArrayValue_NewWithType(t *testing.T) { - - t.Parallel() - - inter := newTestInterpreter(t) - - address := common.Address{0x1} - - newArrayValue := func(arrayType ArrayStaticType) *ArrayValue { - return NewArrayValue( - inter, - EmptyLocationRange, - arrayType, - address, - NewUnmeteredStringValue("a"), - NewUnmeteredStringValue("b"), - NewUnmeteredStringValue("c"), - ) - } - - oldType := &VariableSizedStaticType{ - Type: PrimitiveStaticTypeAnyStruct, - } - - newType := &VariableSizedStaticType{ - Type: PrimitiveStaticTypeString, - } - - require.True(t, - newArrayValue(oldType). - NewWithType(inter, EmptyLocationRange, newType). - Equal( - inter, - EmptyLocationRange, - newArrayValue(newType), - ), - ) -} From 0d6f45ad309318b3acd8123c111162a8ff07949c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20M=C3=BCller?= Date: Fri, 8 Mar 2024 08:50:42 -0800 Subject: [PATCH 3/3] use ArrayValue/DictionaryValue SetType in migrations --- migrations/entitlements/migration.go | 12 ++++-------- migrations/migration_test.go | 4 ++-- migrations/statictypes/statictype_migration.go | 12 ++++-------- 3 files changed, 10 insertions(+), 18 deletions(-) diff --git a/migrations/entitlements/migration.go b/migrations/entitlements/migration.go index 72390a2a96..575812a510 100644 --- a/migrations/entitlements/migration.go +++ b/migrations/entitlements/migration.go @@ -241,11 +241,9 @@ func ConvertValueToEntitlements( return nil, nil } - return v.NewWithType( - inter, - interpreter.EmptyLocationRange, + v.SetType( entitledElementType.(interpreter.ArrayStaticType), - ), nil + ) case *interpreter.DictionaryValue: elementType := v.Type @@ -259,11 +257,9 @@ func ConvertValueToEntitlements( return nil, nil } - return v.NewWithType( - inter, - interpreter.EmptyLocationRange, + v.SetType( entitledElementType.(*interpreter.DictionaryStaticType), - ), nil + ) case *interpreter.IDCapabilityValue: borrowType := v.BorrowType diff --git a/migrations/migration_test.go b/migrations/migration_test.go index 2efa95efbc..73ae881844 100644 --- a/migrations/migration_test.go +++ b/migrations/migration_test.go @@ -1151,7 +1151,7 @@ func (testContainerMigration) Migrate( interpreter.PrimitiveStaticTypeAnyStruct, ) - return value.NewWithType(inter, emptyLocationRange, newType), nil + value.SetType(newType) case *interpreter.ArrayValue: @@ -1159,7 +1159,7 @@ func (testContainerMigration) Migrate( interpreter.PrimitiveStaticTypeAnyStruct, ) - return value.NewWithType(inter, emptyLocationRange, newType), nil + value.SetType(newType) case *interpreter.CompositeValue: if value.QualifiedIdentifier == "Inner" { diff --git a/migrations/statictypes/statictype_migration.go b/migrations/statictypes/statictype_migration.go index 3bc3e7bd8f..66ee3f1a32 100644 --- a/migrations/statictypes/statictype_migration.go +++ b/migrations/statictypes/statictype_migration.go @@ -136,11 +136,9 @@ func (m *StaticTypeMigration) Migrate( return } - return value.NewWithType( - inter, - interpreter.EmptyLocationRange, + value.SetType( convertedElementType.(interpreter.ArrayStaticType), - ), nil + ) case *interpreter.DictionaryValue: convertedElementType := m.maybeConvertStaticType(value.Type, nil) @@ -148,11 +146,9 @@ func (m *StaticTypeMigration) Migrate( return } - return value.NewWithType( - inter, - interpreter.EmptyLocationRange, + value.SetType( convertedElementType.(*interpreter.DictionaryStaticType), - ), nil + ) } return