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.

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