Command line now overrides saved config, saved config overrides system default

This commit is contained in:
nolash 2016-12-22 00:54:29 +01:00
parent 0ee796632a
commit b520d7caa5
2 changed files with 20 additions and 4 deletions

View file

@ -39,7 +39,7 @@ import (
"github.com/ethereum/go-ethereum/p2p/discover"
"github.com/ethereum/go-ethereum/swarm"
bzzapi "github.com/ethereum/go-ethereum/swarm/api"
"github.com/ethereum/go-ethereum/swarm/network"
//"github.com/ethereum/go-ethereum/swarm/network"
"gopkg.in/urfave/cli.v1"
)
@ -76,7 +76,8 @@ var (
SwarmNetworkIdFlag = cli.IntFlag{
Name: "bzznetworkid",
Usage: "Network identifier (integer, default 3=swarm testnet)",
Value: network.NetworkId,
// defining this here means we can't distinguish if it was passed at cmd line or system default when deciding to use saved values from config
//Value: network.NetworkId,
}
SwarmConfigPathFlag = cli.StringFlag{
Name: "bzzconfig",
@ -237,6 +238,7 @@ func bzzd(ctx *cli.Context) error {
}
func registerBzzService(ctx *cli.Context, stack *node.Node) {
prvkey := getAccount(ctx, stack)
chbookaddr := common.HexToAddress(ctx.GlobalString(ChequebookAddrFlag.Name))
@ -244,6 +246,7 @@ func registerBzzService(ctx *cli.Context, stack *node.Node) {
if bzzdir == "" {
bzzdir = stack.InstanceDir()
}
bzzconfig, err := bzzapi.NewConfig(bzzdir, chbookaddr, prvkey, ctx.GlobalUint64(SwarmNetworkIdFlag.Name))
if err != nil {
utils.Fatalf("unable to configure swarm: %v", err)
@ -274,6 +277,7 @@ func registerBzzService(ctx *cli.Context, stack *node.Node) {
func getAccount(ctx *cli.Context, stack *node.Node) *ecdsa.PrivateKey {
keyid := ctx.GlobalString(SwarmAccountFlag.Name)
if keyid == "" {
utils.Fatalf("Option %q is required", SwarmAccountFlag.Name)
}

View file

@ -85,10 +85,16 @@ func NewConfig(path string, contract common.Address, prvKey *ecdsa.PrivateKey, n
NetworkId: networkId,
}
data, err = ioutil.ReadFile(confpath)
if networkId == 0 {
self.NetworkId = network.NetworkId
}
if err != nil {
if !os.IsNotExist(err) {
return
}
// file does not exist
// write out config file
err = self.Save()
@ -97,6 +103,7 @@ func NewConfig(path string, contract common.Address, prvKey *ecdsa.PrivateKey, n
}
return
}
// file exists, deserialise
err = json.Unmarshal(data, self)
if err != nil {
@ -109,6 +116,11 @@ func NewConfig(path string, contract common.Address, prvKey *ecdsa.PrivateKey, n
if keyhex != self.BzzKey {
return nil, fmt.Errorf("bzz key does not match the one in the config file %v != %v", keyhex, self.BzzKey)
}
if networkId != 0 {
self.NetworkId = networkId
}
self.Swap.SetKey(prvKey)
if (self.EnsRoot == common.Address{}) {