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: remove delegator address in WrappedFilePV, change init cmd and add migration cmd #434

Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

### Improvements

- [#434](https://github.com/babylonlabs-io/babylon/pull/434) Removal of delegator address, change init cmd and add migration cmd
- [#396](https://github.com/babylonlabs-io/babylon/pull/396) BLS Key Separation and ERC2335 Implementation
- [#391](https://github.com/babylonlabs-io/babylon/pull/391) Fix e2e `TestBTCRewardsDistribution` flunky
check of rewards
Expand Down
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ else
BUILD_TAGS += mainnet
endif

# Handles the inclusion of e2e upgrade in binary
ifeq (e2e_upgrade,$(findstring e2e_upgrade,$(BABYLON_BUILD_OPTIONS)))
BUILD_TAGS += e2e_upgrade
endif

# DB backend selection
ifeq (cleveldb,$(findstring cleveldb,$(BABYLON_BUILD_OPTIONS)))
ldflags += -X github.com/cosmos/cosmos-sdk/types.DBBackend=cleveldb
Expand Down
5 changes: 5 additions & 0 deletions app/build_tags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package app

// This variable is only set true
// when build tag is set to "e2e_upgrade"
var IsE2EUpgradeBuildFlag bool
9 changes: 9 additions & 0 deletions app/include_upgrade_e2e.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//go:build e2e_upgrade

package app

// init is used to include v1 upgrade testnet data
// it is also used for e2e testing
func init() {
IsE2EUpgradeBuildFlag = true
}
2 changes: 1 addition & 1 deletion app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func (ak *AppKeepers) InitKeepers(
checkpointingKeeper := checkpointingkeeper.NewKeeper(
appCodec,
runtime.NewKVStoreService(keys[checkpointingtypes.StoreKey]),
privSigner.WrappedPV,
privSigner.PV,
epochingKeeper,
)

Expand Down
21 changes: 11 additions & 10 deletions app/signer/private.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import (
cmtconfig "github.com/cometbft/cometbft/config"

"github.com/babylonlabs-io/babylon/privval"
cmtos "github.com/cometbft/cometbft/libs/os"
cmtprivval "github.com/cometbft/cometbft/privval"
)

type PrivSigner struct {
WrappedPV *privval.WrappedFilePV
PV *privval.WrappedFilePV
}

func InitPrivSigner(nodeDir string) (*PrivSigner, error) {
Expand All @@ -26,18 +27,18 @@ func InitPrivSigner(nodeDir string) (*PrivSigner, error) {
return nil, fmt.Errorf("failed to ensure dirs: %w", err)
}

cometPV := cmtprivval.LoadFilePV(pvKeyFile, pvStateFile)
blsPV := privval.LoadBlsPV(blsKeyFile, blsPasswordFile)
if !cmtos.FileExists(pvKeyFile) {
return nil, fmt.Errorf("validator key file does not exist. create file using `babylond init`: %s", pvKeyFile)
}

wrappedPV := &privval.WrappedFilePV{
Key: privval.WrappedFilePVKey{
CometPVKey: cometPV.Key,
BlsPVKey: blsPV.Key,
},
LastSignState: cometPV.LastSignState,
if !cmtos.FileExists(blsKeyFile) || !cmtos.FileExists(blsPasswordFile) {
return nil, fmt.Errorf("BLS key file does not exist. create file using `babylond init` or `babylond create-bls-key`: %s", blsKeyFile)
}

cometPV := cmtprivval.LoadFilePV(pvKeyFile, pvStateFile)
blsPV := privval.LoadBlsPV(blsKeyFile, blsPasswordFile)

return &PrivSigner{
WrappedPV: wrappedPV,
PV: privval.NewWrappedFilePV(cometPV.Key, blsPV.Key),
}, nil
}
12 changes: 8 additions & 4 deletions app/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,16 +239,20 @@ func SetupWithBitcoinConf(t *testing.T, isCheckTx bool, btcConf bbn.SupportedBtc

ps, err := signer.SetupTestPrivSigner()
require.NoError(t, err)
valPubKey := ps.WrappedPV.Key.CometPVKey.PubKey
valPubKey := ps.PV.Comet.PubKey
// generate genesis account
acc := authtypes.NewBaseAccount(valPubKey.Address().Bytes(), &cosmosed.PubKey{Key: valPubKey.Bytes()}, 0, 0)
acc := authtypes.NewBaseAccount(
valPubKey.Address().Bytes(),
&cosmosed.PubKey{Key: valPubKey.Bytes()},
0,
0,
)
balance := banktypes.Balance{
Address: acc.GetAddress().String(),
Coins: sdk.NewCoins(sdk.NewCoin(appparams.DefaultBondDenom, math.NewInt(100000000000000))),
}
ps.WrappedPV.Key.BlsPVKey.DelegatorAddress = acc.GetAddress().String()
// create validator set with single validator
genesisKey, err := signer.GenesisKeyFromPrivSigner(ps)
genesisKey, err := signer.GenesisKeyFromPrivSigner(ps, sdk.ValAddress(acc.GetAddress()))
require.NoError(t, err)
genesisValSet := []*checkpointingtypes.GenesisKey{genesisKey}

Expand Down
1 change: 1 addition & 0 deletions cmd/babylond/cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func TestInitCmd(t *testing.T) {
"init", // Test the init cmd
"app-test", // Moniker
fmt.Sprintf("--%s=%s", cli.FlagOverwrite, "true"), // Overwrite genesis.json, in case it already exists
fmt.Sprintf("--%s=%s", "bls-password", "testpassword"),
})

require.NoError(t, svrcmd.Execute(rootCmd, app.BabylonAppEnvPrefix, app.DefaultNodeHome))
Expand Down
50 changes: 13 additions & 37 deletions cmd/babylond/cmd/create_bls_key.go
Original file line number Diff line number Diff line change
@@ -1,71 +1,47 @@
package cmd

import (
"fmt"
"strings"

"github.com/cosmos/cosmos-sdk/client/flags"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/spf13/cobra"

"github.com/babylonlabs-io/babylon/app"
appparams "github.com/babylonlabs-io/babylon/app/params"
"github.com/babylonlabs-io/babylon/privval"
)

const (
FlagPassword = "bls-password"
)

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

cmd := &cobra.Command{
Use: "create-bls-key [account-address]",
Args: cobra.ExactArgs(1),
Use: "create-bls-key",
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
Long: strings.TrimSpace(`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 ./
$ babylond create-bls-key --home ./
`,
bech32PrefixAccAddr,
),
),

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

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

var password string
password, _ = cmd.Flags().GetString(FlagPassword)
if password == "" {
password = privval.NewBlsPassword()
}
return CreateBlsKey(homeDir, password, addr)
password, _ := cmd.Flags().GetString(flagBlsPassword)
createBlsKeyAndSave(homeDir, password)
return nil
},
}

cmd.Flags().String(flags.FlagHome, app.DefaultNodeHome, "The node home directory")
cmd.Flags().String(FlagPassword, "", "The password for the BLS key. If a flag is set, the non-empty password should be provided. If a flag is not set, the password will be read from the prompt.")
cmd.Flags().String(flagBlsPassword, "", "The password for the BLS key. If a flag is set, the non-empty password should be provided. If a flag is not set, the password will be read from the prompt.")
return cmd
}

func CreateBlsKey(home, password string, addr sdk.AccAddress) error {
privval.GenBlsPV(
privval.DefaultBlsKeyFile(home),
privval.DefaultBlsPasswordFile(home),
password,
addr.String(),
)
return nil
// createBlsKeyAndSave creates a pair of BLS keys and saves them to files
func createBlsKeyAndSave(homeDir, password string) {
if password == "" {
password = privval.NewBlsPassword()
}
privval.GenBlsPV(privval.DefaultBlsKeyFile(homeDir), privval.DefaultBlsPasswordFile(homeDir), password)
}
1 change: 1 addition & 0 deletions cmd/babylond/cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const (
flagMinSignedPerWindow = "min-signed-per-window"
flagFinalitySigTimeout = "finality-sig-timeout"
flagJailDuration = "jail-duration"
flagBlsPassword = "bls-password"
)

type GenesisCLIArgs struct {
Expand Down
4 changes: 2 additions & 2 deletions cmd/babylond/cmd/genhelpers/bls_add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@ func Test_CmdAddBlsWithGentx(t *testing.T) {
filePV := cmtprivval.GenFilePV(keyPath, statePath)
filePV.Key.Save()
filePV.LastSignState.Save()
privval.GenBlsPV(blsKeyFile, blsPasswordFile, "password", v.Address.String())
privval.GenBlsPV(blsKeyFile, blsPasswordFile, "password")

_, err = cli.ExecTestCLICmd(v.ClientCtx, genBlsCmd, []string{fmt.Sprintf("--%s=%s", flags.FlagHome, homeDir)})
_, err = cli.ExecTestCLICmd(v.ClientCtx, genBlsCmd, []string{v.Address.String(), fmt.Sprintf("--%s=%s", flags.FlagHome, homeDir)})
require.NoError(t, err)
genKeyFileName := filepath.Join(filepath.Dir(keyPath), fmt.Sprintf("gen-bls-%s.json", v.ValAddress))
genKey, err := types.LoadGenesisKeyFromFile(genKeyFileName)
Expand Down
37 changes: 23 additions & 14 deletions cmd/babylond/cmd/genhelpers/bls_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,34 @@ import (
"github.com/spf13/cobra"

"github.com/babylonlabs-io/babylon/app"
appparams "github.com/babylonlabs-io/babylon/app/params"
"github.com/babylonlabs-io/babylon/privval"
cmtprivval "github.com/cometbft/cometbft/privval"
sdk "github.com/cosmos/cosmos-sdk/types"
)

// CmdCreateBls CLI command to create BLS file with proof of possession.
func CmdCreateBls() *cobra.Command {
bech32PrefixAccAddr := appparams.Bech32PrefixAccAddr

cmd := &cobra.Command{
Use: "create-bls",
Use: "create-bls [account-address]",
Args: cobra.ExactArgs(1),
Short: "Create genesis BLS key file for the validator",
Long: strings.TrimSpace(`genbls will create a BLS key file that consists of
Long: strings.TrimSpace(
fmt.Sprintf(`genbls will create a BLS key file that consists of
{address, bls_pub_key, pop, pub_key} where pop is the proof-of-possession that proves
the ownership of bls_pub_key which is bonded with pub_key.

The pre-conditions of running the generate-genesis-bls-key are the existence of the keyring,
and the existence of priv_validator_key.json which contains the validator private key.


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

RunE: func(cmd *cobra.Command, args []string) error {
homeDir, _ := cmd.Flags().GetString(flags.FlagHome)
Expand All @@ -56,17 +64,19 @@ $ babylond genbls --home ./
cmtPV := cmtprivval.LoadFilePV(cmtPvKeyFile, cmtPvStateFile)
blsPV := privval.LoadBlsPV(blsKeyFile, blsPasswordFile)

wrappedPV := &privval.WrappedFilePV{
Key: privval.WrappedFilePVKey{
CometPVKey: cmtPV.Key,
BlsPVKey: blsPV.Key,
},
LastSignState: cmtPV.LastSignState,
addr, err := sdk.AccAddressFromBech32(args[0])
if err != nil {
return fmt.Errorf("invalid address: %w", err)
}

outputFileName, err := wrappedPV.ExportGenBls(filepath.Dir(cmtPvKeyFile))
outputFileName, err := privval.ExportGenBls(
sdk.ValAddress(addr),
cmtPV.Key.PrivKey,
blsPV.Key.PrivKey,
filepath.Dir(cmtPvKeyFile),
)
if err != nil {
return err
return fmt.Errorf("failed to export genesis bls: %w", err)
}

cmd.PrintErrf("Genesis BLS keys written to %q\n", outputFileName)
Expand All @@ -75,6 +85,5 @@ $ babylond genbls --home ./
}

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

return cmd
}
8 changes: 6 additions & 2 deletions cmd/babylond/cmd/genhelpers/bls_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ func Test_CmdCreateBls(t *testing.T) {
ctx = context.WithValue(ctx, server.ServerContextKey, serverCtx)
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
genBlsCmd := genhelpers.CmdCreateBls()
genBlsCmd.SetArgs([]string{fmt.Sprintf("--%s=%s", flags.FlagHome, home)})

// create keyring to get the validator address
kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, home, bufio.NewReader(genBlsCmd.InOrStdin()), clientCtx.Codec)
Expand All @@ -84,9 +83,14 @@ func Test_CmdCreateBls(t *testing.T) {
filePV := cmtprivval.GenFilePV(keyPath, statePath)
filePV.Key.Save()

blsPV := privval.GenBlsPV(blsKeyFile, blsPasswordFile, "password", addr.String())
blsPV := privval.GenBlsPV(blsKeyFile, blsPasswordFile, "password")
defer Clean(keyPath, statePath, blsKeyFile, blsPasswordFile)

genBlsCmd.SetArgs([]string{
addr.String(),
fmt.Sprintf("--%s=%s", flags.FlagHome, home),
})

// execute the gen-bls cmd
err = genBlsCmd.ExecuteContext(ctx)
require.NoError(t, err)
Expand Down
32 changes: 32 additions & 0 deletions cmd/babylond/cmd/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package cmd

import (
"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/types/module"
genutil "github.com/cosmos/cosmos-sdk/x/genutil/client/cli"
)

func InitCmd(mbm module.BasicManager, defaultNodeHome string) *cobra.Command {
cosmosInitCmd := genutil.InitCmd(mbm, defaultNodeHome)
cmd := &cobra.Command{
Use: cosmosInitCmd.Use,
Short: cosmosInitCmd.Short,
Long: cosmosInitCmd.Long,
Copy link
Member

Choose a reason for hiding this comment

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

I think maybe we should extend comsosInitCmd.Long with BLS description

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed bbe2efd

Args: cosmosInitCmd.Args,
RunE: func(cmd *cobra.Command, args []string) error {
if err := cosmosInitCmd.RunE(cmd, args); err != nil {
return err
}

homeDir, _ := cmd.Flags().GetString(flags.FlagHome)
password, _ := cmd.Flags().GetString(flagBlsPassword)
createBlsKeyAndSave(homeDir, password)
return nil
},
}
cmd.Flags().AddFlagSet(cosmosInitCmd.Flags())
cmd.Flags().String(flagBlsPassword, "", "The password for the BLS key. If a flag is set, the non-empty password should be provided. If a flag is not set, the password will be read from the prompt.")
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
cmd.Flags().String(flagBlsPassword, "", "The password for the BLS key. If a flag is set, the non-empty password should be provided. If a flag is not set, the password will be read from the prompt.")
cmd.Flags().String(flagBlsPassword, "", "The password for the BLS key. If the flag is not set, the password will be read from the prompt.")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed bbe2efd

return cmd
}
Loading