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.

110 lines
2.9 KiB

7 years ago
7 years ago
7 years ago
  1. package types
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. "github.com/tendermint/tendermint/crypto/merkle"
  7. "github.com/tendermint/tendermint/crypto/tmhash"
  8. tmbytes "github.com/tendermint/tendermint/libs/bytes"
  9. )
  10. // Tx is an arbitrary byte array.
  11. // NOTE: Tx has no types at this level, so when wire encoded it's just length-prefixed.
  12. // Might we want types here ?
  13. type Tx []byte
  14. // Hash computes the TMHASH hash of the wire encoded transaction.
  15. func (tx Tx) Hash() []byte {
  16. return tmhash.Sum(tx)
  17. }
  18. // String returns the hex-encoded transaction as a string.
  19. func (tx Tx) String() string {
  20. return fmt.Sprintf("Tx{%X}", []byte(tx))
  21. }
  22. // Txs is a slice of Tx.
  23. type Txs []Tx
  24. // Hash returns the Merkle root hash of the transaction hashes.
  25. // i.e. the leaves of the tree are the hashes of the txs.
  26. func (txs Txs) Hash() []byte {
  27. // These allocations will be removed once Txs is switched to [][]byte,
  28. // ref #2603. This is because golang does not allow type casting slices without unsafe
  29. txBzs := make([][]byte, len(txs))
  30. for i := 0; i < len(txs); i++ {
  31. txBzs[i] = txs[i].Hash()
  32. }
  33. return merkle.HashFromByteSlices(txBzs)
  34. }
  35. // Index returns the index of this transaction in the list, or -1 if not found
  36. func (txs Txs) Index(tx Tx) int {
  37. for i := range txs {
  38. if bytes.Equal(txs[i], tx) {
  39. return i
  40. }
  41. }
  42. return -1
  43. }
  44. // IndexByHash returns the index of this transaction hash in the list, or -1 if not found
  45. func (txs Txs) IndexByHash(hash []byte) int {
  46. for i := range txs {
  47. if bytes.Equal(txs[i].Hash(), hash) {
  48. return i
  49. }
  50. }
  51. return -1
  52. }
  53. // Proof returns a simple merkle proof for this node.
  54. // Panics if i < 0 or i >= len(txs)
  55. // TODO: optimize this!
  56. func (txs Txs) Proof(i int) TxProof {
  57. l := len(txs)
  58. bzs := make([][]byte, l)
  59. for i := 0; i < l; i++ {
  60. bzs[i] = txs[i].Hash()
  61. }
  62. root, proofs := merkle.ProofsFromByteSlices(bzs)
  63. return TxProof{
  64. RootHash: root,
  65. Data: txs[i],
  66. Proof: *proofs[i],
  67. }
  68. }
  69. // TxProof represents a Merkle proof of the presence of a transaction in the Merkle tree.
  70. type TxProof struct {
  71. RootHash tmbytes.HexBytes `json:"root_hash"`
  72. Data Tx `json:"data"`
  73. Proof merkle.Proof `json:"proof"`
  74. }
  75. // Leaf returns the hash(tx), which is the leaf in the merkle tree which this proof refers to.
  76. func (tp TxProof) Leaf() []byte {
  77. return tp.Data.Hash()
  78. }
  79. // Validate verifies the proof. It returns nil if the RootHash matches the dataHash argument,
  80. // and if the proof is internally consistent. Otherwise, it returns a sensible error.
  81. func (tp TxProof) Validate(dataHash []byte) error {
  82. if !bytes.Equal(dataHash, tp.RootHash) {
  83. return errors.New("proof matches different data hash")
  84. }
  85. if tp.Proof.Index < 0 {
  86. return errors.New("proof index cannot be negative")
  87. }
  88. if tp.Proof.Total <= 0 {
  89. return errors.New("proof total must be positive")
  90. }
  91. valid := tp.Proof.Verify(tp.RootHash, tp.Leaf())
  92. if valid != nil {
  93. return errors.New("proof is not internally consistent")
  94. }
  95. return nil
  96. }