Skip to content

Commit

Permalink
* Fix GRPC client codec usage.
Browse files Browse the repository at this point in the history
  • Loading branch information
dzmitryhil committed Aug 11, 2023
1 parent 073c2ea commit ee61b9c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
26 changes: 23 additions & 3 deletions infra/apps/cored/cored.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
sdkmath "cosmossdk.io/math"
cosmosclient "github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
Expand Down Expand Up @@ -216,11 +217,11 @@ func (c Cored) ClientContext() client.Context {
rpcClient, err := cosmosclient.NewClientFromNode(infra.JoinNetAddr("http", c.Info().HostFromHost, c.Config().Ports.RPC))
must.OK(err)

grpcClient, err := grpc.Dial(infra.JoinNetAddr("", c.Info().HostFromHost, c.Config().Ports.GRPC),
grpc.WithTransportCredentials(insecure.NewCredentials()))
mm := newBasicManager()
grpcClient, err := dialGRPCClient(infra.JoinNetAddr("", c.Info().HostFromHost, c.Config().Ports.GRPC), mm)
must.OK(err)

return client.NewContext(client.DefaultContextConfig(), newBasicManager()).
return client.NewContext(client.DefaultContextConfig(), mm).
WithChainID(string(c.config.NetworkConfig.ChainID())).
WithRPCClient(rpcClient).
WithGRPCClient(grpcClient)
Expand Down Expand Up @@ -438,3 +439,22 @@ func copyFile(src, dst string, perm os.FileMode) error {

return nil
}

func dialGRPCClient(url string, mm module.BasicManager) (*grpc.ClientConn, error) {
encodingConfig := config.NewEncodingConfig(mm)
pc, ok := encodingConfig.Codec.(codec.GRPCCodecProvider)
if !ok {
return nil, errors.New("failed to cast codec to codec.GRPCCodecProvider)")
}

grpClient, err := grpc.Dial(
url,
grpc.WithDefaultCallOptions(grpc.ForceCodec(pc.GRPCCodec())),
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
if err != nil {
return nil, errors.WithStack(err)
}

return grpClient, nil
}
27 changes: 24 additions & 3 deletions infra/cosmoschain/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"

cosmosclient "github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/bank"
Expand All @@ -22,6 +23,7 @@ import (

"github.com/CoreumFoundation/coreum-tools/pkg/must"
"github.com/CoreumFoundation/coreum/v2/pkg/client"
"github.com/CoreumFoundation/coreum/v2/pkg/config"
"github.com/CoreumFoundation/crust/infra"
"github.com/CoreumFoundation/crust/infra/targets"
)
Expand Down Expand Up @@ -113,11 +115,11 @@ func (ba BaseApp) ClientContext() client.Context {
rpcClient, err := cosmosclient.NewClientFromNode(infra.JoinNetAddr("http", ba.Info().HostFromHost, ba.appConfig.Ports.RPC))
must.OK(err)

grpcClient, err := grpc.Dial(infra.JoinNetAddr("", ba.Info().HostFromHost, ba.appConfig.Ports.GRPC),
grpc.WithTransportCredentials(insecure.NewCredentials()))
mm := newBasicManager()
grpcClient, err := dialGRPCClient(infra.JoinNetAddr("", ba.Info().HostFromHost, ba.appConfig.Ports.GRPC), mm)
must.OK(err)

return client.NewContext(client.DefaultContextConfig(), newBasicManager()).
return client.NewContext(client.DefaultContextConfig(), mm).
WithChainID(ba.appConfig.ChainID).
WithRPCClient(rpcClient).
WithGRPCClient(grpcClient)
Expand Down Expand Up @@ -189,6 +191,25 @@ func (ba BaseApp) prepare() error {
return nil
}

func dialGRPCClient(url string, mm module.BasicManager) (*grpc.ClientConn, error) {
encodingConfig := config.NewEncodingConfig(mm)
pc, ok := encodingConfig.Codec.(codec.GRPCCodecProvider)
if !ok {
return nil, errors.New("failed to cast codec to codec.GRPCCodecProvider)")
}

grpClient, err := grpc.Dial(
url,
grpc.WithDefaultCallOptions(grpc.ForceCodec(pc.GRPCCodec())),
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
if err != nil {
return nil, errors.WithStack(err)
}

return grpClient, nil
}

func newBasicManager() module.BasicManager {
return module.NewBasicManager(
auth.AppModuleBasic{},
Expand Down

0 comments on commit ee61b9c

Please sign in to comment.