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.

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