github.com/vipernet-xyz/tm@v0.34.24/test/e2e/runner/setup.go (about)

     1  package main
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/base64"
     6  	"encoding/json"
     7  	"errors"
     8  	"fmt"
     9  	"os"
    10  	"path/filepath"
    11  	"regexp"
    12  	"sort"
    13  	"strconv"
    14  	"strings"
    15  	"time"
    16  
    17  	"github.com/BurntSushi/toml"
    18  
    19  	"github.com/vipernet-xyz/tm/config"
    20  	"github.com/vipernet-xyz/tm/crypto/ed25519"
    21  	"github.com/vipernet-xyz/tm/libs/log"
    22  	"github.com/vipernet-xyz/tm/p2p"
    23  	"github.com/vipernet-xyz/tm/privval"
    24  	e2e "github.com/vipernet-xyz/tm/test/e2e/pkg"
    25  	"github.com/vipernet-xyz/tm/test/e2e/pkg/infra"
    26  	"github.com/vipernet-xyz/tm/types"
    27  )
    28  
    29  const (
    30  	AppAddressTCP  = "tcp://127.0.0.1:30000"
    31  	AppAddressUNIX = "unix:///var/run/app.sock"
    32  
    33  	PrivvalAddressTCP     = "tcp://0.0.0.0:27559"
    34  	PrivvalAddressUNIX    = "unix:///var/run/privval.sock"
    35  	PrivvalKeyFile        = "config/priv_validator_key.json"
    36  	PrivvalStateFile      = "data/priv_validator_state.json"
    37  	PrivvalDummyKeyFile   = "config/dummy_validator_key.json"
    38  	PrivvalDummyStateFile = "data/dummy_validator_state.json"
    39  )
    40  
    41  // Setup sets up the testnet configuration.
    42  func Setup(testnet *e2e.Testnet, infp infra.Provider) error {
    43  	logger.Info("setup", "msg", log.NewLazySprintf("Generating testnet files in %q", testnet.Dir))
    44  
    45  	err := os.MkdirAll(testnet.Dir, os.ModePerm)
    46  	if err != nil {
    47  		return err
    48  	}
    49  
    50  	err = infp.Setup()
    51  	if err != nil {
    52  		return err
    53  	}
    54  
    55  	genesis, err := MakeGenesis(testnet)
    56  	if err != nil {
    57  		return err
    58  	}
    59  
    60  	for _, node := range testnet.Nodes {
    61  		nodeDir := filepath.Join(testnet.Dir, node.Name)
    62  
    63  		dirs := []string{
    64  			filepath.Join(nodeDir, "config"),
    65  			filepath.Join(nodeDir, "data"),
    66  			filepath.Join(nodeDir, "data", "app"),
    67  		}
    68  		for _, dir := range dirs {
    69  			// light clients don't need an app directory
    70  			if node.Mode == e2e.ModeLight && strings.Contains(dir, "app") {
    71  				continue
    72  			}
    73  			err := os.MkdirAll(dir, 0o755)
    74  			if err != nil {
    75  				return err
    76  			}
    77  		}
    78  
    79  		cfg, err := MakeConfig(node)
    80  		if err != nil {
    81  			return err
    82  		}
    83  		config.WriteConfigFile(filepath.Join(nodeDir, "config", "config.toml"), cfg) // panics
    84  
    85  		appCfg, err := MakeAppConfig(node)
    86  		if err != nil {
    87  			return err
    88  		}
    89  		//nolint:gosec // G306: Expect WriteFile permissions to be 0600 or less
    90  		err = os.WriteFile(filepath.Join(nodeDir, "config", "app.toml"), appCfg, 0o644)
    91  		if err != nil {
    92  			return err
    93  		}
    94  
    95  		if node.Mode == e2e.ModeLight {
    96  			// stop early if a light client
    97  			continue
    98  		}
    99  
   100  		err = genesis.SaveAs(filepath.Join(nodeDir, "config", "genesis.json"))
   101  		if err != nil {
   102  			return err
   103  		}
   104  
   105  		err = (&p2p.NodeKey{PrivKey: node.NodeKey}).SaveAs(filepath.Join(nodeDir, "config", "node_key.json"))
   106  		if err != nil {
   107  			return err
   108  		}
   109  
   110  		(privval.NewFilePV(node.PrivvalKey,
   111  			filepath.Join(nodeDir, PrivvalKeyFile),
   112  			filepath.Join(nodeDir, PrivvalStateFile),
   113  		)).Save()
   114  
   115  		// Set up a dummy validator. Tendermint requires a file PV even when not used, so we
   116  		// give it a dummy such that it will fail if it actually tries to use it.
   117  		(privval.NewFilePV(ed25519.GenPrivKey(),
   118  			filepath.Join(nodeDir, PrivvalDummyKeyFile),
   119  			filepath.Join(nodeDir, PrivvalDummyStateFile),
   120  		)).Save()
   121  	}
   122  
   123  	return nil
   124  }
   125  
   126  // MakeGenesis generates a genesis document.
   127  func MakeGenesis(testnet *e2e.Testnet) (types.GenesisDoc, error) {
   128  	genesis := types.GenesisDoc{
   129  		GenesisTime:     time.Now(),
   130  		ChainID:         testnet.Name,
   131  		ConsensusParams: types.DefaultConsensusParams(),
   132  		InitialHeight:   testnet.InitialHeight,
   133  	}
   134  	// set the app version to 1
   135  	genesis.ConsensusParams.Version.AppVersion = 1
   136  	for validator, power := range testnet.Validators {
   137  		genesis.Validators = append(genesis.Validators, types.GenesisValidator{
   138  			Name:    validator.Name,
   139  			Address: validator.PrivvalKey.PubKey().Address(),
   140  			PubKey:  validator.PrivvalKey.PubKey(),
   141  			Power:   power,
   142  		})
   143  	}
   144  	// The validator set will be sorted internally by Tendermint ranked by power,
   145  	// but we sort it here as well so that all genesis files are identical.
   146  	sort.Slice(genesis.Validators, func(i, j int) bool {
   147  		return strings.Compare(genesis.Validators[i].Name, genesis.Validators[j].Name) == -1
   148  	})
   149  	if len(testnet.InitialState) > 0 {
   150  		appState, err := json.Marshal(testnet.InitialState)
   151  		if err != nil {
   152  			return genesis, err
   153  		}
   154  		genesis.AppState = appState
   155  	}
   156  	return genesis, genesis.ValidateAndComplete()
   157  }
   158  
   159  // MakeConfig generates a Tendermint config for a node.
   160  func MakeConfig(node *e2e.Node) (*config.Config, error) {
   161  	cfg := config.DefaultConfig()
   162  	cfg.Moniker = node.Name
   163  	cfg.ProxyApp = AppAddressTCP
   164  	cfg.RPC.ListenAddress = "tcp://0.0.0.0:26657"
   165  	cfg.RPC.PprofListenAddress = ":6060"
   166  	cfg.P2P.ExternalAddress = fmt.Sprintf("tcp://%v", node.AddressP2P(false))
   167  	cfg.P2P.AddrBookStrict = false
   168  	cfg.DBBackend = node.Database
   169  	cfg.StateSync.DiscoveryTime = 5 * time.Second
   170  
   171  	switch node.ABCIProtocol {
   172  	case e2e.ProtocolUNIX:
   173  		cfg.ProxyApp = AppAddressUNIX
   174  	case e2e.ProtocolTCP:
   175  		cfg.ProxyApp = AppAddressTCP
   176  	case e2e.ProtocolGRPC:
   177  		cfg.ProxyApp = AppAddressTCP
   178  		cfg.ABCI = "grpc"
   179  	case e2e.ProtocolBuiltin:
   180  		cfg.ProxyApp = ""
   181  		cfg.ABCI = ""
   182  	default:
   183  		return nil, fmt.Errorf("unexpected ABCI protocol setting %q", node.ABCIProtocol)
   184  	}
   185  
   186  	// Tendermint errors if it does not have a privval key set up, regardless of whether
   187  	// it's actually needed (e.g. for remote KMS or non-validators). We set up a dummy
   188  	// key here by default, and use the real key for actual validators that should use
   189  	// the file privval.
   190  	cfg.PrivValidatorListenAddr = ""
   191  	cfg.PrivValidatorKey = PrivvalDummyKeyFile
   192  	cfg.PrivValidatorState = PrivvalDummyStateFile
   193  
   194  	switch node.Mode {
   195  	case e2e.ModeValidator:
   196  		switch node.PrivvalProtocol {
   197  		case e2e.ProtocolFile:
   198  			cfg.PrivValidatorKey = PrivvalKeyFile
   199  			cfg.PrivValidatorState = PrivvalStateFile
   200  		case e2e.ProtocolUNIX:
   201  			cfg.PrivValidatorListenAddr = PrivvalAddressUNIX
   202  		case e2e.ProtocolTCP:
   203  			cfg.PrivValidatorListenAddr = PrivvalAddressTCP
   204  		default:
   205  			return nil, fmt.Errorf("invalid privval protocol setting %q", node.PrivvalProtocol)
   206  		}
   207  	case e2e.ModeSeed:
   208  		cfg.P2P.SeedMode = true
   209  		cfg.P2P.PexReactor = true
   210  	case e2e.ModeFull, e2e.ModeLight:
   211  		// Don't need to do anything, since we're using a dummy privval key by default.
   212  	default:
   213  		return nil, fmt.Errorf("unexpected mode %q", node.Mode)
   214  	}
   215  	if node.Mempool != "" {
   216  		cfg.Mempool.Version = node.Mempool
   217  	}
   218  
   219  	if node.FastSync == "" {
   220  		cfg.FastSyncMode = false
   221  	} else {
   222  		cfg.FastSync.Version = node.FastSync
   223  	}
   224  
   225  	if node.StateSync {
   226  		cfg.StateSync.Enable = true
   227  		cfg.StateSync.RPCServers = []string{}
   228  		for _, peer := range node.Testnet.ArchiveNodes() {
   229  			if peer.Name == node.Name {
   230  				continue
   231  			}
   232  			cfg.StateSync.RPCServers = append(cfg.StateSync.RPCServers, peer.AddressRPC())
   233  		}
   234  		if len(cfg.StateSync.RPCServers) < 2 {
   235  			return nil, errors.New("unable to find 2 suitable state sync RPC servers")
   236  		}
   237  	}
   238  
   239  	cfg.P2P.Seeds = ""
   240  	for _, seed := range node.Seeds {
   241  		if len(cfg.P2P.Seeds) > 0 {
   242  			cfg.P2P.Seeds += ","
   243  		}
   244  		cfg.P2P.Seeds += seed.AddressP2P(true)
   245  	}
   246  	cfg.P2P.PersistentPeers = ""
   247  	for _, peer := range node.PersistentPeers {
   248  		if len(cfg.P2P.PersistentPeers) > 0 {
   249  			cfg.P2P.PersistentPeers += ","
   250  		}
   251  		cfg.P2P.PersistentPeers += peer.AddressP2P(true)
   252  	}
   253  	return cfg, nil
   254  }
   255  
   256  // MakeAppConfig generates an ABCI application config for a node.
   257  func MakeAppConfig(node *e2e.Node) ([]byte, error) {
   258  	cfg := map[string]interface{}{
   259  		"chain_id":          node.Testnet.Name,
   260  		"dir":               "data/app",
   261  		"listen":            AppAddressUNIX,
   262  		"mode":              node.Mode,
   263  		"proxy_port":        node.ProxyPort,
   264  		"protocol":          "socket",
   265  		"persist_interval":  node.PersistInterval,
   266  		"snapshot_interval": node.SnapshotInterval,
   267  		"retain_blocks":     node.RetainBlocks,
   268  		"key_type":          node.PrivvalKey.Type(),
   269  	}
   270  	switch node.ABCIProtocol {
   271  	case e2e.ProtocolUNIX:
   272  		cfg["listen"] = AppAddressUNIX
   273  	case e2e.ProtocolTCP:
   274  		cfg["listen"] = AppAddressTCP
   275  	case e2e.ProtocolGRPC:
   276  		cfg["listen"] = AppAddressTCP
   277  		cfg["protocol"] = "grpc"
   278  	case e2e.ProtocolBuiltin:
   279  		delete(cfg, "listen")
   280  		cfg["protocol"] = "builtin"
   281  	default:
   282  		return nil, fmt.Errorf("unexpected ABCI protocol setting %q", node.ABCIProtocol)
   283  	}
   284  	if node.Mode == e2e.ModeValidator {
   285  		switch node.PrivvalProtocol {
   286  		case e2e.ProtocolFile:
   287  		case e2e.ProtocolTCP:
   288  			cfg["privval_server"] = PrivvalAddressTCP
   289  			cfg["privval_key"] = PrivvalKeyFile
   290  			cfg["privval_state"] = PrivvalStateFile
   291  		case e2e.ProtocolUNIX:
   292  			cfg["privval_server"] = PrivvalAddressUNIX
   293  			cfg["privval_key"] = PrivvalKeyFile
   294  			cfg["privval_state"] = PrivvalStateFile
   295  		default:
   296  			return nil, fmt.Errorf("unexpected privval protocol setting %q", node.PrivvalProtocol)
   297  		}
   298  	}
   299  
   300  	misbehaviors := make(map[string]string)
   301  	for height, misbehavior := range node.Misbehaviors {
   302  		misbehaviors[strconv.Itoa(int(height))] = misbehavior
   303  	}
   304  	cfg["misbehaviors"] = misbehaviors
   305  
   306  	if len(node.Testnet.ValidatorUpdates) > 0 {
   307  		validatorUpdates := map[string]map[string]int64{}
   308  		for height, validators := range node.Testnet.ValidatorUpdates {
   309  			updateVals := map[string]int64{}
   310  			for node, power := range validators {
   311  				updateVals[base64.StdEncoding.EncodeToString(node.PrivvalKey.PubKey().Bytes())] = power
   312  			}
   313  			validatorUpdates[fmt.Sprintf("%v", height)] = updateVals
   314  		}
   315  		cfg["validator_update"] = validatorUpdates
   316  	}
   317  
   318  	var buf bytes.Buffer
   319  	err := toml.NewEncoder(&buf).Encode(cfg)
   320  	if err != nil {
   321  		return nil, fmt.Errorf("failed to generate app config: %w", err)
   322  	}
   323  	return buf.Bytes(), nil
   324  }
   325  
   326  // UpdateConfigStateSync updates the state sync config for a node.
   327  func UpdateConfigStateSync(node *e2e.Node, height int64, hash []byte) error {
   328  	cfgPath := filepath.Join(node.Testnet.Dir, node.Name, "config", "config.toml")
   329  
   330  	// FIXME Apparently there's no function to simply load a config file without
   331  	// involving the entire Viper apparatus, so we'll just resort to regexps.
   332  	bz, err := os.ReadFile(cfgPath)
   333  	if err != nil {
   334  		return err
   335  	}
   336  	bz = regexp.MustCompile(`(?m)^trust_height =.*`).ReplaceAll(bz, []byte(fmt.Sprintf(`trust_height = %v`, height)))
   337  	bz = regexp.MustCompile(`(?m)^trust_hash =.*`).ReplaceAll(bz, []byte(fmt.Sprintf(`trust_hash = "%X"`, hash)))
   338  	//nolint:gosec // G306: Expect WriteFile permissions to be 0600 or less
   339  	return os.WriteFile(cfgPath, bz, 0o644)
   340  }