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: checkpointing/add create-bls-key cli #162

Merged
merged 3 commits into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
69 changes: 69 additions & 0 deletions cmd/babylond/cmd/create_bls_key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package cmd

import (
"errors"
"fmt"
"github.com/babylonchain/babylon/app"
appparams "github.com/babylonchain/babylon/app/params"
"github.com/babylonchain/babylon/crypto/bls12381"
"github.com/babylonchain/babylon/privval"
"github.com/cosmos/cosmos-sdk/client/flags"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/spf13/cobra"
tmconfig "github.com/tendermint/tendermint/config"
tmos "github.com/tendermint/tendermint/libs/os"
"path/filepath"
"strings"
)

func createBlsKeyCmd() *cobra.Command {
bech32PrefixAccAddr := appparams.Bech32PrefixAccAddr

cmd := &cobra.Command{
Use: "create-bls-key [account-address]",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Use: "create-bls-key [account-address]",
Use: "create-bls-key [--account-address <bbn-account-address>]",

[] brackets mean this flag is optional, and the example should contain -- to show it's a flag, otherwise I would think the way to call this is create-bls-key or crate-bls-key bbn1f5tnl46mk4dfp4nx3n2vnrvyw2h2ydz6ykhk3r.

If it's not an optional parameter than maybe it should be an arg instead of a flag.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you are right. It should an arg instead of a flag. Thanks!

Args: cobra.ExactArgs(1),
Short: "Create a pair of BLS keys for a validator",
Long: strings.TrimSpace(
fmt.Sprintf(`create-bls will create a pair of BLS keys that are used to
send BLS signatures for checkpointing.

BLS keys are stored along with other validator keys in priv_validator_key.json,
which should exist before running the command (via babylond init or babylond testnet).

Example:
$ babylond create-bls-key %s1f5tnl46mk4dfp4nx3n2vnrvyw2h2ydz6ykhk3r --home ./
`,
bech32PrefixAccAddr,
),
),

RunE: func(cmd *cobra.Command, args []string) error {
homeDir, _ := cmd.Flags().GetString(flags.FlagHome)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error is ignored here, but not for the account address. Does that mean this is optional, but the other isn't?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other one is an arg instead of a flag. FlagHome is optional. There's a default value for that.


addr, err := sdk.AccAddressFromBech32(args[0])
if err != nil {
return err
}

return CreateBlsKey(homeDir, addr)
},
}

cmd.Flags().String(flags.FlagHome, app.DefaultNodeHome, "The node home directory")

return cmd
}

func CreateBlsKey(home string, addr sdk.AccAddress) error {
nodeCfg := tmconfig.DefaultConfig()
keyPath := filepath.Join(home, nodeCfg.PrivValidatorKeyFile())
statePath := filepath.Join(home, nodeCfg.PrivValidatorStateFile())
if !tmos.FileExists(keyPath) {
return errors.New("validator key file does not exist")
}
pv := privval.LoadWrappedFilePV(keyPath, statePath)
wrappedPV := privval.NewWrappedFilePV(pv.GetValPrivKey(), bls12381.GenPrivKey(), keyPath, statePath)
wrappedPV.SetAccAddress(addr)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So it automatically saves itself?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, when we set the account address, we should save the file.


return nil
}
1 change: 1 addition & 0 deletions cmd/babylond/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig) {
AddGenesisAccountCmd(app.DefaultNodeHome),
tmcli.NewCompletionCmd(rootCmd, true),
testnetCmd(app.ModuleBasics, banktypes.GenesisBalancesIterator{}),
createBlsKeyCmd(),
debug.Cmd(),
config.Cmd(),
)
Expand Down