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.

217 lines
5.9 KiB

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