Skip to content

Commit

Permalink
use config.yml with more validators
Browse files Browse the repository at this point in the history
  • Loading branch information
likesToEatFish committed Oct 2, 2024
1 parent dedd592 commit c2fb623
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 84 deletions.
3 changes: 1 addition & 2 deletions ignite/cmd/model/testnet_multi_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,9 @@ func (m MultiNode) View() string {
}

infoNode := func(i int) string {
chainID := m.args.ChainID
home := m.args.OutputDir
ipaddr := "tcp://127.0.0.1:" + strconv.Itoa(26657-3*i)
return fmt.Sprintf("INFO: ChainID:%s | Home:%s | Node:%s ", chainID, home, ipaddr)
return fmt.Sprintf("INFO:| Home:%s | Node:%s ", home, ipaddr)
}

output := "Press keys 1,2,3.. to start and stop node 1,2,3.. respectively \nNode Control:\n"
Expand Down
64 changes: 20 additions & 44 deletions ignite/cmd/testnet_multi_node.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ignitecmd

import (
"math/rand"
"os"
"path/filepath"
"strconv"
Expand All @@ -10,16 +9,18 @@ import (
tea "github.com/charmbracelet/bubbletea"
"github.com/spf13/cobra"

"cosmossdk.io/math"

sdk "github.com/cosmos/cosmos-sdk/types"

cmdmodel "github.com/ignite/cli/v29/ignite/cmd/model"
"github.com/ignite/cli/v29/ignite/config/chain/base"
"github.com/ignite/cli/v29/ignite/config/chain/v1"
"github.com/ignite/cli/v29/ignite/pkg/cliui"
"github.com/ignite/cli/v29/ignite/services/chain"
)

const (
flagNodeDirPrefix = "node-dir-prefix"
)

func NewTestnetMultiNode() *cobra.Command {
c := &cobra.Command{
Use: "multi-node",
Expand Down Expand Up @@ -63,6 +64,7 @@ func NewTestnetMultiNode() *cobra.Command {
c.Flags().AddFlagSet(flagSetSkipProto())
c.Flags().AddFlagSet(flagSetVerbose())
c.Flags().BoolP(flagResetOnce, "r", false, "reset the app state once on init")
c.Flags().String(flagNodeDirPrefix, "validator", "prefix of dir node")

c.Flags().Bool(flagQuitOnFail, false, "quit program if the app fails to start")
return c
Expand Down Expand Up @@ -104,22 +106,22 @@ func testnetMultiNode(cmd *cobra.Command, session *cliui.Session) error {
return err
}

numVal, amountDetails, err := getValidatorAmountStake(cfg.MultiNode)
numVal, amountDetails, err := getValidatorAmountStake(cfg.Validators)
if err != nil {
return err
}
homeDir, err := os.UserHomeDir()
if err != nil {
return err
}
nodeDirPrefix, _ := cmd.Flags().GetString(flagNodeDirPrefix)

outputDir := filepath.Join(homeDir, ".ignite/local-chains/"+c.Name()+"d/testnet/")
args := chain.MultiNodeArgs{
ChainID: cfg.MultiNode.ChainID,
ValidatorsStakeAmount: amountDetails,
OutputDir: outputDir,
NumValidator: strconv.Itoa(numVal),
NodeDirPrefix: cfg.MultiNode.NodeDirPrefix,
ValidatorsStakeAmount: amountDetails,
NodeDirPrefix: nodeDirPrefix,
}

resetOnce, _ := cmd.Flags().GetBool(flagResetOnce)
Expand All @@ -144,47 +146,21 @@ func testnetMultiNode(cmd *cobra.Command, session *cliui.Session) error {
}

// getValidatorAmountStake returns the number of validators and the amountStakes arg from config.MultiNode.
func getValidatorAmountStake(cfg base.MultiNode) (int, string, error) {
func getValidatorAmountStake(validators []v1.Validator) (int, string, error) {
numVal := len(validators)
var amounts string
count := 0

if len(cfg.Validators) == 0 {
numVal := cfg.RandomValidators.Count
minStake, err := sdk.ParseCoinNormalized(cfg.RandomValidators.MinStake)
if err != nil {
return count, amounts, err
}
maxStake, err := sdk.ParseCoinNormalized(cfg.RandomValidators.MaxStake)
for _, v := range validators {
stakeAmount, err := sdk.ParseCoinNormalized(v.Bonded)
if err != nil {
return count, amounts, err
}
minS := minStake.Amount.Uint64()
maxS := maxStake.Amount.Uint64()
for i := 0; i < numVal; i++ {
stakeAmount := minS + rand.Uint64()%(maxS-minS+1) // #nosec G404
if amounts == "" {
amounts = math.NewIntFromUint64(stakeAmount).String()
count++
} else {
amounts = amounts + "," + math.NewIntFromUint64(stakeAmount).String()
count++
}
return numVal, amounts, err
}
} else {
for _, v := range cfg.Validators {
stakeAmount, err := sdk.ParseCoinNormalized(v.Stake)
if err != nil {
return count, amounts, err
}
if amounts == "" {
amounts = stakeAmount.Amount.String()
count++
} else {
amounts = amounts + "," + stakeAmount.Amount.String()
count++
}
if amounts == "" {
amounts = stakeAmount.Amount.String()
} else {
amounts = amounts + "," + stakeAmount.Amount.String()
}
}

return count, amounts, nil
return numVal, amounts, nil
}
22 changes: 0 additions & 22 deletions ignite/config/chain/base/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,28 +141,6 @@ type Config struct {
Client Client `yaml:"client,omitempty" doc:"Configures client code generation."`
Genesis xyaml.Map `yaml:"genesis,omitempty" doc:"Custom genesis block modifications. Follow the nesting of the genesis file here to access all the parameters."`
Minimal bool `yaml:"minimal,omitempty" doc:"Indicates if the blockchain is minimal with the required Cosmos SDK modules."`
MultiNode MultiNode `yaml:"multi-node" doc:"Configuration for testnet multi node."`
}

// Validator defines the configuration for a single validator.
type ValidatorDetails struct {
Name string `yaml:"name" doc:"Name of the validator."`
Stake string `yaml:"stake" doc:"Amount of stake associated with the validator."`
}

// RandomValidator defines the configuration for random validators.
type RandomValidatorDetails struct {
Count int `yaml:"count" doc:"Number of random validators to be generated."`
MinStake string `yaml:"min_stake" doc:"Minimum stake for each random validator."`
MaxStake string `yaml:"max_stake" doc:"Maximum stake for each random validator."`
}

// MultiNode holds the configuration related to multiple validators and random validators.
type MultiNode struct {
Validators []ValidatorDetails `yaml:"validators" doc:"List of manually configured validators."`
RandomValidators RandomValidatorDetails `yaml:"random_validators" doc:"Configuration for randomly generated validators."`
ChainID string `yaml:"chain-id" doc:"Chain id for the testnet"`
NodeDirPrefix string `yaml:"node-dir-prefix" doc:"Node directory prefix for the testnet"`
}

// GetVersion returns the config version.
Expand Down
1 change: 1 addition & 0 deletions ignite/pkg/chaincmd/chaincmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const (
optionAmountStakes = "--validators-stake-amount"
optionOutPutDir = "--output-dir"
optionNumValidator = "--v"
optionNodeDirPrefix = "--node-dir-prefix"

constTendermint = "tendermint"
constJSON = "json"
Expand Down
9 changes: 9 additions & 0 deletions ignite/pkg/chaincmd/in-place-testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ func MultiNodeWithValidatorsStakeAmount(satkeAmounts string) MultiNodeOption {
}
}

func MultiNodeDirPrefix(nodeDirPrefix string) MultiNodeOption {
return func(s []string) []string {
if len(nodeDirPrefix) > 0 {
return append(s, optionNodeDirPrefix, nodeDirPrefix)
}
return s
}
}

// TestnetMultiNodeCommand return command to start testnet multinode.
func (c ChainCmd) TestnetMultiNodeCommand(options ...MultiNodeOption) step.Option {
command := []string{
Expand Down
2 changes: 1 addition & 1 deletion ignite/services/chain/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ func (c Chain) InPlace(ctx context.Context, runner chaincmdrunner.Runner, args I
// MultiNode sets up multiple nodes in the chain network with the specified arguments and returns an error if any issue occurs.
func (c Chain) MultiNode(ctx context.Context, runner chaincmdrunner.Runner, args MultiNodeArgs) error {
err := runner.MultiNode(ctx,
chaincmd.MultiNodeWithChainID(args.ChainID),
chaincmd.MultiNodeWithDirOutput(args.OutputDir),
chaincmd.MultiNodeWithNumValidator(args.NumValidator),
chaincmd.MultiNodeWithValidatorsStakeAmount(args.ValidatorsStakeAmount),
chaincmd.MultiNodeDirPrefix(args.NodeDirPrefix),
)
return err
}
Expand Down
1 change: 0 additions & 1 deletion ignite/services/chain/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ func (c Chain) TestnetInPlace(ctx context.Context, args InPlaceArgs) error {
}

type MultiNodeArgs struct {
ChainID string
OutputDir string
NumValidator string
ValidatorsStakeAmount string
Expand Down
20 changes: 6 additions & 14 deletions ignite/templates/app/files/config.yml.plush
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,9 @@ faucet:
validators:
- name: alice
bonded: 100000000stake
multi-node:
validators:
- name: validator1
stake: 100000000stake
- name: validator2
stake: 200000000stake
- name: validator3
stake: 300000000stake
random_validators:
count: 3
min_stake: 50000000stake
max_stake: 150000000stake
chain-id: <%= AppName %>-test-1
node-dir-prefix: validator
- name: validator1
bonded: 100000000stake
- name: validator2
bonded: 200000000stake
- name: validator3
bonded: 300000000stake

0 comments on commit c2fb623

Please sign in to comment.