Skip to content

Commit

Permalink
test: add ibctesting in KeeperTestSuite
Browse files Browse the repository at this point in the history
  • Loading branch information
jaybxyz committed Oct 1, 2023
1 parent 9c21e1d commit 4a6728f
Showing 1 changed file with 88 additions and 2 deletions.
90 changes: 88 additions & 2 deletions x/ratelimit/keeper/keeper_test.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,59 @@
package keeper_test

import (
"encoding/binary"
"testing"
"time"

"github.com/stretchr/testify/suite"

sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"

tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
sdk "github.com/cosmos/cosmos-sdk/types"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
ibctesting "github.com/cosmos/ibc-go/v7/testing"

"github.com/notional-labs/centauri/v5/app"
"github.com/notional-labs/centauri/v5/app/helpers"
"github.com/notional-labs/centauri/v5/x/ratelimit/keeper"
"github.com/notional-labs/centauri/v5/x/ratelimit/types"
)

var (
sampleRateLimitA = types.MsgAddRateLimit{

Check failure on line 23 in x/ratelimit/keeper/keeper_test.go

View workflow job for this annotation

GitHub Actions / lint

var `sampleRateLimitA` is unused (unused)
Denom: "denomA",
ChannelID: "channel-0",
MaxPercentSend: sdkmath.NewInt(10),
MaxPercentRecv: sdkmath.NewInt(10),
MinRateLimitAmount: sdkmath.NewInt(1_000_000),
DurationHours: uint64(1),
}
sampleRateLimitB = types.MsgAddRateLimit{

Check failure on line 31 in x/ratelimit/keeper/keeper_test.go

View workflow job for this annotation

GitHub Actions / lint

var `sampleRateLimitB` is unused (unused)
Denom: "denomB",
ChannelID: "channel-0",
MaxPercentSend: sdkmath.NewInt(20),
MaxPercentRecv: sdkmath.NewInt(20),
MinRateLimitAmount: sdkmath.NewInt(1_000_000),
DurationHours: uint64(1),
}
sampleRateLimitC = types.MsgAddRateLimit{

Check failure on line 39 in x/ratelimit/keeper/keeper_test.go

View workflow job for this annotation

GitHub Actions / lint

var `sampleRateLimitC` is unused (unused)
Denom: "denomB",
ChannelID: "channel-1",
MaxPercentSend: sdkmath.NewInt(50),
MaxPercentRecv: sdkmath.NewInt(50),
MinRateLimitAmount: sdkmath.NewInt(5_000_000),
DurationHours: uint64(5),
}
sampleRateLimitD = types.MsgAddRateLimit{

Check failure on line 47 in x/ratelimit/keeper/keeper_test.go

View workflow job for this annotation

GitHub Actions / lint

var `sampleRateLimitD` is unused (unused)
Denom: "denomC",
ChannelID: "channel-2",
MaxPercentSend: sdkmath.NewInt(80),
MaxPercentRecv: sdkmath.NewInt(80),
MinRateLimitAmount: sdkmath.NewInt(10_000_000),
DurationHours: uint64(10),
}
)

type KeeperTestSuite struct {
suite.Suite

Expand All @@ -25,6 +62,12 @@ type KeeperTestSuite struct {
keeper keeper.Keeper
querier types.QueryServer
msgServer types.MsgServer

coordinator *ibctesting.Coordinator

// testing chains used for convenience and readability
chainA *ibctesting.TestChain
chainB *ibctesting.TestChain
}

func TestKeeperTestSuite(t *testing.T) {
Expand All @@ -41,6 +84,46 @@ func (s *KeeperTestSuite) SetupTest() {
s.keeper = s.app.RatelimitKeeper
s.querier = keeper.NewQueryServer(s.keeper)
s.msgServer = keeper.NewMsgServerImpl(s.keeper)

// Creates a coordinator with 2 test chains
s.coordinator = ibctesting.NewCoordinator(s.T(), 2)
s.chainA = s.coordinator.GetChain(ibctesting.GetChainID(1))
s.chainB = s.coordinator.GetChain(ibctesting.GetChainID(2))

// Commit some blocks so that QueryProof returns valid proof (cannot return valid query if height <= 1)
s.coordinator.CommitNBlocks(s.chainA, 2)
s.coordinator.CommitNBlocks(s.chainB, 2)
}

func (s *KeeperTestSuite) SetupSampleRateLimits(rateLimits ...types.MsgAddRateLimit) {
for _, rateLimit := range rateLimits {
s.addRateLimit(
rateLimit.Denom,
rateLimit.ChannelID,
rateLimit.MaxPercentSend,
rateLimit.MaxPercentRecv,
rateLimit.MinRateLimitAmount,
rateLimit.DurationHours,
)
}
}

//
// Below are helper functions to write test code easily
//

func (s *KeeperTestSuite) addr(addrNum int) sdk.AccAddress {
addr := make(sdk.AccAddress, 20)
binary.PutVarint(addr, int64(addrNum))
return addr
}

func (s *KeeperTestSuite) fundAddr(addr sdk.AccAddress, amt sdk.Coins) {
s.T().Helper()
err := s.app.BankKeeper.MintCoins(s.ctx, minttypes.ModuleName, amt)
s.Require().NoError(err)
err = s.app.BankKeeper.SendCoinsFromModuleToAccount(s.ctx, minttypes.ModuleName, addr, amt)
s.Require().NoError(err)
}

// addRateLimit is a convenient method to add new RateLimit without the need of authority.
Expand All @@ -54,6 +137,9 @@ func (s *KeeperTestSuite) addRateLimit(
) {
s.T().Helper()

// Add new RateLimit requires total supply of the given denom
s.fundAddr(s.addr(0), sdk.NewCoins(sdk.NewCoin(denom, sdk.NewInt(100_000_000_000))))

err := s.keeper.AddRateLimit(s.ctx, &types.MsgAddRateLimit{
Authority: "",
Denom: denom,
Expand Down

0 comments on commit 4a6728f

Please sign in to comment.