From 75f0248db01458a285fbef29f25a88717395268c Mon Sep 17 00:00:00 2001 From: zelig Date: Fri, 13 Feb 2015 12:29:32 +0100 Subject: [PATCH] add pull cli flag to start swarm download --- bzz/chunker_test.go | 12 ++++++++++-- cmd/ethereum/flags.go | 2 ++ cmd/ethereum/main.go | 2 +- cmd/utils/cmd.go | 4 ++-- eth/backend.go | 22 ++++++++++++++++++---- 5 files changed, 33 insertions(+), 9 deletions(-) diff --git a/bzz/chunker_test.go b/bzz/chunker_test.go index d533317f0e..5886dfd6f5 100644 --- a/bzz/chunker_test.go +++ b/bzz/chunker_test.go @@ -178,14 +178,22 @@ func readAll(reader SectionReader, result []byte) { } func benchmarkJoinRandomData(n int, chunks int, t *testing.B) { + t.StopTimer() for i := 0; i < t.N; i++ { - t.StopTimer() + // fmt.Printf("round %v\n", i) chunker, tester := chunkerAndTester() - key, _ := tester.Split(chunker, n) + key, slice := tester.Split(chunker, n) + // fmt.Printf("split done %v, joining...\n", i) t.StartTimer() reader := tester.Join(chunker, key, i) + // fmt.Printf("join done %v, reading...\n", i) result := make([]byte, n) readAll(reader, result) + // fmt.Printf("read done %v\n", i) + t.StopTimer() + if !bytes.Equal(slice, result) { + t.Errorf("input output mismatch") + } } } diff --git a/cmd/ethereum/flags.go b/cmd/ethereum/flags.go index d69ea5cfb6..169756e272 100644 --- a/cmd/ethereum/flags.go +++ b/cmd/ethereum/flags.go @@ -66,6 +66,7 @@ var ( Dial bool PrintVersion bool Peers string + Pull string ) // flags specific to cli client @@ -105,6 +106,7 @@ func Init() { flag.BoolVar(&Dial, "dial", true, "dial out connections (on)") flag.BoolVar(&GenAddr, "genaddr", false, "create a new priv/pub key") flag.StringVar(&Peers, "peers", "", "imports the file given (hex or mnemonic formats)") + flag.StringVar(&Peers, "pull", "", "swarm pull key") flag.StringVar(&SecretFile, "import", "", "imports the file given (hex or mnemonic formats)") flag.StringVar(&ExportDir, "export", "", "exports the session keyring to files in the directory given") flag.StringVar(&LogFile, "logfile", "", "log file (defaults to standard output)") diff --git a/cmd/ethereum/main.go b/cmd/ethereum/main.go index ea53b9a304..8a549dbea0 100644 --- a/cmd/ethereum/main.go +++ b/cmd/ethereum/main.go @@ -134,7 +134,7 @@ func main() { utils.StartWebSockets(ethereum) } - utils.StartEthereum(ethereum, UseSeed, Peers) + utils.StartEthereum(ethereum, UseSeed, Peers, Pull) if StartJsConsole { InitJsConsole(ethereum) diff --git a/cmd/utils/cmd.go b/cmd/utils/cmd.go index 06e3fb93f1..f6e7999e75 100644 --- a/cmd/utils/cmd.go +++ b/cmd/utils/cmd.go @@ -120,9 +120,9 @@ func exit(err error) { os.Exit(status) } -func StartEthereum(ethereum *eth.Ethereum, UseSeed bool, Peers string) { +func StartEthereum(ethereum *eth.Ethereum, UseSeed bool, Peers string, Pull string) { clilogger.Infof("Starting %s", ethereum.ClientIdentity()) - err := ethereum.Start(UseSeed, Peers) + err := ethereum.Start(UseSeed, Peers, Pull) if err != nil { exit(err) } diff --git a/eth/backend.go b/eth/backend.go index f5163bdfd5..535444b02a 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -2,7 +2,9 @@ package eth import ( "fmt" + "io" "net" + "os" "strings" "sync" @@ -70,6 +72,7 @@ type Ethereum struct { RpcServer *rpc.JsonRpcServer keyManager *crypto.KeyManager + dpa *bzz.DPA clientIdentity p2p.ClientIdentity logger ethlogger.LogSystem @@ -144,12 +147,12 @@ func New(config *Config) (*Ethereum, error) { } chunker := &bzz.TreeChunker{} chunker.Init() - dpa := &bzz.DPA{ + eth.dpa = &bzz.DPA{ Chunker: chunker, ChunkStore: netStore, } - dpa.Start() - go bzz.StartHttpServer(dpa) + eth.dpa.Start() + go bzz.StartHttpServer(eth.dpa) nat, err := p2p.ParseNAT(config.NATType, config.PMPGateway) if err != nil { @@ -234,7 +237,7 @@ func (s *Ethereum) MaxPeers() int { } // Start the ethereum -func (s *Ethereum) Start(seed bool, p string) error { +func (s *Ethereum) Start(seed bool, p string, pull string) error { err := s.net.Start() if err != nil { return err @@ -264,6 +267,17 @@ func (s *Ethereum) Start(seed bool, p string) error { } } + if len(pull) > 0 { + key := make([]byte, s.dpa.Chunker.KeySize()) + reader := s.dpa.Retrieve(key) + fo, err := os.Open("/tmp/swarm.tmp") + if err != nil { + logger.Warnf("file open error %v", err) + } else { + io.Copy(fo, reader) + } + } + // TODO: read peers here if seed { logger.Infof("Connect to seed node %v", seedNodeAddress)