Skip to content

Commit

Permalink
Update to new Atree API
Browse files Browse the repository at this point in the history
This commit updates Cadence to use new Atree API
- Array.Get()
- OrderedMap.Get()
- MaxInlineArrayElementSize()
- MaxInlineMapKeySize()

For more info, see Atree PRs:
- onflow/atree#314
- onflow/atree#316
- onflow/atree#318
  • Loading branch information
fxamacker authored and turbolent committed Jan 25, 2024
1 parent 40b4d69 commit d364afc
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 26 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,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.20230629205511-5b7b45a566a9
github.com/rivo/uniseg v0.4.4
github.com/schollz/progressbar/v3 v3.13.1
github.com/stretchr/testify v1.8.4
Expand Down
6 changes: 3 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,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.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/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=
Expand Down Expand Up @@ -167,5 +167,5 @@ gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b h1:QRR6H1YWRnHb4Y/HeNFCTJLF
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
lukechampine.com/blake3 v1.1.7 h1:GgRMhmdsuK8+ii6UZFDL8Nb+VyMwadAgcJyfYHxG6n0=
lukechampine.com/blake3 v1.2.1 h1:YuqqRuaqsGV71BV/nm9xlI0MKUv4QC54jQnBChWbGnI=
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
2 changes: 1 addition & 1 deletion runtime/interpreter/deepcopyremove_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestValueDeepCopyAndDeepRemove(t *testing.T) {
}

dictValueKey := NewUnmeteredStringValue(
strings.Repeat("x", int(atree.MaxInlineMapKeyOrValueSize+1)),
strings.Repeat("x", int(atree.MaxInlineMapKeySize()+1)),
)

dictValueValue := NewUnmeteredInt256ValueFromInt64(1)
Expand Down
22 changes: 11 additions & 11 deletions runtime/interpreter/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ func TestEncodeDecodeString(t *testing.T) {

t.Parallel()

maxInlineElementSize := atree.MaxInlineArrayElementSize
maxInlineElementSize := atree.MaxInlineArrayElementSize()
expected := NewUnmeteredStringValue(strings.Repeat("x", int(maxInlineElementSize+1)))

testEncodeDecode(t,
Expand Down Expand Up @@ -304,7 +304,7 @@ func TestEncodeDecodeStringAtreeValue(t *testing.T) {

t.Parallel()

maxInlineElementSize := atree.MaxInlineArrayElementSize
maxInlineElementSize := atree.MaxInlineArrayElementSize()
expected := StringAtreeValue(strings.Repeat("x", int(maxInlineElementSize+1)))

testEncodeDecode(t,
Expand Down Expand Up @@ -661,7 +661,7 @@ func TestEncodeDecodeIntValue(t *testing.T) {

expected := NewUnmeteredIntValueFromInt64(1_000_000_000)

maxInlineElementSize := atree.MaxInlineArrayElementSize
maxInlineElementSize := atree.MaxInlineArrayElementSize()
for len(expected.BigInt.Bytes()) < int(maxInlineElementSize+1) {
expected = expected.Mul(inter, expected, EmptyLocationRange).(IntValue)
}
Expand Down Expand Up @@ -1619,7 +1619,7 @@ func TestEncodeDecodeUIntValue(t *testing.T) {

expected := NewUnmeteredUIntValueFromUint64(1_000_000_000)

maxInlineElementSize := atree.MaxInlineArrayElementSize
maxInlineElementSize := atree.MaxInlineArrayElementSize()
for len(expected.BigInt.Bytes()) < int(maxInlineElementSize+1) {
expected = expected.Mul(inter, expected, EmptyLocationRange).(UIntValue)
}
Expand Down Expand Up @@ -2811,7 +2811,7 @@ func TestEncodeDecodeSomeValue(t *testing.T) {
// It will not get inlined, but the outer value will

var str *StringValue
maxInlineElementSize := atree.MaxInlineArrayElementSize
maxInlineElementSize := atree.MaxInlineArrayElementSize()
for i := uint64(0); i < maxInlineElementSize; i++ {
str = NewUnmeteredStringValue(strings.Repeat("x", int(maxInlineElementSize-i)))
size, err := StorableSize(str)
Expand Down Expand Up @@ -2845,7 +2845,7 @@ func TestEncodeDecodeSomeValue(t *testing.T) {
// Generate a string that has an encoding size just above the max inline element size

var str *StringValue
maxInlineElementSize := atree.MaxInlineArrayElementSize
maxInlineElementSize := atree.MaxInlineArrayElementSize()
for i := uint64(0); i < maxInlineElementSize; i++ {
str = NewUnmeteredStringValue(strings.Repeat("x", int(maxInlineElementSize-i)))
size, err := StorableSize(str)
Expand Down Expand Up @@ -3233,7 +3233,7 @@ func TestEncodeDecodePathValue(t *testing.T) {

t.Parallel()

maxInlineElementSize := atree.MaxInlineArrayElementSize
maxInlineElementSize := atree.MaxInlineArrayElementSize()
identifier := strings.Repeat("x", int(maxInlineElementSize+1))

expected := PathValue{
Expand Down Expand Up @@ -3333,7 +3333,7 @@ func TestEncodeDecodeCapabilityValue(t *testing.T) {
t.Parallel()

// Generate an arbitrary, large static type
maxInlineElementSize := atree.MaxInlineArrayElementSize
maxInlineElementSize := atree.MaxInlineArrayElementSize()
var borrowType StaticType = PrimitiveStaticTypeNever

for i := uint64(0); i < maxInlineElementSize; i++ {
Expand Down Expand Up @@ -3516,7 +3516,7 @@ func TestEncodeDecodeTypeValue(t *testing.T) {

t.Parallel()

maxInlineElementSize := atree.MaxInlineArrayElementSize
maxInlineElementSize := atree.MaxInlineArrayElementSize()
identifier := strings.Repeat("x", int(maxInlineElementSize+1))

expected := TypeValue{
Expand Down Expand Up @@ -4023,7 +4023,7 @@ func TestEncodeDecodeStorageCapabilityControllerValue(t *testing.T) {

t.Parallel()

maxInlineElementSize := atree.MaxInlineArrayElementSize
maxInlineElementSize := atree.MaxInlineArrayElementSize()
identifier := strings.Repeat("x", int(maxInlineElementSize+1))

path := PathValue{
Expand Down Expand Up @@ -4229,7 +4229,7 @@ func TestEncodeDecodeAccountCapabilityControllerValue(t *testing.T) {
t.Parallel()

// Generate an arbitrary, large static type
maxInlineElementSize := atree.MaxInlineArrayElementSize
maxInlineElementSize := atree.MaxInlineArrayElementSize()
var borrowType StaticType = PrimitiveStaticTypeNever

for i := uint64(0); i < maxInlineElementSize; i++ {
Expand Down
4 changes: 2 additions & 2 deletions runtime/interpreter/storagemap.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (s StorageMap) ValueExists(key StorageMapKey) bool {
// ReadValue returns the value for the given key.
// Returns nil if the key does not exist.
func (s StorageMap) ReadValue(gauge common.MemoryGauge, key StorageMapKey) Value {
storable, err := s.orderedMap.Get(
storedValue, err := s.orderedMap.Get(
key.AtreeValueCompare,
key.AtreeValueHashInput,
key.AtreeValue(),
Expand All @@ -95,7 +95,7 @@ func (s StorageMap) ReadValue(gauge common.MemoryGauge, key StorageMapKey) Value
panic(errors.NewExternalError(err))
}

return StoredValue(gauge, storable, s.orderedMap.Storage)
return MustConvertStoredValue(gauge, storedValue)
}

// WriteValue sets or removes a value in the storage map.
Expand Down
14 changes: 6 additions & 8 deletions runtime/interpreter/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -2045,14 +2045,14 @@ func (v *ArrayValue) Get(interpreter *Interpreter, locationRange LocationRange,
})
}

storable, err := v.array.Get(uint64(index))
storedValue, err := v.array.Get(uint64(index))
if err != nil {
v.handleIndexOutOfBoundsError(err, index, locationRange)

panic(errors.NewExternalError(err))
}

return StoredValue(interpreter, storable, interpreter.Storage())
return MustConvertStoredValue(interpreter, storedValue)
}

func (v *ArrayValue) SetKey(interpreter *Interpreter, locationRange LocationRange, key Value, value Value) {
Expand Down Expand Up @@ -17232,7 +17232,7 @@ func formatComposite(memoryGauge common.MemoryGauge, typeId string, fields []Com
}

func (v *CompositeValue) GetField(interpreter *Interpreter, locationRange LocationRange, name string) Value {
storable, err := v.dictionary.Get(
storedValue, err := v.dictionary.Get(
StringAtreeValueComparator,
StringAtreeValueHashInput,
StringAtreeValue(name),
Expand All @@ -17245,7 +17245,7 @@ func (v *CompositeValue) GetField(interpreter *Interpreter, locationRange Locati
panic(errors.NewExternalError(err))
}

return StoredValue(interpreter, storable, v.dictionary.Storage)
return MustConvertStoredValue(interpreter, storedValue)
}

func (v *CompositeValue) Equal(interpreter *Interpreter, locationRange LocationRange, other Value) bool {
Expand Down Expand Up @@ -18737,7 +18737,7 @@ func (v *DictionaryValue) Get(
valueComparator := newValueComparator(interpreter, locationRange)
hashInputProvider := newHashInputProvider(interpreter, locationRange)

storable, err := v.dictionary.Get(
storedValue, err := v.dictionary.Get(
valueComparator,
hashInputProvider,
keyValue,
Expand All @@ -18750,9 +18750,7 @@ func (v *DictionaryValue) Get(
panic(errors.NewExternalError(err))
}

storage := v.dictionary.Storage
value := StoredValue(interpreter, storable, storage)
return value, true
return MustConvertStoredValue(interpreter, storedValue), true
}

func (v *DictionaryValue) GetKey(interpreter *Interpreter, locationRange LocationRange, keyValue Value) Value {
Expand Down

0 comments on commit d364afc

Please sign in to comment.