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.

521 lines
15 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. tmtime "github.com/tendermint/tendermint/types/time"
  16. "github.com/tendermint/tendermint/version"
  17. )
  18. func TestMain(m *testing.M) {
  19. RegisterMockEvidences(cdc)
  20. code := m.Run()
  21. os.Exit(code)
  22. }
  23. func TestBlockAddEvidence(t *testing.T) {
  24. txs := []Tx{Tx("foo"), Tx("bar")}
  25. lastID := makeBlockIDRandom()
  26. h := int64(3)
  27. voteSet, valSet, vals := randVoteSet(h-1, 1, PrecommitType, 10, 1)
  28. commit, err := MakeCommit(lastID, h-1, 1, voteSet, vals)
  29. require.NoError(t, err)
  30. ev := NewMockGoodEvidence(h, 0, valSet.Validators[0].Address)
  31. evList := []Evidence{ev}
  32. block := MakeBlock(h, txs, commit, evList)
  33. require.NotNil(t, block)
  34. require.Equal(t, 1, len(block.Evidence.Evidence))
  35. require.NotNil(t, block.EvidenceHash)
  36. }
  37. func TestBlockValidateBasic(t *testing.T) {
  38. require.Error(t, (*Block)(nil).ValidateBasic())
  39. txs := []Tx{Tx("foo"), Tx("bar")}
  40. lastID := makeBlockIDRandom()
  41. h := int64(3)
  42. voteSet, valSet, vals := randVoteSet(h-1, 1, PrecommitType, 10, 1)
  43. commit, err := MakeCommit(lastID, h-1, 1, voteSet, vals)
  44. require.NoError(t, err)
  45. ev := NewMockGoodEvidence(h, 0, valSet.Validators[0].Address)
  46. evList := []Evidence{ev}
  47. testCases := []struct {
  48. testName string
  49. malleateBlock func(*Block)
  50. expErr bool
  51. }{
  52. {"Make Block", func(blk *Block) {}, false},
  53. {"Make Block w/ proposer Addr", func(blk *Block) { blk.ProposerAddress = valSet.GetProposer().Address }, false},
  54. {"Negative Height", func(blk *Block) { blk.Height = -1 }, true},
  55. {"Increase NumTxs", func(blk *Block) { blk.NumTxs++ }, true},
  56. {"Remove 1/2 the commits", func(blk *Block) {
  57. blk.LastCommit.Precommits = commit.Precommits[:commit.Size()/2]
  58. blk.LastCommit.hash = nil // clear hash or change wont be noticed
  59. }, true},
  60. {"Remove LastCommitHash", func(blk *Block) { blk.LastCommitHash = []byte("something else") }, true},
  61. {"Tampered Data", func(blk *Block) {
  62. blk.Data.Txs[0] = Tx("something else")
  63. blk.Data.hash = nil // clear hash or change wont be noticed
  64. }, true},
  65. {"Tampered DataHash", func(blk *Block) {
  66. blk.DataHash = cmn.RandBytes(len(blk.DataHash))
  67. }, true},
  68. {"Tampered EvidenceHash", func(blk *Block) {
  69. blk.EvidenceHash = []byte("something else")
  70. }, true},
  71. }
  72. for i, tc := range testCases {
  73. tc := tc
  74. i := i
  75. t.Run(tc.testName, func(t *testing.T) {
  76. block := MakeBlock(h, txs, commit, evList)
  77. block.ProposerAddress = valSet.GetProposer().Address
  78. tc.malleateBlock(block)
  79. err = block.ValidateBasic()
  80. assert.Equal(t, tc.expErr, err != nil, "#%d: %v", i, err)
  81. })
  82. }
  83. }
  84. func TestBlockHash(t *testing.T) {
  85. assert.Nil(t, (*Block)(nil).Hash())
  86. assert.Nil(t, MakeBlock(int64(3), []Tx{Tx("Hello World")}, nil, nil).Hash())
  87. }
  88. func TestBlockMakePartSet(t *testing.T) {
  89. assert.Nil(t, (*Block)(nil).MakePartSet(2))
  90. partSet := MakeBlock(int64(3), []Tx{Tx("Hello World")}, nil, nil).MakePartSet(1024)
  91. assert.NotNil(t, partSet)
  92. assert.Equal(t, 1, partSet.Total())
  93. }
  94. func TestBlockMakePartSetWithEvidence(t *testing.T) {
  95. assert.Nil(t, (*Block)(nil).MakePartSet(2))
  96. lastID := makeBlockIDRandom()
  97. h := int64(3)
  98. voteSet, valSet, vals := randVoteSet(h-1, 1, PrecommitType, 10, 1)
  99. commit, err := MakeCommit(lastID, h-1, 1, voteSet, vals)
  100. require.NoError(t, err)
  101. ev := NewMockGoodEvidence(h, 0, valSet.Validators[0].Address)
  102. evList := []Evidence{ev}
  103. partSet := MakeBlock(h, []Tx{Tx("Hello World")}, commit, evList).MakePartSet(1024)
  104. assert.NotNil(t, partSet)
  105. assert.Equal(t, 3, partSet.Total())
  106. }
  107. func TestBlockHashesTo(t *testing.T) {
  108. assert.False(t, (*Block)(nil).HashesTo(nil))
  109. lastID := makeBlockIDRandom()
  110. h := int64(3)
  111. voteSet, valSet, vals := randVoteSet(h-1, 1, PrecommitType, 10, 1)
  112. commit, err := MakeCommit(lastID, h-1, 1, voteSet, vals)
  113. require.NoError(t, err)
  114. ev := NewMockGoodEvidence(h, 0, valSet.Validators[0].Address)
  115. evList := []Evidence{ev}
  116. block := MakeBlock(h, []Tx{Tx("Hello World")}, commit, evList)
  117. block.ValidatorsHash = valSet.Hash()
  118. assert.False(t, block.HashesTo([]byte{}))
  119. assert.False(t, block.HashesTo([]byte("something else")))
  120. assert.True(t, block.HashesTo(block.Hash()))
  121. }
  122. func TestBlockSize(t *testing.T) {
  123. size := MakeBlock(int64(3), []Tx{Tx("Hello World")}, nil, nil).Size()
  124. if size <= 0 {
  125. t.Fatal("Size of the block is zero or negative")
  126. }
  127. }
  128. func TestBlockString(t *testing.T) {
  129. assert.Equal(t, "nil-Block", (*Block)(nil).String())
  130. assert.Equal(t, "nil-Block", (*Block)(nil).StringIndented(""))
  131. assert.Equal(t, "nil-Block", (*Block)(nil).StringShort())
  132. block := MakeBlock(int64(3), []Tx{Tx("Hello World")}, nil, nil)
  133. assert.NotEqual(t, "nil-Block", block.String())
  134. assert.NotEqual(t, "nil-Block", block.StringIndented(""))
  135. assert.NotEqual(t, "nil-Block", block.StringShort())
  136. }
  137. func makeBlockIDRandom() BlockID {
  138. blockHash := make([]byte, tmhash.Size)
  139. partSetHash := make([]byte, tmhash.Size)
  140. rand.Read(blockHash) //nolint: gosec
  141. rand.Read(partSetHash) //nolint: gosec
  142. blockPartsHeader := PartSetHeader{123, partSetHash}
  143. return BlockID{blockHash, blockPartsHeader}
  144. }
  145. func makeBlockID(hash []byte, partSetSize int, partSetHash []byte) BlockID {
  146. return BlockID{
  147. Hash: hash,
  148. PartsHeader: PartSetHeader{
  149. Total: partSetSize,
  150. Hash: partSetHash,
  151. },
  152. }
  153. }
  154. var nilBytes []byte
  155. func TestNilHeaderHashDoesntCrash(t *testing.T) {
  156. assert.Equal(t, []byte((*Header)(nil).Hash()), nilBytes)
  157. assert.Equal(t, []byte((new(Header)).Hash()), nilBytes)
  158. }
  159. func TestNilDataHashDoesntCrash(t *testing.T) {
  160. assert.Equal(t, []byte((*Data)(nil).Hash()), nilBytes)
  161. assert.Equal(t, []byte(new(Data).Hash()), nilBytes)
  162. }
  163. func TestCommit(t *testing.T) {
  164. lastID := makeBlockIDRandom()
  165. h := int64(3)
  166. voteSet, _, vals := randVoteSet(h-1, 1, PrecommitType, 10, 1)
  167. commit, err := MakeCommit(lastID, h-1, 1, voteSet, vals)
  168. require.NoError(t, err)
  169. assert.Equal(t, h-1, commit.Height())
  170. assert.Equal(t, 1, commit.Round())
  171. assert.Equal(t, PrecommitType, SignedMsgType(commit.Type()))
  172. if commit.Size() <= 0 {
  173. t.Fatalf("commit %v has a zero or negative size: %d", commit, commit.Size())
  174. }
  175. require.NotNil(t, commit.BitArray())
  176. assert.Equal(t, cmn.NewBitArray(10).Size(), commit.BitArray().Size())
  177. assert.Equal(t, voteSet.GetByIndex(0), commit.GetByIndex(0))
  178. assert.True(t, commit.IsCommit())
  179. }
  180. func TestCommitValidateBasic(t *testing.T) {
  181. testCases := []struct {
  182. testName string
  183. malleateCommit func(*Commit)
  184. expectErr bool
  185. }{
  186. {"Random Commit", func(com *Commit) {}, false},
  187. {"Nil precommit", func(com *Commit) { com.Precommits[0] = nil }, false},
  188. {"Incorrect signature", func(com *Commit) { com.Precommits[0].Signature = []byte{0} }, false},
  189. {"Incorrect type", func(com *Commit) { com.Precommits[0].Type = PrevoteType }, true},
  190. {"Incorrect height", func(com *Commit) { com.Precommits[0].Height = int64(100) }, true},
  191. {"Incorrect round", func(com *Commit) { com.Precommits[0].Round = 100 }, true},
  192. }
  193. for _, tc := range testCases {
  194. tc := tc
  195. t.Run(tc.testName, func(t *testing.T) {
  196. com := randCommit()
  197. tc.malleateCommit(com)
  198. assert.Equal(t, tc.expectErr, com.ValidateBasic() != nil, "Validate Basic had an unexpected result")
  199. })
  200. }
  201. }
  202. func TestMaxHeaderBytes(t *testing.T) {
  203. // Construct a UTF-8 string of MaxChainIDLen length using the supplementary
  204. // characters.
  205. // Each supplementary character takes 4 bytes.
  206. // http://www.i18nguy.com/unicode/supplementary-test.html
  207. maxChainID := ""
  208. for i := 0; i < MaxChainIDLen; i++ {
  209. maxChainID += "𠜎"
  210. }
  211. // time is varint encoded so need to pick the max.
  212. // year int, month Month, day, hour, min, sec, nsec int, loc *Location
  213. timestamp := time.Date(math.MaxInt64, 0, 0, 0, 0, 0, math.MaxInt64, time.UTC)
  214. h := Header{
  215. Version: version.Consensus{Block: math.MaxInt64, App: math.MaxInt64},
  216. ChainID: maxChainID,
  217. Height: math.MaxInt64,
  218. Time: timestamp,
  219. NumTxs: math.MaxInt64,
  220. TotalTxs: math.MaxInt64,
  221. LastBlockID: makeBlockID(make([]byte, tmhash.Size), math.MaxInt64, make([]byte, tmhash.Size)),
  222. LastCommitHash: tmhash.Sum([]byte("last_commit_hash")),
  223. DataHash: tmhash.Sum([]byte("data_hash")),
  224. ValidatorsHash: tmhash.Sum([]byte("validators_hash")),
  225. NextValidatorsHash: tmhash.Sum([]byte("next_validators_hash")),
  226. ConsensusHash: tmhash.Sum([]byte("consensus_hash")),
  227. AppHash: tmhash.Sum([]byte("app_hash")),
  228. LastResultsHash: tmhash.Sum([]byte("last_results_hash")),
  229. EvidenceHash: tmhash.Sum([]byte("evidence_hash")),
  230. ProposerAddress: crypto.AddressHash([]byte("proposer_address")),
  231. }
  232. bz, err := cdc.MarshalBinaryLengthPrefixed(h)
  233. require.NoError(t, err)
  234. assert.EqualValues(t, MaxHeaderBytes, len(bz))
  235. }
  236. func randCommit() *Commit {
  237. lastID := makeBlockIDRandom()
  238. h := int64(3)
  239. voteSet, _, vals := randVoteSet(h-1, 1, PrecommitType, 10, 1)
  240. commit, err := MakeCommit(lastID, h-1, 1, voteSet, vals)
  241. if err != nil {
  242. panic(err)
  243. }
  244. return commit
  245. }
  246. func TestBlockMaxDataBytes(t *testing.T) {
  247. testCases := []struct {
  248. maxBytes int64
  249. valsCount int
  250. evidenceCount int
  251. panics bool
  252. result int64
  253. }{
  254. 0: {-10, 1, 0, true, 0},
  255. 1: {10, 1, 0, true, 0},
  256. 2: {886, 1, 0, true, 0},
  257. 3: {887, 1, 0, false, 0},
  258. 4: {888, 1, 0, false, 1},
  259. }
  260. for i, tc := range testCases {
  261. tc := tc
  262. if tc.panics {
  263. assert.Panics(t, func() {
  264. MaxDataBytes(tc.maxBytes, tc.valsCount, tc.evidenceCount)
  265. }, "#%v", i)
  266. } else {
  267. assert.Equal(t,
  268. tc.result,
  269. MaxDataBytes(tc.maxBytes, tc.valsCount, tc.evidenceCount),
  270. "#%v", i)
  271. }
  272. }
  273. }
  274. func TestBlockMaxDataBytesUnknownEvidence(t *testing.T) {
  275. testCases := []struct {
  276. maxBytes int64
  277. valsCount int
  278. panics bool
  279. result int64
  280. }{
  281. 0: {-10, 1, true, 0},
  282. 1: {10, 1, true, 0},
  283. 2: {984, 1, true, 0},
  284. 3: {985, 1, false, 0},
  285. 4: {986, 1, false, 1},
  286. }
  287. for i, tc := range testCases {
  288. tc := tc
  289. if tc.panics {
  290. assert.Panics(t, func() {
  291. MaxDataBytesUnknownEvidence(tc.maxBytes, tc.valsCount)
  292. }, "#%v", i)
  293. } else {
  294. assert.Equal(t,
  295. tc.result,
  296. MaxDataBytesUnknownEvidence(tc.maxBytes, tc.valsCount),
  297. "#%v", i)
  298. }
  299. }
  300. }
  301. func TestCommitToVoteSet(t *testing.T) {
  302. lastID := makeBlockIDRandom()
  303. h := int64(3)
  304. voteSet, valSet, vals := randVoteSet(h-1, 1, PrecommitType, 10, 1)
  305. commit, err := MakeCommit(lastID, h-1, 1, voteSet, vals)
  306. assert.NoError(t, err)
  307. chainID := voteSet.ChainID()
  308. voteSet2 := CommitToVoteSet(chainID, commit, valSet)
  309. for i := 0; i < len(vals); i++ {
  310. vote1 := voteSet.GetByIndex(i)
  311. vote2 := voteSet2.GetByIndex(i)
  312. vote3 := commit.GetVote(i)
  313. vote1bz := cdc.MustMarshalBinaryBare(vote1)
  314. vote2bz := cdc.MustMarshalBinaryBare(vote2)
  315. vote3bz := cdc.MustMarshalBinaryBare(vote3)
  316. assert.Equal(t, vote1bz, vote2bz)
  317. assert.Equal(t, vote1bz, vote3bz)
  318. }
  319. }
  320. func TestCommitToVoteSetWithVotesForAnotherBlockOrNilBlock(t *testing.T) {
  321. blockID := makeBlockID([]byte("blockhash"), 1000, []byte("partshash"))
  322. blockID2 := makeBlockID([]byte("blockhash2"), 1000, []byte("partshash"))
  323. blockID3 := makeBlockID([]byte("blockhash3"), 10000, []byte("partshash"))
  324. height := int64(3)
  325. round := 1
  326. type commitVoteTest struct {
  327. blockIDs []BlockID
  328. numVotes []int // must sum to numValidators
  329. numValidators int
  330. valid bool
  331. }
  332. testCases := []commitVoteTest{
  333. {[]BlockID{blockID, blockID2, blockID3}, []int{8, 1, 1}, 10, true},
  334. {[]BlockID{blockID, blockID2, blockID3}, []int{67, 20, 13}, 100, true},
  335. {[]BlockID{blockID, blockID2, blockID3}, []int{1, 1, 1}, 3, false},
  336. {[]BlockID{blockID, blockID2, blockID3}, []int{3, 1, 1}, 5, false},
  337. {[]BlockID{blockID, {}}, []int{67, 33}, 100, true},
  338. {[]BlockID{blockID, blockID2, {}}, []int{10, 5, 5}, 20, false},
  339. }
  340. for _, tc := range testCases {
  341. voteSet, valSet, vals := randVoteSet(height-1, 1, PrecommitType, tc.numValidators, 1)
  342. vi := 0
  343. for n := range tc.blockIDs {
  344. for i := 0; i < tc.numVotes[n]; i++ {
  345. addr := vals[vi].GetPubKey().Address()
  346. vote := &Vote{
  347. ValidatorAddress: addr,
  348. ValidatorIndex: vi,
  349. Height: height - 1,
  350. Round: round,
  351. Type: PrecommitType,
  352. BlockID: tc.blockIDs[n],
  353. Timestamp: tmtime.Now(),
  354. }
  355. _, err := signAddVote(vals[vi], vote, voteSet)
  356. assert.NoError(t, err)
  357. vi++
  358. }
  359. }
  360. if tc.valid {
  361. commit := voteSet.MakeCommit() // panics without > 2/3 valid votes
  362. assert.NotNil(t, commit)
  363. err := valSet.VerifyCommit(voteSet.ChainID(), blockID, height-1, commit)
  364. assert.Nil(t, err)
  365. } else {
  366. assert.Panics(t, func() { voteSet.MakeCommit() })
  367. }
  368. }
  369. }
  370. func TestSignedHeaderValidateBasic(t *testing.T) {
  371. commit := randCommit()
  372. chainID := "𠜎"
  373. timestamp := time.Date(math.MaxInt64, 0, 0, 0, 0, 0, math.MaxInt64, time.UTC)
  374. h := Header{
  375. Version: version.Consensus{Block: math.MaxInt64, App: math.MaxInt64},
  376. ChainID: chainID,
  377. Height: commit.Height(),
  378. Time: timestamp,
  379. NumTxs: math.MaxInt64,
  380. TotalTxs: math.MaxInt64,
  381. LastBlockID: commit.BlockID,
  382. LastCommitHash: commit.Hash(),
  383. DataHash: commit.Hash(),
  384. ValidatorsHash: commit.Hash(),
  385. NextValidatorsHash: commit.Hash(),
  386. ConsensusHash: commit.Hash(),
  387. AppHash: commit.Hash(),
  388. LastResultsHash: commit.Hash(),
  389. EvidenceHash: commit.Hash(),
  390. ProposerAddress: crypto.AddressHash([]byte("proposer_address")),
  391. }
  392. validSignedHeader := SignedHeader{Header: &h, Commit: commit}
  393. validSignedHeader.Commit.BlockID.Hash = validSignedHeader.Hash()
  394. invalidSignedHeader := SignedHeader{}
  395. testCases := []struct {
  396. testName string
  397. shHeader *Header
  398. shCommit *Commit
  399. expectErr bool
  400. }{
  401. {"Valid Signed Header", validSignedHeader.Header, validSignedHeader.Commit, false},
  402. {"Invalid Signed Header", invalidSignedHeader.Header, validSignedHeader.Commit, true},
  403. {"Invalid Signed Header", validSignedHeader.Header, invalidSignedHeader.Commit, true},
  404. }
  405. for _, tc := range testCases {
  406. tc := tc
  407. t.Run(tc.testName, func(t *testing.T) {
  408. sh := SignedHeader{
  409. Header: tc.shHeader,
  410. Commit: tc.shCommit,
  411. }
  412. assert.Equal(t, tc.expectErr, sh.ValidateBasic(validSignedHeader.Header.ChainID) != nil, "Validate Basic had an unexpected result")
  413. })
  414. }
  415. }
  416. func TestBlockIDValidateBasic(t *testing.T) {
  417. validBlockID := BlockID{
  418. Hash: cmn.HexBytes{},
  419. PartsHeader: PartSetHeader{
  420. Total: 1,
  421. Hash: cmn.HexBytes{},
  422. },
  423. }
  424. invalidBlockID := BlockID{
  425. Hash: []byte{0},
  426. PartsHeader: PartSetHeader{
  427. Total: -1,
  428. Hash: cmn.HexBytes{},
  429. },
  430. }
  431. testCases := []struct {
  432. testName string
  433. blockIDHash cmn.HexBytes
  434. blockIDPartsHeader PartSetHeader
  435. expectErr bool
  436. }{
  437. {"Valid BlockID", validBlockID.Hash, validBlockID.PartsHeader, false},
  438. {"Invalid BlockID", invalidBlockID.Hash, validBlockID.PartsHeader, true},
  439. {"Invalid BlockID", validBlockID.Hash, invalidBlockID.PartsHeader, true},
  440. }
  441. for _, tc := range testCases {
  442. tc := tc
  443. t.Run(tc.testName, func(t *testing.T) {
  444. blockID := BlockID{
  445. Hash: tc.blockIDHash,
  446. PartsHeader: tc.blockIDPartsHeader,
  447. }
  448. assert.Equal(t, tc.expectErr, blockID.ValidateBasic() != nil, "Validate Basic had an unexpected result")
  449. })
  450. }
  451. }