Skip to content

Add more X-chain tests #1487

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

Merged
merged 13 commits into from
Jun 2, 2023
31 changes: 15 additions & 16 deletions codec/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,13 @@ const (
)

var (
ErrUnknownVersion = errors.New("unknown codec version")

errMarshalNil = errors.New("can't marshal nil pointer or interface")
errUnmarshalNil = errors.New("can't unmarshal nil")
errUnmarshalTooBig = errors.New("byte array exceeds maximum length")
errCantPackVersion = errors.New("couldn't pack codec version")
errCantUnpackVersion = errors.New("couldn't unpack codec version")
errDuplicatedVersion = errors.New("duplicated codec version")
ErrUnknownVersion = errors.New("unknown codec version")
ErrMarshalNil = errors.New("can't marshal nil pointer or interface")
ErrUnmarshalNil = errors.New("can't unmarshal nil")
ErrUnmarshalTooBig = errors.New("byte array exceeds maximum length")
ErrCantPackVersion = errors.New("couldn't pack codec version")
ErrCantUnpackVersion = errors.New("couldn't unpack codec version")
ErrDuplicatedVersion = errors.New("duplicated codec version")
)

var _ Manager = (*manager)(nil)
Expand All @@ -43,7 +42,7 @@ type Manager interface {
// Size returns the size, in bytes, of [value] when it's marshaled
// using the codec with the given version.
// RegisterCodec must have been called with that version.
// If [value] is nil, returns [errMarshalNil]
// If [value] is nil, returns [ErrMarshalNil]
Size(version uint16, value interface{}) (int, error)

// Marshal the given value using the codec with the given version.
Expand Down Expand Up @@ -82,15 +81,15 @@ func (m *manager) RegisterCodec(version uint16, codec Codec) error {
defer m.lock.Unlock()

if _, exists := m.codecs[version]; exists {
return errDuplicatedVersion
return ErrDuplicatedVersion
}
m.codecs[version] = codec
return nil
}

func (m *manager) Size(version uint16, value interface{}) (int, error) {
if value == nil {
return 0, errMarshalNil // can't marshal nil
return 0, ErrMarshalNil // can't marshal nil
}

m.lock.RLock()
Expand All @@ -110,7 +109,7 @@ func (m *manager) Size(version uint16, value interface{}) (int, error) {
// To marshal an interface, [value] must be a pointer to the interface.
func (m *manager) Marshal(version uint16, value interface{}) ([]byte, error) {
if value == nil {
return nil, errMarshalNil // can't marshal nil
return nil, ErrMarshalNil // can't marshal nil
}

m.lock.RLock()
Expand All @@ -126,7 +125,7 @@ func (m *manager) Marshal(version uint16, value interface{}) ([]byte, error) {
}
p.PackShort(version)
if p.Errored() {
return nil, errCantPackVersion // Should never happen
return nil, ErrCantPackVersion // Should never happen
}
return p.Bytes, c.MarshalInto(value, &p)
}
Expand All @@ -135,19 +134,19 @@ func (m *manager) Marshal(version uint16, value interface{}) ([]byte, error) {
// interface.
func (m *manager) Unmarshal(bytes []byte, dest interface{}) (uint16, error) {
if dest == nil {
return 0, errUnmarshalNil
return 0, ErrUnmarshalNil
}

if byteLen := len(bytes); byteLen > m.maxSize {
return 0, fmt.Errorf("%w: %d > %d", errUnmarshalTooBig, byteLen, m.maxSize)
return 0, fmt.Errorf("%w: %d > %d", ErrUnmarshalTooBig, byteLen, m.maxSize)
}

p := wrappers.Packer{
Bytes: bytes,
}
version := p.UnpackShort()
if p.Errored() { // Make sure the codec version is correct
return 0, errCantUnpackVersion
return 0, ErrCantUnpackVersion
}

m.lock.RLock()
Expand Down
2 changes: 1 addition & 1 deletion codec/test_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,7 @@ func TestTooLargeUnmarshal(codec GeneralCodec, t testing.TB) {

s := inner{}
_, err := manager.Unmarshal(bytes, &s)
require.ErrorIs(err, errUnmarshalTooBig)
require.ErrorIs(err, ErrUnmarshalTooBig)
}

type outerInterface interface {
Expand Down
12 changes: 12 additions & 0 deletions vms/avm/blocks/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ var (
assetID = ids.GenerateTestID()
)

func TestInvalidBlock(t *testing.T) {
require := require.New(t)

parser, err := NewParser([]fxs.Fx{
&secp256k1fx.Fx{},
})
require.NoError(err)

_, err = parser.ParseBlock(nil)
require.ErrorIs(err, codec.ErrCantUnpackVersion)
}

func TestStandardBlocks(t *testing.T) {
// check standard block can be built and parsed
require := require.New(t)
Expand Down
Loading