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.

595 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. Height: 10,
  169. Time: defaultEvidenceTime,
  170. ValidatorsHash: conflictingVals.Hash(),
  171. })
  172. require.NoError(t, err)
  173. trustedHeader, _ := factory.MakeHeader(&types.Header{
  174. Height: 10,
  175. Time: defaultEvidenceTime,
  176. ValidatorsHash: conflictingHeader.ValidatorsHash,
  177. NextValidatorsHash: conflictingHeader.NextValidatorsHash,
  178. ConsensusHash: conflictingHeader.ConsensusHash,
  179. AppHash: conflictingHeader.AppHash,
  180. LastResultsHash: conflictingHeader.LastResultsHash,
  181. })
  182. // we are simulating a duplicate vote attack where all the validators in the conflictingVals set
  183. // except the last validator vote twice
  184. blockID := factory.MakeBlockIDWithHash(conflictingHeader.Hash())
  185. voteSet := types.NewVoteSet(evidenceChainID, 10, 1, tmproto.SignedMsgType(2), conflictingVals)
  186. commit, err := factory.MakeCommit(blockID, 10, 1, voteSet, conflictingPrivVals[:4], defaultEvidenceTime)
  187. require.NoError(t, err)
  188. ev := &types.LightClientAttackEvidence{
  189. ConflictingBlock: &types.LightBlock{
  190. SignedHeader: &types.SignedHeader{
  191. Header: conflictingHeader,
  192. Commit: commit,
  193. },
  194. ValidatorSet: conflictingVals,
  195. },
  196. CommonHeight: 10,
  197. ByzantineValidators: conflictingVals.Validators[:4],
  198. TotalVotingPower: 50,
  199. Timestamp: defaultEvidenceTime,
  200. }
  201. trustedBlockID := makeBlockID(trustedHeader.Hash(), 1000, []byte("partshash"))
  202. trustedVoteSet := types.NewVoteSet(evidenceChainID, 10, 1, tmproto.SignedMsgType(2), conflictingVals)
  203. trustedCommit, err := factory.MakeCommit(trustedBlockID, 10, 1,
  204. trustedVoteSet, conflictingPrivVals, defaultEvidenceTime)
  205. require.NoError(t, err)
  206. trustedSignedHeader := &types.SignedHeader{
  207. Header: trustedHeader,
  208. Commit: trustedCommit,
  209. }
  210. // good pass -> no error
  211. err = evidence.VerifyLightClientAttack(ev, trustedSignedHeader, trustedSignedHeader, conflictingVals,
  212. defaultEvidenceTime.Add(1*time.Minute), 2*time.Hour)
  213. assert.NoError(t, err)
  214. // trusted and conflicting hashes are the same -> an error should be returned
  215. err = evidence.VerifyLightClientAttack(ev, trustedSignedHeader, ev.ConflictingBlock.SignedHeader, conflictingVals,
  216. defaultEvidenceTime.Add(1*time.Minute), 2*time.Hour)
  217. assert.Error(t, err)
  218. // conflicting header has different next validators hash which should have been correctly derived from
  219. // the previous round
  220. ev.ConflictingBlock.Header.NextValidatorsHash = crypto.CRandBytes(tmhash.Size)
  221. err = evidence.VerifyLightClientAttack(ev, trustedSignedHeader, trustedSignedHeader, nil,
  222. defaultEvidenceTime.Add(1*time.Minute), 2*time.Hour)
  223. assert.Error(t, err)
  224. // revert next validators hash
  225. ev.ConflictingBlock.Header.NextValidatorsHash = trustedHeader.NextValidatorsHash
  226. state := sm.State{
  227. LastBlockTime: defaultEvidenceTime.Add(1 * time.Minute),
  228. LastBlockHeight: 11,
  229. ConsensusParams: *types.DefaultConsensusParams(),
  230. }
  231. stateStore := &smmocks.Store{}
  232. stateStore.On("LoadValidators", int64(10)).Return(conflictingVals, nil)
  233. stateStore.On("Load").Return(state, nil)
  234. blockStore := &mocks.BlockStore{}
  235. blockStore.On("LoadBlockMeta", int64(10)).Return(&types.BlockMeta{Header: *trustedHeader})
  236. blockStore.On("LoadBlockCommit", int64(10)).Return(trustedCommit)
  237. pool, err := evidence.NewPool(log.TestingLogger(), dbm.NewMemDB(), stateStore, blockStore)
  238. require.NoError(t, err)
  239. evList := types.EvidenceList{ev}
  240. err = pool.CheckEvidence(evList)
  241. assert.NoError(t, err)
  242. pendingEvs, _ := pool.PendingEvidence(state.ConsensusParams.Evidence.MaxBytes)
  243. assert.Equal(t, 1, len(pendingEvs))
  244. }
  245. func TestVerifyLightClientAttack_Amnesia(t *testing.T) {
  246. var height int64 = 10
  247. conflictingVals, conflictingPrivVals := factory.RandValidatorSet(5, 10)
  248. conflictingHeader, err := factory.MakeHeader(&types.Header{
  249. Height: height,
  250. Time: defaultEvidenceTime,
  251. ValidatorsHash: conflictingVals.Hash(),
  252. })
  253. require.NoError(t, err)
  254. trustedHeader, _ := factory.MakeHeader(&types.Header{
  255. Height: height,
  256. Time: defaultEvidenceTime,
  257. ValidatorsHash: conflictingHeader.ValidatorsHash,
  258. NextValidatorsHash: conflictingHeader.NextValidatorsHash,
  259. ConsensusHash: conflictingHeader.ConsensusHash,
  260. AppHash: conflictingHeader.AppHash,
  261. LastResultsHash: conflictingHeader.LastResultsHash,
  262. })
  263. // we are simulating an amnesia attack where all the validators in the conflictingVals set
  264. // except the last validator vote twice. However this time the commits are of different rounds.
  265. blockID := makeBlockID(conflictingHeader.Hash(), 1000, []byte("partshash"))
  266. voteSet := types.NewVoteSet(evidenceChainID, height, 0, tmproto.SignedMsgType(2), conflictingVals)
  267. commit, err := factory.MakeCommit(blockID, height, 0, voteSet, conflictingPrivVals, defaultEvidenceTime)
  268. require.NoError(t, err)
  269. ev := &types.LightClientAttackEvidence{
  270. ConflictingBlock: &types.LightBlock{
  271. SignedHeader: &types.SignedHeader{
  272. Header: conflictingHeader,
  273. Commit: commit,
  274. },
  275. ValidatorSet: conflictingVals,
  276. },
  277. CommonHeight: height,
  278. ByzantineValidators: nil, // with amnesia evidence no validators are submitted as abci evidence
  279. TotalVotingPower: 50,
  280. Timestamp: defaultEvidenceTime,
  281. }
  282. trustedBlockID := makeBlockID(trustedHeader.Hash(), 1000, []byte("partshash"))
  283. trustedVoteSet := types.NewVoteSet(evidenceChainID, height, 1, tmproto.SignedMsgType(2), conflictingVals)
  284. trustedCommit, err := factory.MakeCommit(trustedBlockID, height, 1,
  285. trustedVoteSet, conflictingPrivVals, defaultEvidenceTime)
  286. require.NoError(t, err)
  287. trustedSignedHeader := &types.SignedHeader{
  288. Header: trustedHeader,
  289. Commit: trustedCommit,
  290. }
  291. // good pass -> no error
  292. err = evidence.VerifyLightClientAttack(ev, trustedSignedHeader, trustedSignedHeader, conflictingVals,
  293. defaultEvidenceTime.Add(1*time.Minute), 2*time.Hour)
  294. assert.NoError(t, err)
  295. // trusted and conflicting hashes are the same -> an error should be returned
  296. err = evidence.VerifyLightClientAttack(ev, trustedSignedHeader, ev.ConflictingBlock.SignedHeader, conflictingVals,
  297. defaultEvidenceTime.Add(1*time.Minute), 2*time.Hour)
  298. assert.Error(t, err)
  299. state := sm.State{
  300. LastBlockTime: defaultEvidenceTime.Add(1 * time.Minute),
  301. LastBlockHeight: 11,
  302. ConsensusParams: *types.DefaultConsensusParams(),
  303. }
  304. stateStore := &smmocks.Store{}
  305. stateStore.On("LoadValidators", int64(10)).Return(conflictingVals, nil)
  306. stateStore.On("Load").Return(state, nil)
  307. blockStore := &mocks.BlockStore{}
  308. blockStore.On("LoadBlockMeta", int64(10)).Return(&types.BlockMeta{Header: *trustedHeader})
  309. blockStore.On("LoadBlockCommit", int64(10)).Return(trustedCommit)
  310. pool, err := evidence.NewPool(log.TestingLogger(), dbm.NewMemDB(), stateStore, blockStore)
  311. require.NoError(t, err)
  312. evList := types.EvidenceList{ev}
  313. err = pool.CheckEvidence(evList)
  314. assert.NoError(t, err)
  315. pendingEvs, _ := pool.PendingEvidence(state.ConsensusParams.Evidence.MaxBytes)
  316. assert.Equal(t, 1, len(pendingEvs))
  317. }
  318. type voteData struct {
  319. vote1 *types.Vote
  320. vote2 *types.Vote
  321. valid bool
  322. }
  323. func TestVerifyDuplicateVoteEvidence(t *testing.T) {
  324. val := types.NewMockPV()
  325. val2 := types.NewMockPV()
  326. valSet := types.NewValidatorSet([]*types.Validator{val.ExtractIntoValidator(1)})
  327. blockID := makeBlockID([]byte("blockhash"), 1000, []byte("partshash"))
  328. blockID2 := makeBlockID([]byte("blockhash2"), 1000, []byte("partshash"))
  329. blockID3 := makeBlockID([]byte("blockhash"), 10000, []byte("partshash"))
  330. blockID4 := makeBlockID([]byte("blockhash"), 10000, []byte("partshash2"))
  331. const chainID = "mychain"
  332. vote1 := makeVote(t, val, chainID, 0, 10, 2, 1, blockID, defaultEvidenceTime)
  333. v1 := vote1.ToProto()
  334. err := val.SignVote(context.Background(), chainID, v1)
  335. require.NoError(t, err)
  336. badVote := makeVote(t, val, chainID, 0, 10, 2, 1, blockID, defaultEvidenceTime)
  337. bv := badVote.ToProto()
  338. err = val2.SignVote(context.Background(), chainID, bv)
  339. require.NoError(t, err)
  340. vote1.Signature = v1.Signature
  341. badVote.Signature = bv.Signature
  342. cases := []voteData{
  343. {vote1, makeVote(t, val, chainID, 0, 10, 2, 1, blockID2, defaultEvidenceTime), true}, // different block ids
  344. {vote1, makeVote(t, val, chainID, 0, 10, 2, 1, blockID3, defaultEvidenceTime), true},
  345. {vote1, makeVote(t, val, chainID, 0, 10, 2, 1, blockID4, defaultEvidenceTime), true},
  346. {vote1, makeVote(t, val, chainID, 0, 10, 2, 1, blockID, defaultEvidenceTime), false}, // wrong block id
  347. {vote1, makeVote(t, val, "mychain2", 0, 10, 2, 1, blockID2, defaultEvidenceTime), false}, // wrong chain id
  348. {vote1, makeVote(t, val, chainID, 0, 11, 2, 1, blockID2, defaultEvidenceTime), false}, // wrong height
  349. {vote1, makeVote(t, val, chainID, 0, 10, 3, 1, blockID2, defaultEvidenceTime), false}, // wrong round
  350. {vote1, makeVote(t, val, chainID, 0, 10, 2, 2, blockID2, defaultEvidenceTime), false}, // wrong step
  351. {vote1, makeVote(t, val2, chainID, 0, 10, 2, 1, blockID2, defaultEvidenceTime), false}, // wrong validator
  352. // a different vote time doesn't matter
  353. {vote1, makeVote(t, val, chainID, 0, 10, 2, 1, blockID2, time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)), true},
  354. {vote1, badVote, false}, // signed by wrong key
  355. }
  356. require.NoError(t, err)
  357. for _, c := range cases {
  358. ev := &types.DuplicateVoteEvidence{
  359. VoteA: c.vote1,
  360. VoteB: c.vote2,
  361. ValidatorPower: 1,
  362. TotalVotingPower: 1,
  363. Timestamp: defaultEvidenceTime,
  364. }
  365. if c.valid {
  366. assert.Nil(t, evidence.VerifyDuplicateVote(ev, chainID, valSet), "evidence should be valid")
  367. } else {
  368. assert.NotNil(t, evidence.VerifyDuplicateVote(ev, chainID, valSet), "evidence should be invalid")
  369. }
  370. }
  371. // create good evidence and correct validator power
  372. goodEv := types.NewMockDuplicateVoteEvidenceWithValidator(10, defaultEvidenceTime, val, chainID)
  373. goodEv.ValidatorPower = 1
  374. goodEv.TotalVotingPower = 1
  375. badEv := types.NewMockDuplicateVoteEvidenceWithValidator(10, defaultEvidenceTime, val, chainID)
  376. badTimeEv := types.NewMockDuplicateVoteEvidenceWithValidator(10, defaultEvidenceTime.Add(1*time.Minute), val, chainID)
  377. badTimeEv.ValidatorPower = 1
  378. badTimeEv.TotalVotingPower = 1
  379. state := sm.State{
  380. ChainID: chainID,
  381. LastBlockTime: defaultEvidenceTime.Add(1 * time.Minute),
  382. LastBlockHeight: 11,
  383. ConsensusParams: *types.DefaultConsensusParams(),
  384. }
  385. stateStore := &smmocks.Store{}
  386. stateStore.On("LoadValidators", int64(10)).Return(valSet, nil)
  387. stateStore.On("Load").Return(state, nil)
  388. blockStore := &mocks.BlockStore{}
  389. blockStore.On("LoadBlockMeta", int64(10)).Return(&types.BlockMeta{Header: types.Header{Time: defaultEvidenceTime}})
  390. pool, err := evidence.NewPool(log.TestingLogger(), dbm.NewMemDB(), stateStore, blockStore)
  391. require.NoError(t, err)
  392. evList := types.EvidenceList{goodEv}
  393. err = pool.CheckEvidence(evList)
  394. assert.NoError(t, err)
  395. // evidence with a different validator power should fail
  396. evList = types.EvidenceList{badEv}
  397. err = pool.CheckEvidence(evList)
  398. assert.Error(t, err)
  399. // evidence with a different timestamp should fail
  400. evList = types.EvidenceList{badTimeEv}
  401. err = pool.CheckEvidence(evList)
  402. assert.Error(t, err)
  403. }
  404. func makeLunaticEvidence(
  405. t *testing.T,
  406. height, commonHeight int64,
  407. totalVals, byzVals, phantomVals int,
  408. commonTime, attackTime time.Time,
  409. ) (ev *types.LightClientAttackEvidence, trusted *types.LightBlock, common *types.LightBlock) {
  410. commonValSet, commonPrivVals := factory.RandValidatorSet(totalVals, defaultVotingPower)
  411. require.Greater(t, totalVals, byzVals)
  412. // extract out the subset of byzantine validators in the common validator set
  413. byzValSet, byzPrivVals := commonValSet.Validators[:byzVals], commonPrivVals[:byzVals]
  414. phantomValSet, phantomPrivVals := factory.RandValidatorSet(phantomVals, defaultVotingPower)
  415. conflictingVals := phantomValSet.Copy()
  416. require.NoError(t, conflictingVals.UpdateWithChangeSet(byzValSet))
  417. conflictingPrivVals := append(phantomPrivVals, byzPrivVals...)
  418. conflictingPrivVals = orderPrivValsByValSet(t, conflictingVals, conflictingPrivVals)
  419. commonHeader := factory.MakeRandomHeader()
  420. commonHeader.Height = commonHeight
  421. commonHeader.Time = commonTime
  422. trustedHeader := factory.MakeRandomHeader()
  423. trustedHeader.Height = height
  424. conflictingHeader := factory.MakeRandomHeader()
  425. conflictingHeader.Height = height
  426. conflictingHeader.Time = attackTime
  427. conflictingHeader.ValidatorsHash = conflictingVals.Hash()
  428. blockID := factory.MakeBlockIDWithHash(conflictingHeader.Hash())
  429. voteSet := types.NewVoteSet(evidenceChainID, height, 1, tmproto.SignedMsgType(2), conflictingVals)
  430. commit, err := factory.MakeCommit(blockID, height, 1, voteSet, conflictingPrivVals, defaultEvidenceTime)
  431. require.NoError(t, err)
  432. ev = &types.LightClientAttackEvidence{
  433. ConflictingBlock: &types.LightBlock{
  434. SignedHeader: &types.SignedHeader{
  435. Header: conflictingHeader,
  436. Commit: commit,
  437. },
  438. ValidatorSet: conflictingVals,
  439. },
  440. CommonHeight: commonHeight,
  441. TotalVotingPower: commonValSet.TotalVotingPower(),
  442. ByzantineValidators: byzValSet,
  443. Timestamp: commonTime,
  444. }
  445. common = &types.LightBlock{
  446. SignedHeader: &types.SignedHeader{
  447. Header: commonHeader,
  448. // we can leave this empty because we shouldn't be checking this
  449. Commit: &types.Commit{},
  450. },
  451. ValidatorSet: commonValSet,
  452. }
  453. trustedBlockID := factory.MakeBlockIDWithHash(trustedHeader.Hash())
  454. trustedVals, privVals := factory.RandValidatorSet(totalVals, defaultVotingPower)
  455. trustedVoteSet := types.NewVoteSet(evidenceChainID, height, 1, tmproto.SignedMsgType(2), trustedVals)
  456. trustedCommit, err := factory.MakeCommit(trustedBlockID, height, 1, trustedVoteSet, privVals, defaultEvidenceTime)
  457. require.NoError(t, err)
  458. trusted = &types.LightBlock{
  459. SignedHeader: &types.SignedHeader{
  460. Header: trustedHeader,
  461. Commit: trustedCommit,
  462. },
  463. ValidatorSet: trustedVals,
  464. }
  465. return ev, trusted, common
  466. }
  467. func makeVote(
  468. t *testing.T, val types.PrivValidator, chainID string, valIndex int32, height int64,
  469. round int32, step int, blockID types.BlockID, time time.Time) *types.Vote {
  470. pubKey, err := val.GetPubKey(context.Background())
  471. require.NoError(t, err)
  472. v := &types.Vote{
  473. ValidatorAddress: pubKey.Address(),
  474. ValidatorIndex: valIndex,
  475. Height: height,
  476. Round: round,
  477. Type: tmproto.SignedMsgType(step),
  478. BlockID: blockID,
  479. Timestamp: time,
  480. }
  481. vpb := v.ToProto()
  482. err = val.SignVote(context.Background(), chainID, vpb)
  483. if err != nil {
  484. panic(err)
  485. }
  486. v.Signature = vpb.Signature
  487. return v
  488. }
  489. func makeBlockID(hash []byte, partSetSize uint32, partSetHash []byte) types.BlockID {
  490. var (
  491. h = make([]byte, tmhash.Size)
  492. psH = make([]byte, tmhash.Size)
  493. )
  494. copy(h, hash)
  495. copy(psH, partSetHash)
  496. return types.BlockID{
  497. Hash: h,
  498. PartSetHeader: types.PartSetHeader{
  499. Total: partSetSize,
  500. Hash: psH,
  501. },
  502. }
  503. }
  504. func orderPrivValsByValSet(
  505. t *testing.T, vals *types.ValidatorSet, privVals []types.PrivValidator) []types.PrivValidator {
  506. output := make([]types.PrivValidator, len(privVals))
  507. for idx, v := range vals.Validators {
  508. for _, p := range privVals {
  509. pubKey, err := p.GetPubKey(context.Background())
  510. require.NoError(t, err)
  511. if bytes.Equal(v.Address, pubKey.Address()) {
  512. output[idx] = p
  513. break
  514. }
  515. }
  516. require.NotEmpty(t, output[idx])
  517. }
  518. return output
  519. }