-
Notifications
You must be signed in to change notification settings - Fork 563
refactor: package for blockchain application commands #583
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
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
d4deeea
Initialize chaincmd package
lumtis 6e34022
Implement commands
lumtis b6ede7a
Use step.Option
lumtis 8672cd3
Add options
lumtis f8bcdb2
Rename chain cmds
lumtis 0b954fd
Update plugin stargate with chaincmd
lumtis 09442b5
Fix chainID
lumtis cfea213
Chain commands for network builder
lumtis eea6cff
Merge develop
lumtis 6bcb2b1
starport/pkg/chaincmd/chaincmd.go starport/pkg/chaincmd/launchpad.go …
lumtis 12a3fc9
Refactor plugin
lumtis aec60db
Fix wrong command
lumtis dfa3180
Launchpad commands fix
lumtis 2178c46
Merge branch 'develop' into refactors/chain-commands
lumtis 7c8bacb
Fix launchpad
lumtis f2edb07
New fixes
lumtis fea006f
Merge branch 'develop' into refactors/chain-commands
lumtis 54b9c37
Use constants for keyring backend
lumtis 9fb5e8a
Options for gentxs
lumtis 9bf48b4
Remove else
lumtis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,295 @@ | ||
package chaincmd | ||
|
||
import "github.com/tendermint/starport/starport/pkg/cmdrunner/step" | ||
|
||
const ( | ||
commandStart = "start" | ||
commandInit = "init" | ||
commandKeys = "keys" | ||
commandAddGenesisAccount = "add-genesis-account" | ||
commandGentx = "gentx" | ||
commandCollectGentxs = "collect-gentxs" | ||
commandValidateGenesis = "validate-genesis" | ||
commandShowNodeID = "show-node-id" | ||
|
||
optionHome = "--home" | ||
optionKeyringBackend = "--keyring-backend" | ||
optionChainID = "--chain-id" | ||
optionOutput = "--output" | ||
optionRecover = "--recover" | ||
optionAddress = "--address" | ||
optionAmount = "--amount" | ||
optionValidatorMoniker = "--moniker" | ||
optionValidatorCommissionRate = "--commission-rate" | ||
optionValidatorCommissionMaxRate = "--commission-max-rate" | ||
optionValidatorCommissionMaxChangeRate = "--commission-max-change-rate" | ||
optionValidatorMinSelfDelegation = "--min-self-delegation" | ||
optionValidatorGasPrices = "--gas-prices" | ||
|
||
constTendermint = "tendermint" | ||
constJSON = "json" | ||
) | ||
|
||
type KeyringBackend string | ||
|
||
const ( | ||
KeyringBackendOS KeyringBackend = "os" | ||
KeyringBackendFile KeyringBackend = "file" | ||
KeyringBackendPass KeyringBackend = "pass" | ||
KeyringBackendTest KeyringBackend = "test" | ||
KeyringBackendKwallet KeyringBackend = "kwallet" | ||
) | ||
|
||
type ChainCmd struct { | ||
appCmd string | ||
chainID string | ||
homeDir string | ||
keyringBackend KeyringBackend | ||
cliCmd string | ||
} | ||
|
||
// New creates a new ChainCmd to launch command with the chain app | ||
func New(appCmd string, options ...Option) ChainCmd { | ||
chainCmd := ChainCmd{ | ||
appCmd: appCmd, | ||
} | ||
|
||
// Apply the options provided by the user | ||
for _, applyOption := range options { | ||
applyOption(&chainCmd) | ||
} | ||
|
||
return chainCmd | ||
} | ||
|
||
type Option func(*ChainCmd) | ||
|
||
// WithHome replaces the default home used by the chain | ||
func WithHome(home string) Option { | ||
return func(c *ChainCmd) { | ||
c.homeDir = home | ||
} | ||
} | ||
|
||
// WithChainID provides a specific chain ID for the commands that accept this option | ||
func WithChainID(chainID string) Option { | ||
return func(c *ChainCmd) { | ||
c.chainID = chainID | ||
} | ||
} | ||
|
||
// WithKeyrinBackend provides a specific keyring backend for the commands that accept this option | ||
func WithKeyrinBackend(keyringBackend KeyringBackend) Option { | ||
return func(c *ChainCmd) { | ||
c.keyringBackend = keyringBackend | ||
} | ||
} | ||
|
||
// WithLaunchpadCLI provides the name of the CLI application to call Launchpad CLI commands | ||
func WithLaunchpadCLI(cliCmd string) Option { | ||
return func(c *ChainCmd) { | ||
c.cliCmd = cliCmd | ||
} | ||
} | ||
|
||
// StartCommand returns the command to start the daemon of the chain | ||
func (c ChainCmd) StartCommand(options ...string) step.Option { | ||
command := append([]string{ | ||
commandStart, | ||
}, options...) | ||
return step.Exec(c.appCmd, c.attachHome(command)...) | ||
} | ||
|
||
// InitCommand returns the command to initialize the chain | ||
func (c ChainCmd) InitCommand(moniker string) step.Option { | ||
command := []string{ | ||
commandInit, | ||
moniker, | ||
} | ||
command = c.attachChainID(command) | ||
return step.Exec(c.appCmd, c.attachHome(command)...) | ||
} | ||
|
||
// AddKeyCommand returns the command to add a new key in the chain keyring | ||
func (c ChainCmd) AddKeyCommand(accountName string) step.Option { | ||
command := []string{ | ||
commandKeys, | ||
"add", | ||
accountName, | ||
optionOutput, | ||
constJSON, | ||
} | ||
command = c.attachKeyringBackend(command) | ||
return step.Exec(c.appCmd, c.attachHome(command)...) | ||
} | ||
|
||
// ImportKeyCommand returns the command to import a key into the chain keyring from a mnemonic | ||
func (c ChainCmd) ImportKeyCommand(accountName string) step.Option { | ||
command := []string{ | ||
commandKeys, | ||
"add", | ||
accountName, | ||
optionRecover, | ||
} | ||
command = c.attachKeyringBackend(command) | ||
return step.Exec(c.appCmd, c.attachHome(command)...) | ||
} | ||
|
||
// ShowKeyAddressCommand returns the command to print the address of a key in the chain keyring | ||
func (c ChainCmd) ShowKeyAddressCommand(accountName string) step.Option { | ||
command := []string{ | ||
commandKeys, | ||
"show", | ||
accountName, | ||
optionAddress, | ||
} | ||
command = c.attachKeyringBackend(command) | ||
return step.Exec(c.appCmd, c.attachHome(command)...) | ||
} | ||
|
||
// AddGenesisAccountCommand returns the command to add a new account in the genesis file of the chain | ||
func (c ChainCmd) AddGenesisAccountCommand(address string, coins string) step.Option { | ||
command := []string{ | ||
commandAddGenesisAccount, | ||
address, | ||
coins, | ||
} | ||
return step.Exec(c.appCmd, c.attachHome(command)...) | ||
} | ||
|
||
// Options for the GentxCommand | ||
type GentxOption func([]string) []string | ||
|
||
// GentxWithMoniker provides moniker option for the gentx command | ||
func GentxWithMoniker(moniker string) GentxOption { | ||
return func(command []string) []string { | ||
if len(moniker) > 0 { | ||
return append(command, optionValidatorMoniker, moniker) | ||
} | ||
return command | ||
} | ||
} | ||
|
||
// GentxWithCommissionRate provides commission rate option for the gentx command | ||
func GentxWithCommissionRate(commissionRate string) GentxOption { | ||
return func(command []string) []string { | ||
if len(commissionRate) > 0 { | ||
return append(command, optionValidatorCommissionRate, commissionRate) | ||
} | ||
return command | ||
} | ||
} | ||
|
||
// GentxWithCommissionMaxRate provides commission max rate option for the gentx command | ||
func GentxWithCommissionMaxRate(commissionMaxRate string) GentxOption { | ||
return func(command []string) []string { | ||
if len(commissionMaxRate) > 0 { | ||
return append(command, optionValidatorCommissionMaxRate, commissionMaxRate) | ||
} | ||
return command | ||
} | ||
} | ||
|
||
// GentxWithCommissionMaxChangeRate provides commission max change rate option for the gentx command | ||
func GentxWithCommissionMaxChangeRate(commissionMaxChangeRate string) GentxOption { | ||
return func(command []string) []string { | ||
if len(commissionMaxChangeRate) > 0 { | ||
return append(command, optionValidatorCommissionMaxChangeRate, commissionMaxChangeRate) | ||
} | ||
return command | ||
} | ||
} | ||
|
||
// GentxWithMinSelfDelegation provides minimum self delegation option for the gentx command | ||
func GentxWithMinSelfDelegation(minSelfDelegation string) GentxOption { | ||
return func(command []string) []string { | ||
if len(minSelfDelegation) > 0 { | ||
return append(command, optionValidatorMinSelfDelegation, minSelfDelegation) | ||
} | ||
return command | ||
} | ||
} | ||
|
||
// GentxWithGasPrices provides gas price option for the gentx command | ||
func GentxWithGasPrices(gasPrices string) GentxOption { | ||
return func(command []string) []string { | ||
if len(gasPrices) > 0 { | ||
return append(command, optionValidatorGasPrices, gasPrices) | ||
} | ||
return command | ||
} | ||
} | ||
|
||
// GentxCommand returns the command to generate a gentx for the chain | ||
func (c ChainCmd) GentxCommand( | ||
validatorName string, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would cleaner if we don't require optional flags as arguments. Instead, maybe we can use a struct to pass these or |
||
selfDelegation string, | ||
options ...GentxOption, | ||
) step.Option { | ||
command := []string{ | ||
commandGentx, | ||
validatorName, | ||
optionAmount, | ||
selfDelegation, | ||
} | ||
|
||
// Apply the options provided by the user | ||
for _, applyOption := range options { | ||
command = applyOption(command) | ||
} | ||
|
||
// Add necessary flags | ||
command = c.attachChainID(command) | ||
command = c.attachKeyringBackend(command) | ||
|
||
return step.Exec(c.appCmd, c.attachHome(command)...) | ||
} | ||
|
||
// CollectGentxsCommand returns the command to gather the gentxs in /gentx dir into the genesis file of the chain | ||
func (c ChainCmd) CollectGentxsCommand() step.Option { | ||
command := []string{ | ||
commandCollectGentxs, | ||
} | ||
return step.Exec(c.appCmd, c.attachHome(command)...) | ||
} | ||
|
||
// ValidateGenesisCommand returns the command to check the validity of the chain genesis | ||
func (c ChainCmd) ValidateGenesisCommand() step.Option { | ||
command := []string{ | ||
commandValidateGenesis, | ||
} | ||
return step.Exec(c.appCmd, c.attachHome(command)...) | ||
} | ||
|
||
// ShowNodeIDCommand returns the command to print the node ID of the node for the chain | ||
func (c ChainCmd) ShowNodeIDCommand() step.Option { | ||
command := []string{ | ||
constTendermint, | ||
commandShowNodeID, | ||
} | ||
return step.Exec(c.appCmd, c.attachHome(command)...) | ||
} | ||
|
||
// attachChainID appends the chain ID flag to the provided command | ||
func (c ChainCmd) attachChainID(command []string) []string { | ||
if c.chainID != "" { | ||
command = append(command, []string{optionChainID, c.chainID}...) | ||
} | ||
return command | ||
} | ||
|
||
// attachKeyringBackend appends the keyring backend flag to the provided command | ||
func (c ChainCmd) attachKeyringBackend(command []string) []string { | ||
if c.keyringBackend != "" { | ||
command = append(command, []string{optionKeyringBackend, string(c.keyringBackend)}...) | ||
} | ||
return command | ||
} | ||
|
||
// attachHome appends the home flag to the provided command | ||
func (c ChainCmd) attachHome(command []string) []string { | ||
if c.homeDir != "" { | ||
command = append(command, []string{optionHome, c.homeDir}...) | ||
} | ||
return command | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package chaincmd | ||
|
||
import "github.com/tendermint/starport/starport/pkg/cmdrunner/step" | ||
|
||
const ( | ||
commandConfig = "config" | ||
commandRestServer = "rest-server" | ||
|
||
optionUnsafeCors = "--unsafe-cors" | ||
optionAPIAddress = "--laddr" | ||
optionRPCAddress = "--node" | ||
optionName = "--name" | ||
) | ||
|
||
// LaunchpadAddKeyCommand returns the command to add a new key in the chain keyring with Launchpad chains | ||
func (c ChainCmd) LaunchpadAddKeyCommand(accountName string) step.Option { | ||
command := []string{ | ||
commandKeys, | ||
"add", | ||
accountName, | ||
optionOutput, | ||
constJSON, | ||
} | ||
command = c.attachKeyringBackend(command) | ||
return step.Exec(c.cliCmd, c.attachHome(command)...) | ||
} | ||
|
||
// LaunchpadImportKeyCommand returns the command to import a key into the chain keyring from a mnemonic with Launchpad chains | ||
func (c ChainCmd) LaunchpadImportKeyCommand(accountName string) step.Option { | ||
command := []string{ | ||
commandKeys, | ||
"add", | ||
accountName, | ||
optionRecover, | ||
} | ||
command = c.attachKeyringBackend(command) | ||
return step.Exec(c.cliCmd, c.attachHome(command)...) | ||
} | ||
|
||
// LaunchpadShowKeyAddressCommand returns the command to print the address of a key in the chain keyring with Launchpad chains | ||
func (c ChainCmd) LaunchpadShowKeyAddressCommand(accountName string) step.Option { | ||
command := []string{ | ||
commandKeys, | ||
"show", | ||
accountName, | ||
optionAddress, | ||
} | ||
command = c.attachKeyringBackend(command) | ||
return step.Exec(c.cliCmd, c.attachHome(command)...) | ||
} | ||
|
||
// LaunchpadSetConfigCommand | ||
func (c ChainCmd) LaunchpadSetConfigCommand(name string, value string) step.Option { | ||
command := []string{ | ||
commandConfig, | ||
name, | ||
value, | ||
} | ||
return step.Exec(c.cliCmd, c.attachHome(command)...) | ||
} | ||
|
||
// LaunchpadRestServerCommand | ||
func (c ChainCmd) LaunchpadRestServerCommand(apiAddress string, rpcAddress string) step.Option { | ||
command := []string{ | ||
commandRestServer, | ||
optionUnsafeCors, | ||
optionAPIAddress, | ||
apiAddress, | ||
optionRPCAddress, | ||
rpcAddress, | ||
} | ||
return step.Exec(c.cliCmd, c.attachHome(command)...) | ||
} | ||
|
||
// LaunchpadGentxCommand returns the command to generate a gentx for the chain | ||
func (c ChainCmd) LaunchpadGentxCommand( | ||
validatorName string, | ||
selfDelegation string, | ||
options ...GentxOption, | ||
) step.Option { | ||
command := []string{ | ||
commandGentx, | ||
optionName, | ||
validatorName, | ||
optionAmount, | ||
selfDelegation, | ||
} | ||
|
||
// Apply the options provided by the user | ||
for _, applyOption := range options { | ||
command = applyOption(command) | ||
} | ||
|
||
command = c.attachKeyringBackend(command) | ||
return step.Exec(c.appCmd, c.attachHome(command)...) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since
--recover
option is interactive, what do you think about implementingImportKeyCommand
like this by usingstep.Write
to write interactive input?But this will require
ImportKeyCommand
to return a[]step.Option
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer to keep minimal usage for this command and only returning
step.Option
of the command