From 294e8688aed1191909dca717e1131cd64c7cbc80 Mon Sep 17 00:00:00 2001 From: Mingjing <2595400537@qq.com> Date: Sat, 17 Apr 2021 19:34:37 +0800 Subject: [PATCH] feat(config): replace NodeKey (#1897) * feat(config): replace NodeKey function * style(config):adjust import order --- cmd/bytomd/commands/run_node.go | 1 - config/config.go | 50 +++++++++++++++++++++-------------------- config/config_test.go | 38 ------------------------------- p2p/switch.go | 12 +--------- p2p/switch_test.go | 12 ---------- 5 files changed, 27 insertions(+), 86 deletions(-) diff --git a/cmd/bytomd/commands/run_node.go b/cmd/bytomd/commands/run_node.go index e562acb8..b510cd1a 100644 --- a/cmd/bytomd/commands/run_node.go +++ b/cmd/bytomd/commands/run_node.go @@ -37,7 +37,6 @@ func init() { // p2p flags runNodeCmd.Flags().String("p2p.laddr", config.P2P.ListenAddress, "Node listen address. (0.0.0.0:0 means any interface, any port)") runNodeCmd.Flags().String("p2p.seeds", config.P2P.Seeds, "Comma delimited host:port seed nodes") - runNodeCmd.Flags().String("p2p.node_key", config.P2P.PrivateKey, "Node key for p2p communication") runNodeCmd.Flags().Bool("p2p.skip_upnp", config.P2P.SkipUPNP, "Skip UPNP configuration") runNodeCmd.Flags().Bool("p2p.lan_discoverable", config.P2P.LANDiscover, "Whether the node can be discovered by nodes in the LAN") runNodeCmd.Flags().Int("p2p.max_num_peers", config.P2P.MaxNumPeers, "Set max num peers") diff --git a/config/config.go b/config/config.go index 71fcf1f3..de68907b 100644 --- a/config/config.go +++ b/config/config.go @@ -1,8 +1,8 @@ package config import ( + "encoding/hex" "io" - "io/ioutil" "os" "os/user" "path/filepath" @@ -10,7 +10,7 @@ import ( log "github.com/sirupsen/logrus" - "github.com/bytom/bytom/crypto/ed25519" + "github.com/bytom/bytom/crypto/ed25519/chainkd" ) var ( @@ -52,32 +52,32 @@ func (cfg *Config) SetRoot(root string) *Config { // NodeKey retrieves the currently configured private key of the node, checking // first any manually set key, falling back to the one found in the configured // data folder. If no key can be found, a new one is generated. -func (cfg *Config) NodeKey() (string, error) { - // Use any specifically configured key. - if cfg.P2P.PrivateKey != "" { - return cfg.P2P.PrivateKey, nil +func (cfg *Config) PrivateKey() *chainkd.XPrv { + if cfg.XPrv != nil { + return cfg.XPrv } - keyFile := rootify(cfg.P2P.NodeKeyFile, cfg.BaseConfig.RootDir) - buf := make([]byte, ed25519.PrivateKeySize*2) - fd, err := os.Open(keyFile) - defer fd.Close() - if err == nil { - if _, err = io.ReadFull(fd, buf); err == nil { - return string(buf), nil - } + filePath := rootify(cfg.PrivateKeyFile, cfg.BaseConfig.RootDir) + fildReader, err := os.Open(filePath) + if err != nil { + log.WithField("err", err).Panic("fail on open private key file") } - log.WithField("err", err).Warning("key file access failed") - _, privKey, err := ed25519.GenerateKey(nil) - if err != nil { - return "", err + defer fildReader.Close() + buf := make([]byte, 128) + if _, err = io.ReadFull(fildReader, buf); err != nil { + log.WithField("err", err).Panic("fail on read private key file") } - if err = ioutil.WriteFile(keyFile, []byte(privKey.String()), 0600); err != nil { - return "", err + var xprv chainkd.XPrv + if _, err := hex.Decode(xprv[:], buf); err != nil { + log.WithField("err", err).Panic("fail on decode private key") } - return privKey.String(), nil + + cfg.XPrv = &xprv + xpub := cfg.XPrv.XPub() + cfg.XPub = &xpub + return cfg.XPrv } //----------------------------------------------------------------------------- @@ -117,6 +117,10 @@ type BaseConfig struct { // log file name LogFile string `mapstructure:"log_file"` + + PrivateKeyFile string `mapstructure:"private_key_file"` + XPrv *chainkd.XPrv + XPub *chainkd.XPub } // Default configurable base parameters. @@ -129,6 +133,7 @@ func DefaultBaseConfig() BaseConfig { KeysPath: "keystore", NodeAlias: "", LogFile: "log", + PrivateKeyFile: "node_key.txt", } } @@ -148,8 +153,6 @@ func (b BaseConfig) KeysDir() string { type P2PConfig struct { ListenAddress string `mapstructure:"laddr"` Seeds string `mapstructure:"seeds"` - PrivateKey string `mapstructure:"node_key"` - NodeKeyFile string `mapstructure:"node_key_file"` SkipUPNP bool `mapstructure:"skip_upnp"` LANDiscover bool `mapstructure:"lan_discoverable"` MaxNumPeers int `mapstructure:"max_num_peers"` @@ -165,7 +168,6 @@ type P2PConfig struct { func DefaultP2PConfig() *P2PConfig { return &P2PConfig{ ListenAddress: "tcp://0.0.0.0:46656", - NodeKeyFile: "nodekey", SkipUPNP: false, LANDiscover: true, MaxNumPeers: 50, diff --git a/config/config_test.go b/config/config_test.go index 280f47e7..f324d40a 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -1,9 +1,6 @@ package config import ( - "io/ioutil" - "os" - "strings" "testing" "github.com/stretchr/testify/assert" @@ -23,38 +20,3 @@ func TestDefaultConfig(t *testing.T) { assert.Equal("/opt/data", cfg.DBDir()) } - -func TestNodeKey(t *testing.T) { - tmpDir, err := ioutil.TempDir(".", "") - if err != nil { - t.Fatalf("failed to create temporary data folder: %v", err) - } - defer os.RemoveAll(tmpDir) - config := DefaultConfig() - config.BaseConfig.RootDir = tmpDir - - config.P2P.PrivateKey = "0fcbd0be11e35c35c41c686b7ca597bbcf8ecb78e320d01a93349c8ce9420ea4f26d0fbe651bb2c248d6727801329b589ed19e384c9e906d1da4ab2360558bc0" - privKey, err := config.NodeKey() - if err != nil { - t.Fatal("test node key error:", err) - } - - if strings.Compare(privKey, config.P2P.PrivateKey) != 0 { - t.Fatal("test node key error. want:", config.P2P.PrivateKey, "got:", privKey) - } - - config.P2P.PrivateKey = "" - writePrivKey, err := config.NodeKey() - if err != nil { - t.Fatal("test node key error:", err) - } - - readPrivKey, err := config.NodeKey() - if err != nil { - t.Fatal("test node key error:", err) - } - - if strings.Compare(writePrivKey, readPrivKey) != 0 { - t.Fatal("test node key error. write:", writePrivKey, "read:", readPrivKey) - } -} diff --git a/p2p/switch.go b/p2p/switch.go index 7eb3d652..a22f7ca7 100644 --- a/p2p/switch.go +++ b/p2p/switch.go @@ -1,7 +1,6 @@ package p2p import ( - "encoding/hex" "fmt" "net" "sync" @@ -84,16 +83,7 @@ func NewSwitch(config *cfg.Config) (*Switch, error) { var discv *dht.Network var lanDiscv *mdns.LANDiscover - config.P2P.PrivateKey, err = config.NodeKey() - if err != nil { - return nil, err - } - - bytes, err := hex.DecodeString(config.P2P.PrivateKey) - if err != nil { - return nil, err - } - + bytes := config.PrivateKey().Bytes() var newKey [64]byte copy(newKey[:], bytes) privKey := crypto.PrivKeyEd25519(newKey) diff --git a/p2p/switch_test.go b/p2p/switch_test.go index b577c276..af3cc22b 100644 --- a/p2p/switch_test.go +++ b/p2p/switch_test.go @@ -1,7 +1,6 @@ package p2p import ( - "encoding/hex" "io/ioutil" "os" "sync" @@ -140,7 +139,6 @@ func TestFiltersOutItself(t *testing.T) { cfg.DBPath = dirPath cfg.P2P.ListenAddress = "127.0.1.1:0" swPrivKey := crypto.GenPrivKeyEd25519() - cfg.P2P.PrivateKey = hex.EncodeToString(swPrivKey.Bytes()) s1 := MakeSwitch(&cfg, testDB, swPrivKey, initSwitchFunc) s1.Start() defer s1.Stop() @@ -182,7 +180,6 @@ func TestDialBannedPeer(t *testing.T) { cfg.DBPath = dirPath cfg.P2P.ListenAddress = "127.0.1.1:0" swPrivKey := crypto.GenPrivKeyEd25519() - cfg.P2P.PrivateKey = hex.EncodeToString(swPrivKey.Bytes()) s1 := MakeSwitch(&cfg, testDB, swPrivKey, initSwitchFunc) s1.Start() defer s1.Stop() @@ -221,7 +218,6 @@ func TestDuplicateOutBoundPeer(t *testing.T) { cfg.DBPath = dirPath cfg.P2P.ListenAddress = "127.0.1.1:0" swPrivKey := crypto.GenPrivKeyEd25519() - cfg.P2P.PrivateKey = hex.EncodeToString(swPrivKey.Bytes()) s1 := MakeSwitch(&cfg, testDB, swPrivKey, initSwitchFunc) s1.Start() defer s1.Stop() @@ -259,7 +255,6 @@ func TestDuplicateInBoundPeer(t *testing.T) { cfg.DBPath = dirPath cfg.P2P.ListenAddress = "127.0.1.1:0" swPrivKey := crypto.GenPrivKeyEd25519() - cfg.P2P.PrivateKey = hex.EncodeToString(swPrivKey.Bytes()) s1 := MakeSwitch(&cfg, testDB, swPrivKey, initSwitchFunc) s1.Start() defer s1.Stop() @@ -296,14 +291,12 @@ func TestAddInboundPeer(t *testing.T) { cfg.P2P.MaxNumPeers = 2 cfg.P2P.ListenAddress = "127.0.1.1:0" swPrivKey := crypto.GenPrivKeyEd25519() - cfg.P2P.PrivateKey = hex.EncodeToString(swPrivKey.Bytes()) s1 := MakeSwitch(&cfg, testDB, swPrivKey, initSwitchFunc) s1.Start() defer s1.Stop() inpCfg := *testCfg inpPrivKey := crypto.GenPrivKeyEd25519() - inpCfg.P2P.PrivateKey = hex.EncodeToString(swPrivKey.Bytes()) inp := &inboundPeer{PrivKey: inpPrivKey, config: &inpCfg} addr := NewNetAddress(s1.listeners[0].(*DefaultListener).NetListener().Addr()) if err != nil { @@ -313,7 +306,6 @@ func TestAddInboundPeer(t *testing.T) { rpCfg := *testCfg rpPrivKey := crypto.GenPrivKeyEd25519() - rpCfg.P2P.PrivateKey = hex.EncodeToString(swPrivKey.Bytes()) rp := &remotePeer{PrivKey: rpPrivKey, Config: &rpCfg} rp.Start() defer rp.Stop() @@ -324,7 +316,6 @@ func TestAddInboundPeer(t *testing.T) { inp2Cfg := *testCfg inp2PrivKey := crypto.GenPrivKeyEd25519() - inp2Cfg.P2P.PrivateKey = hex.EncodeToString(swPrivKey.Bytes()) inp2 := &inboundPeer{PrivKey: inp2PrivKey, config: &inp2Cfg} go inp2.dial(addr) @@ -349,14 +340,12 @@ func TestStopPeer(t *testing.T) { cfg.P2P.MaxNumPeers = 2 cfg.P2P.ListenAddress = "127.0.1.1:0" swPrivKey := crypto.GenPrivKeyEd25519() - cfg.P2P.PrivateKey = hex.EncodeToString(swPrivKey.Bytes()) s1 := MakeSwitch(&cfg, testDB, swPrivKey, initSwitchFunc) s1.Start() defer s1.Stop() inpCfg := *testCfg inpPrivKey := crypto.GenPrivKeyEd25519() - inpCfg.P2P.PrivateKey = hex.EncodeToString(swPrivKey.Bytes()) inp := &inboundPeer{PrivKey: inpPrivKey, config: &inpCfg} addr := NewNetAddress(s1.listeners[0].(*DefaultListener).NetListener().Addr()) if err != nil { @@ -366,7 +355,6 @@ func TestStopPeer(t *testing.T) { rpCfg := *testCfg rpPrivKey := crypto.GenPrivKeyEd25519() - rpCfg.P2P.PrivateKey = hex.EncodeToString(swPrivKey.Bytes()) rp := &remotePeer{PrivKey: rpPrivKey, Config: &rpCfg} rp.Start() defer rp.Stop() -- 2.11.0