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.

101 lines
2.5 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. )
  8. // Tx allows you to query the transaction results. `nil` could mean the
  9. // transaction is in the mempool, invalidated, or was not sent in the first
  10. // place.
  11. //
  12. // ```shell
  13. // curl "localhost:46657/tx?hash=0x2B8EC32BA2579B3B8606E42C06DE2F7AFA2556EF"
  14. // ```
  15. //
  16. // ```go
  17. // client := client.NewHTTP("tcp://0.0.0.0:46657", "/websocket")
  18. // tx, err := client.Tx([]byte("2B8EC32BA2579B3B8606E42C06DE2F7AFA2556EF"), true)
  19. // ```
  20. //
  21. // > The above command returns JSON structured like this:
  22. //
  23. // ```json
  24. // {
  25. // "error": "",
  26. // "result": {
  27. // "proof": {
  28. // "Proof": {
  29. // "aunts": []
  30. // },
  31. // "Data": "YWJjZA==",
  32. // "RootHash": "2B8EC32BA2579B3B8606E42C06DE2F7AFA2556EF",
  33. // "Total": 1,
  34. // "Index": 0
  35. // },
  36. // "tx": "YWJjZA==",
  37. // "tx_result": {
  38. // "log": "",
  39. // "data": "",
  40. // "code": 0
  41. // },
  42. // "index": 0,
  43. // "height": 52
  44. // },
  45. // "id": "",
  46. // "jsonrpc": "2.0"
  47. // }
  48. // ```
  49. //
  50. // Returns a transaction matching the given transaction hash.
  51. //
  52. // ### Query Parameters
  53. //
  54. // | Parameter | Type | Default | Required | Description |
  55. // |-----------+--------+---------+----------+-----------------------------------------------------------|
  56. // | hash | []byte | nil | true | The transaction hash |
  57. // | prove | bool | false | false | Include a proof of the transaction inclusion in the block |
  58. //
  59. // ### Returns
  60. //
  61. // - `proof`: the `types.TxProof` object
  62. // - `tx`: `[]byte` - the transaction
  63. // - `tx_result`: the `abci.Result` object
  64. // - `index`: `int` - index of the transaction
  65. // - `height`: `int` - height of the block where this transaction was in
  66. func Tx(hash []byte, prove bool) (*ctypes.ResultTx, error) {
  67. // if index is disabled, return error
  68. if _, ok := txIndexer.(*null.TxIndex); ok {
  69. return nil, fmt.Errorf("Transaction indexing is disabled.")
  70. }
  71. r, err := txIndexer.Get(hash)
  72. if err != nil {
  73. return nil, err
  74. }
  75. if r == nil {
  76. return nil, fmt.Errorf("Tx (%X) not found", hash)
  77. }
  78. height := int(r.Height) // XXX
  79. index := int(r.Index)
  80. var proof types.TxProof
  81. if prove {
  82. block := blockStore.LoadBlock(height)
  83. proof = block.Data.Txs.Proof(index)
  84. }
  85. return &ctypes.ResultTx{
  86. Height: height,
  87. Index: index,
  88. TxResult: r.Result.Result(),
  89. Tx: r.Tx,
  90. Proof: proof,
  91. }, nil
  92. }