Skip to content

Commit

Permalink
fix docs and export MaxDataLen for callers
Browse files Browse the repository at this point in the history
  • Loading branch information
chappjc committed May 25, 2022
1 parent e89c0a6 commit 7fc7f0c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
19 changes: 10 additions & 9 deletions dex/encode/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package encode

import (
"bytes"
"crypto/rand"
"crypto/sha256"
"encoding/binary"
Expand All @@ -22,9 +21,10 @@ var (
// A byte-slice representation of boolean false.
ByteFalse = []byte{0}
// A byte-slice representation of boolean true.
ByteTrue = []byte{1}
bEqual = bytes.Equal
maxDataLen = 0x00fe_ffff // top two bytes in big endian stop at 254, signalling 32-bit len
ByteTrue = []byte{1}
// MaxDataLen is the largest byte slice that can be stored when using
// (BuildyBytes).AddData.
MaxDataLen = 0x00fe_ffff // top two bytes in big endian stop at 254, signalling 32-bit len
)

// Uint64Bytes converts the uint16 to a length-2, big-endian encoded byte slice.
Expand Down Expand Up @@ -167,9 +167,9 @@ func DecodeBlob(b []byte, preAlloc ...int) (byte, [][]byte, error) {

// BuildyBytes is a byte-slice with an AddData method for building linearly
// encoded 2D byte slices. The AddData method supports chaining. The canonical
// use case is to create "versioned blobs", where the BuildyBytes is instantated
// with a single version byte, and then data pushes are added using the AddData
// method. Example use:
// use case is to create "versioned blobs", where the BuildyBytes is
// instantiated with a single version byte, and then data pushes are added using
// the AddData method. Example use:
//
// version := 0
// b := BuildyBytes{version}.AddData(data1).AddData(data2)
Expand All @@ -180,12 +180,13 @@ func DecodeBlob(b []byte, preAlloc ...int) (byte, [][]byte, error) {
type BuildyBytes []byte

// AddData adds the data to the BuildyBytes, and returns the new BuildyBytes.
// The data has hard-coded length limit of uint16_max = 65535 bytes.
// The data has hard-coded length limit of MaxDataLen = 16711679 bytes. The
// caller should ensure the data is not larger since AddData panics if it is.
func (b BuildyBytes) AddData(d []byte) BuildyBytes {
l := len(d)
var lBytes []byte
if l >= 0xff {
if l > maxDataLen {
if l > MaxDataLen {
panic("cannot use addData for pushes > 16711679 bytes")
}
var i []byte
Expand Down
8 changes: 5 additions & 3 deletions dex/encode/encode_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package encode

import (
"bytes"
"testing"
)

var (
bEqual = bytes.Equal
tEmpty = []byte{}
tA = []byte{0xaa}
tB = []byte{0xbb, 0xbb}
Expand Down Expand Up @@ -53,8 +55,8 @@ func TestDecodeBlob(t *testing.T) {
longerBlob := RandomBytes(65536)
longerBlob2 := RandomBytes(65599)
megaBlob := RandomBytes(12_345_678)
almostLargestBlob := RandomBytes(maxDataLen - 1)
largestBlob := RandomBytes(maxDataLen)
almostLargestBlob := RandomBytes(MaxDataLen - 1)
largestBlob := RandomBytes(MaxDataLen)
// tooLargeBlob tested after the loop, recovering the expected panic

type test struct {
Expand Down Expand Up @@ -146,7 +148,7 @@ func TestDecodeBlob(t *testing.T) {
}
}

tooLargeBlob := RandomBytes(maxDataLen + 1)
tooLargeBlob := RandomBytes(MaxDataLen + 1)
func() {
defer func() {
if r := recover(); r == nil {
Expand Down

0 comments on commit 7fc7f0c

Please sign in to comment.