You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
97 lines
2.3 KiB
97 lines
2.3 KiB
5 years ago
|
package wallet
|
||
6 years ago
|
|
||
|
// this module in utils handles the ini file read/write
|
||
|
import (
|
||
|
"fmt"
|
||
5 years ago
|
"os"
|
||
6 years ago
|
"strings"
|
||
|
|
||
5 years ago
|
ini "gopkg.in/ini.v1"
|
||
5 years ago
|
|
||
|
"github.com/harmony-one/harmony/internal/params"
|
||
6 years ago
|
"github.com/harmony-one/harmony/p2p"
|
||
|
)
|
||
|
|
||
5 years ago
|
// Profile contains a section and key value pair map
|
||
|
type Profile struct {
|
||
6 years ago
|
Profile string
|
||
5 years ago
|
ChainID string
|
||
6 years ago
|
Bootnodes []string
|
||
|
Shards int
|
||
|
RPCServer [][]p2p.Peer
|
||
5 years ago
|
Network string
|
||
6 years ago
|
}
|
||
|
|
||
5 years ago
|
// ReadProfile reads an ini file and return Profile
|
||
|
func ReadProfile(iniBytes []byte, profile string) (*Profile, error) {
|
||
6 years ago
|
cfg, err := ini.ShadowLoad(iniBytes)
|
||
6 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
5 years ago
|
config := new(Profile)
|
||
6 years ago
|
config.Profile = profile
|
||
|
|
||
|
// get the profile section
|
||
|
sec, err := cfg.GetSection(profile)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
5 years ago
|
profile = sec.Name() // sanitized name
|
||
6 years ago
|
|
||
|
if sec.HasKey("bootnode") {
|
||
|
config.Bootnodes = sec.Key("bootnode").ValueWithShadows()
|
||
|
} else {
|
||
|
return nil, fmt.Errorf("can't find bootnode key")
|
||
|
}
|
||
5 years ago
|
if sec.HasKey("network") {
|
||
|
config.Network = sec.Key("network").String()
|
||
|
} else {
|
||
|
config.Network = "devnet"
|
||
|
}
|
||
5 years ago
|
if sec.HasKey("chain_id") {
|
||
|
config.ChainID = sec.Key("chain_id").String()
|
||
|
} else {
|
||
|
// backward compatibility; use profile name to determine
|
||
5 years ago
|
// (deprecated; require chain_id after 2010-01).
|
||
5 years ago
|
switch profile {
|
||
5 years ago
|
case "main", "default":
|
||
|
config.ChainID = params.MainnetChainID.String()
|
||
5 years ago
|
case "pangaea":
|
||
|
config.ChainID = params.PangaeaChainID.String()
|
||
5 years ago
|
default:
|
||
|
config.ChainID = params.TestnetChainID.String()
|
||
|
}
|
||
5 years ago
|
_, _ = fmt.Fprintf(os.Stderr,
|
||
|
"NOTICE: Chain ID not found in config profile, assuming %s; "+
|
||
|
"please add \"chain_id = %s\" to section [%s] of wallet.ini "+
|
||
|
"before 2020-01\n",
|
||
|
config.ChainID, config.ChainID, profile)
|
||
5 years ago
|
}
|
||
6 years ago
|
|
||
|
if sec.HasKey("shards") {
|
||
|
config.Shards = sec.Key("shards").MustInt()
|
||
|
config.RPCServer = make([][]p2p.Peer, config.Shards)
|
||
|
} else {
|
||
|
return nil, fmt.Errorf("can't find shards key")
|
||
|
}
|
||
|
|
||
|
for i := 0; i < config.Shards; i++ {
|
||
|
rpcSec, err := cfg.GetSection(fmt.Sprintf("%s.shard%v.rpc", profile, i))
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
rpcKey := rpcSec.Key("rpc").ValueWithShadows()
|
||
|
for _, key := range rpcKey {
|
||
|
v := strings.Split(key, ":")
|
||
|
rpc := p2p.Peer{
|
||
|
IP: v[0],
|
||
|
Port: v[1],
|
||
|
}
|
||
|
config.RPCServer[i] = append(config.RPCServer[i], rpc)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return config, nil
|
||
|
|
||
|
}
|