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.

123 lines
2.9 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. package types
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. abci "github.com/tendermint/abci/types"
  7. "github.com/tendermint/go-wire/data"
  8. "github.com/tendermint/tmlibs/merkle"
  9. )
  10. // Tx represents a transaction, which may contain arbitrary bytes.
  11. type Tx []byte
  12. // Hash returns the hash of the go-wire encoded Tx.
  13. // Tx has no types at this level, so go-wire encoding only adds length-prefix.
  14. // NOTE: It may make sense to add types here one day and let []byte be type 0x1
  15. // so we can have versioned txs if need be in the future.
  16. func (tx Tx) Hash() []byte {
  17. return merkle.SimpleHashFromBinary(tx)
  18. }
  19. // String returns a string representation of the Tx.
  20. func (tx Tx) String() string {
  21. return fmt.Sprintf("Tx{%X}", []byte(tx))
  22. }
  23. // Txs is a slice of Tx.
  24. type Txs []Tx
  25. // Hash returns the simple Merkle root hash of the Txs.
  26. func (txs Txs) Hash() []byte {
  27. // Recursive impl.
  28. // Copied from tmlibs/merkle to avoid allocations
  29. switch len(txs) {
  30. case 0:
  31. return nil
  32. case 1:
  33. return txs[0].Hash()
  34. default:
  35. left := Txs(txs[:(len(txs)+1)/2]).Hash()
  36. right := Txs(txs[(len(txs)+1)/2:]).Hash()
  37. return merkle.SimpleHashFromTwoHashes(left, right)
  38. }
  39. }
  40. // Index returns the index of this transaction in the list, or -1 if not found
  41. func (txs Txs) Index(tx Tx) int {
  42. for i := range txs {
  43. if bytes.Equal(txs[i], tx) {
  44. return i
  45. }
  46. }
  47. return -1
  48. }
  49. // IndexByHash returns the index of this transaction hash in the list, or -1 if not found
  50. func (txs Txs) IndexByHash(hash []byte) int {
  51. for i := range txs {
  52. if bytes.Equal(txs[i].Hash(), hash) {
  53. return i
  54. }
  55. }
  56. return -1
  57. }
  58. // Proof returns a simple merkle proof for this node.
  59. //
  60. // Panics if i < 0 or i >= len(txs)
  61. //
  62. // TODO: optimize this!
  63. func (txs Txs) Proof(i int) TxProof {
  64. l := len(txs)
  65. hashables := make([]merkle.Hashable, l)
  66. for i := 0; i < l; i++ {
  67. hashables[i] = txs[i]
  68. }
  69. root, proofs := merkle.SimpleProofsFromHashables(hashables)
  70. return TxProof{
  71. Index: i,
  72. Total: l,
  73. RootHash: root,
  74. Data: txs[i],
  75. Proof: *proofs[i],
  76. }
  77. }
  78. type TxProof struct {
  79. Index, Total int
  80. RootHash data.Bytes
  81. Data Tx
  82. Proof merkle.SimpleProof
  83. }
  84. func (tp TxProof) LeafHash() []byte {
  85. return tp.Data.Hash()
  86. }
  87. // Validate returns nil if it matches the dataHash, and is internally consistent
  88. // otherwise, returns a sensible error
  89. func (tp TxProof) Validate(dataHash []byte) error {
  90. if !bytes.Equal(dataHash, tp.RootHash) {
  91. return errors.New("Proof matches different data hash")
  92. }
  93. valid := tp.Proof.Verify(tp.Index, tp.Total, tp.LeafHash(), tp.RootHash)
  94. if !valid {
  95. return errors.New("Proof is not internally consistent")
  96. }
  97. return nil
  98. }
  99. // TxResult contains results of executing the transaction.
  100. //
  101. // One usage is indexing transaction results.
  102. type TxResult struct {
  103. Height uint64 `json:"height"`
  104. Index uint32 `json:"index"`
  105. Tx Tx `json:"tx"`
  106. Result abci.ResponseDeliverTx `json:"result"`
  107. }