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.

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