Skip to content

Commit

Permalink
feat(network): initialize init command (#1771)
Browse files Browse the repository at this point in the history
* feat(cmd): add network publish cmd

and refactor the network related code.

* re-add network command

* hide the network cmd

* Apply suggestions from code review

Co-authored-by: Danilo Pantani <danpantani@gmail.com>
Co-authored-by: Lucas Bertrand <lucas.bertrand.22@gmail.com>

* consistent event printing

* docs

* Command initialization

* misc modifications

* chain home

* chain fetch

* Lint

* init account

* Add validator description

* Account creation

* Add mnemonic

* overwrite confirmation

* Fix mnemonic typo

* starport/cmd/network_chain_init.go

* Initialize recover

* Small modification

* Network initialization

* Refactor genesis and chain verification

* lint

* Refactor account

* Fix no account

* validator name change

* import account command

* import starport account

* fix account created

* Remove todos

* Update starport/cmd/network_chain_init.go

Co-authored-by: İlker G. Öztürk <ilkergoktugozturk@gmail.com>

* shutdown renaming

* Validator flag

* Fix cli spinner

* comments

* Fix genesis URL

* spinner

* small text change

* comment

* Genesis initialization

* Refactor genesis url init

Co-authored-by: İlker G. Öztürk <ilkergoktugozturk@gmail.com>
Co-authored-by: Danilo Pantani <danpantani@gmail.com>
  • Loading branch information
3 people authored Nov 17, 2021
1 parent 8a4cef6 commit 9aa042f
Show file tree
Hide file tree
Showing 14 changed files with 656 additions and 226 deletions.
7 changes: 7 additions & 0 deletions starport/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
flag "github.com/spf13/pflag"
"github.com/tendermint/starport/starport/internal/version"
"github.com/tendermint/starport/starport/pkg/clispinner"
"github.com/tendermint/starport/starport/pkg/cosmosaccount"
"github.com/tendermint/starport/starport/pkg/cosmosver"
"github.com/tendermint/starport/starport/pkg/events"
"github.com/tendermint/starport/starport/pkg/gitpod"
Expand Down Expand Up @@ -110,6 +111,12 @@ func flagSetHome() *flag.FlagSet {
return fs
}

func flagNetworkFrom() *flag.FlagSet {
fs := flag.NewFlagSet("", flag.ContinueOnError)
fs.String(flagFrom, cosmosaccount.DefaultAccount, "Account name to use for sending transactions to SPN")
return fs
}

func getHome(cmd *cobra.Command) (home string) {
home, _ = cmd.Flags().GetString(flagHome)
return
Expand Down
32 changes: 32 additions & 0 deletions starport/cmd/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package starportcmd

import (
"fmt"
"sync"

"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/tendermint/starport/starport/pkg/clispinner"
"github.com/tendermint/starport/starport/pkg/cosmosaccount"
"github.com/tendermint/starport/starport/pkg/cosmosclient"
"github.com/tendermint/starport/starport/pkg/events"
"github.com/tendermint/starport/starport/pkg/gitpod"
"github.com/tendermint/starport/starport/services/network"
)
Expand Down Expand Up @@ -67,6 +70,35 @@ func NewNetwork() *cobra.Command {

var cosmos *cosmosclient.Client

// initializeNetwork initializes event bus, CLIn components such as spinner and returns a new network builder
func initializeNetwork(cmd *cobra.Command) (
nb *network.Builder,
spinner *clispinner.Spinner,
cleanup func(),
err error,
) {
var (
wg sync.WaitGroup
s = clispinner.New()
ev = events.NewBus()
)
wg.Add(1)
go printEvents(&wg, ev, s)

shutdown := func() {
s.Stop()
ev.Shutdown()
wg.Wait()
}

nb, err = newNetwork(cmd, network.CollectEvents(ev))
if err != nil {
shutdown()
}
return nb, s, shutdown, err
}

// newNetwork returns a new network builder initialized with command flag
func newNetwork(cmd *cobra.Command, options ...network.Option) (*network.Builder, error) {
// check preconfigured networks
if nightly && local {
Expand Down
5 changes: 4 additions & 1 deletion starport/cmd/network_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ func NewNetworkChain() *cobra.Command {
Short: "Build networks",
}

c.AddCommand(NewNetworkChainPublish())
c.AddCommand(
NewNetworkChainPublish(),
NewNetworkChainInit(),
)

return c
}
142 changes: 142 additions & 0 deletions starport/cmd/network_chain_init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package starportcmd

import (
"fmt"
"strconv"

"github.com/manifoldco/promptui"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/tendermint/starport/starport/pkg/cliquiz"
"github.com/tendermint/starport/starport/pkg/clispinner"
"github.com/tendermint/starport/starport/pkg/cosmosaccount"
"github.com/tendermint/starport/starport/services/chain"
"github.com/tendermint/starport/starport/services/network"
)

const (
flagValidatorAccount = "validator-account"
)

// NewNetworkChainInit returns a new command to initialize a chain from a published chain ID
func NewNetworkChainInit() *cobra.Command {
c := &cobra.Command{
Use: "init [launch-id]",
Short: "Initialize a chain from a published chain ID",
Args: cobra.ExactArgs(1),
RunE: networkChainInitHandler,
}

c.Flags().String(flagValidatorAccount, cosmosaccount.DefaultAccount, "Account for the chain validator")
c.Flags().AddFlagSet(flagNetworkFrom())
c.Flags().AddFlagSet(flagSetHome())
c.Flags().AddFlagSet(flagSetKeyringBackend())
c.Flags().AddFlagSet(flagSetYes())

return c
}

func networkChainInitHandler(cmd *cobra.Command, args []string) error {
nb, s, shutdown, err := initializeNetwork(cmd)
if err != nil {
return err
}
defer shutdown()

// parse launch ID
launchID, err := strconv.ParseUint(args[0], 10, 64)
if err != nil {
return errors.Wrap(err, "error parsing launchID")
}
if launchID == 0 {
return errors.New("launch ID must be greater than 0")
}

// check if the provided account for the validator exists
validatorAccount, _ := cmd.Flags().GetString(flagValidatorAccount)
_, err = nb.AccountRegistry().GetByName(validatorAccount)
if err != nil {
return err
}

// if a chain has already been initialized with this launch ID, we ask for confirmation before erasing the directory
chainHome, exist, err := network.IsChainHomeExist(launchID)
if err != nil {
return err
}
if !getYes(cmd) && exist {
prompt := promptui.Prompt{
Label: fmt.Sprintf("The chain has already been initialized under: %s. Would you like to overwrite the home directory",
chainHome,
),
IsConfirm: true,
}
s.Stop()
if _, err := prompt.Run(); err != nil {
fmt.Println("said no")
return nil
}
s.Start()
}

// initialize the blockchain from the launch ID
initOptions := initOptionWithHomeFlag(cmd, []network.InitOption{network.MustNotInitializedBefore()})
sourceOption := network.SourceLaunchID(launchID)
blockchain, err := nb.Blockchain(cmd.Context(), sourceOption, initOptions...)
if err != nil {
return err
}

if err := blockchain.Init(cmd.Context()); err != nil {
return err
}

// ask validator information
v, err := askValidatorInfo(validatorAccount)
if err != nil {
return err
}

gentxPath, err := blockchain.InitAccount(cmd.Context(), v, validatorAccount)
if err != nil {
return err
}
fmt.Printf("%s Gentx generated: %s\n", clispinner.Bullet, gentxPath)

return nil
}

// askValidatorInfo prompts to the user questions to query validator information
func askValidatorInfo(validatorName string) (chain.Validator, error) {
// TODO: allowing more customization for the validator
v := chain.Validator{
Name: validatorName,
Moniker: validatorName,
GasPrices: "0stake",
MinSelfDelegation: "1",
}

questions := append([]cliquiz.Question{},
cliquiz.NewQuestion("Staking amount",
&v.StakingAmount,
cliquiz.DefaultAnswer("95000000stake"),
cliquiz.Required(),
),
cliquiz.NewQuestion("Commission rate",
&v.CommissionRate,
cliquiz.DefaultAnswer("0.10"),
cliquiz.Required(),
),
cliquiz.NewQuestion("Commission max rate",
&v.CommissionMaxRate,
cliquiz.DefaultAnswer("0.20"),
cliquiz.Required(),
),
cliquiz.NewQuestion("Commission max change rate",
&v.CommissionMaxChangeRate,
cliquiz.DefaultAnswer("0.01"),
cliquiz.Required(),
),
)
return v, cliquiz.Ask(questions...)
}
39 changes: 12 additions & 27 deletions starport/cmd/network_chain_publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ package starportcmd

import (
"fmt"
"sync"

"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
"github.com/tendermint/starport/starport/pkg/clispinner"
"github.com/tendermint/starport/starport/pkg/cosmosaccount"
"github.com/tendermint/starport/starport/pkg/events"
"github.com/tendermint/starport/starport/pkg/xurl"
"github.com/tendermint/starport/starport/services/network"
)
Expand Down Expand Up @@ -37,7 +34,7 @@ func NewNetworkChainPublish() *cobra.Command {
c.Flags().String(flagGenesis, "", "URL to a custom Genesis")
c.Flags().Uint64(flagCampaign, 0, "Campaign ID to use for this network")
c.Flags().Bool(flagNoCheck, false, "Skip verifying chain's integrity")
c.Flags().String(flagFrom, cosmosaccount.DefaultAccount, "Account name to use for sending transactions to SPN")
c.Flags().AddFlagSet(flagNetworkFrom())
c.Flags().AddFlagSet(flagSetKeyringBackend())
c.Flags().AddFlagSet(flagSetHome())
c.Flags().AddFlagSet(flagSetYes())
Expand All @@ -55,25 +52,11 @@ func networkChainPublishHandler(cmd *cobra.Command, args []string) error {
campaign, _ = cmd.Flags().GetUint64(flagCampaign)
noCheck, _ = cmd.Flags().GetBool(flagNoCheck)
)

s := clispinner.New()
defer s.Stop()

var (
wg sync.WaitGroup
ev = events.NewBus()
)
wg.Add(1)

defer wg.Wait()
defer ev.Shutdown()

go printEvents(&wg, ev, s)

nb, err := newNetwork(cmd, network.CollectEvents(ev))
nb, s, shutdown, err := initializeNetwork(cmd)
if err != nil {
return err
}
defer shutdown()

// initialize the blockchain
initOptions := initOptionWithHomeFlag(cmd, []network.InitOption{network.MustNotInitializedBefore()})
Expand All @@ -93,6 +76,10 @@ func networkChainPublishHandler(cmd *cobra.Command, args []string) error {
}
}

if genesisURL != "" {
initOptions = append(initOptions, network.InitializationGenesisURL(genesisURL))
}

// init the chain.
blockchain, err := nb.Blockchain(cmd.Context(), sourceOption, initOptions...)
if err != nil {
Expand All @@ -101,16 +88,15 @@ func networkChainPublishHandler(cmd *cobra.Command, args []string) error {

var createOptions []network.CreateOption

if genesisURL != "" {
createOptions = append(createOptions, network.WithCustomGenesisFromURL(genesisURL))
}
if campaign != 0 {
createOptions = append(createOptions, network.WithCampaign(campaign))
}

if noCheck {
createOptions = append(createOptions, network.WithNoCheck())
} else if genesisURL != "" {
} else {
// perform checks for the chain requires to initialize it and therefore erase the current home if it exists
// we ask the user for confirmation
ok, err := blockchain.IsHomeDirExist()
if err != nil {
return err
Expand All @@ -136,13 +122,12 @@ func networkChainPublishHandler(cmd *cobra.Command, args []string) error {
s.Start()
}

// initialize the chain for checking
if err := blockchain.Init(cmd.Context()); err != nil {
return err
return nil
}
}

s.SetText("Publishing...")

launchID, campaignID, err := blockchain.Publish(cmd.Context(), createOptions...)
if err != nil {
return err
Expand Down
Loading

0 comments on commit 9aa042f

Please sign in to comment.