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.

281 lines
8.1 KiB

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