Skip to content

Commit

Permalink
Add Array.ID() and OrderedMap.ID()
Browse files Browse the repository at this point in the history
Currently Array.StorageID() and OrderedMap.StorageID() are used as
identifier in client because storage IDs are guaranteed to unique.
However, storage ID should be only used to retrieve slabs (registers)
from storage.

In the future, when Atree register inlining is implemented,
some resources may not be stored in separate slabs, so they
will not have storage IDs anymore.

This commit adds ID() to uniquly identify Array and OrderedMap.  For now,
this is implemented as identical to StorageID in raw bytes ([16]bytes).
In the future, this can be changed to be decoupled from storage ID
completely.  Clients should use ID instead of StorageID to identify
atree composite values.
  • Loading branch information
fxamacker committed Jun 29, 2023
1 parent a996413 commit 116c959
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 0 deletions.
10 changes: 10 additions & 0 deletions array.go
Original file line number Diff line number Diff line change
Expand Up @@ -2373,6 +2373,16 @@ func (a *Array) StorageID() StorageID {
return a.root.ID()
}

func (a *Array) ID() ID {
sid := a.StorageID()

var id ID
copy(id[:], sid.Address[:])
copy(id[8:], sid.Index[:])

return id
}

func (a *Array) Type() TypeInfo {
if extraData := a.root.ExtraData(); extraData != nil {
return extraData.TypeInfo
Expand Down
15 changes: 15 additions & 0 deletions array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2589,3 +2589,18 @@ func errorCategorizationCount(err error) int {
}
return count
}

func TestArrayID(t *testing.T) {
typeInfo := testTypeInfo{42}
storage := newTestPersistentStorage(t)
address := Address{1, 2, 3, 4, 5, 6, 7, 8}

array, err := NewArray(storage, address, typeInfo)
require.NoError(t, err)

sid := array.StorageID()
id := array.ID()

require.Equal(t, sid.Address[:], id[:8])
require.Equal(t, sid.Index[:], id[8:])
}
10 changes: 10 additions & 0 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -3862,6 +3862,16 @@ func (m *OrderedMap) StorageID() StorageID {
return m.root.Header().id
}

func (m *OrderedMap) ID() ID {
sid := m.StorageID()

var id ID
copy(id[:], sid.Address[:])
copy(id[8:], sid.Index[:])

return id
}

func (m *OrderedMap) StoredValue(_ SlabStorage) (Value, error) {
return m, nil
}
Expand Down
15 changes: 15 additions & 0 deletions map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4217,3 +4217,18 @@ func TestMaxInlineMapValueSize(t *testing.T) {
verifyMap(t, storage, typeInfo, address, m, keyValues, nil, false)
})
}

func TestMapID(t *testing.T) {
typeInfo := testTypeInfo{42}
storage := newTestPersistentStorage(t)
address := Address{1, 2, 3, 4, 5, 6, 7, 8}

m, err := NewMap(storage, address, newBasicDigesterBuilder(), typeInfo)
require.NoError(t, err)

sid := m.StorageID()
id := m.ID()

require.Equal(t, sid.Address[:], id[:8])
require.Equal(t, sid.Index[:], id[8:])
}
2 changes: 2 additions & 0 deletions storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import (

const LedgerBaseStorageSlabPrefix = "$"

type ID [16]byte

type (
Address [8]byte
StorageIndex [8]byte
Expand Down

0 comments on commit 116c959

Please sign in to comment.