2023-02-22 23:40:56 +00:00
|
|
|
package testutil
|
|
|
|
|
|
|
|
import (
|
2023-03-02 01:05:53 +00:00
|
|
|
"context"
|
2023-02-22 23:40:56 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/client/grpc/tmservice"
|
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/hd"
|
2023-03-02 01:05:53 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2023-02-22 23:40:56 +00:00
|
|
|
txtypes "github.com/cosmos/cosmos-sdk/types/tx"
|
|
|
|
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
|
|
|
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
|
|
|
|
"github.com/ethereum/go-ethereum/ethclient"
|
|
|
|
|
|
|
|
"github.com/kava-labs/kava/app"
|
|
|
|
kavaparams "github.com/kava-labs/kava/app/params"
|
|
|
|
"github.com/kava-labs/kava/tests/e2e/runner"
|
|
|
|
"github.com/kava-labs/kava/tests/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
ChainId = "kavalocalnet_8888-1"
|
|
|
|
FundedAccountName = "whale"
|
|
|
|
StakingDenom = "ukava"
|
2023-03-02 01:05:53 +00:00
|
|
|
// use coin type 60 so we are compatible with accounts from `kava add keys --eth <name>`
|
|
|
|
// these accounts use the ethsecp256k1 signing algorithm that allows the signing client
|
|
|
|
// to manage both sdk & evm txs.
|
|
|
|
Bip44CoinType = 60
|
2023-02-22 23:40:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type E2eTestSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
|
|
|
|
runner runner.NodeRunner
|
|
|
|
grpcConn *grpc.ClientConn
|
|
|
|
encodingConfig kavaparams.EncodingConfig
|
|
|
|
|
|
|
|
EvmClient *ethclient.Client
|
|
|
|
Auth authtypes.QueryClient
|
|
|
|
Bank banktypes.QueryClient
|
|
|
|
Tm tmservice.ServiceClient
|
|
|
|
Tx txtypes.ServiceClient
|
|
|
|
|
|
|
|
accounts map[string]*SigningAccount
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *E2eTestSuite) SetupSuite() {
|
2023-03-02 01:05:53 +00:00
|
|
|
fmt.Println("setting up test suite.")
|
2023-02-22 23:40:56 +00:00
|
|
|
app.SetSDKConfig()
|
|
|
|
suite.encodingConfig = app.MakeEncodingConfig()
|
|
|
|
|
|
|
|
// this mnemonic is expected to be a funded account that can seed the funds for all
|
|
|
|
// new accounts created during tests. it will be available under Accounts["whale"]
|
|
|
|
fundedAccountMnemonic := os.Getenv("E2E_KAVA_FUNDED_ACCOUNT_MNEMONIC")
|
|
|
|
if fundedAccountMnemonic == "" {
|
|
|
|
suite.Fail("no E2E_KAVA_FUNDED_ACCOUNT_MNEMONIC provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
configDir, err := filepath.Abs("./generated/kava-1/config")
|
|
|
|
if err != nil {
|
|
|
|
suite.Fail("failed to get config dir: %s", err)
|
|
|
|
}
|
|
|
|
config := runner.Config{
|
|
|
|
ConfigDir: configDir,
|
|
|
|
|
|
|
|
KavaRpcPort: "26657",
|
|
|
|
KavaRestPort: "1317",
|
|
|
|
KavaGrpcPort: "9090",
|
|
|
|
KavaEvmPort: "8545",
|
|
|
|
|
|
|
|
ImageTag: "local",
|
|
|
|
}
|
|
|
|
suite.runner = runner.NewSingleKavaNode(config)
|
|
|
|
suite.runner.StartChains()
|
|
|
|
|
|
|
|
// setup an unauthenticated evm client
|
|
|
|
evmRpcUrl := fmt.Sprintf("http://localhost:%s", config.KavaEvmPort)
|
|
|
|
suite.EvmClient, err = ethclient.Dial(evmRpcUrl)
|
|
|
|
if err != nil {
|
|
|
|
suite.runner.Shutdown()
|
|
|
|
suite.Fail("failed to connect to evm: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// create grpc connection
|
|
|
|
grpcUrl := fmt.Sprintf("http://localhost:%s", config.KavaGrpcPort)
|
|
|
|
suite.grpcConn, err = util.NewGrpcConnection(grpcUrl)
|
|
|
|
if err != nil {
|
|
|
|
suite.runner.Shutdown()
|
|
|
|
suite.Fail("failed to create grpc connection: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// setup unauthenticated query clients for kava / cosmos
|
|
|
|
suite.Auth = authtypes.NewQueryClient(suite.grpcConn)
|
|
|
|
suite.Bank = banktypes.NewQueryClient(suite.grpcConn)
|
|
|
|
suite.Tm = tmservice.NewServiceClient(suite.grpcConn)
|
|
|
|
suite.Tx = txtypes.NewServiceClient(suite.grpcConn)
|
|
|
|
|
|
|
|
// initialize accounts map
|
|
|
|
suite.accounts = make(map[string]*SigningAccount)
|
|
|
|
// setup the signing account for the initially funded account (used to fund all other accounts)
|
2023-03-02 01:05:53 +00:00
|
|
|
whale := suite.AddNewSigningAccount(
|
2023-02-22 23:40:56 +00:00
|
|
|
FundedAccountName,
|
2023-03-02 01:05:53 +00:00
|
|
|
hd.CreateHDPath(Bip44CoinType, 0, 0),
|
2023-02-22 23:40:56 +00:00
|
|
|
ChainId,
|
|
|
|
fundedAccountMnemonic,
|
|
|
|
)
|
2023-03-02 01:05:53 +00:00
|
|
|
|
|
|
|
// check that funded account is actually funded.
|
|
|
|
fmt.Printf("account used for funding (%s) address: %s\n", FundedAccountName, whale.SdkAddress)
|
|
|
|
whaleFunds := suite.QuerySdkForBalances(whale.SdkAddress)
|
|
|
|
if whaleFunds.IsZero() {
|
|
|
|
suite.FailNow("no available funds.", "funded account mnemonic is for account with no funds")
|
|
|
|
}
|
2023-02-22 23:40:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *E2eTestSuite) TearDownSuite() {
|
2023-03-02 01:05:53 +00:00
|
|
|
fmt.Println("tearing down test suite.")
|
2023-02-22 23:40:56 +00:00
|
|
|
// close all account request channels
|
|
|
|
for _, a := range suite.accounts {
|
2023-03-02 01:05:53 +00:00
|
|
|
close(a.sdkReqChan)
|
2023-02-22 23:40:56 +00:00
|
|
|
}
|
|
|
|
// gracefully shutdown docker container(s)
|
|
|
|
suite.runner.Shutdown()
|
|
|
|
}
|
2023-03-02 01:05:53 +00:00
|
|
|
|
|
|
|
func (suite *E2eTestSuite) QuerySdkForBalances(addr sdk.AccAddress) sdk.Coins {
|
|
|
|
res, err := suite.Bank.AllBalances(context.Background(), &banktypes.QueryAllBalancesRequest{
|
|
|
|
Address: addr.String(),
|
|
|
|
})
|
|
|
|
suite.NoError(err)
|
|
|
|
return res.Balances
|
|
|
|
}
|