diff options
author | holisticode <holistic.computing@gmail.com> | 2017-12-12 05:56:06 +0800 |
---|---|---|
committer | Felix Lange <fjl@users.noreply.github.com> | 2017-12-12 05:56:06 +0800 |
commit | 32516c768ec09e2a71cab5983d2c8b8ae5d92fc7 (patch) | |
tree | 9f02126fd6f219163776ad1ab8a8b924a32811a4 /swarm/api/config.go | |
parent | 1a32bdf92cceb7a42e5636e12d95609e17b8f786 (diff) | |
download | dexon-32516c768ec09e2a71cab5983d2c8b8ae5d92fc7.tar dexon-32516c768ec09e2a71cab5983d2c8b8ae5d92fc7.tar.gz dexon-32516c768ec09e2a71cab5983d2c8b8ae5d92fc7.tar.bz2 dexon-32516c768ec09e2a71cab5983d2c8b8ae5d92fc7.tar.lz dexon-32516c768ec09e2a71cab5983d2c8b8ae5d92fc7.tar.xz dexon-32516c768ec09e2a71cab5983d2c8b8ae5d92fc7.tar.zst dexon-32516c768ec09e2a71cab5983d2c8b8ae5d92fc7.zip |
cmd/swarm: add config file (#15548)
This commit adds a TOML configuration option to swarm. It reuses
the TOML configuration structure used in geth with swarm
customized items.
The commit:
* Adds a "dumpconfig" command to the swarm executable which
allows printing the (default) configuration to stdout, which
then can be redirected to a file in order to customize it.
* Adds a "--config <file>" option to the swarm executable which will
allow to load a configuration file in TOML format from the
specified location in order to initialize the Swarm node The
override priorities are like follows: environment variables
override command line arguments override config file override
default config.
Diffstat (limited to 'swarm/api/config.go')
-rw-r--r-- | swarm/api/config.go | 131 |
1 files changed, 49 insertions, 82 deletions
diff --git a/swarm/api/config.go b/swarm/api/config.go index d8d25b1c8..140c938ae 100644 --- a/swarm/api/config.go +++ b/swarm/api/config.go @@ -18,15 +18,15 @@ package api import ( "crypto/ecdsa" - "encoding/json" "fmt" - "io/ioutil" "os" "path/filepath" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/contracts/ens" "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/log" + "github.com/ethereum/go-ethereum/node" "github.com/ethereum/go-ethereum/swarm/network" "github.com/ethereum/go-ethereum/swarm/services/swap" "github.com/ethereum/go-ethereum/swarm/storage" @@ -46,101 +46,68 @@ type Config struct { *network.HiveParams Swap *swap.SwapParams *network.SyncParams - Path string - ListenAddr string - Port string - PublicKey string - BzzKey string - EnsRoot common.Address - NetworkId uint64 + Contract common.Address + EnsRoot common.Address + EnsApi string + Path string + ListenAddr string + Port string + PublicKey string + BzzKey string + NetworkId uint64 + SwapEnabled bool + SyncEnabled bool + SwapApi string + Cors string + BzzAccount string + BootNodes string } -// config is agnostic to where private key is coming from -// so managing accounts is outside swarm and left to wrappers -func NewConfig(path string, contract common.Address, prvKey *ecdsa.PrivateKey, networkId uint64) (self *Config, err error) { - address := crypto.PubkeyToAddress(prvKey.PublicKey) // default beneficiary address - dirpath := filepath.Join(path, "bzz-"+common.Bytes2Hex(address.Bytes())) - err = os.MkdirAll(dirpath, os.ModePerm) - if err != nil { - return - } - confpath := filepath.Join(dirpath, "config.json") - var data []byte - pubkey := crypto.FromECDSAPub(&prvKey.PublicKey) - pubkeyhex := common.ToHex(pubkey) - keyhex := crypto.Keccak256Hash(pubkey).Hex() +//create a default config with all parameters to set to defaults +func NewDefaultConfig() (self *Config) { self = &Config{ - SyncParams: network.NewSyncParams(dirpath), - HiveParams: network.NewHiveParams(dirpath), + StoreParams: storage.NewDefaultStoreParams(), ChunkerParams: storage.NewChunkerParams(), - StoreParams: storage.NewStoreParams(dirpath), + HiveParams: network.NewDefaultHiveParams(), + SyncParams: network.NewDefaultSyncParams(), + Swap: swap.NewDefaultSwapParams(), ListenAddr: DefaultHTTPListenAddr, Port: DefaultHTTPPort, - Path: dirpath, - Swap: swap.DefaultSwapParams(contract, prvKey), - PublicKey: pubkeyhex, - BzzKey: keyhex, + Path: node.DefaultDataDir(), + EnsApi: node.DefaultIPCEndpoint("geth"), EnsRoot: ens.TestNetAddress, - NetworkId: networkId, - } - data, err = ioutil.ReadFile(confpath) - - // if not set in function param, then set default for swarm network, will be overwritten by config file if present - if networkId == 0 { - self.NetworkId = network.NetworkId + NetworkId: network.NetworkId, + SwapEnabled: false, + SyncEnabled: true, + SwapApi: "", + BootNodes: "", } - if err != nil { - if !os.IsNotExist(err) { - return - } + return +} - // file does not exist - // write out config file - err = self.Save() - if err != nil { - err = fmt.Errorf("error writing config: %v", err) - } - return - } +//some config params need to be initialized after the complete +//config building phase is completed (e.g. due to overriding flags) +func (self *Config) Init(prvKey *ecdsa.PrivateKey) { - // file exists, deserialise - err = json.Unmarshal(data, self) + address := crypto.PubkeyToAddress(prvKey.PublicKey) + self.Path = filepath.Join(self.Path, "bzz-"+common.Bytes2Hex(address.Bytes())) + err := os.MkdirAll(self.Path, os.ModePerm) if err != nil { - return nil, fmt.Errorf("unable to parse config: %v", err) - } - // check public key - if pubkeyhex != self.PublicKey { - return nil, fmt.Errorf("public key does not match the one in the config file %v != %v", pubkeyhex, self.PublicKey) - } - 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 set in function param, replace id set from config file - if networkId != 0 { - self.NetworkId = networkId + log.Error(fmt.Sprintf("Error creating root swarm data directory: %v", err)) + return } - self.Swap.SetKey(prvKey) - - if (self.EnsRoot == common.Address{}) { - self.EnsRoot = ens.TestNetAddress - } + pubkey := crypto.FromECDSAPub(&prvKey.PublicKey) + pubkeyhex := common.ToHex(pubkey) + keyhex := crypto.Keccak256Hash(pubkey).Hex() - return -} + self.PublicKey = pubkeyhex + self.BzzKey = keyhex -func (self *Config) Save() error { - data, err := json.MarshalIndent(self, "", " ") - if err != nil { - return err - } - err = os.MkdirAll(self.Path, os.ModePerm) - if err != nil { - return err - } - confpath := filepath.Join(self.Path, "config.json") - return ioutil.WriteFile(confpath, data, os.ModePerm) + self.Swap.Init(self.Contract, prvKey) + self.SyncParams.Init(self.Path) + self.HiveParams.Init(self.Path) + self.StoreParams.Init(self.Path) } |