diff options
author | Janos Guljas <janos@resenje.org> | 2017-12-13 17:23:11 +0800 |
---|---|---|
committer | Janos Guljas <janos@resenje.org> | 2017-12-13 17:40:39 +0800 |
commit | 19982f946735948478b6b7e7706f1b615f171d0d (patch) | |
tree | cbacbdb6f9e6e731c2ebc17bad74e875f4d8ea8b /cmd/swarm | |
parent | 1dc19de5da64962a98a37bbc7b93a3895d2eb6e6 (diff) | |
parent | 32516c768ec09e2a71cab5983d2c8b8ae5d92fc7 (diff) | |
download | go-tangerine-19982f946735948478b6b7e7706f1b615f171d0d.tar go-tangerine-19982f946735948478b6b7e7706f1b615f171d0d.tar.gz go-tangerine-19982f946735948478b6b7e7706f1b615f171d0d.tar.bz2 go-tangerine-19982f946735948478b6b7e7706f1b615f171d0d.tar.lz go-tangerine-19982f946735948478b6b7e7706f1b615f171d0d.tar.xz go-tangerine-19982f946735948478b6b7e7706f1b615f171d0d.tar.zst go-tangerine-19982f946735948478b6b7e7706f1b615f171d0d.zip |
swarm, cmd/swarm: Merge branch 'master' into multiple-ens-endpoints
Merge with changes that implement config file PR #15548.
Field *EnsApi string* in swarm/api.Config is replaced with
*EnsAPIs []string*.
A new field *EnsDisabled bool* is added to swarm/api.Config for
easy way to disable ENS resolving with config file.
Signature of function swarm.NewSwarm is changed and simplified.
Diffstat (limited to 'cmd/swarm')
-rw-r--r-- | cmd/swarm/config.go | 342 | ||||
-rw-r--r-- | cmd/swarm/config_test.go | 459 | ||||
-rw-r--r-- | cmd/swarm/main.go | 229 | ||||
-rw-r--r-- | cmd/swarm/main_test.go | 141 | ||||
-rw-r--r-- | cmd/swarm/manifest.go | 8 | ||||
-rw-r--r-- | cmd/swarm/run_test.go | 17 |
6 files changed, 896 insertions, 300 deletions
diff --git a/cmd/swarm/config.go b/cmd/swarm/config.go new file mode 100644 index 000000000..05a020dc5 --- /dev/null +++ b/cmd/swarm/config.go @@ -0,0 +1,342 @@ +// Copyright 2017 The go-ethereum Authors +// This file is part of go-ethereum. +// +// go-ethereum is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// go-ethereum is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. + +package main + +import ( + "errors" + "fmt" + "io" + "os" + "reflect" + "strconv" + "strings" + "unicode" + + cli "gopkg.in/urfave/cli.v1" + + "github.com/ethereum/go-ethereum/cmd/utils" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/log" + "github.com/ethereum/go-ethereum/node" + "github.com/naoina/toml" + + bzzapi "github.com/ethereum/go-ethereum/swarm/api" +) + +var ( + //flag definition for the dumpconfig command + DumpConfigCommand = cli.Command{ + Action: utils.MigrateFlags(dumpConfig), + Name: "dumpconfig", + Usage: "Show configuration values", + ArgsUsage: "", + Flags: app.Flags, + Category: "MISCELLANEOUS COMMANDS", + Description: `The dumpconfig command shows configuration values.`, + } + + //flag definition for the config file command + SwarmTomlConfigPathFlag = cli.StringFlag{ + Name: "config", + Usage: "TOML configuration file", + } +) + +//constants for environment variables +const ( + SWARM_ENV_CHEQUEBOOK_ADDR = "SWARM_CHEQUEBOOK_ADDR" + SWARM_ENV_ACCOUNT = "SWARM_ACCOUNT" + SWARM_ENV_LISTEN_ADDR = "SWARM_LISTEN_ADDR" + SWARM_ENV_PORT = "SWARM_PORT" + SWARM_ENV_NETWORK_ID = "SWARM_NETWORK_ID" + SWARM_ENV_SWAP_ENABLE = "SWARM_SWAP_ENABLE" + SWARM_ENV_SWAP_API = "SWARM_SWAP_API" + SWARM_ENV_SYNC_ENABLE = "SWARM_SYNC_ENABLE" + SWARM_ENV_ENS_API = "SWARM_ENS_API" + SWARM_ENV_ENS_ADDR = "SWARM_ENS_ADDR" + SWARM_ENV_CORS = "SWARM_CORS" + SWARM_ENV_BOOTNODES = "SWARM_BOOTNODES" + GETH_ENV_DATADIR = "GETH_DATADIR" +) + +// These settings ensure that TOML keys use the same names as Go struct fields. +var tomlSettings = toml.Config{ + NormFieldName: func(rt reflect.Type, key string) string { + return key + }, + FieldToKey: func(rt reflect.Type, field string) string { + return field + }, + MissingField: func(rt reflect.Type, field string) error { + link := "" + if unicode.IsUpper(rune(rt.Name()[0])) && rt.PkgPath() != "main" { + link = fmt.Sprintf(", check github.com/ethereum/go-ethereum/swarm/api/config.go for available fields") + } + return fmt.Errorf("field '%s' is not defined in %s%s", field, rt.String(), link) + }, +} + +//before booting the swarm node, build the configuration +func buildConfig(ctx *cli.Context) (config *bzzapi.Config, err error) { + //check for deprecated flags + checkDeprecated(ctx) + //start by creating a default config + config = bzzapi.NewDefaultConfig() + //first load settings from config file (if provided) + config, err = configFileOverride(config, ctx) + //override settings provided by environment variables + config = envVarsOverride(config) + //override settings provided by command line + config = cmdLineOverride(config, ctx) + + return +} + +//finally, after the configuration build phase is finished, initialize +func initSwarmNode(config *bzzapi.Config, stack *node.Node, ctx *cli.Context) { + //at this point, all vars should be set in the Config + //get the account for the provided swarm account + prvkey := getAccount(config.BzzAccount, ctx, stack) + //set the resolved config path (geth --datadir) + config.Path = stack.InstanceDir() + //finally, initialize the configuration + config.Init(prvkey) + //configuration phase completed here + log.Debug("Starting Swarm with the following parameters:") + //after having created the config, print it to screen + log.Debug(printConfig(config)) +} + +//override the current config with whatever is in the config file, if a config file has been provided +func configFileOverride(config *bzzapi.Config, ctx *cli.Context) (*bzzapi.Config, error) { + var err error + + //only do something if the -config flag has been set + if ctx.GlobalIsSet(SwarmTomlConfigPathFlag.Name) { + var filepath string + if filepath = ctx.GlobalString(SwarmTomlConfigPathFlag.Name); filepath == "" { + utils.Fatalf("Config file flag provided with invalid file path") + } + f, err := os.Open(filepath) + if err != nil { + return nil, err + } + defer f.Close() + + //decode the TOML file into a Config struct + //note that we are decoding into the existing defaultConfig; + //if an entry is not present in the file, the default entry is kept + err = tomlSettings.NewDecoder(f).Decode(&config) + // Add file name to errors that have a line number. + if _, ok := err.(*toml.LineError); ok { + err = errors.New(filepath + ", " + err.Error()) + } + } + return config, err +} + +//override the current config with whatever is provided through the command line +//most values are not allowed a zero value (empty string), if not otherwise noted +func cmdLineOverride(currentConfig *bzzapi.Config, ctx *cli.Context) *bzzapi.Config { + + if keyid := ctx.GlobalString(SwarmAccountFlag.Name); keyid != "" { + currentConfig.BzzAccount = keyid + } + + if chbookaddr := ctx.GlobalString(ChequebookAddrFlag.Name); chbookaddr != "" { + currentConfig.Contract = common.HexToAddress(chbookaddr) + } + + if networkid := ctx.GlobalString(SwarmNetworkIdFlag.Name); networkid != "" { + if id, _ := strconv.Atoi(networkid); id != 0 { + currentConfig.NetworkId = uint64(id) + } + } + + if ctx.GlobalIsSet(utils.DataDirFlag.Name) { + if datadir := ctx.GlobalString(utils.DataDirFlag.Name); datadir != "" { + currentConfig.Path = datadir + } + } + + bzzport := ctx.GlobalString(SwarmPortFlag.Name) + if len(bzzport) > 0 { + currentConfig.Port = bzzport + } + + if bzzaddr := ctx.GlobalString(SwarmListenAddrFlag.Name); bzzaddr != "" { + currentConfig.ListenAddr = bzzaddr + } + + if ctx.GlobalIsSet(SwarmSwapEnabledFlag.Name) { + currentConfig.SwapEnabled = true + } + + if ctx.GlobalIsSet(SwarmSyncEnabledFlag.Name) { + currentConfig.SyncEnabled = true + } + + currentConfig.SwapApi = ctx.GlobalString(SwarmSwapAPIFlag.Name) + if currentConfig.SwapEnabled && currentConfig.SwapApi == "" { + utils.Fatalf(SWARM_ERR_SWAP_SET_NO_API) + } + + //EnsAPIs can be set to "", so can't check for empty string, as it is allowed! + if ctx.GlobalIsSet(EnsAPIFlag.Name) { + ensAPIs := ctx.GlobalStringSlice(EnsAPIFlag.Name) + // Disable ENS resolver if --ens-api="" is specified + if len(ensAPIs) == 1 && ensAPIs[0] == "" { + currentConfig.EnsDisabled = true + currentConfig.EnsAPIs = nil + } else { + currentConfig.EnsDisabled = false + currentConfig.EnsAPIs = ensAPIs + } + } + + if ensaddr := ctx.GlobalString(DeprecatedEnsAddrFlag.Name); ensaddr != "" { + currentConfig.EnsRoot = common.HexToAddress(ensaddr) + } + + if cors := ctx.GlobalString(CorsStringFlag.Name); cors != "" { + currentConfig.Cors = cors + } + + if ctx.GlobalIsSet(utils.BootnodesFlag.Name) { + currentConfig.BootNodes = ctx.GlobalString(utils.BootnodesFlag.Name) + } + + return currentConfig + +} + +//override the current config with whatver is provided in environment variables +//most values are not allowed a zero value (empty string), if not otherwise noted +func envVarsOverride(currentConfig *bzzapi.Config) (config *bzzapi.Config) { + + if keyid := os.Getenv(SWARM_ENV_ACCOUNT); keyid != "" { + currentConfig.BzzAccount = keyid + } + + if chbookaddr := os.Getenv(SWARM_ENV_CHEQUEBOOK_ADDR); chbookaddr != "" { + currentConfig.Contract = common.HexToAddress(chbookaddr) + } + + if networkid := os.Getenv(SWARM_ENV_NETWORK_ID); networkid != "" { + if id, _ := strconv.Atoi(networkid); id != 0 { + currentConfig.NetworkId = uint64(id) + } + } + + if datadir := os.Getenv(GETH_ENV_DATADIR); datadir != "" { + currentConfig.Path = datadir + } + + bzzport := os.Getenv(SWARM_ENV_PORT) + if len(bzzport) > 0 { + currentConfig.Port = bzzport + } + + if bzzaddr := os.Getenv(SWARM_ENV_LISTEN_ADDR); bzzaddr != "" { + currentConfig.ListenAddr = bzzaddr + } + + if swapenable := os.Getenv(SWARM_ENV_SWAP_ENABLE); swapenable != "" { + if swap, err := strconv.ParseBool(swapenable); err != nil { + currentConfig.SwapEnabled = swap + } + } + + if syncenable := os.Getenv(SWARM_ENV_SYNC_ENABLE); syncenable != "" { + if sync, err := strconv.ParseBool(syncenable); err != nil { + currentConfig.SyncEnabled = sync + } + } + + if swapapi := os.Getenv(SWARM_ENV_SWAP_API); swapapi != "" { + currentConfig.SwapApi = swapapi + } + + if currentConfig.SwapEnabled && currentConfig.SwapApi == "" { + utils.Fatalf(SWARM_ERR_SWAP_SET_NO_API) + } + + //EnsAPIs can be set to "", so can't check for empty string, as it is allowed + if ensapi, exists := os.LookupEnv(SWARM_ENV_ENS_API); exists == true { + ensAPIs := strings.Split(ensapi, ",") + // Disable ENS resolver if SWARM_ENS_API="" is specified + if len(ensAPIs) == 0 { + currentConfig.EnsDisabled = true + currentConfig.EnsAPIs = nil + } else { + currentConfig.EnsDisabled = false + currentConfig.EnsAPIs = ensAPIs + } + } + + if ensaddr := os.Getenv(SWARM_ENV_ENS_ADDR); ensaddr != "" { + currentConfig.EnsRoot = common.HexToAddress(ensaddr) + } + + if cors := os.Getenv(SWARM_ENV_CORS); cors != "" { + currentConfig.Cors = cors + } + + if bootnodes := os.Getenv(SWARM_ENV_BOOTNODES); bootnodes != "" { + currentConfig.BootNodes = bootnodes + } + + return currentConfig +} + +// dumpConfig is the dumpconfig command. +// writes a default config to STDOUT +func dumpConfig(ctx *cli.Context) error { + cfg, err := buildConfig(ctx) + if err != nil { + utils.Fatalf(fmt.Sprintf("Uh oh - dumpconfig triggered an error %v", err)) + } + comment := "" + out, err := tomlSettings.Marshal(&cfg) + if err != nil { + return err + } + io.WriteString(os.Stdout, comment) + os.Stdout.Write(out) + return nil +} + +//deprecated flags checked here +func checkDeprecated(ctx *cli.Context) { + // exit if the deprecated --ethapi flag is set + if ctx.GlobalString(DeprecatedEthAPIFlag.Name) != "" { + utils.Fatalf("--ethapi is no longer a valid command line flag, please use --ens-api and/or --swap-api.") + } + // warn if --ens-api flag is set + if ctx.GlobalString(DeprecatedEnsAddrFlag.Name) != "" { + log.Warn("--ens-addr is no longer a valid command line flag, please use --ens-api to specify contract address.") + } +} + +//print a Config as string +func printConfig(config *bzzapi.Config) string { + out, err := tomlSettings.Marshal(&config) + if err != nil { + return (fmt.Sprintf("Something is not right with the configuration: %v", err)) + } + return string(out) +} diff --git a/cmd/swarm/config_test.go b/cmd/swarm/config_test.go new file mode 100644 index 000000000..7ffe2cfb9 --- /dev/null +++ b/cmd/swarm/config_test.go @@ -0,0 +1,459 @@ +// Copyright 2017 The go-ethereum Authors +// This file is part of go-ethereum. +// +// go-ethereum is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// go-ethereum is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. + +package main + +import ( + "fmt" + "io" + "io/ioutil" + "os" + "os/exec" + "testing" + "time" + + "github.com/ethereum/go-ethereum/rpc" + "github.com/ethereum/go-ethereum/swarm" + "github.com/ethereum/go-ethereum/swarm/api" + + "github.com/docker/docker/pkg/reexec" +) + +func TestDumpConfig(t *testing.T) { + swarm := runSwarm(t, "dumpconfig") + defaultConf := api.NewDefaultConfig() + out, err := tomlSettings.Marshal(&defaultConf) + if err != nil { + t.Fatal(err) + } + swarm.Expect(string(out)) + swarm.ExpectExit() +} + +func TestFailsSwapEnabledNoSwapApi(t *testing.T) { + flags := []string{ + fmt.Sprintf("--%s", SwarmNetworkIdFlag.Name), "42", + fmt.Sprintf("--%s", SwarmPortFlag.Name), "54545", + fmt.Sprintf("--%s", SwarmSwapEnabledFlag.Name), + } + + swarm := runSwarm(t, flags...) + swarm.Expect("Fatal: " + SWARM_ERR_SWAP_SET_NO_API + "\n") + swarm.ExpectExit() +} + +func TestFailsNoBzzAccount(t *testing.T) { + flags := []string{ + fmt.Sprintf("--%s", SwarmNetworkIdFlag.Name), "42", + fmt.Sprintf("--%s", SwarmPortFlag.Name), "54545", + } + + swarm := runSwarm(t, flags...) + swarm.Expect("Fatal: " + SWARM_ERR_NO_BZZACCOUNT + "\n") + swarm.ExpectExit() +} + +func TestCmdLineOverrides(t *testing.T) { + dir, err := ioutil.TempDir("", "bzztest") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(dir) + + conf, account := getTestAccount(t, dir) + node := &testNode{Dir: dir} + + // assign ports + httpPort, err := assignTCPPort() + if err != nil { + t.Fatal(err) + } + + flags := []string{ + fmt.Sprintf("--%s", SwarmNetworkIdFlag.Name), "42", + fmt.Sprintf("--%s", SwarmPortFlag.Name), httpPort, + fmt.Sprintf("--%s", SwarmSyncEnabledFlag.Name), + fmt.Sprintf("--%s", CorsStringFlag.Name), "*", + fmt.Sprintf("--%s", SwarmAccountFlag.Name), account.Address.String(), + fmt.Sprintf("--%s", EnsAPIFlag.Name), "", + "--datadir", dir, + "--ipcpath", conf.IPCPath, + } + node.Cmd = runSwarm(t, flags...) + node.Cmd.InputLine(testPassphrase) + defer func() { + if t.Failed() { + node.Shutdown() + } + }() + // wait for the node to start + for start := time.Now(); time.Since(start) < 10*time.Second; time.Sleep(50 * time.Millisecond) { + node.Client, err = rpc.Dial(conf.IPCEndpoint()) + if err == nil { + break + } + } + if node.Client == nil { + t.Fatal(err) + } + + // load info + var info swarm.Info + if err := node.Client.Call(&info, "bzz_info"); err != nil { + t.Fatal(err) + } + + if info.Port != httpPort { + t.Fatalf("Expected port to be %s, got %s", httpPort, info.Port) + } + + if info.NetworkId != 42 { + t.Fatalf("Expected network ID to be %d, got %d", 42, info.NetworkId) + } + + if info.SyncEnabled != true { + t.Fatal("Expected Sync to be enabled, but is false") + } + + if info.Cors != "*" { + t.Fatalf("Expected Cors flag to be set to %s, got %s", "*", info.Cors) + } + + node.Shutdown() +} + +func TestFileOverrides(t *testing.T) { + + // assign ports + httpPort, err := assignTCPPort() + if err != nil { + t.Fatal(err) + } + + //create a config file + //first, create a default conf + defaultConf := api.NewDefaultConfig() + //change some values in order to test if they have been loaded + defaultConf.SyncEnabled = true + defaultConf.NetworkId = 54 + defaultConf.Port = httpPort + defaultConf.StoreParams.DbCapacity = 9000000 + defaultConf.ChunkerParams.Branches = 64 + defaultConf.HiveParams.CallInterval = 6000000000 + defaultConf.Swap.Params.Strategy.AutoCashInterval = 600 * time.Second + defaultConf.SyncParams.KeyBufferSize = 512 + //create a TOML string + out, err := tomlSettings.Marshal(&defaultConf) + if err != nil { + t.Fatalf("Error creating TOML file in TestFileOverride: %v", err) + } + //create file + f, err := ioutil.TempFile("", "testconfig.toml") + if err != nil { + t.Fatalf("Error writing TOML file in TestFileOverride: %v", err) + } + //write file + _, err = f.WriteString(string(out)) + if err != nil { + t.Fatalf("Error writing TOML file in TestFileOverride: %v", err) + } + f.Sync() + + dir, err := ioutil.TempDir("", "bzztest") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(dir) + conf, account := getTestAccount(t, dir) + node := &testNode{Dir: dir} + + flags := []string{ + fmt.Sprintf("--%s", SwarmTomlConfigPathFlag.Name), f.Name(), + fmt.Sprintf("--%s", SwarmAccountFlag.Name), account.Address.String(), + "--ens-api", "", + "--ipcpath", conf.IPCPath, + "--datadir", dir, + } + node.Cmd = runSwarm(t, flags...) + node.Cmd.InputLine(testPassphrase) + defer func() { + if t.Failed() { + node.Shutdown() + } + }() + // wait for the node to start + for start := time.Now(); time.Since(start) < 10*time.Second; time.Sleep(50 * time.Millisecond) { + node.Client, err = rpc.Dial(conf.IPCEndpoint()) + if err == nil { + break + } + } + if node.Client == nil { + t.Fatal(err) + } + + // load info + var info swarm.Info + if err := node.Client.Call(&info, "bzz_info"); err != nil { + t.Fatal(err) + } + + if info.Port != httpPort { + t.Fatalf("Expected port to be %s, got %s", httpPort, info.Port) + } + + if info.NetworkId != 54 { + t.Fatalf("Expected network ID to be %d, got %d", 54, info.NetworkId) + } + + if info.SyncEnabled != true { + t.Fatal("Expected Sync to be enabled, but is false") + } + + if info.StoreParams.DbCapacity != 9000000 { + t.Fatalf("Expected network ID to be %d, got %d", 54, info.NetworkId) + } + + if info.ChunkerParams.Branches != 64 { + t.Fatalf("Expected chunker params branches to be %d, got %d", 64, info.ChunkerParams.Branches) + } + + if info.HiveParams.CallInterval != 6000000000 { + t.Fatalf("Expected HiveParams CallInterval to be %d, got %d", uint64(6000000000), uint64(info.HiveParams.CallInterval)) + } + + if info.Swap.Params.Strategy.AutoCashInterval != 600*time.Second { + t.Fatalf("Expected SwapParams AutoCashInterval to be %ds, got %d", 600, info.Swap.Params.Strategy.AutoCashInterval) + } + + if info.SyncParams.KeyBufferSize != 512 { + t.Fatalf("Expected info.SyncParams.KeyBufferSize to be %d, got %d", 512, info.SyncParams.KeyBufferSize) + } + + node.Shutdown() +} + +func TestEnvVars(t *testing.T) { + // assign ports + httpPort, err := assignTCPPort() + if err != nil { + t.Fatal(err) + } + + envVars := os.Environ() + envVars = append(envVars, fmt.Sprintf("%s=%s", SwarmPortFlag.EnvVar, httpPort)) + envVars = append(envVars, fmt.Sprintf("%s=%s", SwarmNetworkIdFlag.EnvVar, "999")) + envVars = append(envVars, fmt.Sprintf("%s=%s", CorsStringFlag.EnvVar, "*")) + envVars = append(envVars, fmt.Sprintf("%s=%s", SwarmSyncEnabledFlag.EnvVar, "true")) + + dir, err := ioutil.TempDir("", "bzztest") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(dir) + conf, account := getTestAccount(t, dir) + node := &testNode{Dir: dir} + flags := []string{ + fmt.Sprintf("--%s", SwarmAccountFlag.Name), account.Address.String(), + "--ens-api", "", + "--datadir", dir, + "--ipcpath", conf.IPCPath, + } + + //node.Cmd = runSwarm(t,flags...) + //node.Cmd.cmd.Env = envVars + //the above assignment does not work, so we need a custom Cmd here in order to pass envVars: + cmd := &exec.Cmd{ + Path: reexec.Self(), + Args: append([]string{"swarm-test"}, flags...), + Stderr: os.Stderr, + Stdout: os.Stdout, + } + cmd.Env = envVars + //stdout, err := cmd.StdoutPipe() + //if err != nil { + // t.Fatal(err) + //} + //stdout = bufio.NewReader(stdout) + var stdin io.WriteCloser + if stdin, err = cmd.StdinPipe(); err != nil { + t.Fatal(err) + } + if err := cmd.Start(); err != nil { + t.Fatal(err) + } + + //cmd.InputLine(testPassphrase) + io.WriteString(stdin, testPassphrase+"\n") + defer func() { + if t.Failed() { + node.Shutdown() + cmd.Process.Kill() + } + }() + // wait for the node to start + for start := time.Now(); time.Since(start) < 10*time.Second; time.Sleep(50 * time.Millisecond) { + node.Client, err = rpc.Dial(conf.IPCEndpoint()) + if err == nil { + break + } + } + + if node.Client == nil { + t.Fatal(err) + } + + // load info + var info swarm.Info + if err := node.Client.Call(&info, "bzz_info"); err != nil { + t.Fatal(err) + } + + if info.Port != httpPort { + t.Fatalf("Expected port to be %s, got %s", httpPort, info.Port) + } + + if info.NetworkId != 999 { + t.Fatalf("Expected network ID to be %d, got %d", 999, info.NetworkId) + } + + if info.Cors != "*" { + t.Fatalf("Expected Cors flag to be set to %s, got %s", "*", info.Cors) + } + + if info.SyncEnabled != true { + t.Fatal("Expected Sync to be enabled, but is false") + } + + node.Shutdown() + cmd.Process.Kill() +} + +func TestCmdLineOverridesFile(t *testing.T) { + + // assign ports + httpPort, err := assignTCPPort() + if err != nil { + t.Fatal(err) + } + + //create a config file + //first, create a default conf + defaultConf := api.NewDefaultConfig() + //change some values in order to test if they have been loaded + defaultConf.SyncEnabled = false + defaultConf.NetworkId = 54 + defaultConf.Port = "8588" + defaultConf.StoreParams.DbCapacity = 9000000 + defaultConf.ChunkerParams.Branches = 64 + defaultConf.HiveParams.CallInterval = 6000000000 + defaultConf.Swap.Params.Strategy.AutoCashInterval = 600 * time.Second + defaultConf.SyncParams.KeyBufferSize = 512 + //create a TOML file + out, err := tomlSettings.Marshal(&defaultConf) + if err != nil { + t.Fatalf("Error creating TOML file in TestFileOverride: %v", err) + } + //write file + f, err := ioutil.TempFile("", "testconfig.toml") + if err != nil { + t.Fatalf("Error writing TOML file in TestFileOverride: %v", err) + } + //write file + _, err = f.WriteString(string(out)) + if err != nil { + t.Fatalf("Error writing TOML file in TestFileOverride: %v", err) + } + f.Sync() + + dir, err := ioutil.TempDir("", "bzztest") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(dir) + conf, account := getTestAccount(t, dir) + node := &testNode{Dir: dir} + + expectNetworkId := uint64(77) + + flags := []string{ + fmt.Sprintf("--%s", SwarmNetworkIdFlag.Name), "77", + fmt.Sprintf("--%s", SwarmPortFlag.Name), httpPort, + fmt.Sprintf("--%s", SwarmSyncEnabledFlag.Name), + fmt.Sprintf("--%s", SwarmTomlConfigPathFlag.Name), f.Name(), + fmt.Sprintf("--%s", SwarmAccountFlag.Name), account.Address.String(), + "--ens-api", "", + "--datadir", dir, + "--ipcpath", conf.IPCPath, + } + node.Cmd = runSwarm(t, flags...) + node.Cmd.InputLine(testPassphrase) + defer func() { + if t.Failed() { + node.Shutdown() + } + }() + // wait for the node to start + for start := time.Now(); time.Since(start) < 10*time.Second; time.Sleep(50 * time.Millisecond) { + node.Client, err = rpc.Dial(conf.IPCEndpoint()) + if err == nil { + break + } + } + if node.Client == nil { + t.Fatal(err) + } + + // load info + var info swarm.Info + if err := node.Client.Call(&info, "bzz_info"); err != nil { + t.Fatal(err) + } + + if info.Port != httpPort { + t.Fatalf("Expected port to be %s, got %s", httpPort, info.Port) + } + + if info.NetworkId != expectNetworkId { + t.Fatalf("Expected network ID to be %d, got %d", expectNetworkId, info.NetworkId) + } + + if info.SyncEnabled != true { + t.Fatal("Expected Sync to be enabled, but is false") + } + + if info.StoreParams.DbCapacity != 9000000 { + t.Fatalf("Expected network ID to be %d, got %d", 54, info.NetworkId) + } + + if info.ChunkerParams.Branches != 64 { + t.Fatalf("Expected chunker params branches to be %d, got %d", 64, info.ChunkerParams.Branches) + } + + if info.HiveParams.CallInterval != 6000000000 { + t.Fatalf("Expected HiveParams CallInterval to be %d, got %d", uint64(6000000000), uint64(info.HiveParams.CallInterval)) + } + + if info.Swap.Params.Strategy.AutoCashInterval != 600*time.Second { + t.Fatalf("Expected SwapParams AutoCashInterval to be %ds, got %d", 600, info.Swap.Params.Strategy.AutoCashInterval) + } + + if info.SyncParams.KeyBufferSize != 512 { + t.Fatalf("Expected info.SyncParams.KeyBufferSize to be %d, got %d", 512, info.SyncParams.KeyBufferSize) + } + + node.Shutdown() +} diff --git a/cmd/swarm/main.go b/cmd/swarm/main.go index 48cf032a2..4b0823796 100644 --- a/cmd/swarm/main.go +++ b/cmd/swarm/main.go @@ -27,7 +27,6 @@ import ( "strconv" "strings" "syscall" - "unicode" "github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/accounts/keystore" @@ -44,6 +43,7 @@ import ( "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/swarm" bzzapi "github.com/ethereum/go-ethereum/swarm/api" + "gopkg.in/urfave/cli.v1" ) @@ -62,44 +62,53 @@ var ( var ( ChequebookAddrFlag = cli.StringFlag{ - Name: "chequebook", - Usage: "chequebook contract address", + Name: "chequebook", + Usage: "chequebook contract address", + EnvVar: SWARM_ENV_CHEQUEBOOK_ADDR, } SwarmAccountFlag = cli.StringFlag{ - Name: "bzzaccount", - Usage: "Swarm account key file", + Name: "bzzaccount", + Usage: "Swarm account key file", + EnvVar: SWARM_ENV_ACCOUNT, } SwarmListenAddrFlag = cli.StringFlag{ - Name: "httpaddr", - Usage: "Swarm HTTP API listening interface", + Name: "httpaddr", + Usage: "Swarm HTTP API listening interface", + EnvVar: SWARM_ENV_LISTEN_ADDR, } SwarmPortFlag = cli.StringFlag{ - Name: "bzzport", - Usage: "Swarm local http api port", + Name: "bzzport", + Usage: "Swarm local http api port", + EnvVar: SWARM_ENV_PORT, } SwarmNetworkIdFlag = cli.IntFlag{ - Name: "bzznetworkid", - Usage: "Network identifier (integer, default 3=swarm testnet)", + Name: "bzznetworkid", + Usage: "Network identifier (integer, default 3=swarm testnet)", + EnvVar: SWARM_ENV_NETWORK_ID, } SwarmConfigPathFlag = cli.StringFlag{ Name: "bzzconfig", - Usage: "Swarm config file path (datadir/bzz)", + Usage: "DEPRECATED: please use --config path/to/TOML-file", } SwarmSwapEnabledFlag = cli.BoolFlag{ - Name: "swap", - Usage: "Swarm SWAP enabled (default false)", + Name: "swap", + Usage: "Swarm SWAP enabled (default false)", + EnvVar: SWARM_ENV_SWAP_ENABLE, } SwarmSwapAPIFlag = cli.StringFlag{ - Name: "swap-api", - Usage: "URL of the Ethereum API provider to use to settle SWAP payments", + Name: "swap-api", + Usage: "URL of the Ethereum API provider to use to settle SWAP payments", + EnvVar: SWARM_ENV_SWAP_API, } SwarmSyncEnabledFlag = cli.BoolTFlag{ - Name: "sync", - Usage: "Swarm Syncing enabled (default true)", + Name: "sync", + Usage: "Swarm Syncing enabled (default true)", + EnvVar: SWARM_ENV_SYNC_ENABLE, } EnsAPIFlag = cli.StringSliceFlag{ - Name: "ens-api", - Usage: "ENS API endpoint for a TLD and with contract address, can be repeated, format [tld:][contract-addr@]url", + Name: "ens-api", + Usage: "ENS API endpoint for a TLD and with contract address, can be repeated, format [tld:][contract-addr@]url", + EnvVar: SWARM_ENV_ENS_API, } SwarmApiFlag = cli.StringFlag{ Name: "bzzapi", @@ -127,8 +136,9 @@ var ( Usage: "force mime type", } CorsStringFlag = cli.StringFlag{ - Name: "corsdomain", - Usage: "Domain on which to send Access-Control-Allow-Origin header (multiple domains can be supplied separated by a ',')", + Name: "corsdomain", + Usage: "Domain on which to send Access-Control-Allow-Origin header (multiple domains can be supplied separated by a ',')", + EnvVar: SWARM_ENV_CORS, } // the following flags are deprecated and should be removed in the future @@ -142,6 +152,12 @@ var ( } ) +//declare a few constant error messages, useful for later error check comparisons in test +var ( + SWARM_ERR_NO_BZZACCOUNT = "bzzaccount option is required but not set; check your config file, command line or environment variables" + SWARM_ERR_SWAP_SET_NO_API = "SWAP is enabled but --swap-api is not set" +) + var defaultNodeConfig = node.DefaultConfig // This init function sets defaults so cmd/swarm can run alongside geth. @@ -297,6 +313,8 @@ Remove corrupt entries from a local chunk database. DEPRECATED: use 'swarm db clean'. `, }, + // See config.go + DumpConfigCommand, } sort.Sort(cli.CommandsByName(app.Commands)) @@ -319,6 +337,7 @@ DEPRECATED: use 'swarm db clean'. // bzzd-specific flags CorsStringFlag, EnsAPIFlag, + SwarmTomlConfigPathFlag, SwarmConfigPathFlag, SwarmSwapEnabledFlag, SwarmSwapAPIFlag, @@ -372,19 +391,32 @@ func version(ctx *cli.Context) error { } func bzzd(ctx *cli.Context) error { - // exit if the deprecated --ethapi flag is set - if ctx.GlobalString(DeprecatedEthAPIFlag.Name) != "" { - utils.Fatalf("--ethapi is no longer a valid command line flag, please use --ens-api and/or --swap-api.") + //build a valid bzzapi.Config from all available sources: + //default config, file config, command line and env vars + bzzconfig, err := buildConfig(ctx) + if err != nil { + utils.Fatalf("unable to configure swarm: %v", err) } cfg := defaultNodeConfig + //geth only supports --datadir via command line + //in order to be consistent within swarm, if we pass --datadir via environment variable + //or via config file, we get the same directory for geth and swarm + if _, err := os.Stat(bzzconfig.Path); err == nil { + cfg.DataDir = bzzconfig.Path + } + //setup the ethereum node utils.SetNodeConfig(ctx, &cfg) stack, err := node.New(&cfg) if err != nil { utils.Fatalf("can't create node: %v", err) } - - registerBzzService(ctx, stack) + //a few steps need to be done after the config phase is completed, + //due to overriding behavior + initSwarmNode(bzzconfig, stack, ctx) + //register BZZ as node.Service in the ethereum node + registerBzzService(bzzconfig, ctx, stack) + //start the node utils.StartNode(stack) go func() { @@ -396,13 +428,12 @@ func bzzd(ctx *cli.Context) error { stack.Stop() }() - networkId := ctx.GlobalUint64(SwarmNetworkIdFlag.Name) // Add bootnodes as initial peers. - if ctx.GlobalIsSet(utils.BootnodesFlag.Name) { - bootnodes := strings.Split(ctx.GlobalString(utils.BootnodesFlag.Name), ",") + if bzzconfig.BootNodes != "" { + bootnodes := strings.Split(bzzconfig.BootNodes, ",") injectBootnodes(stack.Server(), bootnodes) } else { - if networkId == 3 { + if bzzconfig.NetworkId == 3 { injectBootnodes(stack.Server(), testbetBootNodes) } } @@ -411,139 +442,35 @@ func bzzd(ctx *cli.Context) error { return nil } -func registerBzzService(ctx *cli.Context, stack *node.Node) { - prvkey := getAccount(ctx, stack) - - chbookaddr := common.HexToAddress(ctx.GlobalString(ChequebookAddrFlag.Name)) - bzzdir := ctx.GlobalString(SwarmConfigPathFlag.Name) - 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) - } - bzzport := ctx.GlobalString(SwarmPortFlag.Name) - if len(bzzport) > 0 { - bzzconfig.Port = bzzport - } - if bzzaddr := ctx.GlobalString(SwarmListenAddrFlag.Name); bzzaddr != "" { - bzzconfig.ListenAddr = bzzaddr - } - swapEnabled := ctx.GlobalBool(SwarmSwapEnabledFlag.Name) - syncEnabled := ctx.GlobalBoolT(SwarmSyncEnabledFlag.Name) - - swapapi := ctx.GlobalString(SwarmSwapAPIFlag.Name) - if swapEnabled && swapapi == "" { - utils.Fatalf("SWAP is enabled but --swap-api is not set") - } - - ensAPIs := ctx.GlobalStringSlice(EnsAPIFlag.Name) - ensAddr := ctx.GlobalString(DeprecatedEnsAddrFlag.Name) - - if ensAddr != "" { - log.Warn("--ens-addr is no longer a valid command line flag, please use --ens-api to specify contract address.") - } - - cors := ctx.GlobalString(CorsStringFlag.Name) +func registerBzzService(bzzconfig *bzzapi.Config, ctx *cli.Context, stack *node.Node) { + //define the swarm service boot function boot := func(ctx *node.ServiceContext) (node.Service, error) { var swapClient *ethclient.Client - if swapapi != "" { - log.Info("connecting to SWAP API", "url", swapapi) - swapClient, err = ethclient.Dial(swapapi) + var err error + if bzzconfig.SwapApi != "" { + log.Info("connecting to SWAP API", "url", bzzconfig.SwapApi) + swapClient, err = ethclient.Dial(bzzconfig.SwapApi) if err != nil { - return nil, fmt.Errorf("error connecting to SWAP API %s: %s", swapapi, err) - } - } - - ensClientConfigs := []swarm.ENSClientConfig{} - switch len(ensAPIs) { - case 0: - ensClientConfigs = append(ensClientConfigs, swarm.ENSClientConfig{ - Endpoint: node.DefaultIPCEndpoint("geth"), - ContractAddress: ensAddr, - }) - case 1: - // Check if "--ens-api ''" is specified in order to disable ENS. - if ensAPIs[0] == "" { - break - } - // Check if only one --ens-api is specified in order to use --ens-addr value - // to preserve the backward compatibility with single --ens-api flag. - c := parseFlagEnsAPI(ensAPIs[0]) - if ensAddr != "" { - // If contract address is specified in both cases, check for conflict. - if c.ContractAddress != "" && ensAddr != c.ContractAddress { - utils.Fatalf("--ens-addr flag in conflict with --ens-api flag contract address") - } - c.ContractAddress = ensAddr - } - ensClientConfigs = append(ensClientConfigs, c) - default: - // Backward compatibility with single --ens-api flag and --ens-addr is preserved. - // Check for case where multiple --ens-api flags are set with --ens-addr where - // the specified contract address is not clear to which api belongs. - if ensAddr != "" { - utils.Fatalf("--ens-addr flag can not be used with multiple --ens-api flags") + return nil, fmt.Errorf("error connecting to SWAP API %s: %s", bzzconfig.SwapApi, err) } - for _, s := range ensAPIs { - if s != "" { - ensClientConfigs = append(ensClientConfigs, parseFlagEnsAPI(s)) - } - } - } - if len(ensClientConfigs) == 0 { - log.Warn("No ENS, please specify non-empty --ens-api to use domain name resolution") } - return swarm.NewSwarm(ctx, swapClient, ensClientConfigs, bzzconfig, swapEnabled, syncEnabled, cors) + return swarm.NewSwarm(ctx, swapClient, bzzconfig) } + //register within the ethereum node if err := stack.Register(boot); err != nil { utils.Fatalf("Failed to register the Swarm service: %v", err) } } -// parseFlagEnsAPI parses EnsAPIFlag according to format -// [tld:][contract-addr@]url and returns ENSClientConfig structure -// with endpoint, contract address and TLD. -func parseFlagEnsAPI(s string) swarm.ENSClientConfig { - isAllLetterString := func(s string) bool { - for _, r := range s { - if !unicode.IsLetter(r) { - return false - } - } - return true - } - endpoint := s - var addr, tld string - if i := strings.Index(endpoint, ":"); i > 0 { - if isAllLetterString(endpoint[:i]) && len(endpoint) > i+2 && endpoint[i+1:i+3] != "//" { - tld = endpoint[:i] - endpoint = endpoint[i+1:] - } - } - if i := strings.Index(endpoint, "@"); i > 0 { - addr = endpoint[:i] - endpoint = endpoint[i+1:] - } - return swarm.ENSClientConfig{ - Endpoint: endpoint, - ContractAddress: addr, - TLD: tld, - } -} - -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) +func getAccount(bzzaccount string, ctx *cli.Context, stack *node.Node) *ecdsa.PrivateKey { + //an account is mandatory + if bzzaccount == "" { + utils.Fatalf(SWARM_ERR_NO_BZZACCOUNT) } // Try to load the arg as a hex key file. - if key, err := crypto.LoadECDSA(keyid); err == nil { + if key, err := crypto.LoadECDSA(bzzaccount); err == nil { log.Info("Swarm account key loaded", "address", crypto.PubkeyToAddress(key.PublicKey)) return key } @@ -551,7 +478,7 @@ func getAccount(ctx *cli.Context, stack *node.Node) *ecdsa.PrivateKey { am := stack.AccountManager() ks := am.Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore) - return decryptStoreAccount(ks, keyid, utils.MakePasswordList(ctx)) + return decryptStoreAccount(ks, bzzaccount, utils.MakePasswordList(ctx)) } func decryptStoreAccount(ks *keystore.KeyStore, account string, passwords []string) *ecdsa.PrivateKey { @@ -569,7 +496,7 @@ func decryptStoreAccount(ks *keystore.KeyStore, account string, passwords []stri utils.Fatalf("Can't find swarm account key %s", account) } if err != nil { - utils.Fatalf("Can't find swarm account key: %v", err) + utils.Fatalf("Can't find swarm account key: %v - Is the provided bzzaccount(%s) from the right datadir/Path?", err, account) } keyjson, err := ioutil.ReadFile(a.URL.Path) if err != nil { diff --git a/cmd/swarm/main_test.go b/cmd/swarm/main_test.go deleted file mode 100644 index f815f3387..000000000 --- a/cmd/swarm/main_test.go +++ /dev/null @@ -1,141 +0,0 @@ -// Copyright 2017 The go-ethereum Authors -// This file is part of go-ethereum. -// -// go-ethereum is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// go-ethereum is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. - -package main - -import ( - "testing" - - "github.com/ethereum/go-ethereum/swarm" -) - -func TestParseFlagEnsAPI(t *testing.T) { - for _, x := range []struct { - description string - value string - config swarm.ENSClientConfig - }{ - { - description: "IPC endpoint", - value: "/data/testnet/geth.ipc", - config: swarm.ENSClientConfig{ - Endpoint: "/data/testnet/geth.ipc", - }, - }, - { - description: "HTTP endpoint", - value: "http://127.0.0.1:1234", - config: swarm.ENSClientConfig{ - Endpoint: "http://127.0.0.1:1234", - }, - }, - { - description: "WS endpoint", - value: "ws://127.0.0.1:1234", - config: swarm.ENSClientConfig{ - Endpoint: "ws://127.0.0.1:1234", - }, - }, - { - description: "IPC Endpoint and TLD", - value: "test:/data/testnet/geth.ipc", - config: swarm.ENSClientConfig{ - Endpoint: "/data/testnet/geth.ipc", - TLD: "test", - }, - }, - { - description: "HTTP endpoint and TLD", - value: "test:http://127.0.0.1:1234", - config: swarm.ENSClientConfig{ - Endpoint: "http://127.0.0.1:1234", - TLD: "test", - }, - }, - { - description: "WS endpoint and TLD", - value: "test:ws://127.0.0.1:1234", - config: swarm.ENSClientConfig{ - Endpoint: "ws://127.0.0.1:1234", - TLD: "test", - }, - }, - { - description: "IPC Endpoint and contract address", - value: "314159265dD8dbb310642f98f50C066173C1259b@/data/testnet/geth.ipc", - config: swarm.ENSClientConfig{ - Endpoint: "/data/testnet/geth.ipc", - ContractAddress: "314159265dD8dbb310642f98f50C066173C1259b", - }, - }, - { - description: "HTTP endpoint and contract address", - value: "314159265dD8dbb310642f98f50C066173C1259b@http://127.0.0.1:1234", - config: swarm.ENSClientConfig{ - Endpoint: "http://127.0.0.1:1234", - ContractAddress: "314159265dD8dbb310642f98f50C066173C1259b", - }, - }, - { - description: "WS endpoint and contract address", - value: "314159265dD8dbb310642f98f50C066173C1259b@ws://127.0.0.1:1234", - config: swarm.ENSClientConfig{ - Endpoint: "ws://127.0.0.1:1234", - ContractAddress: "314159265dD8dbb310642f98f50C066173C1259b", - }, - }, - { - description: "IPC Endpoint, TLD and contract address", - value: "test:314159265dD8dbb310642f98f50C066173C1259b@/data/testnet/geth.ipc", - config: swarm.ENSClientConfig{ - Endpoint: "/data/testnet/geth.ipc", - ContractAddress: "314159265dD8dbb310642f98f50C066173C1259b", - TLD: "test", - }, - }, - { - description: "HTTP endpoint, TLD and contract address", - value: "eth:314159265dD8dbb310642f98f50C066173C1259b@http://127.0.0.1:1234", - config: swarm.ENSClientConfig{ - Endpoint: "http://127.0.0.1:1234", - ContractAddress: "314159265dD8dbb310642f98f50C066173C1259b", - TLD: "eth", - }, - }, - { - description: "WS endpoint, TLD and contract address", - value: "eth:314159265dD8dbb310642f98f50C066173C1259b@ws://127.0.0.1:1234", - config: swarm.ENSClientConfig{ - Endpoint: "ws://127.0.0.1:1234", - ContractAddress: "314159265dD8dbb310642f98f50C066173C1259b", - TLD: "eth", - }, - }, - } { - t.Run(x.description, func(t *testing.T) { - config := parseFlagEnsAPI(x.value) - if config.Endpoint != x.config.Endpoint { - t.Errorf("expected Endpoint %q, got %q", x.config.Endpoint, config.Endpoint) - } - if config.ContractAddress != x.config.ContractAddress { - t.Errorf("expected ContractAddress %q, got %q", x.config.ContractAddress, config.ContractAddress) - } - if config.TLD != x.config.TLD { - t.Errorf("expected TLD %q, got %q", x.config.TLD, config.TLD) - } - }) - } -} diff --git a/cmd/swarm/manifest.go b/cmd/swarm/manifest.go index 7c4d6052c..aa276e0f9 100644 --- a/cmd/swarm/manifest.go +++ b/cmd/swarm/manifest.go @@ -30,6 +30,8 @@ import ( "gopkg.in/urfave/cli.v1" ) +const bzzManifestJSON = "application/bzz-manifest+json" + func add(ctx *cli.Context) { args := ctx.Args() if len(args) < 3 { @@ -145,7 +147,7 @@ func addEntryToManifest(ctx *cli.Context, mhash, path, hash, ctype string) strin if path == entry.Path { utils.Fatalf("Path %s already present, not adding anything", path) } else { - if entry.ContentType == "application/bzz-manifest+json" { + if entry.ContentType == bzzManifestJSON { prfxlen := strings.HasPrefix(path, entry.Path) if prfxlen && len(path) > len(longestPathEntry.Path) { longestPathEntry = entry @@ -207,7 +209,7 @@ func updateEntryInManifest(ctx *cli.Context, mhash, path, hash, ctype string) st if path == entry.Path { newEntry = entry } else { - if entry.ContentType == "application/bzz-manifest+json" { + if entry.ContentType == bzzManifestJSON { prfxlen := strings.HasPrefix(path, entry.Path) if prfxlen && len(path) > len(longestPathEntry.Path) { longestPathEntry = entry @@ -281,7 +283,7 @@ func removeEntryFromManifest(ctx *cli.Context, mhash, path string) string { if path == entry.Path { entryToRemove = entry } else { - if entry.ContentType == "application/bzz-manifest+json" { + if entry.ContentType == bzzManifestJSON { prfxlen := strings.HasPrefix(path, entry.Path) if prfxlen && len(path) > len(longestPathEntry.Path) { longestPathEntry = entry diff --git a/cmd/swarm/run_test.go b/cmd/swarm/run_test.go index aaaf9e1e5..ed1502868 100644 --- a/cmd/swarm/run_test.go +++ b/cmd/swarm/run_test.go @@ -27,6 +27,7 @@ import ( "time" "github.com/docker/docker/pkg/reexec" + "github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/accounts/keystore" "github.com/ethereum/go-ethereum/internal/cmdtest" "github.com/ethereum/go-ethereum/node" @@ -156,9 +157,9 @@ type testNode struct { const testPassphrase = "swarm-test-passphrase" -func newTestNode(t *testing.T, dir string) *testNode { +func getTestAccount(t *testing.T, dir string) (conf *node.Config, account accounts.Account) { // create key - conf := &node.Config{ + conf = &node.Config{ DataDir: dir, IPCPath: "bzzd.ipc", NoUSB: true, @@ -167,18 +168,24 @@ func newTestNode(t *testing.T, dir string) *testNode { if err != nil { t.Fatal(err) } - account, err := n.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore).NewAccount(testPassphrase) + account, err = n.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore).NewAccount(testPassphrase) if err != nil { t.Fatal(err) } - node := &testNode{Dir: dir} - // use a unique IPCPath when running tests on Windows if runtime.GOOS == "windows" { conf.IPCPath = fmt.Sprintf("bzzd-%s.ipc", account.Address.String()) } + return conf, account +} + +func newTestNode(t *testing.T, dir string) *testNode { + + conf, account := getTestAccount(t, dir) + node := &testNode{Dir: dir} + // assign ports httpPort, err := assignTCPPort() if err != nil { |