2021-06-25 09:18:37 +00:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-11-14 13:34:10 +00:00
|
|
|
"net/http"
|
2021-06-25 09:18:37 +00:00
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2021-11-14 13:34:10 +00:00
|
|
|
"strings"
|
2021-06-25 09:18:37 +00:00
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-10-10 10:38:33 +00:00
|
|
|
sdkmath "cosmossdk.io/math"
|
2021-06-25 09:18:37 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/ethclient"
|
2021-11-14 13:34:10 +00:00
|
|
|
"github.com/spf13/cobra"
|
2022-10-10 10:38:33 +00:00
|
|
|
tmcfg "github.com/tendermint/tendermint/config"
|
|
|
|
tmflags "github.com/tendermint/tendermint/libs/cli/flags"
|
|
|
|
"github.com/tendermint/tendermint/libs/log"
|
|
|
|
tmrand "github.com/tendermint/tendermint/libs/rand"
|
|
|
|
"github.com/tendermint/tendermint/node"
|
2021-06-25 09:18:37 +00:00
|
|
|
tmclient "github.com/tendermint/tendermint/rpc/client"
|
2022-10-10 10:38:33 +00:00
|
|
|
dbm "github.com/tendermint/tm-db"
|
2021-11-14 13:34:10 +00:00
|
|
|
"google.golang.org/grpc"
|
2021-06-25 09:18:37 +00:00
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/baseapp"
|
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
|
|
|
"github.com/cosmos/cosmos-sdk/client/tx"
|
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
|
|
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
|
|
|
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
|
2022-06-06 12:37:13 +00:00
|
|
|
pruningtypes "github.com/cosmos/cosmos-sdk/pruning/types"
|
2021-06-25 09:18:37 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/server"
|
|
|
|
"github.com/cosmos/cosmos-sdk/server/api"
|
2021-11-14 13:34:10 +00:00
|
|
|
srvconfig "github.com/cosmos/cosmos-sdk/server/config"
|
2021-06-25 09:18:37 +00:00
|
|
|
servertypes "github.com/cosmos/cosmos-sdk/server/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/simapp"
|
|
|
|
"github.com/cosmos/cosmos-sdk/simapp/params"
|
2022-01-18 21:02:10 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/testutil"
|
2021-06-25 09:18:37 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
|
|
|
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/genutil"
|
|
|
|
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
|
|
|
|
|
2022-09-07 06:36:11 +00:00
|
|
|
"github.com/cerc-io/laconicd/crypto/hd"
|
|
|
|
"github.com/cerc-io/laconicd/encoding"
|
|
|
|
"github.com/cerc-io/laconicd/server/config"
|
|
|
|
ethermint "github.com/cerc-io/laconicd/types"
|
|
|
|
evmtypes "github.com/cerc-io/laconicd/x/evm/types"
|
2021-11-14 13:34:10 +00:00
|
|
|
|
2022-09-07 06:36:11 +00:00
|
|
|
"github.com/cerc-io/laconicd/app"
|
2021-06-25 09:18:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// package-wide network lock to only allow one test network at a time
|
|
|
|
var lock = new(sync.Mutex)
|
|
|
|
|
|
|
|
// AppConstructor defines a function which accepts a network configuration and
|
|
|
|
// creates an ABCI Application to provide to Tendermint.
|
|
|
|
type AppConstructor = func(val Validator) servertypes.Application
|
|
|
|
|
|
|
|
// NewAppConstructor returns a new simapp AppConstructor
|
|
|
|
func NewAppConstructor(encodingCfg params.EncodingConfig) AppConstructor {
|
|
|
|
return func(val Validator) servertypes.Application {
|
2022-04-26 10:39:18 +00:00
|
|
|
return app.NewEthermintApp(
|
2022-09-30 06:41:39 +00:00
|
|
|
val.Ctx.Logger, dbm.NewMemDB(), nil, true, make(map[int64]bool), val.Ctx.Config.RootDir, 0,
|
2021-06-25 09:18:37 +00:00
|
|
|
encodingCfg,
|
|
|
|
simapp.EmptyAppOptions{},
|
2022-06-06 12:37:13 +00:00
|
|
|
baseapp.SetPruning(pruningtypes.NewPruningOptionsFromString(val.AppConfig.Pruning)),
|
2021-06-25 09:18:37 +00:00
|
|
|
baseapp.SetMinGasPrices(val.AppConfig.MinGasPrices),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Config defines the necessary configuration used to bootstrap and start an
|
|
|
|
// in-process local testing network.
|
|
|
|
type Config struct {
|
2021-11-14 13:34:10 +00:00
|
|
|
KeyringOptions []keyring.Option // keyring configuration options
|
2021-06-29 17:02:21 +00:00
|
|
|
Codec codec.Codec
|
2021-06-25 09:18:37 +00:00
|
|
|
LegacyAmino *codec.LegacyAmino // TODO: Remove!
|
|
|
|
InterfaceRegistry codectypes.InterfaceRegistry
|
2021-11-14 13:34:10 +00:00
|
|
|
TxConfig client.TxConfig
|
|
|
|
AccountRetriever client.AccountRetriever
|
|
|
|
AppConstructor AppConstructor // the ABCI application constructor
|
|
|
|
GenesisState simapp.GenesisState // custom gensis state to provide
|
|
|
|
TimeoutCommit time.Duration // the consensus commitment timeout
|
2022-10-10 10:38:33 +00:00
|
|
|
AccountTokens sdkmath.Int // the amount of unique validator tokens (e.g. 1000node0)
|
|
|
|
StakingTokens sdkmath.Int // the amount of tokens each validator has available to stake
|
|
|
|
BondedTokens sdkmath.Int // the amount of tokens each validator stakes
|
2021-11-14 13:34:10 +00:00
|
|
|
NumValidators int // the total number of validators to create and bond
|
|
|
|
ChainID string // the network chain-id
|
|
|
|
BondDenom string // the staking bond denomination
|
|
|
|
MinGasPrices string // the minimum gas prices each validator will accept
|
|
|
|
PruningStrategy string // the pruning strategy each validator will have
|
|
|
|
SigningAlgo string // signing algorithm for keys
|
|
|
|
RPCAddress string // RPC listen address (including port)
|
|
|
|
JSONRPCAddress string // JSON-RPC listen address (including port)
|
|
|
|
APIAddress string // REST API listen address (including port)
|
|
|
|
GRPCAddress string // GRPC server listen address (including port)
|
|
|
|
EnableTMLogging bool // enable Tendermint logging to STDOUT
|
|
|
|
CleanupDir bool // remove base temporary directory during cleanup
|
|
|
|
PrintMnemonic bool // print the mnemonic of first validator as log output for testing
|
2021-06-25 09:18:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultConfig returns a sane default configuration suitable for nearly all
|
|
|
|
// testing requirements.
|
|
|
|
func DefaultConfig() Config {
|
|
|
|
encCfg := encoding.MakeConfig(app.ModuleBasics)
|
|
|
|
|
|
|
|
return Config{
|
2022-04-11 08:09:39 +00:00
|
|
|
Codec: encCfg.Codec,
|
2021-06-25 09:18:37 +00:00
|
|
|
TxConfig: encCfg.TxConfig,
|
|
|
|
LegacyAmino: encCfg.Amino,
|
|
|
|
InterfaceRegistry: encCfg.InterfaceRegistry,
|
|
|
|
AccountRetriever: authtypes.AccountRetriever{},
|
|
|
|
AppConstructor: NewAppConstructor(encCfg),
|
2022-04-26 10:39:18 +00:00
|
|
|
GenesisState: app.ModuleBasics.DefaultGenesis(encCfg.Codec),
|
2021-06-25 09:18:37 +00:00
|
|
|
TimeoutCommit: 2 * time.Second,
|
2022-10-10 10:38:33 +00:00
|
|
|
ChainID: fmt.Sprintf("ethermint_%d-1", tmrand.Int63n(9999999999999)+1),
|
2021-06-25 09:18:37 +00:00
|
|
|
NumValidators: 4,
|
|
|
|
BondDenom: ethermint.AttoPhoton,
|
|
|
|
MinGasPrices: fmt.Sprintf("0.000006%s", ethermint.AttoPhoton),
|
2021-06-29 17:02:21 +00:00
|
|
|
AccountTokens: sdk.TokensFromConsensusPower(1000, ethermint.PowerReduction),
|
|
|
|
StakingTokens: sdk.TokensFromConsensusPower(500, ethermint.PowerReduction),
|
|
|
|
BondedTokens: sdk.TokensFromConsensusPower(100, ethermint.PowerReduction),
|
2022-06-06 12:37:13 +00:00
|
|
|
PruningStrategy: pruningtypes.PruningOptionNothing,
|
2021-06-25 09:18:37 +00:00
|
|
|
CleanupDir: true,
|
|
|
|
SigningAlgo: string(hd.EthSecp256k1Type),
|
|
|
|
KeyringOptions: []keyring.Option{hd.EthSecp256k1Option()},
|
2021-11-14 13:34:10 +00:00
|
|
|
PrintMnemonic: false,
|
2021-06-25 09:18:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type (
|
|
|
|
// Network defines a local in-process testing network using SimApp. It can be
|
|
|
|
// configured to start any number of validators, each with its own RPC and API
|
|
|
|
// clients. Typically, this test network would be used in client and integration
|
|
|
|
// testing where user input is expected.
|
|
|
|
//
|
|
|
|
// Note, due to Tendermint constraints in regards to RPC functionality, there
|
|
|
|
// may only be one test network running at a time. Thus, any caller must be
|
|
|
|
// sure to Cleanup after testing is finished in order to allow other tests
|
|
|
|
// to create networks. In addition, only the first validator will have a valid
|
|
|
|
// RPC and API server/client.
|
|
|
|
Network struct {
|
2021-11-14 13:34:10 +00:00
|
|
|
Logger Logger
|
2021-06-25 09:18:37 +00:00
|
|
|
BaseDir string
|
|
|
|
Validators []*Validator
|
|
|
|
|
|
|
|
Config Config
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validator defines an in-process Tendermint validator node. Through this object,
|
|
|
|
// a client can make RPC and API calls and interact with any client command
|
|
|
|
// or handler.
|
|
|
|
Validator struct {
|
2021-11-14 13:34:10 +00:00
|
|
|
AppConfig *config.Config
|
|
|
|
ClientCtx client.Context
|
|
|
|
Ctx *server.Context
|
|
|
|
Dir string
|
|
|
|
NodeID string
|
|
|
|
PubKey cryptotypes.PubKey
|
|
|
|
Moniker string
|
|
|
|
APIAddress string
|
|
|
|
RPCAddress string
|
|
|
|
P2PAddress string
|
|
|
|
Address sdk.AccAddress
|
|
|
|
ValAddress sdk.ValAddress
|
|
|
|
RPCClient tmclient.Client
|
|
|
|
JSONRPCClient *ethclient.Client
|
|
|
|
|
2022-10-10 10:38:33 +00:00
|
|
|
tmNode *node.Node
|
2021-11-14 13:34:10 +00:00
|
|
|
api *api.Server
|
|
|
|
grpc *grpc.Server
|
|
|
|
grpcWeb *http.Server
|
|
|
|
jsonrpc *http.Server
|
|
|
|
jsonrpcDone chan struct{}
|
2021-06-25 09:18:37 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-11-14 13:34:10 +00:00
|
|
|
// Logger is a network logger interface that exposes testnet-level Log() methods for an in-process testing network
|
|
|
|
// This is not to be confused with logging that may happen at an individual node or validator level
|
|
|
|
type Logger interface {
|
|
|
|
Log(args ...interface{})
|
|
|
|
Logf(format string, args ...interface{})
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ Logger = (*testing.T)(nil)
|
|
|
|
_ Logger = (*CLILogger)(nil)
|
|
|
|
)
|
|
|
|
|
|
|
|
type CLILogger struct {
|
|
|
|
cmd *cobra.Command
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s CLILogger) Log(args ...interface{}) {
|
|
|
|
s.cmd.Println(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s CLILogger) Logf(format string, args ...interface{}) {
|
|
|
|
s.cmd.Printf(format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCLILogger(cmd *cobra.Command) CLILogger {
|
|
|
|
return CLILogger{cmd}
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new Network for integration tests or in-process testnets run via the CLI
|
2023-01-06 09:23:03 +00:00
|
|
|
// nosemgrep: missing-unlock-before-return
|
2021-11-14 13:34:10 +00:00
|
|
|
func New(l Logger, baseDir string, cfg Config) (*Network, error) {
|
2021-06-25 09:18:37 +00:00
|
|
|
// only one caller/test can create and use a network at a time
|
2021-11-14 13:34:10 +00:00
|
|
|
l.Log("acquiring test network lock")
|
2021-06-25 09:18:37 +00:00
|
|
|
lock.Lock()
|
2023-01-06 09:23:03 +00:00
|
|
|
// defer lock.Unlock()
|
2021-06-25 09:18:37 +00:00
|
|
|
|
2021-11-14 13:34:10 +00:00
|
|
|
if !ethermint.IsValidChainID(cfg.ChainID) {
|
|
|
|
return nil, fmt.Errorf("invalid chain-id: %s", cfg.ChainID)
|
|
|
|
}
|
2021-06-25 09:18:37 +00:00
|
|
|
|
|
|
|
network := &Network{
|
2021-11-14 13:34:10 +00:00
|
|
|
Logger: l,
|
2021-06-25 09:18:37 +00:00
|
|
|
BaseDir: baseDir,
|
|
|
|
Validators: make([]*Validator, cfg.NumValidators),
|
|
|
|
Config: cfg,
|
|
|
|
}
|
|
|
|
|
2021-11-14 13:34:10 +00:00
|
|
|
l.Logf("preparing test network with chain-id \"%s\"\n", cfg.ChainID)
|
2021-06-25 09:18:37 +00:00
|
|
|
|
2021-11-14 13:34:10 +00:00
|
|
|
monikers := make([]string, cfg.NumValidators)
|
|
|
|
nodeIDs := make([]string, cfg.NumValidators)
|
|
|
|
valPubKeys := make([]cryptotypes.PubKey, cfg.NumValidators)
|
2021-06-25 09:18:37 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
genAccounts []authtypes.GenesisAccount
|
|
|
|
genBalances []banktypes.Balance
|
|
|
|
genFiles []string
|
|
|
|
)
|
|
|
|
|
|
|
|
buf := bufio.NewReader(os.Stdin)
|
|
|
|
|
|
|
|
// generate private keys, node IDs, and initial transactions
|
|
|
|
for i := 0; i < cfg.NumValidators; i++ {
|
2021-11-14 13:34:10 +00:00
|
|
|
appCfg := config.DefaultConfig()
|
2021-06-25 09:18:37 +00:00
|
|
|
appCfg.Pruning = cfg.PruningStrategy
|
|
|
|
appCfg.MinGasPrices = cfg.MinGasPrices
|
|
|
|
appCfg.API.Enable = true
|
|
|
|
appCfg.API.Swagger = false
|
|
|
|
appCfg.Telemetry.Enabled = false
|
2021-11-14 13:34:10 +00:00
|
|
|
appCfg.Telemetry.GlobalLabels = [][]string{{"chain_id", cfg.ChainID}}
|
2021-06-25 09:18:37 +00:00
|
|
|
|
|
|
|
ctx := server.NewDefaultContext()
|
|
|
|
tmCfg := ctx.Config
|
|
|
|
tmCfg.Consensus.TimeoutCommit = cfg.TimeoutCommit
|
|
|
|
|
|
|
|
// Only allow the first validator to expose an RPC, API and gRPC
|
|
|
|
// server/client due to Tendermint in-process constraints.
|
|
|
|
apiAddr := ""
|
|
|
|
tmCfg.RPC.ListenAddress = ""
|
|
|
|
appCfg.GRPC.Enable = false
|
2021-11-14 13:34:10 +00:00
|
|
|
appCfg.GRPCWeb.Enable = false
|
|
|
|
apiListenAddr := ""
|
2021-06-25 09:18:37 +00:00
|
|
|
if i == 0 {
|
2021-11-14 13:34:10 +00:00
|
|
|
if cfg.APIAddress != "" {
|
|
|
|
apiListenAddr = cfg.APIAddress
|
|
|
|
} else {
|
|
|
|
var err error
|
|
|
|
apiListenAddr, _, err = server.FreeTCPAddr()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2021-06-25 09:18:37 +00:00
|
|
|
|
2021-11-14 13:34:10 +00:00
|
|
|
appCfg.API.Address = apiListenAddr
|
2021-06-25 09:18:37 +00:00
|
|
|
apiURL, err := url.Parse(apiListenAddr)
|
2021-11-14 13:34:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-06-25 09:18:37 +00:00
|
|
|
apiAddr = fmt.Sprintf("http://%s:%s", apiURL.Hostname(), apiURL.Port())
|
|
|
|
|
2021-11-14 13:34:10 +00:00
|
|
|
if cfg.RPCAddress != "" {
|
|
|
|
tmCfg.RPC.ListenAddress = cfg.RPCAddress
|
|
|
|
} else {
|
|
|
|
rpcAddr, _, err := server.FreeTCPAddr()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tmCfg.RPC.ListenAddress = rpcAddr
|
|
|
|
}
|
2021-06-25 09:18:37 +00:00
|
|
|
|
2021-11-14 13:34:10 +00:00
|
|
|
if cfg.GRPCAddress != "" {
|
|
|
|
appCfg.GRPC.Address = cfg.GRPCAddress
|
|
|
|
} else {
|
|
|
|
_, grpcPort, err := server.FreeTCPAddr()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
appCfg.GRPC.Address = fmt.Sprintf("0.0.0.0:%s", grpcPort)
|
|
|
|
}
|
2021-06-25 09:18:37 +00:00
|
|
|
appCfg.GRPC.Enable = true
|
2021-11-14 13:34:10 +00:00
|
|
|
|
|
|
|
_, grpcWebPort, err := server.FreeTCPAddr()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
appCfg.GRPCWeb.Address = fmt.Sprintf("0.0.0.0:%s", grpcWebPort)
|
|
|
|
appCfg.GRPCWeb.Enable = true
|
|
|
|
|
|
|
|
if cfg.JSONRPCAddress != "" {
|
|
|
|
appCfg.JSONRPC.Address = cfg.JSONRPCAddress
|
|
|
|
} else {
|
|
|
|
_, jsonRPCPort, err := server.FreeTCPAddr()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
appCfg.JSONRPC.Address = fmt.Sprintf("0.0.0.0:%s", jsonRPCPort)
|
|
|
|
}
|
|
|
|
appCfg.JSONRPC.Enable = true
|
2021-11-30 09:16:40 +00:00
|
|
|
appCfg.JSONRPC.API = config.GetAPINamespaces()
|
2021-06-25 09:18:37 +00:00
|
|
|
}
|
|
|
|
|
2022-10-10 10:38:33 +00:00
|
|
|
logger := log.NewNopLogger()
|
2021-11-14 13:34:10 +00:00
|
|
|
if cfg.EnableTMLogging {
|
2022-10-10 10:38:33 +00:00
|
|
|
logger = log.NewTMLogger(log.NewSyncWriter(os.Stdout))
|
|
|
|
logger, _ = tmflags.ParseLogLevel("info", logger, tmcfg.DefaultLogLevel)
|
2021-06-25 09:18:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Logger = logger
|
|
|
|
|
|
|
|
nodeDirName := fmt.Sprintf("node%d", i)
|
2022-10-10 10:38:33 +00:00
|
|
|
nodeDir := filepath.Join(network.BaseDir, nodeDirName, "evmosd")
|
|
|
|
clientDir := filepath.Join(network.BaseDir, nodeDirName, "evmoscli")
|
2021-06-25 09:18:37 +00:00
|
|
|
gentxsDir := filepath.Join(network.BaseDir, "gentxs")
|
|
|
|
|
2021-11-25 15:12:57 +00:00
|
|
|
err := os.MkdirAll(filepath.Join(nodeDir, "config"), 0o750)
|
2021-11-14 13:34:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-11-25 15:12:57 +00:00
|
|
|
err = os.MkdirAll(clientDir, 0o750)
|
2021-11-14 13:34:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-06-25 09:18:37 +00:00
|
|
|
|
|
|
|
tmCfg.SetRoot(nodeDir)
|
|
|
|
tmCfg.Moniker = nodeDirName
|
|
|
|
monikers[i] = nodeDirName
|
|
|
|
|
|
|
|
proxyAddr, _, err := server.FreeTCPAddr()
|
2021-11-14 13:34:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-06-25 09:18:37 +00:00
|
|
|
tmCfg.ProxyApp = proxyAddr
|
|
|
|
|
|
|
|
p2pAddr, _, err := server.FreeTCPAddr()
|
2021-11-14 13:34:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-06-25 09:18:37 +00:00
|
|
|
tmCfg.P2P.ListenAddress = p2pAddr
|
|
|
|
tmCfg.P2P.AddrBookStrict = false
|
|
|
|
tmCfg.P2P.AllowDuplicateIP = true
|
|
|
|
|
|
|
|
nodeID, pubKey, err := genutil.InitializeNodeValidatorFiles(tmCfg)
|
2021-11-14 13:34:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-06-25 09:18:37 +00:00
|
|
|
nodeIDs[i] = nodeID
|
|
|
|
valPubKeys[i] = pubKey
|
|
|
|
|
2022-04-11 08:09:39 +00:00
|
|
|
kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, clientDir, buf, cfg.Codec, cfg.KeyringOptions...)
|
2021-11-14 13:34:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-06-25 09:18:37 +00:00
|
|
|
|
|
|
|
keyringAlgos, _ := kb.SupportedAlgorithms()
|
|
|
|
algo, err := keyring.NewSigningAlgoFromString(cfg.SigningAlgo, keyringAlgos)
|
2021-11-14 13:34:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-06-25 09:18:37 +00:00
|
|
|
|
2022-01-18 21:02:10 +00:00
|
|
|
addr, secret, err := testutil.GenerateSaveCoinKey(kb, nodeDirName, "", true, algo)
|
2021-11-14 13:34:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// if PrintMnemonic is set to true, we print the first validator node's secret to the network's logger
|
|
|
|
// for debugging and manual testing
|
|
|
|
if cfg.PrintMnemonic && i == 0 {
|
|
|
|
printMnemonic(l, secret)
|
|
|
|
}
|
2021-06-25 09:18:37 +00:00
|
|
|
|
|
|
|
info := map[string]string{"secret": secret}
|
|
|
|
infoBz, err := json.Marshal(info)
|
2021-11-14 13:34:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-06-25 09:18:37 +00:00
|
|
|
|
|
|
|
// save private key seed words
|
2021-11-14 13:34:10 +00:00
|
|
|
err = WriteFile(fmt.Sprintf("%v.json", "key_seed"), clientDir, infoBz)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-06-25 09:18:37 +00:00
|
|
|
|
|
|
|
balances := sdk.NewCoins(
|
|
|
|
sdk.NewCoin(fmt.Sprintf("%stoken", nodeDirName), cfg.AccountTokens),
|
|
|
|
sdk.NewCoin(cfg.BondDenom, cfg.StakingTokens),
|
|
|
|
)
|
|
|
|
|
|
|
|
genFiles = append(genFiles, tmCfg.GenesisFile())
|
|
|
|
genBalances = append(genBalances, banktypes.Balance{Address: addr.String(), Coins: balances.Sort()})
|
2021-11-14 13:34:10 +00:00
|
|
|
genAccounts = append(genAccounts, ðermint.EthAccount{
|
|
|
|
BaseAccount: authtypes.NewBaseAccount(addr, nil, 0, 0),
|
|
|
|
CodeHash: common.BytesToHash(evmtypes.EmptyCodeHash).Hex(),
|
|
|
|
})
|
2021-06-25 09:18:37 +00:00
|
|
|
|
|
|
|
commission, err := sdk.NewDecFromStr("0.5")
|
2021-11-14 13:34:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-06-25 09:18:37 +00:00
|
|
|
|
|
|
|
createValMsg, err := stakingtypes.NewMsgCreateValidator(
|
|
|
|
sdk.ValAddress(addr),
|
|
|
|
valPubKeys[i],
|
|
|
|
sdk.NewCoin(cfg.BondDenom, cfg.BondedTokens),
|
|
|
|
stakingtypes.NewDescription(nodeDirName, "", "", "", ""),
|
|
|
|
stakingtypes.NewCommissionRates(commission, sdk.OneDec(), sdk.OneDec()),
|
|
|
|
sdk.OneInt(),
|
|
|
|
)
|
2021-11-14 13:34:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-06-25 09:18:37 +00:00
|
|
|
|
|
|
|
p2pURL, err := url.Parse(p2pAddr)
|
2021-11-14 13:34:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-06-25 09:18:37 +00:00
|
|
|
|
|
|
|
memo := fmt.Sprintf("%s@%s:%s", nodeIDs[i], p2pURL.Hostname(), p2pURL.Port())
|
2022-10-10 10:38:33 +00:00
|
|
|
fee := sdk.NewCoins(sdk.NewCoin(cfg.BondDenom, sdkmath.NewInt(0)))
|
2021-06-25 09:18:37 +00:00
|
|
|
txBuilder := cfg.TxConfig.NewTxBuilder()
|
2021-11-14 13:34:10 +00:00
|
|
|
err = txBuilder.SetMsgs(createValMsg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-06-25 09:18:37 +00:00
|
|
|
txBuilder.SetFeeAmount(fee) // Arbitrary fee
|
|
|
|
txBuilder.SetGasLimit(1000000) // Need at least 100386
|
|
|
|
txBuilder.SetMemo(memo)
|
|
|
|
|
|
|
|
txFactory := tx.Factory{}
|
|
|
|
txFactory = txFactory.
|
|
|
|
WithChainID(cfg.ChainID).
|
|
|
|
WithMemo(memo).
|
|
|
|
WithKeybase(kb).
|
|
|
|
WithTxConfig(cfg.TxConfig)
|
|
|
|
|
2021-11-14 13:34:10 +00:00
|
|
|
if err := tx.Sign(txFactory, nodeDirName, txBuilder, true); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-06-25 09:18:37 +00:00
|
|
|
|
|
|
|
txBz, err := cfg.TxConfig.TxJSONEncoder()(txBuilder.GetTx())
|
2021-11-14 13:34:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-06-25 09:18:37 +00:00
|
|
|
|
2021-11-14 13:34:10 +00:00
|
|
|
if err := WriteFile(fmt.Sprintf("%v.json", nodeDirName), gentxsDir, txBz); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
customAppTemplate, _ := config.AppConfig(ethermint.AttoPhoton)
|
|
|
|
srvconfig.SetConfigTemplate(customAppTemplate)
|
|
|
|
srvconfig.WriteConfigFile(filepath.Join(nodeDir, "config/app.toml"), appCfg)
|
|
|
|
|
|
|
|
ctx.Viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_"))
|
|
|
|
ctx.Viper.SetConfigFile(filepath.Join(nodeDir, "config/app.toml"))
|
2021-09-17 23:47:13 +00:00
|
|
|
err = ctx.Viper.ReadInConfig()
|
|
|
|
if err != nil {
|
2021-11-14 13:34:10 +00:00
|
|
|
return nil, err
|
2021-09-17 23:47:13 +00:00
|
|
|
}
|
2021-06-25 09:18:37 +00:00
|
|
|
|
|
|
|
clientCtx := client.Context{}.
|
2021-11-14 13:34:10 +00:00
|
|
|
WithKeyringDir(clientDir).
|
2021-06-25 09:18:37 +00:00
|
|
|
WithKeyring(kb).
|
|
|
|
WithHomeDir(tmCfg.RootDir).
|
|
|
|
WithChainID(cfg.ChainID).
|
|
|
|
WithInterfaceRegistry(cfg.InterfaceRegistry).
|
2021-06-29 17:02:21 +00:00
|
|
|
WithCodec(cfg.Codec).
|
2021-06-25 09:18:37 +00:00
|
|
|
WithLegacyAmino(cfg.LegacyAmino).
|
|
|
|
WithTxConfig(cfg.TxConfig).
|
|
|
|
WithAccountRetriever(cfg.AccountRetriever)
|
|
|
|
|
|
|
|
network.Validators[i] = &Validator{
|
2021-11-14 13:34:10 +00:00
|
|
|
AppConfig: appCfg,
|
|
|
|
ClientCtx: clientCtx,
|
|
|
|
Ctx: ctx,
|
|
|
|
Dir: filepath.Join(network.BaseDir, nodeDirName),
|
|
|
|
NodeID: nodeID,
|
|
|
|
PubKey: pubKey,
|
|
|
|
Moniker: nodeDirName,
|
|
|
|
RPCAddress: tmCfg.RPC.ListenAddress,
|
|
|
|
P2PAddress: tmCfg.P2P.ListenAddress,
|
|
|
|
APIAddress: apiAddr,
|
|
|
|
Address: addr,
|
|
|
|
ValAddress: sdk.ValAddress(addr),
|
2021-06-25 09:18:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-14 13:34:10 +00:00
|
|
|
err := initGenFiles(cfg, genAccounts, genBalances, genFiles)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = collectGenFiles(cfg, network.Validators, network.BaseDir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-06-25 09:18:37 +00:00
|
|
|
|
2021-11-14 13:34:10 +00:00
|
|
|
l.Log("starting test network...")
|
2021-06-25 09:18:37 +00:00
|
|
|
for _, v := range network.Validators {
|
2021-11-14 13:34:10 +00:00
|
|
|
err := startInProcess(cfg, v)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-06-25 09:18:37 +00:00
|
|
|
}
|
|
|
|
|
2021-11-14 13:34:10 +00:00
|
|
|
l.Log("started test network")
|
2021-06-25 09:18:37 +00:00
|
|
|
|
|
|
|
// Ensure we cleanup incase any test was abruptly halted (e.g. SIGINT) as any
|
|
|
|
// defer in a test would not be called.
|
|
|
|
server.TrapSignal(network.Cleanup)
|
|
|
|
|
2021-11-14 13:34:10 +00:00
|
|
|
return network, nil
|
2021-06-25 09:18:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// LatestHeight returns the latest height of the network or an error if the
|
|
|
|
// query fails or no validators exist.
|
|
|
|
func (n *Network) LatestHeight() (int64, error) {
|
|
|
|
if len(n.Validators) == 0 {
|
|
|
|
return 0, errors.New("no validators available")
|
|
|
|
}
|
|
|
|
|
|
|
|
status, err := n.Validators[0].RPCClient.Status(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return status.SyncInfo.LatestBlockHeight, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// WaitForHeight performs a blocking check where it waits for a block to be
|
|
|
|
// committed after a given block. If that height is not reached within a timeout,
|
|
|
|
// an error is returned. Regardless, the latest height queried is returned.
|
|
|
|
func (n *Network) WaitForHeight(h int64) (int64, error) {
|
2022-05-18 08:08:56 +00:00
|
|
|
return n.WaitForHeightWithTimeout(h, 10*time.Second)
|
2021-06-25 09:18:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WaitForHeightWithTimeout is the same as WaitForHeight except the caller can
|
|
|
|
// provide a custom timeout.
|
|
|
|
func (n *Network) WaitForHeightWithTimeout(h int64, t time.Duration) (int64, error) {
|
|
|
|
ticker := time.NewTicker(time.Second)
|
2022-10-10 10:38:33 +00:00
|
|
|
timeout := time.After(t)
|
2021-06-25 09:18:37 +00:00
|
|
|
|
|
|
|
if len(n.Validators) == 0 {
|
|
|
|
return 0, errors.New("no validators available")
|
|
|
|
}
|
|
|
|
|
|
|
|
var latestHeight int64
|
2022-10-10 10:38:33 +00:00
|
|
|
val := n.Validators[0]
|
|
|
|
|
2021-06-25 09:18:37 +00:00
|
|
|
for {
|
|
|
|
select {
|
2022-10-10 10:38:33 +00:00
|
|
|
case <-timeout:
|
|
|
|
ticker.Stop()
|
2021-06-25 09:18:37 +00:00
|
|
|
return latestHeight, errors.New("timeout exceeded waiting for block")
|
|
|
|
case <-ticker.C:
|
2022-10-10 10:38:33 +00:00
|
|
|
status, err := val.RPCClient.Status(context.Background())
|
2021-06-25 09:18:37 +00:00
|
|
|
if err == nil && status != nil {
|
|
|
|
latestHeight = status.SyncInfo.LatestBlockHeight
|
|
|
|
if latestHeight >= h {
|
|
|
|
return latestHeight, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WaitForNextBlock waits for the next block to be committed, returning an error
|
|
|
|
// upon failure.
|
|
|
|
func (n *Network) WaitForNextBlock() error {
|
|
|
|
lastBlock, err := n.LatestHeight()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = n.WaitForHeight(lastBlock + 1)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup removes the root testing (temporary) directory and stops both the
|
|
|
|
// Tendermint and API services. It allows other callers to create and start
|
|
|
|
// test networks. This method must be called when a test is finished, typically
|
|
|
|
// in a defer.
|
|
|
|
func (n *Network) Cleanup() {
|
|
|
|
defer func() {
|
|
|
|
lock.Unlock()
|
2021-11-14 13:34:10 +00:00
|
|
|
n.Logger.Log("released test network lock")
|
2021-06-25 09:18:37 +00:00
|
|
|
}()
|
|
|
|
|
2021-11-14 13:34:10 +00:00
|
|
|
n.Logger.Log("cleaning up test network...")
|
2021-06-25 09:18:37 +00:00
|
|
|
|
|
|
|
for _, v := range n.Validators {
|
|
|
|
if v.tmNode != nil && v.tmNode.IsRunning() {
|
|
|
|
_ = v.tmNode.Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.api != nil {
|
|
|
|
_ = v.api.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.grpc != nil {
|
|
|
|
v.grpc.Stop()
|
2021-11-14 13:34:10 +00:00
|
|
|
if v.grpcWeb != nil {
|
|
|
|
_ = v.grpcWeb.Close()
|
|
|
|
}
|
2021-06-25 09:18:37 +00:00
|
|
|
}
|
2022-10-10 10:38:33 +00:00
|
|
|
|
|
|
|
if v.jsonrpc != nil {
|
|
|
|
shutdownCtx, cancelFn := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
defer cancelFn()
|
|
|
|
|
|
|
|
if err := v.jsonrpc.Shutdown(shutdownCtx); err != nil {
|
|
|
|
v.tmNode.Logger.Error("HTTP server shutdown produced a warning", "error", err.Error())
|
|
|
|
} else {
|
|
|
|
v.tmNode.Logger.Info("HTTP server shut down, waiting 5 sec")
|
|
|
|
select {
|
|
|
|
case <-time.Tick(5 * time.Second):
|
|
|
|
case <-v.jsonrpcDone:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-25 09:18:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if n.Config.CleanupDir {
|
|
|
|
_ = os.RemoveAll(n.BaseDir)
|
|
|
|
}
|
|
|
|
|
2021-11-14 13:34:10 +00:00
|
|
|
n.Logger.Log("finished cleaning up test network")
|
|
|
|
}
|
|
|
|
|
|
|
|
// printMnemonic prints a provided mnemonic seed phrase on a network logger
|
|
|
|
// for debugging and manual testing
|
|
|
|
func printMnemonic(l Logger, secret string) {
|
|
|
|
lines := []string{
|
|
|
|
"THIS MNEMONIC IS FOR TESTING PURPOSES ONLY",
|
|
|
|
"DO NOT USE IN PRODUCTION",
|
|
|
|
"",
|
|
|
|
strings.Join(strings.Fields(secret)[0:8], " "),
|
|
|
|
strings.Join(strings.Fields(secret)[8:16], " "),
|
|
|
|
strings.Join(strings.Fields(secret)[16:24], " "),
|
|
|
|
}
|
|
|
|
|
|
|
|
lineLengths := make([]int, len(lines))
|
|
|
|
for i, line := range lines {
|
|
|
|
lineLengths[i] = len(line)
|
|
|
|
}
|
|
|
|
|
|
|
|
maxLineLength := 0
|
|
|
|
for _, lineLen := range lineLengths {
|
|
|
|
if lineLen > maxLineLength {
|
|
|
|
maxLineLength = lineLen
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
l.Log("\n")
|
|
|
|
l.Log(strings.Repeat("+", maxLineLength+8))
|
|
|
|
for _, line := range lines {
|
|
|
|
l.Logf("++ %s ++\n", centerText(line, maxLineLength))
|
|
|
|
}
|
|
|
|
l.Log(strings.Repeat("+", maxLineLength+8))
|
|
|
|
l.Log("\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
// centerText centers text across a fixed width, filling either side with whitespace buffers
|
|
|
|
func centerText(text string, width int) string {
|
|
|
|
textLen := len(text)
|
|
|
|
leftBuffer := strings.Repeat(" ", (width-textLen)/2)
|
|
|
|
rightBuffer := strings.Repeat(" ", (width-textLen)/2+(width-textLen)%2)
|
|
|
|
|
|
|
|
return fmt.Sprintf("%s%s%s", leftBuffer, text, rightBuffer)
|
2021-06-25 09:18:37 +00:00
|
|
|
}
|