clean up unused code and remove panic

pull/1085/head
Minh Doan 6 years ago committed by Minh Doan
parent 9924c02f95
commit 4ccaca9b7e
  1. 6
      cmd/client/wallet/main.go
  2. 37
      cmd/harmony/main.go
  3. 9
      consensus/consensus.go
  4. 6
      core/resharding.go
  5. 82
      internal/blsgen/lib.go
  6. 11
      internal/blsgen/lib_test.go
  7. 8
      internal/utils/utils.go

@ -399,7 +399,11 @@ func processBlsgenCommand() {
}
}
privateKey, fileName := blsgen.GenBlsKeyWithPassPhrase(password)
privateKey, fileName, err := blsgen.GenBlsKeyWithPassPhrase(password)
if err != nil {
fmt.Printf("error when generating bls key: %v\n", err)
os.Exit(100)
}
publickKey := privateKey.GetPublicKey()
fmt.Printf("Bls private key: %s\n", privateKey.SerializeToHexStr())
fmt.Printf("Bls public key: %s\n", publickKey.SerializeToHexStr())

@ -12,7 +12,6 @@ import (
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/log"
"github.com/harmony-one/bls/ffi/go/bls"
"github.com/harmony-one/harmony/accounts"
"github.com/harmony-one/harmony/accounts/keystore"
@ -103,9 +102,8 @@ var (
shardID = flag.Int("shard_id", -1, "the shard ID of this node")
enableMemProfiling = flag.Bool("enableMemProfiling", false, "Enable memsize logging.")
enableGC = flag.Bool("enableGC", true, "Enable calling garbage collector manually .")
blsPrivateKey = flag.String("blskey", "", "Bls serialized private key.")
blsKeyFile = flag.String("blskey_file", "", "The file of bls serialized private key with passphrase.")
blsPassFile = flag.String("blspass_file", "", "The file of bls serialized private key with passphrase.")
blsKeyFile = flag.String("blskey_file", "", "The encrypted file of bls serialized private key by passphrase.")
blsPassFile = flag.String("blspass_file", "", "The file containing passphrase to decrypt the encrypted bls file.")
// logConn logs incoming/outgoing connections
logConn = flag.Bool("log_conn", false, "log incoming/outgoing connections")
@ -228,30 +226,33 @@ func initSetup() {
}
func setUpConsensusKey(nodeConfig *nodeconfig.ConfigType) {
consensusPriKey := &bls.SecretKey{}
// If FN node running, they should either specify blsPrivateKey or the file with passphrase
if *blsPrivateKey != "" {
if err := consensusPriKey.DeserializeHexStr(*blsPrivateKey); err != nil {
panic(err)
if *blsKeyFile != "" && *blsPassFile != "" {
passPhrase, err := utils.ReadStringFromFile(*blsPassFile)
if err != nil {
fmt.Printf("error when reading passphrase file: %v\n", err)
os.Exit(100)
}
consensusPriKey, err := blsgen.LoadBlsKeyWithPassPhrase(*blsKeyFile, passPhrase)
if err != nil {
fmt.Printf("error when loading bls key, err :%v\n", err)
os.Exit(100)
}
} else if *blsKeyFile != "" && *blsPassFile != "" {
passPhrase := utils.ReadStringFromFile(*blsPassFile)
consensusPriKey = blsgen.LoadBlsKeyWithPassPhrase(*blsKeyFile, passPhrase)
if !genesis.IsBlsPublicKeyWhiteListed(consensusPriKey.GetPublicKey().SerializeToHexStr()) {
fmt.Println("Your bls key is not whitelisted")
os.Exit(101)
}
// Consensus keys are the BLS12-381 keys used to sign consensus messages
nodeConfig.ConsensusPriKey, nodeConfig.ConsensusPubKey = consensusPriKey, consensusPriKey.GetPublicKey()
if nodeConfig.ConsensusPriKey == nil || nodeConfig.ConsensusPubKey == nil {
fmt.Println("error to get consensus keys.")
os.Exit(100)
}
} else {
fmt.Println("Internal nodes need to have pass to decrypt blskey")
os.Exit(101)
}
// Consensus keys are the BLS12-381 keys used to sign consensus messages
nodeConfig.ConsensusPriKey, nodeConfig.ConsensusPubKey = consensusPriKey, consensusPriKey.GetPublicKey()
if nodeConfig.ConsensusPriKey == nil || nodeConfig.ConsensusPubKey == nil {
panic(fmt.Errorf("generate key error"))
}
}
func createGlobalConfig() *nodeconfig.ConfigType {

@ -405,15 +405,6 @@ func NewGenesisStakeInfoFinder() (*GenesisStakeInfoFinder, error) {
byAccount: make(map[common.Address][]*structs.StakeInfo),
}
for idx, account := range genesis.GenesisAccounts {
// TODO: this is old code. Will remove when migration will be successful.
// blsSecretKeyHex := account.BlsPriKey
// blsSecretKey := bls.SecretKey{}
// if err := blsSecretKey.DeserializeHexStr(blsSecretKeyHex); err != nil {
// return nil, ctxerror.New("cannot convert BLS secret key",
// "accountIndex", idx,
// ).WithCause(err)
// }
// pub := blsSecretKey.GetPublicKey()
pub := &bls.PublicKey{}
pub.DeserializeHexStr(account.BlsPublicKey)
var blsPublicKey types.BlsPublicKey

@ -23,11 +23,11 @@ const (
// FirstEpoch is the number of the first epoch.
FirstEpoch = 1
// GenesisShardNum is the number of shard at genesis
GenesisShardNum = 4
GenesisShardNum = 1
// GenesisShardSize is the size of each shard at genesis
GenesisShardSize = 100
GenesisShardSize = 20
// GenesisShardHarmonyNodes is the number of harmony node at each shard
GenesisShardHarmonyNodes = 72
GenesisShardHarmonyNodes = 20
// CuckooRate is the percentage of nodes getting reshuffled in the second step of cuckoo resharding.
CuckooRate = 0.1
)

@ -9,20 +9,13 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"time"
ffi_bls "github.com/harmony-one/bls/ffi/go/bls"
"github.com/harmony-one/harmony/crypto/bls"
)
// Constants for bls lib.
const (
BlsPriKeyStore = ".hmy/blsstore/"
)
func toISO8601(t time.Time) string {
var tz string
name, offset := t.Zone()
@ -35,55 +28,8 @@ func toISO8601(t time.Time) string {
t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), tz)
}
// WritePrivateKeyWithPassPhrase encrypt the key with passphrase and write into disk.
func WritePrivateKeyWithPassPhrase(privateKey *ffi_bls.SecretKey, passphrase string) string {
publickKey := privateKey.GetPublicKey()
fileName := publickKey.SerializeToHexStr() + ".key"
privateKeyHex := privateKey.SerializeToHexStr()
// Encrypt with passphrase
encryptedPrivateKeyStr := encrypt([]byte(privateKeyHex), passphrase)
// Write to file.
err := WriteToFile(fileName, encryptedPrivateKeyStr)
check(err)
return fileName
}
func visit(files *[]string) filepath.WalkFunc {
return func(path string, info os.FileInfo, err error) error {
if err != nil {
log.Fatal(err)
}
fi, err := os.Stat(path)
if !fi.Mode().IsDir() {
*files = append(*files, path)
}
return nil
}
}
// FindBlsPriKey finds bls private key from bls private store with passphrase.
func FindBlsPriKey(BlsPublicKey, blsPass string, consensusPriKey *ffi_bls.SecretKey) bool {
var files []string
var err error
if _, err = os.Stat(BlsPriKeyStore); os.IsNotExist(err) {
return false
}
err = filepath.Walk(BlsPriKeyStore, visit(&files))
check(err)
for _, f := range files {
k := LoadBlsKeyWithPassPhrase(f, blsPass)
if k.GetPublicKey().SerializeToHexStr() == BlsPublicKey {
consensusPriKey = k
return true
}
}
return false
}
// GenBlsKeyWithPassPhrase generates bls key with passphrase and write into disk.
func GenBlsKeyWithPassPhrase(passphrase string) (*ffi_bls.SecretKey, string) {
func GenBlsKeyWithPassPhrase(passphrase string) (*ffi_bls.SecretKey, string, error) {
privateKey := bls.RandPrivateKey()
publickKey := privateKey.GetPublicKey()
fileName := publickKey.SerializeToHexStr() + ".key"
@ -92,32 +38,36 @@ func GenBlsKeyWithPassPhrase(passphrase string) (*ffi_bls.SecretKey, string) {
encryptedPrivateKeyStr := encrypt([]byte(privateKeyHex), passphrase)
// Write to file.
err := WriteToFile(fileName, encryptedPrivateKeyStr)
check(err)
return privateKey, fileName
return privateKey, fileName, err
}
// WriteToFile will print any string of text to a file safely by
// checking for errors and syncing at the end.
func WriteToFile(filename string, data string) error {
file, err := os.Create(filename)
check(err)
if err != nil {
return err
}
defer file.Close()
_, err = io.WriteString(file, data)
check(err)
if err != nil {
return err
}
return file.Sync()
}
// LoadBlsKeyWithPassPhrase loads bls key with passphrase.
func LoadBlsKeyWithPassPhrase(fileName, passphrase string) *ffi_bls.SecretKey {
func LoadBlsKeyWithPassPhrase(fileName, passphrase string) (*ffi_bls.SecretKey, error) {
encryptedPrivateKeyBytes, err := ioutil.ReadFile(fileName)
check(err)
if err != nil {
return nil, err
}
encryptedPrivateKeyStr := string(encryptedPrivateKeyBytes)
decryptedBytes := decrypt(encryptedPrivateKeyStr, passphrase)
priKey := &ffi_bls.SecretKey{}
priKey.DeserializeHexStr(string(decryptedBytes))
return priKey
return priKey, nil
}
func createHash(key string) string {
@ -164,9 +114,3 @@ func decrypt(encryptedStr string, passphrase string) []byte {
}
return plaintext
}
func check(err error) {
if err != nil {
panic(err)
}
}

@ -7,8 +7,15 @@ import (
// TestUpdateStakingList creates a bls key with passphrase and compare it with the one loaded from the generated file.
func TestUpdateStakingList(t *testing.T) {
privateKey, fileName := GenBlsKeyWithPassPhrase("abcd")
anotherPriKey := LoadBlsKeyWithPassPhrase(fileName, "abcd")
var err error
privateKey, fileName, err := GenBlsKeyWithPassPhrase("abcd")
if err != nil {
t.Error(err)
}
anotherPriKey, err := LoadBlsKeyWithPassPhrase(fileName, "abcd")
if err != nil {
t.Error(err)
}
if !privateKey.IsEqual(anotherPriKey) {
t.Error("Error when generating bls key.")

@ -244,10 +244,12 @@ func GetPortFromDiff(port string, diff int) string {
}
// ReadStringFromFile reads string from a file.
func ReadStringFromFile(fileName string) string {
func ReadStringFromFile(fileName string) (string, error) {
data, err := ioutil.ReadFile(fileName)
check(err)
return string(data)
if err != nil {
return "", err
}
return string(data), nil
}
func check(err error) {

Loading…
Cancel
Save