Skip to content

Commit

Permalink
refactor: encapsulate slim client in wrapper (#1388)
Browse files Browse the repository at this point in the history
* refactor: encapsulate slim client in wrapper

* test: fix missed change when handling merge conflicts
  • Loading branch information
jtieri authored Jan 30, 2024
1 parent 7cb083c commit ef1578e
Show file tree
Hide file tree
Showing 13 changed files with 105 additions and 154 deletions.
63 changes: 44 additions & 19 deletions client/client_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import (
"context"

"github.com/cometbft/cometbft/abci/types"
cometcrypto "github.com/cometbft/cometbft/crypto"
ced25519 "github.com/cometbft/cometbft/crypto/ed25519"
"github.com/cometbft/cometbft/crypto/merkle"
csecp256k1 "github.com/cometbft/cometbft/crypto/secp256k1"
csr25519 "github.com/cometbft/cometbft/crypto/sr25519"
"github.com/cometbft/cometbft/libs/bytes"
"github.com/cometbft/cometbft/p2p"
"github.com/cometbft/cometbft/proto/tendermint/crypto"
Expand All @@ -14,6 +18,10 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
sltypes "github.com/strangelove-ventures/cometbft-client/abci/types"
"github.com/strangelove-ventures/cometbft-client/client"
slcrypto "github.com/strangelove-ventures/cometbft-client/crypto"
"github.com/strangelove-ventures/cometbft-client/crypto/ed25519"
"github.com/strangelove-ventures/cometbft-client/crypto/secp256k1"
"github.com/strangelove-ventures/cometbft-client/crypto/sr25519"
slbytes "github.com/strangelove-ventures/cometbft-client/libs/bytes"
slclient "github.com/strangelove-ventures/cometbft-client/rpc/client"
coretypes2 "github.com/strangelove-ventures/cometbft-client/rpc/core/types"
Expand All @@ -23,11 +31,15 @@ import (
// RPCClient wraps our slimmed down CometBFT client and converts the returned types to the upstream CometBFT types.
// This is useful so that it can be used in any function calls that expect the upstream types.
type RPCClient struct {
Client *client.Client
c *client.Client
}

func NewRPCClient(c *client.Client) RPCClient {
return RPCClient{c: c}
}

func (r RPCClient) ABCIInfo(ctx context.Context) (*coretypes.ResultABCIInfo, error) {
res, err := r.Client.ABCIInfo(ctx)
res, err := r.c.ABCIInfo(ctx)
if err != nil {
return nil, err
}
Expand All @@ -48,7 +60,7 @@ func (r RPCClient) ABCIQuery(
path string,
data bytes.HexBytes,
) (*coretypes.ResultABCIQuery, error) {
res, err := r.Client.ABCIQuery(ctx, path, slbytes.HexBytes(data))
res, err := r.c.ABCIQuery(ctx, path, slbytes.HexBytes(data))
if err != nil {
return nil, err
}
Expand All @@ -67,7 +79,7 @@ func (r RPCClient) ABCIQueryWithOptions(
Prove: opts.Prove,
}

res, err := r.Client.ABCIQueryWithOptions(ctx, path, slbytes.HexBytes(data), o)
res, err := r.c.ABCIQueryWithOptions(ctx, path, slbytes.HexBytes(data), o)
if err != nil {
return nil, err
}
Expand All @@ -76,7 +88,7 @@ func (r RPCClient) ABCIQueryWithOptions(
}

func (r RPCClient) BroadcastTxCommit(ctx context.Context, tx tmtypes.Tx) (*coretypes.ResultBroadcastTxCommit, error) {
res, err := r.Client.BroadcastTxCommit(ctx, types2.Tx(tx))
res, err := r.c.BroadcastTxCommit(ctx, types2.Tx(tx))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -108,7 +120,7 @@ func (r RPCClient) BroadcastTxCommit(ctx context.Context, tx tmtypes.Tx) (*coret
}

func (r RPCClient) BroadcastTxAsync(ctx context.Context, tx tmtypes.Tx) (*coretypes.ResultBroadcastTx, error) {
res, err := r.Client.BroadcastTxAsync(ctx, types2.Tx(tx))
res, err := r.c.BroadcastTxAsync(ctx, types2.Tx(tx))
if err != nil {
return nil, err
}
Expand All @@ -123,7 +135,7 @@ func (r RPCClient) BroadcastTxAsync(ctx context.Context, tx tmtypes.Tx) (*corety
}

func (r RPCClient) BroadcastTxSync(ctx context.Context, tx tmtypes.Tx) (*coretypes.ResultBroadcastTx, error) {
res, err := r.Client.BroadcastTxSync(ctx, types2.Tx(tx))
res, err := r.c.BroadcastTxSync(ctx, types2.Tx(tx))
if err != nil {
return nil, err
}
Expand All @@ -142,7 +154,7 @@ func (r RPCClient) Validators(
height *int64,
page, perPage *int,
) (*coretypes.ResultValidators, error) {
res, err := r.Client.Validators(ctx, height, page, perPage)
res, err := r.c.Validators(ctx, height, page, perPage)
if err != nil {
return nil, err
}
Expand All @@ -151,7 +163,7 @@ func (r RPCClient) Validators(
for i, val := range res.Validators {
vals[i] = &tmtypes.Validator{
Address: tmtypes.Address(val.Address),
PubKey: nil,
PubKey: convertPubKey(val.PubKey),
VotingPower: val.VotingPower,
ProposerPriority: val.ProposerPriority,
}
Expand All @@ -166,7 +178,7 @@ func (r RPCClient) Validators(
}

func (r RPCClient) Status(ctx context.Context) (*coretypes.ResultStatus, error) {
res, err := r.Client.Status(ctx)
res, err := r.c.Status(ctx)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -202,14 +214,14 @@ func (r RPCClient) Status(ctx context.Context) (*coretypes.ResultStatus, error)
},
ValidatorInfo: coretypes.ValidatorInfo{
Address: bytes.HexBytes(res.ValidatorInfo.Address),
PubKey: nil,
PubKey: convertPubKey(res.ValidatorInfo.PubKey),
VotingPower: res.ValidatorInfo.VotingPower,
},
}, nil
}

func (r RPCClient) Block(ctx context.Context, height *int64) (*coretypes.ResultBlock, error) {
res, err := r.Client.Block(ctx, height)
res, err := r.c.Block(ctx, height)
if err != nil {
return nil, err
}
Expand All @@ -221,7 +233,7 @@ func (r RPCClient) Block(ctx context.Context, height *int64) (*coretypes.ResultB
}

func (r RPCClient) BlockByHash(ctx context.Context, hash []byte) (*coretypes.ResultBlock, error) {
res, err := r.Client.BlockByHash(ctx, hash)
res, err := r.c.BlockByHash(ctx, hash)
if err != nil {
return nil, err
}
Expand All @@ -233,7 +245,7 @@ func (r RPCClient) BlockByHash(ctx context.Context, hash []byte) (*coretypes.Res
}

func (r RPCClient) BlockResults(ctx context.Context, height *int64) (*coretypes.ResultBlockResults, error) {
res, err := r.Client.BlockResults(ctx, height)
res, err := r.c.BlockResults(ctx, height)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -266,7 +278,7 @@ func (r RPCClient) BlockchainInfo(
ctx context.Context,
minHeight, maxHeight int64,
) (*coretypes.ResultBlockchainInfo, error) {
res, err := r.Client.BlockchainInfo(ctx, minHeight, maxHeight)
res, err := r.c.BlockchainInfo(ctx, minHeight, maxHeight)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -294,7 +306,7 @@ func (r RPCClient) BlockchainInfo(
}

func (r RPCClient) Commit(ctx context.Context, height *int64) (*coretypes.ResultCommit, error) {
res, err := r.Client.Commit(ctx, height)
res, err := r.c.Commit(ctx, height)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -325,7 +337,7 @@ func (r RPCClient) Commit(ctx context.Context, height *int64) (*coretypes.Result
}

func (r RPCClient) Tx(ctx context.Context, hash []byte, prove bool) (*coretypes.ResultTx, error) {
res, err := r.Client.Tx(ctx, hash, prove)
res, err := r.c.Tx(ctx, hash, prove)
if err != nil {
return nil, err
}
Expand All @@ -340,7 +352,7 @@ func (r RPCClient) TxSearch(
page, perPage *int,
orderBy string,
) (*coretypes.ResultTxSearch, error) {
res, err := r.Client.TxSearch(ctx, query, prove, page, perPage, orderBy)
res, err := r.c.TxSearch(ctx, query, prove, page, perPage, orderBy)
if err != nil {
return nil, err
}
Expand All @@ -362,7 +374,7 @@ func (r RPCClient) BlockSearch(
page, perPage *int,
orderBy string,
) (*coretypes.ResultBlockSearch, error) {
res, err := r.Client.BlockSearch(ctx, query, page, perPage, orderBy)
res, err := r.c.BlockSearch(ctx, query, page, perPage, orderBy)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -553,3 +565,16 @@ func convertResultTx(res *client.TxResponse) *coretypes.ResultTx {
},
}
}

func convertPubKey(pk slcrypto.PubKey) cometcrypto.PubKey {
switch key := pk.(type) {
case ed25519.PubKey:
return ced25519.PubKey(key)
case secp256k1.PubKey:
return csecp256k1.PubKey(key)
case sr25519.PubKey:
return csr25519.PubKey(key)
default:
return nil
}
}
2 changes: 1 addition & 1 deletion cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ func newDefaultGlobalConfig(memo string) GlobalConfig {
LightCacheSize: 20,
Memo: memo,
LogLevel: "info",
MaxReceiverSize: 128,
MaxReceiverSize: 150,
}
}

Expand Down
11 changes: 6 additions & 5 deletions interchaintest/feegrant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ import (
sdkmath "cosmossdk.io/math"
"cosmossdk.io/x/feegrant"
"github.com/avast/retry-go/v4"
coretypes "github.com/cometbft/cometbft/rpc/core/types"
"github.com/cosmos/cosmos-sdk/types"
txtypes "github.com/cosmos/cosmos-sdk/types/tx"
"github.com/cosmos/go-bip39"
transfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
chantypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"
"github.com/cosmos/relayer/v2/client"
"github.com/cosmos/relayer/v2/relayer"
"github.com/cosmos/relayer/v2/relayer/chains/cosmos"
"github.com/cosmos/relayer/v2/relayer/processor"
clienttypes "github.com/strangelove-ventures/cometbft-client/client"
"github.com/strangelove-ventures/interchaintest/v8"
cosmosv8 "github.com/strangelove-ventures/interchaintest/v8/chain/cosmos"
"github.com/strangelove-ventures/interchaintest/v8/ibc"
Expand Down Expand Up @@ -395,7 +396,7 @@ func TestRelayerFeeGrant(t *testing.T) {

hash, err := hex.DecodeString(curr.Response.TxHash)
require.Nil(t, err)
txResp, err := TxWithRetry(ctx, cProv.RPCClient.Client, hash)
txResp, err := TxWithRetry(ctx, cProv.RPCClient, hash)
require.Nil(t, err)

require.Nil(t, err)
Expand Down Expand Up @@ -537,9 +538,9 @@ func TestRelayerFeeGrant(t *testing.T) {
}
}

func TxWithRetry(ctx context.Context, client *clienttypes.Client, hash []byte) (*clienttypes.TxResponse, error) {
func TxWithRetry(ctx context.Context, client client.RPCClient, hash []byte) (*coretypes.ResultTx, error) {
var err error
var res *clienttypes.TxResponse
var res *coretypes.ResultTx
if err = retry.Do(func() error {
res, err = client.Tx(ctx, hash, true)
return err
Expand Down Expand Up @@ -869,7 +870,7 @@ func TestRelayerFeeGrantExternal(t *testing.T) {

hash, err := hex.DecodeString(curr.Response.TxHash)
require.Nil(t, err)
txResp, err := TxWithRetry(ctx, cProv.RPCClient.Client, hash)
txResp, err := TxWithRetry(ctx, cProv.RPCClient, hash)
require.Nil(t, err)

require.Nil(t, err)
Expand Down
13 changes: 6 additions & 7 deletions relayer/chains/cosmos/cosmos_chain_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/avast/retry-go/v4"
coretypes "github.com/cometbft/cometbft/rpc/core/types"
sdk "github.com/cosmos/cosmos-sdk/types"
clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
conntypes "github.com/cosmos/ibc-go/v8/modules/core/03-connection/types"
Expand All @@ -16,8 +17,6 @@ import (
"github.com/cosmos/relayer/v2/relayer/chains"
"github.com/cosmos/relayer/v2/relayer/processor"
"github.com/cosmos/relayer/v2/relayer/provider"
"github.com/strangelove-ventures/cometbft-client/client"
coretypes "github.com/strangelove-ventures/cometbft-client/rpc/core/types"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
)
Expand Down Expand Up @@ -389,7 +388,7 @@ func (ccp *CosmosChainProcessor) queryCycle(ctx context.Context, persistence *qu
for i := persistence.latestQueriedBlock + 1; i <= persistence.latestHeight; i++ {
var (
eg errgroup.Group
blockRes *client.BlockResponse
blockRes *coretypes.ResultBlockResults
ibcHeader provider.IBCHeader
)

Expand All @@ -399,7 +398,7 @@ func (ccp *CosmosChainProcessor) queryCycle(ctx context.Context, persistence *qu
queryCtx, cancelQueryCtx := context.WithTimeout(ctx, blockResultsQueryTimeout)
defer cancelQueryCtx()

blockRes, err = ccp.chainProvider.RPCClient.Client.BlockResults(queryCtx, &sI)
blockRes, err = ccp.chainProvider.RPCClient.BlockResults(queryCtx, &sI)
if err != nil && ccp.metrics != nil {
ccp.metrics.IncBlockQueryFailure(chainID, "RPC Client")
}
Expand Down Expand Up @@ -458,19 +457,19 @@ func (ccp *CosmosChainProcessor) queryCycle(ctx context.Context, persistence *qu
ibcHeaderCache[heightUint64] = latestHeader
ppChanged = true

messages := chains.ParseIBCMessagesFromEvents(ccp.log, chainID, heightUint64, blockRes.Events)
messages := chains.IbcMessagesFromEvents(ccp.log, blockRes.FinalizeBlockEvents, chainID, heightUint64)

for _, m := range messages {
ccp.handleMessage(ctx, m, ibcMessagesCache)
}

for _, tx := range blockRes.TxResponses {
for _, tx := range blockRes.TxsResults {
if tx.Code != 0 {
// tx was not successful
continue
}

messages := chains.ParseIBCMessagesFromEvents(ccp.log, chainID, heightUint64, tx.Events)
messages := chains.IbcMessagesFromEvents(ccp.log, tx.Events, chainID, heightUint64)

for _, m := range messages {
ccp.handleMessage(ctx, m, ibcMessagesCache)
Expand Down
2 changes: 1 addition & 1 deletion relayer/chains/cosmos/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ func (cc *CosmosProvider) Init(ctx context.Context) error {
return err
}

rpcClient := cwrapper.RPCClient{Client: c}
rpcClient := cwrapper.NewRPCClient(c)

cc.RPCClient = rpcClient
cc.LightProvider = lightprovider
Expand Down
Loading

0 comments on commit ef1578e

Please sign in to comment.