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.

205 lines
5.4 KiB

7 years ago
  1. package core
  2. import (
  3. "fmt"
  4. ctypes "github.com/tendermint/tendermint/rpc/core/types"
  5. "github.com/tendermint/tendermint/state/txindex/null"
  6. "github.com/tendermint/tendermint/types"
  7. tmquery "github.com/tendermint/tmlibs/pubsub/query"
  8. )
  9. // Tx allows you to query the transaction results. `nil` could mean the
  10. // transaction is in the mempool, invalidated, or was not sent in the first
  11. // place.
  12. //
  13. // ```shell
  14. // curl "localhost:46657/tx?hash=0x2B8EC32BA2579B3B8606E42C06DE2F7AFA2556EF"
  15. // ```
  16. //
  17. // ```go
  18. // client := client.NewHTTP("tcp://0.0.0.0:46657", "/websocket")
  19. // tx, err := client.Tx([]byte("2B8EC32BA2579B3B8606E42C06DE2F7AFA2556EF"), true)
  20. // ```
  21. //
  22. // > The above command returns JSON structured like this:
  23. //
  24. // ```json
  25. // {
  26. // "error": "",
  27. // "result": {
  28. // "proof": {
  29. // "Proof": {
  30. // "aunts": []
  31. // },
  32. // "Data": "YWJjZA==",
  33. // "RootHash": "2B8EC32BA2579B3B8606E42C06DE2F7AFA2556EF",
  34. // "Total": 1,
  35. // "Index": 0
  36. // },
  37. // "tx": "YWJjZA==",
  38. // "tx_result": {
  39. // "log": "",
  40. // "data": "",
  41. // "code": 0
  42. // },
  43. // "index": 0,
  44. // "height": 52
  45. // },
  46. // "id": "",
  47. // "jsonrpc": "2.0"
  48. // }
  49. // ```
  50. //
  51. // Returns a transaction matching the given transaction hash.
  52. //
  53. // ### Query Parameters
  54. //
  55. // | Parameter | Type | Default | Required | Description |
  56. // |-----------+--------+---------+----------+-----------------------------------------------------------|
  57. // | hash | []byte | nil | true | The transaction hash |
  58. // | prove | bool | false | false | Include a proof of the transaction inclusion in the block |
  59. //
  60. // ### Returns
  61. //
  62. // - `proof`: the `types.TxProof` object
  63. // - `tx`: `[]byte` - the transaction
  64. // - `tx_result`: the `abci.Result` object
  65. // - `index`: `int` - index of the transaction
  66. // - `height`: `int` - height of the block where this transaction was in
  67. func Tx(hash []byte, prove bool) (*ctypes.ResultTx, error) {
  68. // if index is disabled, return error
  69. if _, ok := txIndexer.(*null.TxIndex); ok {
  70. return nil, fmt.Errorf("Transaction indexing is disabled.")
  71. }
  72. r, err := txIndexer.Get(hash)
  73. if err != nil {
  74. return nil, err
  75. }
  76. if r == nil {
  77. return nil, fmt.Errorf("Tx (%X) not found", hash)
  78. }
  79. height := r.Height
  80. index := int(r.Index) // XXX:overflow
  81. var proof types.TxProof
  82. if prove {
  83. block := blockStore.LoadBlock(height)
  84. // TODO: handle overflow
  85. proof = block.Data.Txs.Proof(index)
  86. }
  87. return &ctypes.ResultTx{
  88. Height: height,
  89. Index: uint32(index),
  90. TxResult: r.Result,
  91. Tx: r.Tx,
  92. Proof: proof,
  93. }, nil
  94. }
  95. // TxSearch allows you to query for multiple transactions results.
  96. //
  97. // ```shell
  98. // curl "localhost:46657/tx_search?query=\"account.owner='Ivan'\"&prove=true"
  99. // ```
  100. //
  101. // ```go
  102. // client := client.NewHTTP("tcp://0.0.0.0:46657", "/websocket")
  103. // q, err := tmquery.New("account.owner='Ivan'")
  104. // tx, err := client.TxSearch(q, true)
  105. // ```
  106. //
  107. // > The above command returns JSON structured like this:
  108. //
  109. // ```json
  110. // {
  111. // "result": [
  112. // {
  113. // "proof": {
  114. // "Proof": {
  115. // "aunts": [
  116. // "J3LHbizt806uKnABNLwG4l7gXCA=",
  117. // "iblMO/M1TnNtlAefJyNCeVhjAb0=",
  118. // "iVk3ryurVaEEhdeS0ohAJZ3wtB8=",
  119. // "5hqMkTeGqpct51ohX0lZLIdsn7Q=",
  120. // "afhsNxFnLlZgFDoyPpdQSe0bR8g="
  121. // ]
  122. // },
  123. // "Data": "mvZHHa7HhZ4aRT0xMDA=",
  124. // "RootHash": "F6541223AA46E428CB1070E9840D2C3DF3B6D776",
  125. // "Total": 32,
  126. // "Index": 31
  127. // },
  128. // "tx": "mvZHHa7HhZ4aRT0xMDA=",
  129. // "tx_result": {},
  130. // "index": 31,
  131. // "height": 12
  132. // }
  133. // ],
  134. // "id": "",
  135. // "jsonrpc": "2.0"
  136. // }
  137. // ```
  138. //
  139. // Returns transactions matching the given query.
  140. //
  141. // ### Query Parameters
  142. //
  143. // | Parameter | Type | Default | Required | Description |
  144. // |-----------+--------+---------+----------+-----------------------------------------------------------|
  145. // | query | string | "" | true | Query |
  146. // | prove | bool | false | false | Include proofs of the transactions inclusion in the block |
  147. //
  148. // ### Returns
  149. //
  150. // - `proof`: the `types.TxProof` object
  151. // - `tx`: `[]byte` - the transaction
  152. // - `tx_result`: the `abci.Result` object
  153. // - `index`: `int` - index of the transaction
  154. // - `height`: `int` - height of the block where this transaction was in
  155. func TxSearch(query string, prove bool) ([]*ctypes.ResultTx, error) {
  156. // if index is disabled, return error
  157. if _, ok := txIndexer.(*null.TxIndex); ok {
  158. return nil, fmt.Errorf("Transaction indexing is disabled.")
  159. }
  160. q, err := tmquery.New(query)
  161. if err != nil {
  162. return nil, err
  163. }
  164. results, err := txIndexer.Search(q)
  165. if err != nil {
  166. return nil, err
  167. }
  168. // TODO: we may want to consider putting a maximum on this length and somehow
  169. // informing the user that things were truncated.
  170. apiResults := make([]*ctypes.ResultTx, len(results))
  171. var proof types.TxProof
  172. for i, r := range results {
  173. height := r.Height
  174. index := r.Index
  175. if prove {
  176. block := blockStore.LoadBlock(height)
  177. // TODO: handle overflow
  178. proof = block.Data.Txs.Proof(int(index))
  179. }
  180. apiResults[i] = &ctypes.ResultTx{
  181. Height: height,
  182. Index: index,
  183. TxResult: r.Result,
  184. Tx: r.Tx,
  185. Proof: proof,
  186. }
  187. }
  188. return apiResults, nil
  189. }