OSDN Git Service

V0.1 federation parse (#89)
authorwz <mars@bytom.io>
Mon, 27 May 2019 16:18:10 +0000 (00:18 +0800)
committerPaladz <yzhu101@uottawa.ca>
Mon, 27 May 2019 16:18:10 +0000 (00:18 +0800)
* add init federation

* add init

* add load federation info

* add test

cmd/bytomd/commands/init.go
config/config.go
config/federation.go [new file with mode: 0644]
config/federation_test.go [new file with mode: 0644]
node/node.go

index 331a346..af7bde7 100644 (file)
@@ -36,5 +36,16 @@ func initFiles(cmd *cobra.Command, args []string) {
                cfg.EnsureRoot(config.RootDir, "solonet")
        }
 
+       fedFile := config.FederationFile()
+       if _, err := os.Stat(fedFile); !os.IsNotExist(err) {
+               log.WithFields(log.Fields{"module": logModule, "config": fedFile}).Info("Already exists federation file.")
+               return
+       }
+
+       if err := cfg.ExportFederationFile(fedFile, config); err != nil {
+               log.WithFields(log.Fields{"module": logModule, "config": fedFile, "error": err}).Info("exportFederationFile failed.")
+               return
+       }
+
        log.WithFields(log.Fields{"module": logModule, "config": configFilePath}).Info("Initialized bytom")
 }
index c04662f..bf23881 100644 (file)
@@ -120,17 +120,21 @@ type BaseConfig struct {
 
        // Cipher Service Provider
        // CipherServiceProvider string `mapstructure:"csp"`
+
+       // Federation file name
+       FederationFileName string `mapstructure:"federation_file"`
 }
 
 // Default configurable base parameters.
 func DefaultBaseConfig() BaseConfig {
        return BaseConfig{
-               Moniker:           "anonymous",
-               ProfListenAddress: "",
-               Mining:            false,
-               DBBackend:         "leveldb",
-               DBPath:            "data",
-               KeysPath:          "keystore",
+               Moniker:            "anonymous",
+               ProfListenAddress:  "",
+               Mining:             false,
+               DBBackend:          "leveldb",
+               DBPath:             "data",
+               KeysPath:           "keystore",
+               FederationFileName: "federation.json",
        }
 }
 
@@ -142,6 +146,10 @@ func (b BaseConfig) KeysDir() string {
        return rootify(b.KeysPath, b.RootDir)
 }
 
+func (b BaseConfig) FederationFile() string {
+       return rootify(b.FederationFileName, b.RootDir)
+}
+
 // P2PConfig
 type P2PConfig struct {
        ListenAddress    string `mapstructure:"laddr"`
diff --git a/config/federation.go b/config/federation.go
new file mode 100644 (file)
index 0000000..5d5f913
--- /dev/null
@@ -0,0 +1,30 @@
+package config
+
+import (
+       "bytes"
+       "encoding/json"
+       "io/ioutil"
+       "os"
+)
+
+func ExportFederationFile(fedFile string, config *Config) error {
+       buf := new(bytes.Buffer)
+
+       encoder := json.NewEncoder(buf)
+       encoder.SetIndent("", "  ")
+       if err := encoder.Encode(config.Federation); err != nil {
+               return err
+       }
+
+       return ioutil.WriteFile(fedFile, buf.Bytes(), 0644)
+}
+
+func LoadFederationFile(fedFile string, config *Config) error {
+       file, err := os.Open(fedFile)
+       if err != nil {
+               return err
+       }
+       defer file.Close()
+
+       return json.NewDecoder(file).Decode(config.Federation)
+}
diff --git a/config/federation_test.go b/config/federation_test.go
new file mode 100644 (file)
index 0000000..00e92ad
--- /dev/null
@@ -0,0 +1,37 @@
+package config
+
+import (
+       "io/ioutil"
+       "os"
+       "testing"
+
+       "github.com/vapor/testutil"
+)
+
+func TestFederation(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
+
+       if err := ExportFederationFile(config.FederationFile(), config); err != nil {
+               t.Fatal(err)
+       }
+
+       loadConfig := &Config{
+               Federation: &FederationConfig{},
+       }
+
+       if err := LoadFederationFile(config.FederationFile(), loadConfig); err != nil {
+               t.Fatal(err)
+       }
+
+       if !testutil.DeepEqual(config.Federation, loadConfig.Federation) {
+               t.Fatalf("export: %v, load: %v", config.Federation, loadConfig.Federation)
+       }
+}
index 01e901f..628fab2 100644 (file)
@@ -62,6 +62,9 @@ func NewNode(config *cfg.Config) *Node {
        if err := lockDataDirectory(config); err != nil {
                cmn.Exit("Error: " + err.Error())
        }
+       if err := cfg.LoadFederationFile(config.FederationFile(), config); err != nil {
+               cmn.Exit(cmn.Fmt("Failed to load federated information:[%s]", err.Error()))
+       }
        initLogFile(config)
        initActiveNetParams(config)
        initCommonConfig(config)