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.

340 lines
10 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. // time is varint encoded so need to pick the max.
  204. // year int, month Month, day, hour, min, sec, nsec int, loc *Location
  205. timestamp := time.Date(math.MaxInt64, 0, 0, 0, 0, 0, math.MaxInt64, time.UTC)
  206. h := Header{
  207. Version: version.Consensus{math.MaxInt64, math.MaxInt64},
  208. ChainID: maxChainID,
  209. Height: math.MaxInt64,
  210. Time: timestamp,
  211. NumTxs: math.MaxInt64,
  212. TotalTxs: math.MaxInt64,
  213. LastBlockID: makeBlockID(make([]byte, tmhash.Size), math.MaxInt64, make([]byte, tmhash.Size)),
  214. LastCommitHash: tmhash.Sum([]byte("last_commit_hash")),
  215. DataHash: tmhash.Sum([]byte("data_hash")),
  216. ValidatorsHash: tmhash.Sum([]byte("validators_hash")),
  217. NextValidatorsHash: tmhash.Sum([]byte("next_validators_hash")),
  218. ConsensusHash: tmhash.Sum([]byte("consensus_hash")),
  219. AppHash: tmhash.Sum([]byte("app_hash")),
  220. LastResultsHash: tmhash.Sum([]byte("last_results_hash")),
  221. EvidenceHash: tmhash.Sum([]byte("evidence_hash")),
  222. ProposerAddress: tmhash.Sum([]byte("proposer_address")),
  223. }
  224. bz, err := cdc.MarshalBinaryLengthPrefixed(h)
  225. require.NoError(t, err)
  226. assert.EqualValues(t, MaxHeaderBytes, len(bz))
  227. }
  228. func randCommit() *Commit {
  229. lastID := makeBlockIDRandom()
  230. h := int64(3)
  231. voteSet, _, vals := randVoteSet(h-1, 1, PrecommitType, 10, 1)
  232. commit, err := MakeCommit(lastID, h-1, 1, voteSet, vals)
  233. if err != nil {
  234. panic(err)
  235. }
  236. return commit
  237. }
  238. func TestBlockMaxDataBytes(t *testing.T) {
  239. testCases := []struct {
  240. maxBytes int64
  241. valsCount int
  242. evidenceCount int
  243. panics bool
  244. result int64
  245. }{
  246. 0: {-10, 1, 0, true, 0},
  247. 1: {10, 1, 0, true, 0},
  248. 2: {742, 1, 0, true, 0},
  249. 3: {743, 1, 0, false, 0},
  250. 4: {744, 1, 0, false, 1},
  251. }
  252. for i, tc := range testCases {
  253. if tc.panics {
  254. assert.Panics(t, func() {
  255. MaxDataBytes(tc.maxBytes, tc.valsCount, tc.evidenceCount)
  256. }, "#%v", i)
  257. } else {
  258. assert.Equal(t,
  259. tc.result,
  260. MaxDataBytes(tc.maxBytes, tc.valsCount, tc.evidenceCount),
  261. "#%v", i)
  262. }
  263. }
  264. }
  265. func TestBlockMaxDataBytesUnknownEvidence(t *testing.T) {
  266. testCases := []struct {
  267. maxBytes int64
  268. valsCount int
  269. panics bool
  270. result int64
  271. }{
  272. 0: {-10, 1, true, 0},
  273. 1: {10, 1, true, 0},
  274. 2: {824, 1, true, 0},
  275. 3: {825, 1, false, 0},
  276. 4: {826, 1, false, 1},
  277. }
  278. for i, tc := range testCases {
  279. if tc.panics {
  280. assert.Panics(t, func() {
  281. MaxDataBytesUnknownEvidence(tc.maxBytes, tc.valsCount)
  282. }, "#%v", i)
  283. } else {
  284. assert.Equal(t,
  285. tc.result,
  286. MaxDataBytesUnknownEvidence(tc.maxBytes, tc.valsCount),
  287. "#%v", i)
  288. }
  289. }
  290. }