Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexport SlabID fields to prevent misuse #323

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions array.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,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 +588,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 +1722,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 @@ -2377,8 +2377,8 @@ func (a *Array) ID() ID {
sid := a.SlabID()

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

return id
}
Expand Down
12 changes: 6 additions & 6 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 @@ -2601,6 +2601,6 @@ func TestArrayID(t *testing.T) {
sid := array.SlabID()
id := array.ID()

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
20 changes: 10 additions & 10 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -2317,7 +2317,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 +2383,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 +3348,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 +3545,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 @@ -3866,8 +3866,8 @@ func (m *OrderedMap) ID() ID {
sid := m.SlabID()

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

return id
}
Expand All @@ -3885,7 +3885,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
28 changes: 14 additions & 14 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 @@ -4229,6 +4229,6 @@ func TestMapID(t *testing.T) {
sid := m.SlabID()
id := m.ID()

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:])
}
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