Skip to content

Commit

Permalink
Remove codec length check after Durango
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenButtolph committed Jan 4, 2024
1 parent 72dc442 commit 34917f3
Show file tree
Hide file tree
Showing 61 changed files with 451 additions and 251 deletions.
7 changes: 4 additions & 3 deletions api/keystore/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package keystore

import (
"time"

"github.com/ava-labs/avalanchego/codec"
"github.com/ava-labs/avalanchego/codec/linearcodec"
"github.com/ava-labs/avalanchego/utils/units"
Expand All @@ -12,14 +14,13 @@ import (
const (
CodecVersion = 0

maxPackerSize = 1 * units.GiB // max size, in bytes, of something being marshalled by Marshal()
maxSliceLength = linearcodec.DefaultMaxSliceLength
maxPackerSize = 1 * units.GiB // max size, in bytes, of something being marshalled by Marshal()
)

var Codec codec.Manager

func init() {
lc := linearcodec.NewCustomMaxLength(maxSliceLength)
lc := linearcodec.NewDefault(time.Time{})
Codec = codec.NewManager(maxPackerSize)
if err := Codec.RegisterCodec(CodecVersion, lc); err != nil {
panic(err)
Expand Down
7 changes: 5 additions & 2 deletions chains/atomic/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
package atomic

import (
"math"
"time"

"github.com/ava-labs/avalanchego/codec"
"github.com/ava-labs/avalanchego/codec/linearcodec"
)
Expand All @@ -14,8 +17,8 @@ const CodecVersion = 0
var Codec codec.Manager

func init() {
lc := linearcodec.NewDefault()
Codec = codec.NewDefaultManager()
lc := linearcodec.NewDefault(time.Time{})
Codec = codec.NewManager(math.MaxInt)
if err := Codec.RegisterCodec(CodecVersion, lc); err != nil {
panic(err)
}
Expand Down
9 changes: 5 additions & 4 deletions codec/hierarchycodec/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"reflect"
"sync"
"time"

"github.com/ava-labs/avalanchego/codec"
"github.com/ava-labs/avalanchego/codec/reflectcodec"
Expand Down Expand Up @@ -50,19 +51,19 @@ type hierarchyCodec struct {
}

// New returns a new, concurrency-safe codec
func New(tagNames []string, maxSliceLen uint32) Codec {
func New(durangoTime time.Time, tagNames []string, maxSliceLen uint32) Codec {
hCodec := &hierarchyCodec{
currentGroupID: 0,
nextTypeID: 0,
registeredTypes: bimap.New[typeID, reflect.Type](),
}
hCodec.Codec = reflectcodec.New(hCodec, tagNames, maxSliceLen)
hCodec.Codec = reflectcodec.New(hCodec, tagNames, durangoTime, maxSliceLen)
return hCodec
}

// NewDefault returns a new codec with reasonable default values
func NewDefault() Codec {
return New([]string{reflectcodec.DefaultTagName}, defaultMaxSliceLength)
func NewDefault(durangoTime time.Time) Codec {
return New(durangoTime, []string{reflectcodec.DefaultTagName}, defaultMaxSliceLength)
}

// SkipRegistrations some number of type IDs
Expand Down
7 changes: 4 additions & 3 deletions codec/hierarchycodec/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,24 @@ import (
"testing"

"github.com/ava-labs/avalanchego/codec"
"github.com/ava-labs/avalanchego/utils/timer/mockable"
)

func TestVectors(t *testing.T) {
for _, test := range codec.Tests {
c := NewDefault()
c := NewDefault(mockable.MaxTime)
test(c, t)
}
}

func TestMultipleTags(t *testing.T) {
for _, test := range codec.MultipleTagsTests {
c := New([]string{"tag1", "tag2"}, defaultMaxSliceLength)
c := New(mockable.MaxTime, []string{"tag1", "tag2"}, defaultMaxSliceLength)
test(c, t)
}
}

func FuzzStructUnmarshalHierarchyCodec(f *testing.F) {
c := NewDefault()
c := NewDefault(mockable.MaxTime)
codec.FuzzStructUnmarshal(c, f)
}
13 changes: 7 additions & 6 deletions codec/linearcodec/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"reflect"
"sync"
"time"

"github.com/ava-labs/avalanchego/codec"
"github.com/ava-labs/avalanchego/codec/reflectcodec"
Expand Down Expand Up @@ -44,23 +45,23 @@ type linearCodec struct {

// New returns a new, concurrency-safe codec; it allow to specify
// both tagNames and maxSlicelenght
func New(tagNames []string, maxSliceLen uint32) Codec {
func New(durangoTime time.Time, tagNames []string, maxSliceLen uint32) Codec {
hCodec := &linearCodec{
nextTypeID: 0,
registeredTypes: bimap.New[uint32, reflect.Type](),
}
hCodec.Codec = reflectcodec.New(hCodec, tagNames, maxSliceLen)
hCodec.Codec = reflectcodec.New(hCodec, tagNames, durangoTime, maxSliceLen)
return hCodec
}

// NewDefault is a convenience constructor; it returns a new codec with reasonable default values
func NewDefault() Codec {
return New([]string{reflectcodec.DefaultTagName}, DefaultMaxSliceLength)
func NewDefault(durangoTime time.Time) Codec {
return New(durangoTime, []string{reflectcodec.DefaultTagName}, DefaultMaxSliceLength)
}

// NewCustomMaxLength is a convenience constructor; it returns a new codec with custom max length and default tags
func NewCustomMaxLength(maxSliceLen uint32) Codec {
return New([]string{reflectcodec.DefaultTagName}, maxSliceLen)
func NewCustomMaxLength(durangoTime time.Time, maxSliceLen uint32) Codec {
return New(durangoTime, []string{reflectcodec.DefaultTagName}, maxSliceLen)
}

// Skip some number of type IDs
Expand Down
7 changes: 4 additions & 3 deletions codec/linearcodec/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,24 @@ import (
"testing"

"github.com/ava-labs/avalanchego/codec"
"github.com/ava-labs/avalanchego/utils/timer/mockable"
)

func TestVectors(t *testing.T) {
for _, test := range codec.Tests {
c := NewDefault()
c := NewDefault(mockable.MaxTime)
test(c, t)
}
}

func TestMultipleTags(t *testing.T) {
for _, test := range codec.MultipleTagsTests {
c := New([]string{"tag1", "tag2"}, DefaultMaxSliceLength)
c := New(mockable.MaxTime, []string{"tag1", "tag2"}, DefaultMaxSliceLength)
test(c, t)
}
}

func FuzzStructUnmarshalLinearCodec(f *testing.F) {
c := NewDefault()
c := NewDefault(mockable.MaxTime)
codec.FuzzStructUnmarshal(c, f)
}
49 changes: 33 additions & 16 deletions codec/reflectcodec/type_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"math"
"reflect"
"time"

"golang.org/x/exp/slices"

Expand Down Expand Up @@ -72,14 +73,16 @@ type TypeCodec interface {
// 7. nil slices are marshaled as empty slices
type genericCodec struct {
typer TypeCodec
durangoTime time.Time // Time after which [maxSliceLen] will be ignored
maxSliceLen uint32
fielder StructFielder
}

// New returns a new, concurrency-safe codec
func New(typer TypeCodec, tagNames []string, maxSliceLen uint32) codec.Codec {
func New(typer TypeCodec, tagNames []string, durangoTime time.Time, maxSliceLen uint32) codec.Codec {
return &genericCodec{
typer: typer,
durangoTime: durangoTime,
maxSliceLen: maxSliceLen,
fielder: NewStructFielder(tagNames),
}
Expand Down Expand Up @@ -361,7 +364,14 @@ func (c *genericCodec) marshal(
return p.Err
case reflect.Slice:
numElts := value.Len() // # elements in the slice/array. 0 if this slice is nil.
if uint32(numElts) > c.maxSliceLen {
if numElts > math.MaxInt32 {
return fmt.Errorf("%w; slice length, %d, exceeds maximum length, %d",
codec.ErrMaxSliceLenExceeded,
numElts,
math.MaxInt32,
)
}
if time.Now().Before(c.durangoTime) && uint32(numElts) > c.maxSliceLen {
return fmt.Errorf("%w; slice length, %d, exceeds maximum length, %d",
codec.ErrMaxSliceLenExceeded,
numElts,
Expand Down Expand Up @@ -391,19 +401,12 @@ func (c *genericCodec) marshal(
}
return nil
case reflect.Array:
numElts := value.Len()
if elemKind := value.Type().Kind(); elemKind == reflect.Uint8 {
sliceVal := value.Convert(reflect.TypeOf([]byte{}))
p.PackFixedBytes(sliceVal.Bytes())
return p.Err
}
if uint32(numElts) > c.maxSliceLen {
return fmt.Errorf("%w; array length, %d, exceeds maximum length, %d",
codec.ErrMaxSliceLenExceeded,
numElts,
c.maxSliceLen,
)
}
numElts := value.Len()
for i := 0; i < numElts; i++ { // Process each element in the array
if err := c.marshal(value.Index(i), p, typeStack); err != nil {
return err
Expand All @@ -424,7 +427,14 @@ func (c *genericCodec) marshal(
case reflect.Map:
keys := value.MapKeys()
numElts := len(keys)
if uint32(numElts) > c.maxSliceLen {
if numElts > math.MaxInt32 {
return fmt.Errorf("%w; slice length, %d, exceeds maximum length, %d",
codec.ErrMaxSliceLenExceeded,
numElts,
math.MaxInt32,
)
}
if time.Now().Before(c.durangoTime) && uint32(numElts) > c.maxSliceLen {
return fmt.Errorf("%w; map length, %d, exceeds maximum length, %d",
codec.ErrMaxSliceLenExceeded,
numElts,
Expand Down Expand Up @@ -586,18 +596,18 @@ func (c *genericCodec) unmarshal(
if p.Err != nil {
return fmt.Errorf("couldn't unmarshal slice: %w", p.Err)
}
if numElts32 > c.maxSliceLen {
if numElts32 > math.MaxInt32 {
return fmt.Errorf("%w; array length, %d, exceeds maximum length, %d",
codec.ErrMaxSliceLenExceeded,
numElts32,
c.maxSliceLen,
math.MaxInt32,
)
}
if numElts32 > math.MaxInt32 {
if time.Now().Before(c.durangoTime) && numElts32 > c.maxSliceLen {
return fmt.Errorf("%w; array length, %d, exceeds maximum length, %d",
codec.ErrMaxSliceLenExceeded,
numElts32,
math.MaxInt32,
c.maxSliceLen,
)
}
numElts := int(numElts32)
Expand Down Expand Up @@ -694,7 +704,14 @@ func (c *genericCodec) unmarshal(
if p.Err != nil {
return fmt.Errorf("couldn't unmarshal map: %w", p.Err)
}
if numElts32 > c.maxSliceLen {
if numElts32 > math.MaxInt32 {
return fmt.Errorf("%w; map length, %d, exceeds maximum length, %d",
codec.ErrMaxSliceLenExceeded,
numElts32,
math.MaxInt32,
)
}
if time.Now().Before(c.durangoTime) && numElts32 > c.maxSliceLen {
return fmt.Errorf("%w; map length, %d, exceeds maximum length, %d",
codec.ErrMaxSliceLenExceeded,
numElts32,
Expand Down
4 changes: 3 additions & 1 deletion database/encdb/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package encdb

import (
"time"

"github.com/ava-labs/avalanchego/codec"
"github.com/ava-labs/avalanchego/codec/linearcodec"
)
Expand All @@ -13,7 +15,7 @@ const CodecVersion = 0
var Codec codec.Manager

func init() {
lc := linearcodec.NewDefault()
lc := linearcodec.NewDefault(time.Time{})
Codec = codec.NewDefaultManager()

if err := Codec.RegisterCodec(CodecVersion, lc); err != nil {
Expand Down
3 changes: 2 additions & 1 deletion database/linkeddb/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package linkeddb

import (
"math"
"time"

"github.com/ava-labs/avalanchego/codec"
"github.com/ava-labs/avalanchego/codec/linearcodec"
Expand All @@ -15,7 +16,7 @@ const CodecVersion = 0
var Codec codec.Manager

func init() {
lc := linearcodec.NewCustomMaxLength(math.MaxUint32)
lc := linearcodec.NewDefault(time.Time{})
Codec = codec.NewManager(math.MaxInt32)

if err := Codec.RegisterCodec(CodecVersion, lc); err != nil {
Expand Down
9 changes: 6 additions & 3 deletions genesis/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,9 +552,12 @@ func VMGenesis(genesisBytes []byte, vmID ids.ID) (*pchaintxs.Tx, error) {
}

func AVAXAssetID(avmGenesisBytes []byte) (ids.ID, error) {
parser, err := xchaintxs.NewParser([]fxs.Fx{
&secp256k1fx.Fx{},
})
parser, err := xchaintxs.NewParser(
time.Time{},
[]fxs.Fx{
&secp256k1fx.Fx{},
},
)
if err != nil {
return ids.Empty, err
}
Expand Down
3 changes: 2 additions & 1 deletion indexer/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package indexer

import (
"math"
"time"

"github.com/ava-labs/avalanchego/codec"
"github.com/ava-labs/avalanchego/codec/linearcodec"
Expand All @@ -15,7 +16,7 @@ const CodecVersion = 0
var Codec codec.Manager

func init() {
lc := linearcodec.NewCustomMaxLength(math.MaxUint32)
lc := linearcodec.NewDefault(time.Time{})
Codec = codec.NewManager(math.MaxInt)

if err := Codec.RegisterCodec(CodecVersion, lc); err != nil {
Expand Down
Loading

0 comments on commit 34917f3

Please sign in to comment.