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.

190 lines
4.9 KiB

  1. package types
  2. import (
  3. "bytes"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. tmrand "github.com/tendermint/tendermint/libs/rand"
  7. ctest "github.com/tendermint/tendermint/libs/test"
  8. )
  9. func makeTxs(cnt, size int) Txs {
  10. txs := make(Txs, cnt)
  11. for i := 0; i < cnt; i++ {
  12. txs[i] = tmrand.Bytes(size)
  13. }
  14. return txs
  15. }
  16. func randInt(low, high int) int {
  17. off := tmrand.Int() % (high - low)
  18. return low + off
  19. }
  20. func TestTxIndex(t *testing.T) {
  21. for i := 0; i < 20; i++ {
  22. txs := makeTxs(15, 60)
  23. for j := 0; j < len(txs); j++ {
  24. tx := txs[j]
  25. idx := txs.Index(tx)
  26. assert.Equal(t, j, idx)
  27. }
  28. assert.Equal(t, -1, txs.Index(nil))
  29. assert.Equal(t, -1, txs.Index(Tx("foodnwkf")))
  30. }
  31. }
  32. func TestTxIndexByHash(t *testing.T) {
  33. for i := 0; i < 20; i++ {
  34. txs := makeTxs(15, 60)
  35. for j := 0; j < len(txs); j++ {
  36. tx := txs[j]
  37. idx := txs.IndexByHash(tx.Hash())
  38. assert.Equal(t, j, idx)
  39. }
  40. assert.Equal(t, -1, txs.IndexByHash(nil))
  41. assert.Equal(t, -1, txs.IndexByHash(Tx("foodnwkf").Hash()))
  42. }
  43. }
  44. func TestValidTxProof(t *testing.T) {
  45. cases := []struct {
  46. txs Txs
  47. }{
  48. {Txs{{1, 4, 34, 87, 163, 1}}},
  49. {Txs{{5, 56, 165, 2}, {4, 77}}},
  50. {Txs{Tx("foo"), Tx("bar"), Tx("baz")}},
  51. {makeTxs(20, 5)},
  52. {makeTxs(7, 81)},
  53. {makeTxs(61, 15)},
  54. }
  55. for h, tc := range cases {
  56. txs := tc.txs
  57. root := txs.Hash()
  58. // make sure valid proof for every tx
  59. for i := range txs {
  60. tx := []byte(txs[i])
  61. proof := txs.Proof(i)
  62. assert.Equal(t, i, proof.Proof.Index, "%d: %d", h, i)
  63. assert.Equal(t, len(txs), proof.Proof.Total, "%d: %d", h, i)
  64. assert.EqualValues(t, root, proof.RootHash, "%d: %d", h, i)
  65. assert.EqualValues(t, tx, proof.Data, "%d: %d", h, i)
  66. assert.EqualValues(t, txs[i].Hash(), proof.Leaf(), "%d: %d", h, i)
  67. assert.Nil(t, proof.Validate(root), "%d: %d", h, i)
  68. assert.NotNil(t, proof.Validate([]byte("foobar")), "%d: %d", h, i)
  69. // read-write must also work
  70. var p2 TxProof
  71. bin, err := cdc.MarshalBinaryLengthPrefixed(proof)
  72. assert.Nil(t, err)
  73. err = cdc.UnmarshalBinaryLengthPrefixed(bin, &p2)
  74. if assert.Nil(t, err, "%d: %d: %+v", h, i, err) {
  75. assert.Nil(t, p2.Validate(root), "%d: %d", h, i)
  76. }
  77. }
  78. }
  79. }
  80. func TestTxProofUnchangable(t *testing.T) {
  81. // run the other test a bunch...
  82. for i := 0; i < 40; i++ {
  83. testTxProofUnchangable(t)
  84. }
  85. }
  86. func TestComputeTxsOverhead(t *testing.T) {
  87. cases := []struct {
  88. txs Txs
  89. wantOverhead int
  90. }{
  91. {Txs{[]byte{6, 6, 6, 6, 6, 6}}, 2},
  92. // one 21 Mb transaction:
  93. {Txs{make([]byte, 22020096)}, 5},
  94. // two 21Mb/2 sized transactions:
  95. {Txs{make([]byte, 11010048), make([]byte, 11010048)}, 10},
  96. {Txs{[]byte{1, 2, 3}, []byte{1, 2, 3}, []byte{4, 5, 6}}, 6},
  97. {Txs{[]byte{100, 5, 64}, []byte{42, 116, 118}, []byte{6, 6, 6}, []byte{6, 6, 6}}, 8},
  98. }
  99. for _, tc := range cases {
  100. totalBytes := int64(0)
  101. totalOverhead := int64(0)
  102. for _, tx := range tc.txs {
  103. aminoOverhead := ComputeAminoOverhead(tx, 1)
  104. totalOverhead += aminoOverhead
  105. totalBytes += aminoOverhead + int64(len(tx))
  106. }
  107. bz, err := cdc.MarshalBinaryBare(tc.txs)
  108. assert.EqualValues(t, tc.wantOverhead, totalOverhead)
  109. assert.NoError(t, err)
  110. assert.EqualValues(t, len(bz), totalBytes)
  111. }
  112. }
  113. func TestComputeAminoOverhead(t *testing.T) {
  114. cases := []struct {
  115. tx Tx
  116. fieldNum int
  117. want int
  118. }{
  119. {[]byte{6, 6, 6}, 1, 2},
  120. {[]byte{6, 6, 6}, 16, 3},
  121. {[]byte{6, 6, 6}, 32, 3},
  122. {[]byte{6, 6, 6}, 64, 3},
  123. {[]byte{6, 6, 6}, 512, 3},
  124. {[]byte{6, 6, 6}, 1024, 3},
  125. {[]byte{6, 6, 6}, 2048, 4},
  126. {make([]byte, 64), 1, 2},
  127. {make([]byte, 65), 1, 2},
  128. {make([]byte, 127), 1, 2},
  129. {make([]byte, 128), 1, 3},
  130. {make([]byte, 256), 1, 3},
  131. {make([]byte, 512), 1, 3},
  132. {make([]byte, 1024), 1, 3},
  133. {make([]byte, 128), 16, 4},
  134. }
  135. for _, tc := range cases {
  136. got := ComputeAminoOverhead(tc.tx, tc.fieldNum)
  137. assert.EqualValues(t, tc.want, got)
  138. }
  139. }
  140. func testTxProofUnchangable(t *testing.T) {
  141. // make some proof
  142. txs := makeTxs(randInt(2, 100), randInt(16, 128))
  143. root := txs.Hash()
  144. i := randInt(0, len(txs)-1)
  145. proof := txs.Proof(i)
  146. // make sure it is valid to start with
  147. assert.Nil(t, proof.Validate(root))
  148. bin, err := cdc.MarshalBinaryLengthPrefixed(proof)
  149. assert.Nil(t, err)
  150. // try mutating the data and make sure nothing breaks
  151. for j := 0; j < 500; j++ {
  152. bad := ctest.MutateByteSlice(bin)
  153. if !bytes.Equal(bad, bin) {
  154. assertBadProof(t, root, bad, proof)
  155. }
  156. }
  157. }
  158. // This makes sure that the proof doesn't deserialize into something valid.
  159. func assertBadProof(t *testing.T, root []byte, bad []byte, good TxProof) {
  160. var proof TxProof
  161. err := cdc.UnmarshalBinaryLengthPrefixed(bad, &proof)
  162. if err == nil {
  163. err = proof.Validate(root)
  164. if err == nil {
  165. // XXX Fix simple merkle proofs so the following is *not* OK.
  166. // This can happen if we have a slightly different total (where the
  167. // path ends up the same). If it is something else, we have a real
  168. // problem.
  169. assert.NotEqual(t, proof.Proof.Total, good.Proof.Total, "bad: %#v\ngood: %#v", proof, good)
  170. }
  171. }
  172. }