Skip to content

Commit

Permalink
temp merge
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenButtolph committed Mar 11, 2024
2 parents 5ecde55 + ddf66ea commit f37699e
Show file tree
Hide file tree
Showing 109 changed files with 534 additions and 1,286 deletions.
4 changes: 1 addition & 3 deletions api/keystore/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
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 @@ -20,7 +18,7 @@ const (
var Codec codec.Manager

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

import (
"math"
"time"

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

func init() {
lc := linearcodec.NewDefault(time.Time{})
lc := linearcodec.NewDefault()
Codec = codec.NewManager(math.MaxInt)
if err := Codec.RegisterCodec(CodecVersion, lc); err != nil {
panic(err)
Expand Down
17 changes: 6 additions & 11 deletions chains/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package chains
import (
"context"
"crypto"
"crypto/tls"
"errors"
"fmt"
"os"
Expand Down Expand Up @@ -173,7 +172,8 @@ type ChainConfig struct {

type ManagerConfig struct {
SybilProtectionEnabled bool
StakingTLSCert tls.Certificate // needed to sign snowman++ blocks
StakingTLSSigner crypto.Signer
StakingTLSCert *staking.Certificate
StakingBLSKey *bls.SecretKey
TracingEnabled bool
// Must not be used unless [TracingEnabled] is true as this may be nil.
Expand Down Expand Up @@ -239,9 +239,6 @@ type manager struct {
ids.Aliaser
ManagerConfig

stakingSigner crypto.Signer
stakingCert *staking.Certificate

// Those notified when a chain is created
registrants []Registrant

Expand All @@ -268,8 +265,6 @@ func New(config *ManagerConfig) Manager {
return &manager{
Aliaser: ids.NewAliaser(),
ManagerConfig: *config,
stakingSigner: config.StakingTLSCert.PrivateKey.(crypto.Signer),
stakingCert: staking.CertificateFromX509(config.StakingTLSCert.Leaf),
chains: make(map[ids.ID]handler.Handler),
chainsQueue: buffer.NewUnboundedBlockingDeque[ChainParameters](initialQueueSize),
unblockChainCreatorCh: make(chan struct{}),
Expand Down Expand Up @@ -725,8 +720,8 @@ func (m *manager) createAvalancheChain(
MinimumPChainHeight: m.ApricotPhase4MinPChainHeight,
MinBlkDelay: minBlockDelay,
NumHistoricalBlocks: numHistoricalBlocks,
StakingLeafSigner: m.stakingSigner,
StakingCertLeaf: m.stakingCert,
StakingLeafSigner: m.StakingTLSSigner,
StakingCertLeaf: m.StakingTLSCert,
},
)

Expand Down Expand Up @@ -1062,8 +1057,8 @@ func (m *manager) createSnowmanChain(
MinimumPChainHeight: m.ApricotPhase4MinPChainHeight,
MinBlkDelay: minBlockDelay,
NumHistoricalBlocks: numHistoricalBlocks,
StakingLeafSigner: m.stakingSigner,
StakingCertLeaf: m.stakingCert,
StakingLeafSigner: m.StakingTLSSigner,
StakingCertLeaf: m.StakingTLSCert,
},
)

Expand Down
14 changes: 4 additions & 10 deletions codec/hierarchycodec/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,13 @@ import (
"fmt"
"reflect"
"sync"
"time"

"github.com/ava-labs/avalanchego/codec"
"github.com/ava-labs/avalanchego/codec/reflectcodec"
"github.com/ava-labs/avalanchego/utils/bimap"
"github.com/ava-labs/avalanchego/utils/wrappers"
)

const (
// default max length of a slice being marshalled by Marshal(). Should be <= math.MaxUint32.
defaultMaxSliceLength = 256 * 1024
)

var (
_ Codec = (*hierarchyCodec)(nil)
_ codec.Codec = (*hierarchyCodec)(nil)
Expand Down Expand Up @@ -51,19 +45,19 @@ type hierarchyCodec struct {
}

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

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

// SkipRegistrations some number of type IDs
Expand Down
22 changes: 3 additions & 19 deletions codec/hierarchycodec/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,25 @@ package hierarchycodec

import (
"testing"
"time"

"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(mockable.MaxTime)
c := NewDefault()
test(c, t)
}
}

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

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

func TestIgnoreSliceLen(t *testing.T) {
for _, test := range codec.IgnoreSliceLenTests {
c := NewDefault(time.Time{})
c := New([]string{"tag1", "tag2"})
test(c, t)
}
}

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

"github.com/ava-labs/avalanchego/codec"
"github.com/ava-labs/avalanchego/codec/reflectcodec"
"github.com/ava-labs/avalanchego/utils/bimap"
"github.com/ava-labs/avalanchego/utils/wrappers"
)

const (
// default max length of a slice being marshalled by Marshal(). Should be <= math.MaxUint32.
DefaultMaxSliceLength = 256 * 1024
)

var (
_ Codec = (*linearCodec)(nil)
_ codec.Codec = (*linearCodec)(nil)
Expand All @@ -43,25 +37,20 @@ type linearCodec struct {
registeredTypes *bimap.BiMap[uint32, reflect.Type]
}

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

// NewDefault is a convenience constructor; it returns a new codec with reasonable default values
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(durangoTime time.Time, maxSliceLen uint32) Codec {
return New(durangoTime, []string{reflectcodec.DefaultTagName}, maxSliceLen)
// NewDefault is a convenience constructor; it returns a new codec with default
// tagNames.
func NewDefault() Codec {
return New([]string{reflectcodec.DefaultTagName})
}

// Skip some number of type IDs
Expand Down
22 changes: 3 additions & 19 deletions codec/linearcodec/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,25 @@ package linearcodec

import (
"testing"
"time"

"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(mockable.MaxTime)
c := NewDefault()
test(c, t)
}
}

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

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

func TestIgnoreSliceLen(t *testing.T) {
for _, test := range codec.IgnoreSliceLenTests {
c := NewDefault(time.Time{})
c := New([]string{"tag1", "tag2"})
test(c, t)
}
}

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

"github.com/ava-labs/avalanchego/codec"
"github.com/ava-labs/avalanchego/utils/set"
Expand Down Expand Up @@ -71,19 +70,15 @@ type TypeCodec interface {
// 6. Serialized fields must be exported
// 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
typer TypeCodec
fielder StructFielder
}

// New returns a new, concurrency-safe codec
func New(typer TypeCodec, tagNames []string, durangoTime time.Time, maxSliceLen uint32) codec.Codec {
func New(typer TypeCodec, tagNames []string) codec.Codec {
return &genericCodec{
typer: typer,
durangoTime: durangoTime,
maxSliceLen: maxSliceLen,
fielder: NewStructFielder(tagNames),
typer: typer,
fielder: NewStructFielder(tagNames),
}
}

Expand Down Expand Up @@ -378,13 +373,6 @@ func (c *genericCodec) marshal(
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,
c.maxSliceLen,
)
}
p.PackInt(uint32(numElts)) // pack # elements
if p.Err != nil {
return p.Err
Expand Down Expand Up @@ -445,13 +433,6 @@ func (c *genericCodec) marshal(
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,
c.maxSliceLen,
)
}
p.PackInt(uint32(numElts)) // pack # elements
if p.Err != nil {
return p.Err
Expand Down Expand Up @@ -619,13 +600,6 @@ func (c *genericCodec) unmarshal(
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,
c.maxSliceLen,
)
}
numElts := int(numElts32)

sliceType := value.Type()
Expand Down Expand Up @@ -732,13 +706,6 @@ func (c *genericCodec) unmarshal(
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,
c.maxSliceLen,
)
}

var (
numElts = int(numElts32)
Expand Down
Loading

0 comments on commit f37699e

Please sign in to comment.