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.

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