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.

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