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.

325 lines
9.5 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. "crypto/rand"
  4. "math"
  5. "testing"
  6. "time"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/require"
  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. testCases := []struct {
  37. testName string
  38. malleateBlock func(*Block)
  39. expErr bool
  40. }{
  41. {"Make Block", func(blk *Block) {}, false},
  42. {"Make Block w/ proposer Addr", func(blk *Block) { blk.ProposerAddress = valSet.GetProposer().Address }, false},
  43. {"Increase NumTxs", func(blk *Block) { blk.NumTxs++ }, true},
  44. {"Remove 1/2 the commits", func(blk *Block) {
  45. blk.LastCommit.Precommits = commit.Precommits[:commit.Size()/2]
  46. blk.LastCommit.hash = nil // clear hash or change wont be noticed
  47. }, true},
  48. {"Remove LastCommitHash", func(blk *Block) { blk.LastCommitHash = []byte("something else") }, true},
  49. {"Tampered Data", func(blk *Block) {
  50. blk.Data.Txs[0] = Tx("something else")
  51. blk.Data.hash = nil // clear hash or change wont be noticed
  52. }, true},
  53. {"Tampered DataHash", func(blk *Block) {
  54. blk.DataHash = cmn.RandBytes(len(blk.DataHash))
  55. }, true},
  56. {"Tampered EvidenceHash", func(blk *Block) {
  57. blk.EvidenceHash = []byte("something else")
  58. }, true},
  59. }
  60. for _, tc := range testCases {
  61. t.Run(tc.testName, func(t *testing.T) {
  62. block := MakeBlock(h, txs, commit, evList)
  63. tc.malleateBlock(block)
  64. assert.Equal(t, tc.expErr, block.ValidateBasic() != nil, "Validate Basic had an unexpected result")
  65. })
  66. }
  67. }
  68. func TestBlockHash(t *testing.T) {
  69. assert.Nil(t, (*Block)(nil).Hash())
  70. assert.Nil(t, MakeBlock(int64(3), []Tx{Tx("Hello World")}, nil, nil).Hash())
  71. }
  72. func TestBlockMakePartSet(t *testing.T) {
  73. assert.Nil(t, (*Block)(nil).MakePartSet(2))
  74. partSet := MakeBlock(int64(3), []Tx{Tx("Hello World")}, nil, nil).MakePartSet(1024)
  75. assert.NotNil(t, partSet)
  76. assert.Equal(t, 1, partSet.Total())
  77. }
  78. func TestBlockMakePartSetWithEvidence(t *testing.T) {
  79. assert.Nil(t, (*Block)(nil).MakePartSet(2))
  80. lastID := makeBlockIDRandom()
  81. h := int64(3)
  82. voteSet, valSet, vals := randVoteSet(h-1, 1, VoteTypePrecommit, 10, 1)
  83. commit, err := MakeCommit(lastID, h-1, 1, voteSet, vals)
  84. require.NoError(t, err)
  85. ev := NewMockGoodEvidence(h, 0, valSet.Validators[0].Address)
  86. evList := []Evidence{ev}
  87. partSet := MakeBlock(h, []Tx{Tx("Hello World")}, commit, evList).MakePartSet(1024)
  88. assert.NotNil(t, partSet)
  89. assert.Equal(t, 2, partSet.Total())
  90. }
  91. func TestBlockHashesTo(t *testing.T) {
  92. assert.False(t, (*Block)(nil).HashesTo(nil))
  93. lastID := makeBlockIDRandom()
  94. h := int64(3)
  95. voteSet, valSet, vals := randVoteSet(h-1, 1, VoteTypePrecommit, 10, 1)
  96. commit, err := MakeCommit(lastID, h-1, 1, voteSet, vals)
  97. require.NoError(t, err)
  98. ev := NewMockGoodEvidence(h, 0, valSet.Validators[0].Address)
  99. evList := []Evidence{ev}
  100. block := MakeBlock(h, []Tx{Tx("Hello World")}, commit, evList)
  101. block.ValidatorsHash = valSet.Hash()
  102. assert.False(t, block.HashesTo([]byte{}))
  103. assert.False(t, block.HashesTo([]byte("something else")))
  104. assert.True(t, block.HashesTo(block.Hash()))
  105. }
  106. func TestBlockSize(t *testing.T) {
  107. size := MakeBlock(int64(3), []Tx{Tx("Hello World")}, nil, nil).Size()
  108. if size <= 0 {
  109. t.Fatal("Size of the block is zero or negative")
  110. }
  111. }
  112. func TestBlockString(t *testing.T) {
  113. assert.Equal(t, "nil-Block", (*Block)(nil).String())
  114. assert.Equal(t, "nil-Block", (*Block)(nil).StringIndented(""))
  115. assert.Equal(t, "nil-Block", (*Block)(nil).StringShort())
  116. block := MakeBlock(int64(3), []Tx{Tx("Hello World")}, nil, nil)
  117. assert.NotEqual(t, "nil-Block", block.String())
  118. assert.NotEqual(t, "nil-Block", block.StringIndented(""))
  119. assert.NotEqual(t, "nil-Block", block.StringShort())
  120. }
  121. func makeBlockIDRandom() BlockID {
  122. blockHash := make([]byte, tmhash.Size)
  123. partSetHash := make([]byte, tmhash.Size)
  124. rand.Read(blockHash)
  125. rand.Read(partSetHash)
  126. blockPartsHeader := PartSetHeader{123, partSetHash}
  127. return BlockID{blockHash, blockPartsHeader}
  128. }
  129. func makeBlockID(hash []byte, partSetSize int, partSetHash []byte) BlockID {
  130. return BlockID{
  131. Hash: hash,
  132. PartsHeader: PartSetHeader{
  133. Total: partSetSize,
  134. Hash: 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. testCases := []struct {
  167. testName string
  168. malleateCommit func(*Commit)
  169. expectErr bool
  170. }{
  171. {"Random Commit", func(com *Commit) {}, false},
  172. {"Nil precommit", func(com *Commit) { com.Precommits[0] = nil }, false},
  173. {"Incorrect signature", func(com *Commit) { com.Precommits[0].Signature = []byte{0} }, false},
  174. {"Incorrect type", func(com *Commit) { com.Precommits[0].Type = VoteTypePrevote }, true},
  175. {"Incorrect height", func(com *Commit) { com.Precommits[0].Height = int64(100) }, true},
  176. {"Incorrect round", func(com *Commit) { com.Precommits[0].Round = 100 }, true},
  177. }
  178. for _, tc := range testCases {
  179. t.Run(tc.testName, func(t *testing.T) {
  180. com := randCommit()
  181. tc.malleateCommit(com)
  182. assert.Equal(t, tc.expectErr, com.ValidateBasic() != nil, "Validate Basic had an unexpected result")
  183. })
  184. }
  185. }
  186. func TestMaxHeaderBytes(t *testing.T) {
  187. // Construct a UTF-8 string of MaxChainIDLen length using the supplementary
  188. // characters.
  189. // Each supplementary character takes 4 bytes.
  190. // http://www.i18nguy.com/unicode/supplementary-test.html
  191. maxChainID := ""
  192. for i := 0; i < MaxChainIDLen; i++ {
  193. maxChainID += "𠜎"
  194. }
  195. h := Header{
  196. ChainID: maxChainID,
  197. Height: math.MaxInt64,
  198. Time: time.Now().UTC(),
  199. NumTxs: math.MaxInt64,
  200. TotalTxs: math.MaxInt64,
  201. LastBlockID: makeBlockID(make([]byte, tmhash.Size), math.MaxInt64, make([]byte, tmhash.Size)),
  202. LastCommitHash: tmhash.Sum([]byte("last_commit_hash")),
  203. DataHash: tmhash.Sum([]byte("data_hash")),
  204. ValidatorsHash: tmhash.Sum([]byte("validators_hash")),
  205. NextValidatorsHash: tmhash.Sum([]byte("next_validators_hash")),
  206. ConsensusHash: tmhash.Sum([]byte("consensus_hash")),
  207. AppHash: tmhash.Sum([]byte("app_hash")),
  208. LastResultsHash: tmhash.Sum([]byte("last_results_hash")),
  209. EvidenceHash: tmhash.Sum([]byte("evidence_hash")),
  210. ProposerAddress: tmhash.Sum([]byte("proposer_address")),
  211. }
  212. bz, err := cdc.MarshalBinary(h)
  213. require.NoError(t, err)
  214. assert.EqualValues(t, MaxHeaderBytes, len(bz))
  215. }
  216. func randCommit() *Commit {
  217. lastID := makeBlockIDRandom()
  218. h := int64(3)
  219. voteSet, _, vals := randVoteSet(h-1, 1, VoteTypePrecommit, 10, 1)
  220. commit, err := MakeCommit(lastID, h-1, 1, voteSet, vals)
  221. if err != nil {
  222. panic(err)
  223. }
  224. return commit
  225. }
  226. func TestBlockMaxDataBytes(t *testing.T) {
  227. testCases := []struct {
  228. maxBytes int64
  229. valsCount int
  230. evidenceCount int
  231. panics bool
  232. result int64
  233. }{
  234. 0: {-10, 1, 0, true, 0},
  235. 1: {10, 1, 0, true, 0},
  236. 2: {721, 1, 0, true, 0},
  237. 3: {722, 1, 0, false, 0},
  238. 4: {723, 1, 0, false, 1},
  239. }
  240. for i, tc := range testCases {
  241. if tc.panics {
  242. assert.Panics(t, func() {
  243. MaxDataBytes(tc.maxBytes, tc.valsCount, tc.evidenceCount)
  244. }, "#%v", i)
  245. } else {
  246. assert.Equal(t,
  247. tc.result,
  248. MaxDataBytes(tc.maxBytes, tc.valsCount, tc.evidenceCount),
  249. "#%v", i)
  250. }
  251. }
  252. }
  253. func TestBlockMaxDataBytesUnknownEvidence(t *testing.T) {
  254. testCases := []struct {
  255. maxBytes int64
  256. valsCount int
  257. panics bool
  258. result int64
  259. }{
  260. 0: {-10, 1, true, 0},
  261. 1: {10, 1, true, 0},
  262. 2: {801, 1, true, 0},
  263. 3: {802, 1, false, 0},
  264. 4: {803, 1, false, 1},
  265. }
  266. for i, tc := range testCases {
  267. if tc.panics {
  268. assert.Panics(t, func() {
  269. MaxDataBytesUnknownEvidence(tc.maxBytes, tc.valsCount)
  270. }, "#%v", i)
  271. } else {
  272. assert.Equal(t,
  273. tc.result,
  274. MaxDataBytesUnknownEvidence(tc.maxBytes, tc.valsCount),
  275. "#%v", i)
  276. }
  277. }
  278. }