Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
15 changes: 14 additions & 1 deletion app/antedecorators/gasless.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,27 @@ func NewGaslessDecorator(wrapped []sdk.AnteDecorator, oracleKeeper oraclekeeper.
func (gd GaslessDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
if !isTxGasless(tx, ctx, gd.oracleKeeper) {
// if not gasless, then we use the wrappers
return sdk.ChainAnteDecorators(gd.wrapped...)(ctx, tx, simulate)

// AnteHandle always takes a `next` so we need a no-op to execute only one handler at a time
terminatorHandler := func(ctx sdk.Context, _ sdk.Tx, _ bool) (sdk.Context, error) {
return ctx, nil
}
// iterating instead of recursing the handler for readability
for _, handler := range gd.wrapped {
ctx, err = handler.AnteHandle(ctx, tx, simulate, terminatorHandler)
}
return next(ctx, tx, simulate)
}
gaslessMeter := sdk.NewInfiniteGasMeter()

return next(ctx.WithGasMeter(gaslessMeter), tx, simulate)
}

func isTxGasless(tx sdk.Tx, ctx sdk.Context, oracleKeeper oraclekeeper.Keeper) bool {
if len(tx.GetMsgs()) == 0 {
// empty TX shouldn't be gasless
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

return false
}
for _, msg := range tx.GetMsgs() {
switch m := msg.(type) {
case *dextypes.MsgPlaceOrders:
Expand Down
55 changes: 55 additions & 0 deletions app/antedecorators/gasless_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package antedecorators_test

import (
"fmt"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/sei-protocol/sei-chain/app/antedecorators"
oraclekeeper "github.com/sei-protocol/sei-chain/x/oracle/keeper"
"github.com/stretchr/testify/require"
)

var output = ""

type FakeAnteDecoratorOne struct{}

func (ad FakeAnteDecoratorOne) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
output = fmt.Sprintf("%sone", output)
return next(ctx, tx, simulate)
}

type FakeAnteDecoratorTwo struct{}

func (ad FakeAnteDecoratorTwo) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
output = fmt.Sprintf("%stwo", output)
return next(ctx, tx, simulate)
}

type FakeAnteDecoratorThree struct{}

func (ad FakeAnteDecoratorThree) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
output = fmt.Sprintf("%sthree", output)
return next(ctx, tx, simulate)
}

type FakeTx struct{}

func (tx FakeTx) GetMsgs() []sdk.Msg {
return []sdk.Msg{}
}

func (tx FakeTx) ValidateBasic() error {
return nil
}

func TestGaslessDecorator(t *testing.T) {
anteDecorators := []sdk.AnteDecorator{
FakeAnteDecoratorOne{},
antedecorators.NewGaslessDecorator([]sdk.AnteDecorator{FakeAnteDecoratorTwo{}}, oraclekeeper.Keeper{}),
FakeAnteDecoratorThree{},
}
chainedHandler := sdk.ChainAnteDecorators(anteDecorators...)
chainedHandler(sdk.Context{}, FakeTx{}, false)
require.Equal(t, "onetwothree", output)
}