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: resolve ci error in bls-key-separation #6

Closed
18 changes: 12 additions & 6 deletions cmd/babylond/cmd/create_bls_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ import (
"github.com/babylonlabs-io/babylon/privval"
)

const (
FlagPassword = "bls-password"
)

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

Expand Down Expand Up @@ -43,23 +47,25 @@ $ babylond create-bls-key %s1f5tnl46mk4dfp4nx3n2vnrvyw2h2ydz6ykhk3r --home ./
return err
}

return CreateBlsKey(homeDir, addr)
var password string
password, _ = cmd.Flags().GetString(FlagPassword)
if password == "" {
password = privval.GetBlsPassword()
}
return CreateBlsKey(homeDir, password, addr)
},
}

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.")
return cmd
}

func CreateBlsKey(home string, addr sdk.AccAddress) error {

func CreateBlsKey(home, password string, addr sdk.AccAddress) error {
blsCfg := privval.DefaultBlsConfig()
keyPath := filepath.Join(home, blsCfg.BlsKeyFile())
passwordPath := filepath.Join(home, blsCfg.BlsPasswordFile())

password := privval.GetBlsPassword()

privval.GenBlsPV(keyPath, passwordPath, password, addr.String())
return nil
}
8 changes: 8 additions & 0 deletions privval/bls.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,14 @@ func (k *BlsPVKey) Save(password, addr string) {
}
}

func (k *BlsPVKey) GetKeyFilePath() string {
return k.filePath
}

func (k *BlsPVKey) GetPasswordFilePath() string {
return k.passwordPath
}

// -------------------------------------------------------------------------------
// ---------------------------- BLS Config ---------------------------------------
// -------------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion privval/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func IsValidFilePath(paths ...string) error {
// Check file path of bls key
for _, path := range paths {
if path == "" {
return fmt.Errorf("filePath for bls key not set")
return fmt.Errorf("filePath is not set completely")
}
if err := cmtos.EnsureDir(filepath.Dir(path), 0777); err != nil {
return fmt.Errorf("failed to ensure key path dir: %w", err)
Expand Down
80 changes: 80 additions & 0 deletions test/e2e/configurer/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
"sync"
Expand All @@ -18,10 +19,13 @@ import (

"github.com/babylonlabs-io/babylon/app"
appparams "github.com/babylonlabs-io/babylon/app/params"
"github.com/babylonlabs-io/babylon/privval"
"github.com/babylonlabs-io/babylon/test/e2e/configurer/chain"
"github.com/babylonlabs-io/babylon/test/e2e/configurer/config"
"github.com/babylonlabs-io/babylon/test/e2e/containers"
"github.com/babylonlabs-io/babylon/test/e2e/initialization"

cmtos "github.com/cometbft/cometbft/libs/os"
)

type UpgradeSettings struct {
Expand Down Expand Up @@ -250,13 +254,89 @@ func (uc *UpgradeConfigurer) runForkUpgrade() {
}
}

// func saveFilesToVolume(node *chain.NodeConfig) error {
// dir := node.ConfigDir

// cmtCfg := cmtcfg.DefaultConfig()
// cmtCfg.SetRoot(dir)

// cmtKeyFile := cmtCfg.PrivValidatorKeyFile()
// cmtStateFile := cmtCfg.PrivValidatorStateFile()

// blsCfg := privval.DefaultBlsConfig()
// blsCfg.SetRoot(dir)

// blsKeyFile := blsCfg.BlsKeyFile()
// blsPasswordFile := blsCfg.BlsPasswordFile()

// if err := privval.IsValidFilePath(cmtKeyFile, cmtStateFile, blsKeyFile, blsPasswordFile); err != nil {
// return err
// }

// var password string
// if node.TempBlsInfo.Password == "" {
// log.Print("node.TempBlsInfo.Password is empty")
// password = "password"
// } else {
// password = node.TempBlsInfo.Password
// }

// var blsPrivKey bls12381.PrivateKey
// if node.TempBlsInfo.PrivateKey == nil {
// log.Print("node.TempBlsInfo.PrivateKey is empty")
// blsPrivKey = bls12381.GenPrivKey()
// } else {
// blsPrivKey = node.TempBlsInfo.PrivateKey
// }
// blsPv := privval.NewBlsPV(blsPrivKey, blsKeyFile, blsPasswordFile, node.TempBlsInfo.DelegatorAddress)
// blsPv.Key.Save(password, node.TempBlsInfo.DelegatorAddress)

// var privKey ed25519.PrivKey
// if node.PrivateKey == nil {
// log.Print("node.PrivateKey is empty")
// privKey = ed25519.GenPrivKey()
// } else {
// privKey = ed25519.PrivKey(node.PrivateKey)
// }
// cmtprivval.NewFilePV(privKey, cmtKeyFile, cmtStateFile).Save()
// return nil
// }

func (uc *UpgradeConfigurer) upgradeContainers(chainConfig *chain.Config, propHeight int64) error {
// upgrade containers to the locally compiled daemon
uc.t.Logf("starting upgrade for chain-id: %s...", chainConfig.Id)
uc.containerManager.CurrentRepository = containers.BabylonContainerName
uc.containerManager.CurrentTag = "latest"

for _, node := range chainConfig.NodeConfigs {
// ======= TESTING START =======
log.Print("==> upgradeContainers()")
log.Print("=> node.ConfigDir: ", node.ConfigDir)

var keyIs, pwIs bool

log.Print("=> bls")
if cmtos.FileExists(node.ConsensusKey.BlsPVKey.GetKeyFilePath()) {
log.Print("=> file exists: node.ConsensusKey.BlsPVKey.GetKeyFilePath(): ", node.ConsensusKey.BlsPVKey.GetKeyFilePath())
keyIs = true
} else {
log.Print("=> file does not exist: node.ConsensusKey.BlsPVKey.GetKeyFilePath(): ", node.ConsensusKey.BlsPVKey.GetKeyFilePath())
keyIs = false
}

if cmtos.FileExists(node.ConsensusKey.BlsPVKey.GetPasswordFilePath()) {
log.Print("=> file exists: node.ConsensusKey.BlsPVKey.GetPasswordFilePath(): ", node.ConsensusKey.BlsPVKey.GetPasswordFilePath())
pwIs = true
} else {
log.Print("=> file does not exist: node.ConsensusKey.BlsPVKey.GetPasswordFilePath(): ", node.ConsensusKey.BlsPVKey.GetPasswordFilePath())
pwIs = false
}

if keyIs && pwIs {
pv := privval.LoadBlsPV(node.ConsensusKey.BlsPVKey.GetKeyFilePath(), node.ConsensusKey.BlsPVKey.GetPasswordFilePath())
log.Print("bls pv: ", pv)
}
// ======= TESTING END =======
if err := node.Run(); err != nil {
return err
}
Expand Down
42 changes: 42 additions & 0 deletions test/e2e/containers/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@ import (
"context"
"errors"
"fmt"
"log"
"os"
"path/filepath"
"regexp"
"strings"
"testing"
"time"

"github.com/babylonlabs-io/babylon/privval"
cmtcfg "github.com/cometbft/cometbft/config"
cmtos "github.com/cometbft/cometbft/libs/os"
cmtprivval "github.com/cometbft/cometbft/privval"
"github.com/ory/dockertest/v3"
"github.com/ory/dockertest/v3/docker"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -263,6 +269,42 @@ func (m *Manager) RunNodeResource(chainId string, containerName, valCondifDir st
return nil, err
}

// ======= TESTING START =======
log.Print("==> RunNodeResource()")
log.Print("=> valCondifDir: ", valCondifDir)
// ======= TESTING END =======

// ======= CREATE KEY DIRECTLY =======
cmtKeyFile := filepath.Join(valCondifDir, cmtcfg.DefaultConfig().PrivValidatorKeyFile())
cmtStateFile := filepath.Join(valCondifDir, cmtcfg.DefaultConfig().PrivValidatorStateFile())
blsKeyFile := filepath.Join(valCondifDir, privval.DefaultBlsConfig().BlsKeyFile())
blsPasswordFile := filepath.Join(valCondifDir, privval.DefaultBlsConfig().BlsPasswordFile())

log.Print("=> cmtKeyFile: ", cmtKeyFile)
log.Print("=> cmtStateFile: ", cmtStateFile)
log.Print("=> blsKeyFile: ", blsKeyFile)
log.Print("=> blsPasswordFile: ", blsPasswordFile)

if !cmtos.FileExists(cmtKeyFile) {
if err := privval.IsValidFilePath(cmtKeyFile, cmtStateFile); err != nil {
return nil, err
}
log.Print("=> file not exists: cmtKeyFile: ", cmtKeyFile)
log.Print("=> create new cometPv and save to file")
cmtprivval.GenFilePV(cmtKeyFile, cmtStateFile).Save()
}

if !cmtos.FileExists(blsKeyFile) {
if err := privval.IsValidFilePath(blsKeyFile, blsPasswordFile); err != nil {
return nil, err
}
log.Print("=> file not exists: blsKeyFile: ", blsKeyFile)
log.Print("=> create new blsPv and save to file")
privval.GenBlsPV(blsKeyFile, blsPasswordFile, "password", "")
}

// ======= CREATE KEY DIRECTLY =======

runOpts := &dockertest.RunOptions{
Name: containerName,
Repository: m.CurrentRepository,
Expand Down
23 changes: 23 additions & 0 deletions test/e2e/initialization/chain.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package initialization

import (
"log"

cmtos "github.com/cometbft/cometbft/libs/os"
)

const (
keyringPassphrase = "testpassphrase"
keyringAppName = "testnet"
Expand All @@ -26,6 +32,23 @@ func (c *internalChain) export() *Chain {
exportNodes := make([]*Node, 0, len(c.nodes))
for _, v := range c.nodes {
exportNodes = append(exportNodes, v.export())

// ======= TESTING START =======
log.Print("==> export()")
log.Print("=> v.export().ConfigDir: ", v.export().ConfigDir)

if cmtos.FileExists(v.export().ConsensusKey.BlsPVKey.GetKeyFilePath()) {
log.Print("=> file exists: blsKeyFile: ", v.export().ConsensusKey.BlsPVKey.GetKeyFilePath())
} else {
log.Print("=> file does not exist: blsKeyFile: ", v.export().ConsensusKey.BlsPVKey.GetKeyFilePath())
}

if cmtos.FileExists(v.export().ConsensusKey.BlsPVKey.GetPasswordFilePath()) {
log.Print("=> file exists: blsPasswordFile: ", v.export().ConsensusKey.BlsPVKey.GetPasswordFilePath())
} else {
log.Print("=> file does not exist: blsPasswordFile: ", v.export().ConsensusKey.BlsPVKey.GetPasswordFilePath())
}
// ======= TESTING END =======
}

return &Chain{
Expand Down
3 changes: 3 additions & 0 deletions test/e2e/initialization/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package initialization

import (
"fmt"

"github.com/babylonlabs-io/babylon/privval"
)

type ChainMeta struct {
Expand All @@ -19,6 +21,7 @@ type Node struct {
PrivateKey []byte `json:"privateKey"`
PeerId string `json:"peerId"`
IsValidator bool `json:"isValidator"`
ConsensusKey privval.WrappedFilePVKey
}

type Chain struct {
Expand Down
40 changes: 40 additions & 0 deletions test/e2e/initialization/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package initialization
import (
"encoding/json"
"fmt"
"log"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -34,8 +35,10 @@ import (
babylonApp "github.com/babylonlabs-io/babylon/app"
appparams "github.com/babylonlabs-io/babylon/app/params"
"github.com/babylonlabs-io/babylon/cmd/babylond/cmd"
"github.com/babylonlabs-io/babylon/crypto/bls12381"
"github.com/babylonlabs-io/babylon/privval"
"github.com/babylonlabs-io/babylon/test/e2e/util"
cmtos "github.com/cometbft/cometbft/libs/os"
cmtprivval "github.com/cometbft/cometbft/privval"
)

Expand All @@ -51,6 +54,14 @@ type internalNode struct {
isValidator bool
}

type TempBlsInfo struct {
PrivateKey bls12381.PrivateKey
Password string
DelegatorAddress string
KeyFilePath string
PasswordFilePath string
}

func newNode(chain *internalChain, nodeConfig *NodeConfig) (*internalNode, error) {
node := &internalNode{
chain: chain,
Expand Down Expand Up @@ -198,6 +209,34 @@ func (n *internalNode) createConsensusKey() error {
CometPVKey: filePV.Key,
BlsPVKey: blsPV.Key,
}

// ======= TESTING START =======
log.Print("==> createConsensusKey()")
if cmtos.FileExists(pvKeyFile) {
log.Print("=> file exists: pvKeyFile: ", pvKeyFile)
} else {
log.Print("=> file does not exist: pvKeyFile: ", pvKeyFile)
}

if cmtos.FileExists(blsPasswordFile) {
log.Print("=> file exists: blsPasswordFile: ", blsPasswordFile)
} else {
log.Print("=> file does not exist: blsPasswordFile: ", blsPasswordFile)
}

if cmtos.FileExists(blsKeyFile) {
log.Print("=> file exists: blsKeyFile: ", blsKeyFile)
} else {
log.Print("=> file does not exist: blsKeyFile: ", blsKeyFile)
}

if cmtos.FileExists(blsPasswordFile) {
log.Print("=> file exists: blsPasswordFile: ", blsPasswordFile)
} else {
log.Print("=> file does not exist: blsPasswordFile: ", blsPasswordFile)
}
// ======= TESTING END =======

return nil
}

Expand Down Expand Up @@ -267,6 +306,7 @@ func (n *internalNode) export() *Node {
PrivateKey: n.privateKey.Bytes(),
PeerId: n.peerId,
IsValidator: n.isValidator,
ConsensusKey: n.consensusKey,
}
}

Expand Down
2 changes: 1 addition & 1 deletion testutil/datagen/btc_blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func GenRandomBtcdBlockWithTransactions(

var proofs []*btcctypes.BTCSpvProof

for i, _ := range msgTxs {
for i := range msgTxs {
headerBytes := bbn.NewBTCHeaderBytesFromBlockHeader(header)
proof, err := btcctypes.SpvProofFromHeaderAndTransactions(&headerBytes, txBytes, uint(i))
if err != nil {
Expand Down
Loading
Loading