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.

609 lines
23 KiB

  1. package evidence_test
  2. import (
  3. "bytes"
  4. "context"
  5. "testing"
  6. "time"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/require"
  9. dbm "github.com/tendermint/tm-db"
  10. "github.com/tendermint/tendermint/crypto"
  11. "github.com/tendermint/tendermint/crypto/tmhash"
  12. "github.com/tendermint/tendermint/evidence"
  13. "github.com/tendermint/tendermint/evidence/mocks"
  14. "github.com/tendermint/tendermint/internal/test/factory"
  15. "github.com/tendermint/tendermint/libs/log"
  16. tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
  17. sm "github.com/tendermint/tendermint/state"
  18. smmocks "github.com/tendermint/tendermint/state/mocks"
  19. "github.com/tendermint/tendermint/types"
  20. )
  21. const (
  22. defaultVotingPower = 10
  23. )
  24. func TestVerifyLightClientAttack_Lunatic(t *testing.T) {
  25. const (
  26. height int64 = 10
  27. commonHeight int64 = 4
  28. totalVals = 10
  29. byzVals = 4
  30. )
  31. attackTime := defaultEvidenceTime.Add(1 * time.Hour)
  32. // create valid lunatic evidence
  33. ev, trusted, common := makeLunaticEvidence(
  34. t, height, commonHeight, totalVals, byzVals, totalVals-byzVals, defaultEvidenceTime, attackTime)
  35. require.NoError(t, ev.ValidateBasic())
  36. // good pass -> no error
  37. err := evidence.VerifyLightClientAttack(ev, common.SignedHeader, trusted.SignedHeader, common.ValidatorSet,
  38. defaultEvidenceTime.Add(2*time.Hour), 3*time.Hour)
  39. assert.NoError(t, err)
  40. // trusted and conflicting hashes are the same -> an error should be returned
  41. err = evidence.VerifyLightClientAttack(ev, common.SignedHeader, ev.ConflictingBlock.SignedHeader, common.ValidatorSet,
  42. defaultEvidenceTime.Add(2*time.Hour), 3*time.Hour)
  43. assert.Error(t, err)
  44. // evidence with different total validator power should fail
  45. ev.TotalVotingPower = 1 * defaultVotingPower
  46. err = evidence.VerifyLightClientAttack(ev, common.SignedHeader, trusted.SignedHeader, common.ValidatorSet,
  47. defaultEvidenceTime.Add(2*time.Hour), 3*time.Hour)
  48. assert.Error(t, err)
  49. // evidence without enough malicious votes should fail
  50. ev, trusted, common = makeLunaticEvidence(
  51. t, height, commonHeight, totalVals, byzVals-1, totalVals-byzVals, defaultEvidenceTime, attackTime)
  52. err = evidence.VerifyLightClientAttack(ev, common.SignedHeader, trusted.SignedHeader, common.ValidatorSet,
  53. defaultEvidenceTime.Add(2*time.Hour), 3*time.Hour)
  54. assert.Error(t, err)
  55. }
  56. func TestVerify_LunaticAttackAgainstState(t *testing.T) {
  57. const (
  58. height int64 = 10
  59. commonHeight int64 = 4
  60. totalVals = 10
  61. byzVals = 4
  62. )
  63. attackTime := defaultEvidenceTime.Add(1 * time.Hour)
  64. // create valid lunatic evidence
  65. ev, trusted, common := makeLunaticEvidence(
  66. t, height, commonHeight, totalVals, byzVals, totalVals-byzVals, defaultEvidenceTime, attackTime)
  67. // now we try to test verification against state
  68. state := sm.State{
  69. LastBlockTime: defaultEvidenceTime.Add(2 * time.Hour),
  70. LastBlockHeight: height + 1,
  71. ConsensusParams: *types.DefaultConsensusParams(),
  72. }
  73. stateStore := &smmocks.Store{}
  74. stateStore.On("LoadValidators", commonHeight).Return(common.ValidatorSet, nil)
  75. stateStore.On("Load").Return(state, nil)
  76. blockStore := &mocks.BlockStore{}
  77. blockStore.On("LoadBlockMeta", commonHeight).Return(&types.BlockMeta{Header: *common.Header})
  78. blockStore.On("LoadBlockMeta", height).Return(&types.BlockMeta{Header: *trusted.Header})
  79. blockStore.On("LoadBlockCommit", commonHeight).Return(common.Commit)
  80. blockStore.On("LoadBlockCommit", height).Return(trusted.Commit)
  81. pool, err := evidence.NewPool(log.TestingLogger(), dbm.NewMemDB(), stateStore, blockStore)
  82. require.NoError(t, err)
  83. evList := types.EvidenceList{ev}
  84. // check that the evidence pool correctly verifies the evidence
  85. assert.NoError(t, pool.CheckEvidence(evList))
  86. // as it was not originally in the pending bucket, it should now have been added
  87. pendingEvs, _ := pool.PendingEvidence(state.ConsensusParams.Evidence.MaxBytes)
  88. assert.Equal(t, 1, len(pendingEvs))
  89. assert.Equal(t, ev, pendingEvs[0])
  90. // if we submit evidence only against a single byzantine validator when we see there are more validators then this
  91. // should return an error
  92. ev.ByzantineValidators = ev.ByzantineValidators[:1]
  93. t.Log(evList)
  94. assert.Error(t, pool.CheckEvidence(evList))
  95. // restore original byz vals
  96. ev.ByzantineValidators = ev.GetByzantineValidators(common.ValidatorSet, trusted.SignedHeader)
  97. // duplicate evidence should be rejected
  98. evList = types.EvidenceList{ev, ev}
  99. pool, err = evidence.NewPool(log.TestingLogger(), dbm.NewMemDB(), stateStore, blockStore)
  100. require.NoError(t, err)
  101. assert.Error(t, pool.CheckEvidence(evList))
  102. // If evidence is submitted with an altered timestamp it should return an error
  103. ev.Timestamp = defaultEvidenceTime.Add(1 * time.Minute)
  104. pool, err = evidence.NewPool(log.TestingLogger(), dbm.NewMemDB(), stateStore, blockStore)
  105. require.NoError(t, err)
  106. assert.Error(t, pool.AddEvidence(ev))
  107. ev.Timestamp = defaultEvidenceTime
  108. // Evidence submitted with a different validator power should fail
  109. ev.TotalVotingPower = 1
  110. pool, err = evidence.NewPool(log.TestingLogger(), dbm.NewMemDB(), stateStore, blockStore)
  111. require.NoError(t, err)
  112. assert.Error(t, pool.AddEvidence(ev))
  113. ev.TotalVotingPower = common.ValidatorSet.TotalVotingPower()
  114. }
  115. func TestVerify_ForwardLunaticAttack(t *testing.T) {
  116. const (
  117. nodeHeight int64 = 8
  118. attackHeight int64 = 10
  119. commonHeight int64 = 4
  120. totalVals = 10
  121. byzVals = 5
  122. )
  123. attackTime := defaultEvidenceTime.Add(1 * time.Hour)
  124. // create a forward lunatic attack
  125. ev, trusted, common := makeLunaticEvidence(
  126. t, attackHeight, commonHeight, totalVals, byzVals, totalVals-byzVals, defaultEvidenceTime, attackTime)
  127. // now we try to test verification against state
  128. state := sm.State{
  129. LastBlockTime: defaultEvidenceTime.Add(2 * time.Hour),
  130. LastBlockHeight: nodeHeight,
  131. ConsensusParams: *types.DefaultConsensusParams(),
  132. }
  133. // modify trusted light block so that it is of a height less than the conflicting one
  134. trusted.Header.Height = state.LastBlockHeight
  135. trusted.Header.Time = state.LastBlockTime
  136. stateStore := &smmocks.Store{}
  137. stateStore.On("LoadValidators", commonHeight).Return(common.ValidatorSet, nil)
  138. stateStore.On("Load").Return(state, nil)
  139. blockStore := &mocks.BlockStore{}
  140. blockStore.On("LoadBlockMeta", commonHeight).Return(&types.BlockMeta{Header: *common.Header})
  141. blockStore.On("LoadBlockMeta", nodeHeight).Return(&types.BlockMeta{Header: *trusted.Header})
  142. blockStore.On("LoadBlockMeta", attackHeight).Return(nil)
  143. blockStore.On("LoadBlockCommit", commonHeight).Return(common.Commit)
  144. blockStore.On("LoadBlockCommit", nodeHeight).Return(trusted.Commit)
  145. blockStore.On("Height").Return(nodeHeight)
  146. pool, err := evidence.NewPool(log.TestingLogger(), dbm.NewMemDB(), stateStore, blockStore)
  147. require.NoError(t, err)
  148. // check that the evidence pool correctly verifies the evidence
  149. assert.NoError(t, pool.CheckEvidence(types.EvidenceList{ev}))
  150. // now we use a time which isn't able to contradict the FLA - thus we can't verify the evidence
  151. oldBlockStore := &mocks.BlockStore{}
  152. oldHeader := trusted.Header
  153. oldHeader.Time = defaultEvidenceTime
  154. oldBlockStore.On("LoadBlockMeta", commonHeight).Return(&types.BlockMeta{Header: *common.Header})
  155. oldBlockStore.On("LoadBlockMeta", nodeHeight).Return(&types.BlockMeta{Header: *oldHeader})
  156. oldBlockStore.On("LoadBlockMeta", attackHeight).Return(nil)
  157. oldBlockStore.On("LoadBlockCommit", commonHeight).Return(common.Commit)
  158. oldBlockStore.On("LoadBlockCommit", nodeHeight).Return(trusted.Commit)
  159. oldBlockStore.On("Height").Return(nodeHeight)
  160. require.Equal(t, defaultEvidenceTime, oldBlockStore.LoadBlockMeta(nodeHeight).Header.Time)
  161. pool, err = evidence.NewPool(log.TestingLogger(), dbm.NewMemDB(), stateStore, oldBlockStore)
  162. require.NoError(t, err)
  163. assert.Error(t, pool.CheckEvidence(types.EvidenceList{ev}))
  164. }
  165. func TestVerifyLightClientAttack_Equivocation(t *testing.T) {
  166. conflictingVals, conflictingPrivVals := factory.RandValidatorSet(5, 10)
  167. conflictingHeader, err := factory.MakeHeader(&types.Header{
  168. ChainID: evidenceChainID,
  169. Height: 10,
  170. Time: defaultEvidenceTime,
  171. ValidatorsHash: conflictingVals.Hash(),
  172. })
  173. require.NoError(t, err)
  174. trustedHeader, _ := factory.MakeHeader(&types.Header{
  175. ChainID: evidenceChainID,
  176. Height: 10,
  177. Time: defaultEvidenceTime,
  178. ValidatorsHash: conflictingHeader.ValidatorsHash,
  179. NextValidatorsHash: conflictingHeader.NextValidatorsHash,
  180. ConsensusHash: conflictingHeader.ConsensusHash,
  181. AppHash: conflictingHeader.AppHash,
  182. LastResultsHash: conflictingHeader.LastResultsHash,
  183. })
  184. // we are simulating a duplicate vote attack where all the validators in the conflictingVals set
  185. // except the last validator vote twice
  186. blockID := factory.MakeBlockIDWithHash(conflictingHeader.Hash())
  187. voteSet := types.NewVoteSet(evidenceChainID, 10, 1, tmproto.SignedMsgType(2), conflictingVals)
  188. commit, err := factory.MakeCommit(blockID, 10, 1, voteSet, conflictingPrivVals[:4], defaultEvidenceTime)
  189. require.NoError(t, err)
  190. ev := &types.LightClientAttackEvidence{
  191. ConflictingBlock: &types.LightBlock{
  192. SignedHeader: &types.SignedHeader{
  193. Header: conflictingHeader,
  194. Commit: commit,
  195. },
  196. ValidatorSet: conflictingVals,
  197. },
  198. CommonHeight: 10,
  199. ByzantineValidators: conflictingVals.Validators[:4],
  200. TotalVotingPower: 50,
  201. Timestamp: defaultEvidenceTime,
  202. }
  203. trustedBlockID := makeBlockID(trustedHeader.Hash(), 1000, []byte("partshash"))
  204. trustedVoteSet := types.NewVoteSet(evidenceChainID, 10, 1, tmproto.SignedMsgType(2), conflictingVals)
  205. trustedCommit, err := factory.MakeCommit(trustedBlockID, 10, 1,
  206. trustedVoteSet, conflictingPrivVals, defaultEvidenceTime)
  207. require.NoError(t, err)
  208. trustedSignedHeader := &types.SignedHeader{
  209. Header: trustedHeader,
  210. Commit: trustedCommit,
  211. }
  212. // good pass -> no error
  213. err = evidence.VerifyLightClientAttack(ev, trustedSignedHeader, trustedSignedHeader, conflictingVals,
  214. defaultEvidenceTime.Add(1*time.Minute), 2*time.Hour)
  215. assert.NoError(t, err)
  216. // trusted and conflicting hashes are the same -> an error should be returned
  217. err = evidence.VerifyLightClientAttack(ev, trustedSignedHeader, ev.ConflictingBlock.SignedHeader, conflictingVals,
  218. defaultEvidenceTime.Add(1*time.Minute), 2*time.Hour)
  219. assert.Error(t, err)
  220. // conflicting header has different next validators hash which should have been correctly derived from
  221. // the previous round
  222. ev.ConflictingBlock.Header.NextValidatorsHash = crypto.CRandBytes(tmhash.Size)
  223. err = evidence.VerifyLightClientAttack(ev, trustedSignedHeader, trustedSignedHeader, nil,
  224. defaultEvidenceTime.Add(1*time.Minute), 2*time.Hour)
  225. assert.Error(t, err)
  226. // revert next validators hash
  227. ev.ConflictingBlock.Header.NextValidatorsHash = trustedHeader.NextValidatorsHash
  228. state := sm.State{
  229. LastBlockTime: defaultEvidenceTime.Add(1 * time.Minute),
  230. LastBlockHeight: 11,
  231. ConsensusParams: *types.DefaultConsensusParams(),
  232. }
  233. stateStore := &smmocks.Store{}
  234. stateStore.On("LoadValidators", int64(10)).Return(conflictingVals, nil)
  235. stateStore.On("Load").Return(state, nil)
  236. blockStore := &mocks.BlockStore{}
  237. blockStore.On("LoadBlockMeta", int64(10)).Return(&types.BlockMeta{Header: *trustedHeader})
  238. blockStore.On("LoadBlockCommit", int64(10)).Return(trustedCommit)
  239. pool, err := evidence.NewPool(log.TestingLogger(), dbm.NewMemDB(), stateStore, blockStore)
  240. require.NoError(t, err)
  241. evList := types.EvidenceList{ev}
  242. err = pool.CheckEvidence(evList)
  243. assert.NoError(t, err)
  244. pendingEvs, _ := pool.PendingEvidence(state.ConsensusParams.Evidence.MaxBytes)
  245. assert.Equal(t, 1, len(pendingEvs))
  246. }
  247. func TestVerifyLightClientAttack_Amnesia(t *testing.T) {
  248. var height int64 = 10
  249. conflictingVals, conflictingPrivVals := factory.RandValidatorSet(5, 10)
  250. conflictingHeader, err := factory.MakeHeader(&types.Header{
  251. ChainID: evidenceChainID,
  252. Height: height,
  253. Time: defaultEvidenceTime,
  254. ValidatorsHash: conflictingVals.Hash(),
  255. })
  256. require.NoError(t, err)
  257. trustedHeader, _ := factory.MakeHeader(&types.Header{
  258. ChainID: evidenceChainID,
  259. Height: height,
  260. Time: defaultEvidenceTime,
  261. ValidatorsHash: conflictingHeader.ValidatorsHash,
  262. NextValidatorsHash: conflictingHeader.NextValidatorsHash,
  263. ConsensusHash: conflictingHeader.ConsensusHash,
  264. AppHash: conflictingHeader.AppHash,
  265. LastResultsHash: conflictingHeader.LastResultsHash,
  266. })
  267. // we are simulating an amnesia attack where all the validators in the conflictingVals set
  268. // except the last validator vote twice. However this time the commits are of different rounds.
  269. blockID := makeBlockID(conflictingHeader.Hash(), 1000, []byte("partshash"))
  270. voteSet := types.NewVoteSet(evidenceChainID, height, 0, tmproto.SignedMsgType(2), conflictingVals)
  271. commit, err := factory.MakeCommit(blockID, height, 0, voteSet, conflictingPrivVals, defaultEvidenceTime)
  272. require.NoError(t, err)
  273. ev := &types.LightClientAttackEvidence{
  274. ConflictingBlock: &types.LightBlock{
  275. SignedHeader: &types.SignedHeader{
  276. Header: conflictingHeader,
  277. Commit: commit,
  278. },
  279. ValidatorSet: conflictingVals,
  280. },
  281. CommonHeight: height,
  282. ByzantineValidators: nil, // with amnesia evidence no validators are submitted as abci evidence
  283. TotalVotingPower: 50,
  284. Timestamp: defaultEvidenceTime,
  285. }
  286. trustedBlockID := makeBlockID(trustedHeader.Hash(), 1000, []byte("partshash"))
  287. trustedVoteSet := types.NewVoteSet(evidenceChainID, height, 1, tmproto.SignedMsgType(2), conflictingVals)
  288. trustedCommit, err := factory.MakeCommit(trustedBlockID, height, 1,
  289. trustedVoteSet, conflictingPrivVals, defaultEvidenceTime)
  290. require.NoError(t, err)
  291. trustedSignedHeader := &types.SignedHeader{
  292. Header: trustedHeader,
  293. Commit: trustedCommit,
  294. }
  295. // good pass -> no error
  296. err = evidence.VerifyLightClientAttack(ev, trustedSignedHeader, trustedSignedHeader, conflictingVals,
  297. defaultEvidenceTime.Add(1*time.Minute), 2*time.Hour)
  298. assert.NoError(t, err)
  299. // trusted and conflicting hashes are the same -> an error should be returned
  300. err = evidence.VerifyLightClientAttack(ev, trustedSignedHeader, ev.ConflictingBlock.SignedHeader, conflictingVals,
  301. defaultEvidenceTime.Add(1*time.Minute), 2*time.Hour)
  302. assert.Error(t, err)
  303. state := sm.State{
  304. LastBlockTime: defaultEvidenceTime.Add(1 * time.Minute),
  305. LastBlockHeight: 11,
  306. ConsensusParams: *types.DefaultConsensusParams(),
  307. }
  308. stateStore := &smmocks.Store{}
  309. stateStore.On("LoadValidators", int64(10)).Return(conflictingVals, nil)
  310. stateStore.On("Load").Return(state, nil)
  311. blockStore := &mocks.BlockStore{}
  312. blockStore.On("LoadBlockMeta", int64(10)).Return(&types.BlockMeta{Header: *trustedHeader})
  313. blockStore.On("LoadBlockCommit", int64(10)).Return(trustedCommit)
  314. pool, err := evidence.NewPool(log.TestingLogger(), dbm.NewMemDB(), stateStore, blockStore)
  315. require.NoError(t, err)
  316. evList := types.EvidenceList{ev}
  317. err = pool.CheckEvidence(evList)
  318. assert.NoError(t, err)
  319. pendingEvs, _ := pool.PendingEvidence(state.ConsensusParams.Evidence.MaxBytes)
  320. assert.Equal(t, 1, len(pendingEvs))
  321. }
  322. type voteData struct {
  323. vote1 *types.Vote
  324. vote2 *types.Vote
  325. valid bool
  326. }
  327. func TestVerifyDuplicateVoteEvidence(t *testing.T) {
  328. val := types.NewMockPV()
  329. val2 := types.NewMockPV()
  330. valSet := types.NewValidatorSet([]*types.Validator{val.ExtractIntoValidator(1)})
  331. blockID := makeBlockID([]byte("blockhash"), 1000, []byte("partshash"))
  332. blockID2 := makeBlockID([]byte("blockhash2"), 1000, []byte("partshash"))
  333. blockID3 := makeBlockID([]byte("blockhash"), 10000, []byte("partshash"))
  334. blockID4 := makeBlockID([]byte("blockhash"), 10000, []byte("partshash2"))
  335. const chainID = "mychain"
  336. vote1 := makeVote(t, val, chainID, 0, 10, 2, 1, blockID, defaultEvidenceTime)
  337. v1 := vote1.ToProto()
  338. err := val.SignVote(context.Background(), chainID, v1)
  339. require.NoError(t, err)
  340. badVote := makeVote(t, val, chainID, 0, 10, 2, 1, blockID, defaultEvidenceTime)
  341. bv := badVote.ToProto()
  342. err = val2.SignVote(context.Background(), chainID, bv)
  343. require.NoError(t, err)
  344. vote1.Signature = v1.Signature
  345. badVote.Signature = bv.Signature
  346. cases := []voteData{
  347. {vote1, makeVote(t, val, chainID, 0, 10, 2, 1, blockID2, defaultEvidenceTime), true}, // different block ids
  348. {vote1, makeVote(t, val, chainID, 0, 10, 2, 1, blockID3, defaultEvidenceTime), true},
  349. {vote1, makeVote(t, val, chainID, 0, 10, 2, 1, blockID4, defaultEvidenceTime), true},
  350. {vote1, makeVote(t, val, chainID, 0, 10, 2, 1, blockID, defaultEvidenceTime), false}, // wrong block id
  351. {vote1, makeVote(t, val, "mychain2", 0, 10, 2, 1, blockID2, defaultEvidenceTime), false}, // wrong chain id
  352. {vote1, makeVote(t, val, chainID, 0, 11, 2, 1, blockID2, defaultEvidenceTime), false}, // wrong height
  353. {vote1, makeVote(t, val, chainID, 0, 10, 3, 1, blockID2, defaultEvidenceTime), false}, // wrong round
  354. {vote1, makeVote(t, val, chainID, 0, 10, 2, 2, blockID2, defaultEvidenceTime), false}, // wrong step
  355. {vote1, makeVote(t, val2, chainID, 0, 10, 2, 1, blockID2, defaultEvidenceTime), false}, // wrong validator
  356. // a different vote time doesn't matter
  357. {vote1, makeVote(t, val, chainID, 0, 10, 2, 1, blockID2, time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)), true},
  358. {vote1, badVote, false}, // signed by wrong key
  359. }
  360. require.NoError(t, err)
  361. for _, c := range cases {
  362. ev := &types.DuplicateVoteEvidence{
  363. VoteA: c.vote1,
  364. VoteB: c.vote2,
  365. ValidatorPower: 1,
  366. TotalVotingPower: 1,
  367. Timestamp: defaultEvidenceTime,
  368. }
  369. if c.valid {
  370. assert.Nil(t, evidence.VerifyDuplicateVote(ev, chainID, valSet), "evidence should be valid")
  371. } else {
  372. assert.NotNil(t, evidence.VerifyDuplicateVote(ev, chainID, valSet), "evidence should be invalid")
  373. }
  374. }
  375. // create good evidence and correct validator power
  376. goodEv := types.NewMockDuplicateVoteEvidenceWithValidator(10, defaultEvidenceTime, val, chainID)
  377. goodEv.ValidatorPower = 1
  378. goodEv.TotalVotingPower = 1
  379. badEv := types.NewMockDuplicateVoteEvidenceWithValidator(10, defaultEvidenceTime, val, chainID)
  380. badTimeEv := types.NewMockDuplicateVoteEvidenceWithValidator(10, defaultEvidenceTime.Add(1*time.Minute), val, chainID)
  381. badTimeEv.ValidatorPower = 1
  382. badTimeEv.TotalVotingPower = 1
  383. state := sm.State{
  384. ChainID: chainID,
  385. LastBlockTime: defaultEvidenceTime.Add(1 * time.Minute),
  386. LastBlockHeight: 11,
  387. ConsensusParams: *types.DefaultConsensusParams(),
  388. }
  389. stateStore := &smmocks.Store{}
  390. stateStore.On("LoadValidators", int64(10)).Return(valSet, nil)
  391. stateStore.On("Load").Return(state, nil)
  392. blockStore := &mocks.BlockStore{}
  393. blockStore.On("LoadBlockMeta", int64(10)).Return(&types.BlockMeta{Header: types.Header{Time: defaultEvidenceTime}})
  394. pool, err := evidence.NewPool(log.TestingLogger(), dbm.NewMemDB(), stateStore, blockStore)
  395. require.NoError(t, err)
  396. evList := types.EvidenceList{goodEv}
  397. err = pool.CheckEvidence(evList)
  398. assert.NoError(t, err)
  399. // evidence with a different validator power should fail
  400. evList = types.EvidenceList{badEv}
  401. err = pool.CheckEvidence(evList)
  402. assert.Error(t, err)
  403. // evidence with a different timestamp should fail
  404. evList = types.EvidenceList{badTimeEv}
  405. err = pool.CheckEvidence(evList)
  406. assert.Error(t, err)
  407. }
  408. func makeLunaticEvidence(
  409. t *testing.T,
  410. height, commonHeight int64,
  411. totalVals, byzVals, phantomVals int,
  412. commonTime, attackTime time.Time,
  413. ) (ev *types.LightClientAttackEvidence, trusted *types.LightBlock, common *types.LightBlock) {
  414. commonValSet, commonPrivVals := factory.RandValidatorSet(totalVals, defaultVotingPower)
  415. require.Greater(t, totalVals, byzVals)
  416. // extract out the subset of byzantine validators in the common validator set
  417. byzValSet, byzPrivVals := commonValSet.Validators[:byzVals], commonPrivVals[:byzVals]
  418. phantomValSet, phantomPrivVals := factory.RandValidatorSet(phantomVals, defaultVotingPower)
  419. conflictingVals := phantomValSet.Copy()
  420. require.NoError(t, conflictingVals.UpdateWithChangeSet(byzValSet))
  421. conflictingPrivVals := append(phantomPrivVals, byzPrivVals...)
  422. conflictingPrivVals = orderPrivValsByValSet(t, conflictingVals, conflictingPrivVals)
  423. commonHeader, err := factory.MakeHeader(&types.Header{
  424. ChainID: evidenceChainID,
  425. Height: commonHeight,
  426. Time: commonTime,
  427. })
  428. require.NoError(t, err)
  429. trustedHeader, err := factory.MakeHeader(&types.Header{
  430. ChainID: evidenceChainID,
  431. Height: height,
  432. Time: defaultEvidenceTime,
  433. })
  434. require.NoError(t, err)
  435. conflictingHeader, err := factory.MakeHeader(&types.Header{
  436. ChainID: evidenceChainID,
  437. Height: height,
  438. Time: attackTime,
  439. ValidatorsHash: conflictingVals.Hash(),
  440. })
  441. require.NoError(t, err)
  442. blockID := factory.MakeBlockIDWithHash(conflictingHeader.Hash())
  443. voteSet := types.NewVoteSet(evidenceChainID, height, 1, tmproto.SignedMsgType(2), conflictingVals)
  444. commit, err := factory.MakeCommit(blockID, height, 1, voteSet, conflictingPrivVals, defaultEvidenceTime)
  445. require.NoError(t, err)
  446. ev = &types.LightClientAttackEvidence{
  447. ConflictingBlock: &types.LightBlock{
  448. SignedHeader: &types.SignedHeader{
  449. Header: conflictingHeader,
  450. Commit: commit,
  451. },
  452. ValidatorSet: conflictingVals,
  453. },
  454. CommonHeight: commonHeight,
  455. TotalVotingPower: commonValSet.TotalVotingPower(),
  456. ByzantineValidators: byzValSet,
  457. Timestamp: commonTime,
  458. }
  459. common = &types.LightBlock{
  460. SignedHeader: &types.SignedHeader{
  461. Header: commonHeader,
  462. // we can leave this empty because we shouldn't be checking this
  463. Commit: &types.Commit{},
  464. },
  465. ValidatorSet: commonValSet,
  466. }
  467. trustedBlockID := factory.MakeBlockIDWithHash(trustedHeader.Hash())
  468. trustedVals, privVals := factory.RandValidatorSet(totalVals, defaultVotingPower)
  469. trustedVoteSet := types.NewVoteSet(evidenceChainID, height, 1, tmproto.SignedMsgType(2), trustedVals)
  470. trustedCommit, err := factory.MakeCommit(trustedBlockID, height, 1, trustedVoteSet, privVals, defaultEvidenceTime)
  471. require.NoError(t, err)
  472. trusted = &types.LightBlock{
  473. SignedHeader: &types.SignedHeader{
  474. Header: trustedHeader,
  475. Commit: trustedCommit,
  476. },
  477. ValidatorSet: trustedVals,
  478. }
  479. return ev, trusted, common
  480. }
  481. func makeVote(
  482. t *testing.T, val types.PrivValidator, chainID string, valIndex int32, height int64,
  483. round int32, step int, blockID types.BlockID, time time.Time) *types.Vote {
  484. pubKey, err := val.GetPubKey(context.Background())
  485. require.NoError(t, err)
  486. v := &types.Vote{
  487. ValidatorAddress: pubKey.Address(),
  488. ValidatorIndex: valIndex,
  489. Height: height,
  490. Round: round,
  491. Type: tmproto.SignedMsgType(step),
  492. BlockID: blockID,
  493. Timestamp: time,
  494. }
  495. vpb := v.ToProto()
  496. err = val.SignVote(context.Background(), chainID, vpb)
  497. if err != nil {
  498. panic(err)
  499. }
  500. v.Signature = vpb.Signature
  501. return v
  502. }
  503. func makeBlockID(hash []byte, partSetSize uint32, partSetHash []byte) types.BlockID {
  504. var (
  505. h = make([]byte, tmhash.Size)
  506. psH = make([]byte, tmhash.Size)
  507. )
  508. copy(h, hash)
  509. copy(psH, partSetHash)
  510. return types.BlockID{
  511. Hash: h,
  512. PartSetHeader: types.PartSetHeader{
  513. Total: partSetSize,
  514. Hash: psH,
  515. },
  516. }
  517. }
  518. func orderPrivValsByValSet(
  519. t *testing.T, vals *types.ValidatorSet, privVals []types.PrivValidator) []types.PrivValidator {
  520. output := make([]types.PrivValidator, len(privVals))
  521. for idx, v := range vals.Validators {
  522. for _, p := range privVals {
  523. pubKey, err := p.GetPubKey(context.Background())
  524. require.NoError(t, err)
  525. if bytes.Equal(v.Address, pubKey.Address()) {
  526. output[idx] = p
  527. break
  528. }
  529. }
  530. require.NotEmpty(t, output[idx])
  531. }
  532. return output
  533. }