From 2c1f33c14dd45d9ad4072d924a9515461d071fea Mon Sep 17 00:00:00 2001 From: b00f Date: Sun, 21 May 2023 00:06:14 +0800 Subject: [PATCH] fix: adding password option to the start commands (#473) --- cmd/cmd.go | 3 +++ cmd/daemon/init.go | 4 ++-- cmd/daemon/main.go | 6 +++--- cmd/daemon/start.go | 17 ++++++++++++++--- cmd/gtk/main.go | 9 +++++++-- consensus/log/messages.go | 2 +- network/dht.go | 2 +- network/gossip.go | 2 +- store/account.go | 4 ++-- store/block.go | 4 ++-- store/validator.go | 4 ++-- 11 files changed, 38 insertions(+), 19 deletions(-) diff --git a/cmd/cmd.go b/cmd/cmd.go index 753657cde..3418f0dc3 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -214,6 +214,9 @@ func PactusHomeDir() string { } else { home = path.Join(usr.HomeDir, "pactus") } + + // TODO: remove it before the mainnet launch + home = path.Join(home, "testnet") } return home } diff --git a/cmd/daemon/init.go b/cmd/daemon/init.go index 6ddb55f25..45b294e4e 100644 --- a/cmd/daemon/init.go +++ b/cmd/daemon/init.go @@ -15,7 +15,7 @@ func Init() func(c *cli.Cmd) { return func(c *cli.Cmd) { workingDirOpt := c.String(cli.StringOpt{ Name: "w working-dir", - Desc: "Working directory to save node configuration and genesis files.", + Desc: "A path to the working directory to save the wallet and node files", Value: cmd.PactusHomeDir(), }) testnetOpt := c.Bool(cli.BoolOpt{ @@ -24,7 +24,7 @@ func Init() func(c *cli.Cmd) { Value: true, // TODO: make it false after mainnet launch }) - c.LongDesc = "Initializing the working directory by new validator's private key and genesis file." + c.LongDesc = "Initializing the working directory by new validator's private key and genesis file" c.Before = func() { fmt.Println(cmd.Pactus) } c.Action = func() { workingDir, _ := filepath.Abs(*workingDirOpt) diff --git a/cmd/daemon/main.go b/cmd/daemon/main.go index dae5c3b51..e0415c96d 100644 --- a/cmd/daemon/main.go +++ b/cmd/daemon/main.go @@ -9,9 +9,9 @@ import ( func main() { app := cli.App("pactus-daemon", "Pactus daemon") - app.Command("init", "Initialize the pactus blockchain", Init()) - app.Command("start", "Start the pactus blockchain", Start()) - app.Command("version", "Print the pactus version", Version()) + app.Command("init", "Initialize the Pactus blockchain", Init()) + app.Command("start", "Start the Pactus blockchain", Start()) + app.Command("version", "Print the Pactus version", Version()) if err := app.Run(os.Args); err != nil { panic(err) diff --git a/cmd/daemon/start.go b/cmd/daemon/start.go index a015510cb..e310b876b 100644 --- a/cmd/daemon/start.go +++ b/cmd/daemon/start.go @@ -19,9 +19,14 @@ func Start() func(c *cli.Cmd) { return func(c *cli.Cmd) { workingDirOpt := c.String(cli.StringOpt{ Name: "w working-dir", - Desc: "Working directory to read node configuration and genesis files", - Value: ".", + Desc: "A path to the working directory to read the wallet and node files", + Value: cmd.PactusHomeDir(), }) + passwordOpt := c.String(cli.StringOpt{ + Name: "p password", + Desc: "The wallet password", + }) + // TODO: do we need this? pprofOpt := c.String(cli.StringOpt{ Name: "pprof", Desc: "debug pprof server address(not recommended to expose to internet)", @@ -56,7 +61,13 @@ func Start() func(c *cli.Cmd) { if !wallet.IsEncrypted() { return "", true } - password := cmd.PromptPassword("Wallet password", false) + + var password string + if *passwordOpt != "" { + password = *passwordOpt + } else { + password = cmd.PromptPassword("Wallet password", false) + } return password, true } gen, conf, signers, rewardAddrs, _, err := cmd.GetKeys( diff --git a/cmd/gtk/main.go b/cmd/gtk/main.go index 9c2ab1ccb..1ff3070b8 100644 --- a/cmd/gtk/main.go +++ b/cmd/gtk/main.go @@ -22,12 +22,14 @@ const appID = "com.github.pactus-project.pactus.pactus-gui" var ( workingDirOpt *string + passwordOpt *string testnetOpt *bool ) func init() { - workingDirOpt = flag.String("working-dir", cmd.PactusHomeDir(), "working directory") - testnetOpt = flag.Bool("testnet", true, "working directory") // TODO: make it false after mainnet launch + workingDirOpt = flag.String("working-dir", cmd.PactusHomeDir(), "working directory path") + passwordOpt = flag.String("password", "", "wallet password") + testnetOpt = flag.Bool("testnet", true, "initializing for the testnet") // TODO: make it false after mainnet launch gtk.Init(nil) } @@ -76,6 +78,9 @@ func main() { func startingNode(workingDir string) (*node.Node, *config.Config, *time.Time, *wallet.Wallet, error) { passwordFetcher := func(wallet *wallet.Wallet) (string, bool) { + if *passwordOpt != "" { + return *passwordOpt, true + } return getWalletPassword(wallet) } diff --git a/consensus/log/messages.go b/consensus/log/messages.go index 1f22a7f68..2b879cbac 100644 --- a/consensus/log/messages.go +++ b/consensus/log/messages.go @@ -49,6 +49,6 @@ func (m *Messages) voteSet(voteType vote.Type) *voteset.VoteSet { return m.changeProposerVotes } - logger.Panic("unexpected vote type %d", voteType) + logger.Panic("unexpected vote type", "voteType", voteType) return nil } diff --git a/network/dht.go b/network/dht.go index efd9cf6ce..03e61a5a6 100644 --- a/network/dht.go +++ b/network/dht.go @@ -26,7 +26,7 @@ func newDHTService(ctx context.Context, host lp2phost.Host, protocolID lp2pcore. kademlia, err := lp2pdht.New(ctx, host, opts...) if err != nil { - logger.Panic("unable to start DHT service: %v", err) + logger.Panic("unable to start DHT service", "err", err) return nil } diff --git a/network/gossip.go b/network/gossip.go index 2044e7a61..c85d43d76 100644 --- a/network/gossip.go +++ b/network/gossip.go @@ -25,7 +25,7 @@ func newGossipService(ctx context.Context, host lp2phost.Host, eventCh chan Even logger *logger.Logger) *gossipService { pubsub, err := lp2pps.NewGossipSub(ctx, host) if err != nil { - logger.Panic("unable to start Gossip service: %v", err) + logger.Panic("unable to start Gossip service", "err", err) return nil } diff --git a/store/account.go b/store/account.go index d23105a01..9627d4bcd 100644 --- a/store/account.go +++ b/store/account.go @@ -64,7 +64,7 @@ func (as *accountStore) iterateAccounts(consumer func(crypto.Address, *account.A acc, err := account.FromBytes(value) if err != nil { - logger.Panic("unable to decode account: %v", err) + logger.Panic("unable to decode account", "err", err) } stopped := consumer(addr, acc) @@ -78,7 +78,7 @@ func (as *accountStore) iterateAccounts(consumer func(crypto.Address, *account.A func (as *accountStore) updateAccount(batch *leveldb.Batch, addr crypto.Address, acc *account.Account) { data, err := acc.Bytes() if err != nil { - logger.Panic("unable to encode account: %v", err) + logger.Panic("unable to encode account", "err", err) } if !as.hasAccount(addr) { as.total++ diff --git a/store/block.go b/store/block.go index 48d231866..bdd5f0a7e 100644 --- a/store/block.go +++ b/store/block.go @@ -29,11 +29,11 @@ func newBlockStore(db *leveldb.DB) *blockStore { func (bs *blockStore) saveBlock(batch *leveldb.Batch, height uint32, block *block.Block) []blockRegion { if height > 1 { if !bs.hasBlock(height - 1) { - logger.Panic("previous block not found: %v", height) + logger.Panic("previous block not found", "height", height) } } if bs.hasBlock(height) { - logger.Panic("duplicated block: %v", height) + logger.Panic("duplicated block", "height", height) } blockHash := block.Hash() diff --git a/store/validator.go b/store/validator.go index c8bcc321b..95d76aef9 100644 --- a/store/validator.go +++ b/store/validator.go @@ -77,7 +77,7 @@ func (vs *validatorStore) iterateValidators(consumer func(*validator.Validator) val, err := validator.FromBytes(value) if err != nil { - logger.Panic("unable to decode validator: %v", err) + logger.Panic("unable to decode validator", "err", err) } stopped := consumer(val) @@ -91,7 +91,7 @@ func (vs *validatorStore) iterateValidators(consumer func(*validator.Validator) func (vs *validatorStore) updateValidator(batch *leveldb.Batch, val *validator.Validator) { data, err := val.Bytes() if err != nil { - logger.Panic("unable to encode validator: %v", err) + logger.Panic("unable to encode validator", "err", err) } if !vs.hasValidator(val.Address()) { vs.total++