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.

125 lines
4.1 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 "net/http"
  18. import "encoding/hex"
  19. import "encoding/json"
  20. import "github.com/deroproject/derosuite/crypto"
  21. // we definitely need to clear up the MESS that has been created by the MONERO project
  22. // half of their APIs are json rpc and half are http
  23. // for compatibility reasons, we are implementing theirs ( however we are also providin a json rpc implementation)
  24. // we should DISCARD the http
  25. // NOTE: we have currently not implemented the decode as json parameter
  26. // it is however on the pending list
  27. type (
  28. GetTransaction_Handler struct{}
  29. GetTransaction_Params struct {
  30. Tx_Hashes []string `json:"txs_hashes"`
  31. Decode uint64 `json:"decode_as_json,omitempty"` // Monero Daemon breaks if this sent
  32. } // no params
  33. GetTransaction_Result struct {
  34. Txs_as_hex []string `json:"txs_as_hex"`
  35. Txs_as_json []string `json:"txs_as_json"`
  36. Txs []Tx_Related_Info `json:"txs"`
  37. Status string `json:"status"`
  38. }
  39. Tx_Related_Info struct {
  40. As_Hex string `json:"as_hex"`
  41. As_Json string `json:"as_json"`
  42. Block_Height int64 `json:"block_height"`
  43. In_pool bool `json:"in_pool"`
  44. Output_Indices []uint64 `json:"output_indices"`
  45. Tx_hash string `json:"tx_hash"`
  46. }
  47. )
  48. func gettransactions(rw http.ResponseWriter, req *http.Request) {
  49. decoder := json.NewDecoder(req.Body)
  50. var p GetTransaction_Params
  51. err := decoder.Decode(&p)
  52. if err != nil {
  53. panic(err)
  54. }
  55. defer req.Body.Close()
  56. result := gettransactions_fill(p)
  57. //logger.Debugf("Request %+v", p)
  58. encoder := json.NewEncoder(rw)
  59. encoder.Encode(result)
  60. }
  61. // fill up the response
  62. func gettransactions_fill(p GetTransaction_Params) (result GetTransaction_Result) {
  63. for i := 0; i < len(p.Tx_Hashes); i++ {
  64. hash := crypto.HashHexToHash(p.Tx_Hashes[i])
  65. // check whether we can get the tx from the pool
  66. {
  67. tx := chain.Mempool.Mempool_Get_TX(hash)
  68. if tx != nil { // found the tx in the mempool
  69. result.Txs_as_hex = append(result.Txs_as_hex, hex.EncodeToString(tx.Serialize()))
  70. var related Tx_Related_Info
  71. related.Block_Height = -1 // not mined
  72. related.In_pool = true
  73. for i := 0; i < len(tx.Vout); i++ {
  74. related.Output_Indices = append(related.Output_Indices, 0) // till the tx is mined we do not get indices
  75. }
  76. result.Txs = append(result.Txs, related)
  77. continue // no more processing required
  78. }
  79. }
  80. tx, err := chain.Load_TX_FROM_ID(hash)
  81. if err == nil {
  82. result.Txs_as_hex = append(result.Txs_as_hex, hex.EncodeToString(tx.Serialize()))
  83. var related Tx_Related_Info
  84. related.Block_Height = int64(chain.Load_TX_Height(hash))
  85. index := chain.Find_TX_Output_Index(hash)
  86. // logger.Infof("TX hash %s height %d",hash, related.Block_Height)
  87. for i := 0; i < len(tx.Vout); i++ {
  88. related.Output_Indices = append(related.Output_Indices, index+uint64(i))
  89. }
  90. result.Txs = append(result.Txs, related)
  91. } else { // we could not fetch the tx, return an empty string
  92. result.Txs_as_hex = append(result.Txs_as_hex, "")
  93. result.Status = "TX NOT FOUND"
  94. return
  95. }
  96. }
  97. result.Status = "OK"
  98. return
  99. }