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.

229 lines
6.4 KiB

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