Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat/ibc_connection_module_queries #219

Merged
merged 1 commit into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 94 additions & 28 deletions client/chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"github.com/cosmos/gogoproto/proto"
ibctransfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
ibcclienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
ibcconnectiontypes "github.com/cosmos/ibc-go/v7/modules/core/03-connection/types"
ibcchanneltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"
eth "github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
Expand Down Expand Up @@ -279,6 +280,14 @@
FetchIBCUpgradedClientState(ctx context.Context) (*ibcclienttypes.QueryUpgradedClientStateResponse, error)
FetchIBCUpgradedConsensusState(ctx context.Context) (*ibcclienttypes.QueryUpgradedConsensusStateResponse, error)

// IBC Core Connection module
FetchIBCConnection(ctx context.Context, connectionId string) (*ibcconnectiontypes.QueryConnectionResponse, error)
FetchIBCConnections(ctx context.Context, pagination *query.PageRequest) (*ibcconnectiontypes.QueryConnectionsResponse, error)
FetchIBCClientConnections(ctx context.Context, clientId string) (*ibcconnectiontypes.QueryClientConnectionsResponse, error)
FetchIBCConnectionClientState(ctx context.Context, connectionId string) (*ibcconnectiontypes.QueryConnectionClientStateResponse, error)
FetchIBCConnectionConsensusState(ctx context.Context, connectionId string, revisionNumber uint64, revisionHeight uint64) (*ibcconnectiontypes.QueryConnectionConsensusStateResponse, error)
FetchIBCConnectionParams(ctx context.Context) (*ibcconnectiontypes.QueryConnectionParamsResponse, error)

Close()
}

Expand All @@ -305,20 +314,21 @@

sessionEnabled bool

authQueryClient authtypes.QueryClient
authzQueryClient authztypes.QueryClient
bankQueryClient banktypes.QueryClient
chainStreamClient chainstreamtypes.StreamClient
distributionQueryClient distributiontypes.QueryClient
exchangeQueryClient exchangetypes.QueryClient
ibcChannelQueryClient ibcchanneltypes.QueryClient
ibcClientQueryClient ibcclienttypes.QueryClient
ibcTransferQueryClient ibctransfertypes.QueryClient
tendermintQueryClient tmservice.ServiceClient
tokenfactoryQueryClient tokenfactorytypes.QueryClient
txClient txtypes.ServiceClient
wasmQueryClient wasmtypes.QueryClient
subaccountToNonce map[ethcommon.Hash]uint32
authQueryClient authtypes.QueryClient
authzQueryClient authztypes.QueryClient
bankQueryClient banktypes.QueryClient
chainStreamClient chainstreamtypes.StreamClient
distributionQueryClient distributiontypes.QueryClient
exchangeQueryClient exchangetypes.QueryClient
ibcChannelQueryClient ibcchanneltypes.QueryClient
ibcClientQueryClient ibcclienttypes.QueryClient
ibcConnectionQueryClient ibcconnectiontypes.QueryClient
ibcTransferQueryClient ibctransfertypes.QueryClient
tendermintQueryClient tmservice.ServiceClient
tokenfactoryQueryClient tokenfactorytypes.QueryClient
txClient txtypes.ServiceClient
wasmQueryClient wasmtypes.QueryClient
subaccountToNonce map[ethcommon.Hash]uint32

closed int64
canSign bool
Expand Down Expand Up @@ -404,20 +414,21 @@

sessionEnabled: stickySessionEnabled,

authQueryClient: authtypes.NewQueryClient(conn),
authzQueryClient: authztypes.NewQueryClient(conn),
bankQueryClient: banktypes.NewQueryClient(conn),
chainStreamClient: chainstreamtypes.NewStreamClient(chainStreamConn),
distributionQueryClient: distributiontypes.NewQueryClient(conn),
exchangeQueryClient: exchangetypes.NewQueryClient(conn),
ibcChannelQueryClient: ibcchanneltypes.NewQueryClient(conn),
ibcClientQueryClient: ibcclienttypes.NewQueryClient(conn),
ibcTransferQueryClient: ibctransfertypes.NewQueryClient(conn),
tendermintQueryClient: tmservice.NewServiceClient(conn),
tokenfactoryQueryClient: tokenfactorytypes.NewQueryClient(conn),
txClient: txtypes.NewServiceClient(conn),
wasmQueryClient: wasmtypes.NewQueryClient(conn),
subaccountToNonce: make(map[ethcommon.Hash]uint32),
authQueryClient: authtypes.NewQueryClient(conn),
authzQueryClient: authztypes.NewQueryClient(conn),
bankQueryClient: banktypes.NewQueryClient(conn),
chainStreamClient: chainstreamtypes.NewStreamClient(chainStreamConn),
distributionQueryClient: distributiontypes.NewQueryClient(conn),
exchangeQueryClient: exchangetypes.NewQueryClient(conn),
ibcChannelQueryClient: ibcchanneltypes.NewQueryClient(conn),
ibcClientQueryClient: ibcclienttypes.NewQueryClient(conn),
ibcConnectionQueryClient: ibcconnectiontypes.NewQueryClient(conn),
ibcTransferQueryClient: ibctransfertypes.NewQueryClient(conn),
tendermintQueryClient: tmservice.NewServiceClient(conn),
tokenfactoryQueryClient: tokenfactorytypes.NewQueryClient(conn),
txClient: txtypes.NewServiceClient(conn),
wasmQueryClient: wasmtypes.NewQueryClient(conn),
subaccountToNonce: make(map[ethcommon.Hash]uint32),
}

if cc.canSign {
Expand Down Expand Up @@ -2565,3 +2576,58 @@

return res, err
}

// IBC Core Connection module
func (c *chainClient) FetchIBCConnection(ctx context.Context, connectionId string) (*ibcconnectiontypes.QueryConnectionResponse, error) {
req := &ibcconnectiontypes.QueryConnectionRequest{
ConnectionId: connectionId,
}
res, err := common.ExecuteCall(ctx, c.network.ChainCookieAssistant, c.ibcConnectionQueryClient.Connection, req)

return res, err

Check warning on line 2587 in client/chain/chain.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain.go#L2581-L2587

Added lines #L2581 - L2587 were not covered by tests
}

func (c *chainClient) FetchIBCConnections(ctx context.Context, pagination *query.PageRequest) (*ibcconnectiontypes.QueryConnectionsResponse, error) {
req := &ibcconnectiontypes.QueryConnectionsRequest{
Pagination: pagination,
}
res, err := common.ExecuteCall(ctx, c.network.ChainCookieAssistant, c.ibcConnectionQueryClient.Connections, req)

return res, err

Check warning on line 2596 in client/chain/chain.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain.go#L2590-L2596

Added lines #L2590 - L2596 were not covered by tests
}

func (c *chainClient) FetchIBCClientConnections(ctx context.Context, clientId string) (*ibcconnectiontypes.QueryClientConnectionsResponse, error) {
req := &ibcconnectiontypes.QueryClientConnectionsRequest{
ClientId: clientId,
}
res, err := common.ExecuteCall(ctx, c.network.ChainCookieAssistant, c.ibcConnectionQueryClient.ClientConnections, req)

return res, err

Check warning on line 2605 in client/chain/chain.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain.go#L2599-L2605

Added lines #L2599 - L2605 were not covered by tests
}

func (c *chainClient) FetchIBCConnectionClientState(ctx context.Context, connectionId string) (*ibcconnectiontypes.QueryConnectionClientStateResponse, error) {
req := &ibcconnectiontypes.QueryConnectionClientStateRequest{
ConnectionId: connectionId,
}
res, err := common.ExecuteCall(ctx, c.network.ChainCookieAssistant, c.ibcConnectionQueryClient.ConnectionClientState, req)

return res, err

Check warning on line 2614 in client/chain/chain.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain.go#L2608-L2614

Added lines #L2608 - L2614 were not covered by tests
}

func (c *chainClient) FetchIBCConnectionConsensusState(ctx context.Context, connectionId string, revisionNumber uint64, revisionHeight uint64) (*ibcconnectiontypes.QueryConnectionConsensusStateResponse, error) {
req := &ibcconnectiontypes.QueryConnectionConsensusStateRequest{
ConnectionId: connectionId,
RevisionNumber: revisionNumber,
RevisionHeight: revisionHeight,
}
res, err := common.ExecuteCall(ctx, c.network.ChainCookieAssistant, c.ibcConnectionQueryClient.ConnectionConsensusState, req)

return res, err

Check warning on line 2625 in client/chain/chain.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain.go#L2617-L2625

Added lines #L2617 - L2625 were not covered by tests
}

func (c *chainClient) FetchIBCConnectionParams(ctx context.Context) (*ibcconnectiontypes.QueryConnectionParamsResponse, error) {
req := &ibcconnectiontypes.QueryConnectionParamsRequest{}
res, err := common.ExecuteCall(ctx, c.network.ChainCookieAssistant, c.ibcConnectionQueryClient.ConnectionParams, req)

return res, err

Check warning on line 2632 in client/chain/chain.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain.go#L2628-L2632

Added lines #L2628 - L2632 were not covered by tests
}
44 changes: 32 additions & 12 deletions client/chain/chain_test_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,13 @@
"errors"
"time"

ibcclienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"

ibcchanneltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"

ibctransfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"

"github.com/cosmos/cosmos-sdk/client/grpc/tmservice"

distributiontypes "github.com/cosmos/cosmos-sdk/x/distribution/types"

tokenfactorytypes "github.com/InjectiveLabs/sdk-go/chain/tokenfactory/types"
"google.golang.org/grpc"

wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"

exchangetypes "github.com/InjectiveLabs/sdk-go/chain/exchange/types"
chainstreamtypes "github.com/InjectiveLabs/sdk-go/chain/stream/types"
tokenfactorytypes "github.com/InjectiveLabs/sdk-go/chain/tokenfactory/types"
"github.com/InjectiveLabs/sdk-go/client/common"
rpchttp "github.com/cometbft/cometbft/rpc/client/http"
"github.com/cosmos/cosmos-sdk/client"
Expand All @@ -30,8 +21,12 @@
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
authztypes "github.com/cosmos/cosmos-sdk/x/authz"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
distributiontypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
ibctransfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
ibcclienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
ibcconnectiontypes "github.com/cosmos/ibc-go/v7/modules/core/03-connection/types"
ibcchanneltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"
eth "github.com/ethereum/go-ethereum/common"
"google.golang.org/grpc"
)

type MockChainClient struct {
Expand Down Expand Up @@ -695,3 +690,28 @@
func (c *MockChainClient) FetchIBCUpgradedConsensusState(ctx context.Context) (*ibcclienttypes.QueryUpgradedConsensusStateResponse, error) {
return &ibcclienttypes.QueryUpgradedConsensusStateResponse{}, nil
}

// IBC Core Connection module
func (c *MockChainClient) FetchIBCConnection(ctx context.Context, connectionId string) (*ibcconnectiontypes.QueryConnectionResponse, error) {
return &ibcconnectiontypes.QueryConnectionResponse{}, nil

Check warning on line 696 in client/chain/chain_test_support.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain_test_support.go#L695-L696

Added lines #L695 - L696 were not covered by tests
}

func (c *MockChainClient) FetchIBCConnections(ctx context.Context, pagination *query.PageRequest) (*ibcconnectiontypes.QueryConnectionsResponse, error) {
return &ibcconnectiontypes.QueryConnectionsResponse{}, nil

Check warning on line 700 in client/chain/chain_test_support.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain_test_support.go#L699-L700

Added lines #L699 - L700 were not covered by tests
}

func (c *MockChainClient) FetchIBCClientConnections(ctx context.Context, clientId string) (*ibcconnectiontypes.QueryClientConnectionsResponse, error) {
return &ibcconnectiontypes.QueryClientConnectionsResponse{}, nil

Check warning on line 704 in client/chain/chain_test_support.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain_test_support.go#L703-L704

Added lines #L703 - L704 were not covered by tests
}

func (c *MockChainClient) FetchIBCConnectionClientState(ctx context.Context, connectionId string) (*ibcconnectiontypes.QueryConnectionClientStateResponse, error) {
return &ibcconnectiontypes.QueryConnectionClientStateResponse{}, nil

Check warning on line 708 in client/chain/chain_test_support.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain_test_support.go#L707-L708

Added lines #L707 - L708 were not covered by tests
}

func (c *MockChainClient) FetchIBCConnectionConsensusState(ctx context.Context, connectionId string, revisionNumber uint64, revisionHeight uint64) (*ibcconnectiontypes.QueryConnectionConsensusStateResponse, error) {
return &ibcconnectiontypes.QueryConnectionConsensusStateResponse{}, nil

Check warning on line 712 in client/chain/chain_test_support.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain_test_support.go#L711-L712

Added lines #L711 - L712 were not covered by tests
}

func (c *MockChainClient) FetchIBCConnectionParams(ctx context.Context) (*ibcconnectiontypes.QueryConnectionParamsResponse, error) {
return &ibcconnectiontypes.QueryConnectionParamsResponse{}, nil

Check warning on line 716 in client/chain/chain_test_support.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain_test_support.go#L715-L716

Added lines #L715 - L716 were not covered by tests
}
71 changes: 71 additions & 0 deletions examples/chain/ibc/connection/query/1_Connection/example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package main

import (
"context"
"encoding/json"
"fmt"

"github.com/InjectiveLabs/sdk-go/client"

chainclient "github.com/InjectiveLabs/sdk-go/client/chain"
"github.com/InjectiveLabs/sdk-go/client/common"
rpchttp "github.com/cometbft/cometbft/rpc/client/http"

"os"
)

func main() {
network := common.LoadNetwork("testnet", "lb")
tmClient, err := rpchttp.New(network.TmEndpoint, "/websocket")
if err != nil {
panic(err)
Comment on lines +19 to +21
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure proper error handling instead of using panic.

Replace panic(err) with more robust error handling. This could involve logging the error and exiting gracefully, which would be more suitable for production environments.

}

senderAddress, cosmosKeyring, err := chainclient.InitCosmosKeyring(
os.Getenv("HOME")+"/.injectived",
"injectived",
"file",
"inj-user",
"12345678",
"5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e", // keyring will be used if pk not provided
false,
)

if err != nil {
panic(err)
}
Comment on lines +34 to +36
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure proper error handling instead of using panic.

Similar to previous comments, replace panic(err) with structured error handling. This improves the stability and user experience by providing clear error messages.


clientCtx, err := chainclient.NewClientContext(
network.ChainId,
senderAddress.String(),
cosmosKeyring,
)

if err != nil {
panic(err)
}
Comment on lines +44 to +46
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure proper error handling instead of using panic.

Again, replace panic(err) with a more user-friendly error handling strategy. Consistent and thoughtful error handling is crucial for maintaining the reliability of the application.


clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient)

chainClient, err := chainclient.NewChainClient(
clientCtx,
network,
common.OptionGasPrices(client.DefaultGasPriceWithDenom),
)

if err != nil {
panic(err)
}
Comment on lines +56 to +58
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure proper error handling instead of using panic.

This is another instance where replacing panic(err) with a more controlled error handling approach would be beneficial. Consider using error logging and a controlled shutdown of the application.


connectionId := "connection-0"
ctx := context.Background()

res, err := chainClient.FetchIBCConnection(ctx, connectionId)
if err != nil {
fmt.Println(err)
Comment on lines +63 to +65
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handle the error case more explicitly.

The error from FetchIBCConnection is only printed. Consider adding logic to handle errors more effectively, such as retrying the request or terminating the application based on the error severity.

}

str, _ := json.MarshalIndent(res, "", " ")
fmt.Print(string(str))

}
73 changes: 73 additions & 0 deletions examples/chain/ibc/connection/query/2_Connections/example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package main

import (
"context"
"encoding/json"
"fmt"

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

"github.com/InjectiveLabs/sdk-go/client"

chainclient "github.com/InjectiveLabs/sdk-go/client/chain"
"github.com/InjectiveLabs/sdk-go/client/common"
rpchttp "github.com/cometbft/cometbft/rpc/client/http"

"os"
)

func main() {
network := common.LoadNetwork("testnet", "lb")
tmClient, err := rpchttp.New(network.TmEndpoint, "/websocket")
if err != nil {
panic(err)
Comment on lines +21 to +23
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure proper error handling instead of using panic.

Replace panic(err) with more robust error handling. This could involve logging the error and exiting gracefully, which would be more suitable for production environments.

}

senderAddress, cosmosKeyring, err := chainclient.InitCosmosKeyring(
os.Getenv("HOME")+"/.injectived",
"injectived",
"file",
"inj-user",
"12345678",
"5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e", // keyring will be used if pk not provided
false,
)

if err != nil {
panic(err)
}
Comment on lines +36 to +38
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure proper error handling instead of using panic.

Similar to previous comments, replace panic(err) with structured error handling. This improves the stability and user experience by providing clear error messages.


clientCtx, err := chainclient.NewClientContext(
network.ChainId,
senderAddress.String(),
cosmosKeyring,
)

if err != nil {
panic(err)
}
Comment on lines +46 to +48
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure proper error handling instead of using panic.

Again, replace panic(err) with a more user-friendly error handling strategy. Consistent and thoughtful error handling is crucial for maintaining the reliability of the application.


clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient)

chainClient, err := chainclient.NewChainClient(
clientCtx,
network,
common.OptionGasPrices(client.DefaultGasPriceWithDenom),
)

if err != nil {
panic(err)
}
Comment on lines +58 to +60
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure proper error handling instead of using panic.

This is another instance where replacing panic(err) with a more controlled error handling approach would be beneficial. Consider using error logging and a controlled shutdown of the application.


pagination := query.PageRequest{Offset: 2, Limit: 4}
ctx := context.Background()

res, err := chainClient.FetchIBCConnections(ctx, &pagination)
if err != nil {
fmt.Println(err)
}
Comment on lines +66 to +68
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handle the error case more explicitly.

The error from FetchIBCConnections is only printed. Consider adding logic to handle errors more effectively, such as retrying the request or terminating the application based on the error severity.


str, _ := json.MarshalIndent(res, "", " ")
fmt.Print(string(str))

}
Loading
Loading