Skip to content

Commit

Permalink
test: use customibctesting package and adding test cases for gRPC que…
Browse files Browse the repository at this point in the history
…ries
  • Loading branch information
jaybxyz committed Oct 2, 2023
1 parent 4a6728f commit aca48ae
Show file tree
Hide file tree
Showing 3 changed files with 275 additions and 21 deletions.
7 changes: 7 additions & 0 deletions app/ibctesting/path.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ibctesting

import (
channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"
ibctesting "github.com/cosmos/ibc-go/v7/testing"
)

Expand All @@ -26,6 +27,12 @@ func NewPath(chainA, chainB *TestChain) *Path {
}
}

// SetChannelOrdered sets the channel order for both endpoints to ORDERED.
func (path *Path) SetChannelOrdered() {
path.EndpointA.ChannelConfig.Order = channeltypes.ORDERED
path.EndpointB.ChannelConfig.Order = channeltypes.ORDERED
}

// NewDefaultEndpoint constructs a new endpoint using default values.
// CONTRACT: the counterparty endpoitn must be set by the caller.
func NewDefaultEndpoint(chain *TestChain) *Endpoint {
Expand Down
279 changes: 262 additions & 17 deletions x/ratelimit/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
package keeper_test

import (
sdkmath "cosmossdk.io/math"
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"

// ibctesting "github.com/cosmos/ibc-go/v7/testing"

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

func (s *KeeperTestSuite) TestGRPCAllRateLimits() {
var (
denom = sdk.DefaultBondDenom // Should use DefaultBondDenom; otherwise it returns channel value error
channelID = "channel-0"
maxPercentSend = sdkmath.NewInt(50)
maxPercentRecv = sdkmath.NewInt(50)
minRateLimitAmount = sdkmath.NewInt(100000)
durationHours = uint64(1)
)
s.addRateLimit(denom, channelID, maxPercentSend, maxPercentRecv, minRateLimitAmount, durationHours)
// Add some sample rate limits
s.SetupSampleRateLimits(sampleRateLimitA, sampleRateLimitB, sampleRateLimitC, sampleRateLimitD)

for _, tc := range []struct {
name string
Expand All @@ -35,13 +32,13 @@ func (s *KeeperTestSuite) TestGRPCAllRateLimits() {
&types.QueryAllRateLimitsRequest{},
false,
func(resp *types.QueryAllRateLimitsResponse) {
s.Require().Len(resp.GetRateLimits(), 1)
s.Require().Equal(denom, resp.RateLimits[0].Path.Denom)
s.Require().Equal(channelID, resp.RateLimits[0].Path.ChannelID)
s.Require().Equal(maxPercentSend, resp.RateLimits[0].Quota.MaxPercentSend)
s.Require().Equal(maxPercentRecv, resp.RateLimits[0].Quota.MaxPercentRecv)
s.Require().Equal(minRateLimitAmount, resp.RateLimits[0].MinRateLimitAmount)
s.Require().Equal(durationHours, resp.RateLimits[0].Quota.DurationHours)
s.Require().Len(resp.GetRateLimits(), 4)
s.Require().Equal(sampleRateLimitA.Denom, resp.RateLimits[0].Path.Denom)
s.Require().Equal(sampleRateLimitA.ChannelID, resp.RateLimits[0].Path.ChannelID)
s.Require().Equal(sampleRateLimitA.MaxPercentSend, resp.RateLimits[0].Quota.MaxPercentSend)
s.Require().Equal(sampleRateLimitA.MaxPercentRecv, resp.RateLimits[0].Quota.MaxPercentRecv)
s.Require().Equal(sampleRateLimitA.MinRateLimitAmount, resp.RateLimits[0].MinRateLimitAmount)
s.Require().Equal(sampleRateLimitA.DurationHours, resp.RateLimits[0].Quota.DurationHours)
},
},
} {
Expand All @@ -56,3 +53,251 @@ func (s *KeeperTestSuite) TestGRPCAllRateLimits() {
})
}
}

func (s *KeeperTestSuite) TestGRPCRateLimit() {
// Add some sample rate limits
s.SetupSampleRateLimits(sampleRateLimitA, sampleRateLimitB, sampleRateLimitC, sampleRateLimitD)

for _, tc := range []struct {
name string
req *types.QueryRateLimitRequest
expectErr bool
postRun func(*types.QueryRateLimitResponse)
}{
{
"nil request",
nil,
true,
nil,
},
{
"happy case",
&types.QueryRateLimitRequest{
Denom: sampleRateLimitA.Denom,
ChannelID: sampleRateLimitA.ChannelID,
},
false,
func(resp *types.QueryRateLimitResponse) {
s.Require().Equal(sampleRateLimitA.Denom, resp.RateLimit.Path.Denom)
s.Require().Equal(sampleRateLimitA.ChannelID, resp.RateLimit.Path.ChannelID)
s.Require().Equal(sampleRateLimitA.MaxPercentSend, resp.RateLimit.Quota.MaxPercentSend)
s.Require().Equal(sampleRateLimitA.MaxPercentRecv, resp.RateLimit.Quota.MaxPercentRecv)
s.Require().Equal(sampleRateLimitA.MinRateLimitAmount, resp.RateLimit.MinRateLimitAmount)
s.Require().Equal(sampleRateLimitA.DurationHours, resp.RateLimit.Quota.DurationHours)
},
},
{
"query by invalid denom",
&types.QueryRateLimitRequest{
Denom: "invalidDenom",
ChannelID: sampleRateLimitA.ChannelID,
},
true,
nil,
},
{
"query by invalid channel id",
&types.QueryRateLimitRequest{
Denom: sampleRateLimitA.Denom,
ChannelID: "invalidChannelID",
},
true,
nil,
},
} {
s.Run(tc.name, func() {
resp, err := s.querier.RateLimit(sdk.WrapSDKContext(s.ctx), tc.req)
if tc.expectErr {
s.Require().Error(err)
} else {
s.Require().NoError(err)
tc.postRun(resp)
}
})
}
}

func (s *KeeperTestSuite) TestRateLimitsByChainID() {
// Add some sample rate limits
s.SetupSampleRateLimits(sampleRateLimitA, sampleRateLimitB)

// // Create client and connections on both chains
// path := ibctesting.NewPath(s.chainA, s.chainB)
// s.coordinator.SetupConnections(path)
// path.SetChannelOrdered()

// // Initialize channel
// err := path.EndpointA.ChanOpenInit()
// s.Require().NoError(err)

for _, tc := range []struct {
name string
req *types.QueryRateLimitsByChainIDRequest
expectErr bool
postRun func(*types.QueryRateLimitsByChainIDResponse)
}{
{
"nil request",
nil,
true,
nil,
},
{
"happy case",
&types.QueryRateLimitsByChainIDRequest{
ChainId: s.chainA.ChainID,
},
false,
func(resp *types.QueryRateLimitsByChainIDResponse) {
fmt.Println("resp: ", resp)
// s.Require().Len(resp.GetRateLimits(), 2)
},
},
} {
s.Run(tc.name, func() {
ctx := sdk.WrapSDKContext(s.chainA.GetContext())

resp, err := s.querier.RateLimitsByChainID(ctx, tc.req)
if tc.expectErr {
s.Require().Error(err)
} else {
s.Require().NoError(err)
tc.postRun(resp)
}
})
}
}

func (s *KeeperTestSuite) TestRateLimitsByChannelID() {
// Add some sample rate limits
s.SetupSampleRateLimits(sampleRateLimitA, sampleRateLimitB, sampleRateLimitC, sampleRateLimitD)

for _, tc := range []struct {
name string
req *types.QueryRateLimitsByChannelIDRequest
expectErr bool
postRun func(*types.QueryRateLimitsByChannelIDResponse)
}{
{
"nil request",
nil,
true,
nil,
},
{
"happy case",
&types.QueryRateLimitsByChannelIDRequest{
ChannelID: sampleRateLimitA.ChannelID,
},
false,
func(resp *types.QueryRateLimitsByChannelIDResponse) {
s.Require().Len(resp.GetRateLimits(), 2)
s.Require().Equal(sampleRateLimitA.Denom, resp.RateLimits[0].Path.Denom)
s.Require().Equal(sampleRateLimitA.ChannelID, resp.RateLimits[0].Path.ChannelID)
s.Require().Equal(sampleRateLimitA.MaxPercentSend, resp.RateLimits[0].Quota.MaxPercentSend)
s.Require().Equal(sampleRateLimitA.MaxPercentRecv, resp.RateLimits[0].Quota.MaxPercentRecv)
s.Require().Equal(sampleRateLimitA.MinRateLimitAmount, resp.RateLimits[0].MinRateLimitAmount)
s.Require().Equal(sampleRateLimitA.DurationHours, resp.RateLimits[0].Quota.DurationHours)
s.Require().Equal(sampleRateLimitB.Denom, resp.RateLimits[1].Path.Denom)
s.Require().Equal(sampleRateLimitB.ChannelID, resp.RateLimits[1].Path.ChannelID)
s.Require().Equal(sampleRateLimitB.MaxPercentSend, resp.RateLimits[1].Quota.MaxPercentSend)
s.Require().Equal(sampleRateLimitB.MaxPercentRecv, resp.RateLimits[1].Quota.MaxPercentRecv)
s.Require().Equal(sampleRateLimitB.MinRateLimitAmount, resp.RateLimits[1].MinRateLimitAmount)
s.Require().Equal(sampleRateLimitB.DurationHours, resp.RateLimits[1].Quota.DurationHours)
},
},
{
"query by chain id that does not exist",
&types.QueryRateLimitsByChannelIDRequest{
ChannelID: "channel-10",
},
false,
func(resp *types.QueryRateLimitsByChannelIDResponse) {
s.Require().Empty(resp.RateLimits)
},
},
{
"query by invalid chain id",
&types.QueryRateLimitsByChannelIDRequest{
ChannelID: "invalid/ChannelID",
},
true,
nil,
},
} {
s.Run(tc.name, func() {
resp, err := s.querier.RateLimitsByChannelID(sdk.WrapSDKContext(s.ctx), tc.req)
if tc.expectErr {
s.Require().Error(err)
} else {
s.Require().NoError(err)
tc.postRun(resp)
}
})
}
}

func (s *KeeperTestSuite) TestAllWhitelistedAddresses() {
// Add some sample whitelisted addresses
whitelistedAddrPairs := []types.WhitelistedAddressPair{
{Sender: s.addr(1).String(), Receiver: s.addr(2).String()},
{Sender: s.addr(3).String(), Receiver: s.addr(4).String()},
{Sender: s.addr(5).String(), Receiver: s.addr(6).String()},
}

for _, wap := range whitelistedAddrPairs {
s.keeper.SetWhitelistedAddressPair(s.ctx, wap)
}

for _, tc := range []struct {
name string
req *types.QueryAllWhitelistedAddressesRequest
expectErr bool
postRun func(*types.QueryAllWhitelistedAddressesResponse)
}{
{
"nil request",
nil,
true,
nil,
},
{
"happy case",
&types.QueryAllWhitelistedAddressesRequest{},
false,
func(resp *types.QueryAllWhitelistedAddressesResponse) {
s.Require().Len(resp.GetAddressPairs(), 3)
},
},
} {
s.Run(tc.name, func() {
resp, err := s.querier.AllWhitelistedAddresses(sdk.WrapSDKContext(s.ctx), tc.req)
if tc.expectErr {
s.Require().Error(err)
} else {
s.Require().NoError(err)
tc.postRun(resp)
}
})
}
}

func (s *KeeperTestSuite) TestChanOpenInit() {
// Create client and connections on both chains
path := ibctesting.NewPath(s.chainA, s.chainB)
s.coordinator.SetupConnections(path)

path.SetChannelOrdered()

// Initialize channel
//
// Works well with ibctesting "github.com/cosmos/ibc-go/v7/testing" but it does not working due to the following error message with customibctesting
// Error: could not retrieve module from port-id: ports/mock: capability not found
//
err := path.EndpointA.ChanOpenInit()
s.Require().NoError(err)

storedChannel, found := s.chainA.App.GetIBCKeeper().ChannelKeeper.GetChannel(s.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)
s.True(found)
fmt.Println("storedChannel: ", storedChannel)
}
10 changes: 6 additions & 4 deletions x/ratelimit/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import (
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"

// ibctesting "github.com/cosmos/ibc-go/v7/testing"

"github.com/notional-labs/centauri/v5/app"
"github.com/notional-labs/centauri/v5/app/helpers"
ibctesting "github.com/notional-labs/centauri/v5/app/ibctesting"
"github.com/notional-labs/centauri/v5/x/ratelimit/keeper"
"github.com/notional-labs/centauri/v5/x/ratelimit/types"
)
Expand Down Expand Up @@ -87,8 +89,8 @@ func (s *KeeperTestSuite) SetupTest() {

// 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))
s.chainA = s.coordinator.GetChain(ibctesting.GetChainID(0))
s.chainB = s.coordinator.GetChain(ibctesting.GetChainID(1))

// Commit some blocks so that QueryProof returns valid proof (cannot return valid query if height <= 1)
s.coordinator.CommitNBlocks(s.chainA, 2)
Expand Down Expand Up @@ -137,7 +139,7 @@ func (s *KeeperTestSuite) addRateLimit(
) {
s.T().Helper()

// Add new RateLimit requires total supply of the given denom
// Increase total supply since adding new rate limit 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{
Expand Down

0 comments on commit aca48ae

Please sign in to comment.