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

feat(orm): allow bytes keys longer than 255 bytes #11522

Merged
merged 6 commits into from
Apr 1, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
27 changes: 13 additions & 14 deletions orm/encoding/ormfield/bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ package ormfield

import (
"bytes"
"encoding/binary"
"io"

"google.golang.org/protobuf/reflect/protoreflect"

"github.com/cosmos/cosmos-sdk/orm/types/ormerrors"
)

// BytesCodec encodes bytes as raw bytes. It errors if the byte array is longer
Expand All @@ -22,12 +21,7 @@ func (b BytesCodec) ComputeBufferSize(value protoreflect.Value) (int, error) {
}

func bytesSize(value protoreflect.Value) (int, error) {
aaronc marked this conversation as resolved.
Show resolved Hide resolved
bz := value.Bytes()
n := len(bz)
if n > 255 {
return -1, ormerrors.BytesFieldTooLong
}
return n, nil
return len(value.Bytes()), nil
}

func (b BytesCodec) IsOrdered() bool {
Expand Down Expand Up @@ -58,7 +52,13 @@ func (b NonTerminalBytesCodec) FixedBufferSize() int {

func (b NonTerminalBytesCodec) ComputeBufferSize(value protoreflect.Value) (int, error) {
aaronc marked this conversation as resolved.
Show resolved Hide resolved
n, err := bytesSize(value)
return n + 1, err
prefixLen := 1
// prefix is a varint, whose msb signals continuation
for n >= 0x80 {
aaronc marked this conversation as resolved.
Show resolved Hide resolved
aaronc marked this conversation as resolved.
Show resolved Hide resolved
prefixLen++
n >>= 7
}
return n + prefixLen, err
}

func (b NonTerminalBytesCodec) IsOrdered() bool {
Expand All @@ -70,7 +70,7 @@ func (b NonTerminalBytesCodec) Compare(v1, v2 protoreflect.Value) int {
}

func (b NonTerminalBytesCodec) Decode(r Reader) (protoreflect.Value, error) {
n, err := r.ReadByte()
n, err := binary.ReadUvarint(r)
if err != nil {
return protoreflect.Value{}, err
}
Expand All @@ -87,10 +87,9 @@ func (b NonTerminalBytesCodec) Decode(r Reader) (protoreflect.Value, error) {
func (b NonTerminalBytesCodec) Encode(value protoreflect.Value, w io.Writer) error {
bz := value.Bytes()
n := len(bz)
if n > 255 {
return ormerrors.BytesFieldTooLong
}
_, err := w.Write([]byte{byte(n)})
var prefix [binary.MaxVarintLen64]byte
aaronc marked this conversation as resolved.
Show resolved Hide resolved
prefixLen := binary.PutUvarint(prefix[:], uint64(n))
_, err := w.Write(prefix[:prefixLen])
if err != nil {
return err
}
Expand Down
10 changes: 0 additions & 10 deletions orm/encoding/ormfield/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,6 @@ func TestUnsupportedFields(t *testing.T) {
assert.ErrorContains(t, err, ormerrors.UnsupportedKeyField.Error())
}

func TestNTBytesTooLong(t *testing.T) {
cdc, err := ormfield.GetCodec(testutil.GetTestField("bz"), true)
assert.NilError(t, err)
buf := &bytes.Buffer{}
bz := protoreflect.ValueOfBytes(make([]byte, 256))
assert.ErrorContains(t, cdc.Encode(bz, buf), ormerrors.BytesFieldTooLong.Error())
_, err = cdc.ComputeBufferSize(bz)
assert.ErrorContains(t, err, ormerrors.BytesFieldTooLong.Error())
}

func TestCompactUInt32(t *testing.T) {
var lastBz []byte
testEncodeDecode := func(x uint32, expectedLen int) {
Expand Down
6 changes: 3 additions & 3 deletions orm/internal/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package testutil

import (
"fmt"
"math"
"strings"

"google.golang.org/protobuf/types/known/durationpb"

"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/timestamppb"
"pgregory.net/rapid"

Expand Down Expand Up @@ -39,7 +39,7 @@ var TestFieldSpecs = []TestFieldSpec{
},
{
"bz",
rapid.SliceOfN(rapid.Byte(), 0, 255),
rapid.SliceOfN(rapid.Byte(), 0, math.MaxUint32),
},
{
"i32",
Expand Down
1 change: 0 additions & 1 deletion orm/types/ormerrors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ var (
AutoIncrementKeyAlreadySet = errors.New(codespace, 12, "can't create with auto-increment primary key already set")
CantFindIndex = errors.New(codespace, 13, "can't find index")
UnexpectedDecodePrefix = errors.New(codespace, 14, "unexpected prefix while trying to decode an entry")
BytesFieldTooLong = errors.New(codespace, 15, "bytes field is longer than 255 bytes")
UnsupportedOperation = errors.New(codespace, 16, "unsupported operation")
BadDecodeEntry = errors.New(codespace, 17, "bad decode entry")
IndexOutOfBounds = errors.New(codespace, 18, "index out of bounds")
Expand Down