Skip to content

Commit

Permalink
Merge branch 'develop' into solana-hotfix-from-v19.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
ws4charlie authored Aug 29, 2024
2 parents 7f0b746 + 91c323d commit e49ff53
Show file tree
Hide file tree
Showing 22 changed files with 626 additions and 473 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
81 changes: 70 additions & 11 deletions cmd/zetae2e/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ import (
"github.com/zeta-chain/zetacore/e2e/config"
"github.com/zeta-chain/zetacore/e2e/e2etests"
"github.com/zeta-chain/zetacore/e2e/runner"
fungibletypes "github.com/zeta-chain/zetacore/x/fungible/types"
observertypes "github.com/zeta-chain/zetacore/x/observer/types"
)

const flagVerbose = "verbose"
const flagConfig = "config"
const flagERC20ChainName = "erc20-chain-name"
const flagERC20Symbol = "erc20-symbol"

// NewRunCmd returns the run command
// which runs the E2E from a config file describing the tests, networks, and accounts
Expand All @@ -41,6 +45,9 @@ For example: zetae2e run deposit:1000 withdraw: --config config.yml`,
os.Exit(1)
}

cmd.Flags().String(flagERC20ChainName, "", "chain_name from /zeta-chain/observer/supportedChains")
cmd.Flags().String(flagERC20Symbol, "", "symbol from /zeta-chain/fungible/foreign_coins")

// Retain the verbose flag
cmd.Flags().Bool(flagVerbose, false, "set to true to enable verbose logging")

Expand All @@ -67,6 +74,29 @@ func runE2ETest(cmd *cobra.Command, args []string) error {
// initialize logger
logger := runner.NewLogger(verbose, color.FgHiCyan, "e2e")

// update config with dynamic ERC20
erc20ChainName, err := cmd.Flags().GetString(flagERC20ChainName)
if err != nil {
return err
}
erc20Symbol, err := cmd.Flags().GetString(flagERC20Symbol)
if err != nil {
return err
}
if erc20ChainName != "" && erc20Symbol != "" {
erc20Asset, zrc20ContractAddress, err := findERC20(
cmd.Context(),
conf,
erc20ChainName,
erc20Symbol,
)
if err != nil {
return err
}
conf.Contracts.EVM.ERC20 = config.DoubleQuotedString(erc20Asset)
conf.Contracts.ZEVM.ERC20ZRC20Addr = config.DoubleQuotedString(zrc20ContractAddress)
}

// set config
app.SetConfig()

Expand Down Expand Up @@ -99,11 +129,6 @@ func runE2ETest(cmd *cobra.Command, args []string) error {
testRunner.CctxTimeout = 60 * time.Minute
testRunner.ReceiptTimeout = 60 * time.Minute

balancesBefore, err := testRunner.GetAccountBalances(true)
if err != nil {
return err
}

// parse test names and arguments from cmd args and run them
userTestsConfigs, err := parseCmdArgsToE2ETestRunConfig(args)
if err != nil {
Expand All @@ -119,15 +144,9 @@ func runE2ETest(cmd *cobra.Command, args []string) error {
return err
}

balancesAfter, err := testRunner.GetAccountBalances(true)
if err != nil {
return err
}

// Print tests completion info
logger.Print("tests finished successfully in %s", time.Since(testStartTime).String())
testRunner.Logger.SetColor(color.FgHiRed)
testRunner.PrintTotalDiff(runner.GetAccountBalancesDiff(balancesBefore, balancesAfter))
testRunner.Logger.SetColor(color.FgHiGreen)
testRunner.PrintTestReports(reports)

Expand Down Expand Up @@ -157,3 +176,43 @@ func parseCmdArgsToE2ETestRunConfig(args []string) ([]runner.E2ETestRunConfig, e
}
return tests, nil
}

// findERC20 loads ERC20 addresses via gRPC given CLI flags
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.Observer.SupportedChains(ctx, &observertypes.QuerySupportedChains{})
if err != nil {
return "", "", fmt.Errorf("get chain params: %w", err)
}

chainID := int64(0)
for _, chain := range supportedChainsRes.Chains {
if chain.Name == erc20ChainName {
chainID = chain.ChainId
break
}
}
if chainID == 0 {
return "", "", fmt.Errorf("chain %s not found", erc20ChainName)
}

foreignCoinsRes, err := clients.Fungible.ForeignCoinsAll(ctx, &fungibletypes.QueryAllForeignCoinsRequest{})
if err != nil {
return "", "", fmt.Errorf("get foreign coins: %w", err)
}

for _, coin := range foreignCoinsRes.ForeignCoins {
if coin.ForeignChainId != chainID {
continue
}
// sometimes symbol is USDT, sometimes it's like USDT.SEPOLIA
if strings.HasPrefix(coin.Symbol, erc20Symbol) || strings.HasSuffix(coin.Symbol, erc20Symbol) {
return coin.Asset, coin.Zrc20ContractAddress, nil
}
}
return "", "", fmt.Errorf("erc20 %s not found on %s", erc20Symbol, erc20ChainName)
}
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
20 changes: 15 additions & 5 deletions e2e/runner/balances.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ import (
"github.com/btcsuite/btcutil"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/pkg/errors"
"github.com/zeta-chain/protocol-contracts/v2/pkg/zrc20.sol"
)

var errNilZRC20 = errors.New("zrc20 contract is nil")

// AccountBalances is a struct that contains the balances of the accounts used in the E2E test
type AccountBalances struct {
ZetaETH *big.Int
Expand All @@ -31,6 +34,13 @@ type AccountBalancesDiff struct {
ERC20 *big.Int
}

func (r *E2ERunner) getZRC20BalanceSafe(z *zrc20.ZRC20) (*big.Int, error) {
if z == nil {
return new(big.Int), errNilZRC20
}
return z.BalanceOf(&bind.CallOpts{}, r.EVMAddress())
}

// GetAccountBalances returns the account balances of the accounts used in the E2E test
func (r *E2ERunner) GetAccountBalances(skipBTC bool) (AccountBalances, error) {
// zevm
Expand All @@ -42,21 +52,21 @@ func (r *E2ERunner) GetAccountBalances(skipBTC bool) (AccountBalances, error) {
if err != nil {
return AccountBalances{}, err
}
zetaEth, err := r.ETHZRC20.BalanceOf(&bind.CallOpts{}, r.EVMAddress())
zetaEth, err := r.getZRC20BalanceSafe(r.ETHZRC20)
if err != nil {
return AccountBalances{}, err
}
zetaErc20, err := r.ERC20ZRC20.BalanceOf(&bind.CallOpts{}, r.EVMAddress())
zetaErc20, err := r.getZRC20BalanceSafe(r.ERC20ZRC20)
if err != nil {
return AccountBalances{}, err
}
zetaBtc, err := r.BTCZRC20.BalanceOf(&bind.CallOpts{}, r.EVMAddress())
zetaBtc, err := r.getZRC20BalanceSafe(r.BTCZRC20)
if err != nil {
return AccountBalances{}, err
}
zetaSol, err := r.SOLZRC20.BalanceOf(&bind.CallOpts{}, r.EVMAddress())
zetaSol, err := r.getZRC20BalanceSafe(r.SOLZRC20)
if err != nil {
return AccountBalances{}, err
r.Logger.Error("get SOL balance: %v", err)
}

// evm
Expand Down
Loading

0 comments on commit e49ff53

Please sign in to comment.