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

chore: remove cosmos/relayer dependency #429

Merged
merged 22 commits into from
Jan 23, 2025
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ This PR contains a series of PRs on multi-staking support and BTC stakingintegra
- [#391](https://github.com/babylonlabs-io/babylon/pull/391) Fix e2e `TestBTCRewardsDistribution` flunky
check of rewards
- [#419](https://github.com/babylonlabs-io/babylon/pull/419) Add new modules to swagger config
- [#429](https://github.com/babylonlabs-io/babylon/pull/429) chore: remove cosmos/relayer dependency

### Bug fixes

Expand Down
78 changes: 78 additions & 0 deletions client/babylonclient/account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// This file is derived from the Cosmos Relayer repository (https://github.com/cosmos/relayer),
// originally licensed under the Apache License, Version 2.0.

package babylonclient

import (
"context"
"fmt"
"strconv"

"github.com/cosmos/cosmos-sdk/client"
sdk "github.com/cosmos/cosmos-sdk/types"
grpctypes "github.com/cosmos/cosmos-sdk/types/grpc"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)

var _ client.AccountRetriever = &CosmosProvider{}

// GetAccount queries for an account given an address and a block height. An
// error is returned if the query or decoding fails.
func (cc *CosmosProvider) GetAccount(clientCtx client.Context, addr sdk.AccAddress) (client.Account, error) {
account, _, err := cc.GetAccountWithHeight(clientCtx, addr)
return account, err
}

// GetAccountWithHeight queries for an account given an address. Returns the
// height of the query with the account. An error is returned if the query
// or decoding fails.
func (cc *CosmosProvider) GetAccountWithHeight(_ client.Context, addr sdk.AccAddress) (client.Account, int64, error) {
var header metadata.MD
address, err := cc.EncodeBech32AccAddr(addr)
if err != nil {
return nil, 0, err
}

queryClient := authtypes.NewQueryClient(cc)
res, err := queryClient.Account(context.Background(), &authtypes.QueryAccountRequest{Address: address}, grpc.Header(&header))
if err != nil {
return nil, 0, err
}

blockHeight := header.Get(grpctypes.GRPCBlockHeightHeader)
if l := len(blockHeight); l != 1 {
return nil, 0, fmt.Errorf("unexpected '%s' header length; got %d, expected: %d", grpctypes.GRPCBlockHeightHeader, l, 1)
}

nBlockHeight, err := strconv.Atoi(blockHeight[0])
if err != nil {
return nil, 0, fmt.Errorf("failed to parse block height: %w", err)
}

var acc sdk.AccountI
if err := cc.Cdc.InterfaceRegistry.UnpackAny(res.Account, &acc); err != nil {
return nil, 0, err
}

return acc, int64(nBlockHeight), nil
}

// EnsureExists returns an error if no account exists for the given address else nil.
func (cc *CosmosProvider) EnsureExists(clientCtx client.Context, addr sdk.AccAddress) error {
if _, err := cc.GetAccount(clientCtx, addr); err != nil {
return err
}
return nil
}

// GetAccountNumberSequence returns sequence and account number for the given address.
// It returns an error if the account couldn't be retrieved from the state.
func (cc *CosmosProvider) GetAccountNumberSequence(clientCtx client.Context, addr sdk.AccAddress) (uint64, uint64, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

can we make a function that increases the account sequence also in the WalletState

acc, err := cc.GetAccount(clientCtx, addr)
if err != nil {
return 0, 0, err
}
return acc.GetAccountNumber(), acc.GetSequence(), nil
}
31 changes: 31 additions & 0 deletions client/babylonclient/bech32_hack.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// This file is derived from the Cosmos Relayer repository (https://github.com/cosmos/relayer),
// originally licensed under the Apache License, Version 2.0.

package babylonclient

import (
"sync"

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

// This file is cursed and this mutex is too
// you don't want none of this dewey cox.
var sdkConfigMutex sync.Mutex

// SetSDKContext sets the SDK config to the proper bech32 prefixes.
// Don't use this unless you know what you're doing.
// TODO: :dagger: :knife: :chainsaw: remove this function
func (cc *CosmosProvider) SetSDKContext() func() {
return SetSDKConfigContext(cc.PCfg.AccountPrefix)
}

// SetSDKConfigContext sets the SDK config to the given bech32 prefixes
func SetSDKConfigContext(prefix string) func() {
sdkConfigMutex.Lock()
sdkConf := sdk.GetConfig()
sdkConf.SetBech32PrefixForAccount(prefix, prefix+"pub")
sdkConf.SetBech32PrefixForValidator(prefix+"valoper", prefix+"valoperpub")
sdkConf.SetBech32PrefixForConsensusNode(prefix+"valcons", prefix+"valconspub")
return sdkConfigMutex.Unlock
}
58 changes: 58 additions & 0 deletions client/babylonclient/chain_provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// This file is derived from the Cosmos Relayer repository (https://github.com/cosmos/relayer),
// originally licensed under the Apache License, Version 2.0.

package babylonclient

import (
"context"
)

type BroadcastMode string

const (
BroadcastModeSingle BroadcastMode = "single"
BroadcastModeBatch BroadcastMode = "batch"
)

type ProviderConfig interface {
NewProvider(homepath string, chainName string) (ChainProvider, error)
Validate() error
BroadcastMode() BroadcastMode
}

type RelayerMessage interface {
Type() string
MsgBytes() ([]byte, error)
}

type RelayerTxResponse struct {
Height int64
TxHash string
Codespace string
Code uint32
Data string
Events []RelayerEvent
}

type RelayerEvent struct {
EventType string
Attributes map[string]string
}

type ChainProvider interface {
Init() error
SendMessagesToMempool(
ctx context.Context,
msgs []RelayerMessage,
memo string,
asyncCtx context.Context,
asyncCallbacks []func(*RelayerTxResponse, error),
) error
ChainName() string
ChainId() string
ProviderConfig() ProviderConfig
Key() string
Address() (string, error)
Timeout() string
SetRpcAddr(rpcAddr string) error
}
111 changes: 111 additions & 0 deletions client/babylonclient/client_wrapper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// This file is derived from the Cosmos Relayer repository (https://github.com/cosmos/relayer),
// originally licensed under the Apache License, Version 2.0.

package babylonclient

import (
"context"
"github.com/cometbft/cometbft/libs/bytes"
rpcclient "github.com/cometbft/cometbft/rpc/client"
coretypes "github.com/cometbft/cometbft/rpc/core/types"
tmtypes "github.com/cometbft/cometbft/types"
)

type RPCClient struct {
c rpcclient.Client
}

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

func (r RPCClient) ABCIInfo(ctx context.Context) (*coretypes.ResultABCIInfo, error) {
return r.c.ABCIInfo(ctx)
}

func (r RPCClient) ABCIQuery(
ctx context.Context,
path string,
data bytes.HexBytes,
) (*coretypes.ResultABCIQuery, error) {
return r.c.ABCIQuery(ctx, path, (data))
}

func (r RPCClient) ABCIQueryWithOptions(
ctx context.Context,
path string,
data bytes.HexBytes,
opts rpcclient.ABCIQueryOptions,
) (*coretypes.ResultABCIQuery, error) {
return r.c.ABCIQueryWithOptions(ctx, path, data, opts)
}

func (r RPCClient) BroadcastTxCommit(ctx context.Context, tx tmtypes.Tx) (*coretypes.ResultBroadcastTxCommit, error) {
return r.c.BroadcastTxCommit(ctx, tx)
}

func (r RPCClient) BroadcastTxAsync(ctx context.Context, tx tmtypes.Tx) (*coretypes.ResultBroadcastTx, error) {
return r.c.BroadcastTxAsync(ctx, tx)
}

func (r RPCClient) BroadcastTxSync(ctx context.Context, tx tmtypes.Tx) (*coretypes.ResultBroadcastTx, error) {
return r.c.BroadcastTxSync(ctx, tx)
}

func (r RPCClient) Validators(
ctx context.Context,
height *int64,
page, perPage *int,
) (*coretypes.ResultValidators, error) {
return r.c.Validators(ctx, height, page, perPage)
}

func (r RPCClient) Status(ctx context.Context) (*coretypes.ResultStatus, error) {
return r.c.Status(ctx)
}

func (r RPCClient) Block(ctx context.Context, height *int64) (*coretypes.ResultBlock, error) {
return r.c.Block(ctx, height)
}

func (r RPCClient) BlockByHash(ctx context.Context, hash []byte) (*coretypes.ResultBlock, error) {
return r.c.BlockByHash(ctx, hash)
}

func (r RPCClient) BlockResults(ctx context.Context, height *int64) (*coretypes.ResultBlockResults, error) {
return r.c.BlockResults(ctx, height)
}

func (r RPCClient) BlockchainInfo(
ctx context.Context,
minHeight, maxHeight int64,
) (*coretypes.ResultBlockchainInfo, error) {
return r.c.BlockchainInfo(ctx, minHeight, maxHeight)
}

func (r RPCClient) Commit(ctx context.Context, height *int64) (*coretypes.ResultCommit, error) {
return r.c.Commit(ctx, height)
}

func (r RPCClient) Tx(ctx context.Context, hash []byte, prove bool) (*coretypes.ResultTx, error) {
return r.c.Tx(ctx, hash, prove)
}

func (r RPCClient) TxSearch(
ctx context.Context,
query string,
prove bool,
page, perPage *int,
orderBy string,
) (*coretypes.ResultTxSearch, error) {
return r.c.TxSearch(ctx, query, prove, page, perPage, orderBy)
}

func (r RPCClient) BlockSearch(
ctx context.Context,
query string,
page, perPage *int,
orderBy string,
) (*coretypes.ResultBlockSearch, error) {
return r.c.BlockSearch(ctx, query, page, perPage, orderBy)
}
Loading
Loading