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.

147 lines
3.8 KiB

6 years ago
7 years ago
7 years ago
  1. package types
  2. import (
  3. "bytes"
  4. "crypto/sha256"
  5. "errors"
  6. "fmt"
  7. "github.com/tendermint/tendermint/crypto/merkle"
  8. "github.com/tendermint/tendermint/crypto/tmhash"
  9. tmbytes "github.com/tendermint/tendermint/libs/bytes"
  10. tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
  11. )
  12. // Tx is an arbitrary byte array.
  13. // NOTE: Tx has no types at this level, so when wire encoded it's just length-prefixed.
  14. // Might we want types here ?
  15. type Tx []byte
  16. // Key produces a fixed-length key for use in indexing.
  17. func (tx Tx) Key() TxKey { return sha256.Sum256(tx) }
  18. // Hash computes the TMHASH hash of the wire encoded transaction.
  19. func (tx Tx) Hash() []byte { return tmhash.Sum(tx) }
  20. // String returns the hex-encoded transaction as a string.
  21. func (tx Tx) String() string { return fmt.Sprintf("Tx{%X}", []byte(tx)) }
  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. }
  97. func (tp TxProof) ToProto() tmproto.TxProof {
  98. pbProof := tp.Proof.ToProto()
  99. pbtp := tmproto.TxProof{
  100. RootHash: tp.RootHash,
  101. Data: tp.Data,
  102. Proof: pbProof,
  103. }
  104. return pbtp
  105. }
  106. func TxProofFromProto(pb tmproto.TxProof) (TxProof, error) {
  107. pbProof, err := merkle.ProofFromProto(pb.Proof)
  108. if err != nil {
  109. return TxProof{}, err
  110. }
  111. pbtp := TxProof{
  112. RootHash: pb.RootHash,
  113. Data: pb.Data,
  114. Proof: *pbProof,
  115. }
  116. return pbtp, nil
  117. }
  118. // ComputeProtoSizeForTxs wraps the transactions in tmproto.Data{} and calculates the size.
  119. // https://developers.google.com/protocol-buffers/docs/encoding
  120. func ComputeProtoSizeForTxs(txs []Tx) int64 {
  121. data := Data{Txs: txs}
  122. pdData := data.ToProto()
  123. return int64(pdData.Size())
  124. }