github.com/aergoio/aergo@v1.3.1/polaris/server/genesisreader.go (about)

     1  /*
     2   * @file
     3   * @copyright defined in aergo/LICENSE.txt
     4   */
     5  
     6  package server
     7  
     8  import (
     9  	"encoding/json"
    10  	"fmt"
    11  	"github.com/aergoio/aergo/types"
    12  	"os"
    13  )
    14  
    15  // readGenesis is based on code in cmd/aergosvr/cmd.go .
    16  func readGenesis(filePath string) (ret *types.Genesis, rerr error) {
    17  		jsonpath := filePath
    18  
    19  		file, err := os.Open(jsonpath)
    20  		if err != nil {
    21  			rerr = fmt.Errorf("fail to open %s \n", jsonpath)
    22  			return
    23  		}
    24  		defer file.Close()
    25  
    26  
    27  		// use default config's DataDir if empty
    28  		genesis := new(types.Genesis)
    29  		if err := json.NewDecoder(file).Decode(genesis); err != nil {
    30  			rerr = fmt.Errorf("fail to deserialize %s (error:%s)\n", jsonpath, err)
    31  			return
    32  		}
    33  
    34  		if err := genesis.Validate(); err != nil {
    35  			rerr = fmt.Errorf( " %s (error:%s)\n", jsonpath, err)
    36  			return
    37  		}
    38  		ret = genesis
    39  		return
    40  }
    41