-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: remove
cosmos/relayer
dependency (#429)
- Migrates the minimal amount of code needed from the [relayer](https://github.com/cosmos/relayer) repo - Removes dependency for `cosmos/relayer` - Removes dependency for `strangelove-ventures/cometbft-client` ~Todo:~ - ~As this is modified code from the [relayer](https://github.com/cosmos/relayer), what license if any should we include in the header of those files? I've also modified them (deleted a bunch of stuff, and added more code quality improvements)~ - ~Is feegrant.go needed, feels like it can be removed and other parts related to it~ - ~Future PRs can remove references to strangelove cometbft client, tho we do have to satisfy the `CometRPC interface`, `ABCIClient interface` etc. (not sure if it's worth the extra effort)~ - ~Decide on the name of the relayer package, the clients would currently use `relayerclient.Whatever`~ [Closes](babylonlabs-io/pm#149)
- Loading branch information
Showing
17 changed files
with
1,523 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
acc, err := cc.GetAccount(clientCtx, addr) | ||
if err != nil { | ||
return 0, 0, err | ||
} | ||
return acc.GetAccountNumber(), acc.GetSequence(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.