OSDN Git Service

Fix check network type bug (#578)
authoryahtoo <yahtoo.ma@gmail.com>
Tue, 10 Apr 2018 14:06:11 +0000 (22:06 +0800)
committerPaladz <yzhu101@uottawa.ca>
Tue, 10 Apr 2018 14:06:11 +0000 (22:06 +0800)
* Fix check network type bug

* Del useless code

blockchain/rpc/types/responses.go
cmd/bytomd/commands/init.go
cmd/bytomd/commands/run_node.go
config/config.go
config/config_test.go
config/toml.go
netsync/handle.go
types/genesis.go [deleted file]

index b991693..710f001 100644 (file)
@@ -4,11 +4,11 @@ import (
        "strings"
        "time"
 
-       "github.com/bytom/p2p"
-       "github.com/bytom/protocol/bc"
-       "github.com/bytom/types"
        "github.com/tendermint/go-crypto"
        "github.com/tendermint/go-wire/data"
+
+       "github.com/bytom/p2p"
+       "github.com/bytom/protocol/bc"
 )
 
 type BlockNonce [8]byte
@@ -17,10 +17,6 @@ type ResultBlockchainInfo struct {
        LastHeight uint64 `json:"last_height"`
 }
 
-type ResultGenesis struct {
-       Genesis *types.GenesisDoc `json:"genesis"`
-}
-
 type ResultBlock struct {
 }
 
index 690f800..15baa35 100644 (file)
@@ -1,16 +1,9 @@
 package commands
 
 import (
-       "encoding/hex"
-       "os"
-
-       log "github.com/sirupsen/logrus"
        "github.com/spf13/cobra"
-       cmn "github.com/tendermint/tmlibs/common"
 
        cfg "github.com/bytom/config"
-       "github.com/bytom/crypto/ed25519/chainkd"
-       "github.com/bytom/types"
 )
 
 var initFilesCmd = &cobra.Command{
@@ -31,21 +24,4 @@ func initFiles(cmd *cobra.Command, args []string) {
        } else {
                cfg.EnsureRoot(config.RootDir, "testnet")
        }
-
-       genFile := config.GenesisFile()
-       if _, err := os.Stat(genFile); !os.IsNotExist(err) {
-               log.WithField("genesis", config.GenesisFile()).Info("Already exists config file.")
-               return
-       }
-       xprv, err := chainkd.NewXPrv(nil)
-       if err != nil {
-               log.WithField("error", err).Error("Spawn node's key failed.")
-               return
-       }
-       genDoc := types.GenesisDoc{
-               ChainID:    cmn.Fmt(config.ChainID),
-               PrivateKey: hex.EncodeToString(xprv.Bytes()),
-       }
-       genDoc.SaveAs(genFile)
-       log.WithField("genesis", config.GenesisFile()).Info("Initialized bytom")
 }
index 8fbd226..60c0d1f 100644 (file)
@@ -2,14 +2,11 @@ package commands
 
 import (
        "fmt"
-       "io/ioutil"
 
        log "github.com/sirupsen/logrus"
        "github.com/spf13/cobra"
-       cmn "github.com/tendermint/tmlibs/common"
 
        "github.com/bytom/node"
-       "github.com/bytom/types"
 )
 
 var runNodeCmd = &cobra.Command{
@@ -27,6 +24,7 @@ func init() {
        runNodeCmd.Flags().Bool("wallet.disable", config.Wallet.Disable, "Disable wallet")
 
        runNodeCmd.Flags().Bool("web.closed", config.Web.Closed, "Lanch web browser or not")
+       runNodeCmd.Flags().String("chain_id", config.ChainID, "Select network type")
 
        // p2p flags
        runNodeCmd.Flags().String("p2p.laddr", config.P2P.ListenAddress, "Node listen address. (0.0.0.0:0 means any interface, any port)")
@@ -41,27 +39,6 @@ func init() {
 }
 
 func runNode(cmd *cobra.Command, args []string) error {
-       genDocFile := config.GenesisFile()
-       if cmn.FileExists(genDocFile) {
-               jsonBlob, err := ioutil.ReadFile(genDocFile)
-               if err != nil {
-                       return fmt.Errorf("Couldn't read GenesisDoc file: %v ", err)
-               }
-               genDoc, err := types.GenesisDocFromJSON(jsonBlob)
-               if err != nil {
-                       return fmt.Errorf("Error reading GenesisDoc: %v ", err)
-               }
-               if genDoc.ChainID == "" {
-                       return fmt.Errorf("Genesis doc %v must include non-empty chain_id ", genDocFile)
-               }
-
-               config.ChainID = genDoc.ChainID
-               config.PrivateKey = genDoc.PrivateKey
-               config.Time = genDoc.GenesisTime
-       } else {
-               return fmt.Errorf("not find genesis.json")
-       }
-
        // Create & start node
        n := node.NewNode(config)
        if _, err := n.Start(); err != nil {
index e25cf79..f73f7ea 100644 (file)
@@ -40,9 +40,6 @@ type BaseConfig struct {
        // This should be set in viper so it can unmarshal into this struct
        RootDir string `mapstructure:"home"`
 
-       // A JSON file containing the initial validator set and other meta data
-       Genesis string `mapstructure:"genesis_file"`
-
        //The ID of the network to json
        ChainID string `mapstructure:"chain_id"`
 
@@ -87,7 +84,6 @@ type BaseConfig struct {
 // Default configurable base parameters.
 func DefaultBaseConfig() BaseConfig {
        return BaseConfig{
-               Genesis:           "genesis.json",
                Moniker:           "anonymous",
                ProfListenAddress: "",
                FastSync:          true,
@@ -101,10 +97,6 @@ func DefaultBaseConfig() BaseConfig {
        }
 }
 
-func (b BaseConfig) GenesisFile() string {
-       return rootify(b.Genesis, b.RootDir)
-}
-
 func (b BaseConfig) DBDir() string {
        return rootify(b.DBPath, b.RootDir)
 }
index 673b4a4..f324d40 100644 (file)
@@ -15,10 +15,8 @@ func TestDefaultConfig(t *testing.T) {
 
        // check the root dir stuff...
        cfg.SetRoot("/foo")
-       cfg.Genesis = "bar"
        cfg.DBPath = "/opt/data"
 
-       assert.Equal("/foo/bar", cfg.GenesisFile())
        assert.Equal("/opt/data", cfg.DBDir())
 
 }
index 6c29dd6..c9fd589 100644 (file)
@@ -24,21 +24,25 @@ var defaultConfigTmpl = `# This is a TOML config file.
 fast_sync = true
 db_backend = "leveldb"
 api_addr = "0.0.0.0:9888"
+`
 
+var mainNetConfigTmpl = `chain_id = "mainnet"
 [p2p]
 laddr = "tcp://0.0.0.0:46656"
+seeds = ""
 `
 
-var testnetSeeds = `
+var testNetConfigTmpl = `chain_id = "testnet"
+[p2p]
+laddr = "tcp://0.0.0.0:46656"
 seeds = "139.162.105.40:46656,139.162.88.74:46656,47.96.42.1:46656,45.79.213.28:46656,212.111.41.245:46656"
 `
-var mainnetSeeds = `seeds = ""`
 
 // Select network seeds to merge a new string.
 func selectNetwork(network string) string {
        if network == "testnet" {
-               return defaultConfigTmpl + testnetSeeds
+               return defaultConfigTmpl + testNetConfigTmpl
        } else {
-               return defaultConfigTmpl + mainnetSeeds
+               return defaultConfigTmpl + mainNetConfigTmpl
        }
 }
index 2438e09..770b4d4 100644 (file)
@@ -88,7 +88,7 @@ func (sm *SyncManager) makeNodeInfo() *p2p.NodeInfo {
        nodeInfo := &p2p.NodeInfo{
                PubKey:  sm.privKey.PubKey().Unwrap().(crypto.PubKeyEd25519),
                Moniker: sm.config.Moniker,
-               Network: "bytom",
+               Network: sm.config.ChainID,
                Version: version.Version,
                Other: []string{
                        cmn.Fmt("wire_version=%v", wire.Version),
diff --git a/types/genesis.go b/types/genesis.go
deleted file mode 100644 (file)
index a3566fc..0000000
+++ /dev/null
@@ -1,34 +0,0 @@
-package types
-
-import (
-       "encoding/json"
-       "time"
-
-       cmn "github.com/tendermint/tmlibs/common"
-)
-
-//------------------------------------------------------------
-// core types for a genesis definition
-type GenesisDoc struct {
-       GenesisTime time.Time `json:"genesis_time"`
-       ChainID     string    `json:"chain_id"`
-       PrivateKey  string    `json:"private_key"`
-}
-
-// Utility method for saving GenensisDoc as JSON file.
-func (genDoc *GenesisDoc) SaveAs(file string) error {
-       genDocBytes, err := json.Marshal(genDoc)
-       if err != nil {
-               return err
-       }
-       return cmn.WriteFile(file, genDocBytes, 0644)
-}
-
-//------------------------------------------------------------
-// Make genesis state from file
-
-func GenesisDocFromJSON(jsonBlob []byte) (*GenesisDoc, error) {
-       genDoc := GenesisDoc{}
-       err := json.Unmarshal(jsonBlob, &genDoc)
-       return &genDoc, err
-}