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.

584 lines
18 KiB

  1. package evidence_test
  2. import (
  3. "context"
  4. "encoding/hex"
  5. "math/rand"
  6. "sync"
  7. "testing"
  8. "time"
  9. "github.com/fortytw2/leaktest"
  10. "github.com/stretchr/testify/assert"
  11. "github.com/stretchr/testify/mock"
  12. "github.com/stretchr/testify/require"
  13. dbm "github.com/tendermint/tm-db"
  14. "github.com/tendermint/tendermint/crypto"
  15. "github.com/tendermint/tendermint/crypto/tmhash"
  16. "github.com/tendermint/tendermint/internal/eventbus"
  17. "github.com/tendermint/tendermint/internal/evidence"
  18. "github.com/tendermint/tendermint/internal/evidence/mocks"
  19. "github.com/tendermint/tendermint/internal/p2p"
  20. "github.com/tendermint/tendermint/internal/p2p/p2ptest"
  21. sm "github.com/tendermint/tendermint/internal/state"
  22. "github.com/tendermint/tendermint/libs/log"
  23. tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
  24. "github.com/tendermint/tendermint/types"
  25. )
  26. var (
  27. numEvidence = 10
  28. rng = rand.New(rand.NewSource(time.Now().UnixNano()))
  29. )
  30. type reactorTestSuite struct {
  31. network *p2ptest.Network
  32. logger log.Logger
  33. reactors map[types.NodeID]*evidence.Reactor
  34. pools map[types.NodeID]*evidence.Pool
  35. evidenceChannels map[types.NodeID]*p2p.Channel
  36. peerUpdates map[types.NodeID]*p2p.PeerUpdates
  37. peerChans map[types.NodeID]chan p2p.PeerUpdate
  38. nodes []*p2ptest.Node
  39. numStateStores int
  40. }
  41. func setup(ctx context.Context, t *testing.T, stateStores []sm.Store, chBuf uint) *reactorTestSuite {
  42. t.Helper()
  43. pID := make([]byte, 16)
  44. _, err := rng.Read(pID)
  45. require.NoError(t, err)
  46. numStateStores := len(stateStores)
  47. rts := &reactorTestSuite{
  48. numStateStores: numStateStores,
  49. logger: log.NewNopLogger().With("testCase", t.Name()),
  50. network: p2ptest.MakeNetwork(ctx, t, p2ptest.NetworkOptions{NumNodes: numStateStores}),
  51. reactors: make(map[types.NodeID]*evidence.Reactor, numStateStores),
  52. pools: make(map[types.NodeID]*evidence.Pool, numStateStores),
  53. peerUpdates: make(map[types.NodeID]*p2p.PeerUpdates, numStateStores),
  54. peerChans: make(map[types.NodeID]chan p2p.PeerUpdate, numStateStores),
  55. }
  56. chDesc := &p2p.ChannelDescriptor{ID: evidence.EvidenceChannel, MessageType: new(tmproto.Evidence)}
  57. rts.evidenceChannels = rts.network.MakeChannelsNoCleanup(ctx, t, chDesc)
  58. require.Len(t, rts.network.RandomNode().PeerManager.Peers(), 0)
  59. idx := 0
  60. evidenceTime := time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC)
  61. for nodeID := range rts.network.Nodes {
  62. logger := rts.logger.With("validator", idx)
  63. evidenceDB := dbm.NewMemDB()
  64. blockStore := &mocks.BlockStore{}
  65. state, _ := stateStores[idx].Load()
  66. blockStore.On("LoadBlockMeta", mock.AnythingOfType("int64")).Return(func(h int64) *types.BlockMeta {
  67. if h <= state.LastBlockHeight {
  68. return &types.BlockMeta{Header: types.Header{Time: evidenceTime}}
  69. }
  70. return nil
  71. })
  72. eventBus := eventbus.NewDefault(logger)
  73. err = eventBus.Start(ctx)
  74. require.NoError(t, err)
  75. rts.pools[nodeID] = evidence.NewPool(logger, evidenceDB, stateStores[idx], blockStore, evidence.NopMetrics(), eventBus)
  76. startPool(t, rts.pools[nodeID], stateStores[idx])
  77. require.NoError(t, err)
  78. rts.peerChans[nodeID] = make(chan p2p.PeerUpdate)
  79. rts.peerUpdates[nodeID] = p2p.NewPeerUpdates(rts.peerChans[nodeID], 1)
  80. rts.network.Nodes[nodeID].PeerManager.Register(ctx, rts.peerUpdates[nodeID])
  81. rts.nodes = append(rts.nodes, rts.network.Nodes[nodeID])
  82. chCreator := func(ctx context.Context, chdesc *p2p.ChannelDescriptor) (*p2p.Channel, error) {
  83. return rts.evidenceChannels[nodeID], nil
  84. }
  85. rts.reactors[nodeID], err = evidence.NewReactor(
  86. ctx,
  87. logger,
  88. chCreator,
  89. rts.peerUpdates[nodeID],
  90. rts.pools[nodeID])
  91. require.NoError(t, err)
  92. require.NoError(t, rts.reactors[nodeID].Start(ctx))
  93. require.True(t, rts.reactors[nodeID].IsRunning())
  94. idx++
  95. }
  96. t.Cleanup(func() {
  97. for _, r := range rts.reactors {
  98. if r.IsRunning() {
  99. r.Stop()
  100. r.Wait()
  101. require.False(t, r.IsRunning())
  102. }
  103. }
  104. })
  105. t.Cleanup(leaktest.Check(t))
  106. return rts
  107. }
  108. func (rts *reactorTestSuite) start(ctx context.Context, t *testing.T) {
  109. rts.network.Start(ctx, t)
  110. require.Len(t,
  111. rts.network.RandomNode().PeerManager.Peers(),
  112. rts.numStateStores-1,
  113. "network does not have expected number of nodes")
  114. }
  115. func (rts *reactorTestSuite) waitForEvidence(t *testing.T, evList types.EvidenceList, ids ...types.NodeID) {
  116. t.Helper()
  117. fn := func(pool *evidence.Pool) {
  118. var (
  119. localEvList []types.Evidence
  120. size int64
  121. loops int
  122. )
  123. // wait till we have at least the amount of evidence
  124. // that we expect. if there's more local evidence then
  125. // it doesn't make sense to wait longer and a
  126. // different assertion should catch the resulting error
  127. for len(localEvList) < len(evList) {
  128. // each evidence should not be more than 500 bytes
  129. localEvList, size = pool.PendingEvidence(int64(len(evList) * 500))
  130. if loops == 100 {
  131. t.Log("current wait status:", "|",
  132. "local", len(localEvList), "|",
  133. "waitlist", len(evList), "|",
  134. "size", size)
  135. }
  136. loops++
  137. }
  138. // put the reaped evidence in a map so we can quickly check we got everything
  139. evMap := make(map[string]types.Evidence)
  140. for _, e := range localEvList {
  141. evMap[string(e.Hash())] = e
  142. }
  143. for i, expectedEv := range evList {
  144. gotEv := evMap[string(expectedEv.Hash())]
  145. require.Equalf(
  146. t,
  147. expectedEv,
  148. gotEv,
  149. "evidence for pool %d in pool does not match; got: %v, expected: %v", i, gotEv, expectedEv,
  150. )
  151. }
  152. }
  153. if len(ids) == 1 {
  154. // special case waiting once, just to avoid the extra
  155. // goroutine, in the case that this hits a timeout,
  156. // the stack will be clearer.
  157. fn(rts.pools[ids[0]])
  158. return
  159. }
  160. wg := sync.WaitGroup{}
  161. for id := range rts.pools {
  162. if len(ids) > 0 && !p2ptest.NodeInSlice(id, ids) {
  163. // if an ID list is specified, then we only
  164. // want to wait for those pools that are
  165. // specified in the list, otherwise, wait for
  166. // all pools.
  167. continue
  168. }
  169. wg.Add(1)
  170. go func(id types.NodeID) { defer wg.Done(); fn(rts.pools[id]) }(id)
  171. }
  172. wg.Wait()
  173. }
  174. func createEvidenceList(
  175. ctx context.Context,
  176. t *testing.T,
  177. pool *evidence.Pool,
  178. val types.PrivValidator,
  179. numEvidence int,
  180. ) types.EvidenceList {
  181. t.Helper()
  182. evList := make([]types.Evidence, numEvidence)
  183. for i := 0; i < numEvidence; i++ {
  184. ev, err := types.NewMockDuplicateVoteEvidenceWithValidator(
  185. ctx,
  186. int64(i+1),
  187. time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC),
  188. val,
  189. evidenceChainID,
  190. )
  191. require.NoError(t, err)
  192. err = pool.AddEvidence(ctx, ev)
  193. require.NoError(t, err,
  194. "adding evidence it#%d of %d to pool with height %d",
  195. i, numEvidence, pool.State().LastBlockHeight)
  196. evList[i] = ev
  197. }
  198. return evList
  199. }
  200. func TestReactorMultiDisconnect(t *testing.T) {
  201. ctx, cancel := context.WithCancel(context.Background())
  202. defer cancel()
  203. val := types.NewMockPV()
  204. height := int64(numEvidence) + 10
  205. stateDB1 := initializeValidatorState(ctx, t, val, height)
  206. stateDB2 := initializeValidatorState(ctx, t, val, height)
  207. rts := setup(ctx, t, []sm.Store{stateDB1, stateDB2}, 20)
  208. primary := rts.nodes[0]
  209. secondary := rts.nodes[1]
  210. _ = createEvidenceList(ctx, t, rts.pools[primary.NodeID], val, numEvidence)
  211. require.Equal(t, primary.PeerManager.Status(secondary.NodeID), p2p.PeerStatusDown)
  212. rts.start(ctx, t)
  213. require.Equal(t, primary.PeerManager.Status(secondary.NodeID), p2p.PeerStatusUp)
  214. // Ensure "disconnecting" the secondary peer from the primary more than once
  215. // is handled gracefully.
  216. primary.PeerManager.Disconnected(ctx, secondary.NodeID)
  217. require.Equal(t, primary.PeerManager.Status(secondary.NodeID), p2p.PeerStatusDown)
  218. _, err := primary.PeerManager.TryEvictNext()
  219. require.NoError(t, err)
  220. primary.PeerManager.Disconnected(ctx, secondary.NodeID)
  221. require.Equal(t, primary.PeerManager.Status(secondary.NodeID), p2p.PeerStatusDown)
  222. require.Equal(t, secondary.PeerManager.Status(primary.NodeID), p2p.PeerStatusUp)
  223. }
  224. // TestReactorBroadcastEvidence creates an environment of multiple peers that
  225. // are all at the same height. One peer, designated as a primary, gossips all
  226. // evidence to the remaining peers.
  227. func TestReactorBroadcastEvidence(t *testing.T) {
  228. numPeers := 7
  229. ctx, cancel := context.WithCancel(context.Background())
  230. defer cancel()
  231. // create a stateDB for all test suites (nodes)
  232. stateDBs := make([]sm.Store, numPeers)
  233. val := types.NewMockPV()
  234. // We need all validators saved for heights at least as high as we have
  235. // evidence for.
  236. height := int64(numEvidence) + 10
  237. for i := 0; i < numPeers; i++ {
  238. stateDBs[i] = initializeValidatorState(ctx, t, val, height)
  239. }
  240. rts := setup(ctx, t, stateDBs, 0)
  241. rts.start(ctx, t)
  242. // Create a series of fixtures where each suite contains a reactor and
  243. // evidence pool. In addition, we mark a primary suite and the rest are
  244. // secondaries where each secondary is added as a peer via a PeerUpdate to the
  245. // primary. As a result, the primary will gossip all evidence to each secondary.
  246. primary := rts.network.RandomNode()
  247. secondaries := make([]*p2ptest.Node, 0, len(rts.network.NodeIDs())-1)
  248. secondaryIDs := make([]types.NodeID, 0, cap(secondaries))
  249. for id := range rts.network.Nodes {
  250. if id == primary.NodeID {
  251. continue
  252. }
  253. secondaries = append(secondaries, rts.network.Nodes[id])
  254. secondaryIDs = append(secondaryIDs, id)
  255. }
  256. evList := createEvidenceList(ctx, t, rts.pools[primary.NodeID], val, numEvidence)
  257. // Add each secondary suite (node) as a peer to the primary suite (node). This
  258. // will cause the primary to gossip all evidence to the secondaries.
  259. for _, suite := range secondaries {
  260. rts.peerChans[primary.NodeID] <- p2p.PeerUpdate{
  261. Status: p2p.PeerStatusUp,
  262. NodeID: suite.NodeID,
  263. }
  264. }
  265. // Wait till all secondary suites (reactor) received all evidence from the
  266. // primary suite (node).
  267. rts.waitForEvidence(t, evList, secondaryIDs...)
  268. for _, pool := range rts.pools {
  269. require.Equal(t, numEvidence, int(pool.Size()))
  270. }
  271. }
  272. // TestReactorSelectiveBroadcast tests a context where we have two reactors
  273. // connected to one another but are at different heights. Reactor 1 which is
  274. // ahead receives a list of evidence.
  275. func TestReactorBroadcastEvidence_Lagging(t *testing.T) {
  276. val := types.NewMockPV()
  277. height1 := int64(numEvidence) + 10
  278. height2 := int64(numEvidence) / 2
  279. ctx, cancel := context.WithCancel(context.Background())
  280. defer cancel()
  281. // stateDB1 is ahead of stateDB2, where stateDB1 has all heights (1-20) and
  282. // stateDB2 only has heights 1-5.
  283. stateDB1 := initializeValidatorState(ctx, t, val, height1)
  284. stateDB2 := initializeValidatorState(ctx, t, val, height2)
  285. rts := setup(ctx, t, []sm.Store{stateDB1, stateDB2}, 100)
  286. rts.start(ctx, t)
  287. primary := rts.nodes[0]
  288. secondary := rts.nodes[1]
  289. // Send a list of valid evidence to the first reactor's, the one that is ahead,
  290. // evidence pool.
  291. evList := createEvidenceList(ctx, t, rts.pools[primary.NodeID], val, numEvidence)
  292. // Add each secondary suite (node) as a peer to the primary suite (node). This
  293. // will cause the primary to gossip all evidence to the secondaries.
  294. rts.peerChans[primary.NodeID] <- p2p.PeerUpdate{
  295. Status: p2p.PeerStatusUp,
  296. NodeID: secondary.NodeID,
  297. }
  298. // only ones less than the peers height should make it through
  299. rts.waitForEvidence(t, evList[:height2], secondary.NodeID)
  300. require.Equal(t, numEvidence, int(rts.pools[primary.NodeID].Size()))
  301. require.Equal(t, int(height2), int(rts.pools[secondary.NodeID].Size()))
  302. }
  303. func TestReactorBroadcastEvidence_Pending(t *testing.T) {
  304. val := types.NewMockPV()
  305. height := int64(10)
  306. ctx, cancel := context.WithCancel(context.Background())
  307. defer cancel()
  308. stateDB1 := initializeValidatorState(ctx, t, val, height)
  309. stateDB2 := initializeValidatorState(ctx, t, val, height)
  310. rts := setup(ctx, t, []sm.Store{stateDB1, stateDB2}, 100)
  311. primary := rts.nodes[0]
  312. secondary := rts.nodes[1]
  313. evList := createEvidenceList(ctx, t, rts.pools[primary.NodeID], val, numEvidence)
  314. // Manually add half the evidence to the secondary which will mark them as
  315. // pending.
  316. for i := 0; i < numEvidence/2; i++ {
  317. err := rts.pools[secondary.NodeID].AddEvidence(ctx, evList[i])
  318. require.NoError(t, err)
  319. }
  320. // the secondary should have half the evidence as pending
  321. require.Equal(t, numEvidence/2, int(rts.pools[secondary.NodeID].Size()))
  322. rts.start(ctx, t)
  323. // The secondary reactor should have received all the evidence ignoring the
  324. // already pending evidence.
  325. rts.waitForEvidence(t, evList, secondary.NodeID)
  326. // check to make sure that all of the evidence has
  327. // propogated
  328. require.Len(t, rts.pools, 2)
  329. assert.EqualValues(t, numEvidence, rts.pools[primary.NodeID].Size(),
  330. "primary node should have all the evidence")
  331. assert.EqualValues(t, numEvidence, rts.pools[secondary.NodeID].Size(),
  332. "secondary nodes should have caught up")
  333. }
  334. func TestReactorBroadcastEvidence_Committed(t *testing.T) {
  335. val := types.NewMockPV()
  336. height := int64(10)
  337. ctx, cancel := context.WithCancel(context.Background())
  338. defer cancel()
  339. stateDB1 := initializeValidatorState(ctx, t, val, height)
  340. stateDB2 := initializeValidatorState(ctx, t, val, height)
  341. rts := setup(ctx, t, []sm.Store{stateDB1, stateDB2}, 0)
  342. primary := rts.nodes[0]
  343. secondary := rts.nodes[1]
  344. // add all evidence to the primary reactor
  345. evList := createEvidenceList(ctx, t, rts.pools[primary.NodeID], val, numEvidence)
  346. // Manually add half the evidence to the secondary which will mark them as
  347. // pending.
  348. for i := 0; i < numEvidence/2; i++ {
  349. err := rts.pools[secondary.NodeID].AddEvidence(ctx, evList[i])
  350. require.NoError(t, err)
  351. }
  352. // the secondary should have half the evidence as pending
  353. require.Equal(t, numEvidence/2, int(rts.pools[secondary.NodeID].Size()))
  354. state, err := stateDB2.Load()
  355. require.NoError(t, err)
  356. // update the secondary's pool such that all pending evidence is committed
  357. state.LastBlockHeight++
  358. rts.pools[secondary.NodeID].Update(ctx, state, evList[:numEvidence/2])
  359. // the secondary should have half the evidence as committed
  360. require.Equal(t, 0, int(rts.pools[secondary.NodeID].Size()))
  361. // start the network and ensure it's configured
  362. rts.start(ctx, t)
  363. // The secondary reactor should have received all the evidence ignoring the
  364. // already committed evidence.
  365. rts.waitForEvidence(t, evList[numEvidence/2:], secondary.NodeID)
  366. require.Len(t, rts.pools, 2)
  367. assert.EqualValues(t, numEvidence, rts.pools[primary.NodeID].Size(),
  368. "primary node should have all the evidence")
  369. assert.EqualValues(t, numEvidence/2, rts.pools[secondary.NodeID].Size(),
  370. "secondary nodes should have caught up")
  371. }
  372. func TestReactorBroadcastEvidence_FullyConnected(t *testing.T) {
  373. numPeers := 7
  374. // create a stateDB for all test suites (nodes)
  375. stateDBs := make([]sm.Store, numPeers)
  376. val := types.NewMockPV()
  377. ctx, cancel := context.WithCancel(context.Background())
  378. defer cancel()
  379. // We need all validators saved for heights at least as high as we have
  380. // evidence for.
  381. height := int64(numEvidence) + 10
  382. for i := 0; i < numPeers; i++ {
  383. stateDBs[i] = initializeValidatorState(ctx, t, val, height)
  384. }
  385. rts := setup(ctx, t, stateDBs, 0)
  386. rts.start(ctx, t)
  387. evList := createEvidenceList(ctx, t, rts.pools[rts.network.RandomNode().NodeID], val, numEvidence)
  388. // every suite (reactor) connects to every other suite (reactor)
  389. for outerID, outerChan := range rts.peerChans {
  390. for innerID := range rts.peerChans {
  391. if outerID != innerID {
  392. outerChan <- p2p.PeerUpdate{
  393. Status: p2p.PeerStatusUp,
  394. NodeID: innerID,
  395. }
  396. }
  397. }
  398. }
  399. // wait till all suites (reactors) received all evidence from other suites (reactors)
  400. rts.waitForEvidence(t, evList)
  401. for _, pool := range rts.pools {
  402. require.Equal(t, numEvidence, int(pool.Size()))
  403. // commit state so we do not continue to repeat gossiping the same evidence
  404. state := pool.State()
  405. state.LastBlockHeight++
  406. pool.Update(ctx, state, evList)
  407. }
  408. }
  409. func TestEvidenceListSerialization(t *testing.T) {
  410. exampleVote := func(msgType byte) *types.Vote {
  411. var stamp, err = time.Parse(types.TimeFormat, "2017-12-25T03:00:01.234Z")
  412. require.NoError(t, err)
  413. return &types.Vote{
  414. Type: tmproto.SignedMsgType(msgType),
  415. Height: 3,
  416. Round: 2,
  417. Timestamp: stamp,
  418. BlockID: types.BlockID{
  419. Hash: tmhash.Sum([]byte("blockID_hash")),
  420. PartSetHeader: types.PartSetHeader{
  421. Total: 1000000,
  422. Hash: tmhash.Sum([]byte("blockID_part_set_header_hash")),
  423. },
  424. },
  425. ValidatorAddress: crypto.AddressHash([]byte("validator_address")),
  426. ValidatorIndex: 56789,
  427. }
  428. }
  429. val := &types.Validator{
  430. Address: crypto.AddressHash([]byte("validator_address")),
  431. VotingPower: 10,
  432. }
  433. valSet := types.NewValidatorSet([]*types.Validator{val})
  434. dupl, err := types.NewDuplicateVoteEvidence(
  435. exampleVote(1),
  436. exampleVote(2),
  437. defaultEvidenceTime,
  438. valSet,
  439. )
  440. require.NoError(t, err)
  441. testCases := map[string]struct {
  442. evidenceList []types.Evidence
  443. expBytes string
  444. }{
  445. "DuplicateVoteEvidence": {
  446. []types.Evidence{dupl},
  447. "0a85020a82020a79080210031802224a0a208b01023386c371778ecb6368573e539afc3cc860ec3a2f614e54fe5652f4fc80122608c0843d122072db3d959635dff1bb567bedaa70573392c5159666a3f8caf11e413aac52207a2a0b08b1d381d20510809dca6f32146af1f4111082efb388211bc72c55bcd61e9ac3d538d5bb031279080110031802224a0a208b01023386c371778ecb6368573e539afc3cc860ec3a2f614e54fe5652f4fc80122608c0843d122072db3d959635dff1bb567bedaa70573392c5159666a3f8caf11e413aac52207a2a0b08b1d381d20510809dca6f32146af1f4111082efb388211bc72c55bcd61e9ac3d538d5bb03180a200a2a060880dbaae105",
  448. },
  449. }
  450. for name, tc := range testCases {
  451. tc := tc
  452. t.Run(name, func(t *testing.T) {
  453. protoEv := make([]tmproto.Evidence, len(tc.evidenceList))
  454. for i := 0; i < len(tc.evidenceList); i++ {
  455. ev, err := types.EvidenceToProto(tc.evidenceList[i])
  456. require.NoError(t, err)
  457. protoEv[i] = *ev
  458. }
  459. epl := tmproto.EvidenceList{
  460. Evidence: protoEv,
  461. }
  462. bz, err := epl.Marshal()
  463. require.NoError(t, err)
  464. require.Equal(t, tc.expBytes, hex.EncodeToString(bz))
  465. })
  466. }
  467. }