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.

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