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 25 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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ require (
github.com/stretchr/testify v1.7.0
github.com/tendermint/flutter v1.0.2
github.com/tendermint/spm v0.1.7
github.com/tendermint/spn v0.1.1-0.20211019114618-71b787f1c6ba
github.com/tendermint/spn v0.1.1-0.20211104082149-b65147ba4d75
github.com/tendermint/tendermint v0.34.13
github.com/tendermint/vue v0.1.53
golang.org/x/mod v0.4.2
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1321,6 +1321,10 @@ github.com/tendermint/spm v0.1.7 h1:9PYX843H90im82WUr7EdZCQtyTKHthTIVrXsYCa8RBY=
github.com/tendermint/spm v0.1.7/go.mod h1:d3w1LTzoBLb5A9WcgI+u/754tsIefCrnIOv7B+ynK9s=
github.com/tendermint/spn v0.1.1-0.20211019114618-71b787f1c6ba h1:D+0eA/49vPFQ3QB4ElKLHM6buWrn/M5Cdfc/nS2ZiVY=
github.com/tendermint/spn v0.1.1-0.20211019114618-71b787f1c6ba/go.mod h1:b9drKTa6ceWJ+6tT8Ujleta7GM7lnxAHyInGXyjP7CM=
github.com/tendermint/spn v0.1.1-0.20211103092237-61b41bbb304a h1:Dd1XVHfKCxW/+7J0TiCxWF5MV7az/6wr3gqg47sZEgg=
github.com/tendermint/spn v0.1.1-0.20211103092237-61b41bbb304a/go.mod h1:b9drKTa6ceWJ+6tT8Ujleta7GM7lnxAHyInGXyjP7CM=
github.com/tendermint/spn v0.1.1-0.20211104082149-b65147ba4d75 h1:tRty0k6/WG4HmHMMpPfdAILhco2zfPVnisbgWTSVdLk=
github.com/tendermint/spn v0.1.1-0.20211104082149-b65147ba4d75/go.mod h1:b9drKTa6ceWJ+6tT8Ujleta7GM7lnxAHyInGXyjP7CM=
github.com/tendermint/tendermint v0.34.0-rc4/go.mod h1:yotsojf2C1QBOw4dZrTcxbyxmPUrT4hNuOQWX9XUwB4=
github.com/tendermint/tendermint v0.34.0-rc6/go.mod h1:ugzyZO5foutZImv0Iyx/gOFCX6mjJTgbLHTwi17VDVg=
github.com/tendermint/tendermint v0.34.0/go.mod h1:Aj3PIipBFSNO21r+Lq3TtzQ+uKESxkbA3yo/INM4QwQ=
Expand Down
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
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
}
193 changes: 193 additions & 0 deletions starport/cmd/network_chain_init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
package starportcmd

import (
"fmt"
"os"
"strconv"
"sync"

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

const (
flagRecover = "recover"
flagMnemonic = "mnemonic"
flagKeyName = "key-name"
)

// 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().Bool(flagRecover, false, "Recover chain account from a mnemonic")
c.Flags().String(flagMnemonic, "", "Mnemonic for recovered account")
c.Flags().String(flagKeyName, "", "key name for the chain account")

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 {
var (
recover, _ = cmd.Flags().GetBool(flagRecover)
mnemonic, _ = cmd.Flags().GetString(flagMnemonic)
keyName, _ = cmd.Flags().GetString(flagKeyName)
)

s := clispinner.New()
lumtis marked this conversation as resolved.
Show resolved Hide resolved
defer s.Stop()

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

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

go printEvents(&wg, ev, s)

// parse launch ID
launchID, err := strconv.ParseUint(args[0], 10, 64)
if err != nil {
return fmt.Errorf("error parsing launchID: %s", err.Error())
lumtis marked this conversation as resolved.
Show resolved Hide resolved
}

// if a chain has already been initialized with this launch ID, we ask for confirmation before erasing the directory
chainHome, exist, err := checkChainHomeExist(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
}

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

// 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()
if err != nil {
return err
}

// ask user for mnemonic if not provided and recover option defined
if mnemonic == "" && recover {
cliquiz.Ask(cliquiz.NewQuestion("Enter your account mnemonic",
&mnemonic,
cliquiz.DefaultAnswer(""),
cliquiz.Required(),
))
}

acc, gentxPath, err := blockchain.InitAccount(cmd.Context(), v, keyName, mnemonic)
if err != nil {
return err
}
fmt.Printf(`Account created
lumtis marked this conversation as resolved.
Show resolved Hide resolved
Address: %s
Mnemonic: %s
`, acc.Address, acc.Mnemonic)
fmt.Printf("Gentx generated: %s\n", gentxPath)
lumtis marked this conversation as resolved.
Show resolved Hide resolved

return nil
}

// askValidatorInfo prompts to the user questions to query validator information
func askValidatorInfo() (v chain.Validator, err error) {
questions := append([]cliquiz.Question{},
cliquiz.NewQuestion("Staking amount",
&v.StakingAmount,
cliquiz.DefaultAnswer("95000000stake"),
cliquiz.Required(),
),
cliquiz.NewQuestion("Moniker",
&v.Moniker,
cliquiz.DefaultAnswer("mynode"),
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(),
),
cliquiz.NewQuestion("Min self delegation",
&v.MinSelfDelegation,
cliquiz.DefaultAnswer("1"),
cliquiz.Required(),
),
cliquiz.NewQuestion("Gas prices",
&v.GasPrices,
cliquiz.DefaultAnswer("0.025stake"),
cliquiz.Required(),
),
cliquiz.NewQuestion("Details", &v.Details),
cliquiz.NewQuestion("Identity", &v.Identity),
cliquiz.NewQuestion("Website", &v.Website),
)
return v, cliquiz.Ask(questions...)
}

// checkChainHomeExist checks if a home with the provided launchID already exist
func checkChainHomeExist(launchID uint64) (string, bool, error) {
home, err := network.ChainHome(launchID)
lumtis marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return home, false, err
}

if _, err := os.Stat(home); os.IsNotExist(err) {
return home, false, nil
}
return home, true, err
}
3 changes: 1 addition & 2 deletions starport/cmd/network_chain_publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"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 @@ -35,7 +34,7 @@ func NewNetworkChainPublish() *cobra.Command {
c.Flags().String(flagHash, "", "Git hash to use for the repo")
c.Flags().String(flagGenesis, "", "URL to a custom Genesis")
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 Down
33 changes: 33 additions & 0 deletions starport/pkg/chaincmd/chaincmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ const (
optionValidatorCommissionMaxChangeRate = "--commission-max-change-rate"
optionValidatorMinSelfDelegation = "--min-self-delegation"
optionValidatorGasPrices = "--gas-prices"
optionValidatorDetails = "--details"
optionValidatorIdentity = "--identity"
optionValidatorWebsite = "--website"
optionYes = "--yes"
optionHomeClient = "--home-client"
optionCoinType = "--coin-type"
Expand Down Expand Up @@ -325,6 +328,36 @@ func GentxWithGasPrices(gasPrices string) GentxOption {
}
}

// GentxWithDetails provides validator details option for the gentx command
func GentxWithDetails(details string) GentxOption {
return func(command []string) []string {
if len(details) > 0 {
return append(command, optionValidatorDetails, details)
}
return command
}
}

// GentxWithIdentity provides validator identity option for the gentx command
func GentxWithIdentity(identity string) GentxOption {
return func(command []string) []string {
if len(identity) > 0 {
return append(command, optionValidatorIdentity, identity)
}
return command
}
}

// GentxWithWebsite provides validator website option for the gentx command
func GentxWithWebsite(website string) GentxOption {
return func(command []string) []string {
if len(website) > 0 {
return append(command, optionValidatorWebsite, website)
}
return command
}
}

func (c ChainCmd) IsAutoChainIDDetectionEnabled() bool {
return c.isAutoChainIDDetectionEnabled
}
Expand Down
27 changes: 19 additions & 8 deletions starport/services/chain/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,20 +167,28 @@ func (c *Chain) InitAccounts(ctx context.Context, conf chainconfig.Config) error
}
}

// create the gentx from the validator from the config
if _, err := c.plugin.Gentx(ctx, commands, Validator{
_, err = c.IssueGentx(ctx, Validator{
Name: conf.Validator.Name,
StakingAmount: conf.Validator.Staked,
}); err != nil {
return err
})
return err
}

// IssueGentx generates a gentx from the validator information in chain config and import it in the chain genesis
func (c Chain) IssueGentx(ctx context.Context, v Validator) (string, error) {
commands, err := c.Commands(ctx)
if err != nil {
return "", err
}

// import the gentx into the genesis
if err := commands.CollectGentxs(ctx); err != nil {
return err
// create the gentx from the validator from the config
gentxPath, err := c.plugin.Gentx(ctx, commands, v)
if err != nil {
return "", err
}

return nil
// import the gentx into the genesis
return gentxPath, commands.CollectGentxs(ctx)
}

// IsInitialized checks if the chain is initialized
Expand Down Expand Up @@ -212,6 +220,9 @@ type Validator struct {
CommissionMaxChangeRate string
MinSelfDelegation string
GasPrices string
Details string
Identity string
Website string
}

// Account represents an account in the chain.
Expand Down
3 changes: 3 additions & 0 deletions starport/services/chain/plugin-stargate.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ func (p *stargatePlugin) Gentx(ctx context.Context, runner chaincmdrunner.Runner
chaincmd.GentxWithCommissionMaxChangeRate(v.CommissionMaxChangeRate),
chaincmd.GentxWithMinSelfDelegation(v.MinSelfDelegation),
chaincmd.GentxWithGasPrices(v.GasPrices),
chaincmd.GentxWithDetails(v.Details),
chaincmd.GentxWithIdentity(v.Identity),
chaincmd.GentxWithWebsite(v.Website),
)
}

Expand Down
Loading