Skip to content

Commit

Permalink
fix type_parser tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wlawt committed Apr 23, 2024
1 parent 29048b3 commit ef177a2
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 25 deletions.
2 changes: 0 additions & 2 deletions chain/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ package chain
import (
"time"

"github.com/ava-labs/avalanchego/utils/units"

"github.com/ava-labs/hypersdk/keys"
)

Expand Down
2 changes: 1 addition & 1 deletion chain/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ func (t *Transaction) Execute(
// are set when this function is defined. If any of them are
// modified later, they will not be used here.
ts.Rollback(ctx, actionStart)
return &Result{false, utils.ErrBytes(rerr), maxUnits, maxFee, nil}, nil
return &Result{false, utils.ErrBytes(rerr), maxUnits, maxFee}, nil
}
success, actionCUs, output, err := t.Action.Execute(ctx, r, ts, timestamp, t.Auth.Actor(), t.id)
if err != nil {
Expand Down
20 changes: 10 additions & 10 deletions codec/type_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,42 @@ import (
"github.com/ava-labs/hypersdk/consts"
)

type decoder[T any, X any, Y any] struct {
f func(*Packer, X) (T, error)
type decoder[T any, Y any] struct {
f func(*Packer) (T, error)
y Y
}

// The number of types is limited to 255.
type TypeParser[T any, X any, Y any] struct {
type TypeParser[T any, Y any] struct {
typeToIndex map[string]uint8
indexToDecoder map[uint8]*decoder[T, X, Y]
indexToDecoder map[uint8]*decoder[T, Y]
}

// NewTypeParser returns an instance of a Typeparser with generic type [T].
func NewTypeParser[T any, X any, Y bool]() *TypeParser[T, X, Y] {
return &TypeParser[T, X, Y]{
func NewTypeParser[T any, Y bool]() *TypeParser[T, Y] {
return &TypeParser[T, Y]{
typeToIndex: map[string]uint8{},
indexToDecoder: map[uint8]*decoder[T, X, Y]{},
indexToDecoder: map[uint8]*decoder[T, Y]{},
}
}

// Register registers a new type into TypeParser [p]. Registers the type by using
// the string representation of [o], and sets the decoder of that index to [f].
// Returns an error if [o] has already been registered or the TypeParser is full.
func (p *TypeParser[T, X, Y]) Register(id uint8, f func(*Packer, X) (T, error), y Y) error {
func (p *TypeParser[T, Y]) Register(id uint8, f func(*Packer) (T, error), y Y) error {
if len(p.indexToDecoder) == int(consts.MaxUint8)+1 {
return ErrTooManyItems
}
if _, ok := p.indexToDecoder[id]; ok {
return ErrDuplicateItem
}
p.indexToDecoder[id] = &decoder[T, X, Y]{f, y}
p.indexToDecoder[id] = &decoder[T, Y]{f, y}
return nil
}

// LookupIndex returns the decoder function and success of lookup of [index]
// from Typeparser [p].
func (p *TypeParser[T, X, Y]) LookupIndex(index uint8) (func(*Packer, X) (T, error), Y, bool) {
func (p *TypeParser[T, Y]) LookupIndex(index uint8) (func(*Packer) (T, error), Y, bool) {
d, ok := p.indexToDecoder[index]
if ok {
return d.f, d.y, true
Expand Down
10 changes: 5 additions & 5 deletions codec/type_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (*Blah3) Bark() string { return "blah3" }
func (*Blah3) GetTypeID() uint8 { return 2 }

func TestTypeParser(t *testing.T) {
tp := NewTypeParser[Blah, any, bool]()
tp := NewTypeParser[Blah, bool]()

t.Run("empty parser", func(t *testing.T) {
require := require.New(t)
Expand All @@ -54,29 +54,29 @@ func TestTypeParser(t *testing.T) {
require.NoError(
tp.Register(
blah1.GetTypeID(),
func(*Packer, any) (Blah, error) { return nil, errBlah1 },
func(*Packer) (Blah, error) { return nil, errBlah1 },
true,
),
)
require.NoError(
tp.Register(
blah2.GetTypeID(),
func(*Packer, any) (Blah, error) { return nil, errBlah2 },
func(*Packer) (Blah, error) { return nil, errBlah2 },
false,
),
)

f, b, ok := tp.LookupIndex(blah1.GetTypeID())
require.True(ok)
require.True(b)
res, err := f(nil, nil)
res, err := f(nil)
require.Nil(res)
require.ErrorIs(err, errBlah1)

f, b, ok = tp.LookupIndex(blah2.GetTypeID())
require.True(ok)
require.False(b)
res, err = f(nil, nil)
res, err = f(nil)
require.Nil(res)
require.ErrorIs(err, errBlah2)
})
Expand Down
14 changes: 7 additions & 7 deletions examples/tokenvm/actions/burn_asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,26 +52,26 @@ func (b *BurnAsset) Execute(
_ bool,
) (bool, uint64, []byte, error) {
if b.Value == 0 {
return false, BurnComputeUnits, OutputValueZero, nil, nil
return false, BurnComputeUnits, OutputValueZero, nil
}
if err := storage.SubBalance(ctx, mu, actor, b.Asset, b.Value); err != nil {
return false, BurnComputeUnits, utils.ErrBytes(err), nil, nil
return false, BurnComputeUnits, utils.ErrBytes(err), nil
}
exists, symbol, decimals, metadata, supply, owner, err := storage.GetAsset(ctx, mu, b.Asset)
if err != nil {
return false, BurnComputeUnits, utils.ErrBytes(err), nil, nil
return false, BurnComputeUnits, utils.ErrBytes(err), nil
}
if !exists {
return false, BurnComputeUnits, OutputAssetMissing, nil, nil
return false, BurnComputeUnits, OutputAssetMissing, nil
}
newSupply, err := smath.Sub(supply, b.Value)
if err != nil {
return false, BurnComputeUnits, utils.ErrBytes(err), nil, nil
return false, BurnComputeUnits, utils.ErrBytes(err), nil
}
if err := storage.SetAsset(ctx, mu, b.Asset, symbol, decimals, metadata, newSupply, owner); err != nil {
return false, BurnComputeUnits, utils.ErrBytes(err), nil, nil
return false, BurnComputeUnits, utils.ErrBytes(err), nil
}
return true, BurnComputeUnits, nil, nil, nil
return true, BurnComputeUnits, nil, nil
}

func (*BurnAsset) MaxComputeUnits(chain.Rules) uint64 {
Expand Down

0 comments on commit ef177a2

Please sign in to comment.