cmd/swarm/swarm-snapshot: address pr comments

This commit is contained in:
Elad Nachmias 2019-01-15 12:04:12 +05:30
parent 621e2b64ee
commit 85208256d3
7 changed files with 4 additions and 134 deletions

View file

@ -104,7 +104,7 @@ func createSnapshot(filename string, nodes int, services []string) (err error) {
ctx, cancelSimRun := context.WithTimeout(context.Background(), 2*time.Minute) ctx, cancelSimRun := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancelSimRun() defer cancelSimRun()
if _, err := sim.WaitTillHealthy(ctx, 2); err != nil { if _, err := sim.WaitTillHealthy(ctx); err != nil {
return fmt.Errorf("wait for healthy kademlia: %v", err) return fmt.Errorf("wait for healthy kademlia: %v", err)
} }
@ -143,7 +143,7 @@ func createSnapshot(filename string, nodes int, services []string) (err error) {
func touchPath(filename string) (string, error) { func touchPath(filename string) (string, error) {
if path.IsAbs(filename) { if path.IsAbs(filename) {
if _, err := os.Stat(filename); err == nil { if _, err := os.Stat(filename); err == nil {
// path exists, we will overwrite the file // path exists, overwrite
return filename, nil return filename, nil
} }
} }
@ -156,7 +156,7 @@ func touchPath(filename string) (string, error) {
_, err = os.Stat(path.Join(dir, filename)) _, err = os.Stat(path.Join(dir, filename))
if err == nil { if err == nil {
// path exists, we will overwrite // path exists, overwrite
return filename, nil return filename, nil
} }

View file

@ -133,10 +133,6 @@ func TestSnapshotCreate(t *testing.T) {
} }
} }
err = verifySnapshot(file.Name())
if err != nil {
t.Error(err)
}
}) })
} }
} }

View file

@ -76,15 +76,6 @@ func newApp() (app *cli.App) {
}, },
), ),
}, },
{
Name: "verify",
Aliases: []string{"v"},
Usage: "verify a swarm snapshot",
Action: verify,
// Allow app flags to be specified after the
// command argument.
Flags: app.Flags,
},
} }
return app return app

File diff suppressed because one or more lines are too long

View file

@ -1,81 +0,0 @@
// Copyright 2018 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 (
"context"
"errors"
"fmt"
"os"
"sync"
"time"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/p2p/simulations/adapters"
"github.com/ethereum/go-ethereum/swarm/network"
"github.com/ethereum/go-ethereum/swarm/network/simulation"
cli "gopkg.in/urfave/cli.v1"
)
// verify is used as the entry function for "verify" app command.
func verify(ctx *cli.Context) error {
log.PrintOrigins(true)
log.Root().SetHandler(log.LvlFilterHandler(log.Lvl(ctx.Int("verbosity")), log.StreamHandler(os.Stdout, log.TerminalFormat(true))))
if len(ctx.Args()) < 1 {
return errors.New("argument should be the filename to verify")
}
return verifySnapshot(ctx.Args()[0])
}
// verifySnapshot constructs a simulation, uploads a snapshot from
// a file with provided filename and validates that kademlia is healthy.
func verifySnapshot(filename string) (err error) {
sim := simulation.New(map[string]simulation.ServiceFunc{
"bzz": func(ctx *adapters.ServiceContext, b *sync.Map) (node.Service, func(), error) {
addr := network.NewAddr(ctx.Config.Node())
kad := network.NewKademlia(addr.Over(), network.NewKadParams())
hp := network.NewHiveParams()
hp.KeepAliveInterval = time.Duration(200) * time.Millisecond
hp.Discovery = false // discovery must be false when uploading a snapshot
config := &network.BzzConfig{
OverlayAddr: addr.Over(),
UnderlayAddr: addr.Under(),
HiveParams: hp,
}
return network.NewBzz(config, kad, nil, nil, nil), nil, nil
},
})
defer sim.Close()
err = sim.UploadSnapshot(filename)
if err != nil {
return fmt.Errorf("upload snapshot: %v", err)
}
ctx, cancelSimRun := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancelSimRun()
_, err = sim.WaitTillHealthy(ctx, 2)
if err != nil {
return fmt.Errorf("wait for healthy kademlia: %v", err)
}
return nil
}

View file

@ -1,35 +0,0 @@
// Copyright 2018 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"
)
// TestSnapshotVerify executes "verify" command with a
// pregenerated snapshot file that is valid.
func TestSnapshotVerify(t *testing.T) {
snap := runSnapshot(t,
"verify",
"testdata/snapshot.json",
)
snap.ExpectExit()
if code := snap.ExitStatus(); code != 0 {
t.Fatalf("command exit code %v, expected 0", code)
}
}

View file

@ -227,7 +227,7 @@ func (tt *TestCmd) Kill() {
} }
func (tt *TestCmd) withKillTimeout(fn func()) { func (tt *TestCmd) withKillTimeout(fn func()) {
timeout := time.AfterFunc(15*time.Second, func() { timeout := time.AfterFunc(5*time.Second, func() {
tt.Log("killing the child process (timeout)") tt.Log("killing the child process (timeout)")
tt.Kill() tt.Kill()
}) })