Skip to content

Commit

Permalink
feat: add common rpc package (#2788)
Browse files Browse the repository at this point in the history
* feat: add common rpc package

* review feedback

* update e2e

* port zetacore high level helpers and tests

* embed rpc.Clients in zetaclient/zetacored.Client

* fmt

* fix e2e lint

* changelog
  • Loading branch information
gartnera authored Aug 29, 2024
1 parent 994f1ca commit 91c323d
Show file tree
Hide file tree
Showing 20 changed files with 545 additions and 461 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* [2644](https://github.com/zeta-chain/node/pull/2644) - add created_timestamp to cctx status
* [2673](https://github.com/zeta-chain/node/pull/2673) - add relayer key importer, encryption and decryption
* [2633](https://github.com/zeta-chain/node/pull/2633) - support for stateful precompiled contracts.
* [2788](https://github.com/zeta-chain/node/pull/2788) - add common importable zetacored rpc package

### Refactor

Expand Down
116 changes: 27 additions & 89 deletions cmd/zetae2e/config/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,99 +5,53 @@ import (
"fmt"

"github.com/btcsuite/btcd/rpcclient"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/gagliardetto/solana-go/rpc"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"

"github.com/zeta-chain/zetacore/e2e/config"
authoritytypes "github.com/zeta-chain/zetacore/x/authority/types"
crosschaintypes "github.com/zeta-chain/zetacore/x/crosschain/types"
fungibletypes "github.com/zeta-chain/zetacore/x/fungible/types"
lightclienttypes "github.com/zeta-chain/zetacore/x/lightclient/types"
observertypes "github.com/zeta-chain/zetacore/x/observer/types"
"github.com/zeta-chain/zetacore/e2e/runner"
zetacore_rpc "github.com/zeta-chain/zetacore/pkg/rpc"
)

// E2EClients contains all the RPC clients and gRPC clients for E2E tests
type E2EClients struct {
// the RPC clients for external chains in the localnet
BtcRPCClient *rpcclient.Client
SolanaClient *rpc.Client
EvmClient *ethclient.Client
EvmAuth *bind.TransactOpts

// the gRPC clients for ZetaChain
AuthorityClient authoritytypes.QueryClient
CctxClient crosschaintypes.QueryClient
FungibleClient fungibletypes.QueryClient
AuthClient authtypes.QueryClient
BankClient banktypes.QueryClient
ObserverClient observertypes.QueryClient
LightClient lightclienttypes.QueryClient

// the RPC clients for ZetaChain
ZevmClient *ethclient.Client
ZevmAuth *bind.TransactOpts
}

// ZetaChainClients contains all the RPC clients and gRPC clients for ZetaChain
type ZetaChainClients struct {
AuthorityClient authoritytypes.QueryClient
CctxClient crosschaintypes.QueryClient
FungibleClient fungibletypes.QueryClient
AuthClient authtypes.QueryClient
BankClient banktypes.QueryClient
ObserverClient observertypes.QueryClient
LightClient lightclienttypes.QueryClient
}

// getClientsFromConfig get clients from config
func getClientsFromConfig(ctx context.Context, conf config.Config, account config.Account) (
E2EClients,
runner.Clients,
error,
) {
var solanaClient *rpc.Client
if conf.RPCs.Solana != "" {
if solanaClient = rpc.New(conf.RPCs.Solana); solanaClient == nil {
return E2EClients{}, fmt.Errorf("failed to get solana client")
return runner.Clients{}, fmt.Errorf("failed to get solana client")
}
}
btcRPCClient, err := getBtcClient(conf.RPCs.Bitcoin)
if err != nil {
return E2EClients{}, fmt.Errorf("failed to get btc client: %w", err)
return runner.Clients{}, fmt.Errorf("failed to get btc client: %w", err)
}
evmClient, evmAuth, err := getEVMClient(ctx, conf.RPCs.EVM, account)
if err != nil {
return E2EClients{}, fmt.Errorf("failed to get evm client: %w", err)
return runner.Clients{}, fmt.Errorf("failed to get evm client: %w", err)
}
zetaChainClients, err := GetZetaClients(
conf.RPCs.ZetaCoreGRPC,
)
zetaCoreClients, err := GetZetacoreClient(conf)
if err != nil {
return E2EClients{}, fmt.Errorf("failed to get zeta clients: %w", err)
return runner.Clients{}, fmt.Errorf("failed to get zetacore client: %w", err)
}
zevmClient, zevmAuth, err := getEVMClient(ctx, conf.RPCs.Zevm, account)
if err != nil {
return E2EClients{}, fmt.Errorf("failed to get zevm client: %w", err)
return runner.Clients{}, fmt.Errorf("failed to get zevm client: %w", err)
}

return E2EClients{
BtcRPCClient: btcRPCClient,
SolanaClient: solanaClient,
EvmClient: evmClient,
EvmAuth: evmAuth,
AuthorityClient: zetaChainClients.AuthorityClient,
CctxClient: zetaChainClients.CctxClient,
FungibleClient: zetaChainClients.FungibleClient,
AuthClient: zetaChainClients.AuthClient,
BankClient: zetaChainClients.BankClient,
ObserverClient: zetaChainClients.ObserverClient,
LightClient: zetaChainClients.LightClient,
ZevmClient: zevmClient,
ZevmAuth: zevmAuth,
return runner.Clients{
Zetacore: zetaCoreClients,
BtcRPC: btcRPCClient,
Solana: solanaClient,
Evm: evmClient,
EvmAuth: evmAuth,
Zevm: zevmClient,
ZevmAuth: zevmAuth,
}, nil
}

Expand Down Expand Up @@ -152,31 +106,15 @@ func getEVMClient(
return evmClient, evmAuth, nil
}

// GetZetaClients get zeta clients
func GetZetaClients(rpc string) (
ZetaChainClients,
error,
) {
grpcConn, err := grpc.Dial(rpc, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
return ZetaChainClients{}, err
func GetZetacoreClient(conf config.Config) (zetacore_rpc.Clients, error) {
if conf.RPCs.ZetaCoreGRPC != "" {
return zetacore_rpc.NewGRPCClients(
conf.RPCs.ZetaCoreGRPC,
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
}

authorityClient := authoritytypes.NewQueryClient(grpcConn)
cctxClient := crosschaintypes.NewQueryClient(grpcConn)
fungibleClient := fungibletypes.NewQueryClient(grpcConn)
authClient := authtypes.NewQueryClient(grpcConn)
bankClient := banktypes.NewQueryClient(grpcConn)
observerClient := observertypes.NewQueryClient(grpcConn)
lightclientClient := lightclienttypes.NewQueryClient(grpcConn)

return ZetaChainClients{
AuthorityClient: authorityClient,
CctxClient: cctxClient,
FungibleClient: fungibleClient,
AuthClient: authClient,
BankClient: bankClient,
ObserverClient: observerClient,
LightClient: lightclientClient,
}, nil
if conf.RPCs.ZetaCoreRPC != "" {
return zetacore_rpc.NewCometBFTClients(conf.RPCs.ZetaCoreRPC)
}
return zetacore_rpc.Clients{}, fmt.Errorf("no ZetaCore gRPC or RPC specified")
}
14 changes: 1 addition & 13 deletions cmd/zetae2e/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,7 @@ func RunnerFromConfig(
name,
ctxCancel,
account,
e2eClients.EvmClient,
e2eClients.ZevmClient,
e2eClients.AuthorityClient,
e2eClients.CctxClient,
e2eClients.FungibleClient,
e2eClients.AuthClient,
e2eClients.BankClient,
e2eClients.ObserverClient,
e2eClients.LightClient,
e2eClients.EvmAuth,
e2eClients.ZevmAuth,
e2eClients.BtcRPCClient,
e2eClients.SolanaClient,
e2eClients,

logger,
opts...,
Expand Down
2 changes: 1 addition & 1 deletion cmd/zetae2e/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func localE2ETest(cmd *cobra.Command, _ []string) {
noError(err)

// set the authority client to the zeta tx server to be able to query message permissions
deployerRunner.ZetaTxServer.SetAuthorityClient(deployerRunner.AutorithyClient)
deployerRunner.ZetaTxServer.SetAuthorityClient(deployerRunner.AuthorityClient)

// wait for keygen to be completed
// if setup is skipped, we assume that the keygen is already completed
Expand Down
10 changes: 5 additions & 5 deletions cmd/zetae2e/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func runE2ETest(cmd *cobra.Command, args []string) error {
if erc20ChainName != "" && erc20Symbol != "" {
erc20Asset, zrc20ContractAddress, err := findERC20(
cmd.Context(),
conf.RPCs.ZetaCoreGRPC,
conf,
erc20ChainName,
erc20Symbol,
)
Expand Down Expand Up @@ -178,13 +178,13 @@ func parseCmdArgsToE2ETestRunConfig(args []string) ([]runner.E2ETestRunConfig, e
}

// findERC20 loads ERC20 addresses via gRPC given CLI flags
func findERC20(ctx context.Context, zetaCoreGRPCURL, erc20ChainName, erc20Symbol string) (string, string, error) {
clients, err := zetae2econfig.GetZetaClients(zetaCoreGRPCURL)
func findERC20(ctx context.Context, conf config.Config, erc20ChainName, erc20Symbol string) (string, string, error) {
clients, err := zetae2econfig.GetZetacoreClient(conf)
if err != nil {
return "", "", fmt.Errorf("get zeta clients: %w", err)
}

supportedChainsRes, err := clients.ObserverClient.SupportedChains(ctx, &observertypes.QuerySupportedChains{})
supportedChainsRes, err := clients.Observer.SupportedChains(ctx, &observertypes.QuerySupportedChains{})
if err != nil {
return "", "", fmt.Errorf("get chain params: %w", err)
}
Expand All @@ -200,7 +200,7 @@ func findERC20(ctx context.Context, zetaCoreGRPCURL, erc20ChainName, erc20Symbol
return "", "", fmt.Errorf("chain %s not found", erc20ChainName)
}

foreignCoinsRes, err := clients.FungibleClient.ForeignCoinsAll(ctx, &fungibletypes.QueryAllForeignCoinsRequest{})
foreignCoinsRes, err := clients.Fungible.ForeignCoinsAll(ctx, &fungibletypes.QueryAllForeignCoinsRequest{})
if err != nil {
return "", "", fmt.Errorf("get foreign coins: %w", err)
}
Expand Down
14 changes: 1 addition & 13 deletions e2e/e2etests/test_migrate_chain_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,19 +201,7 @@ func configureEVM2(r *runner.E2ERunner) (*runner.E2ERunner, error) {
"admin-evm2",
r.CtxCancel,
r.Account,
r.EVMClient,
r.ZEVMClient,
r.AutorithyClient,
r.CctxClient,
r.FungibleClient,
r.AuthClient,
r.BankClient,
r.ObserverClient,
r.LightclientClient,
r.EVMAuth,
r.ZEVMAuth,
r.BtcRPCClient,
r.SolanaClient,
r.Clients,
runner.NewLogger(true, color.FgHiYellow, "admin-evm2"),
runner.WithZetaTxServer(r.ZetaTxServer),
)
Expand Down
25 changes: 25 additions & 0 deletions e2e/runner/clients.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package runner

import (
"github.com/btcsuite/btcd/rpcclient"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/gagliardetto/solana-go/rpc"

zetacore_rpc "github.com/zeta-chain/zetacore/pkg/rpc"
)

// Clients contains all the RPC clients and gRPC clients for E2E tests
type Clients struct {
Zetacore zetacore_rpc.Clients

// the RPC clients for external chains in the localnet
BtcRPC *rpcclient.Client
Solana *rpc.Client
Evm *ethclient.Client
EvmAuth *bind.TransactOpts

// the RPC clients for ZetaChain
Zevm *ethclient.Client
ZevmAuth *bind.TransactOpts
}
52 changes: 23 additions & 29 deletions e2e/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,18 @@ type E2ERunner struct {
BTCDeployerAddress *btcutil.AddressWitnessPubKeyHash
SolanaDeployerAddress solana.PublicKey

// all clients.
// a reference to this type is required to enable creating a new E2ERunner.
Clients Clients

// rpc clients
ZEVMClient *ethclient.Client
EVMClient *ethclient.Client
BtcRPCClient *rpcclient.Client
SolanaClient *rpc.Client

// grpc clients
AutorithyClient authoritytypes.QueryClient
// zetacored grpc clients
AuthorityClient authoritytypes.QueryClient
CctxClient crosschaintypes.QueryClient
FungibleClient fungibletypes.QueryClient
AuthClient authtypes.QueryClient
Expand Down Expand Up @@ -165,19 +169,7 @@ func NewE2ERunner(
name string,
ctxCancel context.CancelFunc,
account config.Account,
evmClient *ethclient.Client,
zevmClient *ethclient.Client,
authorityClient authoritytypes.QueryClient,
cctxClient crosschaintypes.QueryClient,
fungibleClient fungibletypes.QueryClient,
authClient authtypes.QueryClient,
bankClient banktypes.QueryClient,
observerClient observertypes.QueryClient,
lightclientClient lightclienttypes.QueryClient,
evmAuth *bind.TransactOpts,
zevmAuth *bind.TransactOpts,
btcRPCClient *rpcclient.Client,
solanaClient *rpc.Client,
clients Clients,
logger *Logger,
opts ...E2ERunnerOption,
) *E2ERunner {
Expand All @@ -187,20 +179,22 @@ func NewE2ERunner(

Account: account,

ZEVMClient: zevmClient,
EVMClient: evmClient,
AutorithyClient: authorityClient,
CctxClient: cctxClient,
FungibleClient: fungibleClient,
AuthClient: authClient,
BankClient: bankClient,
ObserverClient: observerClient,
LightclientClient: lightclientClient,

EVMAuth: evmAuth,
ZEVMAuth: zevmAuth,
BtcRPCClient: btcRPCClient,
SolanaClient: solanaClient,
Clients: clients,

ZEVMClient: clients.Zevm,
EVMClient: clients.Evm,
AuthorityClient: clients.Zetacore.Authority,
CctxClient: clients.Zetacore.Crosschain,
FungibleClient: clients.Zetacore.Fungible,
AuthClient: clients.Zetacore.Auth,
BankClient: clients.Zetacore.Bank,
ObserverClient: clients.Zetacore.Observer,
LightclientClient: clients.Zetacore.Lightclient,

EVMAuth: clients.EvmAuth,
ZEVMAuth: clients.ZevmAuth,
BtcRPCClient: clients.BtcRPC,
SolanaClient: clients.Solana,

Logger: logger,
}
Expand Down
Loading

0 comments on commit 91c323d

Please sign in to comment.