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.

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