You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
2.7 KiB

  1. // Copyright 2017-2018 DERO Project. All rights reserved.
  2. // Use of this source code in any form is governed by RESEARCH license.
  3. // license can be found in the LICENSE file.
  4. // GPG: 0F39 E425 8C65 3947 702A 8234 08B2 0360 A03A 9DE8
  5. //
  6. //
  7. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
  8. // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  9. // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  10. // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  11. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  12. // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  13. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  14. // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  15. // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. package rpcserver
  17. import "strconv"
  18. import "net/http"
  19. import "compress/gzip"
  20. //import "github.com/pierrec/lz4"
  21. // serve the outputs in streaming mode
  22. // feeds any outputs requested by the server
  23. func getoutputs(rw http.ResponseWriter, req *http.Request) {
  24. var err error
  25. start := uint64(0)
  26. stop := uint64(0)
  27. { // parse start query parameter
  28. keys, ok := req.URL.Query()["start"]
  29. if !ok || len(keys) < 1 {
  30. //log.Println("Url Param 'key' is missing")
  31. //return
  32. } else {
  33. start_string := keys[0]
  34. start, err = strconv.ParseUint(start_string, 10, 64)
  35. if err != nil {
  36. start = 0
  37. }
  38. }
  39. }
  40. { // parse stop query parameter
  41. keys, ok := req.URL.Query()["stop"]
  42. if !ok || len(keys) < 1 {
  43. } else {
  44. stop_string := keys[0]
  45. stop, err = strconv.ParseUint(stop_string, 10, 64)
  46. if err != nil {
  47. stop = 0
  48. }
  49. }
  50. }
  51. // do sanity check of stop first
  52. top_id := chain.Get_Top_ID()
  53. biggest_output_index := chain.Block_Count_Vout(top_id) + chain.Get_Block_Output_Index(top_id)
  54. if stop == 0 || stop > biggest_output_index {
  55. stop = biggest_output_index
  56. }
  57. // feed in atleast 1 index
  58. if start >= stop {
  59. start = stop - 1
  60. }
  61. /* lz4writer := lz4.NewWriter(rw)
  62. lz4writer.HighCompression = true // enable extreme but slow compression
  63. lz4writer.BlockMaxSize = 256*1024 // small block size to decrease memory consumption
  64. */
  65. gzipwriter := gzip.NewWriter(rw)
  66. defer gzipwriter.Close()
  67. for i := start; i <= stop; i++ {
  68. // load the bytes and send them
  69. data, err := chain.Read_output_index(i)
  70. if err != nil {
  71. logger.Warnf("err while reading output err: %s\n", err)
  72. break
  73. }
  74. //
  75. //rw.Write(data)
  76. // lz4writer.Write(data)
  77. gzipwriter.Write(data)
  78. }
  79. //lz4writer.Flush() // flush any pending data
  80. }