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.

329 lines
11 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 TestLightClientAttackEvidenceBasic(t *testing.T) {
  82. height := int64(5)
  83. commonHeight := height - 1
  84. nValidators := 10
  85. voteSet, valSet, privVals := randVoteSet(height, 1, tmproto.PrecommitType, nValidators, 1)
  86. header := makeHeaderRandom()
  87. header.Height = height
  88. blockID := makeBlockID(tmhash.Sum([]byte("blockhash")), math.MaxInt32, tmhash.Sum([]byte("partshash")))
  89. commit, err := MakeCommit(blockID, height, 1, voteSet, privVals, defaultVoteTime)
  90. require.NoError(t, err)
  91. lcae := &LightClientAttackEvidence{
  92. ConflictingBlock: &LightBlock{
  93. SignedHeader: &SignedHeader{
  94. Header: header,
  95. Commit: commit,
  96. },
  97. ValidatorSet: valSet,
  98. },
  99. CommonHeight: commonHeight,
  100. TotalVotingPower: valSet.TotalVotingPower(),
  101. Timestamp: header.Time,
  102. ByzantineValidators: valSet.Validators[:nValidators/2],
  103. }
  104. assert.NotNil(t, lcae.String())
  105. assert.NotNil(t, lcae.Hash())
  106. assert.Equal(t, lcae.Height(), commonHeight) // Height should be the common Height
  107. assert.NotNil(t, lcae.Bytes())
  108. // maleate evidence to test hash uniqueness
  109. testCases := []struct {
  110. testName string
  111. malleateEvidence func(*LightClientAttackEvidence)
  112. }{
  113. {"Different header", func(ev *LightClientAttackEvidence) { ev.ConflictingBlock.Header = makeHeaderRandom() }},
  114. {"Different common height", func(ev *LightClientAttackEvidence) {
  115. ev.CommonHeight = height + 1
  116. }},
  117. }
  118. for _, tc := range testCases {
  119. lcae := &LightClientAttackEvidence{
  120. ConflictingBlock: &LightBlock{
  121. SignedHeader: &SignedHeader{
  122. Header: header,
  123. Commit: commit,
  124. },
  125. ValidatorSet: valSet,
  126. },
  127. CommonHeight: commonHeight,
  128. TotalVotingPower: valSet.TotalVotingPower(),
  129. Timestamp: header.Time,
  130. ByzantineValidators: valSet.Validators[:nValidators/2],
  131. }
  132. hash := lcae.Hash()
  133. tc.malleateEvidence(lcae)
  134. assert.NotEqual(t, hash, lcae.Hash(), tc.testName)
  135. }
  136. }
  137. func TestLightClientAttackEvidenceValidation(t *testing.T) {
  138. height := int64(5)
  139. commonHeight := height - 1
  140. nValidators := 10
  141. voteSet, valSet, privVals := randVoteSet(height, 1, tmproto.PrecommitType, nValidators, 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: commonHeight,
  157. TotalVotingPower: valSet.TotalVotingPower(),
  158. Timestamp: header.Time,
  159. ByzantineValidators: valSet.Validators[:nValidators/2],
  160. }
  161. assert.NoError(t, lcae.ValidateBasic())
  162. testCases := []struct {
  163. testName string
  164. malleateEvidence func(*LightClientAttackEvidence)
  165. expectErr bool
  166. }{
  167. {"Good LightClientAttackEvidence", 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. {"Height is equal to the divergent block", func(ev *LightClientAttackEvidence) {
  173. ev.CommonHeight = height
  174. }, false},
  175. {"Nil conflicting header", func(ev *LightClientAttackEvidence) { ev.ConflictingBlock.Header = nil }, true},
  176. {"Nil conflicting blocl", func(ev *LightClientAttackEvidence) { ev.ConflictingBlock = nil }, true},
  177. {"Nil validator set", func(ev *LightClientAttackEvidence) {
  178. ev.ConflictingBlock.ValidatorSet = &ValidatorSet{}
  179. }, true},
  180. {"Negative total voting power", func(ev *LightClientAttackEvidence) {
  181. ev.TotalVotingPower = -1
  182. }, true},
  183. }
  184. for _, tc := range testCases {
  185. tc := tc
  186. t.Run(tc.testName, func(t *testing.T) {
  187. lcae := &LightClientAttackEvidence{
  188. ConflictingBlock: &LightBlock{
  189. SignedHeader: &SignedHeader{
  190. Header: header,
  191. Commit: commit,
  192. },
  193. ValidatorSet: valSet,
  194. },
  195. CommonHeight: commonHeight,
  196. TotalVotingPower: valSet.TotalVotingPower(),
  197. Timestamp: header.Time,
  198. ByzantineValidators: valSet.Validators[:nValidators/2],
  199. }
  200. tc.malleateEvidence(lcae)
  201. if tc.expectErr {
  202. assert.Error(t, lcae.ValidateBasic(), tc.testName)
  203. } else {
  204. assert.NoError(t, lcae.ValidateBasic(), tc.testName)
  205. }
  206. })
  207. }
  208. }
  209. func TestMockEvidenceValidateBasic(t *testing.T) {
  210. goodEvidence := NewMockDuplicateVoteEvidence(int64(1), time.Now(), "mock-chain-id")
  211. assert.Nil(t, goodEvidence.ValidateBasic())
  212. }
  213. func makeVote(
  214. t *testing.T, val PrivValidator, chainID string, valIndex int32, height int64, round int32, step int, blockID BlockID,
  215. time time.Time) *Vote {
  216. pubKey, err := val.GetPubKey(context.Background())
  217. require.NoError(t, err)
  218. v := &Vote{
  219. ValidatorAddress: pubKey.Address(),
  220. ValidatorIndex: valIndex,
  221. Height: height,
  222. Round: round,
  223. Type: tmproto.SignedMsgType(step),
  224. BlockID: blockID,
  225. Timestamp: time,
  226. }
  227. vpb := v.ToProto()
  228. err = val.SignVote(context.Background(), chainID, vpb)
  229. if err != nil {
  230. panic(err)
  231. }
  232. v.Signature = vpb.Signature
  233. return v
  234. }
  235. func makeHeaderRandom() *Header {
  236. return &Header{
  237. Version: version.Consensus{Block: version.BlockProtocol, App: 1},
  238. ChainID: tmrand.Str(12),
  239. Height: int64(tmrand.Uint16()) + 1,
  240. Time: time.Now(),
  241. LastBlockID: makeBlockIDRandom(),
  242. LastCommitHash: crypto.CRandBytes(tmhash.Size),
  243. DataHash: crypto.CRandBytes(tmhash.Size),
  244. ValidatorsHash: crypto.CRandBytes(tmhash.Size),
  245. NextValidatorsHash: crypto.CRandBytes(tmhash.Size),
  246. ConsensusHash: crypto.CRandBytes(tmhash.Size),
  247. AppHash: crypto.CRandBytes(tmhash.Size),
  248. LastResultsHash: crypto.CRandBytes(tmhash.Size),
  249. EvidenceHash: crypto.CRandBytes(tmhash.Size),
  250. ProposerAddress: crypto.CRandBytes(crypto.AddressSize),
  251. }
  252. }
  253. func TestEvidenceProto(t *testing.T) {
  254. // -------- Votes --------
  255. val := NewMockPV()
  256. blockID := makeBlockID(tmhash.Sum([]byte("blockhash")), math.MaxInt32, tmhash.Sum([]byte("partshash")))
  257. blockID2 := makeBlockID(tmhash.Sum([]byte("blockhash2")), math.MaxInt32, tmhash.Sum([]byte("partshash")))
  258. const chainID = "mychain"
  259. v := makeVote(t, val, chainID, math.MaxInt32, math.MaxInt64, 1, 0x01, blockID, defaultVoteTime)
  260. v2 := makeVote(t, val, chainID, math.MaxInt32, math.MaxInt64, 2, 0x01, blockID2, defaultVoteTime)
  261. // -------- SignedHeaders --------
  262. const height int64 = 37
  263. var (
  264. header1 = makeHeaderRandom()
  265. header2 = makeHeaderRandom()
  266. )
  267. header1.Height = height
  268. header1.LastBlockID = blockID
  269. header1.ChainID = chainID
  270. header2.Height = height
  271. header2.LastBlockID = blockID
  272. header2.ChainID = chainID
  273. tests := []struct {
  274. testName string
  275. evidence Evidence
  276. toProtoErr bool
  277. fromProtoErr bool
  278. }{
  279. {"nil fail", nil, true, true},
  280. {"DuplicateVoteEvidence empty fail", &DuplicateVoteEvidence{}, false, true},
  281. {"DuplicateVoteEvidence nil voteB", &DuplicateVoteEvidence{VoteA: v, VoteB: nil}, false, true},
  282. {"DuplicateVoteEvidence nil voteA", &DuplicateVoteEvidence{VoteA: nil, VoteB: v}, false, true},
  283. {"DuplicateVoteEvidence success", &DuplicateVoteEvidence{VoteA: v2, VoteB: v}, false, false},
  284. }
  285. for _, tt := range tests {
  286. tt := tt
  287. t.Run(tt.testName, func(t *testing.T) {
  288. pb, err := EvidenceToProto(tt.evidence)
  289. if tt.toProtoErr {
  290. assert.Error(t, err, tt.testName)
  291. return
  292. }
  293. assert.NoError(t, err, tt.testName)
  294. evi, err := EvidenceFromProto(pb)
  295. if tt.fromProtoErr {
  296. assert.Error(t, err, tt.testName)
  297. return
  298. }
  299. require.Equal(t, tt.evidence, evi, tt.testName)
  300. })
  301. }
  302. }