Skip to content

Commit 193e414

Browse files
authored
Add fee middleware test suite functions (E2E #5) (cosmos#1710)
1 parent c7be122 commit 193e414

2 files changed

Lines changed: 122 additions & 2 deletions

File tree

e2e/fee_middleware_test.go

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,16 @@ import (
44
"context"
55
"testing"
66

7+
sdk "github.com/cosmos/cosmos-sdk/types"
8+
"github.com/strangelove-ventures/ibctest/broadcast"
9+
"github.com/strangelove-ventures/ibctest/chain/cosmos"
710
"github.com/strangelove-ventures/ibctest/ibc"
811
"github.com/stretchr/testify/suite"
912

1013
"e2e/testsuite"
14+
15+
feetypes "github.com/cosmos/ibc-go/v4/modules/apps/29-fee/types"
16+
channeltypes "github.com/cosmos/ibc-go/v4/modules/core/04-channel/types"
1117
)
1218

1319
func TestFeeMiddlewareTestSuite(t *testing.T) {
@@ -18,13 +24,63 @@ type FeeMiddlewareTestSuite struct {
1824
testsuite.E2ETestSuite
1925
}
2026

27+
// RegisterCounterPartyPayee broadcasts a MsgRegisterCounterpartyPayee message.
28+
func (s *FeeMiddlewareTestSuite) RegisterCounterPartyPayee(ctx context.Context, chain *cosmos.CosmosChain,
29+
user broadcast.User, portID, channelID, relayerAddr, counterpartyPayeeAddr string) (sdk.TxResponse, error) {
30+
msg := feetypes.NewMsgRegisterCounterpartyPayee(portID, channelID, relayerAddr, counterpartyPayeeAddr)
31+
return s.BroadcastMessages(ctx, chain, user, msg)
32+
}
33+
34+
// QueryCounterPartyPayee queries the counterparty payee of the given chain and relayer address on the specified channel.
35+
func (s *FeeMiddlewareTestSuite) QueryCounterPartyPayee(ctx context.Context, chain ibc.Chain, relayerAddress, channelID string) (string, error) {
36+
queryClient := s.GetChainGRCPClients(chain).FeeQueryClient
37+
res, err := queryClient.CounterpartyPayee(ctx, &feetypes.QueryCounterpartyPayeeRequest{
38+
ChannelId: channelID,
39+
Relayer: relayerAddress,
40+
})
41+
42+
if err != nil {
43+
return "", err
44+
}
45+
return res.CounterpartyPayee, nil
46+
}
47+
48+
// PayPacketFeeAsync broadcasts a MsgPayPacketFeeAsync message.
49+
func (s *FeeMiddlewareTestSuite) PayPacketFeeAsync(
50+
ctx context.Context,
51+
chain *cosmos.CosmosChain,
52+
user broadcast.User,
53+
packetID channeltypes.PacketId,
54+
packetFee feetypes.PacketFee,
55+
) (sdk.TxResponse, error) {
56+
msg := feetypes.NewMsgPayPacketFeeAsync(packetID, packetFee)
57+
return s.BroadcastMessages(ctx, chain, user, msg)
58+
}
59+
60+
// QueryIncentivizedPacketsForChannel queries the incentivized packets on the specified channel.
61+
func (s *FeeMiddlewareTestSuite) QueryIncentivizedPacketsForChannel(
62+
ctx context.Context,
63+
chain *cosmos.CosmosChain,
64+
portId,
65+
channelId string,
66+
) ([]*feetypes.IdentifiedPacketFees, error) {
67+
queryClient := s.GetChainGRCPClients(chain).FeeQueryClient
68+
res, err := queryClient.IncentivizedPacketsForChannel(ctx, &feetypes.QueryIncentivizedPacketsForChannelRequest{
69+
PortId: portId,
70+
ChannelId: channelId,
71+
})
72+
if err != nil {
73+
return nil, err
74+
}
75+
return res.IncentivizedPackets, err
76+
}
77+
2178
func (s *FeeMiddlewareTestSuite) TestPlaceholder() {
2279
ctx := context.Background()
2380
r := s.SetupChainsRelayerAndChannel(ctx, feeMiddlewareChannelOptions())
2481
s.T().Run("start relayer", func(t *testing.T) {
2582
s.StartRelayer(r)
2683
})
27-
2884
}
2985

3086
// feeMiddlewareChannelOptions configures both of the chains to have fee middleware enabled.

e2e/testsuite/testsuite.go

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,23 @@ import (
77
"strings"
88
"time"
99

10+
"e2e/testconfig"
11+
12+
sdk "github.com/cosmos/cosmos-sdk/types"
1013
dockerclient "github.com/docker/docker/client"
1114
"github.com/strangelove-ventures/ibctest"
15+
"github.com/strangelove-ventures/ibctest/broadcast"
1216
"github.com/strangelove-ventures/ibctest/chain/cosmos"
1317
"github.com/strangelove-ventures/ibctest/ibc"
18+
"github.com/strangelove-ventures/ibctest/test"
1419
"github.com/strangelove-ventures/ibctest/testreporter"
1520
"github.com/stretchr/testify/suite"
1621
"go.uber.org/zap"
1722
"go.uber.org/zap/zaptest"
23+
"google.golang.org/grpc"
24+
"google.golang.org/grpc/credentials/insecure"
1825

19-
"e2e/testconfig"
26+
feetypes "github.com/cosmos/ibc-go/v4/modules/apps/29-fee/types"
2027
)
2128

2229
const (
@@ -29,13 +36,22 @@ const (
2936
// E2ETestSuite has methods and functionality which can be shared among all test suites.
3037
type E2ETestSuite struct {
3138
suite.Suite
39+
40+
grpcClients map[string]GRPCClients
3241
paths map[string]path
3342
logger *zap.Logger
3443
DockerClient *dockerclient.Client
3544
network string
3645
startRelayerFn func(relayer ibc.Relayer)
3746
}
3847

48+
// GRPCClients holds a reference to any GRPC clients that are needed by the tests.
49+
// These should typically be used for query clients only. If we need to make changes, we should
50+
// use E2ETestSuite.BroadcastMessages to broadcast transactions instead.
51+
type GRPCClients struct {
52+
FeeQueryClient feetypes.QueryClient
53+
}
54+
3955
// path is a pairing of two chains which will be used in a test.
4056
type path struct {
4157
chainA, chainB *cosmos.CosmosChain
@@ -102,6 +118,9 @@ func (s *E2ETestSuite) SetupChainsRelayerAndChannel(ctx context.Context, channel
102118
time.Sleep(time.Second * 10)
103119
}
104120

121+
s.initGRPCClients(chainA)
122+
s.initGRPCClients(chainB)
123+
105124
return r
106125
}
107126

@@ -129,6 +148,20 @@ func (s *E2ETestSuite) GetChains(chainOpts ...testconfig.ChainOptionConfiguratio
129148
return path.chainA, path.chainB
130149
}
131150

151+
// BroadcastMessages broadcasts the provided messages to the given chain and signs them on behalf of the provided user.
152+
// Once the broadcast response is returned, we wait for a few blocks to be created on both chain A and chain B.
153+
func (s *E2ETestSuite) BroadcastMessages(ctx context.Context, chain *cosmos.CosmosChain, user broadcast.User, msgs ...sdk.Msg) (sdk.TxResponse, error) {
154+
broadcaster := cosmos.NewBroadcaster(s.T(), chain)
155+
resp, err := ibctest.BroadcastTx(ctx, broadcaster, user, msgs...)
156+
if err != nil {
157+
return sdk.TxResponse{}, err
158+
}
159+
160+
chainA, chainB := s.GetChains()
161+
err = test.WaitForBlocks(ctx, 2, chainA, chainB)
162+
return resp, err
163+
}
164+
132165
// GetRelayerWallets returns the relayer wallets associated with the chains.
133166
func (s *E2ETestSuite) GetRelayerWallets(relayer ibc.Relayer) (ibc.RelayerWallet, ibc.RelayerWallet, error) {
134167
chainA, chainB := s.GetChains()
@@ -196,6 +229,37 @@ func (s *E2ETestSuite) GetChainBNativeBalance(ctx context.Context, user *ibctest
196229
return GetNativeChainBalance(ctx, chainB, user)
197230
}
198231

232+
// GetChainGRCPClients gets the GRPC clients associated with the given chain.
233+
func (s *E2ETestSuite) GetChainGRCPClients(chain ibc.Chain) GRPCClients {
234+
cs, ok := s.grpcClients[chain.Config().ChainID]
235+
s.Require().True(ok, "chain %s does not have GRPC clients", chain.Config().ChainID)
236+
return cs
237+
}
238+
239+
// initGRPCClients establishes GRPC clients with the given chain.
240+
// The created GRPCClients can be retrieved with GetChainGRCPClients.
241+
func (s *E2ETestSuite) initGRPCClients(chain *cosmos.CosmosChain) {
242+
// Create a connection to the gRPC server.
243+
grpcConn, err := grpc.Dial(
244+
chain.GetHostGRPCAddress(),
245+
grpc.WithTransportCredentials(insecure.NewCredentials()),
246+
)
247+
s.Require().NoError(err)
248+
s.T().Cleanup(func() {
249+
if err := grpcConn.Close(); err != nil {
250+
s.T().Logf("failed closing GRPC connection to chain %s: %s", chain.Config().ChainID, err)
251+
}
252+
})
253+
254+
if s.grpcClients == nil {
255+
s.grpcClients = make(map[string]GRPCClients)
256+
}
257+
258+
s.grpcClients[chain.Config().ChainID] = GRPCClients{
259+
FeeQueryClient: feetypes.NewQueryClient(grpcConn),
260+
}
261+
}
262+
199263
// createCosmosChains creates two separate chains in docker containers.
200264
// test and can be retrieved with GetChains.
201265
func (s *E2ETestSuite) createCosmosChains(chainOptions testconfig.ChainOptions) (*cosmos.CosmosChain, *cosmos.CosmosChain) {

0 commit comments

Comments
 (0)