diff --git a/go.mod b/go.mod index 871f5a0563..fe33a992a1 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/fxamacker/cbor/v2 v2.4.1-0.20230228173756-c0c9f774e40c github.com/go-test/deep v1.1.0 github.com/leanovate/gopter v0.2.9 - github.com/onflow/atree v0.6.1-0.20230629205511-5b7b45a566a9 + github.com/onflow/atree v0.6.1-0.20230706233410-78f997992600 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 84e854a294..b46ef1e573 100644 --- a/go.sum +++ b/go.sum @@ -52,8 +52,8 @@ github.com/mattn/go-tty v0.0.4/go.mod h1:u5GGXBtZU6RQoKV8gY5W6UhMudbR5vXnUe7j3px 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.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/atree v0.6.1-0.20230706233410-78f997992600 h1:OOnC8buJso6dSZ6W+RGQDnJW9UTh7HfEGyS/ivXJ7NI= +github.com/onflow/atree v0.6.1-0.20230706233410-78f997992600/go.mod h1:7YNAyCd5JENq+NzH+fR1ABUZVzbSq9dkt0+5fZH3L2A= github.com/pkg/term v1.2.0-beta.2 h1:L3y/h2jkuBVFdWiJvNfYfKmzcCnILw7mJWm2JQuMppw= github.com/pkg/term v1.2.0-beta.2/go.mod h1:E25nymQcrSllhX42Ok8MRm1+hyBdHY0dCeiKZ9jpNGw= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= diff --git a/runtime/cmd/decode-state-values/main.go b/runtime/cmd/decode-state-values/main.go index 76b7a92994..f73a3b5acf 100644 --- a/runtime/cmd/decode-state-values/main.go +++ b/runtime/cmd/decode-state-values/main.go @@ -80,17 +80,18 @@ func isSlabStorageKey(key string) bool { return len(key) == slabKeyLength && key[0] == '$' } -func storageKeySlabStorageID(address atree.Address, key string) atree.StorageID { +func storageKeyToSlabID(address atree.Address, key string) atree.SlabID { if !isSlabStorageKey(key) { - return atree.StorageIDUndefined + return atree.SlabIDUndefined } - var result atree.StorageID - result.Address = address - copy(result.Index[:], key[1:]) - return result + + var index atree.SlabIndex + copy(index[:], key[1:]) + + return atree.NewSlabID(address, index) } -func decodeStorable(decoder *cbor.StreamDecoder, storableSlabStorageID atree.StorageID) (atree.Storable, error) { +func decodeStorable(decoder *cbor.StreamDecoder, storableSlabStorageID atree.SlabID) (atree.Storable, error) { return interpreter.DecodeStorable(decoder, storableSlabStorageID, nil) } @@ -98,7 +99,7 @@ func decodeTypeInfo(decoder *cbor.StreamDecoder) (atree.TypeInfo, error) { return interpreter.DecodeTypeInfo(decoder, nil) } -func decodeSlab(id atree.StorageID, data []byte) (atree.Slab, error) { +func decodeSlab(id atree.SlabID, data []byte) (atree.Slab, error) { return atree.DecodeSlab( id, data, @@ -108,11 +109,17 @@ func decodeSlab(id atree.StorageID, data []byte) (atree.Slab, error) { ) } -func storageIDStorageKey(id atree.StorageID) storageKey { +func slabIDToStorageKey(id atree.SlabID) storageKey { + var b [16]byte + _, err := id.ToRawBytes(b[:]) + if err != nil { + panic(err) + } + return storageKey{ - string(id.Address[:]), + string(b[:8]), "", - "$" + string(id.Index[:]), + "$" + string(b[8:]), } } @@ -122,8 +129,8 @@ type slabStorage struct{} var _ atree.SlabStorage = &slabStorage{} -func (s *slabStorage) Retrieve(id atree.StorageID) (atree.Slab, bool, error) { - data, ok := storage[storageIDStorageKey(id)] +func (s *slabStorage) Retrieve(id atree.SlabID) (atree.Slab, bool, error) { + data, ok := storage[slabIDToStorageKey(id)] if !ok { return nil, false, nil } @@ -136,22 +143,22 @@ func (s *slabStorage) Retrieve(id atree.StorageID) (atree.Slab, bool, error) { return slab, true, nil } -func (s *slabStorage) Store(_ atree.StorageID, _ atree.Slab) error { +func (s *slabStorage) Store(_ atree.SlabID, _ atree.Slab) error { panic("unexpected Store call") } -func (s *slabStorage) Remove(_ atree.StorageID) error { +func (s *slabStorage) Remove(_ atree.SlabID) error { panic("unexpected Remove call") } -func (s *slabStorage) GenerateStorageID(_ atree.Address) (atree.StorageID, error) { +func (s *slabStorage) GenerateSlabID(_ atree.Address) (atree.SlabID, error) { panic("unexpected GenerateStorageID call") } func (s *slabStorage) SlabIterator() (atree.SlabIterator, error) { var slabs []struct { storageKey - atree.StorageID + atree.SlabID } // NOTE: iteration over map is safe, @@ -161,16 +168,16 @@ func (s *slabStorage) SlabIterator() (atree.SlabIterator, error) { var address atree.Address copy(address[:], key[0]) - storageID := storageKeySlabStorageID(address, key[2]) - if storageID == atree.StorageIDUndefined { + slabID := storageKeyToSlabID(address, key[2]) + if slabID == atree.SlabIDUndefined { continue } slabs = append(slabs, struct { storageKey - atree.StorageID + atree.SlabID }{ - StorageID: storageID, + SlabID: slabID, storageKey: key, }) } @@ -178,17 +185,17 @@ func (s *slabStorage) SlabIterator() (atree.SlabIterator, error) { sort.Slice(slabs, func(i, j int) bool { a := slabs[i] b := slabs[j] - return a.StorageID.Compare(b.StorageID) < 0 + return a.SlabID.Compare(b.SlabID) < 0 }) var i int bar := progressbar.Default(int64(len(slabs))) - return func() (atree.StorageID, atree.Slab) { + return func() (atree.SlabID, atree.Slab) { if i >= len(slabs) { _ = bar.Close() - return atree.StorageIDUndefined, nil + return atree.SlabIDUndefined, nil } slabEntry := slabs[i] @@ -196,15 +203,15 @@ func (s *slabStorage) SlabIterator() (atree.SlabIterator, error) { _ = bar.Add(1) - storageID := slabEntry.StorageID + slabID := slabEntry.SlabID data := storage[slabEntry.storageKey] - slab, err := decodeSlab(storageID, data) + slab, err := decodeSlab(slabID, data) if err != nil { - log.Fatalf("failed to decode slab @ %s", storageID) + log.Fatalf("failed to decode slab @ %s", slabID) } - return storageID, slab + return slabID, slab }, nil } @@ -308,20 +315,17 @@ func loadStorageKey( if !*checkSlabsFlag { - var storageIndex atree.StorageIndex + var slabIndex atree.SlabIndex // Skip '$' prefix - copy(storageIndex[:], key[1:]) + copy(slabIndex[:], key[1:]) - storageID := atree.StorageID{ - Address: address, - Index: storageIndex, - } + slabID := atree.NewSlabID(address, slabIndex) - _, err := decodeSlab(storageID, data) + _, err := decodeSlab(slabID, data) if err != nil { log.Printf( "Failed to decode slab @ %s: %s (size: %d)", - storageID, err, len(data), + slabID, err, len(data), ) return err } @@ -339,7 +343,7 @@ func loadStorageKey( reader := bytes.NewReader(data) decoder := interpreter.CBORDecMode.NewStreamDecoder(reader) - storable, err := interpreter.DecodeStorable(decoder, atree.StorageIDUndefined, nil) + storable, err := interpreter.DecodeStorable(decoder, atree.SlabIDUndefined, nil) if err != nil { log.Printf( "Failed to decode storable @ 0x%x %s: %s (data: %x)\n", diff --git a/runtime/interface.go b/runtime/interface.go index 8048171af1..62e8814119 100644 --- a/runtime/interface.go +++ b/runtime/interface.go @@ -67,8 +67,8 @@ type Interface interface { SetValue(owner, key, value []byte) (err error) // ValueExists returns true if the given key exists in the storage, owned by the given account. ValueExists(owner, key []byte) (exists bool, err error) - // AllocateStorageIndex allocates a new storage index under the given account. - AllocateStorageIndex(owner []byte) (atree.StorageIndex, error) + // AllocateSlabIndex allocates a new slab index under the given account. + AllocateSlabIndex(owner []byte) (atree.SlabIndex, error) // CreateAccount creates a new account. CreateAccount(payer Address) (address Address, err error) // AddEncodedAccountKey appends an encoded key to an account. diff --git a/runtime/interpreter/decode.go b/runtime/interpreter/decode.go index 1fc2dd56a8..ac987abecf 100644 --- a/runtime/interpreter/decode.go +++ b/runtime/interpreter/decode.go @@ -117,24 +117,24 @@ func decodeInt64(d StorableDecoder) (int64, error) { func DecodeStorable( decoder *cbor.StreamDecoder, - slabStorageID atree.StorageID, + slabID atree.SlabID, memoryGauge common.MemoryGauge, ) ( atree.Storable, error, ) { - return NewStorableDecoder(decoder, slabStorageID, memoryGauge).decodeStorable() + return NewStorableDecoder(decoder, slabID, memoryGauge).decodeStorable() } func NewStorableDecoder( decoder *cbor.StreamDecoder, - slabStorageID atree.StorageID, + slabID atree.SlabID, memoryGauge common.MemoryGauge, ) StorableDecoder { return StorableDecoder{ - decoder: decoder, - memoryGauge: memoryGauge, - slabStorageID: slabStorageID, + decoder: decoder, + memoryGauge: memoryGauge, + slabID: slabID, TypeDecoder: NewTypeDecoder( decoder, memoryGauge, @@ -144,9 +144,9 @@ func NewStorableDecoder( type StorableDecoder struct { TypeDecoder - memoryGauge common.MemoryGauge - decoder *cbor.StreamDecoder - slabStorageID atree.StorageID + memoryGauge common.MemoryGauge + decoder *cbor.StreamDecoder + slabID atree.SlabID } func (d StorableDecoder) decodeStorable() (atree.Storable, error) { @@ -200,8 +200,8 @@ func (d StorableDecoder) decodeStorable() (atree.Storable, error) { switch num { - case atree.CBORTagStorageID: - return atree.DecodeStorageIDStorable(d.decoder) + case atree.CBORTagSlabID: + return atree.DecodeSlabIDStorable(d.decoder) case CBORTagVoidValue: err := d.decoder.Skip() diff --git a/runtime/interpreter/deepcopyremove_test.go b/runtime/interpreter/deepcopyremove_test.go index df5e4a34e5..b1f2e6f6b4 100644 --- a/runtime/interpreter/deepcopyremove_test.go +++ b/runtime/interpreter/deepcopyremove_test.go @@ -92,7 +92,7 @@ func TestValueDeepCopyAndDeepRemove(t *testing.T) { count := 0 for id := range storage.Slabs { - if id.Address != (atree.Address{}) { + if !id.HasTempAddress() { count++ } } diff --git a/runtime/interpreter/encoding_test.go b/runtime/interpreter/encoding_test.go index c4fc2548c0..247c8bc0a0 100644 --- a/runtime/interpreter/encoding_test.go +++ b/runtime/interpreter/encoding_test.go @@ -46,7 +46,7 @@ type encodeDecodeTest struct { decodeOnly bool deepEquality bool storage Storage - slabStorageID atree.StorageID + slabStorageID atree.SlabID maxInlineElementSize uint64 } @@ -251,7 +251,7 @@ func TestEncodeDecodeString(t *testing.T) { maxInlineElementSize: maxInlineElementSize, encoded: []byte{ // tag - 0xd8, atree.CBORTagStorageID, + 0xd8, atree.CBORTagSlabID, // storage ID 0x50, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x42, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, @@ -313,7 +313,7 @@ func TestEncodeDecodeStringAtreeValue(t *testing.T) { maxInlineElementSize: maxInlineElementSize, encoded: []byte{ // tag - 0xd8, atree.CBORTagStorageID, + 0xd8, atree.CBORTagSlabID, // storage ID 0x50, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x42, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, @@ -413,7 +413,7 @@ func TestEncodeDecodeArray(t *testing.T) { value: expected, encoded: []byte{ // tag - 0xd8, atree.CBORTagStorageID, + 0xd8, atree.CBORTagSlabID, // storage ID 0x50, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, @@ -446,7 +446,7 @@ func TestEncodeDecodeArray(t *testing.T) { value: expected, encoded: []byte{ // tag - 0xd8, atree.CBORTagStorageID, + 0xd8, atree.CBORTagSlabID, // storage ID 0x50, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, @@ -482,7 +482,7 @@ func TestEncodeDecodeComposite(t *testing.T) { value: expected, encoded: []byte{ // tag - 0xd8, atree.CBORTagStorageID, + 0xd8, atree.CBORTagSlabID, // storage ID 0x50, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x42, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, @@ -520,7 +520,7 @@ func TestEncodeDecodeComposite(t *testing.T) { value: expected, encoded: []byte{ // tag - 0xd8, atree.CBORTagStorageID, + 0xd8, atree.CBORTagSlabID, // storage ID 0x50, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x42, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, @@ -672,7 +672,7 @@ func TestEncodeDecodeIntValue(t *testing.T) { maxInlineElementSize: maxInlineElementSize, encoded: []byte{ // tag - 0xd8, atree.CBORTagStorageID, + 0xd8, atree.CBORTagSlabID, // storage ID 0x50, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x42, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, @@ -1630,7 +1630,7 @@ func TestEncodeDecodeUIntValue(t *testing.T) { maxInlineElementSize: maxInlineElementSize, encoded: []byte{ // tag - 0xd8, atree.CBORTagStorageID, + 0xd8, atree.CBORTagSlabID, // storage ID 0x50, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x42, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, @@ -2829,7 +2829,7 @@ func TestEncodeDecodeSomeValue(t *testing.T) { maxInlineElementSize: maxInlineElementSize, encoded: []byte{ // tag - 0xd8, atree.CBORTagStorageID, + 0xd8, atree.CBORTagSlabID, // storage ID 0x50, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x42, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, @@ -2865,7 +2865,7 @@ func TestEncodeDecodeSomeValue(t *testing.T) { // tag 0xd8, CBORTagSomeValue, // value - 0xd8, atree.CBORTagStorageID, + 0xd8, atree.CBORTagSlabID, // storage ID 0x50, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x42, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, }, @@ -3247,7 +3247,7 @@ func TestEncodeDecodePathValue(t *testing.T) { maxInlineElementSize: maxInlineElementSize, encoded: []byte{ // tag - 0xd8, atree.CBORTagStorageID, + 0xd8, atree.CBORTagSlabID, // storage ID 0x50, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x42, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, @@ -3523,7 +3523,7 @@ func TestEncodeDecodePathCapabilityValue(t *testing.T) { maxInlineElementSize: maxInlineElementSize, encoded: []byte{ // tag - 0xd8, atree.CBORTagStorageID, + 0xd8, atree.CBORTagSlabID, // storage ID 0x50, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x42, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, @@ -3568,7 +3568,7 @@ func TestEncodeDecodePathCapabilityValue(t *testing.T) { value: expected, maxInlineElementSize: maxInlineElementSize, encoded: []byte{ - 0xd8, atree.CBORTagStorageID, + 0xd8, atree.CBORTagSlabID, // storage ID 0x50, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x42, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, }, @@ -3674,7 +3674,7 @@ func TestEncodeDecodeIDCapabilityValue(t *testing.T) { maxInlineElementSize: maxInlineElementSize, encoded: []byte{ // tag - 0xd8, atree.CBORTagStorageID, + 0xd8, atree.CBORTagSlabID, // storage ID 0x50, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x42, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, @@ -4146,7 +4146,7 @@ func TestEncodeDecodePathLinkValue(t *testing.T) { maxInlineElementSize: maxInlineElementSize, encoded: []byte{ // tag - 0xd8, atree.CBORTagStorageID, + 0xd8, atree.CBORTagSlabID, // storage ID 0x50, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x42, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, @@ -4280,7 +4280,7 @@ func TestEncodeDecodeTypeValue(t *testing.T) { maxInlineElementSize: maxInlineElementSize, encoded: []byte{ // tag - 0xd8, atree.CBORTagStorageID, + 0xd8, atree.CBORTagSlabID, // storage ID 0x50, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x42, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, @@ -4820,7 +4820,7 @@ func TestEncodeDecodeStorageCapabilityControllerValue(t *testing.T) { maxInlineElementSize: maxInlineElementSize, encoded: []byte{ // tag - 0xd8, atree.CBORTagStorageID, + 0xd8, atree.CBORTagSlabID, // storage ID 0x50, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x42, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, @@ -5002,7 +5002,7 @@ func TestEncodeDecodeAccountCapabilityControllerValue(t *testing.T) { maxInlineElementSize: maxInlineElementSize, encoded: []byte{ // tag - 0xd8, atree.CBORTagStorageID, + 0xd8, atree.CBORTagSlabID, // storage ID 0x50, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x42, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, diff --git a/runtime/interpreter/interpreter.go b/runtime/interpreter/interpreter.go index 3791f68676..614d91f35f 100644 --- a/runtime/interpreter/interpreter.go +++ b/runtime/interpreter/interpreter.go @@ -239,7 +239,7 @@ type Storage interface { CheckHealth() error } -type ReferencedResourceKindedValues map[atree.StorageID]map[ReferenceTrackedResourceKindedValue]struct{} +type ReferencedResourceKindedValues map[atree.SlabID]map[ReferenceTrackedResourceKindedValue]struct{} type Interpreter struct { Location common.Location @@ -4941,13 +4941,13 @@ func (interpreter *Interpreter) checkReferencedResourceNotDestroyed(value Value, } func (interpreter *Interpreter) RemoveReferencedSlab(storable atree.Storable) { - storageIDStorable, ok := storable.(atree.StorageIDStorable) + slabIDStorable, ok := storable.(atree.SlabIDStorable) if !ok { return } - storageID := atree.StorageID(storageIDStorable) - err := interpreter.Storage().Remove(storageID) + slabID := atree.SlabID(slabIDStorable) + err := interpreter.Storage().Remove(slabID) if err != nil { panic(errors.NewExternalError(err)) } @@ -5100,12 +5100,12 @@ func (interpreter *Interpreter) ValidateAtreeValue(value atree.Value) { func (interpreter *Interpreter) maybeTrackReferencedResourceKindedValue(value Value) { if value, ok := value.(ReferenceTrackedResourceKindedValue); ok { - interpreter.trackReferencedResourceKindedValue(value.StorageID(), value) + interpreter.trackReferencedResourceKindedValue(value.SlabID(), value) } } func (interpreter *Interpreter) trackReferencedResourceKindedValue( - id atree.StorageID, + id atree.SlabID, value ReferenceTrackedResourceKindedValue, ) { values := interpreter.SharedState.referencedResourceKindedValues[id] @@ -5117,20 +5117,20 @@ func (interpreter *Interpreter) trackReferencedResourceKindedValue( } func (interpreter *Interpreter) updateReferencedResource( - currentStorageID atree.StorageID, - newStorageID atree.StorageID, + currentID atree.SlabID, + newID atree.SlabID, updateFunc func(value ReferenceTrackedResourceKindedValue), ) { - values := interpreter.SharedState.referencedResourceKindedValues[currentStorageID] + values := interpreter.SharedState.referencedResourceKindedValues[currentID] if values == nil { return } for value := range values { //nolint:maprange updateFunc(value) } - if newStorageID != currentStorageID { - interpreter.SharedState.referencedResourceKindedValues[newStorageID] = values - interpreter.SharedState.referencedResourceKindedValues[currentStorageID] = nil + if newID != currentID { + interpreter.SharedState.referencedResourceKindedValues[newID] = values + interpreter.SharedState.referencedResourceKindedValues[currentID] = nil } } @@ -5252,12 +5252,12 @@ func (interpreter *Interpreter) MeterMemory(usage common.MemoryUsage) error { func (interpreter *Interpreter) DecodeStorable( decoder *cbor.StreamDecoder, - storageID atree.StorageID, + slabID atree.SlabID, ) ( atree.Storable, error, ) { - return DecodeStorable(decoder, storageID, interpreter) + return DecodeStorable(decoder, slabID, interpreter) } func (interpreter *Interpreter) DecodeTypeInfo(decoder *cbor.StreamDecoder) (atree.TypeInfo, error) { @@ -5377,8 +5377,8 @@ func (interpreter *Interpreter) idCapabilityCheckFunction( ) } -func (interpreter *Interpreter) validateMutation(storageID atree.StorageID, locationRange LocationRange) { - _, present := interpreter.SharedState.containerValueIteration[storageID] +func (interpreter *Interpreter) validateMutation(slabID atree.SlabID, locationRange LocationRange) { + _, present := interpreter.SharedState.containerValueIteration[slabID] if !present { return } @@ -5387,32 +5387,32 @@ func (interpreter *Interpreter) validateMutation(storageID atree.StorageID, loca }) } -func (interpreter *Interpreter) withMutationPrevention(storageID atree.StorageID, f func()) { - oldIteration, present := interpreter.SharedState.containerValueIteration[storageID] - interpreter.SharedState.containerValueIteration[storageID] = struct{}{} +func (interpreter *Interpreter) withMutationPrevention(slabID atree.SlabID, f func()) { + oldIteration, present := interpreter.SharedState.containerValueIteration[slabID] + interpreter.SharedState.containerValueIteration[slabID] = struct{}{} f() if !present { - delete(interpreter.SharedState.containerValueIteration, storageID) + delete(interpreter.SharedState.containerValueIteration, slabID) } else { - interpreter.SharedState.containerValueIteration[storageID] = oldIteration + interpreter.SharedState.containerValueIteration[slabID] = oldIteration } } func (interpreter *Interpreter) withResourceDestruction( - storageID atree.StorageID, + slabID atree.SlabID, locationRange LocationRange, f func(), ) { - _, exists := interpreter.SharedState.destroyedResources[storageID] + _, exists := interpreter.SharedState.destroyedResources[slabID] if exists { panic(DestroyedResourceError{ LocationRange: locationRange, }) } - interpreter.SharedState.destroyedResources[storageID] = struct{}{} + interpreter.SharedState.destroyedResources[slabID] = struct{}{} f() } diff --git a/runtime/interpreter/interpreter_expression.go b/runtime/interpreter/interpreter_expression.go index fc49fd6ed3..27f48e4e09 100644 --- a/runtime/interpreter/interpreter_expression.go +++ b/runtime/interpreter/interpreter_expression.go @@ -937,8 +937,8 @@ func (interpreter *Interpreter) visitInvocationExpressionWithImplicitArgument(in if boundFunction, ok := function.(BoundFunctionValue); ok && boundFunction.Self != nil { self := *boundFunction.Self if resource, ok := self.(ReferenceTrackedResourceKindedValue); ok { - storageID := resource.StorageID() - interpreter.trackReferencedResourceKindedValue(storageID, resource) + slabID := resource.SlabID() + interpreter.trackReferencedResourceKindedValue(slabID, resource) } } @@ -1290,7 +1290,7 @@ func (interpreter *Interpreter) VisitAttachExpression(attachExpression *ast.Atta base, interpreter.MustSemaTypeOfValue(base).(*sema.CompositeType), ) - interpreter.trackReferencedResourceKindedValue(base.StorageID(), base) + interpreter.trackReferencedResourceKindedValue(base.SlabID(), base) attachment, ok := interpreter.visitInvocationExpressionWithImplicitArgument( attachExpression.Attachment, @@ -1302,7 +1302,7 @@ func (interpreter *Interpreter) VisitAttachExpression(attachExpression *ast.Atta } // Because `self` in attachments is a reference, we need to track the attachment if it's a resource - interpreter.trackReferencedResourceKindedValue(attachment.StorageID(), attachment) + interpreter.trackReferencedResourceKindedValue(attachment.SlabID(), attachment) base = base.Transfer( interpreter, diff --git a/runtime/interpreter/sharedstate.go b/runtime/interpreter/sharedstate.go index f250ce3f58..682bd68bf2 100644 --- a/runtime/interpreter/sharedstate.go +++ b/runtime/interpreter/sharedstate.go @@ -43,8 +43,8 @@ type SharedState struct { storageMutatedDuringIteration bool CapabilityControllerIterations map[AddressPath]int MutationDuringCapabilityControllerIteration bool - containerValueIteration map[atree.StorageID]struct{} - destroyedResources map[atree.StorageID]struct{} + containerValueIteration map[atree.SlabID]struct{} + destroyedResources map[atree.SlabID]struct{} } func NewSharedState(config *Config) *SharedState { @@ -59,11 +59,11 @@ func NewSharedState(config *Config) *SharedState { }, inStorageIteration: false, storageMutatedDuringIteration: false, - referencedResourceKindedValues: map[atree.StorageID]map[ReferenceTrackedResourceKindedValue]struct{}{}, + referencedResourceKindedValues: map[atree.SlabID]map[ReferenceTrackedResourceKindedValue]struct{}{}, resourceVariables: map[ResourceKindedValue]*Variable{}, CapabilityControllerIterations: map[AddressPath]int{}, - containerValueIteration: map[atree.StorageID]struct{}{}, - destroyedResources: map[atree.StorageID]struct{}{}, + containerValueIteration: map[atree.SlabID]struct{}{}, + destroyedResources: map[atree.SlabID]struct{}{}, } } diff --git a/runtime/interpreter/storage.go b/runtime/interpreter/storage.go index 3eed4fea02..666a530274 100644 --- a/runtime/interpreter/storage.go +++ b/runtime/interpreter/storage.go @@ -121,7 +121,7 @@ type InMemoryStorage struct { var _ Storage = InMemoryStorage{} func NewInMemoryStorage(memoryGauge common.MemoryGauge) InMemoryStorage { - decodeStorable := func(decoder *cbor.StreamDecoder, storableSlabStorageID atree.StorageID) (atree.Storable, error) { + decodeStorable := func(decoder *cbor.StreamDecoder, storableSlabStorageID atree.SlabID) (atree.Storable, error) { return DecodeStorable(decoder, storableSlabStorageID, memoryGauge) } @@ -211,7 +211,7 @@ func StorableSize(storable atree.Storable) (uint32, error) { // maybeLargeImmutableStorable either returns the given immutable atree.Storable // if it can be stored inline inside its parent container, -// or else stores it in a separate slab and returns an atree.StorageIDStorable. +// or else stores it in a separate slab and returns an atree.SlabIDStorable. func maybeLargeImmutableStorable( storable atree.Storable, storage atree.SlabStorage, @@ -226,20 +226,5 @@ func maybeLargeImmutableStorable( return storable, nil } - storageID, err := storage.GenerateStorageID(address) - if err != nil { - return nil, err - } - - slab := &atree.StorableSlab{ - StorageID: storageID, - Storable: storable, - } - - err = storage.Store(storageID, slab) - if err != nil { - return nil, err - } - - return atree.StorageIDStorable(storageID), nil + return atree.NewStorableSlab(storage, address, storable) } diff --git a/runtime/interpreter/storage_test.go b/runtime/interpreter/storage_test.go index b97cbc784a..73a2d1d56c 100644 --- a/runtime/interpreter/storage_test.go +++ b/runtime/interpreter/storage_test.go @@ -55,11 +55,11 @@ func TestCompositeStorage(t *testing.T) { testOwner, ) - require.NotEqual(t, atree.StorageIDUndefined, value.StorageID()) + require.NotEqual(t, atree.SlabIDUndefined, value.SlabID()) require.Equal(t, 1, storage.BasicSlabStorage.Count()) - _, ok, err := storage.BasicSlabStorage.Retrieve(value.StorageID()) + _, ok, err := storage.BasicSlabStorage.Retrieve(value.SlabID()) require.NoError(t, err) require.True(t, ok) @@ -69,7 +69,7 @@ func TestCompositeStorage(t *testing.T) { require.Equal(t, 1, storage.BasicSlabStorage.Count()) - retrievedStorable, ok, err := storage.BasicSlabStorage.Retrieve(value.StorageID()) + retrievedStorable, ok, err := storage.BasicSlabStorage.Retrieve(value.SlabID()) require.NoError(t, err) require.True(t, ok) @@ -128,12 +128,12 @@ func TestArrayStorage(t *testing.T) { common.ZeroAddress, ) - require.NotEqual(t, atree.StorageIDUndefined, value.StorageID()) + require.NotEqual(t, atree.SlabIDUndefined, value.SlabID()) // array + composite require.Equal(t, 2, storage.BasicSlabStorage.Count()) - _, ok, err := storage.BasicSlabStorage.Retrieve(value.StorageID()) + _, ok, err := storage.BasicSlabStorage.Retrieve(value.SlabID()) require.NoError(t, err) require.True(t, ok) @@ -151,7 +151,7 @@ func TestArrayStorage(t *testing.T) { // array + original composite element + new copy of composite element require.Equal(t, 3, storage.BasicSlabStorage.Count()) - retrievedStorable, ok, err := storage.BasicSlabStorage.Retrieve(value.StorageID()) + retrievedStorable, ok, err := storage.BasicSlabStorage.Retrieve(value.SlabID()) require.NoError(t, err) require.True(t, ok) @@ -197,12 +197,12 @@ func TestArrayStorage(t *testing.T) { require.True(t, bool(value.Contains(inter, EmptyLocationRange, element))) - require.NotEqual(t, atree.StorageIDUndefined, value.StorageID()) + require.NotEqual(t, atree.SlabIDUndefined, value.SlabID()) // array + original composite element + new copy of composite element require.Equal(t, 3, storage.BasicSlabStorage.Count()) - _, ok, err := storage.BasicSlabStorage.Retrieve(value.StorageID()) + _, ok, err := storage.BasicSlabStorage.Retrieve(value.SlabID()) require.NoError(t, err) require.True(t, ok) @@ -214,7 +214,7 @@ func TestArrayStorage(t *testing.T) { require.Equal(t, 3, storage.BasicSlabStorage.Count()) - retrievedStorable, ok, err := storage.BasicSlabStorage.Retrieve(value.StorageID()) + retrievedStorable, ok, err := storage.BasicSlabStorage.Retrieve(value.SlabID()) require.NoError(t, err) require.True(t, ok) @@ -253,11 +253,11 @@ func TestDictionaryStorage(t *testing.T) { }, ) - require.NotEqual(t, atree.StorageIDUndefined, value.StorageID()) + require.NotEqual(t, atree.SlabIDUndefined, value.SlabID()) require.Equal(t, 1, storage.BasicSlabStorage.Count()) - _, ok, err := storage.BasicSlabStorage.Retrieve(value.StorageID()) + _, ok, err := storage.BasicSlabStorage.Retrieve(value.SlabID()) require.NoError(t, err) require.True(t, ok) @@ -273,7 +273,7 @@ func TestDictionaryStorage(t *testing.T) { require.Equal(t, 1, storage.BasicSlabStorage.Count()) - retrievedStorable, ok, err := storage.BasicSlabStorage.Retrieve(value.StorageID()) + retrievedStorable, ok, err := storage.BasicSlabStorage.Retrieve(value.SlabID()) require.NoError(t, err) require.True(t, ok) @@ -312,11 +312,11 @@ func TestDictionaryStorage(t *testing.T) { NewUnmeteredSomeValueNonCopying(TrueValue), ) - require.NotEqual(t, atree.StorageIDUndefined, value.StorageID()) + require.NotEqual(t, atree.SlabIDUndefined, value.SlabID()) require.Equal(t, 1, storage.BasicSlabStorage.Count()) - _, ok, err := storage.BasicSlabStorage.Retrieve(value.StorageID()) + _, ok, err := storage.BasicSlabStorage.Retrieve(value.SlabID()) require.NoError(t, err) require.True(t, ok) @@ -329,7 +329,7 @@ func TestDictionaryStorage(t *testing.T) { require.Equal(t, 1, storage.BasicSlabStorage.Count()) - retrievedStorable, ok, err := storage.BasicSlabStorage.Retrieve(value.StorageID()) + retrievedStorable, ok, err := storage.BasicSlabStorage.Retrieve(value.SlabID()) require.NoError(t, err) require.True(t, ok) @@ -362,11 +362,11 @@ func TestDictionaryStorage(t *testing.T) { NewUnmeteredSomeValueNonCopying(TrueValue), ) - require.NotEqual(t, atree.StorageIDUndefined, value.StorageID()) + require.NotEqual(t, atree.SlabIDUndefined, value.SlabID()) require.Equal(t, 1, storage.BasicSlabStorage.Count()) - _, ok, err := storage.BasicSlabStorage.Retrieve(value.StorageID()) + _, ok, err := storage.BasicSlabStorage.Retrieve(value.SlabID()) require.NoError(t, err) require.True(t, ok) @@ -378,7 +378,7 @@ func TestDictionaryStorage(t *testing.T) { require.Equal(t, 1, storage.BasicSlabStorage.Count()) - retrievedStorable, ok, err := storage.BasicSlabStorage.Retrieve(value.StorageID()) + retrievedStorable, ok, err := storage.BasicSlabStorage.Retrieve(value.SlabID()) require.NoError(t, err) require.True(t, ok) @@ -409,11 +409,11 @@ func TestDictionaryStorage(t *testing.T) { }, ) - require.NotEqual(t, atree.StorageIDUndefined, value.StorageID()) + require.NotEqual(t, atree.SlabIDUndefined, value.SlabID()) require.Equal(t, 1, storage.BasicSlabStorage.Count()) - _, ok, err := storage.BasicSlabStorage.Retrieve(value.StorageID()) + _, ok, err := storage.BasicSlabStorage.Retrieve(value.SlabID()) require.NoError(t, err) require.True(t, ok) @@ -426,7 +426,7 @@ func TestDictionaryStorage(t *testing.T) { require.Equal(t, 1, storage.BasicSlabStorage.Count()) - retrievedStorable, ok, err := storage.BasicSlabStorage.Retrieve(value.StorageID()) + retrievedStorable, ok, err := storage.BasicSlabStorage.Retrieve(value.SlabID()) require.NoError(t, err) require.True(t, ok) diff --git a/runtime/interpreter/storagemap.go b/runtime/interpreter/storagemap.go index e8f347fbf7..5d7c5a26d1 100644 --- a/runtime/interpreter/storagemap.go +++ b/runtime/interpreter/storagemap.go @@ -50,10 +50,10 @@ func NewStorageMap(memoryGauge common.MemoryGauge, storage atree.SlabStorage, ad } } -func NewStorageMapWithRootID(storage atree.SlabStorage, storageID atree.StorageID) *StorageMap { +func NewStorageMapWithRootID(storage atree.SlabStorage, slabID atree.SlabID) *StorageMap { orderedMap, err := atree.NewMapWithRootID( storage, - storageID, + slabID, atree.NewDefaultDigesterBuilder(), ) if err != nil { @@ -186,8 +186,8 @@ func (s StorageMap) Iterator(gauge common.MemoryGauge) StorageMapIterator { } } -func (s StorageMap) StorageID() atree.StorageID { - return s.orderedMap.StorageID() +func (s StorageMap) SlabID() atree.SlabID { + return s.orderedMap.SlabID() } func (s StorageMap) Count() uint64 { diff --git a/runtime/interpreter/value.go b/runtime/interpreter/value.go index 6be8679305..984b6d9e58 100644 --- a/runtime/interpreter/value.go +++ b/runtime/interpreter/value.go @@ -217,7 +217,7 @@ func maybeDestroy(interpreter *Interpreter, locationRange LocationRange, value V type ReferenceTrackedResourceKindedValue interface { ResourceKindedValue IsReferenceTrackedResourceKindedValue() - StorageID() atree.StorageID + SlabID() atree.SlabID } // ContractValue is the value of a contract. @@ -1717,7 +1717,7 @@ func (v *ArrayValue) Iterate(interpreter *Interpreter, f func(element Value) (re } if v.IsResourceKinded(interpreter) { - interpreter.withMutationPrevention(v.StorageID(), iterate) + interpreter.withMutationPrevention(v.SlabID(), iterate) } else { iterate() } @@ -1784,10 +1784,10 @@ func (v *ArrayValue) Destroy(interpreter *Interpreter, locationRange LocationRan }() } - storageID := v.StorageID() + slabID := v.SlabID() interpreter.withResourceDestruction( - storageID, + slabID, locationRange, func() { v.Walk(interpreter, func(element Value) { @@ -1803,8 +1803,8 @@ func (v *ArrayValue) Destroy(interpreter *Interpreter, locationRange LocationRan } interpreter.updateReferencedResource( - storageID, - storageID, + slabID, + slabID, func(value ReferenceTrackedResourceKindedValue) { arrayValue, ok := value.(*ArrayValue) if !ok { @@ -1948,7 +1948,7 @@ func (v *ArrayValue) SetKey(interpreter *Interpreter, locationRange LocationRang func (v *ArrayValue) Set(interpreter *Interpreter, locationRange LocationRange, index int, element Value) { - interpreter.validateMutation(v.StorageID(), locationRange) + interpreter.validateMutation(v.SlabID(), locationRange) // We only need to check the lower bound before converting from `int` (signed) to `uint64` (unsigned). // atree's Array.Set function will check the upper bound and report an atree.IndexOutOfBoundsError @@ -2022,7 +2022,7 @@ func (v *ArrayValue) MeteredString(memoryGauge common.MemoryGauge, seenReference func (v *ArrayValue) Append(interpreter *Interpreter, locationRange LocationRange, element Value) { - interpreter.validateMutation(v.StorageID(), locationRange) + interpreter.validateMutation(v.SlabID(), locationRange) // length increases by 1 dataSlabs, metaDataSlabs := common.AdditionalAtreeMemoryUsage( @@ -2070,7 +2070,7 @@ func (v *ArrayValue) InsertKey(interpreter *Interpreter, locationRange LocationR func (v *ArrayValue) Insert(interpreter *Interpreter, locationRange LocationRange, index int, element Value) { - interpreter.validateMutation(v.StorageID(), locationRange) + interpreter.validateMutation(v.SlabID(), locationRange) // We only need to check the lower bound before converting from `int` (signed) to `uint64` (unsigned). // atree's Array.Insert function will check the upper bound and report an atree.IndexOutOfBoundsError @@ -2125,7 +2125,7 @@ func (v *ArrayValue) RemoveKey(interpreter *Interpreter, locationRange LocationR func (v *ArrayValue) Remove(interpreter *Interpreter, locationRange LocationRange, index int) Value { - interpreter.validateMutation(v.StorageID(), locationRange) + interpreter.validateMutation(v.SlabID(), locationRange) // We only need to check the lower bound before converting from `int` (signed) to `uint64` (unsigned). // atree's Array.Remove function will check the upper bound and report an atree.IndexOutOfBoundsError @@ -2586,7 +2586,7 @@ func (v *ArrayValue) Transfer( }() } - currentStorageID := v.StorageID() + currentSlabID := v.SlabID() array := v.array @@ -2655,11 +2655,11 @@ func (v *ArrayValue) Transfer( res = v } - newStorageID := array.StorageID() + newSlabID := array.SlabID() interpreter.updateReferencedResource( - currentStorageID, - newStorageID, + currentSlabID, + newSlabID, func(value ReferenceTrackedResourceKindedValue) { arrayValue, ok := value.(*ArrayValue) if !ok { @@ -2759,8 +2759,8 @@ func (v *ArrayValue) DeepRemove(interpreter *Interpreter) { interpreter.maybeValidateAtreeValue(v.array) } -func (v *ArrayValue) StorageID() atree.StorageID { - return v.array.StorageID() +func (v *ArrayValue) SlabID() atree.SlabID { + return v.array.SlabID() } func (v *ArrayValue) StorageAddress() atree.Address { @@ -15364,10 +15364,10 @@ func (v *CompositeValue) Destroy(interpreter *Interpreter, locationRange Locatio }() } - storageID := v.StorageID() + slabID := v.SlabID() interpreter.withResourceDestruction( - storageID, + slabID, locationRange, func() { // if this type has attachments, destroy all of them before invoking the destructor @@ -15419,8 +15419,8 @@ func (v *CompositeValue) Destroy(interpreter *Interpreter, locationRange Locatio } interpreter.updateReferencedResource( - storageID, - storageID, + slabID, + slabID, func(value ReferenceTrackedResourceKindedValue) { compositeValue, ok := value.(*CompositeValue) if !ok { @@ -16077,7 +16077,7 @@ func (v *CompositeValue) Transfer( }() } - currentStorageID := v.StorageID() + currentSlabID := v.SlabID() currentAddress := v.StorageAddress() dictionary := v.dictionary @@ -16170,11 +16170,11 @@ func (v *CompositeValue) Transfer( res = v } - newStorageID := dictionary.StorageID() + newSlabID := dictionary.SlabID() interpreter.updateReferencedResource( - currentStorageID, - newStorageID, + currentSlabID, + newSlabID, func(value ReferenceTrackedResourceKindedValue) { compositeValue, ok := value.(*CompositeValue) if !ok { @@ -16351,8 +16351,8 @@ func (v *CompositeValue) ForEachField(gauge common.MemoryGauge, f func(fieldName } } -func (v *CompositeValue) StorageID() atree.StorageID { - return v.dictionary.StorageID() +func (v *CompositeValue) SlabID() atree.SlabID { + return v.dictionary.SlabID() } func (v *CompositeValue) StorageAddress() atree.Address { @@ -16446,7 +16446,7 @@ func (v *CompositeValue) setBaseValue(interpreter *Interpreter, base *CompositeV // the base reference can only be borrowed with the declared type of the attachment's base v.base = NewEphemeralReferenceValue(interpreter, false, base, baseType) - interpreter.trackReferencedResourceKindedValue(base.StorageID(), base) + interpreter.trackReferencedResourceKindedValue(base.SlabID(), base) } func attachmentMemberName(ty sema.Type) string { @@ -16476,7 +16476,7 @@ func attachmentBaseAndSelfValues( base = v.getBaseValue(interpreter, locationRange) // in attachment functions, self is a reference value self = NewEphemeralReferenceValue(interpreter, false, v, interpreter.MustSemaTypeOfValue(v)) - interpreter.trackReferencedResourceKindedValue(v.StorageID(), v) + interpreter.trackReferencedResourceKindedValue(v.SlabID(), v) return } @@ -16527,7 +16527,7 @@ func (v *CompositeValue) GetTypeKey( attachment.setBaseValue(interpreter, v) attachmentRef := NewEphemeralReferenceValue(interpreter, false, attachment, ty) - interpreter.trackReferencedResourceKindedValue(attachment.StorageID(), attachment) + interpreter.trackReferencedResourceKindedValue(attachment.SlabID(), attachment) return NewSomeValueNonCopying(interpreter, attachmentRef) } @@ -16736,7 +16736,7 @@ func (v *DictionaryValue) Iterate(interpreter *Interpreter, f func(key, value Va } } if v.IsResourceKinded(interpreter) { - interpreter.withMutationPrevention(v.StorageID(), iterate) + interpreter.withMutationPrevention(v.SlabID(), iterate) } else { iterate() } @@ -16834,10 +16834,10 @@ func (v *DictionaryValue) Destroy(interpreter *Interpreter, locationRange Locati }() } - storageID := v.StorageID() + slabID := v.SlabID() interpreter.withResourceDestruction( - storageID, + slabID, locationRange, func() { v.Iterate(interpreter, func(key, value Value) (resume bool) { @@ -16857,8 +16857,8 @@ func (v *DictionaryValue) Destroy(interpreter *Interpreter, locationRange Locati } interpreter.updateReferencedResource( - storageID, - storageID, + slabID, + slabID, func(value ReferenceTrackedResourceKindedValue) { dictionaryValue, ok := value.(*DictionaryValue) if !ok { @@ -16913,7 +16913,7 @@ func (v *DictionaryValue) ForEachKey( } if v.IsResourceKinded(interpreter) { - interpreter.withMutationPrevention(v.StorageID(), iterate) + interpreter.withMutationPrevention(v.SlabID(), iterate) } else { iterate() } @@ -16985,7 +16985,7 @@ func (v *DictionaryValue) SetKey( keyValue Value, value Value, ) { - interpreter.validateMutation(v.StorageID(), locationRange) + interpreter.validateMutation(v.SlabID(), locationRange) config := interpreter.SharedState.Config @@ -17269,7 +17269,7 @@ func (v *DictionaryValue) Remove( keyValue Value, ) OptionalValue { - interpreter.validateMutation(v.StorageID(), locationRange) + interpreter.validateMutation(v.SlabID(), locationRange) valueComparator := newValueComparator(interpreter, locationRange) hashInputProvider := newHashInputProvider(interpreter, locationRange) @@ -17326,7 +17326,7 @@ func (v *DictionaryValue) Insert( keyValue, value Value, ) OptionalValue { - interpreter.validateMutation(v.StorageID(), locationRange) + interpreter.validateMutation(v.SlabID(), locationRange) // length increases by 1 dataSlabs, metaDataSlabs := common.AdditionalAtreeMemoryUsage(v.dictionary.Count(), v.elementSize, false) @@ -17579,7 +17579,7 @@ func (v *DictionaryValue) Transfer( }() } - currentStorageID := v.StorageID() + currentSlabID := v.SlabID() dictionary := v.dictionary @@ -17663,11 +17663,11 @@ func (v *DictionaryValue) Transfer( res = v } - newStorageID := dictionary.StorageID() + newSlabID := dictionary.SlabID() interpreter.updateReferencedResource( - currentStorageID, - newStorageID, + currentSlabID, + newSlabID, func(value ReferenceTrackedResourceKindedValue) { dictionaryValue, ok := value.(*DictionaryValue) if !ok { @@ -17786,8 +17786,8 @@ func (v *DictionaryValue) GetOwner() common.Address { return common.Address(v.StorageAddress()) } -func (v *DictionaryValue) StorageID() atree.StorageID { - return v.dictionary.StorageID() +func (v *DictionaryValue) SlabID() atree.SlabID { + return v.dictionary.SlabID() } func (v *DictionaryValue) StorageAddress() atree.Address { diff --git a/runtime/runtime_test.go b/runtime/runtime_test.go index 35d9fe1f71..18a96e1b2b 100644 --- a/runtime/runtime_test.go +++ b/runtime/runtime_test.go @@ -51,11 +51,11 @@ import ( ) type testLedger struct { - storedValues map[string][]byte - valueExists func(owner, key []byte) (exists bool, err error) - getValue func(owner, key []byte) (value []byte, err error) - setValue func(owner, key, value []byte) (err error) - allocateStorageIndex func(owner []byte) (atree.StorageIndex, error) + storedValues map[string][]byte + valueExists func(owner, key []byte) (exists bool, err error) + getValue func(owner, key []byte) (value []byte, err error) + setValue func(owner, key, value []byte) (err error) + allocateSlabIndex func(owner []byte) (atree.SlabIndex, error) } var _ atree.Ledger = testLedger{} @@ -72,8 +72,8 @@ func (s testLedger) ValueExists(owner, key []byte) (exists bool, err error) { return s.valueExists(owner, key) } -func (s testLedger) AllocateStorageIndex(owner []byte) (atree.StorageIndex, error) { - return s.allocateStorageIndex(owner) +func (s testLedger) AllocateSlabIndex(owner []byte) (atree.SlabIndex, error) { + return s.allocateSlabIndex(owner) } func (s testLedger) Dump() { @@ -117,7 +117,7 @@ func newTestLedger( } return nil }, - allocateStorageIndex: func(owner []byte) (result atree.StorageIndex, err error) { + allocateSlabIndex: func(owner []byte) (result atree.SlabIndex, err error) { index := storageIndices[string(owner)] + 1 storageIndices[string(owner)] = index binary.BigEndian.PutUint64(result[:], index) @@ -327,11 +327,11 @@ func (i *testRuntimeInterface) SetValue(owner, key, value []byte) (err error) { return i.storage.SetValue(owner, key, value) } -func (i *testRuntimeInterface) AllocateStorageIndex(owner []byte) (atree.StorageIndex, error) { - if i.storage.allocateStorageIndex == nil { +func (i *testRuntimeInterface) AllocateSlabIndex(owner []byte) (atree.SlabIndex, error) { + if i.storage.allocateSlabIndex == nil { panic("must specify testRuntimeInterface.storage.allocateStorageIndex") } - return i.storage.AllocateStorageIndex(owner) + return i.storage.AllocateSlabIndex(owner) } func (i *testRuntimeInterface) CreateAccount(payer Address) (address Address, err error) { diff --git a/runtime/storage.go b/runtime/storage.go index 53ee07c2e4..0c2e0a93ee 100644 --- a/runtime/storage.go +++ b/runtime/storage.go @@ -35,7 +35,7 @@ const StorageDomainContract = "contract" type Storage struct { *atree.PersistentSlabStorage - newStorageMaps *orderedmap.OrderedMap[interpreter.StorageKey, atree.StorageIndex] + newStorageMaps *orderedmap.OrderedMap[interpreter.StorageKey, atree.SlabIndex] storageMaps map[interpreter.StorageKey]*interpreter.StorageMap contractUpdates *orderedmap.OrderedMap[interpreter.StorageKey, *interpreter.CompositeValue] Ledger atree.Ledger @@ -48,14 +48,14 @@ var _ interpreter.Storage = &Storage{} func NewStorage(ledger atree.Ledger, memoryGauge common.MemoryGauge) *Storage { decodeStorable := func( decoder *cbor.StreamDecoder, - slabStorageID atree.StorageID, + slabID atree.SlabID, ) ( atree.Storable, error, ) { return interpreter.DecodeStorable( decoder, - slabStorageID, + slabID, memoryGauge, ) } @@ -120,9 +120,9 @@ func (s *Storage) GetStorageMap( atreeAddress := atree.Address(address) if isStorageIndex { - var storageIndex atree.StorageIndex - copy(storageIndex[:], data[:]) - storageMap = s.loadExistingStorageMap(atreeAddress, storageIndex) + var slabIndex atree.SlabIndex + copy(slabIndex[:], data[:]) + storageMap = s.loadExistingStorageMap(atreeAddress, slabIndex) } else if createIfNotExists { storageMap = s.storeNewStorageMap(atreeAddress, domain) } @@ -135,27 +135,24 @@ func (s *Storage) GetStorageMap( return storageMap } -func (s *Storage) loadExistingStorageMap(address atree.Address, storageIndex atree.StorageIndex) *interpreter.StorageMap { +func (s *Storage) loadExistingStorageMap(address atree.Address, slabIndex atree.SlabIndex) *interpreter.StorageMap { - storageID := atree.StorageID{ - Address: address, - Index: storageIndex, - } + slabID := atree.NewSlabID(address, slabIndex) - return interpreter.NewStorageMapWithRootID(s, storageID) + return interpreter.NewStorageMapWithRootID(s, slabID) } func (s *Storage) storeNewStorageMap(address atree.Address, domain string) *interpreter.StorageMap { storageMap := interpreter.NewStorageMap(s.memoryGauge, s, address) - storageIndex := storageMap.StorageID().Index + slabIndex := storageMap.SlabID().Index() storageKey := interpreter.NewStorageKey(s.memoryGauge, common.Address(address), domain) if s.newStorageMaps == nil { - s.newStorageMaps = &orderedmap.OrderedMap[interpreter.StorageKey, atree.StorageIndex]{} + s.newStorageMaps = &orderedmap.OrderedMap[interpreter.StorageKey, atree.SlabIndex]{} } - s.newStorageMaps.Set(storageKey, storageIndex) + s.newStorageMaps.Set(storageKey, slabIndex) return storageMap } @@ -274,11 +271,11 @@ func (s *Storage) CheckHealth() error { // Find account / non-temporary root slab IDs - accountRootSlabIDs := make(map[atree.StorageID]struct{}, len(rootSlabIDs)) + accountRootSlabIDs := make(map[atree.SlabID]struct{}, len(rootSlabIDs)) // NOTE: map range is safe, as it creates a subset for rootSlabID := range rootSlabIDs { //nolint:maprange - if rootSlabID.Address == (atree.Address{}) { + if rootSlabID.HasTempAddress() { continue } @@ -287,14 +284,14 @@ func (s *Storage) CheckHealth() error { // Check that each storage map refers to an existing slab. - found := map[atree.StorageID]struct{}{} + found := map[atree.SlabID]struct{}{} - var storageMapStorageIDs []atree.StorageID + var storageMapStorageIDs []atree.SlabID for _, storageMap := range s.storageMaps { //nolint:maprange storageMapStorageIDs = append( storageMapStorageIDs, - storageMap.StorageID(), + storageMap.SlabID(), ) } @@ -317,7 +314,7 @@ func (s *Storage) CheckHealth() error { // If a slab is not referenced, it is garbage. if len(accountRootSlabIDs) > len(found) { - var unreferencedRootSlabIDs []atree.StorageID + var unreferencedRootSlabIDs []atree.SlabID for accountRootSlabID := range accountRootSlabIDs { //nolint:maprange if _, ok := found[accountRootSlabID]; ok { diff --git a/runtime/storage_test.go b/runtime/storage_test.go index 93f8c09883..a2f313119d 100644 --- a/runtime/storage_test.go +++ b/runtime/storage_test.go @@ -62,13 +62,13 @@ func withWritesToStorage( Key: fmt.Sprintf("%d", randomIndex), } - var storageIndex atree.StorageIndex - binary.BigEndian.PutUint32(storageIndex[:], randomIndex) + var slabIndex atree.SlabIndex + binary.BigEndian.PutUint32(slabIndex[:], randomIndex) if storage.newStorageMaps == nil { - storage.newStorageMaps = &orderedmap.OrderedMap[interpreter.StorageKey, atree.StorageIndex]{} + storage.newStorageMaps = &orderedmap.OrderedMap[interpreter.StorageKey, atree.SlabIndex]{} } - storage.newStorageMaps.Set(storageKey, storageIndex) + storage.newStorageMaps.Set(storageKey, slabIndex) } handler(storage, inter) diff --git a/runtime/tests/interpreter/interpreter_test.go b/runtime/tests/interpreter/interpreter_test.go index 5e56e230bd..a34e265ce6 100644 --- a/runtime/tests/interpreter/interpreter_test.go +++ b/runtime/tests/interpreter/interpreter_test.go @@ -7958,7 +7958,7 @@ func TestInterpretResourceMovingAndBorrowing(t *testing.T) { var permanentSlabs []atree.Slab for _, slab := range storage.(interpreter.InMemoryStorage).Slabs { - if slab.ID().Address == (atree.Address{}) { + if slab.SlabID().HasTempAddress() { continue } @@ -7968,8 +7968,8 @@ func TestInterpretResourceMovingAndBorrowing(t *testing.T) { require.Equal(t, 2, len(permanentSlabs)) sort.Slice(permanentSlabs, func(i, j int) bool { - a := permanentSlabs[i].ID() - b := permanentSlabs[j].ID() + a := permanentSlabs[i].SlabID() + b := permanentSlabs[j].SlabID() return a.Compare(b) < 0 }) diff --git a/runtime/tests/interpreter/values_test.go b/runtime/tests/interpreter/values_test.go index 0c13a00ce0..36f1dd3416 100644 --- a/runtime/tests/interpreter/values_test.go +++ b/runtime/tests/interpreter/values_test.go @@ -170,7 +170,7 @@ func TestRandomMapOperations(t *testing.T) { t.Run("deep remove", func(t *testing.T) { copyOfTestMap.DeepRemove(inter) - err = storage.Remove(copyOfTestMap.StorageID()) + err = storage.Remove(copyOfTestMap.SlabID()) require.NoError(t, err) // deep removal should clean up everything @@ -493,7 +493,7 @@ func TestRandomMapOperations(t *testing.T) { require.Equal(t, entries.size(), movedDictionary.Count()) // Cleanup the slab of original dictionary. - err := storage.Remove(dictionary.StorageID()) + err := storage.Remove(dictionary.SlabID()) require.NoError(t, err) // Check the values @@ -618,7 +618,7 @@ func TestRandomArrayOperations(t *testing.T) { t.Run("deep removal", func(t *testing.T) { copyOfTestArray.DeepRemove(inter) - err = storage.Remove(copyOfTestArray.StorageID()) + err = storage.Remove(copyOfTestArray.SlabID()) require.NoError(t, err) // deep removal should clean up everything @@ -863,7 +863,7 @@ func TestRandomArrayOperations(t *testing.T) { require.Equal(t, len(elements), movedArray.Count()) // Cleanup the slab of original array. - err := storage.Remove(array.StorageID()) + err := storage.Remove(array.SlabID()) require.NoError(t, err) // Check the elements @@ -959,7 +959,7 @@ func TestRandomCompositeValueOperations(t *testing.T) { t.Run("deep remove", func(t *testing.T) { copyOfTestComposite.DeepRemove(inter) - err = storage.Remove(copyOfTestComposite.StorageID()) + err = storage.Remove(copyOfTestComposite.SlabID()) require.NoError(t, err) // deep removal should clean up everything @@ -1013,7 +1013,7 @@ func TestRandomCompositeValueOperations(t *testing.T) { ).(*interpreter.CompositeValue) // Cleanup the slab of original composite. - err := storage.Remove(composite.StorageID()) + err := storage.Remove(composite.SlabID()) require.NoError(t, err) // Check the elements @@ -1110,7 +1110,7 @@ func getSlabStorageSize(t *testing.T, storage interpreter.InMemoryStorage) (tota require.NoError(t, err) for id, slab := range slabs { - if id.Address == atree.AddressUndefined { + if id.HasTempAddress() { continue }