upload_and_sync.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // Copyright 2018 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // go-ethereum is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package main
  17. import (
  18. "bytes"
  19. "crypto/md5"
  20. "crypto/rand"
  21. "fmt"
  22. "io"
  23. "io/ioutil"
  24. "net/http"
  25. "os"
  26. "os/exec"
  27. "strings"
  28. "sync"
  29. "time"
  30. "github.com/ethereum/go-ethereum/log"
  31. "github.com/pborman/uuid"
  32. cli "gopkg.in/urfave/cli.v1"
  33. )
  34. func generateEndpoints(scheme string, cluster string, from int, to int) {
  35. for port := from; port <= to; port++ {
  36. endpoints = append(endpoints, fmt.Sprintf("%s://%v.%s.swarm-gateways.net", scheme, port, cluster))
  37. }
  38. if includeLocalhost {
  39. endpoints = append(endpoints, "http://localhost:8500")
  40. }
  41. }
  42. func cliUploadAndSync(c *cli.Context) error {
  43. defer func(now time.Time) { log.Info("total time", "time", time.Since(now), "size", filesize) }(time.Now())
  44. generateEndpoints(scheme, cluster, from, to)
  45. log.Info("uploading to " + endpoints[0] + " and syncing")
  46. f, cleanup := generateRandomFile(filesize * 1000000)
  47. defer cleanup()
  48. hash, err := upload(f, endpoints[0])
  49. if err != nil {
  50. log.Error(err.Error())
  51. return err
  52. }
  53. fhash, err := digest(f)
  54. if err != nil {
  55. log.Error(err.Error())
  56. return err
  57. }
  58. log.Info("uploaded successfully", "hash", hash, "digest", fmt.Sprintf("%x", fhash))
  59. if filesize < 10 {
  60. time.Sleep(15 * time.Second)
  61. } else {
  62. time.Sleep(2 * time.Duration(filesize) * time.Second)
  63. }
  64. wg := sync.WaitGroup{}
  65. for _, endpoint := range endpoints {
  66. endpoint := endpoint
  67. ruid := uuid.New()[:8]
  68. wg.Add(1)
  69. go func(endpoint string, ruid string) {
  70. for {
  71. err := fetch(hash, endpoint, fhash, ruid)
  72. if err != nil {
  73. continue
  74. }
  75. wg.Done()
  76. return
  77. }
  78. }(endpoint, ruid)
  79. }
  80. wg.Wait()
  81. log.Info("all endpoints synced random file successfully")
  82. return nil
  83. }
  84. // fetch is getting the requested `hash` from the `endpoint` and compares it with the `original` file
  85. func fetch(hash string, endpoint string, original []byte, ruid string) error {
  86. log.Trace("sleeping", "ruid", ruid)
  87. time.Sleep(1 * time.Second)
  88. log.Trace("http get request", "ruid", ruid, "api", endpoint, "hash", hash)
  89. res, err := http.Get(endpoint + "/bzz:/" + hash + "/")
  90. if err != nil {
  91. log.Warn(err.Error(), "ruid", ruid)
  92. return err
  93. }
  94. log.Trace("http get response", "ruid", ruid, "api", endpoint, "hash", hash, "code", res.StatusCode, "len", res.ContentLength)
  95. if res.StatusCode != 200 {
  96. err := fmt.Errorf("expected status code %d, got %v", 200, res.StatusCode)
  97. log.Warn(err.Error(), "ruid", ruid)
  98. return err
  99. }
  100. defer res.Body.Close()
  101. rdigest, err := digest(res.Body)
  102. if err != nil {
  103. log.Warn(err.Error(), "ruid", ruid)
  104. return err
  105. }
  106. if !bytes.Equal(rdigest, original) {
  107. err := fmt.Errorf("downloaded imported file md5=%x is not the same as the generated one=%x", rdigest, original)
  108. log.Warn(err.Error(), "ruid", ruid)
  109. return err
  110. }
  111. log.Trace("downloaded file matches random file", "ruid", ruid, "len", res.ContentLength)
  112. return nil
  113. }
  114. // upload is uploading a file `f` to `endpoint` via the `swarm up` cmd
  115. func upload(f *os.File, endpoint string) (string, error) {
  116. var out bytes.Buffer
  117. cmd := exec.Command("swarm", "--bzzapi", endpoint, "up", f.Name())
  118. cmd.Stdout = &out
  119. err := cmd.Run()
  120. if err != nil {
  121. return "", err
  122. }
  123. hash := strings.TrimRight(out.String(), "\r\n")
  124. return hash, nil
  125. }
  126. func digest(r io.Reader) ([]byte, error) {
  127. h := md5.New()
  128. _, err := io.Copy(h, r)
  129. if err != nil {
  130. return nil, err
  131. }
  132. return h.Sum(nil), nil
  133. }
  134. // generateRandomFile is creating a temporary file with the requested byte size
  135. func generateRandomFile(size int) (f *os.File, teardown func()) {
  136. // create a tmp file
  137. tmp, err := ioutil.TempFile("", "swarm-test")
  138. if err != nil {
  139. panic(err)
  140. }
  141. // callback for tmp file cleanup
  142. teardown = func() {
  143. tmp.Close()
  144. os.Remove(tmp.Name())
  145. }
  146. buf := make([]byte, size)
  147. _, err = rand.Read(buf)
  148. if err != nil {
  149. panic(err)
  150. }
  151. ioutil.WriteFile(tmp.Name(), buf, 0755)
  152. return tmp, teardown
  153. }