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.

178 lines
4.7 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. // TODO: handle overflow
  84. block := blockStore.LoadBlock(int(height))
  85. proof = block.Data.Txs.Proof(int(index))
  86. }
  87. return &ctypes.ResultTx{
  88. Height: height,
  89. Index: 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. //
  112. // Returns transactions matching the given query.
  113. //
  114. // ### Query Parameters
  115. //
  116. // | Parameter | Type | Default | Required | Description |
  117. // |-----------+--------+---------+----------+-----------------------------------------------------------|
  118. // | query | string | "" | true | Query |
  119. // | prove | bool | false | false | Include proofs of the transactions inclusion in the block |
  120. //
  121. // ### Returns
  122. //
  123. // - `proof`: the `types.TxProof` object
  124. // - `tx`: `[]byte` - the transaction
  125. // - `tx_result`: the `abci.Result` object
  126. // - `index`: `int` - index of the transaction
  127. // - `height`: `int` - height of the block where this transaction was in
  128. func TxSearch(query string, prove bool) ([]*ctypes.ResultTx, error) {
  129. // if index is disabled, return error
  130. if _, ok := txIndexer.(*null.TxIndex); ok {
  131. return nil, fmt.Errorf("Transaction indexing is disabled.")
  132. }
  133. q, err := tmquery.New(query)
  134. if err != nil {
  135. return nil, err
  136. }
  137. results, err := txIndexer.Search(q)
  138. if err != nil {
  139. return nil, err
  140. }
  141. // TODO: we may want to consider putting a maximum on this length and somehow
  142. // informing the user that things were truncated.
  143. apiResults := make([]*ctypes.ResultTx, len(results))
  144. var proof types.TxProof
  145. for i, r := range results {
  146. height := r.Height
  147. index := r.Index
  148. if prove {
  149. // TODO: handle overflow
  150. block := blockStore.LoadBlock(int(height))
  151. proof = block.Data.Txs.Proof(int(index))
  152. }
  153. apiResults[i] = &ctypes.ResultTx{
  154. Height: height,
  155. Index: index,
  156. TxResult: r.Result,
  157. Tx: r.Tx,
  158. Proof: proof,
  159. }
  160. }
  161. return apiResults, nil
  162. }