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.

343 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.NotNil(t, commit.FirstPrecommit())
  165. assert.Equal(t, h-1, commit.Height())
  166. assert.Equal(t, 1, commit.Round())
  167. assert.Equal(t, PrecommitType, SignedMsgType(commit.Type()))
  168. if commit.Size() <= 0 {
  169. t.Fatalf("commit %v has a zero or negative size: %d", commit, commit.Size())
  170. }
  171. require.NotNil(t, commit.BitArray())
  172. assert.Equal(t, cmn.NewBitArray(10).Size(), commit.BitArray().Size())
  173. assert.Equal(t, voteSet.GetByIndex(0), commit.GetByIndex(0))
  174. assert.True(t, commit.IsCommit())
  175. }
  176. func TestCommitValidateBasic(t *testing.T) {
  177. testCases := []struct {
  178. testName string
  179. malleateCommit func(*Commit)
  180. expectErr bool
  181. }{
  182. {"Random Commit", func(com *Commit) {}, false},
  183. {"Nil precommit", func(com *Commit) { com.Precommits[0] = nil }, false},
  184. {"Incorrect signature", func(com *Commit) { com.Precommits[0].Signature = []byte{0} }, false},
  185. {"Incorrect type", func(com *Commit) { com.Precommits[0].Type = PrevoteType }, true},
  186. {"Incorrect height", func(com *Commit) { com.Precommits[0].Height = int64(100) }, true},
  187. {"Incorrect round", func(com *Commit) { com.Precommits[0].Round = 100 }, true},
  188. }
  189. for _, tc := range testCases {
  190. t.Run(tc.testName, func(t *testing.T) {
  191. com := randCommit()
  192. tc.malleateCommit(com)
  193. assert.Equal(t, tc.expectErr, com.ValidateBasic() != nil, "Validate Basic had an unexpected result")
  194. })
  195. }
  196. }
  197. func TestMaxHeaderBytes(t *testing.T) {
  198. // Construct a UTF-8 string of MaxChainIDLen length using the supplementary
  199. // characters.
  200. // Each supplementary character takes 4 bytes.
  201. // http://www.i18nguy.com/unicode/supplementary-test.html
  202. maxChainID := ""
  203. for i := 0; i < MaxChainIDLen; i++ {
  204. maxChainID += "𠜎"
  205. }
  206. // time is varint encoded so need to pick the max.
  207. // year int, month Month, day, hour, min, sec, nsec int, loc *Location
  208. timestamp := time.Date(math.MaxInt64, 0, 0, 0, 0, 0, math.MaxInt64, time.UTC)
  209. h := Header{
  210. Version: version.Consensus{Block: math.MaxInt64, App: math.MaxInt64},
  211. ChainID: maxChainID,
  212. Height: math.MaxInt64,
  213. Time: timestamp,
  214. NumTxs: math.MaxInt64,
  215. TotalTxs: math.MaxInt64,
  216. LastBlockID: makeBlockID(make([]byte, tmhash.Size), math.MaxInt64, make([]byte, tmhash.Size)),
  217. LastCommitHash: tmhash.Sum([]byte("last_commit_hash")),
  218. DataHash: tmhash.Sum([]byte("data_hash")),
  219. ValidatorsHash: tmhash.Sum([]byte("validators_hash")),
  220. NextValidatorsHash: tmhash.Sum([]byte("next_validators_hash")),
  221. ConsensusHash: tmhash.Sum([]byte("consensus_hash")),
  222. AppHash: tmhash.Sum([]byte("app_hash")),
  223. LastResultsHash: tmhash.Sum([]byte("last_results_hash")),
  224. EvidenceHash: tmhash.Sum([]byte("evidence_hash")),
  225. ProposerAddress: crypto.AddressHash([]byte("proposer_address")),
  226. }
  227. bz, err := cdc.MarshalBinaryLengthPrefixed(h)
  228. require.NoError(t, err)
  229. assert.EqualValues(t, MaxHeaderBytes, len(bz))
  230. }
  231. func randCommit() *Commit {
  232. lastID := makeBlockIDRandom()
  233. h := int64(3)
  234. voteSet, _, vals := randVoteSet(h-1, 1, PrecommitType, 10, 1)
  235. commit, err := MakeCommit(lastID, h-1, 1, voteSet, vals)
  236. if err != nil {
  237. panic(err)
  238. }
  239. return commit
  240. }
  241. func TestBlockMaxDataBytes(t *testing.T) {
  242. testCases := []struct {
  243. maxBytes int64
  244. valsCount int
  245. evidenceCount int
  246. panics bool
  247. result int64
  248. }{
  249. 0: {-10, 1, 0, true, 0},
  250. 1: {10, 1, 0, true, 0},
  251. 2: {886, 1, 0, true, 0},
  252. 3: {887, 1, 0, false, 0},
  253. 4: {888, 1, 0, false, 1},
  254. }
  255. for i, tc := range testCases {
  256. if tc.panics {
  257. assert.Panics(t, func() {
  258. MaxDataBytes(tc.maxBytes, tc.valsCount, tc.evidenceCount)
  259. }, "#%v", i)
  260. } else {
  261. assert.Equal(t,
  262. tc.result,
  263. MaxDataBytes(tc.maxBytes, tc.valsCount, tc.evidenceCount),
  264. "#%v", i)
  265. }
  266. }
  267. }
  268. func TestBlockMaxDataBytesUnknownEvidence(t *testing.T) {
  269. testCases := []struct {
  270. maxBytes int64
  271. valsCount int
  272. panics bool
  273. result int64
  274. }{
  275. 0: {-10, 1, true, 0},
  276. 1: {10, 1, true, 0},
  277. 2: {984, 1, true, 0},
  278. 3: {985, 1, false, 0},
  279. 4: {986, 1, false, 1},
  280. }
  281. for i, tc := range testCases {
  282. if tc.panics {
  283. assert.Panics(t, func() {
  284. MaxDataBytesUnknownEvidence(tc.maxBytes, tc.valsCount)
  285. }, "#%v", i)
  286. } else {
  287. assert.Equal(t,
  288. tc.result,
  289. MaxDataBytesUnknownEvidence(tc.maxBytes, tc.valsCount),
  290. "#%v", i)
  291. }
  292. }
  293. }