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.

318 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
  1. package types
  2. import (
  3. "math"
  4. "testing"
  5. "time"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. "github.com/tendermint/tendermint/crypto"
  9. "github.com/tendermint/tendermint/crypto/tmhash"
  10. tmrand "github.com/tendermint/tendermint/libs/rand"
  11. tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
  12. "github.com/tendermint/tendermint/version"
  13. )
  14. var defaultVoteTime = time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC)
  15. func TestEvidenceList(t *testing.T) {
  16. ev := randomDuplicateVoteEvidence(t)
  17. evl := EvidenceList([]Evidence{ev})
  18. assert.NotNil(t, evl.Hash())
  19. assert.True(t, evl.Has(ev))
  20. assert.False(t, evl.Has(&DuplicateVoteEvidence{}))
  21. }
  22. func randomDuplicateVoteEvidence(t *testing.T) *DuplicateVoteEvidence {
  23. val := NewMockPV()
  24. blockID := makeBlockID([]byte("blockhash"), 1000, []byte("partshash"))
  25. blockID2 := makeBlockID([]byte("blockhash2"), 1000, []byte("partshash"))
  26. const chainID = "mychain"
  27. return &DuplicateVoteEvidence{
  28. VoteA: makeVote(t, val, chainID, 0, 10, 2, 1, blockID, defaultVoteTime),
  29. VoteB: makeVote(t, val, chainID, 0, 10, 2, 1, blockID2, defaultVoteTime.Add(1*time.Minute)),
  30. TotalVotingPower: 30,
  31. ValidatorPower: 10,
  32. Timestamp: defaultVoteTime,
  33. }
  34. }
  35. func TestDuplicateVoteEvidence(t *testing.T) {
  36. const height = int64(13)
  37. ev := NewMockDuplicateVoteEvidence(height, time.Now(), "mock-chain-id")
  38. assert.Equal(t, ev.Hash(), tmhash.Sum(ev.Bytes()))
  39. assert.NotNil(t, ev.String())
  40. assert.Equal(t, ev.Height(), height)
  41. }
  42. func TestDuplicateVoteEvidenceValidation(t *testing.T) {
  43. val := NewMockPV()
  44. blockID := makeBlockID(tmhash.Sum([]byte("blockhash")), math.MaxInt32, tmhash.Sum([]byte("partshash")))
  45. blockID2 := makeBlockID(tmhash.Sum([]byte("blockhash2")), math.MaxInt32, tmhash.Sum([]byte("partshash")))
  46. const chainID = "mychain"
  47. testCases := []struct {
  48. testName string
  49. malleateEvidence func(*DuplicateVoteEvidence)
  50. expectErr bool
  51. }{
  52. {"Good DuplicateVoteEvidence", func(ev *DuplicateVoteEvidence) {}, false},
  53. {"Nil vote A", func(ev *DuplicateVoteEvidence) { ev.VoteA = nil }, true},
  54. {"Nil vote B", func(ev *DuplicateVoteEvidence) { ev.VoteB = nil }, true},
  55. {"Nil votes", func(ev *DuplicateVoteEvidence) {
  56. ev.VoteA = nil
  57. ev.VoteB = nil
  58. }, true},
  59. {"Invalid vote type", func(ev *DuplicateVoteEvidence) {
  60. ev.VoteA = makeVote(t, val, chainID, math.MaxInt32, math.MaxInt64, math.MaxInt32, 0, blockID2, defaultVoteTime)
  61. }, true},
  62. {"Invalid vote order", func(ev *DuplicateVoteEvidence) {
  63. swap := ev.VoteA.Copy()
  64. ev.VoteA = ev.VoteB.Copy()
  65. ev.VoteB = swap
  66. }, true},
  67. }
  68. for _, tc := range testCases {
  69. tc := tc
  70. t.Run(tc.testName, func(t *testing.T) {
  71. vote1 := makeVote(t, val, chainID, math.MaxInt32, math.MaxInt64, math.MaxInt32, 0x02, blockID, defaultVoteTime)
  72. vote2 := makeVote(t, val, chainID, math.MaxInt32, math.MaxInt64, math.MaxInt32, 0x02, blockID2, defaultVoteTime)
  73. valSet := NewValidatorSet([]*Validator{val.ExtractIntoValidator(10)})
  74. ev := NewDuplicateVoteEvidence(vote1, vote2, defaultVoteTime, valSet)
  75. tc.malleateEvidence(ev)
  76. assert.Equal(t, tc.expectErr, ev.ValidateBasic() != nil, "Validate Basic had an unexpected result")
  77. })
  78. }
  79. }
  80. func TestLightClientAttackEvidence(t *testing.T) {
  81. height := int64(5)
  82. voteSet, valSet, privVals := randVoteSet(height, 1, tmproto.PrecommitType, 10, 1)
  83. header := makeHeaderRandom()
  84. header.Height = height
  85. blockID := makeBlockID(tmhash.Sum([]byte("blockhash")), math.MaxInt32, tmhash.Sum([]byte("partshash")))
  86. commit, err := MakeCommit(blockID, height, 1, voteSet, privVals, defaultVoteTime)
  87. require.NoError(t, err)
  88. lcae := &LightClientAttackEvidence{
  89. ConflictingBlock: &LightBlock{
  90. SignedHeader: &SignedHeader{
  91. Header: header,
  92. Commit: commit,
  93. },
  94. ValidatorSet: valSet,
  95. },
  96. CommonHeight: height - 1,
  97. }
  98. assert.NotNil(t, lcae.String())
  99. assert.NotNil(t, lcae.Hash())
  100. // only 7 validators sign
  101. differentCommit, err := MakeCommit(blockID, height, 1, voteSet, privVals[:7], defaultVoteTime)
  102. require.NoError(t, err)
  103. differentEv := &LightClientAttackEvidence{
  104. ConflictingBlock: &LightBlock{
  105. SignedHeader: &SignedHeader{
  106. Header: header,
  107. Commit: differentCommit,
  108. },
  109. ValidatorSet: valSet,
  110. },
  111. CommonHeight: height - 1,
  112. }
  113. assert.Equal(t, lcae.Hash(), differentEv.Hash())
  114. // different header hash
  115. differentHeader := makeHeaderRandom()
  116. differentEv = &LightClientAttackEvidence{
  117. ConflictingBlock: &LightBlock{
  118. SignedHeader: &SignedHeader{
  119. Header: differentHeader,
  120. Commit: differentCommit,
  121. },
  122. ValidatorSet: valSet,
  123. },
  124. CommonHeight: height - 1,
  125. }
  126. assert.NotEqual(t, lcae.Hash(), differentEv.Hash())
  127. // different common height should produce a different header
  128. differentEv = &LightClientAttackEvidence{
  129. ConflictingBlock: &LightBlock{
  130. SignedHeader: &SignedHeader{
  131. Header: header,
  132. Commit: differentCommit,
  133. },
  134. ValidatorSet: valSet,
  135. },
  136. CommonHeight: height - 2,
  137. }
  138. assert.NotEqual(t, lcae.Hash(), differentEv.Hash())
  139. assert.Equal(t, lcae.Height(), int64(4)) // Height should be the common Height
  140. assert.NotNil(t, lcae.Bytes())
  141. }
  142. func TestLightClientAttackEvidenceValidation(t *testing.T) {
  143. height := int64(5)
  144. voteSet, valSet, privVals := randVoteSet(height, 1, tmproto.PrecommitType, 10, 1)
  145. header := makeHeaderRandom()
  146. header.Height = height
  147. header.ValidatorsHash = valSet.Hash()
  148. blockID := makeBlockID(header.Hash(), math.MaxInt32, tmhash.Sum([]byte("partshash")))
  149. commit, err := MakeCommit(blockID, height, 1, voteSet, privVals, time.Now())
  150. require.NoError(t, err)
  151. lcae := &LightClientAttackEvidence{
  152. ConflictingBlock: &LightBlock{
  153. SignedHeader: &SignedHeader{
  154. Header: header,
  155. Commit: commit,
  156. },
  157. ValidatorSet: valSet,
  158. },
  159. CommonHeight: height - 1,
  160. }
  161. assert.NoError(t, lcae.ValidateBasic())
  162. testCases := []struct {
  163. testName string
  164. malleateEvidence func(*LightClientAttackEvidence)
  165. expectErr bool
  166. }{
  167. {"Good DuplicateVoteEvidence", func(ev *LightClientAttackEvidence) {}, false},
  168. {"Negative height", func(ev *LightClientAttackEvidence) { ev.CommonHeight = -10 }, true},
  169. {"Height is greater than divergent block", func(ev *LightClientAttackEvidence) {
  170. ev.CommonHeight = height + 1
  171. }, true},
  172. {"Nil conflicting header", func(ev *LightClientAttackEvidence) { ev.ConflictingBlock.Header = nil }, true},
  173. {"Nil conflicting blocl", func(ev *LightClientAttackEvidence) { ev.ConflictingBlock = nil }, true},
  174. {"Nil validator set", func(ev *LightClientAttackEvidence) {
  175. ev.ConflictingBlock.ValidatorSet = &ValidatorSet{}
  176. }, true},
  177. }
  178. for _, tc := range testCases {
  179. tc := tc
  180. t.Run(tc.testName, func(t *testing.T) {
  181. lcae := &LightClientAttackEvidence{
  182. ConflictingBlock: &LightBlock{
  183. SignedHeader: &SignedHeader{
  184. Header: header,
  185. Commit: commit,
  186. },
  187. ValidatorSet: valSet,
  188. },
  189. CommonHeight: height - 1,
  190. }
  191. tc.malleateEvidence(lcae)
  192. if tc.expectErr {
  193. assert.Error(t, lcae.ValidateBasic(), tc.testName)
  194. } else {
  195. assert.NoError(t, lcae.ValidateBasic(), tc.testName)
  196. }
  197. })
  198. }
  199. }
  200. func TestMockEvidenceValidateBasic(t *testing.T) {
  201. goodEvidence := NewMockDuplicateVoteEvidence(int64(1), time.Now(), "mock-chain-id")
  202. assert.Nil(t, goodEvidence.ValidateBasic())
  203. }
  204. func makeVote(
  205. t *testing.T, val PrivValidator, chainID string, valIndex int32, height int64, round int32, step int, blockID BlockID,
  206. time time.Time) *Vote {
  207. pubKey, err := val.GetPubKey()
  208. require.NoError(t, err)
  209. v := &Vote{
  210. ValidatorAddress: pubKey.Address(),
  211. ValidatorIndex: valIndex,
  212. Height: height,
  213. Round: round,
  214. Type: tmproto.SignedMsgType(step),
  215. BlockID: blockID,
  216. Timestamp: time,
  217. }
  218. vpb := v.ToProto()
  219. err = val.SignVote(chainID, vpb)
  220. if err != nil {
  221. panic(err)
  222. }
  223. v.Signature = vpb.Signature
  224. return v
  225. }
  226. func makeHeaderRandom() *Header {
  227. return &Header{
  228. Version: version.Consensus{Block: version.BlockProtocol, App: 1},
  229. ChainID: tmrand.Str(12),
  230. Height: int64(tmrand.Uint16()) + 1,
  231. Time: time.Now(),
  232. LastBlockID: makeBlockIDRandom(),
  233. LastCommitHash: crypto.CRandBytes(tmhash.Size),
  234. DataHash: crypto.CRandBytes(tmhash.Size),
  235. ValidatorsHash: crypto.CRandBytes(tmhash.Size),
  236. NextValidatorsHash: crypto.CRandBytes(tmhash.Size),
  237. ConsensusHash: crypto.CRandBytes(tmhash.Size),
  238. AppHash: crypto.CRandBytes(tmhash.Size),
  239. LastResultsHash: crypto.CRandBytes(tmhash.Size),
  240. EvidenceHash: crypto.CRandBytes(tmhash.Size),
  241. ProposerAddress: crypto.CRandBytes(crypto.AddressSize),
  242. }
  243. }
  244. func TestEvidenceProto(t *testing.T) {
  245. // -------- Votes --------
  246. val := NewMockPV()
  247. blockID := makeBlockID(tmhash.Sum([]byte("blockhash")), math.MaxInt32, tmhash.Sum([]byte("partshash")))
  248. blockID2 := makeBlockID(tmhash.Sum([]byte("blockhash2")), math.MaxInt32, tmhash.Sum([]byte("partshash")))
  249. const chainID = "mychain"
  250. v := makeVote(t, val, chainID, math.MaxInt32, math.MaxInt64, 1, 0x01, blockID, defaultVoteTime)
  251. v2 := makeVote(t, val, chainID, math.MaxInt32, math.MaxInt64, 2, 0x01, blockID2, defaultVoteTime)
  252. // -------- SignedHeaders --------
  253. const height int64 = 37
  254. var (
  255. header1 = makeHeaderRandom()
  256. header2 = makeHeaderRandom()
  257. )
  258. header1.Height = height
  259. header1.LastBlockID = blockID
  260. header1.ChainID = chainID
  261. header2.Height = height
  262. header2.LastBlockID = blockID
  263. header2.ChainID = chainID
  264. tests := []struct {
  265. testName string
  266. evidence Evidence
  267. toProtoErr bool
  268. fromProtoErr bool
  269. }{
  270. {"nil fail", nil, true, true},
  271. {"DuplicateVoteEvidence empty fail", &DuplicateVoteEvidence{}, false, true},
  272. {"DuplicateVoteEvidence nil voteB", &DuplicateVoteEvidence{VoteA: v, VoteB: nil}, false, true},
  273. {"DuplicateVoteEvidence nil voteA", &DuplicateVoteEvidence{VoteA: nil, VoteB: v}, false, true},
  274. {"DuplicateVoteEvidence success", &DuplicateVoteEvidence{VoteA: v2, VoteB: v}, false, false},
  275. }
  276. for _, tt := range tests {
  277. tt := tt
  278. t.Run(tt.testName, func(t *testing.T) {
  279. pb, err := EvidenceToProto(tt.evidence)
  280. if tt.toProtoErr {
  281. assert.Error(t, err, tt.testName)
  282. return
  283. }
  284. assert.NoError(t, err, tt.testName)
  285. evi, err := EvidenceFromProto(pb)
  286. if tt.fromProtoErr {
  287. assert.Error(t, err, tt.testName)
  288. return
  289. }
  290. require.Equal(t, tt.evidence, evi, tt.testName)
  291. })
  292. }
  293. }