Skip to content

Commit

Permalink
Merge pull request #321 from onflow/fxamacker/add-id-to-array-and-ord…
Browse files Browse the repository at this point in the history
…eredmap

Add `Array.ID()` and `OrderedMap.ID()`
  • Loading branch information
fxamacker authored Jul 6, 2023
2 parents 5b7b45a + 116c959 commit 2554c7a
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 2554c7a

Please sign in to comment.