Skip to content

Commit

Permalink
move gRPC conn on node startup
Browse files Browse the repository at this point in the history
  • Loading branch information
Reecepbcups committed Dec 6, 2023
1 parent 2fb0a4f commit 720d1cd
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 398 deletions.
75 changes: 18 additions & 57 deletions chain/cosmos/authz.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,10 @@ import (
"strings"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/authz"
"github.com/strangelove-ventures/interchaintest/v8/ibc"
)

// TODO: Convert to SDK v50.

// Available Commands:
// exec execute tx on behalf of granter account
// grant Grant authorization to an address
// revoke revoke authorization

const ()

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

// authz.QueryGrantsResponse
type QueryAuthzGrantsResponse struct {
Grants []struct {
Authorization struct {
Type string `json:"type"`
Value struct {
Msg string `json:"msg"`
} `json:"value"`
} `json:"authorization"`
} `json:"grants"`
Pagination struct {
Total string `json:"total"`
} `json:"pagination"`
}

// authz.QueryGranteeGrantsResponse & QueryGranterGrantsResponse
type QueryAuthzGrantsByResponse struct {
Grants []struct {
Granter string `json:"granter"`
Grantee string `json:"grantee"`
Authorization struct {
Type string `json:"type"`
Value struct {
Msg string `json:"msg"`
} `json:"value"`
} `json:"authorization"`
} `json:"grants"`
Pagination struct {
Total string `json:"total"`
} `json:"pagination"`
}

func AuthzQueryGrants(ctx context.Context, chain *CosmosChain, granter string, grantee string, msgType string, extraFlags ...string) (*QueryAuthzGrantsResponse, error) {
cmd := []string{"authz", "grants", granter, grantee, msgType}
cmd = append(cmd, extraFlags...)

var res QueryAuthzGrantsResponse
return &res, chain.ExecQueryToResponse(ctx, chain, cmd, &res)
func (c *CosmosChain) AuthzQueryGrants(ctx context.Context, granter string, grantee string, msgType string, extraFlags ...string) ([]*authz.Grant, error) {
res, err := authz.NewQueryClient(c.GetNode().GrpcConn).Grants(ctx, &authz.QueryGrantsRequest{
Granter: granter,
Grantee: grantee,
MsgTypeUrl: msgType,
})
return res.Grants, err
}

func AuthzQueryGrantsByGrantee(ctx context.Context, chain *CosmosChain, grantee string, extraFlags ...string) (*QueryAuthzGrantsByResponse, error) {
cmd := []string{"authz", "grants-by-grantee", grantee}
cmd = append(cmd, extraFlags...)

var res QueryAuthzGrantsByResponse
return &res, chain.ExecQueryToResponse(ctx, chain, cmd, &res)
func (c *CosmosChain) AuthzQueryGrantsByGrantee(ctx context.Context, grantee string, extraFlags ...string) ([]*authz.GrantAuthorization, error) {
res, err := authz.NewQueryClient(c.GetNode().GrpcConn).GranteeGrants(ctx, &authz.QueryGranteeGrantsRequest{
Grantee: grantee,
})
return res.Grants, err
}

func AuthzQueryGrantsByGranter(ctx context.Context, chain *CosmosChain, granter string, extraFlags ...string) (*QueryAuthzGrantsByResponse, error) {
cmd := []string{"authz", "grants-by-granter", granter}
cmd = append(cmd, extraFlags...)

var res QueryAuthzGrantsByResponse
return &res, chain.ExecQueryToResponse(ctx, chain, cmd, &res)
func (c *CosmosChain) AuthzQueryGrantsByGranter(ctx context.Context, granter string, extraFlags ...string) ([]*authz.GrantAuthorization, error) {
res, err := authz.NewQueryClient(c.GetNode().GrpcConn).GranterGrants(ctx, &authz.QueryGranterGrantsRequest{
Granter: granter,
})
return res.Grants, err
}

// createAuthzJSON creates a JSON file with a single generated message.
Expand Down
125 changes: 13 additions & 112 deletions chain/cosmos/bank.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import (
"context"
"fmt"

"cosmossdk.io/math"
sdkmath "cosmossdk.io/math"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"

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

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

Expand All @@ -36,15 +33,7 @@ func (tn *ChainNode) BankMultiSend(ctx context.Context, keyName string, addresse
// GetBalance fetches the current balance for a specific account address and denom.
// Implements Chain interface
func (c *CosmosChain) GetBalance(ctx context.Context, address string, denom string) (sdkmath.Int, error) {
grpcConn, err := grpc.Dial(
c.GetNode().hostGRPCPort, grpc.WithTransportCredentials(insecure.NewCredentials()),
)
if err != nil {
return sdkmath.Int{}, err
}
defer grpcConn.Close()

res, err := banktypes.NewQueryClient(grpcConn).Balance(ctx, &banktypes.QueryBalanceRequest{Address: address, Denom: denom})
res, err := banktypes.NewQueryClient(c.GetNode().GrpcConn).Balance(ctx, &banktypes.QueryBalanceRequest{Address: address, Denom: denom})
return res.Balance.Amount, err
}

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

// AllBalances fetches an account address's balance for all denoms it holds
func (c *CosmosChain) BankAllBalances(ctx context.Context, address string) (types.Coins, error) {
grpcConn, err := grpc.Dial(
c.GetNode().hostGRPCPort, grpc.WithTransportCredentials(insecure.NewCredentials()),
)
if err != nil {
return nil, err
}
defer grpcConn.Close()

res, err := banktypes.NewQueryClient(grpcConn).AllBalances(ctx, &banktypes.QueryAllBalancesRequest{Address: address})
res, err := banktypes.NewQueryClient(c.GetNode().GrpcConn).AllBalances(ctx, &banktypes.QueryAllBalancesRequest{Address: address})
return res.GetBalances(), err
}

// BankDenomMetadata fetches the metadata of a specific coin denomination
func (c *CosmosChain) BankDenomMetadata(ctx context.Context, denom string) (*banktypes.Metadata, error) {
grpcConn, err := grpc.Dial(
c.GetNode().hostGRPCPort, grpc.WithTransportCredentials(insecure.NewCredentials()),
)
if err != nil {
return nil, err
}
defer grpcConn.Close()

res, err := banktypes.NewQueryClient(grpcConn).DenomMetadata(ctx, &banktypes.QueryDenomMetadataRequest{Denom: denom})
res, err := banktypes.NewQueryClient(c.GetNode().GrpcConn).DenomMetadata(ctx, &banktypes.QueryDenomMetadataRequest{Denom: denom})
return &res.Metadata, err
}

func (c *CosmosChain) BankQueryDenomMetadataByQueryString(ctx context.Context, denom string) (*banktypes.Metadata, error) {
grpcConn, err := grpc.Dial(
c.GetNode().hostGRPCPort, grpc.WithTransportCredentials(insecure.NewCredentials()),
)
if err != nil {
return nil, err
}
defer grpcConn.Close()

res, err := banktypes.NewQueryClient(grpcConn).DenomMetadataByQueryString(ctx, &banktypes.QueryDenomMetadataByQueryStringRequest{Denom: denom})
res, err := banktypes.NewQueryClient(c.GetNode().GrpcConn).DenomMetadataByQueryString(ctx, &banktypes.QueryDenomMetadataByQueryStringRequest{Denom: denom})
return &res.Metadata, err
}

func (c *CosmosChain) BankQueryDenomOwners(ctx context.Context, denom string) ([]*banktypes.DenomOwner, error) {
grpcConn, err := grpc.Dial(
c.GetNode().hostGRPCPort, grpc.WithTransportCredentials(insecure.NewCredentials()),
)
if err != nil {
return nil, err
}
defer grpcConn.Close()

res, err := banktypes.NewQueryClient(grpcConn).DenomOwners(ctx, &banktypes.QueryDenomOwnersRequest{Denom: denom})
res, err := banktypes.NewQueryClient(c.GetNode().GrpcConn).DenomOwners(ctx, &banktypes.QueryDenomOwnersRequest{Denom: denom})
return res.DenomOwners, err
}

func (c *CosmosChain) BankQueryDenomsMetadata(ctx context.Context) ([]banktypes.Metadata, error) {
grpcConn, err := grpc.Dial(
c.GetNode().hostGRPCPort, grpc.WithTransportCredentials(insecure.NewCredentials()),
)
if err != nil {
return nil, err
}
defer grpcConn.Close()

res, err := banktypes.NewQueryClient(grpcConn).DenomsMetadata(ctx, &banktypes.QueryDenomsMetadataRequest{})
res, err := banktypes.NewQueryClient(c.GetNode().GrpcConn).DenomsMetadata(ctx, &banktypes.QueryDenomsMetadataRequest{})
return res.Metadatas, err
}

func (c *CosmosChain) BankQueryParams(ctx context.Context) (*banktypes.Params, error) {
grpcConn, err := grpc.Dial(
c.GetNode().hostGRPCPort, grpc.WithTransportCredentials(insecure.NewCredentials()),
)
if err != nil {
return nil, err
}
defer grpcConn.Close()

res, err := banktypes.NewQueryClient(grpcConn).Params(ctx, &banktypes.QueryParamsRequest{})
res, err := banktypes.NewQueryClient(c.GetNode().GrpcConn).Params(ctx, &banktypes.QueryParamsRequest{})
return &res.Params, err
}

func (c *CosmosChain) BankQuerySendEnabled(ctx context.Context, denoms []string) ([]*banktypes.SendEnabled, error) {
grpcConn, err := grpc.Dial(
c.GetNode().hostGRPCPort, grpc.WithTransportCredentials(insecure.NewCredentials()),
)
if err != nil {
return nil, err
}
defer grpcConn.Close()

res, err := banktypes.NewQueryClient(grpcConn).SendEnabled(ctx, &banktypes.QuerySendEnabledRequest{
res, err := banktypes.NewQueryClient(c.GetNode().GrpcConn).SendEnabled(ctx, &banktypes.QuerySendEnabledRequest{
Denoms: denoms,
})
return res.SendEnabled, err
}

func (c *CosmosChain) BankQuerySpendableBalance(ctx context.Context, address, denom string) (*types.Coin, error) {
grpcConn, err := grpc.Dial(
c.GetNode().hostGRPCPort, grpc.WithTransportCredentials(insecure.NewCredentials()),
)
if err != nil {
return nil, err
}
defer grpcConn.Close()

res, err := banktypes.NewQueryClient(grpcConn).SpendableBalanceByDenom(ctx, &banktypes.QuerySpendableBalanceByDenomRequest{
res, err := banktypes.NewQueryClient(c.GetNode().GrpcConn).SpendableBalanceByDenom(ctx, &banktypes.QuerySpendableBalanceByDenomRequest{
Address: address,
Denom: denom,
})
return res.Balance, err
}

func (c *CosmosChain) BankQuerySpendableBalances(ctx context.Context, address string) (*types.Coins, error) {
grpcConn, err := grpc.Dial(
c.GetNode().hostGRPCPort, grpc.WithTransportCredentials(insecure.NewCredentials()),
)
if err != nil {
return nil, err
}
defer grpcConn.Close()

res, err := banktypes.NewQueryClient(grpcConn).SpendableBalances(ctx, &banktypes.QuerySpendableBalancesRequest{Address: address})
res, err := banktypes.NewQueryClient(c.GetNode().GrpcConn).SpendableBalances(ctx, &banktypes.QuerySpendableBalancesRequest{Address: address})
return &res.Balances, err
}

func (c *CosmosChain) BankQueryTotalSupply(ctx context.Context) (*types.Coins, error) {
grpcConn, err := grpc.Dial(
c.GetNode().hostGRPCPort, grpc.WithTransportCredentials(insecure.NewCredentials()),
)
if err != nil {
return nil, err
}
defer grpcConn.Close()

res, err := banktypes.NewQueryClient(grpcConn).TotalSupply(ctx, &banktypes.QueryTotalSupplyRequest{})
res, err := banktypes.NewQueryClient(c.GetNode().GrpcConn).TotalSupply(ctx, &banktypes.QueryTotalSupplyRequest{})
return &res.Supply, err
}

func (c *CosmosChain) BankQueryTotalSupplyOf(ctx context.Context, address string) (*types.Coin, error) {
grpcConn, err := grpc.Dial(
c.GetNode().hostGRPCPort, grpc.WithTransportCredentials(insecure.NewCredentials()),
)
if err != nil {
return nil, err
}
defer grpcConn.Close()

res, err := banktypes.NewQueryClient(grpcConn).SupplyOf(ctx, &banktypes.QuerySupplyOfRequest{Denom: address})
res, err := banktypes.NewQueryClient(c.GetNode().GrpcConn).SupplyOf(ctx, &banktypes.QuerySupplyOfRequest{Denom: address})

return &res.Amount, err
}
13 changes: 13 additions & 0 deletions chain/cosmos/chain_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import (
"github.com/docker/go-connections/nat"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"

"github.com/strangelove-ventures/interchaintest/v8/ibc"
"github.com/strangelove-ventures/interchaintest/v8/internal/blockdb"
Expand All @@ -51,6 +53,7 @@ type ChainNode struct {
NetworkID string
DockerClient *dockerclient.Client
Client rpcclient.Client
GrpcConn *grpc.ClientConn
TestName string
Image ibc.DockerImage

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

tn.Client = rpcClient

grpcConn, err := grpc.Dial(
tn.hostGRPCPort, grpc.WithTransportCredentials(insecure.NewCredentials()),
)
if err != nil {
return fmt.Errorf("grpc dial: %w", err)
}
tn.GrpcConn = grpcConn

return nil
}

Expand Down Expand Up @@ -174,6 +186,7 @@ func (tn *ChainNode) CliContext() client.Context {
cfg := tn.Chain.Config()
return client.Context{
Client: tn.Client,
GRPCClient: tn.GrpcConn,
ChainID: cfg.ChainID,
InterfaceRegistry: cfg.EncodingConfig.InterfaceRegistry,
Input: os.Stdin,
Expand Down
Loading

0 comments on commit 720d1cd

Please sign in to comment.