Skip to content

Commit

Permalink
refactor: rename to ShareReservedBytes (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
rootulp authored May 28, 2024
1 parent ae56f63 commit 4063a54
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 20 deletions.
3 changes: 2 additions & 1 deletion go.work.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/tools v0.16.0/go.mod h1:kYVVN6I1mBNoB1OX+noeBjbRk4IUEPa7JJ+TJMEooJ0=
golang.org/x/tools v0.16.0/go.mod h1:kYVVN6I1mBNoB1OX+noeBjbRk4IUEPa7JJ+TJMEooJ0=
11 changes: 8 additions & 3 deletions shares/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,20 @@ const (

// CompactShareReservedBytes is the number of bytes reserved for the location of
// the first unit (transaction, ISR) in a compact share.
CompactShareReservedBytes = 4
// Deprecated: use ShareReservedBytes instead.
CompactShareReservedBytes = ShareReservedBytes

// ShareReservedBytes is the number of bytes reserved for the location of
// the first unit (transaction, ISR) in a compact share.
ShareReservedBytes = 4

// FirstCompactShareContentSize is the number of bytes usable for data in
// the first compact share of a sequence.
FirstCompactShareContentSize = ShareSize - namespace.NamespaceSize - ShareInfoBytes - SequenceLenBytes - CompactShareReservedBytes
FirstCompactShareContentSize = ShareSize - namespace.NamespaceSize - ShareInfoBytes - SequenceLenBytes - ShareReservedBytes

// ContinuationCompactShareContentSize is the number of bytes usable for
// data in a continuation compact share of a sequence.
ContinuationCompactShareContentSize = ShareSize - namespace.NamespaceSize - ShareInfoBytes - CompactShareReservedBytes
ContinuationCompactShareContentSize = ShareSize - namespace.NamespaceSize - ShareInfoBytes - ShareReservedBytes

// FirstSparseShareContentSize is the number of bytes usable for data in the
// first sparse share of a sequence.
Expand Down
10 changes: 5 additions & 5 deletions shares/reserved_bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ import (
)

// NewReservedBytes returns a byte slice of length
// CompactShareReservedBytes that contains the byteIndex of the first
// ShareReservedBytes that contains the byteIndex of the first
// unit that starts in a compact share.
func NewReservedBytes(byteIndex uint32) ([]byte, error) {
if byteIndex >= ShareSize {
return []byte{}, fmt.Errorf("byte index %d must be less than share size %d", byteIndex, ShareSize)
}
reservedBytes := make([]byte, CompactShareReservedBytes)
reservedBytes := make([]byte, ShareReservedBytes)
binary.BigEndian.PutUint32(reservedBytes, byteIndex)
return reservedBytes, nil
}

// ParseReservedBytes parses a byte slice of length
// CompactShareReservedBytes into a byteIndex.
// ShareReservedBytes into a byteIndex.
func ParseReservedBytes(reservedBytes []byte) (uint32, error) {
if len(reservedBytes) != CompactShareReservedBytes {
return 0, fmt.Errorf("reserved bytes must be of length %d", CompactShareReservedBytes)
if len(reservedBytes) != ShareReservedBytes {
return 0, fmt.Errorf("reserved bytes must be of length %d", ShareReservedBytes)
}
byteIndex := binary.BigEndian.Uint32(reservedBytes)
if ShareSize <= byteIndex {
Expand Down
8 changes: 4 additions & 4 deletions shares/share_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (b *Builder) Build() (*Share, error) {
func (b *Builder) IsEmptyShare() bool {
expectedLen := namespace.NamespaceSize + ShareInfoBytes
if b.isCompactShare {
expectedLen += CompactShareReservedBytes
expectedLen += ShareReservedBytes
}
if b.isFirstShare {
expectedLen += SequenceLenBytes
Expand All @@ -97,7 +97,7 @@ func (b *Builder) ZeroPadIfNecessary() (bytesOfPadding int) {
// isEmptyReservedBytes returns true if the reserved bytes are empty.
func (b *Builder) isEmptyReservedBytes() (bool, error) {
indexOfReservedBytes := b.indexOfReservedBytes()
reservedBytes, err := ParseReservedBytes(b.rawShareData[indexOfReservedBytes : indexOfReservedBytes+CompactShareReservedBytes])
reservedBytes, err := ParseReservedBytes(b.rawShareData[indexOfReservedBytes : indexOfReservedBytes+ShareReservedBytes])
if err != nil {
return false, err
}
Expand Down Expand Up @@ -144,7 +144,7 @@ func (b *Builder) MaybeWriteReservedBytes() error {

indexOfReservedBytes := b.indexOfReservedBytes()
// overwrite the reserved bytes of the pending share
for i := 0; i < CompactShareReservedBytes; i++ {
for i := 0; i < ShareReservedBytes; i++ {
b.rawShareData[indexOfReservedBytes+i] = reservedBytes[i]
}
return nil
Expand Down Expand Up @@ -184,7 +184,7 @@ func (b *Builder) prepareCompactShare() error {
return err
}
placeholderSequenceLen := make([]byte, SequenceLenBytes)
placeholderReservedBytes := make([]byte, CompactShareReservedBytes)
placeholderReservedBytes := make([]byte, ShareReservedBytes)

shareData = append(shareData, b.namespace.Bytes()...)
shareData = append(shareData, byte(infoByte))
Expand Down
8 changes: 4 additions & 4 deletions shares/share_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func TestShareBuilderAddData(t *testing.T) {
{
name: "exact fit first compact share",
builder: mustNewBuilder(t, namespace.TxNamespace, ShareVersionZero, true),
data: bytes.Repeat([]byte{1}, ShareSize-namespace.NamespaceSize-ShareInfoBytes-CompactShareReservedBytes-SequenceLenBytes),
data: bytes.Repeat([]byte{1}, ShareSize-namespace.NamespaceSize-ShareInfoBytes-ShareReservedBytes-SequenceLenBytes),
want: nil,
},
{
Expand All @@ -176,7 +176,7 @@ func TestShareBuilderAddData(t *testing.T) {
{
name: "exact fit continues compact share",
builder: mustNewBuilder(t, namespace.TxNamespace, ShareVersionZero, false),
data: bytes.Repeat([]byte{1}, ShareSize-namespace.NamespaceSize-CompactShareReservedBytes-1 /*1 = info byte*/),
data: bytes.Repeat([]byte{1}, ShareSize-namespace.NamespaceSize-ShareReservedBytes-1 /*1 = info byte*/),
want: nil,
},
{
Expand All @@ -188,7 +188,7 @@ func TestShareBuilderAddData(t *testing.T) {
{
name: "oversize first compact share",
builder: mustNewBuilder(t, namespace.TxNamespace, ShareVersionZero, true),
data: bytes.Repeat([]byte{1}, 1 /*1 extra byte*/ +ShareSize-namespace.NamespaceSize-CompactShareReservedBytes-SequenceLenBytes-1 /*1 = info byte*/),
data: bytes.Repeat([]byte{1}, 1 /*1 extra byte*/ +ShareSize-namespace.NamespaceSize-ShareReservedBytes-SequenceLenBytes-1 /*1 = info byte*/),
want: []byte{1},
},
{
Expand All @@ -200,7 +200,7 @@ func TestShareBuilderAddData(t *testing.T) {
{
name: "oversize continues compact share",
builder: mustNewBuilder(t, namespace.TxNamespace, ShareVersionZero, false),
data: bytes.Repeat([]byte{1}, 1 /*1 extra byte*/ +ShareSize-namespace.NamespaceSize-CompactShareReservedBytes-1 /*1 = info byte*/),
data: bytes.Repeat([]byte{1}, 1 /*1 extra byte*/ +ShareSize-namespace.NamespaceSize-ShareReservedBytes-1 /*1 = info byte*/),
want: []byte{1},
},
{
Expand Down
4 changes: 2 additions & 2 deletions shares/shares.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func (s *Share) rawDataStartIndex() int {
index += SequenceLenBytes
}
if isCompact {
index += CompactShareReservedBytes
index += ShareReservedBytes
}
return index
}
Expand Down Expand Up @@ -227,7 +227,7 @@ func (s *Share) rawDataStartIndexUsingReserved() (int, error) {
}

if isCompact {
reservedBytes, err := ParseReservedBytes(s.data[index : index+CompactShareReservedBytes])
reservedBytes, err := ParseReservedBytes(s.data[index : index+ShareReservedBytes])
if err != nil {
return 0, err
}
Expand Down
2 changes: 1 addition & 1 deletion shares/split_compact_shares_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func TestExport_write(t *testing.T) {
[]byte{
0x0, // info byte
0x0, 0x0, 0x0, 0x0, // reserved bytes
}, bytes.Repeat([]byte{0xf}, namespace.NamespaceSize+ShareInfoBytes+SequenceLenBytes+CompactShareReservedBytes)..., // data
}, bytes.Repeat([]byte{0xf}, namespace.NamespaceSize+ShareInfoBytes+SequenceLenBytes+ShareReservedBytes)..., // data
)...,
),
ShareSize)
Expand Down

0 comments on commit 4063a54

Please sign in to comment.