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.

137 lines
4.0 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/go-amino"
  7. abci "github.com/tendermint/tendermint/abci/types"
  8. "github.com/tendermint/tendermint/crypto/merkle"
  9. "github.com/tendermint/tendermint/crypto/tmhash"
  10. cmn "github.com/tendermint/tendermint/libs/common"
  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. // Hash computes the TMHASH hash of the wire encoded transaction.
  17. func (tx Tx) Hash() []byte {
  18. return tmhash.Sum(tx)
  19. }
  20. // String returns the hex-encoded transaction as a string.
  21. func (tx Tx) String() string {
  22. return fmt.Sprintf("Tx{%X}", []byte(tx))
  23. }
  24. // Txs is a slice of Tx.
  25. type Txs []Tx
  26. // Hash returns the simple Merkle root hash of the transactions.
  27. func (txs Txs) Hash() []byte {
  28. // These allocations will be removed once Txs is switched to [][]byte,
  29. // ref #2603. This is because golang does not allow type casting slices without unsafe
  30. txBzs := make([][]byte, len(txs))
  31. for i := 0; i < len(txs); i++ {
  32. txBzs[i] = txs[i]
  33. }
  34. return merkle.SimpleHashFromByteSlices(txBzs)
  35. }
  36. // Index returns the index of this transaction in the list, or -1 if not found
  37. func (txs Txs) Index(tx Tx) int {
  38. for i := range txs {
  39. if bytes.Equal(txs[i], tx) {
  40. return i
  41. }
  42. }
  43. return -1
  44. }
  45. // IndexByHash returns the index of this transaction hash in the list, or -1 if not found
  46. func (txs Txs) IndexByHash(hash []byte) int {
  47. for i := range txs {
  48. if bytes.Equal(txs[i].Hash(), hash) {
  49. return i
  50. }
  51. }
  52. return -1
  53. }
  54. // Proof returns a simple merkle proof for this node.
  55. // Panics if i < 0 or i >= len(txs)
  56. // TODO: optimize this!
  57. func (txs Txs) Proof(i int) TxProof {
  58. l := len(txs)
  59. bzs := make([][]byte, l)
  60. for i := 0; i < l; i++ {
  61. bzs[i] = txs[i]
  62. }
  63. root, proofs := merkle.SimpleProofsFromByteSlices(bzs)
  64. return TxProof{
  65. RootHash: root,
  66. Data: txs[i],
  67. Proof: *proofs[i],
  68. }
  69. }
  70. // TxProof represents a Merkle proof of the presence of a transaction in the Merkle tree.
  71. type TxProof struct {
  72. RootHash cmn.HexBytes
  73. Data Tx
  74. Proof merkle.SimpleProof
  75. }
  76. // LeadHash returns the hash of the this proof refers to.
  77. func (tp TxProof) LeafHash() []byte {
  78. return tp.Data.Hash()
  79. }
  80. // Validate verifies the proof. It returns nil if the RootHash matches the dataHash argument,
  81. // and if the proof is internally consistent. Otherwise, it returns a sensible error.
  82. func (tp TxProof) Validate(dataHash []byte) error {
  83. if !bytes.Equal(dataHash, tp.RootHash) {
  84. return errors.New("Proof matches different data hash")
  85. }
  86. if tp.Proof.Index < 0 {
  87. return errors.New("Proof index cannot be negative")
  88. }
  89. if tp.Proof.Total <= 0 {
  90. return errors.New("Proof total must be positive")
  91. }
  92. valid := tp.Proof.Verify(tp.RootHash, tp.LeafHash())
  93. if valid != nil {
  94. return errors.New("Proof is not internally consistent")
  95. }
  96. return nil
  97. }
  98. // TxResult contains results of executing the transaction.
  99. //
  100. // One usage is indexing transaction results.
  101. type TxResult struct {
  102. Height int64 `json:"height"`
  103. Index uint32 `json:"index"`
  104. Tx Tx `json:"tx"`
  105. Result abci.ResponseDeliverTx `json:"result"`
  106. }
  107. // ComputeAminoOverhead calculates the overhead for amino encoding a transaction.
  108. // The overhead consists of varint encoding the field number and the wire type
  109. // (= length-delimited = 2), and another varint encoding the length of the
  110. // transaction.
  111. // The field number can be the field number of the particular transaction, or
  112. // the field number of the parenting struct that contains the transactions []Tx
  113. // as a field (this field number is repeated for each contained Tx).
  114. // If some []Tx are encoded directly (without a parenting struct), the default
  115. // fieldNum is also 1 (see BinFieldNum in amino.MarshalBinaryBare).
  116. func ComputeAminoOverhead(tx Tx, fieldNum int) int64 {
  117. fnum := uint64(fieldNum)
  118. typ3AndFieldNum := (uint64(fnum) << 3) | uint64(amino.Typ3_ByteLength)
  119. return int64(amino.UvarintSize(typ3AndFieldNum)) + int64(amino.UvarintSize(uint64(len(tx))))
  120. }