Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
codchen committed May 11, 2023
1 parent efabc4f commit 5267383
Show file tree
Hide file tree
Showing 11 changed files with 129 additions and 73 deletions.
7 changes: 6 additions & 1 deletion app/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/sei-protocol/sei-chain/app/antedecorators/depdecorators"
"github.com/sei-protocol/sei-chain/utils/tracing"
"github.com/sei-protocol/sei-chain/x/dex"
dexcache "github.com/sei-protocol/sei-chain/x/dex/cache"
dexkeeper "github.com/sei-protocol/sei-chain/x/dex/keeper"
"github.com/sei-protocol/sei-chain/x/oracle"
oraclekeeper "github.com/sei-protocol/sei-chain/x/oracle/keeper"
Expand All @@ -31,6 +32,7 @@ type HandlerOptions struct {
DexKeeper *dexkeeper.Keeper
AccessControlKeeper *aclkeeper.Keeper
TXCounterStoreKey sdk.StoreKey
CheckTxMemState *dexcache.MemState

TracingInfo *tracing.Info
}
Expand Down Expand Up @@ -60,6 +62,9 @@ func NewAnteHandlerAndDepGenerator(options HandlerOptions) (sdk.AnteHandler, sdk
if options.TracingInfo == nil {
return nil, nil, sdkerrors.Wrap(sdkerrors.ErrLogic, "tracing info is required for ante builder")
}
if options.CheckTxMemState == nil {
return nil, nil, sdkerrors.Wrap(sdkerrors.ErrLogic, "checktx memstate is required for ante builder")
}

sigGasConsumer := options.SigGasConsumer
if sigGasConsumer == nil {
Expand Down Expand Up @@ -89,7 +94,7 @@ func NewAnteHandlerAndDepGenerator(options HandlerOptions) (sdk.AnteHandler, sdk
sdk.CustomDepWrappedAnteDecorator(ante.NewIncrementSequenceDecorator(options.AccountKeeper), depdecorators.SignerDepDecorator{ReadOnly: false}),
sdk.DefaultWrappedAnteDecorator(ibcante.NewAnteDecorator(options.IBCKeeper)),
sdk.DefaultWrappedAnteDecorator(dex.NewTickSizeMultipleDecorator(*options.DexKeeper)),
sdk.DefaultWrappedAnteDecorator(dex.NewCheckDexGasDecorator(*options.DexKeeper)),
sdk.DefaultWrappedAnteDecorator(dex.NewCheckDexGasDecorator(*options.DexKeeper, options.CheckTxMemState)),
antedecorators.NewACLWasmDependencyDecorator(*options.AccessControlKeeper, *options.WasmKeeper),
}

Expand Down
1 change: 1 addition & 0 deletions app/ante_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ func (suite *AnteTestSuite) SetupTest(isCheckTx bool) {
DexKeeper: &suite.App.DexKeeper,
AccessControlKeeper: &suite.App.AccessControlKeeper,
TracingInfo: tracingInfo,
CheckTxMemState: suite.App.CheckTxMemState,
},
)

Expand Down
23 changes: 21 additions & 2 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,10 @@ type App struct {
metricCounter *map[string]float32

mounter func()

CheckTxMemState *dexcache.MemState
ProcessProposalMemState *dexcache.MemState
MemState *dexcache.MemState
}

// New returns a reference to an initialized blockchain app
Expand Down Expand Up @@ -781,6 +785,10 @@ func New(
}
app.mounter()

app.CheckTxMemState = dexcache.NewMemState(app.GetMemKey(dexmoduletypes.MemStoreKey))
app.ProcessProposalMemState = dexcache.NewMemState(app.GetMemKey(dexmoduletypes.MemStoreKey))
app.MemState = dexcache.NewMemState(app.GetMemKey(dexmoduletypes.MemStoreKey))

// initialize BaseApp
app.SetInitChainer(app.InitChainer)
app.SetBeginBlocker(app.BeginBlocker)
Expand All @@ -806,6 +814,7 @@ func New(
DexKeeper: &app.DexKeeper,
TracingInfo: app.tracingInfo,
AccessControlKeeper: &app.AccessControlKeeper,
CheckTxMemState: app.CheckTxMemState,
},
)
if err != nil {
Expand Down Expand Up @@ -854,7 +863,6 @@ func New(
app.ScopedTransferKeeper = scopedTransferKeeper
app.ScopedWasmKeeper = scopedWasmKeeper
// this line is used by starport scaffolding # stargate/app/beforeInitReturn

return app
}

Expand Down Expand Up @@ -928,6 +936,7 @@ func (app *App) InitChainer(ctx sdk.Context, req abci.RequestInitChain) abci.Res
if err := json.Unmarshal(req.AppStateBytes, &genesisState); err != nil {
panic(err)
}
ctx = ctx.WithContext(app.decorateContextWithDexMemState(ctx.Context()))
app.UpgradeKeeper.SetModuleVersionMap(ctx, app.mm.GetVersionMap())
return app.mm.InitGenesis(ctx, app.appCodec, genesisState)
}
Expand Down Expand Up @@ -973,6 +982,7 @@ func (app *App) ProcessProposalHandler(ctx sdk.Context, req *abci.RequestProcess
app.optimisticProcessingInfo.Completion <- struct{}{}
} else {
go func() {
ctx = ctx.WithContext(app.decorateProcessProposalContextWithDexMemState(ctx.Context()))
events, txResults, endBlockResp, _ := app.ProcessBlock(ctx, req.Txs, req, req.ProposedLastCommit)
optimisticProcessingInfo.Events = events
optimisticProcessingInfo.TxRes = txResults
Expand Down Expand Up @@ -1006,6 +1016,7 @@ func (app *App) FinalizeBlocker(ctx sdk.Context, req *abci.RequestFinalizeBlock)
}
}
ctx.Logger().Info("optimistic processing ineligible")
ctx = ctx.WithContext(app.decorateContextWithDexMemState(ctx.Context()))
events, txResults, endBlockResp, _ := app.ProcessBlock(ctx, req.Txs, req, req.DecidedLastCommit)

app.SetDeliverStateToCommit()
Expand Down Expand Up @@ -1552,8 +1563,16 @@ func (app *App) BlacklistedAccAddrs() map[string]bool {
return blacklistedAddrs
}

func (app *App) decorateCheckTxContextWithDexMemState(base context.Context) context.Context {
return context.WithValue(base, dexutils.DexMemStateContextKey, app.CheckTxMemState)
}

func (app *App) decorateProcessProposalContextWithDexMemState(base context.Context) context.Context {
return context.WithValue(base, dexutils.DexMemStateContextKey, app.ProcessProposalMemState)
}

func (app *App) decorateContextWithDexMemState(base context.Context) context.Context {
return context.WithValue(base, dexutils.DexMemStateContextKey, dexcache.NewMemState(app.GetMemKey(dexmoduletypes.MemStoreKey)))
return context.WithValue(base, dexutils.DexMemStateContextKey, app.MemState)
}

func init() {
Expand Down
2 changes: 2 additions & 0 deletions app/apptesting/test_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
"github.com/cosmos/cosmos-sdk/x/staking/teststaking"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
dexutils "github.com/sei-protocol/sei-chain/x/dex/utils"
"github.com/stretchr/testify/suite"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto/ed25519"
Expand All @@ -39,6 +40,7 @@ type KeeperTestHelper struct {
func (s *KeeperTestHelper) Setup() {
s.App = app.Setup(false)
s.Ctx = s.App.BaseApp.NewContext(false, tmtypes.Header{Height: 1, ChainID: "sei-test", Time: time.Now().UTC()})
s.Ctx = s.Ctx.WithContext(context.WithValue(s.Ctx.Context(), dexutils.DexMemStateContextKey, s.App.MemState))
s.QueryHelper = &baseapp.QueryServiceTestHelper{
GRPCQueryRouter: s.App.GRPCQueryRouter(),
Ctx: s.Ctx,
Expand Down
16 changes: 12 additions & 4 deletions x/dex/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
dexcache "github.com/sei-protocol/sei-chain/x/dex/cache"
"github.com/sei-protocol/sei-chain/x/dex/keeper"
"github.com/sei-protocol/sei-chain/x/dex/types"
"github.com/sei-protocol/sei-chain/x/dex/utils"
Expand Down Expand Up @@ -100,12 +101,14 @@ func IsDecimalMultipleOf(a, b sdk.Dec) bool {
const DexGasFeeUnit = "usei"

type CheckDexGasDecorator struct {
dexKeeper keeper.Keeper
dexKeeper keeper.Keeper
checkTxMemState *dexcache.MemState
}

func NewCheckDexGasDecorator(dexKeeper keeper.Keeper) CheckDexGasDecorator {
func NewCheckDexGasDecorator(dexKeeper keeper.Keeper, checkTxMemState *dexcache.MemState) CheckDexGasDecorator {
return CheckDexGasDecorator{
dexKeeper: dexKeeper,
dexKeeper: dexKeeper,
checkTxMemState: checkTxMemState,
}
}

Expand All @@ -116,7 +119,12 @@ func (d CheckDexGasDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bo
}
params := d.dexKeeper.GetParams(ctx)
dexGasRequired := uint64(0)
memState := utils.GetMemState(ctx.Context())
var memState *dexcache.MemState
if ctx.IsCheckTx() {
memState = d.checkTxMemState
} else {
memState = utils.GetMemState(ctx.Context())
}
contractLoader := func(addr string) *types.ContractInfoV2 {
contract, err := d.dexKeeper.GetContract(ctx, addr)
if err != nil {
Expand Down
33 changes: 2 additions & 31 deletions x/dex/ante_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
keepertest "github.com/sei-protocol/sei-chain/testutil/keeper"
"github.com/sei-protocol/sei-chain/x/dex"
dexcache "github.com/sei-protocol/sei-chain/x/dex/cache"
"github.com/sei-protocol/sei-chain/x/dex/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -65,7 +66,7 @@ func TestIsDecimalMultipleOf(t *testing.T) {

func TestCheckDexGasDecorator(t *testing.T) {
keeper, ctx := keepertest.DexKeeper(t)
decorator := dex.NewCheckDexGasDecorator(*keeper)
decorator := dex.NewCheckDexGasDecorator(*keeper, dexcache.NewMemState(keeper.GetMemStoreKey()))
terminator := func(ctx sdk.Context, tx sdk.Tx, simulate bool) (newCtx sdk.Context, err error) { return ctx, nil }
tx := TestTx{
msgs: []sdk.Msg{
Expand Down Expand Up @@ -180,36 +181,6 @@ func TestTickSizeMultipleDecorator(t *testing.T) {
_, err = decorator.AnteHandle(ctx, tx, false, terminator)
require.NotNil(t, err)

tx = TestTx{
msgs: []sdk.Msg{
types.NewMsgPlaceOrders("someone", []*types.Order{{
ContractAddr: "contract",
PriceDenom: keepertest.TestPair.PriceDenom,
AssetDenom: keepertest.TestPair.AssetDenom,
Price: sdk.ZeroDec(),
Quantity: quantity,
OrderType: types.OrderType_STOPLOSS,
}}, "contract", sdk.NewCoins())},
fee: sdk.NewCoins(sdk.NewCoin("usei", sdk.NewInt(27500))),
}
_, err = decorator.AnteHandle(ctx, tx, false, terminator)
require.NotNil(t, err)

tx = TestTx{
msgs: []sdk.Msg{
types.NewMsgPlaceOrders("someone", []*types.Order{{
ContractAddr: "contract",
PriceDenom: keepertest.TestPair.PriceDenom,
AssetDenom: keepertest.TestPair.AssetDenom,
Price: sdk.ZeroDec(),
Quantity: quantity,
OrderType: types.OrderType_STOPLIMIT,
}}, "contract", sdk.NewCoins())},
fee: sdk.NewCoins(sdk.NewCoin("usei", sdk.NewInt(27500))),
}
_, err = decorator.AnteHandle(ctx, tx, false, terminator)
require.NotNil(t, err)

// Non-zero Priced Market Order With Divisible Price
tx = TestTx{
msgs: []sdk.Msg{
Expand Down
14 changes: 1 addition & 13 deletions x/dex/keeper/msgserver/msg_server_place_orders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,6 @@ func TestPlaceOrder(t *testing.T) {
PriceDenom: keepertest.TestPriceDenom,
AssetDenom: keepertest.TestAssetDenom,
},
{
Price: sdk.MustNewDecFromStr("20"),
Quantity: sdk.MustNewDecFromStr("5"),
Data: "",
PositionDirection: types.PositionDirection_SHORT,
OrderType: types.OrderType_STOPLOSS,
PriceDenom: keepertest.TestPriceDenom,
AssetDenom: keepertest.TestAssetDenom,
TriggerPrice: sdk.MustNewDecFromStr("10"),
TriggerStatus: false,
},
},
}
keeper, ctx := keepertest.DexKeeper(t)
Expand All @@ -64,10 +53,9 @@ func TestPlaceOrder(t *testing.T) {
server := msgserver.NewMsgServerImpl(*keeper)
res, err := server.PlaceOrders(wctx, msg)
require.Nil(t, err)
require.Equal(t, 3, len(res.OrderIds))
require.Equal(t, 2, len(res.OrderIds))
require.Equal(t, uint64(0), res.OrderIds[0])
require.Equal(t, uint64(1), res.OrderIds[1])
require.Equal(t, uint64(2), res.OrderIds[2])
// Ensure that contract addr and account is set in the order
require.Equal(t, msg.Orders[0].ContractAddr, TestContract)
require.Equal(t, msg.Orders[0].Account, TestCreator)
Expand Down
2 changes: 1 addition & 1 deletion x/dex/keeper/query/grpc_query_order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,5 @@ func TestGetOrders(t *testing.T) {
}
resp, err := wrapper.GetOrders(wctx, &query)
require.Nil(t, err)
require.Equal(t, 2, len(resp.Orders))
require.Equal(t, 1, len(resp.Orders))
}
Loading

0 comments on commit 5267383

Please sign in to comment.