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.

244 lines
6.8 KiB

  1. package types
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. "github.com/stretchr/testify/require"
  6. "github.com/tendermint/tendermint/crypto"
  7. cmn "github.com/tendermint/tendermint/libs/common"
  8. )
  9. func TestBlockAddEvidence(t *testing.T) {
  10. txs := []Tx{Tx("foo"), Tx("bar")}
  11. lastID := makeBlockIDRandom()
  12. h := int64(3)
  13. voteSet, valSet, vals := randVoteSet(h-1, 1, VoteTypePrecommit, 10, 1)
  14. commit, err := MakeCommit(lastID, h-1, 1, voteSet, vals)
  15. require.NoError(t, err)
  16. ev := NewMockGoodEvidence(h, 0, valSet.Validators[0].Address)
  17. evList := []Evidence{ev}
  18. block := MakeBlock(h, txs, commit, evList)
  19. require.NotNil(t, block)
  20. require.Equal(t, 1, len(block.Evidence.Evidence))
  21. require.NotNil(t, block.EvidenceHash)
  22. }
  23. func TestBlockValidateBasic(t *testing.T) {
  24. require.Error(t, (*Block)(nil).ValidateBasic())
  25. txs := []Tx{Tx("foo"), Tx("bar")}
  26. lastID := makeBlockIDRandom()
  27. h := int64(3)
  28. voteSet, valSet, vals := randVoteSet(h-1, 1, VoteTypePrecommit, 10, 1)
  29. commit, err := MakeCommit(lastID, h-1, 1, voteSet, vals)
  30. require.NoError(t, err)
  31. ev := NewMockGoodEvidence(h, 0, valSet.Validators[0].Address)
  32. evList := []Evidence{ev}
  33. block := MakeBlock(h, txs, commit, evList)
  34. require.NotNil(t, block)
  35. // proper block must pass
  36. err = block.ValidateBasic()
  37. require.NoError(t, err)
  38. // tamper with NumTxs
  39. block = MakeBlock(h, txs, commit, evList)
  40. block.NumTxs++
  41. err = block.ValidateBasic()
  42. require.Error(t, err)
  43. // remove 1/2 the commits
  44. block = MakeBlock(h, txs, commit, evList)
  45. block.LastCommit.Precommits = commit.Precommits[:commit.Size()/2]
  46. block.LastCommit.hash = nil // clear hash or change wont be noticed
  47. err = block.ValidateBasic()
  48. require.Error(t, err)
  49. // tamper with LastCommitHash
  50. block = MakeBlock(h, txs, commit, evList)
  51. block.LastCommitHash = []byte("something else")
  52. err = block.ValidateBasic()
  53. require.Error(t, err)
  54. // tamper with data
  55. block = MakeBlock(h, txs, commit, evList)
  56. block.Data.Txs[0] = Tx("something else")
  57. block.Data.hash = nil // clear hash or change wont be noticed
  58. err = block.ValidateBasic()
  59. require.Error(t, err)
  60. // tamper with DataHash
  61. block = MakeBlock(h, txs, commit, evList)
  62. block.DataHash = cmn.RandBytes(len(block.DataHash))
  63. err = block.ValidateBasic()
  64. require.Error(t, err)
  65. // tamper with evidence
  66. block = MakeBlock(h, txs, commit, evList)
  67. block.EvidenceHash = []byte("something else")
  68. err = block.ValidateBasic()
  69. require.Error(t, err)
  70. }
  71. func TestBlockHash(t *testing.T) {
  72. assert.Nil(t, (*Block)(nil).Hash())
  73. assert.Nil(t, MakeBlock(int64(3), []Tx{Tx("Hello World")}, nil, nil).Hash())
  74. }
  75. func TestBlockMakePartSet(t *testing.T) {
  76. assert.Nil(t, (*Block)(nil).MakePartSet(2))
  77. partSet := MakeBlock(int64(3), []Tx{Tx("Hello World")}, nil, nil).MakePartSet(1024)
  78. assert.NotNil(t, partSet)
  79. assert.Equal(t, 1, partSet.Total())
  80. }
  81. func TestBlockMakePartSetWithEvidence(t *testing.T) {
  82. assert.Nil(t, (*Block)(nil).MakePartSet(2))
  83. txs := []Tx{Tx("foo"), Tx("bar")}
  84. lastID := makeBlockIDRandom()
  85. h := int64(3)
  86. voteSet, valSet, vals := randVoteSet(h-1, 1, VoteTypePrecommit, 10, 1)
  87. commit, err := MakeCommit(lastID, h-1, 1, voteSet, vals)
  88. require.NoError(t, err)
  89. ev := NewMockGoodEvidence(h, 0, valSet.Validators[0].Address)
  90. evList := []Evidence{ev}
  91. partSet := MakeBlock(h, txs, commit, evList).MakePartSet(1024)
  92. assert.NotNil(t, partSet)
  93. assert.Equal(t, 3, partSet.Total())
  94. }
  95. func TestBlockHashesTo(t *testing.T) {
  96. assert.False(t, (*Block)(nil).HashesTo(nil))
  97. lastID := makeBlockIDRandom()
  98. h := int64(3)
  99. voteSet, valSet, vals := randVoteSet(h-1, 1, VoteTypePrecommit, 10, 1)
  100. commit, err := MakeCommit(lastID, h-1, 1, voteSet, vals)
  101. require.NoError(t, err)
  102. ev := NewMockGoodEvidence(h, 0, valSet.Validators[0].Address)
  103. evList := []Evidence{ev}
  104. block := MakeBlock(h, []Tx{Tx("Hello World")}, commit, evList)
  105. block.ValidatorsHash = valSet.Hash()
  106. assert.False(t, block.HashesTo([]byte{}))
  107. assert.False(t, block.HashesTo([]byte("something else")))
  108. assert.True(t, block.HashesTo(block.Hash()))
  109. }
  110. func TestBlockSize(t *testing.T) {
  111. size := MakeBlock(int64(3), []Tx{Tx("Hello World")}, nil, nil).Size()
  112. if size <= 0 {
  113. t.Fatal("Size of the block is zero or negative")
  114. }
  115. }
  116. func TestBlockString(t *testing.T) {
  117. assert.Equal(t, "nil-Block", (*Block)(nil).String())
  118. assert.Equal(t, "nil-Block", (*Block)(nil).StringIndented(""))
  119. assert.Equal(t, "nil-Block", (*Block)(nil).StringShort())
  120. block := MakeBlock(int64(3), []Tx{Tx("Hello World")}, nil, nil)
  121. assert.NotEqual(t, "nil-Block", block.String())
  122. assert.NotEqual(t, "nil-Block", block.StringIndented(""))
  123. assert.NotEqual(t, "nil-Block", block.StringShort())
  124. }
  125. func makeBlockIDRandom() BlockID {
  126. blockHash, blockPartsHeader := crypto.CRandBytes(32), PartSetHeader{123, crypto.CRandBytes(32)}
  127. return BlockID{blockHash, blockPartsHeader}
  128. }
  129. func makeBlockID(hash string, partSetSize int, partSetHash string) BlockID {
  130. return BlockID{
  131. Hash: []byte(hash),
  132. PartsHeader: PartSetHeader{
  133. Total: partSetSize,
  134. Hash: []byte(partSetHash),
  135. },
  136. }
  137. }
  138. var nilBytes []byte
  139. func TestNilHeaderHashDoesntCrash(t *testing.T) {
  140. assert.Equal(t, []byte((*Header)(nil).Hash()), nilBytes)
  141. assert.Equal(t, []byte((new(Header)).Hash()), nilBytes)
  142. }
  143. func TestNilDataHashDoesntCrash(t *testing.T) {
  144. assert.Equal(t, []byte((*Data)(nil).Hash()), nilBytes)
  145. assert.Equal(t, []byte(new(Data).Hash()), nilBytes)
  146. }
  147. func TestCommit(t *testing.T) {
  148. lastID := makeBlockIDRandom()
  149. h := int64(3)
  150. voteSet, _, vals := randVoteSet(h-1, 1, VoteTypePrecommit, 10, 1)
  151. commit, err := MakeCommit(lastID, h-1, 1, voteSet, vals)
  152. require.NoError(t, err)
  153. assert.NotNil(t, commit.FirstPrecommit())
  154. assert.Equal(t, h-1, commit.Height())
  155. assert.Equal(t, 1, commit.Round())
  156. assert.Equal(t, VoteTypePrecommit, commit.Type())
  157. if commit.Size() <= 0 {
  158. t.Fatalf("commit %v has a zero or negative size: %d", commit, commit.Size())
  159. }
  160. require.NotNil(t, commit.BitArray())
  161. assert.Equal(t, cmn.NewBitArray(10).Size(), commit.BitArray().Size())
  162. assert.Equal(t, voteSet.GetByIndex(0), commit.GetByIndex(0))
  163. assert.True(t, commit.IsCommit())
  164. }
  165. func TestCommitValidateBasic(t *testing.T) {
  166. commit := randCommit()
  167. assert.NoError(t, commit.ValidateBasic())
  168. // nil precommit is OK
  169. commit = randCommit()
  170. commit.Precommits[0] = nil
  171. assert.NoError(t, commit.ValidateBasic())
  172. // tamper with types
  173. commit = randCommit()
  174. commit.Precommits[0].Type = VoteTypePrevote
  175. assert.Error(t, commit.ValidateBasic())
  176. // tamper with height
  177. commit = randCommit()
  178. commit.Precommits[0].Height = int64(100)
  179. assert.Error(t, commit.ValidateBasic())
  180. // tamper with round
  181. commit = randCommit()
  182. commit.Precommits[0].Round = 100
  183. assert.Error(t, commit.ValidateBasic())
  184. }
  185. func randCommit() *Commit {
  186. lastID := makeBlockIDRandom()
  187. h := int64(3)
  188. voteSet, _, vals := randVoteSet(h-1, 1, VoteTypePrecommit, 10, 1)
  189. commit, err := MakeCommit(lastID, h-1, 1, voteSet, vals)
  190. if err != nil {
  191. panic(err)
  192. }
  193. return commit
  194. }