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.2 KiB

max-bytes PR follow-up (#2318) * ReapMaxTxs: return all txs if max is negative this mirrors ReapMaxBytes behavior See https://github.com/tendermint/tendermint/pull/2184#discussion_r214439950 * increase MaxAminoOverheadForBlock tested with: ``` func TestMaxAminoOverheadForBlock(t *testing.T) { maxChainID := "" for i := 0; i < MaxChainIDLen; i++ { maxChainID += "𠜎" } h := Header{ ChainID: maxChainID, Height: 10, Time: time.Now().UTC(), NumTxs: 100, TotalTxs: 200, LastBlockID: makeBlockID(make([]byte, 20), 300, make([]byte, 20)), LastCommitHash: tmhash.Sum([]byte("last_commit_hash")), DataHash: tmhash.Sum([]byte("data_hash")), ValidatorsHash: tmhash.Sum([]byte("validators_hash")), NextValidatorsHash: tmhash.Sum([]byte("next_validators_hash")), ConsensusHash: tmhash.Sum([]byte("consensus_hash")), AppHash: tmhash.Sum([]byte("app_hash")), LastResultsHash: tmhash.Sum([]byte("last_results_hash")), EvidenceHash: tmhash.Sum([]byte("evidence_hash")), ProposerAddress: tmhash.Sum([]byte("proposer_address")), } b := Block{ Header: h, Data: Data{Txs: makeTxs(10000, 100)}, Evidence: EvidenceData{}, LastCommit: &Commit{}, } bz, err := cdc.MarshalBinary(b) require.NoError(t, err) assert.Equal(t, MaxHeaderBytes+MaxAminoOverheadForBlock-2, len(bz)-1000000-20000-1) } ``` * fix MaxYYY constants calculation by using math.MaxInt64 See https://github.com/tendermint/tendermint/pull/2184#discussion_r214444244 * pass mempool filter as an option See https://github.com/tendermint/tendermint/pull/2184#discussion_r214445869 * fixes after Dev's comments
6 years ago
max-bytes PR follow-up (#2318) * ReapMaxTxs: return all txs if max is negative this mirrors ReapMaxBytes behavior See https://github.com/tendermint/tendermint/pull/2184#discussion_r214439950 * increase MaxAminoOverheadForBlock tested with: ``` func TestMaxAminoOverheadForBlock(t *testing.T) { maxChainID := "" for i := 0; i < MaxChainIDLen; i++ { maxChainID += "𠜎" } h := Header{ ChainID: maxChainID, Height: 10, Time: time.Now().UTC(), NumTxs: 100, TotalTxs: 200, LastBlockID: makeBlockID(make([]byte, 20), 300, make([]byte, 20)), LastCommitHash: tmhash.Sum([]byte("last_commit_hash")), DataHash: tmhash.Sum([]byte("data_hash")), ValidatorsHash: tmhash.Sum([]byte("validators_hash")), NextValidatorsHash: tmhash.Sum([]byte("next_validators_hash")), ConsensusHash: tmhash.Sum([]byte("consensus_hash")), AppHash: tmhash.Sum([]byte("app_hash")), LastResultsHash: tmhash.Sum([]byte("last_results_hash")), EvidenceHash: tmhash.Sum([]byte("evidence_hash")), ProposerAddress: tmhash.Sum([]byte("proposer_address")), } b := Block{ Header: h, Data: Data{Txs: makeTxs(10000, 100)}, Evidence: EvidenceData{}, LastCommit: &Commit{}, } bz, err := cdc.MarshalBinary(b) require.NoError(t, err) assert.Equal(t, MaxHeaderBytes+MaxAminoOverheadForBlock-2, len(bz)-1000000-20000-1) } ``` * fix MaxYYY constants calculation by using math.MaxInt64 See https://github.com/tendermint/tendermint/pull/2184#discussion_r214444244 * pass mempool filter as an option See https://github.com/tendermint/tendermint/pull/2184#discussion_r214445869 * fixes after Dev's comments
6 years ago
max-bytes PR follow-up (#2318) * ReapMaxTxs: return all txs if max is negative this mirrors ReapMaxBytes behavior See https://github.com/tendermint/tendermint/pull/2184#discussion_r214439950 * increase MaxAminoOverheadForBlock tested with: ``` func TestMaxAminoOverheadForBlock(t *testing.T) { maxChainID := "" for i := 0; i < MaxChainIDLen; i++ { maxChainID += "𠜎" } h := Header{ ChainID: maxChainID, Height: 10, Time: time.Now().UTC(), NumTxs: 100, TotalTxs: 200, LastBlockID: makeBlockID(make([]byte, 20), 300, make([]byte, 20)), LastCommitHash: tmhash.Sum([]byte("last_commit_hash")), DataHash: tmhash.Sum([]byte("data_hash")), ValidatorsHash: tmhash.Sum([]byte("validators_hash")), NextValidatorsHash: tmhash.Sum([]byte("next_validators_hash")), ConsensusHash: tmhash.Sum([]byte("consensus_hash")), AppHash: tmhash.Sum([]byte("app_hash")), LastResultsHash: tmhash.Sum([]byte("last_results_hash")), EvidenceHash: tmhash.Sum([]byte("evidence_hash")), ProposerAddress: tmhash.Sum([]byte("proposer_address")), } b := Block{ Header: h, Data: Data{Txs: makeTxs(10000, 100)}, Evidence: EvidenceData{}, LastCommit: &Commit{}, } bz, err := cdc.MarshalBinary(b) require.NoError(t, err) assert.Equal(t, MaxHeaderBytes+MaxAminoOverheadForBlock-2, len(bz)-1000000-20000-1) } ``` * fix MaxYYY constants calculation by using math.MaxInt64 See https://github.com/tendermint/tendermint/pull/2184#discussion_r214444244 * pass mempool filter as an option See https://github.com/tendermint/tendermint/pull/2184#discussion_r214445869 * fixes after Dev's comments
6 years ago
  1. package types
  2. import (
  3. "math"
  4. "testing"
  5. "time"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. "github.com/tendermint/tendermint/crypto"
  9. "github.com/tendermint/tendermint/crypto/tmhash"
  10. cmn "github.com/tendermint/tendermint/libs/common"
  11. )
  12. func TestBlockAddEvidence(t *testing.T) {
  13. txs := []Tx{Tx("foo"), Tx("bar")}
  14. lastID := makeBlockIDRandom()
  15. h := int64(3)
  16. voteSet, valSet, vals := randVoteSet(h-1, 1, VoteTypePrecommit, 10, 1)
  17. commit, err := MakeCommit(lastID, h-1, 1, voteSet, vals)
  18. require.NoError(t, err)
  19. ev := NewMockGoodEvidence(h, 0, valSet.Validators[0].Address)
  20. evList := []Evidence{ev}
  21. block := MakeBlock(h, txs, commit, evList)
  22. require.NotNil(t, block)
  23. require.Equal(t, 1, len(block.Evidence.Evidence))
  24. require.NotNil(t, block.EvidenceHash)
  25. }
  26. func TestBlockValidateBasic(t *testing.T) {
  27. require.Error(t, (*Block)(nil).ValidateBasic())
  28. txs := []Tx{Tx("foo"), Tx("bar")}
  29. lastID := makeBlockIDRandom()
  30. h := int64(3)
  31. voteSet, valSet, vals := randVoteSet(h-1, 1, VoteTypePrecommit, 10, 1)
  32. commit, err := MakeCommit(lastID, h-1, 1, voteSet, vals)
  33. require.NoError(t, err)
  34. ev := NewMockGoodEvidence(h, 0, valSet.Validators[0].Address)
  35. evList := []Evidence{ev}
  36. block := MakeBlock(h, txs, commit, evList)
  37. require.NotNil(t, block)
  38. block.ProposerAddress = valSet.GetProposer().Address
  39. // proper block must pass
  40. err = block.ValidateBasic()
  41. require.NoError(t, err)
  42. // tamper with NumTxs
  43. block = MakeBlock(h, txs, commit, evList)
  44. block.NumTxs++
  45. err = block.ValidateBasic()
  46. require.Error(t, err)
  47. // remove 1/2 the commits
  48. block = MakeBlock(h, txs, commit, evList)
  49. block.LastCommit.Precommits = commit.Precommits[:commit.Size()/2]
  50. block.LastCommit.hash = nil // clear hash or change wont be noticed
  51. err = block.ValidateBasic()
  52. require.Error(t, err)
  53. // tamper with LastCommitHash
  54. block = MakeBlock(h, txs, commit, evList)
  55. block.LastCommitHash = []byte("something else")
  56. err = block.ValidateBasic()
  57. require.Error(t, err)
  58. // tamper with data
  59. block = MakeBlock(h, txs, commit, evList)
  60. block.Data.Txs[0] = Tx("something else")
  61. block.Data.hash = nil // clear hash or change wont be noticed
  62. err = block.ValidateBasic()
  63. require.Error(t, err)
  64. // tamper with DataHash
  65. block = MakeBlock(h, txs, commit, evList)
  66. block.DataHash = cmn.RandBytes(len(block.DataHash))
  67. err = block.ValidateBasic()
  68. require.Error(t, err)
  69. // tamper with evidence
  70. block = MakeBlock(h, txs, commit, evList)
  71. block.EvidenceHash = []byte("something else")
  72. err = block.ValidateBasic()
  73. require.Error(t, err)
  74. }
  75. func TestBlockHash(t *testing.T) {
  76. assert.Nil(t, (*Block)(nil).Hash())
  77. assert.Nil(t, MakeBlock(int64(3), []Tx{Tx("Hello World")}, nil, nil).Hash())
  78. }
  79. func TestBlockMakePartSet(t *testing.T) {
  80. assert.Nil(t, (*Block)(nil).MakePartSet(2))
  81. partSet := MakeBlock(int64(3), []Tx{Tx("Hello World")}, nil, nil).MakePartSet(1024)
  82. assert.NotNil(t, partSet)
  83. assert.Equal(t, 1, partSet.Total())
  84. }
  85. func TestBlockMakePartSetWithEvidence(t *testing.T) {
  86. assert.Nil(t, (*Block)(nil).MakePartSet(2))
  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, []Tx{Tx("Hello World")}, 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: math.MaxInt64,
  200. Time: time.Now().UTC(),
  201. NumTxs: math.MaxInt64,
  202. TotalTxs: math.MaxInt64,
  203. LastBlockID: makeBlockID(make([]byte, tmhash.Size), math.MaxInt64, make([]byte, tmhash.Size)),
  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. }