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.

334 lines
9.7 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. "os"
  6. "testing"
  7. "time"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/require"
  10. "github.com/tendermint/tendermint/crypto/tmhash"
  11. cmn "github.com/tendermint/tendermint/libs/common"
  12. )
  13. func TestMain(m *testing.M) {
  14. RegisterMockEvidences(cdc)
  15. code := m.Run()
  16. os.Exit(code)
  17. }
  18. func TestBlockAddEvidence(t *testing.T) {
  19. txs := []Tx{Tx("foo"), Tx("bar")}
  20. lastID := makeBlockIDRandom()
  21. h := int64(3)
  22. voteSet, valSet, vals := randVoteSet(h-1, 1, PrecommitType, 10, 1)
  23. commit, err := MakeCommit(lastID, h-1, 1, voteSet, vals)
  24. require.NoError(t, err)
  25. ev := NewMockGoodEvidence(h, 0, valSet.Validators[0].Address)
  26. evList := []Evidence{ev}
  27. block := MakeBlock(h, txs, commit, evList)
  28. require.NotNil(t, block)
  29. require.Equal(t, 1, len(block.Evidence.Evidence))
  30. require.NotNil(t, block.EvidenceHash)
  31. }
  32. func TestBlockValidateBasic(t *testing.T) {
  33. require.Error(t, (*Block)(nil).ValidateBasic())
  34. txs := []Tx{Tx("foo"), Tx("bar")}
  35. lastID := makeBlockIDRandom()
  36. h := int64(3)
  37. voteSet, valSet, vals := randVoteSet(h-1, 1, PrecommitType, 10, 1)
  38. commit, err := MakeCommit(lastID, h-1, 1, voteSet, vals)
  39. require.NoError(t, err)
  40. ev := NewMockGoodEvidence(h, 0, valSet.Validators[0].Address)
  41. evList := []Evidence{ev}
  42. testCases := []struct {
  43. testName string
  44. malleateBlock func(*Block)
  45. expErr bool
  46. }{
  47. {"Make Block", func(blk *Block) {}, false},
  48. {"Make Block w/ proposer Addr", func(blk *Block) { blk.ProposerAddress = valSet.GetProposer().Address }, false},
  49. {"Negative Height", func(blk *Block) { blk.Height = -1 }, true},
  50. {"Increase NumTxs", func(blk *Block) { blk.NumTxs++ }, true},
  51. {"Remove 1/2 the commits", func(blk *Block) {
  52. blk.LastCommit.Precommits = commit.Precommits[:commit.Size()/2]
  53. blk.LastCommit.hash = nil // clear hash or change wont be noticed
  54. }, true},
  55. {"Remove LastCommitHash", func(blk *Block) { blk.LastCommitHash = []byte("something else") }, true},
  56. {"Tampered Data", func(blk *Block) {
  57. blk.Data.Txs[0] = Tx("something else")
  58. blk.Data.hash = nil // clear hash or change wont be noticed
  59. }, true},
  60. {"Tampered DataHash", func(blk *Block) {
  61. blk.DataHash = cmn.RandBytes(len(blk.DataHash))
  62. }, true},
  63. {"Tampered EvidenceHash", func(blk *Block) {
  64. blk.EvidenceHash = []byte("something else")
  65. }, true},
  66. }
  67. for _, tc := range testCases {
  68. t.Run(tc.testName, func(t *testing.T) {
  69. block := MakeBlock(h, txs, commit, evList)
  70. tc.malleateBlock(block)
  71. assert.Equal(t, tc.expErr, block.ValidateBasic() != nil, "ValidateBasic had an unexpected result")
  72. })
  73. }
  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, PrecommitType, 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, PrecommitType, 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 := make([]byte, tmhash.Size)
  130. partSetHash := make([]byte, tmhash.Size)
  131. rand.Read(blockHash)
  132. rand.Read(partSetHash)
  133. blockPartsHeader := PartSetHeader{123, partSetHash}
  134. return BlockID{blockHash, blockPartsHeader}
  135. }
  136. func makeBlockID(hash []byte, partSetSize int, partSetHash []byte) BlockID {
  137. return BlockID{
  138. Hash: hash,
  139. PartsHeader: PartSetHeader{
  140. Total: partSetSize,
  141. Hash: partSetHash,
  142. },
  143. }
  144. }
  145. var nilBytes []byte
  146. func TestNilHeaderHashDoesntCrash(t *testing.T) {
  147. assert.Equal(t, []byte((*Header)(nil).Hash()), nilBytes)
  148. assert.Equal(t, []byte((new(Header)).Hash()), nilBytes)
  149. }
  150. func TestNilDataHashDoesntCrash(t *testing.T) {
  151. assert.Equal(t, []byte((*Data)(nil).Hash()), nilBytes)
  152. assert.Equal(t, []byte(new(Data).Hash()), nilBytes)
  153. }
  154. func TestCommit(t *testing.T) {
  155. lastID := makeBlockIDRandom()
  156. h := int64(3)
  157. voteSet, _, vals := randVoteSet(h-1, 1, PrecommitType, 10, 1)
  158. commit, err := MakeCommit(lastID, h-1, 1, voteSet, vals)
  159. require.NoError(t, err)
  160. assert.NotNil(t, commit.FirstPrecommit())
  161. assert.Equal(t, h-1, commit.Height())
  162. assert.Equal(t, 1, commit.Round())
  163. assert.Equal(t, PrecommitType, SignedMsgType(commit.Type()))
  164. if commit.Size() <= 0 {
  165. t.Fatalf("commit %v has a zero or negative size: %d", commit, commit.Size())
  166. }
  167. require.NotNil(t, commit.BitArray())
  168. assert.Equal(t, cmn.NewBitArray(10).Size(), commit.BitArray().Size())
  169. assert.Equal(t, voteSet.GetByIndex(0), commit.GetByIndex(0))
  170. assert.True(t, commit.IsCommit())
  171. }
  172. func TestCommitValidateBasic(t *testing.T) {
  173. testCases := []struct {
  174. testName string
  175. malleateCommit func(*Commit)
  176. expectErr bool
  177. }{
  178. {"Random Commit", func(com *Commit) {}, false},
  179. {"Nil precommit", func(com *Commit) { com.Precommits[0] = nil }, false},
  180. {"Incorrect signature", func(com *Commit) { com.Precommits[0].Signature = []byte{0} }, false},
  181. {"Incorrect type", func(com *Commit) { com.Precommits[0].Type = PrevoteType }, true},
  182. {"Incorrect height", func(com *Commit) { com.Precommits[0].Height = int64(100) }, true},
  183. {"Incorrect round", func(com *Commit) { com.Precommits[0].Round = 100 }, true},
  184. }
  185. for _, tc := range testCases {
  186. t.Run(tc.testName, func(t *testing.T) {
  187. com := randCommit()
  188. tc.malleateCommit(com)
  189. assert.Equal(t, tc.expectErr, com.ValidateBasic() != nil, "Validate Basic had an unexpected result")
  190. })
  191. }
  192. }
  193. func TestMaxHeaderBytes(t *testing.T) {
  194. // Construct a UTF-8 string of MaxChainIDLen length using the supplementary
  195. // characters.
  196. // Each supplementary character takes 4 bytes.
  197. // http://www.i18nguy.com/unicode/supplementary-test.html
  198. maxChainID := ""
  199. for i := 0; i < MaxChainIDLen; i++ {
  200. maxChainID += "𠜎"
  201. }
  202. h := Header{
  203. ChainID: maxChainID,
  204. Height: math.MaxInt64,
  205. Time: time.Now().UTC(),
  206. NumTxs: math.MaxInt64,
  207. TotalTxs: math.MaxInt64,
  208. LastBlockID: makeBlockID(make([]byte, tmhash.Size), math.MaxInt64, make([]byte, tmhash.Size)),
  209. LastCommitHash: tmhash.Sum([]byte("last_commit_hash")),
  210. DataHash: tmhash.Sum([]byte("data_hash")),
  211. ValidatorsHash: tmhash.Sum([]byte("validators_hash")),
  212. NextValidatorsHash: tmhash.Sum([]byte("next_validators_hash")),
  213. ConsensusHash: tmhash.Sum([]byte("consensus_hash")),
  214. AppHash: tmhash.Sum([]byte("app_hash")),
  215. LastResultsHash: tmhash.Sum([]byte("last_results_hash")),
  216. EvidenceHash: tmhash.Sum([]byte("evidence_hash")),
  217. ProposerAddress: tmhash.Sum([]byte("proposer_address")),
  218. }
  219. bz, err := cdc.MarshalBinary(h)
  220. require.NoError(t, err)
  221. assert.EqualValues(t, MaxHeaderBytes, len(bz))
  222. }
  223. func randCommit() *Commit {
  224. lastID := makeBlockIDRandom()
  225. h := int64(3)
  226. voteSet, _, vals := randVoteSet(h-1, 1, PrecommitType, 10, 1)
  227. commit, err := MakeCommit(lastID, h-1, 1, voteSet, vals)
  228. if err != nil {
  229. panic(err)
  230. }
  231. return commit
  232. }
  233. func TestBlockMaxDataBytes(t *testing.T) {
  234. testCases := []struct {
  235. maxBytes int64
  236. valsCount int
  237. evidenceCount int
  238. panics bool
  239. result int64
  240. }{
  241. 0: {-10, 1, 0, true, 0},
  242. 1: {10, 1, 0, true, 0},
  243. 2: {721, 1, 0, true, 0},
  244. 3: {722, 1, 0, false, 0},
  245. 4: {723, 1, 0, false, 1},
  246. }
  247. for i, tc := range testCases {
  248. if tc.panics {
  249. assert.Panics(t, func() {
  250. MaxDataBytes(tc.maxBytes, tc.valsCount, tc.evidenceCount)
  251. }, "#%v", i)
  252. } else {
  253. assert.Equal(t,
  254. tc.result,
  255. MaxDataBytes(tc.maxBytes, tc.valsCount, tc.evidenceCount),
  256. "#%v", i)
  257. }
  258. }
  259. }
  260. func TestBlockMaxDataBytesUnknownEvidence(t *testing.T) {
  261. testCases := []struct {
  262. maxBytes int64
  263. valsCount int
  264. panics bool
  265. result int64
  266. }{
  267. 0: {-10, 1, true, 0},
  268. 1: {10, 1, true, 0},
  269. 2: {801, 1, true, 0},
  270. 3: {802, 1, false, 0},
  271. 4: {803, 1, false, 1},
  272. }
  273. for i, tc := range testCases {
  274. if tc.panics {
  275. assert.Panics(t, func() {
  276. MaxDataBytesUnknownEvidence(tc.maxBytes, tc.valsCount)
  277. }, "#%v", i)
  278. } else {
  279. assert.Equal(t,
  280. tc.result,
  281. MaxDataBytesUnknownEvidence(tc.maxBytes, tc.valsCount),
  282. "#%v", i)
  283. }
  284. }
  285. }