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.

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