Skip to content

Commit

Permalink
fix test with cms update to use snapshot at simulation and check tx
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 committed Jun 17, 2024
1 parent d5dd63d commit c17c309
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 18 deletions.
4 changes: 2 additions & 2 deletions app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func TestInitGenesisOnMigration(t *testing.T) {
logger := log.NewLogger(os.Stdout)
app := NewInitiaApp(
logger, db, nil, true, moveconfig.DefaultMoveConfig(), apporacle.DefaultConfig(), EmptyAppOptions{})
ctx := app.NewContextLegacy(true, cmtproto.Header{Height: app.LastBlockHeight()})
ctx := app.NewUncachedContext(false, cmtproto.Header{Height: app.LastBlockHeight()})

// Create a mock module. This module will serve as the new module we're
// adding during a migration.
Expand Down Expand Up @@ -127,7 +127,7 @@ func TestUpgradeStateOnGenesis(t *testing.T) {
app := SetupWithGenesisAccounts(nil, nil)

// make sure the upgrade keeper has version map in state
ctx := app.NewContext(true)
ctx := app.NewUncachedContext(false, cmtproto.Header{})
vm, err := app.UpgradeKeeper.GetModuleVersionMap(ctx)
require.NoError(t, err)

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ replace (
// use cosmos fork of keyring
github.com/99designs/keyring => github.com/cosmos/keyring v1.2.0

github.com/cosmos/cosmos-sdk => github.com/initia-labs/cosmos-sdk v0.0.0-20240425031032-6bc18cf6e67d
github.com/cosmos/cosmos-sdk => github.com/initia-labs/cosmos-sdk v0.0.0-20240617100523-e47019ce8c64
github.com/cosmos/iavl => github.com/initia-labs/iavl v0.0.0-20240415085037-7e81233cdd9e

// dgrijalva/jwt-go is deprecated and doesn't receive security updates.
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -727,8 +727,8 @@ github.com/initia-labs/OPinit v0.3.1 h1:uffngNx8a7hQl3ENhVbdSAJ8j5MYksiaiCLVI6xu
github.com/initia-labs/OPinit v0.3.1/go.mod h1:pHU2472uQZpiKXBOa/D7/aDYn0gtTiddKvhloyliOQU=
github.com/initia-labs/OPinit/api v0.3.0 h1:OY8ijwmgZLoYwtw9LI1mSY3VC8PY+gtxJFitB6ZNFl4=
github.com/initia-labs/OPinit/api v0.3.0/go.mod h1:Xy/Nt3ubXLQ4zKn0m7RuQOM1sj8TVdlNNyek21TGYR0=
github.com/initia-labs/cosmos-sdk v0.0.0-20240425031032-6bc18cf6e67d h1:8OCL+PBoWydjxP07wE567ySQfY8gbu+rjE04OCNx/9U=
github.com/initia-labs/cosmos-sdk v0.0.0-20240425031032-6bc18cf6e67d/go.mod h1:lVkRY6cdMJ0fG3gp8y4hFrsKZqF4z7y0M2UXFb9Yt40=
github.com/initia-labs/cosmos-sdk v0.0.0-20240617100523-e47019ce8c64 h1:4eODaWXthTX2yCpSCXTZ4QgrkXoHnJqhKjKhRJMrg5k=
github.com/initia-labs/cosmos-sdk v0.0.0-20240617100523-e47019ce8c64/go.mod h1:lVkRY6cdMJ0fG3gp8y4hFrsKZqF4z7y0M2UXFb9Yt40=
github.com/initia-labs/iavl v0.0.0-20240415085037-7e81233cdd9e h1:1gkMWkAgVhYFhEv7K4tX+8uJJLdiTKlQhl5+wGaxdMg=
github.com/initia-labs/iavl v0.0.0-20240415085037-7e81233cdd9e/go.mod h1:jLeUvm6bGT1YutCaL2fIar/8vGUE8cPZvh/gXEWDaDM=
github.com/initia-labs/movevm v0.3.1 h1:vydw0mdqsLyPN9W2DVq1/gDXDR3snbr6c4Wy/sTYVxY=
Expand Down
13 changes: 10 additions & 3 deletions x/move/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/stretchr/testify/require"

abci "github.com/cometbft/cometbft/abci/types"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"

sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
Expand All @@ -22,14 +23,14 @@ func Test_BeginBlocker(t *testing.T) {
require.NoError(t, err)

// initialize staking for secondBondDenom
ctx := app.BaseApp.NewContext(false)
ctx := app.BaseApp.NewUncachedContext(false, tmproto.Header{})
err = app.MoveKeeper.InitializeStaking(ctx, secondBondDenom)
require.NoError(t, err)

// fund addr2
app.BankKeeper.SendCoins(ctx, types.StdAddr, addr2, sdk.NewCoins(secondBondCoin))

_, err = app.FinalizeBlock(&abci.RequestFinalizeBlock{Height: app.LastBlockHeight() + 1})
_, err = app.Commit()
require.NoError(t, err)

// delegate coins via move staking module
Expand Down Expand Up @@ -61,7 +62,7 @@ func Test_BeginBlocker(t *testing.T) {
require.NoError(t, err)

// generate rewards
ctx = app.BaseApp.NewContext(false)
ctx = app.BaseApp.NewUncachedContext(false, tmproto.Header{})
validator, err := app.StakingKeeper.Validator(ctx, sdk.ValAddress(addr1))
require.NoError(t, err)

Expand All @@ -78,10 +79,16 @@ func Test_BeginBlocker(t *testing.T) {
secondBondDenom,
sdk.NewDecCoinsFromCoins(rewardCoins...))

_, err = app.Commit()
require.NoError(t, err)

// rewards distributed
_, err = app.FinalizeBlock(&abci.RequestFinalizeBlock{Height: app.LastBlockHeight() + 1})
require.NoError(t, err)

_, err = app.Commit()
require.NoError(t, err)

// withdraw rewards to move module
_, err = app.FinalizeBlock(&abci.RequestFinalizeBlock{Height: app.LastBlockHeight() + 1})
require.NoError(t, err)
Expand Down
10 changes: 6 additions & 4 deletions x/move/ante/ante_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (

"github.com/stretchr/testify/suite"

tmproto "github.com/cometbft/cometbft/proto/tendermint/types"

"cosmossdk.io/log"
dbm "github.com/cosmos/cosmos-db"
"github.com/cosmos/cosmos-sdk/client"
Expand Down Expand Up @@ -38,15 +40,15 @@ type AnteTestSuite struct {
}

// returns context and app with params set on account keeper
func (suite *AnteTestSuite) createTestApp(isCheckTx bool, tempDir string) (*initiaapp.InitiaApp, sdk.Context) {
func (suite *AnteTestSuite) createTestApp(tempDir string) (*initiaapp.InitiaApp, sdk.Context) {
appOptions := make(simtestutil.AppOptionsMap, 0)
appOptions[flags.FlagHome] = tempDir
appOptions[server.FlagInvCheckPeriod] = simcli.FlagPeriodValue

app := initiaapp.NewInitiaApp(
log.NewNopLogger(), dbm.NewMemDB(), nil, true, moveconfig.DefaultMoveConfig(), initiaapporacle.DefaultConfig(), appOptions,
)
ctx := app.BaseApp.NewContext(isCheckTx)
ctx := app.BaseApp.NewUncachedContext(false, tmproto.Header{})
err := app.AccountKeeper.Params.Set(ctx, authtypes.DefaultParams())
suite.NoError(err)

Expand All @@ -56,9 +58,9 @@ func (suite *AnteTestSuite) createTestApp(isCheckTx bool, tempDir string) (*init
}

// SetupTest setups a new test, with new app, context, and anteHandler.
func (suite *AnteTestSuite) SetupTest(isCheckTx bool) {
func (suite *AnteTestSuite) SetupTest() {
tempDir := suite.T().TempDir()
suite.app, suite.ctx = suite.createTestApp(isCheckTx, tempDir)
suite.app, suite.ctx = suite.createTestApp(tempDir)
suite.ctx = suite.ctx.WithBlockHeight(1)

// Set up TxConfig.
Expand Down
2 changes: 1 addition & 1 deletion x/move/ante/fee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (k TestAnteKeeper) BaseMinGasPrice(ctx context.Context) (math.LegacyDec, er
}

func (suite *AnteTestSuite) TestEnsureMempoolFees() {
suite.SetupTest(true) // setup
suite.SetupTest() // setup
suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder()

dexPools := make(map[string][]math.Int)
Expand Down
2 changes: 1 addition & 1 deletion x/move/ante/gas_prices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func (suite *AnteTestSuite) TestGasPricesDecorator() {
suite.SetupTest(true) // setup
suite.SetupTest() // setup
suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder()

// keys and addresses
Expand Down
4 changes: 2 additions & 2 deletions x/move/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func createApp(t *testing.T) *initiaapp.InitiaApp {
_, err := app.FinalizeBlock(&abci.RequestFinalizeBlock{Height: app.LastBlockHeight() + 1})
require.NoError(t, err)

ctx := app.BaseApp.NewContext(false)
ctx := app.BaseApp.NewUncachedContext(false, tmproto.Header{})
createDexPool(t, ctx, app, baseCoin, quoteCoin, math.LegacyNewDecWithPrec(8, 1), math.LegacyNewDecWithPrec(2, 1))

// set reward weight
Expand All @@ -87,7 +87,7 @@ func createApp(t *testing.T) *initiaapp.InitiaApp {
// fund second bond coin
app.BankKeeper.SendCoins(ctx, types.StdAddr, addr1, sdk.NewCoins(secondBondCoin))

_, err = app.FinalizeBlock(&abci.RequestFinalizeBlock{Height: app.LastBlockHeight() + 1})
_, err = app.Commit()
require.NoError(t, err)

// create validator
Expand Down
31 changes: 29 additions & 2 deletions x/reward/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"

abci "github.com/cometbft/cometbft/abci/types"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"

"github.com/initia-labs/initia/x/reward/types"
)
Expand All @@ -22,7 +23,7 @@ func Test_BeginBlocker(t *testing.T) {
_, err := app.FinalizeBlock(&abci.RequestFinalizeBlock{Height: app.LastBlockHeight() + 1})
require.NoError(t, err)

ctx := app.BaseApp.NewContext(false)
ctx := app.BaseApp.NewUncachedContext(false, tmproto.Header{})

// update params & mint coins for reward distribution
params, err := app.RewardKeeper.GetParams(ctx)
Expand All @@ -46,19 +47,34 @@ func Test_BeginBlocker(t *testing.T) {
lastReleaseTimestamp, err := app.RewardKeeper.GetLastReleaseTimestamp(ctx)
require.NoError(t, err)

_, err = app.Commit()
require.NoError(t, err)

ctx = app.BaseApp.NewContext(true)
paramsAfter, err := app.RewardKeeper.GetParams(ctx)
require.NoError(t, err)
require.Equal(t, paramsAfter, params)

// new block after
_, err = app.FinalizeBlock(&abci.RequestFinalizeBlock{Height: app.LastBlockHeight() + 1, Time: lastReleaseTimestamp})
require.NoError(t, err)

_, err = app.Commit()
require.NoError(t, err)

// new block after 24 hours
_, err = app.FinalizeBlock(&abci.RequestFinalizeBlock{Height: app.LastBlockHeight() + 1, Time: lastReleaseTimestamp.Add(time.Hour * 24).Add(time.Second)})
require.NoError(t, err)

_, err = app.Commit()
require.NoError(t, err)

// check supply
expectedReleasedAmount := math.LegacyNewDec(7).QuoInt64(100).MulInt(supply.Amount).QuoInt64(365).TruncateInt()
checkBalance(t, app, authtypes.NewModuleAddress(types.ModuleName), rewardCoins.Sub(sdk.NewCoin(rewardDenom, expectedReleasedAmount)))

// release rate should be half
ctx = app.BaseApp.NewContext(true)
releaseRate, err := app.RewardKeeper.GetReleaseRate(ctx)
require.NoError(t, err)
require.Equal(t, math.LegacyNewDecWithPrec(35, 3), releaseRate)
Expand All @@ -78,7 +94,7 @@ func Test_BeginBlockerNotEnabled(t *testing.T) {
_, err := app.FinalizeBlock(&abci.RequestFinalizeBlock{Height: app.LastBlockHeight() + 1})
require.NoError(t, err)

ctx := app.BaseApp.NewContext(false)
ctx := app.BaseApp.NewUncachedContext(false, tmproto.Header{})

// update params & mint coins for reward distribution
params, err := app.RewardKeeper.GetParams(ctx)
Expand All @@ -100,14 +116,25 @@ func Test_BeginBlockerNotEnabled(t *testing.T) {
lastReleaseTimestamp, err := app.RewardKeeper.GetLastReleaseTimestamp(ctx)
require.NoError(t, err)

_, err = app.Commit()
require.NoError(t, err)

// new block after
_, err = app.FinalizeBlock(&abci.RequestFinalizeBlock{Height: app.LastBlockHeight() + 1, Time: lastReleaseTimestamp})
require.NoError(t, err)

_, err = app.Commit()
require.NoError(t, err)

// new block after 24 hours
_, err = app.FinalizeBlock(&abci.RequestFinalizeBlock{Height: app.LastBlockHeight() + 1, Time: lastReleaseTimestamp.Add(time.Hour * 24).Add(time.Second)})
require.NoError(t, err)

_, err = app.Commit()
require.NoError(t, err)

ctx = app.BaseApp.NewContext(true)

// check supply
expectedReleasedAmount := math.ZeroInt()
checkBalance(t, app, authtypes.NewModuleAddress(types.ModuleName), rewardCoins.Sub(sdk.NewCoin(rewardDenom, expectedReleasedAmount)))
Expand Down

0 comments on commit c17c309

Please sign in to comment.