Skip to content

Commit

Permalink
init node
Browse files Browse the repository at this point in the history
  • Loading branch information
likesToEatFish committed Sep 28, 2024
1 parent dfb928e commit df91385
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 16 deletions.
48 changes: 33 additions & 15 deletions ignite/cmd/testnet_multi_node.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package ignitecmd

import (
"fmt"
"math/rand"
"strconv"

"cosmossdk.io/math"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -59,10 +59,10 @@ func testnetMultiNodeHandler(cmd *cobra.Command, _ []string) error {
)
defer session.End()

return testnetInplace1(cmd, session)
return testnetMultiNode(cmd, session)
}

func testnetInplace1(cmd *cobra.Command, session *cliui.Session) error {
func testnetMultiNode(cmd *cobra.Command, session *cliui.Session) error {
chainOption := []chain.Option{
chain.WithOutputer(session),
chain.CollectEvents(session.EventBus()),
Expand All @@ -89,44 +89,62 @@ func testnetInplace1(cmd *cobra.Command, session *cliui.Session) error {
return err
}

validatorDetails, err := getValidatorAmountStake(cfg.MultiNode)
numVal, amountDetails, err := getValidatorAmountStake(cfg.MultiNode)
if err != nil {
return err
}
fmt.Println(validatorDetails)
fmt.Println(cfg.MultiNode.OutputDir)
args := chain.MultiNodeArgs{
ChainID: cfg.MultiNode.ChainID,
ValidatorsStakeAmount: amountDetails,
OutputDir: cfg.MultiNode.OutputDir,
NumValidator: strconv.Itoa(numVal),
}

return nil
return c.TestnetMultiNode(cmd.Context(), args)
}

func getValidatorAmountStake(cfg base.MultiNode) ([]math.Int, error) {
var amounts []math.Int
// getValidatorAmountStake returns the number of validators and the amountStakes arg from config.MultiNode
func getValidatorAmountStake(cfg base.MultiNode) (int, string, error) {
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 amounts, err
return count, amounts, err
}
maxStake, err := sdk.ParseCoinNormalized(cfg.RandomValidators.MaxStake)
if err != nil {
return amounts, err
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)
amounts = append(amounts, math.NewIntFromUint64(stakeAmount))
if amounts == "" {
amounts = math.NewIntFromUint64(stakeAmount).String()
count += 1
} else {
amounts = amounts + "," + math.NewIntFromUint64(stakeAmount).String()
count += 1
}
}
} else {
for _, v := range cfg.Validators {
stakeAmount, err := sdk.ParseCoinNormalized(v.Stake)
if err != nil {
return amounts, err
return count, amounts, err
}
if amounts == "" {
amounts = stakeAmount.Amount.String()
count += 1
} else {
amounts = amounts + "," + stakeAmount.Amount.String()
count += 1
}
amounts = append(amounts, stakeAmount.Amount)
}
}

return amounts, nil
return count, amounts, nil
}
1 change: 1 addition & 0 deletions ignite/config/chain/base/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ type MultiNode struct {
Validators []ValidatorDetails `yaml:"validators" doc:"List of manually configured validators."`
RandomValidators RandomValidatorDetails `yaml:"random_validators" doc:"Configuration for randomly generated validators."`
OutputDir string `yaml:"output-dir" doc:"Directory to store initialization data for the testnet"`
ChainID string `yaml:"chain-id" doc:"Directory to store initialization data for the testnet"`
}

// GetVersion returns the config version.
Expand Down
4 changes: 4 additions & 0 deletions ignite/pkg/chaincmd/chaincmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const (
commandExport = "export"
commandTendermint = "tendermint"
commandTestnetInPlace = "in-place-testnet"
commandTestnetMultiNode = "multi-node"

optionHome = "--home"
optionNode = "--node"
Expand Down Expand Up @@ -59,6 +60,9 @@ const (
optionValidatorPrivateKey = "--validator-privkey"
optionAccountToFund = "--accounts-to-fund"
optionSkipConfirmation = "--skip-confirmation"
optionAmountStakes = "--validators-stake-amount"
optionOutPutDir = "--output-dir"
optionNumValidator = "--v"

constTendermint = "tendermint"
constJSON = "json"
Expand Down
54 changes: 54 additions & 0 deletions ignite/pkg/chaincmd/in-place-testnet.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package chaincmd

import (
"fmt"

"github.com/ignite/cli/v29/ignite/pkg/cmdrunner/step"
)

Expand Down Expand Up @@ -45,3 +47,55 @@ func (c ChainCmd) TestnetInPlaceCommand(newChainID, newOperatorAddress string, o

return c.daemonCommand(command)
}

type MultiNodeOption func([]string) []string

func MultiNodeWithChainID(ChainId string) MultiNodeOption {
return func(s []string) []string {
if len(ChainId) > 0 {
return append(s, optionChainID, ChainId)
}
return s
}
}

func MultiNodeWithDirOutput(dirOutput string) MultiNodeOption {
return func(s []string) []string {
if len(dirOutput) > 0 {
return append(s, optionOutPutDir, dirOutput)
}
return s
}
}

func MultiNodeWithNumValidator(numVal string) MultiNodeOption {
return func(s []string) []string {
if len(numVal) > 0 {
return append(s, optionNumValidator, numVal)
}
return s
}
}
func MultiNodeWithValidatorsStakeAmount(satkeAmounts string) MultiNodeOption {
return func(s []string) []string {
if len(satkeAmounts) > 0 {
return append(s, optionAmountStakes, satkeAmounts)
}
return s
}
}

// TestnetMultiNodeCommand return command to start testnet multinode.
func (c ChainCmd) TestnetMultiNodeCommand(options ...MultiNodeOption) step.Option {
command := []string{
commandTestnetMultiNode,
}

// Apply the options provided by the user
for _, apply := range options {
command = apply(command)
}
fmt.Println(command)

return c.daemonCommand(command)
}
12 changes: 12 additions & 0 deletions ignite/pkg/chaincmd/runner/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ func (r Runner) InPlace(ctx context.Context, newChainID, newOperatorAddress stri
)
}

func (r Runner) MultiNode(ctx context.Context, options ...chaincmd.MultiNodeOption) error {
runOptions := runOptions{
stdout: os.Stdout,
stderr: os.Stderr,
}
return r.run(
ctx,
runOptions,
r.chainCmd.TestnetMultiNodeCommand(options...),
)
}

// Gentx generates a genesis tx carrying a self delegation.
func (r Runner) Gentx(
ctx context.Context,
Expand Down
10 changes: 10 additions & 0 deletions ignite/services/chain/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ func (c Chain) InPlace(ctx context.Context, runner chaincmdrunner.Runner, args I
return err
}

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),
)
return err
}

// Start wraps the "appd start" command to begin running a chain from the daemon.
func (c Chain) Start(ctx context.Context, runner chaincmdrunner.Runner, cfg *chainconfig.Config) error {
validator, err := chainconfig.FirstValidator(cfg)
Expand Down
29 changes: 29 additions & 0 deletions ignite/services/chain/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,32 @@ func (c Chain) TestnetInPlace(ctx context.Context, args InPlaceArgs) error {
}
return nil
}

type MultiNodeArgs struct {
ChainID string
OutputDir string
NumValidator string
ValidatorsStakeAmount string
}

func (c Chain) TestnetMultiNode(ctx context.Context, args MultiNodeArgs) error {
commands, err := c.Commands(ctx)
if err != nil {
return err
}

// make sure that config.yml exists
if c.options.ConfigFile != "" {
if _, err := os.Stat(c.options.ConfigFile); err != nil {
return err
}
} else if _, err := chainconfig.LocateDefault(c.app.Path); err != nil {
return err
}

err = c.MultiNode(ctx, commands, args)
if err != nil {
return err
}
return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ func initTestnetFiles(
if err := initGenFiles(clientCtx, mbm, args.chainID, genAccounts, genBalances, genFiles, args.numValidators); err != nil {
return err
}
// copy gentx file
for i := 0; i < args.numValidators; i++ {
for _, file := range gentxsFiles {
nodeDirName := fmt.Sprintf("%s%d", args.nodeDirPrefix, i)
Expand Down
2 changes: 1 addition & 1 deletion ignite/templates/app/files/config.yml.plush
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ multi-node:
count: 3
min_stake: 50000000stake
max_stake: 150000000stake
output-dir: $HOME/.<%= AppName %>d
output-dir: ./.<%= AppName %>-testnet/
chain-id: <%= AppName %>-test-1

0 comments on commit df91385

Please sign in to comment.