Skip to content

Commit

Permalink
[codec] Add ShortBytes type (#563)
Browse files Browse the repository at this point in the history
* working on test

* add shortBytes implementation

* use const in const

* wrap with int

* add short bytes helpers

* add license header
  • Loading branch information
patrick-ogrady authored Oct 16, 2023
1 parent defff97 commit 7127fdb
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 4 deletions.
1 change: 1 addition & 0 deletions codec/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ var (
ErrDuplicateItem = errors.New("duplicate item")
ErrFieldNotPopulated = errors.New("field is not populated")
ErrInvalidBitset = errors.New("invalid bitset")
ErrTooLarge = errors.New("too large")
)
23 changes: 19 additions & 4 deletions codec/packer.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,29 @@ func (p *Packer) PackFixedBytes(b []byte) {
p.p.PackFixedBytes(b)
}

func (p *Packer) PackShortBytes(b ShortBytes) {
l := len(b)
if l > ShortBytesMaxSize {
p.addErr(fmt.Errorf("%w: ShortBytes is too large (found=%d)", ErrTooLarge, len(b)))
return
}
p.PackByte(uint8(l))
p.PackFixedBytes(b)
}

func (p *Packer) PackBytes(b []byte) {
p.p.PackBytes(b)
}

func (p *Packer) UnpackFixedBytes(size int, dest *[]byte) {
copy((*dest), p.p.UnpackFixedBytes(size))
}

func (p *Packer) UnpackShortBytes(dest *ShortBytes) {
l := int(p.p.UnpackByte())
*dest = p.p.UnpackFixedBytes(l)
}

// UnpackBytes unpacks [limit] bytes into [dest]. Otherwise
// if [limit] >= 0, UnpackBytes unpacks a byte slice array into [dest]. If
// [required] is set to true and the amount of bytes written to [dest] is 0,
Expand All @@ -91,10 +110,6 @@ func (p *Packer) UnpackBytes(limit int, required bool, dest *[]byte) {
}
}

func (p *Packer) UnpackFixedBytes(size int, dest *[]byte) {
copy((*dest), p.p.UnpackFixedBytes(size))
}

func (p *Packer) PackUint64(v uint64) {
p.p.PackLong(v)
}
Expand Down
31 changes: 31 additions & 0 deletions codec/packer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func TestPackerPublicKey(t *testing.T) {
copy(pubKey[:], TestPublicKey)
t.Run("Pack", func(t *testing.T) {
// Pack
require.Len(pubKey, ed25519.PublicKeyLen)
wp.PackPublicKey(pubKey)
require.Equal(TestPublicKey, wp.Bytes(), "PublicKey not packed correctly.")
require.NoError(wp.Err(), "Error packing PublicKey.")
Expand Down Expand Up @@ -153,6 +154,36 @@ func TestPackerWindow(t *testing.T) {
})
}

func TestPackerShortBytes(t *testing.T) {
require := require.New(t)
t.Run("Pack Too Large", func(t *testing.T) {
wp := NewWriter(1024, 1024)
wp.PackShortBytes(make([]byte, 1024))
require.ErrorIs(wp.Err(), ErrTooLarge)
})
wp := NewWriter(ed25519.PublicKeyLen+1, ed25519.PublicKeyLen+1)
pubKey := make(ShortBytes, ed25519.PublicKeyLen)
copy(pubKey[:], TestPublicKey)
t.Run("Pack", func(t *testing.T) {
// Pack
wp.PackShortBytes(pubKey)
b := wp.Bytes()
require.NoError(wp.Err(), "Error packing PublicKey.")
require.Len(b, ed25519.PublicKeyLen+1)
require.Equal(uint8(ed25519.PublicKeyLen), b[0], "PublicKeyLen not packed correctly.")
require.Equal(TestPublicKey, b[1:], "PublicKey not packed correctly.")
})
t.Run("Unpack", func(t *testing.T) {
// Unpack
rp := NewReader(wp.Bytes(), ed25519.PublicKeyLen+1)
require.Equal(wp.Bytes(), rp.Bytes(), "Reader not initialized correctly.")
var unpackedPubKey ShortBytes
rp.UnpackShortBytes(&unpackedPubKey)
require.Equal(pubKey, unpackedPubKey, "UnpackPublicKey unpacked incorrectly.")
require.NoError(rp.Err(), "UnpackPublicKey set an error.")
})
}

func TestNewReader(t *testing.T) {
require := require.New(t)
vInt := 900
Expand Down
10 changes: 10 additions & 0 deletions codec/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright (C) 2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package codec

import "github.com/ava-labs/hypersdk/consts"

const ShortBytesMaxSize = int(consts.MaxUint8)

type ShortBytes []byte
8 changes: 8 additions & 0 deletions codec/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ func CummSize[T SizeType](arr []T) int {
return size
}

func ShortBytesLen(msg []byte) int {
return consts.ByteLen + len(msg)
}

func ShortBytesLenSize(msgSize int) int {
return consts.ByteLen + msgSize
}

func BytesLen(msg []byte) int {
return consts.IntLen + len(msg)
}
Expand Down

0 comments on commit 7127fdb

Please sign in to comment.