Skip to content

Commit

Permalink
cmd/swarm, swarm: add DeliverySkipCheck flag and tests (ethereum#548)
Browse files Browse the repository at this point in the history
  • Loading branch information
janos authored May 16, 2018
1 parent 0ef401e commit 41f1bb5
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 47 deletions.
11 changes: 11 additions & 0 deletions cmd/swarm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ const (
SWARM_ENV_SWAP_API = "SWARM_SWAP_API"
SWARM_ENV_SYNC_DISABLE = "SWARM_SYNC_DISABLE"
SWARM_ENV_SYNC_UPDATE_DELAY = "SWARM_ENV_SYNC_UPDATE_DELAY"
SWARM_ENV_DELIVERY_SKIP_CHECK = "SWARM_DELIVERY_SKIP_CHECK"
SWARM_ENV_ENS_API = "SWARM_ENS_API"
SWARM_ENV_ENS_ADDR = "SWARM_ENS_ADDR"
SWARM_ENV_CORS = "SWARM_CORS"
Expand Down Expand Up @@ -203,6 +204,10 @@ func cmdLineOverride(currentConfig *bzzapi.Config, ctx *cli.Context) *bzzapi.Con
currentConfig.SyncUpdateDelay = d
}

if ctx.GlobalIsSet(SwarmDeliverySkipCheckFlag.Name) {
currentConfig.DeliverySkipCheck = true
}

currentConfig.SwapApi = ctx.GlobalString(SwarmSwapAPIFlag.Name)
if currentConfig.SwapEnabled && currentConfig.SwapApi == "" {
utils.Fatalf(SWARM_ERR_SWAP_SET_NO_API)
Expand Down Expand Up @@ -284,6 +289,12 @@ func envVarsOverride(currentConfig *bzzapi.Config) (config *bzzapi.Config) {
}
}

if v := os.Getenv(SWARM_ENV_DELIVERY_SKIP_CHECK); v != "" {
if skipCheck, err := strconv.ParseBool(v); err != nil {
currentConfig.DeliverySkipCheck = skipCheck
}
}

if v := os.Getenv(SWARM_ENV_SYNC_UPDATE_DELAY); v != "" {
if d, err := time.ParseDuration(v); err != nil {
currentConfig.SyncUpdateDelay = d
Expand Down
15 changes: 15 additions & 0 deletions cmd/swarm/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func TestConfigCmdLineOverrides(t *testing.T) {
fmt.Sprintf("--%s", SwarmSyncDisabledFlag.Name),
fmt.Sprintf("--%s", CorsStringFlag.Name), "*",
fmt.Sprintf("--%s", SwarmAccountFlag.Name), account.Address.String(),
fmt.Sprintf("--%s", SwarmDeliverySkipCheckFlag.Name),
fmt.Sprintf("--%s", EnsAPIFlag.Name), "",
"--datadir", dir,
"--ipcpath", conf.IPCPath,
Expand Down Expand Up @@ -128,6 +129,10 @@ func TestConfigCmdLineOverrides(t *testing.T) {
t.Fatal("Expected Sync to be disabled, but is true")
}

if !info.DeliverySkipCheck {
t.Fatal("Expected DeliverySkipCheck to be enabled, but it is not")
}

if info.Cors != "*" {
t.Fatalf("Expected Cors flag to be set to %s, got %s", "*", info.Cors)
}
Expand All @@ -148,6 +153,7 @@ func TestConfigFileOverrides(t *testing.T) {
defaultConf := api.NewConfig()
//change some values in order to test if they have been loaded
defaultConf.SyncEnabled = false
defaultConf.DeliverySkipCheck = true
defaultConf.NetworkId = 54
defaultConf.Port = httpPort
defaultConf.DbCapacity = 9000000
Expand Down Expand Up @@ -222,6 +228,10 @@ func TestConfigFileOverrides(t *testing.T) {
t.Fatal("Expected Sync to be disabled, but is true")
}

if !info.DeliverySkipCheck {
t.Fatal("Expected DeliverySkipCheck to be enabled, but it is not")
}

if info.DbCapacity != 9000000 {
t.Fatalf("Expected network ID to be %d, got %d", 54, info.NetworkId)
}
Expand Down Expand Up @@ -253,6 +263,7 @@ func TestConfigEnvVars(t *testing.T) {
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", SwarmSyncDisabledFlag.EnvVar, "true"))
envVars = append(envVars, fmt.Sprintf("%s=%s", SwarmDeliverySkipCheckFlag.EnvVar, "true"))

dir, err := ioutil.TempDir("", "bzztest")
if err != nil {
Expand Down Expand Up @@ -333,6 +344,10 @@ func TestConfigEnvVars(t *testing.T) {
t.Fatal("Expected Sync to be disabled, but is true")
}

if !info.DeliverySkipCheck {
t.Fatal("Expected DeliverySkipCheck to be enabled, but it is not")
}

node.Shutdown()
cmd.Process.Kill()
}
Expand Down
6 changes: 6 additions & 0 deletions cmd/swarm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ var (
Usage: "Duration for sync subscriptions update after no new peers are added (default 15s)",
EnvVar: SWARM_ENV_SYNC_UPDATE_DELAY,
}
SwarmDeliverySkipCheckFlag = cli.BoolFlag{
Name: "delivery-skip-check",
Usage: "Skip chunk delivery check (default false)",
EnvVar: SWARM_ENV_DELIVERY_SKIP_CHECK,
}
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",
Expand Down Expand Up @@ -343,6 +348,7 @@ Remove corrupt entries from a local chunk database.
SwarmSwapAPIFlag,
SwarmSyncDisabledFlag,
SwarmSyncUpdateDelay,
SwarmDeliverySkipCheckFlag,
SwarmListenAddrFlag,
SwarmPortFlag,
SwarmAccountFlag,
Expand Down
64 changes: 33 additions & 31 deletions swarm/api/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,24 +50,25 @@ type Config struct {
Swap *swap.SwapParams
Pss *pss.PssParams
//*network.SyncParams
Contract common.Address
EnsRoot common.Address
EnsAPIs []string
Path string
ListenAddr string
Port string
PublicKey string
BzzKey string
NodeID string
NetworkId uint64
SwapEnabled bool
SyncEnabled bool
SyncUpdateDelay time.Duration
SwapApi string
Cors string
BzzAccount string
BootNodes string
privateKey *ecdsa.PrivateKey
Contract common.Address
EnsRoot common.Address
EnsAPIs []string
Path string
ListenAddr string
Port string
PublicKey string
BzzKey string
NodeID string
NetworkId uint64
SwapEnabled bool
SyncEnabled bool
DeliverySkipCheck bool
SyncUpdateDelay time.Duration
SwapApi string
Cors string
BzzAccount string
BootNodes string
privateKey *ecdsa.PrivateKey
}

//create a default config with all parameters to set to defaults
Expand All @@ -78,19 +79,20 @@ func NewConfig() (self *Config) {
DPAParams: storage.NewDPAParams(),
HiveParams: network.NewHiveParams(),
//SyncParams: network.NewDefaultSyncParams(),
Swap: swap.NewDefaultSwapParams(),
Pss: pss.NewPssParams(),
ListenAddr: DefaultHTTPListenAddr,
Port: DefaultHTTPPort,
Path: node.DefaultDataDir(),
EnsAPIs: nil,
EnsRoot: ens.TestNetAddress,
NetworkId: network.NetworkID,
SwapEnabled: false,
SyncEnabled: true,
SyncUpdateDelay: 15 * time.Second,
SwapApi: "",
BootNodes: "",
Swap: swap.NewDefaultSwapParams(),
Pss: pss.NewPssParams(),
ListenAddr: DefaultHTTPListenAddr,
Port: DefaultHTTPPort,
Path: node.DefaultDataDir(),
EnsAPIs: nil,
EnsRoot: ens.TestNetAddress,
NetworkId: network.NetworkID,
SwapEnabled: false,
SyncEnabled: true,
DeliverySkipCheck: false,
SyncUpdateDelay: 15 * time.Second,
SwapApi: "",
BootNodes: "",
}

return
Expand Down
13 changes: 13 additions & 0 deletions swarm/network/stream/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,3 +413,16 @@ func (s *testExternalServer) GetData([]byte) ([]byte, error) {
}

func (s *testExternalServer) Close() {}

// Sets the global value defaultSkipCheck.
// It should be used in test function defer to reset the global value
// to the original value.
//
// defer setDefaultSkipCheck(defaultSkipCheck)
// defaultSkipCheck = skipCheck
//
// This works as defer function arguments evaluations are evaluated as ususal,
// but only the function body invocation is deferred.
func setDefaultSkipCheck(skipCheck bool) {
defaultSkipCheck = skipCheck
}
15 changes: 10 additions & 5 deletions swarm/network/stream/intervals_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,22 @@ func newIntervalsStreamerService(ctx *adapters.ServiceContext) (node.Service, er
}

func TestIntervals(t *testing.T) {
testIntervals(t, true, nil)
testIntervals(t, false, NewRange(9, 26))
testIntervals(t, true, NewRange(9, 26))
testIntervals(t, true, nil, false)
testIntervals(t, false, NewRange(9, 26), false)
testIntervals(t, true, NewRange(9, 26), false)

testIntervals(t, true, nil, true)
testIntervals(t, false, NewRange(9, 26), true)
testIntervals(t, true, NewRange(9, 26), true)
}

func testIntervals(t *testing.T, live bool, history *Range) {
func testIntervals(t *testing.T, live bool, history *Range, skipCheck bool) {
nodes := 2
chunkCount := dataChunkCount
skipCheck := false

defer setDefaultSkipCheck(defaultSkipCheck)
defaultSkipCheck = skipCheck

toAddr = network.NewAddrFromNodeID
conf := &streamTesting.RunConfig{
Adapter: *adapter,
Expand Down
1 change: 1 addition & 0 deletions swarm/network/stream/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func TestSyncerSimulation(t *testing.T) {
}

func testSyncBetweenNodes(t *testing.T, nodes, conns, chunkCount int, skipCheck bool, po uint8) {
defer setDefaultSkipCheck(defaultSkipCheck)
defaultSkipCheck = skipCheck
createStoreFunc = createTestLocalStorageFromSim
registries = make(map[discover.NodeID]*TestRegistry)
Expand Down
Loading

0 comments on commit 41f1bb5

Please sign in to comment.