0g-chain/x/bep3/keeper/msg_server_test.go

130 lines
4.7 KiB
Go
Raw Normal View History

package keeper_test
import (
"testing"
"github.com/stretchr/testify/suite"
Update cosmos-sdk to v0.47.7 (#1811) * Update cometbft, cosmos, ethermint, and ibc-go * Replace github.com/tendermint/tendermint by github.com/cometbft/cometbft * Replace github.com/tendermint/tm-db by github.com/cometbft/cometbft-db * Replace gogo/protobuf with cosmos/gogoproto & simapp replacement * Replace cosmos-sdk/simapp/helpers with cosmos-sdk/testutil/sims * Remove no longer used simulations * Replace ibchost with ibcexported See https://github.com/cosmos/ibc-go/blob/v7.2.2/docs/migrations/v6-to-v7.md#ibc-module-constants * Add new consensus params keeper * Add consensus keeper to blockers * Fix keeper and module issues in app.go * Add IsSendEnabledCoins and update SetParams interface changes * Fix protobuf build for cosmos 47 (#1800) * fix cp errors by using -f; fix lint by only linting our proto dir; and use proofs.proto directly from ics23 for ibc-go v7 * run proto-all; commit updated third party deps and swagger changes * regenerate proto files * use correct gocosmos build plugin for buf * re-gen all protobuf files to update paths for new gocosmos plugin * update protoc and buf to latest versions * fix staking keeper issues in app.go * update tally handler for gov changes * chain id fix and flag fixes * update deps for cometbft 47.7 upgrade * remove all module legacy queriers * update stakingKeeper to pointer * Replace ModuleCdc from govv1beta1 to govcodec * remove simulations * abci.LastCommitInfo → abci.CommitInfo * Remove unused code in keys.go * simapp.MakeTestEncodingConfig -> moduletestutil.MakeTestEncodingConfi * Fix chain id issues in tests * Fix remaining unit test issues * Update changelog for upgrade * Fix e2e tests using updated kvtool * Update protonet to v47 compatible genesis * Bump cometbft-db to v0.9.1-kava.1 * Update kvtool * Remove extra changelog * Fix merged rocksdb issues * go mod cleanup * Bump cometbft-db to v9 and go to 1.21 * Bump rocksdb version to v8.10.0 * Update kvtool to latest version * Update gin to v1.9.0 * Use ibctm.ModuleName in app_test * Fallback to genesis chain id instead of client toml * Remove all simulations * Fix cdp migrations issue with v47 * Update dependencies to correct tags --------- Co-authored-by: Nick DeLuca <nickdeluca08@gmail.com>
2024-02-06 22:54:10 +00:00
tmbytes "github.com/cometbft/cometbft/libs/bytes"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
tmtime "github.com/cometbft/cometbft/types/time"
sdk "github.com/cosmos/cosmos-sdk/types"
2024-05-01 03:17:24 +00:00
"github.com/0glabs/0g-chain/app"
"github.com/0glabs/0g-chain/x/bep3"
"github.com/0glabs/0g-chain/x/bep3/keeper"
"github.com/0glabs/0g-chain/x/bep3/types"
)
type MsgServerTestSuite struct {
suite.Suite
ctx sdk.Context
app app.TestApp
msgServer types.MsgServer
keeper keeper.Keeper
addrs []sdk.AccAddress
}
func (suite *MsgServerTestSuite) SetupTest() {
tApp := app.NewTestApp()
ctx := tApp.NewContext(true, tmproto.Header{Height: 1, Time: tmtime.Now()})
cdc := tApp.AppCodec()
// Set up genesis state and initialize
_, addrs := app.GeneratePrivKeyAddressPairs(3)
2024-05-01 05:53:58 +00:00
coins := sdk.NewCoins(c("bnb", 10000000000), c("a0gi", 10000))
authGS := app.NewFundedGenStateWithSameCoins(tApp.AppCodec(), coins, addrs)
tApp.InitializeFromGenesisStates(authGS, NewBep3GenStateMulti(cdc, addrs[0]))
suite.addrs = addrs
suite.keeper = tApp.GetBep3Keeper()
suite.msgServer = keeper.NewMsgServerImpl(suite.keeper)
suite.app = tApp
suite.ctx = ctx
}
func (suite *MsgServerTestSuite) AddAtomicSwap() (tmbytes.HexBytes, tmbytes.HexBytes) {
expireHeight := types.DefaultMinBlockLock
amount := cs(c("bnb", int64(50000)))
timestamp := ts(0)
randomNumber, _ := types.GenerateSecureRandomNumber()
randomNumberHash := types.CalculateRandomHash(randomNumber[:], timestamp)
// Create atomic swap and check err to confirm creation
err := suite.keeper.CreateAtomicSwap(suite.ctx, randomNumberHash, timestamp, expireHeight,
suite.addrs[0], suite.addrs[1], TestSenderOtherChain, TestRecipientOtherChain,
amount, true)
suite.Nil(err)
swapID := types.CalculateSwapID(randomNumberHash, suite.addrs[0], TestSenderOtherChain)
return swapID, randomNumber[:]
}
func (suite *MsgServerTestSuite) TestMsgCreateAtomicSwap() {
amount := cs(c("bnb", int64(10000)))
timestamp := ts(0)
randomNumber, _ := types.GenerateSecureRandomNumber()
randomNumberHash := types.CalculateRandomHash(randomNumber[:], timestamp)
msg := types.NewMsgCreateAtomicSwap(
suite.addrs[0].String(), suite.addrs[2].String(), TestRecipientOtherChain,
TestSenderOtherChain, randomNumberHash, timestamp, amount,
types.DefaultMinBlockLock)
res, err := suite.msgServer.CreateAtomicSwap(sdk.WrapSDKContext(suite.ctx), &msg)
suite.Require().NoError(err)
suite.Require().NotNil(res)
}
func (suite *MsgServerTestSuite) TestMsgClaimAtomicSwap() {
// Attempt claim msg on fake atomic swap
badRandomNumber, _ := types.GenerateSecureRandomNumber()
badRandomNumberHash := types.CalculateRandomHash(badRandomNumber[:], ts(0))
badSwapID := types.CalculateSwapID(badRandomNumberHash, suite.addrs[0], TestSenderOtherChain)
badMsg := types.NewMsgClaimAtomicSwap(suite.addrs[0].String(), badSwapID, badRandomNumber[:])
badRes, err := suite.msgServer.ClaimAtomicSwap(sdk.WrapSDKContext(suite.ctx), &badMsg)
suite.Require().Error(err)
suite.Require().Nil(badRes)
// Add an atomic swap before attempting new claim msg
swapID, randomNumber := suite.AddAtomicSwap()
msg := types.NewMsgClaimAtomicSwap(suite.addrs[0].String(), swapID, randomNumber)
res, err := suite.msgServer.ClaimAtomicSwap(sdk.WrapSDKContext(suite.ctx), &msg)
suite.Require().NoError(err)
suite.Require().NotNil(res)
}
func (suite *MsgServerTestSuite) TestMsgRefundAtomicSwap() {
// Attempt refund msg on fake atomic swap
badRandomNumber, _ := types.GenerateSecureRandomNumber()
badRandomNumberHash := types.CalculateRandomHash(badRandomNumber[:], ts(0))
badSwapID := types.CalculateSwapID(badRandomNumberHash, suite.addrs[0], TestSenderOtherChain)
badMsg := types.NewMsgRefundAtomicSwap(suite.addrs[0].String(), badSwapID)
badRes, err := suite.msgServer.RefundAtomicSwap(sdk.WrapSDKContext(suite.ctx), &badMsg)
suite.Require().Error(err)
suite.Require().Nil(badRes)
// Add an atomic swap and build refund msg
swapID, _ := suite.AddAtomicSwap()
msg := types.NewMsgRefundAtomicSwap(suite.addrs[0].String(), swapID)
// Attempt to refund active atomic swap
res1, err := suite.msgServer.RefundAtomicSwap(sdk.WrapSDKContext(suite.ctx), &msg)
suite.Require().Error(err)
suite.Require().Nil(res1)
// Expire the atomic swap with begin blocker and attempt refund
laterCtx := suite.ctx.WithBlockHeight(suite.ctx.BlockHeight() + 400)
bep3.BeginBlocker(laterCtx, suite.keeper)
res2, err := suite.msgServer.RefundAtomicSwap(sdk.WrapSDKContext(laterCtx), &msg)
suite.Require().NoError(err)
suite.Require().NotNil(res2)
}
func TestMsgServerTestSuite(t *testing.T) {
suite.Run(t, new(MsgServerTestSuite))
}