@@ -2,13 +2,11 @@ package main
2
2
3
3
import (
4
4
"bytes"
5
- "context"
6
5
"crypto/md5"
6
+ crand "crypto/rand"
7
7
"fmt"
8
8
"io"
9
9
"io/ioutil"
10
- "net/http"
11
- "net/http/httptrace"
12
10
"os"
13
11
"os/exec"
14
12
"strings"
@@ -18,13 +16,7 @@ import (
18
16
"github.com/ethereum/go-ethereum/common/hexutil"
19
17
"github.com/ethereum/go-ethereum/crypto"
20
18
"github.com/ethereum/go-ethereum/log"
21
- "github.com/ethereum/go-ethereum/metrics"
22
- "github.com/ethereum/go-ethereum/swarm/api/client"
23
- "github.com/ethereum/go-ethereum/swarm/spancontext"
24
19
"github.com/ethereum/go-ethereum/swarm/storage/feed"
25
- "github.com/ethereum/go-ethereum/swarm/testutil"
26
- colorable "github.com/mattn/go-colorable"
27
- opentracing "github.com/opentracing/opentracing-go"
28
20
"github.com/pborman/uuid"
29
21
cli "gopkg.in/urfave/cli.v1"
30
22
)
@@ -33,27 +25,6 @@ const (
33
25
feedRandomDataLength = 8
34
26
)
35
27
36
- func cliFeedUploadAndSync (c * cli.Context ) error {
37
- metrics .GetOrRegisterCounter ("feed-and-sync" , nil ).Inc (1 )
38
- log .Root ().SetHandler (log .CallerFileHandler (log .LvlFilterHandler (log .Lvl (verbosity ), log .StreamHandler (colorable .NewColorableStderr (), log .TerminalFormat (true )))))
39
-
40
- errc := make (chan error )
41
- go func () {
42
- errc <- feedUploadAndSync (c )
43
- }()
44
-
45
- select {
46
- case err := <- errc :
47
- if err != nil {
48
- metrics .GetOrRegisterCounter ("feed-and-sync.fail" , nil ).Inc (1 )
49
- }
50
- return err
51
- case <- time .After (time .Duration (timeout ) * time .Second ):
52
- metrics .GetOrRegisterCounter ("feed-and-sync.timeout" , nil ).Inc (1 )
53
- return fmt .Errorf ("timeout after %v sec" , timeout )
54
- }
55
- }
56
-
57
28
// TODO: retrieve with manifest + extract repeating code
58
29
func feedUploadAndSync (c * cli.Context ) error {
59
30
defer func (now time.Time ) { log .Info ("total time" , "time" , time .Since (now ), "size (kb)" , filesize ) }(time .Now ())
@@ -232,9 +203,10 @@ func feedUploadAndSync(c *cli.Context) error {
232
203
seed := int (time .Now ().UnixNano () / 1e6 )
233
204
log .Info ("feed uploading to " + endpoints [0 ]+ " and syncing" , "seed" , seed )
234
205
235
- randomBytes := testutil .RandomBytes (seed , filesize * 1000 )
206
+ h = md5 .New ()
207
+ r := io .TeeReader (io .LimitReader (crand .Reader , int64 (filesize * 1000 )), h )
236
208
237
- hash , err := upload (& randomBytes , endpoints [0 ])
209
+ hash , err := upload (r , filesize * 1000 , endpoints [0 ])
238
210
if err != nil {
239
211
return err
240
212
}
@@ -243,10 +215,7 @@ func feedUploadAndSync(c *cli.Context) error {
243
215
return err
244
216
}
245
217
multihashHex := hexutil .Encode (hashBytes )
246
- fileHash , err := digest (bytes .NewReader (randomBytes ))
247
- if err != nil {
248
- return err
249
- }
218
+ fileHash := h .Sum (nil )
250
219
251
220
log .Info ("uploaded successfully" , "hash" , hash , "digest" , fmt .Sprintf ("%x" , fileHash ))
252
221
@@ -307,60 +276,3 @@ func feedUploadAndSync(c *cli.Context) error {
307
276
308
277
return nil
309
278
}
310
-
311
- func fetchFeed (topic string , user string , endpoint string , original []byte , ruid string ) error {
312
- ctx , sp := spancontext .StartSpan (context .Background (), "feed-and-sync.fetch" )
313
- defer sp .Finish ()
314
-
315
- log .Trace ("sleeping" , "ruid" , ruid )
316
- time .Sleep (3 * time .Second )
317
-
318
- log .Trace ("http get request (feed)" , "ruid" , ruid , "api" , endpoint , "topic" , topic , "user" , user )
319
-
320
- var tn time.Time
321
- reqUri := endpoint + "/bzz-feed:/?topic=" + topic + "&user=" + user
322
- req , _ := http .NewRequest ("GET" , reqUri , nil )
323
-
324
- opentracing .GlobalTracer ().Inject (
325
- sp .Context (),
326
- opentracing .HTTPHeaders ,
327
- opentracing .HTTPHeadersCarrier (req .Header ))
328
-
329
- trace := client .GetClientTrace ("feed-and-sync - http get" , "feed-and-sync" , ruid , & tn )
330
-
331
- req = req .WithContext (httptrace .WithClientTrace (ctx , trace ))
332
- transport := http .DefaultTransport
333
-
334
- //transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
335
-
336
- tn = time .Now ()
337
- res , err := transport .RoundTrip (req )
338
- if err != nil {
339
- log .Error (err .Error (), "ruid" , ruid )
340
- return err
341
- }
342
-
343
- log .Trace ("http get response (feed)" , "ruid" , ruid , "api" , endpoint , "topic" , topic , "user" , user , "code" , res .StatusCode , "len" , res .ContentLength )
344
-
345
- if res .StatusCode != 200 {
346
- return fmt .Errorf ("expected status code %d, got %v (ruid %v)" , 200 , res .StatusCode , ruid )
347
- }
348
-
349
- defer res .Body .Close ()
350
-
351
- rdigest , err := digest (res .Body )
352
- if err != nil {
353
- log .Warn (err .Error (), "ruid" , ruid )
354
- return err
355
- }
356
-
357
- if ! bytes .Equal (rdigest , original ) {
358
- err := fmt .Errorf ("downloaded imported file md5=%x is not the same as the generated one=%x" , rdigest , original )
359
- log .Warn (err .Error (), "ruid" , ruid )
360
- return err
361
- }
362
-
363
- log .Trace ("downloaded file matches random file" , "ruid" , ruid , "len" , res .ContentLength )
364
-
365
- return nil
366
- }
0 commit comments