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.

203 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 := r.Index
  81. var proof types.TxProof
  82. if prove {
  83. block := blockStore.LoadBlock(height)
  84. proof = block.Data.Txs.Proof(int(index)) // XXX: overflow on 32-bit machines
  85. }
  86. return &ctypes.ResultTx{
  87. Height: height,
  88. Index: uint32(index),
  89. TxResult: r.Result,
  90. Tx: r.Tx,
  91. Proof: proof,
  92. }, nil
  93. }
  94. // TxSearch allows you to query for multiple transactions results.
  95. //
  96. // ```shell
  97. // curl "localhost:46657/tx_search?query=\"account.owner='Ivan'\"&prove=true"
  98. // ```
  99. //
  100. // ```go
  101. // client := client.NewHTTP("tcp://0.0.0.0:46657", "/websocket")
  102. // q, err := tmquery.New("account.owner='Ivan'")
  103. // tx, err := client.TxSearch(q, true)
  104. // ```
  105. //
  106. // > The above command returns JSON structured like this:
  107. //
  108. // ```json
  109. // {
  110. // "result": [
  111. // {
  112. // "proof": {
  113. // "Proof": {
  114. // "aunts": [
  115. // "J3LHbizt806uKnABNLwG4l7gXCA=",
  116. // "iblMO/M1TnNtlAefJyNCeVhjAb0=",
  117. // "iVk3ryurVaEEhdeS0ohAJZ3wtB8=",
  118. // "5hqMkTeGqpct51ohX0lZLIdsn7Q=",
  119. // "afhsNxFnLlZgFDoyPpdQSe0bR8g="
  120. // ]
  121. // },
  122. // "Data": "mvZHHa7HhZ4aRT0xMDA=",
  123. // "RootHash": "F6541223AA46E428CB1070E9840D2C3DF3B6D776",
  124. // "Total": 32,
  125. // "Index": 31
  126. // },
  127. // "tx": "mvZHHa7HhZ4aRT0xMDA=",
  128. // "tx_result": {},
  129. // "index": 31,
  130. // "height": 12
  131. // }
  132. // ],
  133. // "id": "",
  134. // "jsonrpc": "2.0"
  135. // }
  136. // ```
  137. //
  138. // Returns transactions matching the given query.
  139. //
  140. // ### Query Parameters
  141. //
  142. // | Parameter | Type | Default | Required | Description |
  143. // |-----------+--------+---------+----------+-----------------------------------------------------------|
  144. // | query | string | "" | true | Query |
  145. // | prove | bool | false | false | Include proofs of the transactions inclusion in the block |
  146. //
  147. // ### Returns
  148. //
  149. // - `proof`: the `types.TxProof` object
  150. // - `tx`: `[]byte` - the transaction
  151. // - `tx_result`: the `abci.Result` object
  152. // - `index`: `int` - index of the transaction
  153. // - `height`: `int` - height of the block where this transaction was in
  154. func TxSearch(query string, prove bool) ([]*ctypes.ResultTx, error) {
  155. // if index is disabled, return error
  156. if _, ok := txIndexer.(*null.TxIndex); ok {
  157. return nil, fmt.Errorf("Transaction indexing is disabled.")
  158. }
  159. q, err := tmquery.New(query)
  160. if err != nil {
  161. return nil, err
  162. }
  163. results, err := txIndexer.Search(q)
  164. if err != nil {
  165. return nil, err
  166. }
  167. // TODO: we may want to consider putting a maximum on this length and somehow
  168. // informing the user that things were truncated.
  169. apiResults := make([]*ctypes.ResultTx, len(results))
  170. var proof types.TxProof
  171. for i, r := range results {
  172. height := r.Height
  173. index := r.Index
  174. if prove {
  175. block := blockStore.LoadBlock(height)
  176. proof = block.Data.Txs.Proof(int(index)) // XXX: overflow on 32-bit machines
  177. }
  178. apiResults[i] = &ctypes.ResultTx{
  179. Height: height,
  180. Index: index,
  181. TxResult: r.Result,
  182. Tx: r.Tx,
  183. Proof: proof,
  184. }
  185. }
  186. return apiResults, nil
  187. }