upload_and_sync.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. if cluster == "prod" {
  36. cluster = ""
  37. } else {
  38. cluster = cluster + "."
  39. }
  40. for port := from; port <= to; port++ {
  41. endpoints = append(endpoints, fmt.Sprintf("%s://%v.%sswarm-gateways.net", scheme, port, cluster))
  42. }
  43. if includeLocalhost {
  44. endpoints = append(endpoints, "http://localhost:8500")
  45. }
  46. }
  47. func cliUploadAndSync(c *cli.Context) error {
  48. defer func(now time.Time) { log.Info("total time", "time", time.Since(now), "size", filesize) }(time.Now())
  49. generateEndpoints(scheme, cluster, from, to)
  50. log.Info("uploading to " + endpoints[0] + " and syncing")
  51. f, cleanup := generateRandomFile(filesize * 1000000)
  52. defer cleanup()
  53. hash, err := upload(f, endpoints[0])
  54. if err != nil {
  55. log.Error(err.Error())
  56. return err
  57. }
  58. fhash, err := digest(f)
  59. if err != nil {
  60. log.Error(err.Error())
  61. return err
  62. }
  63. log.Info("uploaded successfully", "hash", hash, "digest", fmt.Sprintf("%x", fhash))
  64. if filesize < 10 {
  65. time.Sleep(35 * time.Second)
  66. } else {
  67. time.Sleep(15 * time.Second)
  68. time.Sleep(2 * time.Duration(filesize) * time.Second)
  69. }
  70. wg := sync.WaitGroup{}
  71. for _, endpoint := range endpoints {
  72. endpoint := endpoint
  73. ruid := uuid.New()[:8]
  74. wg.Add(1)
  75. go func(endpoint string, ruid string) {
  76. for {
  77. err := fetch(hash, endpoint, fhash, ruid)
  78. if err != nil {
  79. continue
  80. }
  81. wg.Done()
  82. return
  83. }
  84. }(endpoint, ruid)
  85. }
  86. wg.Wait()
  87. log.Info("all endpoints synced random file successfully")
  88. return nil
  89. }
  90. // fetch is getting the requested `hash` from the `endpoint` and compares it with the `original` file
  91. func fetch(hash string, endpoint string, original []byte, ruid string) error {
  92. log.Trace("sleeping", "ruid", ruid)
  93. time.Sleep(5 * time.Second)
  94. log.Trace("http get request", "ruid", ruid, "api", endpoint, "hash", hash)
  95. res, err := http.Get(endpoint + "/bzz:/" + hash + "/")
  96. if err != nil {
  97. log.Warn(err.Error(), "ruid", ruid)
  98. return err
  99. }
  100. log.Trace("http get response", "ruid", ruid, "api", endpoint, "hash", hash, "code", res.StatusCode, "len", res.ContentLength)
  101. if res.StatusCode != 200 {
  102. err := fmt.Errorf("expected status code %d, got %v", 200, res.StatusCode)
  103. log.Warn(err.Error(), "ruid", ruid)
  104. return err
  105. }
  106. defer res.Body.Close()
  107. rdigest, err := digest(res.Body)
  108. if err != nil {
  109. log.Warn(err.Error(), "ruid", ruid)
  110. return err
  111. }
  112. if !bytes.Equal(rdigest, original) {
  113. err := fmt.Errorf("downloaded imported file md5=%x is not the same as the generated one=%x", rdigest, original)
  114. log.Warn(err.Error(), "ruid", ruid)
  115. return err
  116. }
  117. log.Trace("downloaded file matches random file", "ruid", ruid, "len", res.ContentLength)
  118. return nil
  119. }
  120. // upload is uploading a file `f` to `endpoint` via the `swarm up` cmd
  121. func upload(f *os.File, endpoint string) (string, error) {
  122. var out bytes.Buffer
  123. cmd := exec.Command("swarm", "--bzzapi", endpoint, "up", f.Name())
  124. cmd.Stdout = &out
  125. err := cmd.Run()
  126. if err != nil {
  127. return "", err
  128. }
  129. hash := strings.TrimRight(out.String(), "\r\n")
  130. return hash, nil
  131. }
  132. func digest(r io.Reader) ([]byte, error) {
  133. h := md5.New()
  134. _, err := io.Copy(h, r)
  135. if err != nil {
  136. return nil, err
  137. }
  138. return h.Sum(nil), nil
  139. }
  140. // generateRandomFile is creating a temporary file with the requested byte size
  141. func generateRandomFile(size int) (f *os.File, teardown func()) {
  142. // create a tmp file
  143. tmp, err := ioutil.TempFile("", "swarm-test")
  144. if err != nil {
  145. panic(err)
  146. }
  147. // callback for tmp file cleanup
  148. teardown = func() {
  149. tmp.Close()
  150. os.Remove(tmp.Name())
  151. }
  152. buf := make([]byte, size)
  153. _, err = rand.Read(buf)
  154. if err != nil {
  155. panic(err)
  156. }
  157. ioutil.WriteFile(tmp.Name(), buf, 0755)
  158. return tmp, teardown
  159. }