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.

125 lines
3.1 KiB

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. bzs := make([][]byte, l)
  64. for i := 0; i < l; i++ {
  65. bzs[i] = txs[i]
  66. }
  67. root, proofs := merkle.SimpleProofsFromByteSlices(bzs)
  68. return TxProof{
  69. RootHash: root,
  70. Data: txs[i],
  71. Proof: *proofs[i],
  72. }
  73. }
  74. // TxProof represents a Merkle proof of the presence of a transaction in the Merkle tree.
  75. type TxProof struct {
  76. RootHash cmn.HexBytes
  77. Data Tx
  78. Proof merkle.SimpleProof
  79. }
  80. // LeadHash returns the hash of the this proof refers to.
  81. func (tp TxProof) LeafHash() []byte {
  82. return tp.Data.Hash()
  83. }
  84. // Validate verifies the proof. It returns nil if the RootHash matches the dataHash argument,
  85. // and if the proof is internally consistent. Otherwise, it returns a sensible error.
  86. func (tp TxProof) Validate(dataHash []byte) error {
  87. if !bytes.Equal(dataHash, tp.RootHash) {
  88. return errors.New("Proof matches different data hash")
  89. }
  90. if tp.Proof.Index < 0 {
  91. return errors.New("Proof index cannot be negative")
  92. }
  93. if tp.Proof.Total <= 0 {
  94. return errors.New("Proof total must be positive")
  95. }
  96. valid := tp.Proof.Verify(tp.RootHash, tp.LeafHash())
  97. if valid != nil {
  98. return errors.New("Proof is not internally consistent")
  99. }
  100. return nil
  101. }
  102. // TxResult contains results of executing the transaction.
  103. //
  104. // One usage is indexing transaction results.
  105. type TxResult struct {
  106. Height int64 `json:"height"`
  107. Index uint32 `json:"index"`
  108. Tx Tx `json:"tx"`
  109. Result abci.ResponseDeliverTx `json:"result"`
  110. }