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.

128 lines
3.2 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/tendermint/abci/types"
  7. "github.com/tendermint/tendermint/crypto/merkle"
  8. "github.com/tendermint/tendermint/crypto/tmhash"
  9. cmn "github.com/tendermint/tendermint/libs/common"
  10. )
  11. // Tx is an arbitrary byte array.
  12. // NOTE: Tx has no types at this level, so when wire encoded it's just length-prefixed.
  13. // Might we want types here ?
  14. type Tx []byte
  15. // Hash computes the TMHASH hash of the wire encoded transaction.
  16. func (tx Tx) Hash() []byte {
  17. return tmhash.Sum(tx)
  18. }
  19. // String returns the hex-encoded transaction as a string.
  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 transactions.
  26. func (txs Txs) Hash() []byte {
  27. // Recursive impl.
  28. // Copied from tendermint/crypto/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. // Panics if i < 0 or i >= len(txs)
  60. // TODO: optimize this!
  61. func (txs Txs) Proof(i int) TxProof {
  62. l := len(txs)
  63. hashers := make([]merkle.Hasher, l)
  64. for i := 0; i < l; i++ {
  65. hashers[i] = txs[i]
  66. }
  67. root, proofs := merkle.SimpleProofsFromHashers(hashers)
  68. return TxProof{
  69. Index: i,
  70. Total: l,
  71. RootHash: root,
  72. Data: txs[i],
  73. Proof: *proofs[i],
  74. }
  75. }
  76. // TxProof represents a Merkle proof of the presence of a transaction in the Merkle tree.
  77. type TxProof struct {
  78. Index, Total int
  79. RootHash cmn.HexBytes
  80. Data Tx
  81. Proof merkle.SimpleProof
  82. }
  83. // LeadHash returns the hash of the this proof refers to.
  84. func (tp TxProof) LeafHash() []byte {
  85. return tp.Data.Hash()
  86. }
  87. // Validate verifies the proof. It returns nil if the RootHash matches the dataHash argument,
  88. // and if the proof is internally consistent. Otherwise, it 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. if tp.Index < 0 {
  94. return errors.New("Proof index cannot be negative")
  95. }
  96. if tp.Total <= 0 {
  97. return errors.New("Proof total must be positive")
  98. }
  99. valid := tp.Proof.Verify(tp.Index, tp.Total, tp.LeafHash(), tp.RootHash)
  100. if !valid {
  101. return errors.New("Proof is not internally consistent")
  102. }
  103. return nil
  104. }
  105. // TxResult contains results of executing the transaction.
  106. //
  107. // One usage is indexing transaction results.
  108. type TxResult struct {
  109. Height int64 `json:"height"`
  110. Index uint32 `json:"index"`
  111. Tx Tx `json:"tx"`
  112. Result abci.ResponseDeliverTx `json:"result"`
  113. }