Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(network): initialize init command #1771

Merged
merged 54 commits into from
Nov 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
f74c297
feat(cmd): add network publish cmd
ilgooz Oct 25, 2021
0a26e1d
Merge branch 'develop' into feat/spn2
ilgooz Oct 28, 2021
01d137e
re-add network command
ilgooz Oct 28, 2021
e792d1c
Merge branch 'develop' into feat/spn2
ilgooz Oct 31, 2021
d07c8e2
hide the network cmd
ilgooz Oct 31, 2021
2301ebd
Merge branch 'develop' into feat/spn2
Pantani Nov 1, 2021
c22fd95
Apply suggestions from code review
ilgooz Nov 2, 2021
1b18c98
consistent event printing
ilgooz Nov 2, 2021
49b2503
docs
ilgooz Nov 2, 2021
3c2e964
Command initialization
lumtis Nov 2, 2021
5c7d9e2
conflict
lumtis Nov 3, 2021
90bbc76
misc modifications
lumtis Nov 3, 2021
ab11f45
chain home
lumtis Nov 3, 2021
cfc311b
chain fetch
lumtis Nov 3, 2021
7aa12bc
Lint
lumtis Nov 4, 2021
be0258e
init account
lumtis Nov 4, 2021
d3ae54b
Add validator description
lumtis Nov 4, 2021
ceedec8
Account creation
lumtis Nov 5, 2021
17efb7d
Add mnemonic
lumtis Nov 5, 2021
0329c3a
Merge branch 'develop' into feat/network-init
lumtis Nov 5, 2021
81fd9e0
overwrite confirmation
lumtis Nov 5, 2021
e738156
Fix mnemonic typo
lumtis Nov 5, 2021
70a3a32
starport/cmd/network_chain_init.go
lumtis Nov 5, 2021
0825c31
Initialize recover
lumtis Nov 5, 2021
3e75c32
Small modification
lumtis Nov 7, 2021
eb1eb11
Network initialization
lumtis Nov 8, 2021
9a5984b
Refactor genesis and chain verification
lumtis Nov 9, 2021
191a55f
lint
lumtis Nov 9, 2021
bc98cc5
Refactor account
lumtis Nov 9, 2021
0833b0b
Merge branch 'develop' into feat/network-init
lumtis Nov 9, 2021
c27ccec
Fix no account
lumtis Nov 9, 2021
4541b25
validator name change
lumtis Nov 9, 2021
49f16df
import account command
lumtis Nov 9, 2021
932d490
import starport account
lumtis Nov 9, 2021
49cb5bf
fix account created
lumtis Nov 9, 2021
e7d643a
Remove todos
lumtis Nov 9, 2021
25407f0
Update starport/cmd/network_chain_init.go
lumtis Nov 10, 2021
bb55a0d
shutdown renaming
lumtis Nov 10, 2021
dd4f69f
Merge branch 'feat/network-init' of https://github.com/tendermint/sta…
lumtis Nov 10, 2021
d1f352f
Validator flag
lumtis Nov 10, 2021
b9b61c3
conflicts
lumtis Nov 10, 2021
216df86
Fix cli spinner
lumtis Nov 10, 2021
3d4b9af
comments
lumtis Nov 10, 2021
3a3ed81
Fix genesis URL
lumtis Nov 10, 2021
2d2059b
spinner
lumtis Nov 10, 2021
64d0987
small text change
lumtis Nov 10, 2021
136c7a3
Merge branch 'develop' into feat/network-init
Pantani Nov 11, 2021
f88086e
Merge branch 'develop' into feat/network-init
Pantani Nov 11, 2021
8ebbbbc
Merge branch 'develop' into feat/network-init
Pantani Nov 12, 2021
d8360dc
Merge branch 'develop' into feat/network-init
Pantani Nov 12, 2021
141e5d0
comment
lumtis Nov 16, 2021
f61cd7e
Genesis initialization
lumtis Nov 17, 2021
bdb03dd
Refactor genesis url init
lumtis Nov 17, 2021
fba7ff2
Merge branch 'develop' into feat/network-init
lumtis Nov 17, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
lumtis marked this conversation as resolved.
Show resolved Hide resolved
}

// 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 {
lumtis marked this conversation as resolved.
Show resolved Hide resolved
return err
return nil
}
}

s.SetText("Publishing...")

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