Skip to content

Commit

Permalink
refactor(e2e): move addresses and private keys to config (#2308)
Browse files Browse the repository at this point in the history
* refactor(e2e): move all addresses and private keys to config

* localnet config and inject into orchestrator

* zetacored from config

* generate keys in init

* fmt

* docs

* more lint fixes

* s/deployer/default_account/

* trim prefix
  • Loading branch information
gartnera authored Jun 27, 2024
1 parent 47d835a commit 98c63b9
Show file tree
Hide file tree
Showing 58 changed files with 631 additions and 408 deletions.
3 changes: 2 additions & 1 deletion Dockerfile-localnet
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ RUN go install cosmossdk.io/tools/cosmovisor/cmd/[email protected]
FROM golang:1.20.14-bookworm AS base-runtime

RUN apt update && \
apt install -yq jq curl tmux python3 openssh-server iputils-ping iproute2 && \
apt install -yq jq yq curl tmux python3 openssh-server iputils-ping iproute2 && \
rm -rf /var/lib/apt/lists/*

RUN ssh-keygen -A && \
Expand All @@ -46,6 +46,7 @@ ENV PATH /root/.zetacored/cosmovisor/current/bin/:/root/.zetaclientd/upgrades/cu
COPY contrib/localnet/scripts /root
COPY contrib/localnet/ssh_config /etc/ssh/ssh_config.d/localnet.conf
COPY contrib/localnet/zetacored /root/zetacored
COPY cmd/zetae2e/config/localnet.yml /root/config.yml

RUN chmod 755 /root/*.sh && \
chmod 644 /etc/ssh/ssh_config.d/localnet.conf
Expand Down
8 changes: 4 additions & 4 deletions cmd/zetae2e/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ This is an example of config for interaction with Athens3:

```go
zeta_chain_id: "athens_7001-1"
accounts:
default_account:
evm_address: "<your evm address>"
evm_priv_key: "<your evm private key>"
private_key: "<your evm private key>"
rpcs:
zevm: "<zevm (ZetaChain) url>, generally using port 8545"
evm: "<evm url>, generally using port 8545"
Expand Down Expand Up @@ -101,9 +101,9 @@ Testing a gas token requires the following values to be defined in the config:

```go
zeta_chain_id
accounts:
default_account:
evm_address
evm_priv_key
private_key
rpcs:
zevm
evm
Expand Down
12 changes: 1 addition & 11 deletions cmd/zetae2e/balances.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package main

import (
"context"
"errors"

ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/fatih/color"
"github.com/spf13/cobra"

Expand Down Expand Up @@ -54,21 +52,13 @@ func runBalances(cmd *cobra.Command, args []string) error {
// initialize context
ctx, cancel := context.WithCancel(context.Background())

// get EVM address from config
evmAddr := conf.Accounts.EVMAddress
if !ethcommon.IsHexAddress(evmAddr) {
cancel()
return errors.New("invalid EVM address")
}

// initialize deployer runner with config
r, err := zetae2econfig.RunnerFromConfig(
ctx,
"e2e",
cancel,
conf,
ethcommon.HexToAddress(evmAddr),
conf.Accounts.EVMPrivKey,
conf.DefaultAccount,
logger,
)
if err != nil {
Expand Down
12 changes: 1 addition & 11 deletions cmd/zetae2e/bitcoin_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package main

import (
"context"
"errors"

ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/fatih/color"
"github.com/spf13/cobra"

Expand Down Expand Up @@ -54,21 +52,13 @@ func runBitcoinAddress(cmd *cobra.Command, args []string) error {
// initialize context
ctx, cancel := context.WithCancel(context.Background())

// get EVM address from config
evmAddr := conf.Accounts.EVMAddress
if !ethcommon.IsHexAddress(evmAddr) {
cancel()
return errors.New("invalid EVM address")
}

// initialize deployer runner with config
r, err := zetae2econfig.RunnerFromConfig(
ctx,
"e2e",
cancel,
conf,
ethcommon.HexToAddress(evmAddr),
conf.Accounts.EVMPrivKey,
conf.DefaultAccount,
logger,
)
if err != nil {
Expand Down
17 changes: 10 additions & 7 deletions cmd/zetae2e/config/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
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/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"google.golang.org/grpc"

Expand All @@ -20,7 +19,7 @@ import (
)

// getClientsFromConfig get clients from config
func getClientsFromConfig(ctx context.Context, conf config.Config, evmPrivKey string) (
func getClientsFromConfig(ctx context.Context, conf config.Config, account config.Account) (
*rpcclient.Client,
*ethclient.Client,
*bind.TransactOpts,
Expand All @@ -38,7 +37,7 @@ func getClientsFromConfig(ctx context.Context, conf config.Config, evmPrivKey st
if err != nil {
return nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, fmt.Errorf("failed to get btc client: %w", err)
}
evmClient, evmAuth, err := getEVMClient(ctx, conf.RPCs.EVM, evmPrivKey)
evmClient, evmAuth, err := getEVMClient(ctx, conf.RPCs.EVM, account)
if err != nil {
return nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, fmt.Errorf("failed to get evm client: %w", err)
}
Expand All @@ -48,7 +47,7 @@ func getClientsFromConfig(ctx context.Context, conf config.Config, evmPrivKey st
if err != nil {
return nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, fmt.Errorf("failed to get zeta clients: %w", err)
}
zevmClient, zevmAuth, err := getEVMClient(ctx, conf.RPCs.Zevm, evmPrivKey)
zevmClient, zevmAuth, err := getEVMClient(ctx, conf.RPCs.Zevm, account)
if err != nil {
return nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, fmt.Errorf("failed to get zevm client: %w", err)
}
Expand Down Expand Up @@ -91,7 +90,11 @@ func getBtcClient(rpcConf config.BitcoinRPC) (*rpcclient.Client, error) {
}

// getEVMClient get evm client
func getEVMClient(ctx context.Context, rpc, privKey string) (*ethclient.Client, *bind.TransactOpts, error) {
func getEVMClient(
ctx context.Context,
rpc string,
account config.Account,
) (*ethclient.Client, *bind.TransactOpts, error) {
evmClient, err := ethclient.Dial(rpc)
if err != nil {
return nil, nil, fmt.Errorf("failed to dial evm client: %w", err)
Expand All @@ -101,11 +104,11 @@ func getEVMClient(ctx context.Context, rpc, privKey string) (*ethclient.Client,
if err != nil {
return nil, nil, fmt.Errorf("failed to get chain id: %w", err)
}
deployerPrivkey, err := crypto.HexToECDSA(privKey)
privKey, err := account.PrivateKey()
if err != nil {
return nil, nil, fmt.Errorf("failed to get deployer privkey: %w", err)
}
evmAuth, err := bind.NewKeyedTransactorWithChainID(deployerPrivkey, chainid)
evmAuth, err := bind.NewKeyedTransactorWithChainID(privKey, chainid)
if err != nil {
return nil, nil, fmt.Errorf("failed to get keyed transactor: %w", err)
}
Expand Down
42 changes: 19 additions & 23 deletions cmd/zetae2e/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"context"
"fmt"

ethcommon "github.com/ethereum/go-ethereum/common"

"github.com/zeta-chain/zetacore/e2e/config"
"github.com/zeta-chain/zetacore/e2e/runner"
)
Expand All @@ -16,8 +14,7 @@ func RunnerFromConfig(
name string,
ctxCancel context.CancelFunc,
conf config.Config,
evmUserAddr ethcommon.Address,
evmUserPrivKey string,
account config.Account,
logger *runner.Logger,
opts ...runner.E2ERunnerOption,
) (*runner.E2ERunner, error) {
Expand All @@ -33,7 +30,7 @@ func RunnerFromConfig(
lightClient,
zevmClient,
zevmAuth,
err := getClientsFromConfig(ctx, conf, evmUserPrivKey)
err := getClientsFromConfig(ctx, conf, account)
if err != nil {
return nil, fmt.Errorf("failed to get clients from config: %w", err)
}
Expand All @@ -43,8 +40,7 @@ func RunnerFromConfig(
ctx,
name,
ctxCancel,
evmUserAddr,
evmUserPrivKey,
account,
evmClient,
zevmClient,
cctxClient,
Expand Down Expand Up @@ -79,23 +75,23 @@ func RunnerFromConfig(
// ExportContractsFromRunner export contracts from the runner to config using a source config
func ExportContractsFromRunner(r *runner.E2ERunner, conf config.Config) config.Config {
// copy contracts from deployer runner
conf.Contracts.EVM.ZetaEthAddress = r.ZetaEthAddr.Hex()
conf.Contracts.EVM.ConnectorEthAddr = r.ConnectorEthAddr.Hex()
conf.Contracts.EVM.CustodyAddr = r.ERC20CustodyAddr.Hex()
conf.Contracts.EVM.ERC20 = r.ERC20Addr.Hex()
conf.Contracts.EVM.TestDappAddr = r.EvmTestDAppAddr.Hex()
conf.Contracts.EVM.ZetaEthAddr = config.DoubleQuotedString(r.ZetaEthAddr.Hex())
conf.Contracts.EVM.ConnectorEthAddr = config.DoubleQuotedString(r.ConnectorEthAddr.Hex())
conf.Contracts.EVM.CustodyAddr = config.DoubleQuotedString(r.ERC20CustodyAddr.Hex())
conf.Contracts.EVM.ERC20 = config.DoubleQuotedString(r.ERC20Addr.Hex())
conf.Contracts.EVM.TestDappAddr = config.DoubleQuotedString(r.EvmTestDAppAddr.Hex())

conf.Contracts.ZEVM.SystemContractAddr = r.SystemContractAddr.Hex()
conf.Contracts.ZEVM.ETHZRC20Addr = r.ETHZRC20Addr.Hex()
conf.Contracts.ZEVM.ERC20ZRC20Addr = r.ERC20ZRC20Addr.Hex()
conf.Contracts.ZEVM.BTCZRC20Addr = r.BTCZRC20Addr.Hex()
conf.Contracts.ZEVM.UniswapFactoryAddr = r.UniswapV2FactoryAddr.Hex()
conf.Contracts.ZEVM.UniswapRouterAddr = r.UniswapV2RouterAddr.Hex()
conf.Contracts.ZEVM.ConnectorZEVMAddr = r.ConnectorZEVMAddr.Hex()
conf.Contracts.ZEVM.WZetaAddr = r.WZetaAddr.Hex()
conf.Contracts.ZEVM.ZEVMSwapAppAddr = r.ZEVMSwapAppAddr.Hex()
conf.Contracts.ZEVM.ContextAppAddr = r.ContextAppAddr.Hex()
conf.Contracts.ZEVM.TestDappAddr = r.ZevmTestDAppAddr.Hex()
conf.Contracts.ZEVM.SystemContractAddr = config.DoubleQuotedString(r.SystemContractAddr.Hex())
conf.Contracts.ZEVM.ETHZRC20Addr = config.DoubleQuotedString(r.ETHZRC20Addr.Hex())
conf.Contracts.ZEVM.ERC20ZRC20Addr = config.DoubleQuotedString(r.ERC20ZRC20Addr.Hex())
conf.Contracts.ZEVM.BTCZRC20Addr = config.DoubleQuotedString(r.BTCZRC20Addr.Hex())
conf.Contracts.ZEVM.UniswapFactoryAddr = config.DoubleQuotedString(r.UniswapV2FactoryAddr.Hex())
conf.Contracts.ZEVM.UniswapRouterAddr = config.DoubleQuotedString(r.UniswapV2RouterAddr.Hex())
conf.Contracts.ZEVM.ConnectorZEVMAddr = config.DoubleQuotedString(r.ConnectorZEVMAddr.Hex())
conf.Contracts.ZEVM.WZetaAddr = config.DoubleQuotedString(r.WZetaAddr.Hex())
conf.Contracts.ZEVM.ZEVMSwapAppAddr = config.DoubleQuotedString(r.ZEVMSwapAppAddr.Hex())
conf.Contracts.ZEVM.ContextAppAddr = config.DoubleQuotedString(r.ContextAppAddr.Hex())
conf.Contracts.ZEVM.TestDappAddr = config.DoubleQuotedString(r.ZevmTestDAppAddr.Hex())

return conf
}
40 changes: 40 additions & 0 deletions cmd/zetae2e/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package config

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/zeta-chain/zetacore/e2e/config"
)

func TestReadConfig(t *testing.T) {
tests := []struct {
name string
filePath string
expectNoError bool
expectNotEmpty bool
}{
{
name: "ReadLocalnetConfig",
filePath: "localnet.yml",
expectNoError: true,
expectNotEmpty: true,
},
{
name: "ReadLocalConfig",
filePath: "local.yml",
expectNoError: true,
expectNotEmpty: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
conf, err := config.ReadConfig(tt.filePath)
require.NoError(t, err)
require.NotEmpty(t, conf.DefaultAccount.RawEVMAddress)
require.NotEmpty(t, conf.DefaultAccount.RawPrivateKey)
})
}
}
Loading

0 comments on commit 98c63b9

Please sign in to comment.