Skip to content

Commit 720d1cd

Browse files
committed
move gRPC conn on node startup
1 parent 2fb0a4f commit 720d1cd

File tree

8 files changed

+97
-398
lines changed

8 files changed

+97
-398
lines changed

chain/cosmos/authz.go

Lines changed: 18 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,10 @@ import (
66
"strings"
77

88
sdk "github.com/cosmos/cosmos-sdk/types"
9+
"github.com/cosmos/cosmos-sdk/x/authz"
910
"github.com/strangelove-ventures/interchaintest/v8/ibc"
1011
)
1112

12-
// TODO: Convert to SDK v50.
13-
14-
// Available Commands:
15-
// exec execute tx on behalf of granter account
16-
// grant Grant authorization to an address
17-
// revoke revoke authorization
18-
1913
const ()
2014

2115
// AuthzGrant grants a message as a permission to an account.
@@ -94,60 +88,27 @@ func AuthzRevoke(ctx context.Context, chain *CosmosChain, granter ibc.Wallet, gr
9488
return chain.GetNode().TxHashToResponse(ctx, txHash)
9589
}
9690

97-
// authz.QueryGrantsResponse
98-
type QueryAuthzGrantsResponse struct {
99-
Grants []struct {
100-
Authorization struct {
101-
Type string `json:"type"`
102-
Value struct {
103-
Msg string `json:"msg"`
104-
} `json:"value"`
105-
} `json:"authorization"`
106-
} `json:"grants"`
107-
Pagination struct {
108-
Total string `json:"total"`
109-
} `json:"pagination"`
110-
}
111-
112-
// authz.QueryGranteeGrantsResponse & QueryGranterGrantsResponse
113-
type QueryAuthzGrantsByResponse struct {
114-
Grants []struct {
115-
Granter string `json:"granter"`
116-
Grantee string `json:"grantee"`
117-
Authorization struct {
118-
Type string `json:"type"`
119-
Value struct {
120-
Msg string `json:"msg"`
121-
} `json:"value"`
122-
} `json:"authorization"`
123-
} `json:"grants"`
124-
Pagination struct {
125-
Total string `json:"total"`
126-
} `json:"pagination"`
127-
}
128-
129-
func AuthzQueryGrants(ctx context.Context, chain *CosmosChain, granter string, grantee string, msgType string, extraFlags ...string) (*QueryAuthzGrantsResponse, error) {
130-
cmd := []string{"authz", "grants", granter, grantee, msgType}
131-
cmd = append(cmd, extraFlags...)
132-
133-
var res QueryAuthzGrantsResponse
134-
return &res, chain.ExecQueryToResponse(ctx, chain, cmd, &res)
91+
func (c *CosmosChain) AuthzQueryGrants(ctx context.Context, granter string, grantee string, msgType string, extraFlags ...string) ([]*authz.Grant, error) {
92+
res, err := authz.NewQueryClient(c.GetNode().GrpcConn).Grants(ctx, &authz.QueryGrantsRequest{
93+
Granter: granter,
94+
Grantee: grantee,
95+
MsgTypeUrl: msgType,
96+
})
97+
return res.Grants, err
13598
}
13699

137-
func AuthzQueryGrantsByGrantee(ctx context.Context, chain *CosmosChain, grantee string, extraFlags ...string) (*QueryAuthzGrantsByResponse, error) {
138-
cmd := []string{"authz", "grants-by-grantee", grantee}
139-
cmd = append(cmd, extraFlags...)
140-
141-
var res QueryAuthzGrantsByResponse
142-
return &res, chain.ExecQueryToResponse(ctx, chain, cmd, &res)
100+
func (c *CosmosChain) AuthzQueryGrantsByGrantee(ctx context.Context, grantee string, extraFlags ...string) ([]*authz.GrantAuthorization, error) {
101+
res, err := authz.NewQueryClient(c.GetNode().GrpcConn).GranteeGrants(ctx, &authz.QueryGranteeGrantsRequest{
102+
Grantee: grantee,
103+
})
104+
return res.Grants, err
143105
}
144106

145-
func AuthzQueryGrantsByGranter(ctx context.Context, chain *CosmosChain, granter string, extraFlags ...string) (*QueryAuthzGrantsByResponse, error) {
146-
cmd := []string{"authz", "grants-by-granter", granter}
147-
cmd = append(cmd, extraFlags...)
148-
149-
var res QueryAuthzGrantsByResponse
150-
return &res, chain.ExecQueryToResponse(ctx, chain, cmd, &res)
107+
func (c *CosmosChain) AuthzQueryGrantsByGranter(ctx context.Context, granter string, extraFlags ...string) ([]*authz.GrantAuthorization, error) {
108+
res, err := authz.NewQueryClient(c.GetNode().GrpcConn).GranterGrants(ctx, &authz.QueryGranterGrantsRequest{
109+
Granter: granter,
110+
})
111+
return res.Grants, err
151112
}
152113

153114
// createAuthzJSON creates a JSON file with a single generated message.

chain/cosmos/bank.go

Lines changed: 13 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ import (
44
"context"
55
"fmt"
66

7-
"cosmossdk.io/math"
87
sdkmath "cosmossdk.io/math"
9-
"google.golang.org/grpc"
10-
"google.golang.org/grpc/credentials/insecure"
118

129
"github.com/cosmos/cosmos-sdk/types"
1310
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
@@ -25,7 +22,7 @@ func (tn *ChainNode) BankSend(ctx context.Context, keyName string, amount ibc.Wa
2522
}
2623

2724
// BankMultiSend sends an amount of token from one account to multiple accounts.
28-
func (tn *ChainNode) BankMultiSend(ctx context.Context, keyName string, addresses []string, amount math.Int, denom string) error {
25+
func (tn *ChainNode) BankMultiSend(ctx context.Context, keyName string, addresses []string, amount sdkmath.Int, denom string) error {
2926
cmd := append([]string{"bank", "multi-send", keyName}, addresses...)
3027
cmd = append(cmd, fmt.Sprintf("%s%s", amount, denom))
3128

@@ -36,15 +33,7 @@ func (tn *ChainNode) BankMultiSend(ctx context.Context, keyName string, addresse
3633
// GetBalance fetches the current balance for a specific account address and denom.
3734
// Implements Chain interface
3835
func (c *CosmosChain) GetBalance(ctx context.Context, address string, denom string) (sdkmath.Int, error) {
39-
grpcConn, err := grpc.Dial(
40-
c.GetNode().hostGRPCPort, grpc.WithTransportCredentials(insecure.NewCredentials()),
41-
)
42-
if err != nil {
43-
return sdkmath.Int{}, err
44-
}
45-
defer grpcConn.Close()
46-
47-
res, err := banktypes.NewQueryClient(grpcConn).Balance(ctx, &banktypes.QueryBalanceRequest{Address: address, Denom: denom})
36+
res, err := banktypes.NewQueryClient(c.GetNode().GrpcConn).Balance(ctx, &banktypes.QueryBalanceRequest{Address: address, Denom: denom})
4837
return res.Balance.Amount, err
4938
}
5039

@@ -55,151 +44,63 @@ func (c *CosmosChain) BankGetBalance(ctx context.Context, address string, denom
5544

5645
// AllBalances fetches an account address's balance for all denoms it holds
5746
func (c *CosmosChain) BankAllBalances(ctx context.Context, address string) (types.Coins, error) {
58-
grpcConn, err := grpc.Dial(
59-
c.GetNode().hostGRPCPort, grpc.WithTransportCredentials(insecure.NewCredentials()),
60-
)
61-
if err != nil {
62-
return nil, err
63-
}
64-
defer grpcConn.Close()
65-
66-
res, err := banktypes.NewQueryClient(grpcConn).AllBalances(ctx, &banktypes.QueryAllBalancesRequest{Address: address})
47+
res, err := banktypes.NewQueryClient(c.GetNode().GrpcConn).AllBalances(ctx, &banktypes.QueryAllBalancesRequest{Address: address})
6748
return res.GetBalances(), err
6849
}
6950

7051
// BankDenomMetadata fetches the metadata of a specific coin denomination
7152
func (c *CosmosChain) BankDenomMetadata(ctx context.Context, denom string) (*banktypes.Metadata, error) {
72-
grpcConn, err := grpc.Dial(
73-
c.GetNode().hostGRPCPort, grpc.WithTransportCredentials(insecure.NewCredentials()),
74-
)
75-
if err != nil {
76-
return nil, err
77-
}
78-
defer grpcConn.Close()
79-
80-
res, err := banktypes.NewQueryClient(grpcConn).DenomMetadata(ctx, &banktypes.QueryDenomMetadataRequest{Denom: denom})
53+
res, err := banktypes.NewQueryClient(c.GetNode().GrpcConn).DenomMetadata(ctx, &banktypes.QueryDenomMetadataRequest{Denom: denom})
8154
return &res.Metadata, err
8255
}
8356

8457
func (c *CosmosChain) BankQueryDenomMetadataByQueryString(ctx context.Context, denom string) (*banktypes.Metadata, error) {
85-
grpcConn, err := grpc.Dial(
86-
c.GetNode().hostGRPCPort, grpc.WithTransportCredentials(insecure.NewCredentials()),
87-
)
88-
if err != nil {
89-
return nil, err
90-
}
91-
defer grpcConn.Close()
92-
93-
res, err := banktypes.NewQueryClient(grpcConn).DenomMetadataByQueryString(ctx, &banktypes.QueryDenomMetadataByQueryStringRequest{Denom: denom})
58+
res, err := banktypes.NewQueryClient(c.GetNode().GrpcConn).DenomMetadataByQueryString(ctx, &banktypes.QueryDenomMetadataByQueryStringRequest{Denom: denom})
9459
return &res.Metadata, err
9560
}
9661

9762
func (c *CosmosChain) BankQueryDenomOwners(ctx context.Context, denom string) ([]*banktypes.DenomOwner, error) {
98-
grpcConn, err := grpc.Dial(
99-
c.GetNode().hostGRPCPort, grpc.WithTransportCredentials(insecure.NewCredentials()),
100-
)
101-
if err != nil {
102-
return nil, err
103-
}
104-
defer grpcConn.Close()
105-
106-
res, err := banktypes.NewQueryClient(grpcConn).DenomOwners(ctx, &banktypes.QueryDenomOwnersRequest{Denom: denom})
63+
res, err := banktypes.NewQueryClient(c.GetNode().GrpcConn).DenomOwners(ctx, &banktypes.QueryDenomOwnersRequest{Denom: denom})
10764
return res.DenomOwners, err
10865
}
10966

11067
func (c *CosmosChain) BankQueryDenomsMetadata(ctx context.Context) ([]banktypes.Metadata, error) {
111-
grpcConn, err := grpc.Dial(
112-
c.GetNode().hostGRPCPort, grpc.WithTransportCredentials(insecure.NewCredentials()),
113-
)
114-
if err != nil {
115-
return nil, err
116-
}
117-
defer grpcConn.Close()
118-
119-
res, err := banktypes.NewQueryClient(grpcConn).DenomsMetadata(ctx, &banktypes.QueryDenomsMetadataRequest{})
68+
res, err := banktypes.NewQueryClient(c.GetNode().GrpcConn).DenomsMetadata(ctx, &banktypes.QueryDenomsMetadataRequest{})
12069
return res.Metadatas, err
12170
}
12271

12372
func (c *CosmosChain) BankQueryParams(ctx context.Context) (*banktypes.Params, error) {
124-
grpcConn, err := grpc.Dial(
125-
c.GetNode().hostGRPCPort, grpc.WithTransportCredentials(insecure.NewCredentials()),
126-
)
127-
if err != nil {
128-
return nil, err
129-
}
130-
defer grpcConn.Close()
131-
132-
res, err := banktypes.NewQueryClient(grpcConn).Params(ctx, &banktypes.QueryParamsRequest{})
73+
res, err := banktypes.NewQueryClient(c.GetNode().GrpcConn).Params(ctx, &banktypes.QueryParamsRequest{})
13374
return &res.Params, err
13475
}
13576

13677
func (c *CosmosChain) BankQuerySendEnabled(ctx context.Context, denoms []string) ([]*banktypes.SendEnabled, error) {
137-
grpcConn, err := grpc.Dial(
138-
c.GetNode().hostGRPCPort, grpc.WithTransportCredentials(insecure.NewCredentials()),
139-
)
140-
if err != nil {
141-
return nil, err
142-
}
143-
defer grpcConn.Close()
144-
145-
res, err := banktypes.NewQueryClient(grpcConn).SendEnabled(ctx, &banktypes.QuerySendEnabledRequest{
78+
res, err := banktypes.NewQueryClient(c.GetNode().GrpcConn).SendEnabled(ctx, &banktypes.QuerySendEnabledRequest{
14679
Denoms: denoms,
14780
})
14881
return res.SendEnabled, err
14982
}
15083

15184
func (c *CosmosChain) BankQuerySpendableBalance(ctx context.Context, address, denom string) (*types.Coin, error) {
152-
grpcConn, err := grpc.Dial(
153-
c.GetNode().hostGRPCPort, grpc.WithTransportCredentials(insecure.NewCredentials()),
154-
)
155-
if err != nil {
156-
return nil, err
157-
}
158-
defer grpcConn.Close()
159-
160-
res, err := banktypes.NewQueryClient(grpcConn).SpendableBalanceByDenom(ctx, &banktypes.QuerySpendableBalanceByDenomRequest{
85+
res, err := banktypes.NewQueryClient(c.GetNode().GrpcConn).SpendableBalanceByDenom(ctx, &banktypes.QuerySpendableBalanceByDenomRequest{
16186
Address: address,
16287
Denom: denom,
16388
})
16489
return res.Balance, err
16590
}
16691

16792
func (c *CosmosChain) BankQuerySpendableBalances(ctx context.Context, address string) (*types.Coins, error) {
168-
grpcConn, err := grpc.Dial(
169-
c.GetNode().hostGRPCPort, grpc.WithTransportCredentials(insecure.NewCredentials()),
170-
)
171-
if err != nil {
172-
return nil, err
173-
}
174-
defer grpcConn.Close()
175-
176-
res, err := banktypes.NewQueryClient(grpcConn).SpendableBalances(ctx, &banktypes.QuerySpendableBalancesRequest{Address: address})
93+
res, err := banktypes.NewQueryClient(c.GetNode().GrpcConn).SpendableBalances(ctx, &banktypes.QuerySpendableBalancesRequest{Address: address})
17794
return &res.Balances, err
17895
}
17996

18097
func (c *CosmosChain) BankQueryTotalSupply(ctx context.Context) (*types.Coins, error) {
181-
grpcConn, err := grpc.Dial(
182-
c.GetNode().hostGRPCPort, grpc.WithTransportCredentials(insecure.NewCredentials()),
183-
)
184-
if err != nil {
185-
return nil, err
186-
}
187-
defer grpcConn.Close()
188-
189-
res, err := banktypes.NewQueryClient(grpcConn).TotalSupply(ctx, &banktypes.QueryTotalSupplyRequest{})
98+
res, err := banktypes.NewQueryClient(c.GetNode().GrpcConn).TotalSupply(ctx, &banktypes.QueryTotalSupplyRequest{})
19099
return &res.Supply, err
191100
}
192101

193102
func (c *CosmosChain) BankQueryTotalSupplyOf(ctx context.Context, address string) (*types.Coin, error) {
194-
grpcConn, err := grpc.Dial(
195-
c.GetNode().hostGRPCPort, grpc.WithTransportCredentials(insecure.NewCredentials()),
196-
)
197-
if err != nil {
198-
return nil, err
199-
}
200-
defer grpcConn.Close()
201-
202-
res, err := banktypes.NewQueryClient(grpcConn).SupplyOf(ctx, &banktypes.QuerySupplyOfRequest{Denom: address})
103+
res, err := banktypes.NewQueryClient(c.GetNode().GrpcConn).SupplyOf(ctx, &banktypes.QuerySupplyOfRequest{Denom: address})
203104

204105
return &res.Amount, err
205106
}

chain/cosmos/chain_node.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ import (
3535
"github.com/docker/go-connections/nat"
3636
"go.uber.org/zap"
3737
"golang.org/x/sync/errgroup"
38+
"google.golang.org/grpc"
39+
"google.golang.org/grpc/credentials/insecure"
3840

3941
"github.com/strangelove-ventures/interchaintest/v8/ibc"
4042
"github.com/strangelove-ventures/interchaintest/v8/internal/blockdb"
@@ -51,6 +53,7 @@ type ChainNode struct {
5153
NetworkID string
5254
DockerClient *dockerclient.Client
5355
Client rpcclient.Client
56+
GrpcConn *grpc.ClientConn
5457
TestName string
5558
Image ibc.DockerImage
5659

@@ -124,6 +127,15 @@ func (tn *ChainNode) NewClient(addr string) error {
124127
}
125128

126129
tn.Client = rpcClient
130+
131+
grpcConn, err := grpc.Dial(
132+
tn.hostGRPCPort, grpc.WithTransportCredentials(insecure.NewCredentials()),
133+
)
134+
if err != nil {
135+
return fmt.Errorf("grpc dial: %w", err)
136+
}
137+
tn.GrpcConn = grpcConn
138+
127139
return nil
128140
}
129141

@@ -174,6 +186,7 @@ func (tn *ChainNode) CliContext() client.Context {
174186
cfg := tn.Chain.Config()
175187
return client.Context{
176188
Client: tn.Client,
189+
GRPCClient: tn.GrpcConn,
177190
ChainID: cfg.ChainID,
178191
InterfaceRegistry: cfg.EncodingConfig.InterfaceRegistry,
179192
Input: os.Stdin,

0 commit comments

Comments
 (0)