2022-12-16 09:48:38 +00:00
|
|
|
// Copyright 2021 Evmos Foundation
|
|
|
|
// This file is part of Evmos' Ethermint library.
|
|
|
|
//
|
|
|
|
// The Ethermint library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The Ethermint library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the Ethermint library. If not, see https://github.com/evmos/ethermint/blob/main/LICENSE
|
2021-06-25 09:18:37 +00:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
|
2021-11-14 13:34:10 +00:00
|
|
|
"github.com/ethereum/go-ethereum/ethclient"
|
2022-10-10 10:38:33 +00:00
|
|
|
tmos "github.com/tendermint/tendermint/libs/os"
|
|
|
|
"github.com/tendermint/tendermint/node"
|
2022-09-30 06:41:39 +00:00
|
|
|
"github.com/tendermint/tendermint/p2p"
|
|
|
|
pvm "github.com/tendermint/tendermint/privval"
|
|
|
|
"github.com/tendermint/tendermint/proxy"
|
2021-06-25 09:18:37 +00:00
|
|
|
"github.com/tendermint/tendermint/rpc/client/local"
|
|
|
|
"github.com/tendermint/tendermint/types"
|
2022-09-30 06:41:39 +00:00
|
|
|
tmtime "github.com/tendermint/tendermint/types/time"
|
2021-06-25 09:18:37 +00:00
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/server/api"
|
|
|
|
servergrpc "github.com/cosmos/cosmos-sdk/server/grpc"
|
2021-11-14 13:34:10 +00:00
|
|
|
srvtypes "github.com/cosmos/cosmos-sdk/server/types"
|
2021-06-25 09:18:37 +00:00
|
|
|
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
|
|
|
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
|
2021-11-14 13:34:10 +00:00
|
|
|
crisistypes "github.com/cosmos/cosmos-sdk/x/crisis/types"
|
2021-06-25 09:18:37 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/genutil"
|
|
|
|
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
|
2021-11-14 13:34:10 +00:00
|
|
|
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
|
2022-10-10 10:38:33 +00:00
|
|
|
govv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
|
2021-11-14 13:34:10 +00:00
|
|
|
mintypes "github.com/cosmos/cosmos-sdk/x/mint/types"
|
2021-06-25 09:18:37 +00:00
|
|
|
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
|
2022-10-10 10:38:33 +00:00
|
|
|
|
|
|
|
"github.com/cerc-io/laconicd/server"
|
|
|
|
evmtypes "github.com/cerc-io/laconicd/x/evm/types"
|
2021-06-25 09:18:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func startInProcess(cfg Config, val *Validator) error {
|
|
|
|
logger := val.Ctx.Logger
|
|
|
|
tmCfg := val.Ctx.Config
|
|
|
|
tmCfg.Instrumentation.Prometheus = false
|
|
|
|
|
2021-11-14 13:34:10 +00:00
|
|
|
if err := val.AppConfig.ValidateBasic(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-09-30 06:41:39 +00:00
|
|
|
nodeKey, err := p2p.LoadOrGenNodeKey(tmCfg.NodeKeyFile())
|
2021-06-25 09:18:37 +00:00
|
|
|
if err != nil {
|
2021-11-14 13:34:10 +00:00
|
|
|
return err
|
2021-06-25 09:18:37 +00:00
|
|
|
}
|
|
|
|
|
2022-10-10 10:38:33 +00:00
|
|
|
app := cfg.AppConstructor(*val)
|
|
|
|
|
2022-09-30 06:41:39 +00:00
|
|
|
genDocProvider := node.DefaultGenesisDocProviderFunc(tmCfg)
|
|
|
|
tmNode, err := node.NewNode(
|
2021-06-25 09:18:37 +00:00
|
|
|
tmCfg,
|
2022-09-30 06:41:39 +00:00
|
|
|
pvm.LoadOrGenFilePV(tmCfg.PrivValidatorKeyFile(), tmCfg.PrivValidatorStateFile()),
|
|
|
|
nodeKey,
|
|
|
|
proxy.NewLocalClientCreator(app),
|
|
|
|
genDocProvider,
|
|
|
|
node.DefaultDBProvider,
|
|
|
|
node.DefaultMetricsProvider(tmCfg.Instrumentation),
|
2021-06-25 09:18:37 +00:00
|
|
|
logger.With("module", val.Moniker),
|
|
|
|
)
|
|
|
|
if err != nil {
|
2021-11-14 13:34:10 +00:00
|
|
|
return err
|
2021-06-25 09:18:37 +00:00
|
|
|
}
|
|
|
|
|
2022-10-03 15:20:09 +00:00
|
|
|
if err := tmNode.Start(); err != nil {
|
2021-11-14 13:34:10 +00:00
|
|
|
return err
|
2021-06-25 09:18:37 +00:00
|
|
|
}
|
|
|
|
|
2022-09-30 06:41:39 +00:00
|
|
|
val.tmNode = tmNode
|
|
|
|
|
2021-06-25 09:18:37 +00:00
|
|
|
if val.RPCAddress != "" {
|
2022-09-30 06:41:39 +00:00
|
|
|
val.RPCClient = local.New(tmNode)
|
2021-06-25 09:18:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// We'll need a RPC client if the validator exposes a gRPC or REST endpoint.
|
|
|
|
if val.APIAddress != "" || val.AppConfig.GRPC.Enable {
|
|
|
|
val.ClientCtx = val.ClientCtx.
|
|
|
|
WithClient(val.RPCClient)
|
|
|
|
|
|
|
|
// Add the tx service in the gRPC router.
|
|
|
|
app.RegisterTxService(val.ClientCtx)
|
|
|
|
|
|
|
|
// Add the tendermint queries service in the gRPC router.
|
|
|
|
app.RegisterTendermintService(val.ClientCtx)
|
|
|
|
}
|
|
|
|
|
2021-11-14 13:34:10 +00:00
|
|
|
if val.AppConfig.API.Enable && val.APIAddress != "" {
|
2021-06-25 09:18:37 +00:00
|
|
|
apiSrv := api.New(val.ClientCtx, logger.With("module", "api-server"))
|
|
|
|
app.RegisterAPIRoutes(apiSrv, val.AppConfig.API)
|
|
|
|
|
|
|
|
errCh := make(chan error)
|
|
|
|
|
|
|
|
go func() {
|
2021-06-29 17:02:21 +00:00
|
|
|
if err := apiSrv.Start(val.AppConfig.Config); err != nil {
|
2021-06-25 09:18:37 +00:00
|
|
|
errCh <- err
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case err := <-errCh:
|
2021-11-14 13:34:10 +00:00
|
|
|
return err
|
|
|
|
case <-time.After(srvtypes.ServerStartTime): // assume server started successfully
|
2021-06-25 09:18:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
val.api = apiSrv
|
|
|
|
}
|
|
|
|
|
|
|
|
if val.AppConfig.GRPC.Enable {
|
2022-06-06 12:37:13 +00:00
|
|
|
grpcSrv, err := servergrpc.StartGRPCServer(val.ClientCtx, app, val.AppConfig.GRPC)
|
2021-06-25 09:18:37 +00:00
|
|
|
if err != nil {
|
2021-11-14 13:34:10 +00:00
|
|
|
return err
|
2021-06-25 09:18:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
val.grpc = grpcSrv
|
|
|
|
|
2021-11-14 13:34:10 +00:00
|
|
|
if val.AppConfig.GRPCWeb.Enable {
|
|
|
|
val.grpcWeb, err = servergrpc.StartGRPCWeb(grpcSrv, val.AppConfig.Config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-06-25 09:18:37 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-14 13:34:10 +00:00
|
|
|
}
|
2021-06-25 09:18:37 +00:00
|
|
|
|
2021-11-14 13:34:10 +00:00
|
|
|
if val.AppConfig.JSONRPC.Enable && val.AppConfig.JSONRPC.Address != "" {
|
|
|
|
if val.Ctx == nil || val.Ctx.Viper == nil {
|
|
|
|
return fmt.Errorf("validator %s context is nil", val.Moniker)
|
2021-06-25 09:18:37 +00:00
|
|
|
}
|
|
|
|
|
2021-11-14 13:34:10 +00:00
|
|
|
tmEndpoint := "/websocket"
|
2022-03-24 07:56:59 +00:00
|
|
|
tmRPCAddr := val.RPCAddress
|
2021-06-25 09:18:37 +00:00
|
|
|
|
2022-10-10 10:38:33 +00:00
|
|
|
val.jsonrpc, val.jsonrpcDone, err = server.StartJSONRPC(val.Ctx, val.ClientCtx, tmRPCAddr, tmEndpoint, val.AppConfig, nil)
|
2021-11-14 13:34:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-06-25 09:18:37 +00:00
|
|
|
|
2021-11-14 13:34:10 +00:00
|
|
|
address := fmt.Sprintf("http://%s", val.AppConfig.JSONRPC.Address)
|
2021-06-25 09:18:37 +00:00
|
|
|
|
2021-11-14 13:34:10 +00:00
|
|
|
val.JSONRPCClient, err = ethclient.Dial(address)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to dial JSON-RPC at %s: %w", val.AppConfig.JSONRPC.Address, err)
|
2021-06-25 09:18:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func collectGenFiles(cfg Config, vals []*Validator, outputDir string) error {
|
2022-04-29 04:20:27 +00:00
|
|
|
genTime := tmtime.Now()
|
2021-06-25 09:18:37 +00:00
|
|
|
|
|
|
|
for i := 0; i < cfg.NumValidators; i++ {
|
|
|
|
tmCfg := vals[i].Ctx.Config
|
|
|
|
|
2022-10-10 10:38:33 +00:00
|
|
|
nodeDir := filepath.Join(outputDir, vals[i].Moniker, "evmosd")
|
2021-06-25 09:18:37 +00:00
|
|
|
gentxsDir := filepath.Join(outputDir, "gentxs")
|
|
|
|
|
|
|
|
tmCfg.Moniker = vals[i].Moniker
|
|
|
|
tmCfg.SetRoot(nodeDir)
|
|
|
|
|
|
|
|
initCfg := genutiltypes.NewInitConfig(cfg.ChainID, gentxsDir, vals[i].NodeID, vals[i].PubKey)
|
|
|
|
|
|
|
|
genFile := tmCfg.GenesisFile()
|
|
|
|
genDoc, err := types.GenesisDocFromFile(genFile)
|
|
|
|
if err != nil {
|
2021-11-14 13:34:10 +00:00
|
|
|
return err
|
2021-06-25 09:18:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
appState, err := genutil.GenAppStateFromConfig(cfg.Codec, cfg.TxConfig,
|
|
|
|
tmCfg, initCfg, *genDoc, banktypes.GenesisBalancesIterator{})
|
|
|
|
if err != nil {
|
2021-11-14 13:34:10 +00:00
|
|
|
return err
|
2021-06-25 09:18:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// overwrite each validator's genesis file to have a canonical genesis time
|
|
|
|
if err := genutil.ExportGenesisFileWithTime(genFile, cfg.ChainID, nil, appState, genTime); err != nil {
|
2021-11-14 13:34:10 +00:00
|
|
|
return err
|
2021-06-25 09:18:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func initGenFiles(cfg Config, genAccounts []authtypes.GenesisAccount, genBalances []banktypes.Balance, genFiles []string) error {
|
|
|
|
// set the accounts in the genesis state
|
|
|
|
var authGenState authtypes.GenesisState
|
|
|
|
cfg.Codec.MustUnmarshalJSON(cfg.GenesisState[authtypes.ModuleName], &authGenState)
|
|
|
|
|
|
|
|
accounts, err := authtypes.PackAccounts(genAccounts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-14 13:34:10 +00:00
|
|
|
authGenState.Accounts = append(authGenState.Accounts, accounts...)
|
2021-06-25 09:18:37 +00:00
|
|
|
cfg.GenesisState[authtypes.ModuleName] = cfg.Codec.MustMarshalJSON(&authGenState)
|
|
|
|
|
|
|
|
// set the balances in the genesis state
|
|
|
|
var bankGenState banktypes.GenesisState
|
2022-10-11 10:13:38 +00:00
|
|
|
cfg.Codec.MustUnmarshalJSON(cfg.GenesisState[banktypes.ModuleName], &bankGenState)
|
|
|
|
|
2021-06-25 09:18:37 +00:00
|
|
|
bankGenState.Balances = genBalances
|
|
|
|
cfg.GenesisState[banktypes.ModuleName] = cfg.Codec.MustMarshalJSON(&bankGenState)
|
|
|
|
|
|
|
|
var stakingGenState stakingtypes.GenesisState
|
|
|
|
cfg.Codec.MustUnmarshalJSON(cfg.GenesisState[stakingtypes.ModuleName], &stakingGenState)
|
|
|
|
|
2021-11-14 13:34:10 +00:00
|
|
|
stakingGenState.Params.BondDenom = cfg.BondDenom
|
2021-06-25 09:18:37 +00:00
|
|
|
cfg.GenesisState[stakingtypes.ModuleName] = cfg.Codec.MustMarshalJSON(&stakingGenState)
|
|
|
|
|
2022-10-10 10:38:33 +00:00
|
|
|
var govGenState govv1.GenesisState
|
2021-11-14 13:34:10 +00:00
|
|
|
cfg.Codec.MustUnmarshalJSON(cfg.GenesisState[govtypes.ModuleName], &govGenState)
|
|
|
|
|
|
|
|
govGenState.DepositParams.MinDeposit[0].Denom = cfg.BondDenom
|
|
|
|
cfg.GenesisState[govtypes.ModuleName] = cfg.Codec.MustMarshalJSON(&govGenState)
|
|
|
|
|
|
|
|
var mintGenState mintypes.GenesisState
|
|
|
|
cfg.Codec.MustUnmarshalJSON(cfg.GenesisState[mintypes.ModuleName], &mintGenState)
|
|
|
|
|
|
|
|
mintGenState.Params.MintDenom = cfg.BondDenom
|
|
|
|
cfg.GenesisState[mintypes.ModuleName] = cfg.Codec.MustMarshalJSON(&mintGenState)
|
|
|
|
|
|
|
|
var crisisGenState crisistypes.GenesisState
|
|
|
|
cfg.Codec.MustUnmarshalJSON(cfg.GenesisState[crisistypes.ModuleName], &crisisGenState)
|
|
|
|
|
|
|
|
crisisGenState.ConstantFee.Denom = cfg.BondDenom
|
|
|
|
cfg.GenesisState[crisistypes.ModuleName] = cfg.Codec.MustMarshalJSON(&crisisGenState)
|
|
|
|
|
|
|
|
var evmGenState evmtypes.GenesisState
|
|
|
|
cfg.Codec.MustUnmarshalJSON(cfg.GenesisState[evmtypes.ModuleName], &evmGenState)
|
|
|
|
|
|
|
|
evmGenState.Params.EvmDenom = cfg.BondDenom
|
|
|
|
cfg.GenesisState[evmtypes.ModuleName] = cfg.Codec.MustMarshalJSON(&evmGenState)
|
|
|
|
|
2021-06-25 09:18:37 +00:00
|
|
|
appGenStateJSON, err := json.MarshalIndent(cfg.GenesisState, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
genDoc := types.GenesisDoc{
|
|
|
|
ChainID: cfg.ChainID,
|
|
|
|
AppState: appGenStateJSON,
|
|
|
|
Validators: nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
// generate empty genesis files for each validator and save
|
|
|
|
for i := 0; i < cfg.NumValidators; i++ {
|
|
|
|
if err := genDoc.SaveAs(genFiles[i]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-14 13:34:10 +00:00
|
|
|
func WriteFile(name string, dir string, contents []byte) error {
|
2022-10-10 10:38:33 +00:00
|
|
|
file := filepath.Join(dir, name)
|
2022-04-11 08:09:39 +00:00
|
|
|
|
2022-10-10 10:38:33 +00:00
|
|
|
err := tmos.EnsureDir(dir, 0o755)
|
2022-04-11 08:09:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-06-25 09:18:37 +00:00
|
|
|
|
2022-10-10 10:38:33 +00:00
|
|
|
return tmos.WriteFile(file, contents, 0o644)
|
2021-06-25 09:18:37 +00:00
|
|
|
}
|