Skip to content

Commit

Permalink
Merge pull request #323 from onflow/fxamacker/unexport-SlabID-fields
Browse files Browse the repository at this point in the history
Unexport `SlabID` fields to prevent misuse
  • Loading branch information
fxamacker authored Jul 6, 2023
2 parents adcd6fa + 5224515 commit 251b774
Show file tree
Hide file tree
Showing 17 changed files with 206 additions and 179 deletions.
19 changes: 9 additions & 10 deletions array.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ func (a *ArrayMetaDataSlab) StoredValue(storage SlabStorage) (Value, error) {

type ArraySlab interface {
Slab
fmt.Stringer

Get(storage SlabStorage, index uint64) (Storable, error)
Set(storage SlabStorage, address Address, index uint64, value Value) (Storable, error)
Expand Down Expand Up @@ -146,7 +145,7 @@ type Array struct {
var _ Value = &Array{}

func (a *Array) Address() Address {
return a.root.SlabID().Address
return a.root.SlabID().address
}

func (a *Array) Storable(_ SlabStorage, _ Address, _ uint64) (Storable, error) {
Expand Down Expand Up @@ -588,14 +587,14 @@ func (a *ArrayDataSlab) Split(storage SlabStorage) (Slab, Slab, error) {
}

// Construct right slab
sID, err := storage.GenerateSlabID(a.header.slabID.Address)
sID, err := storage.GenerateSlabID(a.header.slabID.address)
if err != nil {
// Wrap err as external error (if needed) because err is returned by SlabStorage interface.
return nil, nil, wrapErrorfAsExternalErrorIfNeeded(
err,
fmt.Sprintf(
"failed to generate slab ID for address 0x%x",
a.header.slabID.Address,
a.header.slabID.address,
),
)
}
Expand Down Expand Up @@ -1722,12 +1721,12 @@ func (a *ArrayMetaDataSlab) Split(storage SlabStorage) (Slab, Slab, error) {
}

// Construct right slab
sID, err := storage.GenerateSlabID(a.header.slabID.Address)
sID, err := storage.GenerateSlabID(a.header.slabID.address)
if err != nil {
// Wrap err as external error (if needed) because err is returned by SlabStorage interface.
return nil, nil, wrapErrorfAsExternalErrorIfNeeded(
err,
fmt.Sprintf("failed to generate slab ID for address 0x%x", a.header.slabID.Address))
fmt.Sprintf("failed to generate slab ID for address 0x%x", a.header.slabID.address))
}

rightSlab := &ArrayMetaDataSlab{
Expand Down Expand Up @@ -2373,12 +2372,12 @@ func (a *Array) SlabID() SlabID {
return a.root.SlabID()
}

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

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

return id
}
Expand Down
2 changes: 1 addition & 1 deletion array_debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func DumpArraySlabs(a *Array) ([]string, error) {
if !found {
return nil, NewSlabNotFoundErrorf(id, "slab not found during array slab dump")
}
dumps = append(dumps, fmt.Sprintf("overflow: %s", slab))
dumps = append(dumps, slab.String())
}

return dumps, nil
Expand Down
16 changes: 8 additions & 8 deletions array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1639,10 +1639,10 @@ func TestArrayEncodeDecode(t *testing.T) {
require.Equal(t, uint64(arraySize), array.Count())
require.Equal(t, uint64(1), nestedArray.Count())

id1 := SlabID{Address: address, Index: SlabIndex{0, 0, 0, 0, 0, 0, 0, 1}}
id2 := SlabID{Address: address, Index: SlabIndex{0, 0, 0, 0, 0, 0, 0, 2}}
id3 := SlabID{Address: address, Index: SlabIndex{0, 0, 0, 0, 0, 0, 0, 3}}
id4 := SlabID{Address: address, Index: SlabIndex{0, 0, 0, 0, 0, 0, 0, 4}}
id1 := SlabID{address: address, index: SlabIndex{0, 0, 0, 0, 0, 0, 0, 1}}
id2 := SlabID{address: address, index: SlabIndex{0, 0, 0, 0, 0, 0, 0, 2}}
id3 := SlabID{address: address, index: SlabIndex{0, 0, 0, 0, 0, 0, 0, 3}}
id4 := SlabID{address: address, index: SlabIndex{0, 0, 0, 0, 0, 0, 0, 4}}

// Expected serialized slab data with slab id
expected := map[SlabID][]byte{
Expand Down Expand Up @@ -2563,7 +2563,7 @@ func TestArraySlabDump(t *testing.T) {

want := []string{
"level 1, ArrayDataSlab id:0x102030405060708.1 size:24 count:1 elements: [SlabIDStorable({[1 2 3 4 5 6 7 8] [0 0 0 0 0 0 0 2]})]",
"overflow: &{0x102030405060708.2 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa}",
"StorableSlab id:0x102030405060708.2 storable:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
}

dumps, err := DumpArraySlabs(array)
Expand Down Expand Up @@ -2599,8 +2599,8 @@ func TestArrayID(t *testing.T) {
require.NoError(t, err)

sid := array.SlabID()
id := array.ID()
id := array.ValueID()

require.Equal(t, sid.Address[:], id[:8])
require.Equal(t, sid.Index[:], id[8:])
require.Equal(t, sid.address[:], id[:8])
require.Equal(t, sid.index[:], id[8:])
}
2 changes: 1 addition & 1 deletion basicarray.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ func (a *BasicArray) SlabID() SlabID {
}

func (a *BasicArray) Address() Address {
return a.SlabID().Address
return a.SlabID().address
}

func NewBasicArrayWithRootID(storage SlabStorage, id SlabID) (*BasicArray, error) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/stress/array.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ func testArray(
ids := make([]atree.SlabID, 0, len(rootIDs))
for id := range rootIDs {
// filter out root ids with empty address
if id.Address != atree.AddressUndefined {
if !id.HasTempAddress() {
ids = append(ids, id)
}
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/stress/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ func testMap(
ids := make([]atree.SlabID, 0, len(rootIDs))
for id := range rootIDs {
// filter out root ids with empty address
if id.Address != atree.AddressUndefined {
if !id.HasTempAddress() {
ids = append(ids, id)
}
}
Expand Down
25 changes: 3 additions & 22 deletions cmd/stress/storable.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,30 +348,11 @@ func (v StringValue) StoredValue(_ atree.SlabStorage) (atree.Value, error) {
}

func (v StringValue) Storable(storage atree.SlabStorage, address atree.Address, maxInlineSize uint64) (atree.Storable, error) {
if uint64(v.ByteSize()) > maxInlineSize {

// Create StorableSlab
id, err := storage.GenerateSlabID(address)
if err != nil {
return nil, err
}

slab := &atree.StorableSlab{
ID: id,
Storable: v,
}

// Store StorableSlab in storage
err = storage.Store(id, slab)
if err != nil {
return nil, err
}

// Return slab id as storable
return atree.SlabIDStorable(id), nil
if uint64(v.ByteSize()) <= maxInlineSize {
return v, nil
}

return v, nil
return atree.NewStorableSlab(storage, address, v)
}

func (v StringValue) Encode(enc *atree.Encoder) error {
Expand Down
6 changes: 3 additions & 3 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ func DecodeSlab(
// Wrap err as external error (if needed) because err is returned by StorableDecoder callback.
return nil, wrapErrorfAsExternalErrorIfNeeded(err, "failed to decode slab storable")
}
return StorableSlab{
ID: id,
Storable: storable,
return &StorableSlab{
slabID: id,
storable: storable,
}, nil

default:
Expand Down
25 changes: 12 additions & 13 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,6 @@ var _ MapSlab = &MapMetaDataSlab{}

type MapSlab interface {
Slab
fmt.Stringer

Get(storage SlabStorage, digester Digester, level uint, hkey Digest, comparator ValueComparator, key Value) (MapValue, error)
Set(storage SlabStorage, b DigesterBuilder, digester Digester, level uint, hkey Digest, comparator ValueComparator, hip HashInputProvider, key Value, value Value) (existingValue MapValue, err error)
Expand Down Expand Up @@ -2317,7 +2316,7 @@ func (m *MapDataSlab) StoredValue(storage SlabStorage) (Value, error) {

func (m *MapDataSlab) Set(storage SlabStorage, b DigesterBuilder, digester Digester, level uint, hkey Digest, comparator ValueComparator, hip HashInputProvider, key Value, value Value) (MapValue, error) {

existingValue, err := m.elements.Set(storage, m.SlabID().Address, b, digester, level, hkey, comparator, hip, key, value)
existingValue, err := m.elements.Set(storage, m.SlabID().address, b, digester, level, hkey, comparator, hip, key, value)
if err != nil {
// Don't need to wrap error as external error because err is already categorized by elements.Set().
return nil, err
Expand Down Expand Up @@ -2383,10 +2382,10 @@ func (m *MapDataSlab) Split(storage SlabStorage) (Slab, Slab, error) {
return nil, nil, err
}

sID, err := storage.GenerateSlabID(m.SlabID().Address)
sID, err := storage.GenerateSlabID(m.SlabID().address)
if err != nil {
// Wrap err as external error (if needed) because err is returned by SlabStorage interface.
return nil, nil, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to generate slab ID for address 0x%x", m.SlabID().Address))
return nil, nil, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to generate slab ID for address 0x%x", m.SlabID().address))
}

// Create new right slab
Expand Down Expand Up @@ -3348,10 +3347,10 @@ func (m *MapMetaDataSlab) Split(storage SlabStorage) (Slab, Slab, error) {
leftChildrenCount := int(math.Ceil(float64(len(m.childrenHeaders)) / 2))
leftSize := leftChildrenCount * mapSlabHeaderSize

sID, err := storage.GenerateSlabID(m.SlabID().Address)
sID, err := storage.GenerateSlabID(m.SlabID().address)
if err != nil {
// Wrap err as external error (if needed) because err is returned by SlabStorage interface.
return nil, nil, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to generate slab ID for address 0x%x", m.SlabID().Address))
return nil, nil, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("failed to generate slab ID for address 0x%x", m.SlabID().address))
}

// Construct right slab
Expand Down Expand Up @@ -3545,8 +3544,8 @@ func NewMap(storage SlabStorage, address Address, digestBuilder DigesterBuilder,
// This is for creating the seed, so the seed used here is OK to be 0.
// LittleEndian is needed for compatibility (same digest from []byte and
// two uint64).
a := binary.LittleEndian.Uint64(sID.Address[:])
b := binary.LittleEndian.Uint64(sID.Index[:])
a := binary.LittleEndian.Uint64(sID.address[:])
b := binary.LittleEndian.Uint64(sID.index[:])
k0 := circlehash.Hash64Uint64x2(a, b, uint64(0))

// To save storage space, only store 64-bits of the seed.
Expand Down Expand Up @@ -3862,12 +3861,12 @@ func (m *OrderedMap) SlabID() SlabID {
return m.root.SlabID()
}

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

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

return id
}
Expand All @@ -3885,7 +3884,7 @@ func (m *OrderedMap) Count() uint64 {
}

func (m *OrderedMap) Address() Address {
return m.root.SlabID().Address
return m.root.SlabID().address
}

func (m *OrderedMap) Type() TypeInfo {
Expand Down
2 changes: 1 addition & 1 deletion map_debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ func DumpMapSlabs(m *OrderedMap) ([]string, error) {
if !found {
return nil, NewSlabNotFoundErrorf(id, "slab not found during map slab dump")
}
dumps = append(dumps, fmt.Sprintf("overflow: %s", slab))
dumps = append(dumps, slab.String())
}

return dumps, nil
Expand Down
34 changes: 17 additions & 17 deletions map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1537,7 +1537,7 @@ func TestMapEncodeDecode(t *testing.T) {
require.NoError(t, err)
require.Equal(t, uint64(0), m.Count())

id1 := SlabID{Address: address, Index: SlabIndex{0, 0, 0, 0, 0, 0, 0, 1}}
id1 := SlabID{address: address, index: SlabIndex{0, 0, 0, 0, 0, 0, 0, 1}}

expected := map[SlabID][]byte{
id1: {
Expand Down Expand Up @@ -1623,7 +1623,7 @@ func TestMapEncodeDecode(t *testing.T) {

require.Equal(t, uint64(mapSize), m.Count())

id1 := SlabID{Address: address, Index: SlabIndex{0, 0, 0, 0, 0, 0, 0, 1}}
id1 := SlabID{address: address, index: SlabIndex{0, 0, 0, 0, 0, 0, 0, 1}}

// Expected serialized slab data with slab id
expected := map[SlabID][]byte{
Expand Down Expand Up @@ -1742,10 +1742,10 @@ func TestMapEncodeDecode(t *testing.T) {

require.Equal(t, uint64(mapSize), m.Count())

id1 := SlabID{Address: address, Index: SlabIndex{0, 0, 0, 0, 0, 0, 0, 1}}
id2 := SlabID{Address: address, Index: SlabIndex{0, 0, 0, 0, 0, 0, 0, 2}}
id3 := SlabID{Address: address, Index: SlabIndex{0, 0, 0, 0, 0, 0, 0, 3}}
id4 := SlabID{Address: address, Index: SlabIndex{0, 0, 0, 0, 0, 0, 0, 4}}
id1 := SlabID{address: address, index: SlabIndex{0, 0, 0, 0, 0, 0, 0, 1}}
id2 := SlabID{address: address, index: SlabIndex{0, 0, 0, 0, 0, 0, 0, 2}}
id3 := SlabID{address: address, index: SlabIndex{0, 0, 0, 0, 0, 0, 0, 3}}
id4 := SlabID{address: address, index: SlabIndex{0, 0, 0, 0, 0, 0, 0, 4}}

// Expected serialized slab data with slab id
expected := map[SlabID][]byte{
Expand Down Expand Up @@ -1961,7 +1961,7 @@ func TestMapEncodeDecode(t *testing.T) {

require.Equal(t, uint64(mapSize), m.Count())

id1 := SlabID{Address: address, Index: SlabIndex{0, 0, 0, 0, 0, 0, 0, 1}}
id1 := SlabID{address: address, index: SlabIndex{0, 0, 0, 0, 0, 0, 0, 1}}

// Expected serialized slab data with slab id
expected := map[SlabID][]byte{
Expand Down Expand Up @@ -2154,7 +2154,7 @@ func TestMapEncodeDecode(t *testing.T) {

require.Equal(t, uint64(mapSize), m.Count())

id1 := SlabID{Address: address, Index: SlabIndex{0, 0, 0, 0, 0, 0, 0, 1}}
id1 := SlabID{address: address, index: SlabIndex{0, 0, 0, 0, 0, 0, 0, 1}}

// Expected serialized slab data with slab id
expected := map[SlabID][]byte{
Expand Down Expand Up @@ -2397,9 +2397,9 @@ func TestMapEncodeDecode(t *testing.T) {

require.Equal(t, uint64(mapSize), m.Count())

id1 := SlabID{Address: address, Index: SlabIndex{0, 0, 0, 0, 0, 0, 0, 1}}
id2 := SlabID{Address: address, Index: SlabIndex{0, 0, 0, 0, 0, 0, 0, 2}}
id3 := SlabID{Address: address, Index: SlabIndex{0, 0, 0, 0, 0, 0, 0, 3}}
id1 := SlabID{address: address, index: SlabIndex{0, 0, 0, 0, 0, 0, 0, 1}}
id2 := SlabID{address: address, index: SlabIndex{0, 0, 0, 0, 0, 0, 0, 2}}
id3 := SlabID{address: address, index: SlabIndex{0, 0, 0, 0, 0, 0, 0, 3}}

// Expected serialized slab data with slab id
expected := map[SlabID][]byte{
Expand Down Expand Up @@ -2627,7 +2627,7 @@ func TestMapEncodeDecode(t *testing.T) {

require.Equal(t, uint64(1), m.Count())

id1 := SlabID{Address: address, Index: SlabIndex{0, 0, 0, 0, 0, 0, 0, 1}}
id1 := SlabID{address: address, index: SlabIndex{0, 0, 0, 0, 0, 0, 0, 1}}

expectedNoPointer := []byte{

Expand Down Expand Up @@ -3909,7 +3909,7 @@ func TestMapSlabDump(t *testing.T) {

want := []string{
"level 1, MapDataSlab id:0x102030405060708.1 size:102 firstkey:0 elements: [0:SlabIDStorable({[1 2 3 4 5 6 7 8] [0 0 0 0 0 0 0 2]}):bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb]",
"overflow: &{0x102030405060708.2 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa}",
"StorableSlab id:0x102030405060708.2 storable:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
}
dumps, err := DumpMapSlabs(m)
require.NoError(t, err)
Expand All @@ -3936,7 +3936,7 @@ func TestMapSlabDump(t *testing.T) {

want := []string{
"level 1, MapDataSlab id:0x102030405060708.1 size:100 firstkey:0 elements: [0:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:SlabIDStorable({[1 2 3 4 5 6 7 8] [0 0 0 0 0 0 0 2]})]",
"overflow: &{0x102030405060708.2 bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb}",
"StorableSlab id:0x102030405060708.2 storable:bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
}
dumps, err := DumpMapSlabs(m)
require.NoError(t, err)
Expand Down Expand Up @@ -4227,8 +4227,8 @@ func TestMapID(t *testing.T) {
require.NoError(t, err)

sid := m.SlabID()
id := m.ID()
id := m.ValueID()

require.Equal(t, sid.Address[:], id[:8])
require.Equal(t, sid.Index[:], id[8:])
require.Equal(t, sid.address[:], id[:8])
require.Equal(t, sid.index[:], id[8:])
}
3 changes: 3 additions & 0 deletions slab.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@

package atree

import "fmt"

type Slab interface {
Storable
fmt.Stringer

SlabID() SlabID
Split(SlabStorage) (Slab, Slab, error)
Expand Down
4 changes: 2 additions & 2 deletions storable.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ func (v SlabIDStorable) Encode(enc *Encoder) error {
return NewEncodingError(err)
}

copy(enc.Scratch[:], v.Address[:])
copy(enc.Scratch[8:], v.Index[:])
copy(enc.Scratch[:], v.address[:])
copy(enc.Scratch[8:], v.index[:])

err = enc.CBOR.EncodeBytes(enc.Scratch[:slabIDSize])
if err != nil {
Expand Down
Loading

0 comments on commit 251b774

Please sign in to comment.