upload_and_sync.go 4.6 KB

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