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.

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