diff --git a/go.mod b/go.mod index 2339967f78..17c5c7263a 100644 --- a/go.mod +++ b/go.mod @@ -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.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 04b19e296b..da76bee7f2 100644 --- a/go.sum +++ b/go.sum @@ -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.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/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= diff --git a/runtime/cmd/decode-state-values/main.go b/runtime/cmd/decode-state-values/main.go index 259f852a0c..cea05340a2 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 } @@ -312,20 +319,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 } @@ -343,7 +347,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 7f39261d4a..10664578e6 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) // AddAccountKey appends a key to an account. diff --git a/runtime/interpreter/decode.go b/runtime/interpreter/decode.go index cc7e777783..a7f0c9f97f 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 fe01821e53..233721cceb 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 6780afc98c..8eeada67e4 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, @@ -3354,7 +3354,7 @@ func TestEncodeDecodeCapabilityValue(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, @@ -3533,7 +3533,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, @@ -4046,7 +4046,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, @@ -4252,7 +4252,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 bf4da92c18..0dd755e255 100644 --- a/runtime/interpreter/interpreter.go +++ b/runtime/interpreter/interpreter.go @@ -217,7 +217,7 @@ type Storage interface { CheckHealth() error } -type ReferencedResourceKindedValues map[atree.StorageID]map[*EphemeralReferenceValue]struct{} +type ReferencedResourceKindedValues map[atree.SlabID]map[*EphemeralReferenceValue]struct{} type Interpreter struct { Location common.Location @@ -5012,13 +5012,13 @@ func (interpreter *Interpreter) checkContainerMutation( } 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)) } @@ -5172,13 +5172,13 @@ func (interpreter *Interpreter) ValidateAtreeValue(value atree.Value) { func (interpreter *Interpreter) maybeTrackReferencedResourceKindedValue(value Value) { if referenceValue, ok := value.(*EphemeralReferenceValue); ok { if value, ok := referenceValue.Value.(ReferenceTrackedResourceKindedValue); ok { - interpreter.trackReferencedResourceKindedValue(value.StorageID(), referenceValue) + interpreter.trackReferencedResourceKindedValue(value.SlabID(), referenceValue) } } } func (interpreter *Interpreter) trackReferencedResourceKindedValue( - id atree.StorageID, + id atree.SlabID, value *EphemeralReferenceValue, ) { values := interpreter.SharedState.referencedResourceKindedValues[id] @@ -5196,7 +5196,7 @@ func (interpreter *Interpreter) invalidateReferencedResources(value Value) { return } - var storageID atree.StorageID + var slabID atree.SlabID switch value := value.(type) { case *CompositeValue: @@ -5205,28 +5205,32 @@ func (interpreter *Interpreter) invalidateReferencedResources(value Value) { // continue iteration return true }) - storageID = value.StorageID() + slabID = value.SlabID() + case *DictionaryValue: value.IterateLoaded(interpreter, func(_, value Value) (resume bool) { interpreter.invalidateReferencedResources(value) return true }) - storageID = value.StorageID() + slabID = value.SlabID() + case *ArrayValue: value.IterateLoaded(interpreter, func(element Value) (resume bool) { interpreter.invalidateReferencedResources(element) return true }) - storageID = value.StorageID() + slabID = value.SlabID() + case *SomeValue: interpreter.invalidateReferencedResources(value.value) return + default: // skip non-container typed values. return } - values := interpreter.SharedState.referencedResourceKindedValues[storageID] + values := interpreter.SharedState.referencedResourceKindedValues[slabID] if values == nil { return } @@ -5239,7 +5243,7 @@ func (interpreter *Interpreter) invalidateReferencedResources(value Value) { // So no need to track those stale resources anymore. We will not need to update/clear them again. // Therefore, remove them from the mapping. // This is only to allow GC. No impact to the behavior. - delete(interpreter.SharedState.referencedResourceKindedValues, storageID) + delete(interpreter.SharedState.referencedResourceKindedValues, slabID) } // startResourceTracking starts tracking the life-span of a resource. @@ -5351,12 +5355,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) { @@ -5448,8 +5452,8 @@ func (interpreter *Interpreter) capabilityCheckFunction( ) } -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 } @@ -5458,24 +5462,24 @@ 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) enforceNotResourceDestruction( - storageID atree.StorageID, + slabID atree.SlabID, locationRange LocationRange, ) { - _, exists := interpreter.SharedState.destroyedResources[storageID] + _, exists := interpreter.SharedState.destroyedResources[slabID] if exists { panic(DestroyedResourceError{ LocationRange: locationRange, @@ -5484,13 +5488,13 @@ func (interpreter *Interpreter) enforceNotResourceDestruction( } func (interpreter *Interpreter) withResourceDestruction( - storageID atree.StorageID, + slabID atree.SlabID, locationRange LocationRange, f func(), ) { - interpreter.enforceNotResourceDestruction(storageID, locationRange) + interpreter.enforceNotResourceDestruction(slabID, locationRange) - interpreter.SharedState.destroyedResources[storageID] = struct{}{} + interpreter.SharedState.destroyedResources[slabID] = struct{}{} f() } diff --git a/runtime/interpreter/sharedstate.go b/runtime/interpreter/sharedstate.go index 1668d1aa68..d98d9258d6 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{} currentEntitlementMappedValue Authorization } @@ -59,11 +59,11 @@ func NewSharedState(config *Config) *SharedState { }, inStorageIteration: false, storageMutatedDuringIteration: false, - referencedResourceKindedValues: map[atree.StorageID]map[*EphemeralReferenceValue]struct{}{}, + referencedResourceKindedValues: map[atree.SlabID]map[*EphemeralReferenceValue]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/simplecompositevalue.go b/runtime/interpreter/simplecompositevalue.go index 789db2fe9e..653b37c245 100644 --- a/runtime/interpreter/simplecompositevalue.go +++ b/runtime/interpreter/simplecompositevalue.go @@ -261,7 +261,7 @@ func (v *SimpleCompositeValue) Transfer( _ atree.Address, remove bool, storable atree.Storable, - _ map[atree.StorageID]struct{}, + _ map[atree.SlabID]struct{}, ) Value { // TODO: actually not needed, value is not storable if remove { diff --git a/runtime/interpreter/storage.go b/runtime/interpreter/storage.go index a252321324..780bf6f433 100644 --- a/runtime/interpreter/storage.go +++ b/runtime/interpreter/storage.go @@ -137,7 +137,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) } @@ -227,7 +227,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, @@ -242,20 +242,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 d793f6493f..5d76712975 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) @@ -184,12 +184,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) @@ -207,7 +207,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) @@ -253,12 +253,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) @@ -270,7 +270,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) @@ -309,11 +309,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) @@ -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) @@ -368,11 +368,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) @@ -385,7 +385,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) @@ -418,11 +418,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) @@ -434,7 +434,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) @@ -465,11 +465,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) @@ -482,7 +482,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 07816adb9a..24e5847bc3 100644 --- a/runtime/interpreter/value.go +++ b/runtime/interpreter/value.go @@ -134,7 +134,7 @@ type Value interface { address atree.Address, remove bool, storable atree.Storable, - preventTransfer map[atree.StorageID]struct{}, + preventTransfer map[atree.SlabID]struct{}, ) Value DeepRemove(interpreter *Interpreter) // Clone returns a new value that is equal to this value. @@ -220,7 +220,7 @@ func maybeDestroy(interpreter *Interpreter, locationRange LocationRange, value V type ReferenceTrackedResourceKindedValue interface { ResourceKindedValue IsReferenceTrackedResourceKindedValue() - StorageID() atree.StorageID + SlabID() atree.SlabID IsStaleResource(*Interpreter) bool } @@ -479,7 +479,7 @@ func (v TypeValue) Transfer( _ atree.Address, remove bool, storable atree.Storable, - _ map[atree.StorageID]struct{}, + _ map[atree.SlabID]struct{}, ) Value { if remove { interpreter.RemoveReferencedSlab(storable) @@ -599,7 +599,7 @@ func (v VoidValue) Transfer( _ atree.Address, remove bool, storable atree.Storable, - _ map[atree.StorageID]struct{}, + _ map[atree.SlabID]struct{}, ) Value { if remove { interpreter.RemoveReferencedSlab(storable) @@ -772,7 +772,7 @@ func (v BoolValue) Transfer( _ atree.Address, remove bool, storable atree.Storable, - _ map[atree.StorageID]struct{}, + _ map[atree.SlabID]struct{}, ) Value { if remove { interpreter.RemoveReferencedSlab(storable) @@ -949,7 +949,7 @@ func (v CharacterValue) Transfer( _ atree.Address, remove bool, storable atree.Storable, - _ map[atree.StorageID]struct{}, + _ map[atree.SlabID]struct{}, ) Value { if remove { interpreter.RemoveReferencedSlab(storable) @@ -1511,7 +1511,7 @@ func (v *StringValue) Transfer( _ atree.Address, remove bool, storable atree.Storable, - _ map[atree.StorageID]struct{}, + _ map[atree.SlabID]struct{}, ) Value { if remove { interpreter.RemoveReferencedSlab(storable) @@ -1863,7 +1863,7 @@ func (v *ArrayValue) iterate( } if v.IsResourceKinded(interpreter) { - interpreter.withMutationPrevention(v.StorageID(), iterate) + interpreter.withMutationPrevention(v.SlabID(), iterate) } else { iterate() } @@ -1926,10 +1926,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) { @@ -2062,7 +2062,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 @@ -2085,8 +2085,8 @@ func (v *ArrayValue) Set(interpreter *Interpreter, locationRange LocationRange, v.array.Address(), true, nil, - map[atree.StorageID]struct{}{ - v.StorageID(): {}, + map[atree.SlabID]struct{}{ + v.SlabID(): {}, }, ) @@ -2139,7 +2139,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( @@ -2159,8 +2159,8 @@ func (v *ArrayValue) Append(interpreter *Interpreter, locationRange LocationRang v.array.Address(), true, nil, - map[atree.StorageID]struct{}{ - v.StorageID(): {}, + map[atree.SlabID]struct{}{ + v.SlabID(): {}, }, ) @@ -2184,7 +2184,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 @@ -2215,8 +2215,8 @@ func (v *ArrayValue) Insert(interpreter *Interpreter, locationRange LocationRang v.array.Address(), true, nil, - map[atree.StorageID]struct{}{ - v.StorageID(): {}, + map[atree.SlabID]struct{}{ + v.SlabID(): {}, }, ) @@ -2236,7 +2236,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 @@ -2762,7 +2762,7 @@ func (v *ArrayValue) Transfer( address atree.Address, remove bool, storable atree.Storable, - preventTransfer map[atree.StorageID]struct{}, + preventTransfer map[atree.SlabID]struct{}, ) Value { config := interpreter.SharedState.Config @@ -2787,17 +2787,17 @@ func (v *ArrayValue) Transfer( }() } - currentStorageID := v.StorageID() + currentSlabID := v.SlabID() if preventTransfer == nil { - preventTransfer = map[atree.StorageID]struct{}{} - } else if _, ok := preventTransfer[currentStorageID]; ok { + preventTransfer = map[atree.SlabID]struct{}{} + } else if _, ok := preventTransfer[currentSlabID]; ok { panic(RecursiveTransferError{ LocationRange: locationRange, }) } - preventTransfer[currentStorageID] = struct{}{} - defer delete(preventTransfer, currentStorageID) + preventTransfer[currentSlabID] = struct{}{} + defer delete(preventTransfer, currentSlabID) array := v.array @@ -2962,8 +2962,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 { @@ -4103,7 +4103,7 @@ func (v IntValue) Transfer( _ atree.Address, remove bool, storable atree.Storable, - _ map[atree.StorageID]struct{}, + _ map[atree.SlabID]struct{}, ) Value { if remove { interpreter.RemoveReferencedSlab(storable) @@ -4743,7 +4743,7 @@ func (v Int8Value) Transfer( _ atree.Address, remove bool, storable atree.Storable, - _ map[atree.StorageID]struct{}, + _ map[atree.SlabID]struct{}, ) Value { if remove { interpreter.RemoveReferencedSlab(storable) @@ -5385,7 +5385,7 @@ func (v Int16Value) Transfer( _ atree.Address, remove bool, storable atree.Storable, - _ map[atree.StorageID]struct{}, + _ map[atree.SlabID]struct{}, ) Value { if remove { interpreter.RemoveReferencedSlab(storable) @@ -6027,7 +6027,7 @@ func (v Int32Value) Transfer( _ atree.Address, remove bool, storable atree.Storable, - _ map[atree.StorageID]struct{}, + _ map[atree.SlabID]struct{}, ) Value { if remove { interpreter.RemoveReferencedSlab(storable) @@ -6661,7 +6661,7 @@ func (v Int64Value) Transfer( _ atree.Address, remove bool, storable atree.Storable, - _ map[atree.StorageID]struct{}, + _ map[atree.SlabID]struct{}, ) Value { if remove { interpreter.RemoveReferencedSlab(storable) @@ -7405,7 +7405,7 @@ func (v Int128Value) Transfer( _ atree.Address, remove bool, storable atree.Storable, - _ map[atree.StorageID]struct{}, + _ map[atree.SlabID]struct{}, ) Value { if remove { interpreter.RemoveReferencedSlab(storable) @@ -8146,7 +8146,7 @@ func (v Int256Value) Transfer( _ atree.Address, remove bool, storable atree.Storable, - _ map[atree.StorageID]struct{}, + _ map[atree.SlabID]struct{}, ) Value { if remove { interpreter.RemoveReferencedSlab(storable) @@ -8775,7 +8775,7 @@ func (v UIntValue) Transfer( _ atree.Address, remove bool, storable atree.Storable, - _ map[atree.StorageID]struct{}, + _ map[atree.SlabID]struct{}, ) Value { if remove { interpreter.RemoveReferencedSlab(storable) @@ -9361,7 +9361,7 @@ func (v UInt8Value) Transfer( _ atree.Address, remove bool, storable atree.Storable, - _ map[atree.StorageID]struct{}, + _ map[atree.SlabID]struct{}, ) Value { if remove { interpreter.RemoveReferencedSlab(storable) @@ -9902,7 +9902,7 @@ func (v UInt16Value) Transfer( _ atree.Address, remove bool, storable atree.Storable, - _ map[atree.StorageID]struct{}, + _ map[atree.SlabID]struct{}, ) Value { if remove { interpreter.RemoveReferencedSlab(storable) @@ -10444,7 +10444,7 @@ func (v UInt32Value) Transfer( _ atree.Address, remove bool, storable atree.Storable, - _ map[atree.StorageID]struct{}, + _ map[atree.SlabID]struct{}, ) Value { if remove { interpreter.RemoveReferencedSlab(storable) @@ -11015,7 +11015,7 @@ func (v UInt64Value) Transfer( _ atree.Address, remove bool, storable atree.Storable, - _ map[atree.StorageID]struct{}, + _ map[atree.SlabID]struct{}, ) Value { if remove { interpreter.RemoveReferencedSlab(storable) @@ -11690,7 +11690,7 @@ func (v UInt128Value) Transfer( _ atree.Address, remove bool, storable atree.Storable, - _ map[atree.StorageID]struct{}, + _ map[atree.SlabID]struct{}, ) Value { if remove { interpreter.RemoveReferencedSlab(storable) @@ -12364,7 +12364,7 @@ func (v UInt256Value) Transfer( _ atree.Address, remove bool, storable atree.Storable, - _ map[atree.StorageID]struct{}, + _ map[atree.SlabID]struct{}, ) Value { if remove { interpreter.RemoveReferencedSlab(storable) @@ -12801,7 +12801,7 @@ func (v Word8Value) Transfer( _ atree.Address, remove bool, storable atree.Storable, - _ map[atree.StorageID]struct{}, + _ map[atree.SlabID]struct{}, ) Value { if remove { interpreter.RemoveReferencedSlab(storable) @@ -13239,7 +13239,7 @@ func (v Word16Value) Transfer( _ atree.Address, remove bool, storable atree.Storable, - _ map[atree.StorageID]struct{}, + _ map[atree.SlabID]struct{}, ) Value { if remove { interpreter.RemoveReferencedSlab(storable) @@ -13678,7 +13678,7 @@ func (v Word32Value) Transfer( _ atree.Address, remove bool, storable atree.Storable, - _ map[atree.StorageID]struct{}, + _ map[atree.SlabID]struct{}, ) Value { if remove { interpreter.RemoveReferencedSlab(storable) @@ -14143,7 +14143,7 @@ func (v Word64Value) Transfer( _ atree.Address, remove bool, storable atree.Storable, - _ map[atree.StorageID]struct{}, + _ map[atree.SlabID]struct{}, ) Value { if remove { interpreter.RemoveReferencedSlab(storable) @@ -14723,7 +14723,7 @@ func (v Word128Value) Transfer( _ atree.Address, remove bool, storable atree.Storable, - _ map[atree.StorageID]struct{}, + _ map[atree.SlabID]struct{}, ) Value { if remove { interpreter.RemoveReferencedSlab(storable) @@ -15304,7 +15304,7 @@ func (v Word256Value) Transfer( _ atree.Address, remove bool, storable atree.Storable, - _ map[atree.StorageID]struct{}, + _ map[atree.SlabID]struct{}, ) Value { if remove { interpreter.RemoveReferencedSlab(storable) @@ -15881,7 +15881,7 @@ func (v Fix64Value) Transfer( _ atree.Address, remove bool, storable atree.Storable, - _ map[atree.StorageID]struct{}, + _ map[atree.SlabID]struct{}, ) Value { if remove { interpreter.RemoveReferencedSlab(storable) @@ -16415,7 +16415,7 @@ func (v UFix64Value) Transfer( _ atree.Address, remove bool, storable atree.Storable, - _ map[atree.StorageID]struct{}, + _ map[atree.SlabID]struct{}, ) Value { if remove { interpreter.RemoveReferencedSlab(storable) @@ -16801,10 +16801,10 @@ func (v *CompositeValue) Destroy(interpreter *Interpreter, locationRange Locatio defer interpreter.emitEvent(event, eventType, locationRange) } - storageID := v.StorageID() + slabID := v.SlabID() interpreter.withResourceDestruction( - storageID, + slabID, locationRange, func() { interpreter = v.getInterpreter(interpreter) @@ -17100,7 +17100,7 @@ func (v *CompositeValue) SetMember( ) bool { config := interpreter.SharedState.Config - interpreter.enforceNotResourceDestruction(v.StorageID(), locationRange) + interpreter.enforceNotResourceDestruction(v.SlabID(), locationRange) if config.TracingEnabled { startTime := time.Now() @@ -17128,8 +17128,8 @@ func (v *CompositeValue) SetMember( address, true, nil, - map[atree.StorageID]struct{}{ - v.StorageID(): {}, + map[atree.SlabID]struct{}{ + v.SlabID(): {}, }, ) @@ -17533,7 +17533,7 @@ func (v *CompositeValue) Transfer( address atree.Address, remove bool, storable atree.Storable, - preventTransfer map[atree.StorageID]struct{}, + preventTransfer map[atree.SlabID]struct{}, ) Value { config := interpreter.SharedState.Config @@ -17557,18 +17557,18 @@ func (v *CompositeValue) Transfer( }() } - currentStorageID := v.StorageID() + currentSlabID := v.SlabID() currentAddress := v.StorageAddress() if preventTransfer == nil { - preventTransfer = map[atree.StorageID]struct{}{} - } else if _, ok := preventTransfer[currentStorageID]; ok { + preventTransfer = map[atree.SlabID]struct{}{} + } else if _, ok := preventTransfer[currentSlabID]; ok { panic(RecursiveTransferError{ LocationRange: locationRange, }) } - preventTransfer[currentStorageID] = struct{}{} - defer delete(preventTransfer, currentStorageID) + preventTransfer[currentSlabID] = struct{}{} + defer delete(preventTransfer, currentSlabID) dictionary := v.dictionary @@ -17879,8 +17879,8 @@ func (v *CompositeValue) forEachField( } } -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 { @@ -18053,7 +18053,13 @@ func attachmentBaseAndSelfValues( base = v.getBaseValue(interpreter, attachmentReferenceAuth, locationRange) // in attachment functions, self is a reference value - self = NewEphemeralReferenceValue(interpreter, attachmentReferenceAuth, v, interpreter.MustSemaTypeOfValue(v), locationRange) + self = NewEphemeralReferenceValue( + interpreter, + attachmentReferenceAuth, + v, + interpreter.MustSemaTypeOfValue(v), + locationRange, + ) return } @@ -18509,7 +18515,7 @@ func (v *DictionaryValue) iterateKeys( } } if v.IsResourceKinded(interpreter) { - interpreter.withMutationPrevention(v.StorageID(), iterate) + interpreter.withMutationPrevention(v.SlabID(), iterate) } else { iterate() } @@ -18545,7 +18551,7 @@ func (v *DictionaryValue) iterate( } } if v.IsResourceKinded(interpreter) { - interpreter.withMutationPrevention(v.StorageID(), iterate) + interpreter.withMutationPrevention(v.SlabID(), iterate) } else { iterate() } @@ -18639,10 +18645,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) { @@ -18702,7 +18708,7 @@ func (v *DictionaryValue) ForEachKey( } if v.IsResourceKinded(interpreter) { - interpreter.withMutationPrevention(v.StorageID(), iterate) + interpreter.withMutationPrevention(v.SlabID(), iterate) } else { iterate() } @@ -18768,7 +18774,7 @@ func (v *DictionaryValue) SetKey( keyValue Value, value Value, ) { - interpreter.validateMutation(v.StorageID(), locationRange) + interpreter.validateMutation(v.SlabID(), locationRange) interpreter.checkContainerMutation(v.Type.KeyType, keyValue, locationRange) interpreter.checkContainerMutation( @@ -19041,7 +19047,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) @@ -19099,7 +19105,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) @@ -19112,8 +19118,8 @@ func (v *DictionaryValue) Insert( address := v.dictionary.Address() - preventTransfer := map[atree.StorageID]struct{}{ - v.StorageID(): {}, + preventTransfer := map[atree.SlabID]struct{}{ + v.SlabID(): {}, } keyValue = keyValue.Transfer( @@ -19326,7 +19332,7 @@ func (v *DictionaryValue) Transfer( address atree.Address, remove bool, storable atree.Storable, - preventTransfer map[atree.StorageID]struct{}, + preventTransfer map[atree.SlabID]struct{}, ) Value { config := interpreter.SharedState.Config @@ -19351,17 +19357,17 @@ func (v *DictionaryValue) Transfer( }() } - currentStorageID := v.StorageID() + currentSlabID := v.SlabID() if preventTransfer == nil { - preventTransfer = map[atree.StorageID]struct{}{} - } else if _, ok := preventTransfer[currentStorageID]; ok { + preventTransfer = map[atree.SlabID]struct{}{} + } else if _, ok := preventTransfer[currentSlabID]; ok { panic(RecursiveTransferError{ LocationRange: locationRange, }) } - preventTransfer[currentStorageID] = struct{}{} - defer delete(preventTransfer, currentStorageID) + preventTransfer[currentSlabID] = struct{}{} + defer delete(preventTransfer, currentSlabID) dictionary := v.dictionary @@ -19567,8 +19573,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 { @@ -19729,7 +19735,7 @@ func (v NilValue) Transfer( _ atree.Address, remove bool, storable atree.Storable, - _ map[atree.StorageID]struct{}, + _ map[atree.SlabID]struct{}, ) Value { if remove { interpreter.RemoveReferencedSlab(storable) @@ -19991,7 +19997,7 @@ func (v *SomeValue) Transfer( address atree.Address, remove bool, storable atree.Storable, - preventTransfer map[atree.StorageID]struct{}, + preventTransfer map[atree.SlabID]struct{}, ) Value { innerValue := v.value @@ -20455,7 +20461,7 @@ func (v *StorageReferenceValue) Transfer( _ atree.Address, remove bool, storable atree.Storable, - _ map[atree.StorageID]struct{}, + _ map[atree.SlabID]struct{}, ) Value { if remove { interpreter.RemoveReferencedSlab(storable) @@ -20824,7 +20830,7 @@ func (v *EphemeralReferenceValue) Transfer( _ atree.Address, remove bool, storable atree.Storable, - _ map[atree.StorageID]struct{}, + _ map[atree.SlabID]struct{}, ) Value { if remove { interpreter.RemoveReferencedSlab(storable) @@ -21061,7 +21067,7 @@ func (v AddressValue) Transfer( _ atree.Address, remove bool, storable atree.Storable, - _ map[atree.StorageID]struct{}, + _ map[atree.SlabID]struct{}, ) Value { if remove { interpreter.RemoveReferencedSlab(storable) @@ -21347,7 +21353,7 @@ func (v PathValue) Transfer( _ atree.Address, remove bool, storable atree.Storable, - _ map[atree.StorageID]struct{}, + _ map[atree.SlabID]struct{}, ) Value { if remove { interpreter.RemoveReferencedSlab(storable) @@ -21480,7 +21486,7 @@ func (v *PublishedValue) Transfer( address atree.Address, remove bool, storable atree.Storable, - preventTransfer map[atree.StorageID]struct{}, + preventTransfer map[atree.SlabID]struct{}, ) Value { // NB: if the inner value of a PublishedValue can be a resource, // we must perform resource-related checks here as well diff --git a/runtime/interpreter/value_accountcapabilitycontroller.go b/runtime/interpreter/value_accountcapabilitycontroller.go index 55e4f088dd..b7b821a8d7 100644 --- a/runtime/interpreter/value_accountcapabilitycontroller.go +++ b/runtime/interpreter/value_accountcapabilitycontroller.go @@ -178,7 +178,7 @@ func (v *AccountCapabilityControllerValue) Transfer( _ atree.Address, remove bool, storable atree.Storable, - _ map[atree.StorageID]struct{}, + _ map[atree.SlabID]struct{}, ) Value { if remove { interpreter.RemoveReferencedSlab(storable) diff --git a/runtime/interpreter/value_capability.go b/runtime/interpreter/value_capability.go index c9b20326c7..59e1908552 100644 --- a/runtime/interpreter/value_capability.go +++ b/runtime/interpreter/value_capability.go @@ -189,7 +189,7 @@ func (v *CapabilityValue) Transfer( _ atree.Address, remove bool, storable atree.Storable, - _ map[atree.StorageID]struct{}, + _ map[atree.SlabID]struct{}, ) Value { if remove { v.DeepRemove(interpreter) diff --git a/runtime/interpreter/value_function.go b/runtime/interpreter/value_function.go index e4ce9f64fe..4b7fb422d6 100644 --- a/runtime/interpreter/value_function.go +++ b/runtime/interpreter/value_function.go @@ -151,7 +151,7 @@ func (f *InterpretedFunctionValue) Transfer( _ atree.Address, remove bool, storable atree.Storable, - _ map[atree.StorageID]struct{}, + _ map[atree.SlabID]struct{}, ) Value { // TODO: actually not needed, value is not storable if remove { @@ -300,7 +300,7 @@ func (f *HostFunctionValue) Transfer( _ atree.Address, remove bool, storable atree.Storable, - _ map[atree.StorageID]struct{}, + _ map[atree.SlabID]struct{}, ) Value { // TODO: actually not needed, value is not storable if remove { @@ -441,7 +441,7 @@ func (f BoundFunctionValue) Transfer( _ atree.Address, remove bool, storable atree.Storable, - _ map[atree.StorageID]struct{}, + _ map[atree.SlabID]struct{}, ) Value { // TODO: actually not needed, value is not storable if remove { diff --git a/runtime/interpreter/value_link.go b/runtime/interpreter/value_link.go index 30de6de9c2..73921014d3 100644 --- a/runtime/interpreter/value_link.go +++ b/runtime/interpreter/value_link.go @@ -118,7 +118,7 @@ func (v PathLinkValue) Transfer( _ atree.Address, remove bool, storable atree.Storable, - _ map[atree.StorageID]struct{}, + _ map[atree.SlabID]struct{}, ) Value { if remove { interpreter.RemoveReferencedSlab(storable) @@ -223,7 +223,7 @@ func (v AccountLinkValue) Transfer( _ atree.Address, remove bool, storable atree.Storable, - _ map[atree.StorageID]struct{}, + _ map[atree.SlabID]struct{}, ) Value { if remove { interpreter.RemoveReferencedSlab(storable) diff --git a/runtime/interpreter/value_pathcapability.go b/runtime/interpreter/value_pathcapability.go index eabc1dbd6a..377479da22 100644 --- a/runtime/interpreter/value_pathcapability.go +++ b/runtime/interpreter/value_pathcapability.go @@ -143,7 +143,7 @@ func (v *PathCapabilityValue) Transfer( _ atree.Address, remove bool, storable atree.Storable, - _ map[atree.StorageID]struct{}, + _ map[atree.SlabID]struct{}, ) Value { if remove { v.DeepRemove(interpreter) diff --git a/runtime/interpreter/value_placeholder.go b/runtime/interpreter/value_placeholder.go index 6cc6184852..6fd4acf471 100644 --- a/runtime/interpreter/value_placeholder.go +++ b/runtime/interpreter/value_placeholder.go @@ -87,7 +87,7 @@ func (f placeholderValue) Transfer( _ atree.Address, remove bool, storable atree.Storable, - _ map[atree.StorageID]struct{}, + _ map[atree.SlabID]struct{}, ) Value { // TODO: actually not needed, value is not storable if remove { diff --git a/runtime/interpreter/value_storagecapabilitycontroller.go b/runtime/interpreter/value_storagecapabilitycontroller.go index d775baad1b..8084f323b7 100644 --- a/runtime/interpreter/value_storagecapabilitycontroller.go +++ b/runtime/interpreter/value_storagecapabilitycontroller.go @@ -203,7 +203,7 @@ func (v *StorageCapabilityControllerValue) Transfer( _ atree.Address, remove bool, storable atree.Storable, - _ map[atree.StorageID]struct{}, + _ map[atree.SlabID]struct{}, ) Value { if remove { interpreter.RemoveReferencedSlab(storable) diff --git a/runtime/storage.go b/runtime/storage.go index a679657406..30c4b9bcbe 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 } @@ -285,11 +282,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 } @@ -298,14 +295,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(), ) } @@ -328,7 +325,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 4861d3a8b9..d9dccd0f2d 100644 --- a/runtime/storage_test.go +++ b/runtime/storage_test.go @@ -63,13 +63,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/values_test.go b/runtime/tests/interpreter/values_test.go index 3949103d04..53b56ae4e1 100644 --- a/runtime/tests/interpreter/values_test.go +++ b/runtime/tests/interpreter/values_test.go @@ -170,7 +170,7 @@ func TestInterpretRandomMapOperations(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 @@ -494,7 +494,7 @@ func TestInterpretRandomMapOperations(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 @@ -619,7 +619,7 @@ func TestInterpretRandomArrayOperations(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 @@ -865,7 +865,7 @@ func TestInterpretRandomArrayOperations(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 @@ -964,7 +964,7 @@ func TestInterpretRandomCompositeValueOperations(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 @@ -1020,7 +1020,7 @@ func TestInterpretRandomCompositeValueOperations(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 @@ -1118,7 +1118,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 }