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.

650 lines
20 KiB

blockchain: Reorg reactor (#3561) * go routines in blockchain reactor * Added reference to the go routine diagram * Initial commit * cleanup * Undo testing_logger change, committed by mistake * Fix the test loggers * pulled some fsm code into pool.go * added pool tests * changes to the design added block requests under peer moved the request trigger in the reactor poolRoutine, triggered now by a ticker in general moved everything required for making block requests smarter in the poolRoutine added a simple map of heights to keep track of what will need to be requested next added a few more tests * send errors to FSM in a different channel than blocks send errors (RemovePeer) from switch on a different channel than the one receiving blocks renamed channels added more pool tests * more pool tests * lint errors * more tests * more tests * switch fast sync to new implementation * fixed data race in tests * cleanup * finished fsm tests * address golangci comments :) * address golangci comments :) * Added timeout on next block needed to advance * updating docs and cleanup * fix issue in test from previous cleanup * cleanup * Added termination scenarios, tests and more cleanup * small fixes to adr, comments and cleanup * Fix bug in sendRequest() If we tried to send a request to a peer not present in the switch, a missing continue statement caused the request to be blackholed in a peer that was removed and never retried. While this bug was manifesting, the reactor kept asking for other blocks that would be stored and never consumed. Added the number of unconsumed blocks in the math for requesting blocks ahead of current processing height so eventually there will be no more blocks requested until the already received ones are consumed. * remove bpPeer's didTimeout field * Use distinct err codes for peer timeout and FSM timeouts * Don't allow peers to update with lower height * review comments from Ethan and Zarko * some cleanup, renaming, comments * Move block execution in separate goroutine * Remove pool's numPending * review comments * fix lint, remove old blockchain reactor and duplicates in fsm tests * small reorg around peer after review comments * add the reactor spec * verify block only once * review comments * change to int for max number of pending requests * cleanup and godoc * Add configuration flag fast sync version * golangci fixes * fix config template * move both reactor versions under blockchain * cleanup, golint, renaming stuff * updated documentation, fixed more golint warnings * integrate with behavior package * sync with master * gofmt * add changelog_pending entry * move to improvments * suggestion to changelog entry
6 years ago
  1. package v1
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/tendermint/tendermint/libs/log"
  7. "github.com/tendermint/tendermint/p2p"
  8. "github.com/tendermint/tendermint/types"
  9. )
  10. type testPeer struct {
  11. id p2p.ID
  12. height int64
  13. }
  14. type testBcR struct {
  15. logger log.Logger
  16. }
  17. type testValues struct {
  18. numRequestsSent int
  19. }
  20. var testResults testValues
  21. func resetPoolTestResults() {
  22. testResults.numRequestsSent = 0
  23. }
  24. func (testR *testBcR) sendPeerError(err error, peerID p2p.ID) {
  25. }
  26. func (testR *testBcR) sendStatusRequest() {
  27. }
  28. func (testR *testBcR) sendBlockRequest(peerID p2p.ID, height int64) error {
  29. testResults.numRequestsSent++
  30. return nil
  31. }
  32. func (testR *testBcR) resetStateTimer(name string, timer **time.Timer, timeout time.Duration) {
  33. }
  34. func (testR *testBcR) switchToConsensus() {
  35. }
  36. func newTestBcR() *testBcR {
  37. testBcR := &testBcR{logger: log.TestingLogger()}
  38. return testBcR
  39. }
  40. type tPBlocks struct {
  41. id p2p.ID
  42. create bool
  43. }
  44. // Makes a block pool with specified current height, list of peers, block requests and block responses
  45. func makeBlockPool(bcr *testBcR, height int64, peers []BpPeer, blocks map[int64]tPBlocks) *BlockPool {
  46. bPool := NewBlockPool(height, bcr)
  47. bPool.SetLogger(bcr.logger)
  48. txs := []types.Tx{types.Tx("foo"), types.Tx("bar")}
  49. var maxH int64
  50. for _, p := range peers {
  51. if p.Height > maxH {
  52. maxH = p.Height
  53. }
  54. bPool.peers[p.ID] = NewBpPeer(p.ID, p.Height, bcr.sendPeerError, nil)
  55. bPool.peers[p.ID].SetLogger(bcr.logger)
  56. }
  57. bPool.MaxPeerHeight = maxH
  58. for h, p := range blocks {
  59. bPool.blocks[h] = p.id
  60. bPool.peers[p.id].RequestSent(int64(h))
  61. if p.create {
  62. // simulate that a block at height h has been received
  63. _ = bPool.peers[p.id].AddBlock(types.MakeBlock(int64(h), txs, nil, nil), 100)
  64. }
  65. }
  66. return bPool
  67. }
  68. func assertPeerSetsEquivalent(t *testing.T, set1 map[p2p.ID]*BpPeer, set2 map[p2p.ID]*BpPeer) {
  69. assert.Equal(t, len(set1), len(set2))
  70. for peerID, peer1 := range set1 {
  71. peer2 := set2[peerID]
  72. assert.NotNil(t, peer2)
  73. assert.Equal(t, peer1.NumPendingBlockRequests, peer2.NumPendingBlockRequests)
  74. assert.Equal(t, peer1.Height, peer2.Height)
  75. assert.Equal(t, len(peer1.blocks), len(peer2.blocks))
  76. for h, block1 := range peer1.blocks {
  77. block2 := peer2.blocks[h]
  78. // block1 and block2 could be nil if a request was made but no block was received
  79. assert.Equal(t, block1, block2)
  80. }
  81. }
  82. }
  83. func assertBlockPoolEquivalent(t *testing.T, poolWanted, pool *BlockPool) {
  84. assert.Equal(t, poolWanted.blocks, pool.blocks)
  85. assertPeerSetsEquivalent(t, poolWanted.peers, pool.peers)
  86. assert.Equal(t, poolWanted.MaxPeerHeight, pool.MaxPeerHeight)
  87. assert.Equal(t, poolWanted.Height, pool.Height)
  88. }
  89. func TestBlockPoolUpdatePeer(t *testing.T) {
  90. testBcR := newTestBcR()
  91. tests := []struct {
  92. name string
  93. pool *BlockPool
  94. args testPeer
  95. poolWanted *BlockPool
  96. errWanted error
  97. }{
  98. {
  99. name: "add a first short peer",
  100. pool: makeBlockPool(testBcR, 100, []BpPeer{}, map[int64]tPBlocks{}),
  101. args: testPeer{"P1", 50},
  102. errWanted: errPeerTooShort,
  103. poolWanted: makeBlockPool(testBcR, 100, []BpPeer{}, map[int64]tPBlocks{}),
  104. },
  105. {
  106. name: "add a first good peer",
  107. pool: makeBlockPool(testBcR, 100, []BpPeer{}, map[int64]tPBlocks{}),
  108. args: testPeer{"P1", 101},
  109. poolWanted: makeBlockPool(testBcR, 100, []BpPeer{{ID: "P1", Height: 101}}, map[int64]tPBlocks{}),
  110. },
  111. {
  112. name: "increase the height of P1 from 120 to 123",
  113. pool: makeBlockPool(testBcR, 100, []BpPeer{{ID: "P1", Height: 120}}, map[int64]tPBlocks{}),
  114. args: testPeer{"P1", 123},
  115. poolWanted: makeBlockPool(testBcR, 100, []BpPeer{{ID: "P1", Height: 123}}, map[int64]tPBlocks{}),
  116. },
  117. {
  118. name: "decrease the height of P1 from 120 to 110",
  119. pool: makeBlockPool(testBcR, 100, []BpPeer{{ID: "P1", Height: 120}}, map[int64]tPBlocks{}),
  120. args: testPeer{"P1", 110},
  121. errWanted: errPeerLowersItsHeight,
  122. poolWanted: makeBlockPool(testBcR, 100, []BpPeer{}, map[int64]tPBlocks{}),
  123. },
  124. {
  125. name: "decrease the height of P1 from 105 to 102 with blocks",
  126. pool: makeBlockPool(testBcR, 100, []BpPeer{{ID: "P1", Height: 105}},
  127. map[int64]tPBlocks{
  128. 100: {"P1", true}, 101: {"P1", true}, 102: {"P1", true}}),
  129. args: testPeer{"P1", 102},
  130. errWanted: errPeerLowersItsHeight,
  131. poolWanted: makeBlockPool(testBcR, 100, []BpPeer{},
  132. map[int64]tPBlocks{}),
  133. },
  134. }
  135. for _, tt := range tests {
  136. t.Run(tt.name, func(t *testing.T) {
  137. pool := tt.pool
  138. err := pool.UpdatePeer(tt.args.id, tt.args.height)
  139. assert.Equal(t, tt.errWanted, err)
  140. assert.Equal(t, tt.poolWanted.blocks, tt.pool.blocks)
  141. assertPeerSetsEquivalent(t, tt.poolWanted.peers, tt.pool.peers)
  142. assert.Equal(t, tt.poolWanted.MaxPeerHeight, tt.pool.MaxPeerHeight)
  143. })
  144. }
  145. }
  146. func TestBlockPoolRemovePeer(t *testing.T) {
  147. testBcR := newTestBcR()
  148. type args struct {
  149. peerID p2p.ID
  150. err error
  151. }
  152. tests := []struct {
  153. name string
  154. pool *BlockPool
  155. args args
  156. poolWanted *BlockPool
  157. }{
  158. {
  159. name: "attempt to delete non-existing peer",
  160. pool: makeBlockPool(testBcR, 100, []BpPeer{{ID: "P1", Height: 120}}, map[int64]tPBlocks{}),
  161. args: args{"P99", nil},
  162. poolWanted: makeBlockPool(testBcR, 100, []BpPeer{{ID: "P1", Height: 120}}, map[int64]tPBlocks{}),
  163. },
  164. {
  165. name: "delete the only peer without blocks",
  166. pool: makeBlockPool(testBcR, 100, []BpPeer{{ID: "P1", Height: 120}}, map[int64]tPBlocks{}),
  167. args: args{"P1", nil},
  168. poolWanted: makeBlockPool(testBcR, 100, []BpPeer{}, map[int64]tPBlocks{}),
  169. },
  170. {
  171. name: "delete the shortest of two peers without blocks",
  172. pool: makeBlockPool(testBcR, 100, []BpPeer{{ID: "P1", Height: 100}, {ID: "P2", Height: 120}}, map[int64]tPBlocks{}),
  173. args: args{"P1", nil},
  174. poolWanted: makeBlockPool(testBcR, 100, []BpPeer{{ID: "P2", Height: 120}}, map[int64]tPBlocks{}),
  175. },
  176. {
  177. name: "delete the tallest of two peers without blocks",
  178. pool: makeBlockPool(testBcR, 100, []BpPeer{{ID: "P1", Height: 100}, {ID: "P2", Height: 120}}, map[int64]tPBlocks{}),
  179. args: args{"P2", nil},
  180. poolWanted: makeBlockPool(testBcR, 100, []BpPeer{{ID: "P1", Height: 100}}, map[int64]tPBlocks{}),
  181. },
  182. {
  183. name: "delete the only peer with block requests sent and blocks received",
  184. pool: makeBlockPool(testBcR, 100, []BpPeer{{ID: "P1", Height: 120}},
  185. map[int64]tPBlocks{100: {"P1", true}, 101: {"P1", false}}),
  186. args: args{"P1", nil},
  187. poolWanted: makeBlockPool(testBcR, 100, []BpPeer{}, map[int64]tPBlocks{}),
  188. },
  189. {
  190. name: "delete the shortest of two peers with block requests sent and blocks received",
  191. pool: makeBlockPool(testBcR, 100, []BpPeer{{ID: "P1", Height: 120}, {ID: "P2", Height: 200}},
  192. map[int64]tPBlocks{100: {"P1", true}, 101: {"P1", false}}),
  193. args: args{"P1", nil},
  194. poolWanted: makeBlockPool(testBcR, 100, []BpPeer{{ID: "P2", Height: 200}}, map[int64]tPBlocks{}),
  195. },
  196. {
  197. name: "delete the tallest of two peers with block requests sent and blocks received",
  198. pool: makeBlockPool(testBcR, 100, []BpPeer{{ID: "P1", Height: 120}, {ID: "P2", Height: 110}},
  199. map[int64]tPBlocks{100: {"P1", true}, 101: {"P1", false}}),
  200. args: args{"P1", nil},
  201. poolWanted: makeBlockPool(testBcR, 100, []BpPeer{{ID: "P2", Height: 110}}, map[int64]tPBlocks{}),
  202. },
  203. }
  204. for _, tt := range tests {
  205. t.Run(tt.name, func(t *testing.T) {
  206. tt.pool.RemovePeer(tt.args.peerID, tt.args.err)
  207. assertBlockPoolEquivalent(t, tt.poolWanted, tt.pool)
  208. })
  209. }
  210. }
  211. func TestBlockPoolRemoveShortPeers(t *testing.T) {
  212. testBcR := newTestBcR()
  213. tests := []struct {
  214. name string
  215. pool *BlockPool
  216. poolWanted *BlockPool
  217. }{
  218. {
  219. name: "no short peers",
  220. pool: makeBlockPool(testBcR, 100,
  221. []BpPeer{{ID: "P1", Height: 100}, {ID: "P2", Height: 110}, {ID: "P3", Height: 120}}, map[int64]tPBlocks{}),
  222. poolWanted: makeBlockPool(testBcR, 100,
  223. []BpPeer{{ID: "P1", Height: 100}, {ID: "P2", Height: 110}, {ID: "P3", Height: 120}}, map[int64]tPBlocks{}),
  224. },
  225. {
  226. name: "one short peer",
  227. pool: makeBlockPool(testBcR, 100,
  228. []BpPeer{{ID: "P1", Height: 100}, {ID: "P2", Height: 90}, {ID: "P3", Height: 120}}, map[int64]tPBlocks{}),
  229. poolWanted: makeBlockPool(testBcR, 100,
  230. []BpPeer{{ID: "P1", Height: 100}, {ID: "P3", Height: 120}}, map[int64]tPBlocks{}),
  231. },
  232. {
  233. name: "all short peers",
  234. pool: makeBlockPool(testBcR, 100,
  235. []BpPeer{{ID: "P1", Height: 90}, {ID: "P2", Height: 91}, {ID: "P3", Height: 92}}, map[int64]tPBlocks{}),
  236. poolWanted: makeBlockPool(testBcR, 100, []BpPeer{}, map[int64]tPBlocks{}),
  237. },
  238. }
  239. for _, tt := range tests {
  240. t.Run(tt.name, func(t *testing.T) {
  241. pool := tt.pool
  242. pool.removeShortPeers()
  243. assertBlockPoolEquivalent(t, tt.poolWanted, tt.pool)
  244. })
  245. }
  246. }
  247. func TestBlockPoolSendRequestBatch(t *testing.T) {
  248. type testPeerResult struct {
  249. id p2p.ID
  250. numPendingBlockRequests int
  251. }
  252. testBcR := newTestBcR()
  253. tests := []struct {
  254. name string
  255. pool *BlockPool
  256. maxRequestsPerPeer int
  257. expRequests map[int64]bool
  258. expPeerResults []testPeerResult
  259. expnumPendingBlockRequests int
  260. }{
  261. {
  262. name: "one peer - send up to maxRequestsPerPeer block requests",
  263. pool: makeBlockPool(testBcR, 10, []BpPeer{{ID: "P1", Height: 100}}, map[int64]tPBlocks{}),
  264. maxRequestsPerPeer: 2,
  265. expRequests: map[int64]bool{10: true, 11: true},
  266. expPeerResults: []testPeerResult{{id: "P1", numPendingBlockRequests: 2}},
  267. expnumPendingBlockRequests: 2,
  268. },
  269. {
  270. name: "n peers - send n*maxRequestsPerPeer block requests",
  271. pool: makeBlockPool(testBcR, 10, []BpPeer{{ID: "P1", Height: 100}, {ID: "P2", Height: 100}}, map[int64]tPBlocks{}),
  272. maxRequestsPerPeer: 2,
  273. expRequests: map[int64]bool{10: true, 11: true},
  274. expPeerResults: []testPeerResult{
  275. {id: "P1", numPendingBlockRequests: 2},
  276. {id: "P2", numPendingBlockRequests: 2}},
  277. expnumPendingBlockRequests: 4,
  278. },
  279. }
  280. for _, tt := range tests {
  281. t.Run(tt.name, func(t *testing.T) {
  282. resetPoolTestResults()
  283. var pool = tt.pool
  284. maxRequestsPerPeer = tt.maxRequestsPerPeer
  285. pool.MakeNextRequests(10)
  286. assert.Equal(t, testResults.numRequestsSent, maxRequestsPerPeer*len(pool.peers))
  287. for _, tPeer := range tt.expPeerResults {
  288. var peer = pool.peers[tPeer.id]
  289. assert.NotNil(t, peer)
  290. assert.Equal(t, tPeer.numPendingBlockRequests, peer.NumPendingBlockRequests)
  291. }
  292. assert.Equal(t, testResults.numRequestsSent, maxRequestsPerPeer*len(pool.peers))
  293. })
  294. }
  295. }
  296. func TestBlockPoolAddBlock(t *testing.T) {
  297. testBcR := newTestBcR()
  298. txs := []types.Tx{types.Tx("foo"), types.Tx("bar")}
  299. type args struct {
  300. peerID p2p.ID
  301. block *types.Block
  302. blockSize int
  303. }
  304. tests := []struct {
  305. name string
  306. pool *BlockPool
  307. args args
  308. poolWanted *BlockPool
  309. errWanted error
  310. }{
  311. {name: "block from unknown peer",
  312. pool: makeBlockPool(testBcR, 10, []BpPeer{{ID: "P1", Height: 100}}, map[int64]tPBlocks{}),
  313. args: args{
  314. peerID: "P2",
  315. block: types.MakeBlock(int64(10), txs, nil, nil),
  316. blockSize: 100,
  317. },
  318. poolWanted: makeBlockPool(testBcR, 10, []BpPeer{{ID: "P1", Height: 100}}, map[int64]tPBlocks{}),
  319. errWanted: errBadDataFromPeer,
  320. },
  321. {name: "unexpected block 11 from known peer - waiting for 10",
  322. pool: makeBlockPool(testBcR, 10,
  323. []BpPeer{{ID: "P1", Height: 100}},
  324. map[int64]tPBlocks{10: {"P1", false}}),
  325. args: args{
  326. peerID: "P1",
  327. block: types.MakeBlock(int64(11), txs, nil, nil),
  328. blockSize: 100,
  329. },
  330. poolWanted: makeBlockPool(testBcR, 10,
  331. []BpPeer{{ID: "P1", Height: 100}},
  332. map[int64]tPBlocks{10: {"P1", false}}),
  333. errWanted: errMissingBlock,
  334. },
  335. {name: "unexpected block 10 from known peer - already have 10",
  336. pool: makeBlockPool(testBcR, 10,
  337. []BpPeer{{ID: "P1", Height: 100}},
  338. map[int64]tPBlocks{10: {"P1", true}, 11: {"P1", false}}),
  339. args: args{
  340. peerID: "P1",
  341. block: types.MakeBlock(int64(10), txs, nil, nil),
  342. blockSize: 100,
  343. },
  344. poolWanted: makeBlockPool(testBcR, 10,
  345. []BpPeer{{ID: "P1", Height: 100}},
  346. map[int64]tPBlocks{10: {"P1", true}, 11: {"P1", false}}),
  347. errWanted: errDuplicateBlock,
  348. },
  349. {name: "unexpected block 10 from known peer P2 - expected 10 to come from P1",
  350. pool: makeBlockPool(testBcR, 10,
  351. []BpPeer{{ID: "P1", Height: 100}, {ID: "P2", Height: 100}},
  352. map[int64]tPBlocks{10: {"P1", false}}),
  353. args: args{
  354. peerID: "P2",
  355. block: types.MakeBlock(int64(10), txs, nil, nil),
  356. blockSize: 100,
  357. },
  358. poolWanted: makeBlockPool(testBcR, 10,
  359. []BpPeer{{ID: "P1", Height: 100}, {ID: "P2", Height: 100}},
  360. map[int64]tPBlocks{10: {"P1", false}}),
  361. errWanted: errBadDataFromPeer,
  362. },
  363. {name: "expected block from known peer",
  364. pool: makeBlockPool(testBcR, 10,
  365. []BpPeer{{ID: "P1", Height: 100}},
  366. map[int64]tPBlocks{10: {"P1", false}}),
  367. args: args{
  368. peerID: "P1",
  369. block: types.MakeBlock(int64(10), txs, nil, nil),
  370. blockSize: 100,
  371. },
  372. poolWanted: makeBlockPool(testBcR, 10,
  373. []BpPeer{{ID: "P1", Height: 100}},
  374. map[int64]tPBlocks{10: {"P1", true}}),
  375. errWanted: nil,
  376. },
  377. }
  378. for _, tt := range tests {
  379. t.Run(tt.name, func(t *testing.T) {
  380. err := tt.pool.AddBlock(tt.args.peerID, tt.args.block, tt.args.blockSize)
  381. assert.Equal(t, tt.errWanted, err)
  382. assertBlockPoolEquivalent(t, tt.poolWanted, tt.pool)
  383. })
  384. }
  385. }
  386. func TestBlockPoolFirstTwoBlocksAndPeers(t *testing.T) {
  387. testBcR := newTestBcR()
  388. tests := []struct {
  389. name string
  390. pool *BlockPool
  391. firstWanted int64
  392. secondWanted int64
  393. errWanted error
  394. }{
  395. {
  396. name: "both blocks missing",
  397. pool: makeBlockPool(testBcR, 10,
  398. []BpPeer{{ID: "P1", Height: 100}, {ID: "P2", Height: 100}},
  399. map[int64]tPBlocks{15: {"P1", true}, 16: {"P2", true}}),
  400. errWanted: errMissingBlock,
  401. },
  402. {
  403. name: "second block missing",
  404. pool: makeBlockPool(testBcR, 15,
  405. []BpPeer{{ID: "P1", Height: 100}, {ID: "P2", Height: 100}},
  406. map[int64]tPBlocks{15: {"P1", true}, 18: {"P2", true}}),
  407. firstWanted: 15,
  408. errWanted: errMissingBlock,
  409. },
  410. {
  411. name: "first block missing",
  412. pool: makeBlockPool(testBcR, 15,
  413. []BpPeer{{ID: "P1", Height: 100}, {ID: "P2", Height: 100}},
  414. map[int64]tPBlocks{16: {"P2", true}, 18: {"P2", true}}),
  415. secondWanted: 16,
  416. errWanted: errMissingBlock,
  417. },
  418. {
  419. name: "both blocks present",
  420. pool: makeBlockPool(testBcR, 10,
  421. []BpPeer{{ID: "P1", Height: 100}, {ID: "P2", Height: 100}},
  422. map[int64]tPBlocks{10: {"P1", true}, 11: {"P2", true}}),
  423. firstWanted: 10,
  424. secondWanted: 11,
  425. },
  426. }
  427. for _, tt := range tests {
  428. t.Run(tt.name, func(t *testing.T) {
  429. pool := tt.pool
  430. gotFirst, gotSecond, err := pool.FirstTwoBlocksAndPeers()
  431. assert.Equal(t, tt.errWanted, err)
  432. if tt.firstWanted != 0 {
  433. peer := pool.blocks[tt.firstWanted]
  434. block := pool.peers[peer].blocks[tt.firstWanted]
  435. assert.Equal(t, block, gotFirst.block,
  436. "BlockPool.FirstTwoBlocksAndPeers() gotFirst = %v, want %v",
  437. tt.firstWanted, gotFirst.block.Height)
  438. }
  439. if tt.secondWanted != 0 {
  440. peer := pool.blocks[tt.secondWanted]
  441. block := pool.peers[peer].blocks[tt.secondWanted]
  442. assert.Equal(t, block, gotSecond.block,
  443. "BlockPool.FirstTwoBlocksAndPeers() gotFirst = %v, want %v",
  444. tt.secondWanted, gotSecond.block.Height)
  445. }
  446. })
  447. }
  448. }
  449. func TestBlockPoolInvalidateFirstTwoBlocks(t *testing.T) {
  450. testBcR := newTestBcR()
  451. tests := []struct {
  452. name string
  453. pool *BlockPool
  454. poolWanted *BlockPool
  455. }{
  456. {
  457. name: "both blocks missing",
  458. pool: makeBlockPool(testBcR, 10,
  459. []BpPeer{{ID: "P1", Height: 100}, {ID: "P2", Height: 100}},
  460. map[int64]tPBlocks{15: {"P1", true}, 16: {"P2", true}}),
  461. poolWanted: makeBlockPool(testBcR, 10,
  462. []BpPeer{{ID: "P1", Height: 100}, {ID: "P2", Height: 100}},
  463. map[int64]tPBlocks{15: {"P1", true}, 16: {"P2", true}}),
  464. },
  465. {
  466. name: "second block missing",
  467. pool: makeBlockPool(testBcR, 15,
  468. []BpPeer{{ID: "P1", Height: 100}, {ID: "P2", Height: 100}},
  469. map[int64]tPBlocks{15: {"P1", true}, 18: {"P2", true}}),
  470. poolWanted: makeBlockPool(testBcR, 15,
  471. []BpPeer{{ID: "P2", Height: 100}},
  472. map[int64]tPBlocks{18: {"P2", true}}),
  473. },
  474. {
  475. name: "first block missing",
  476. pool: makeBlockPool(testBcR, 15,
  477. []BpPeer{{ID: "P1", Height: 100}, {ID: "P2", Height: 100}},
  478. map[int64]tPBlocks{18: {"P1", true}, 16: {"P2", true}}),
  479. poolWanted: makeBlockPool(testBcR, 15,
  480. []BpPeer{{ID: "P1", Height: 100}},
  481. map[int64]tPBlocks{18: {"P1", true}}),
  482. },
  483. {
  484. name: "both blocks present",
  485. pool: makeBlockPool(testBcR, 10,
  486. []BpPeer{{ID: "P1", Height: 100}, {ID: "P2", Height: 100}},
  487. map[int64]tPBlocks{10: {"P1", true}, 11: {"P2", true}}),
  488. poolWanted: makeBlockPool(testBcR, 10,
  489. []BpPeer{},
  490. map[int64]tPBlocks{}),
  491. },
  492. }
  493. for _, tt := range tests {
  494. t.Run(tt.name, func(t *testing.T) {
  495. tt.pool.InvalidateFirstTwoBlocks(errNoPeerResponse)
  496. assertBlockPoolEquivalent(t, tt.poolWanted, tt.pool)
  497. })
  498. }
  499. }
  500. func TestProcessedCurrentHeightBlock(t *testing.T) {
  501. testBcR := newTestBcR()
  502. tests := []struct {
  503. name string
  504. pool *BlockPool
  505. poolWanted *BlockPool
  506. }{
  507. {
  508. name: "one peer",
  509. pool: makeBlockPool(testBcR, 100, []BpPeer{{ID: "P1", Height: 120}},
  510. map[int64]tPBlocks{100: {"P1", true}, 101: {"P1", true}}),
  511. poolWanted: makeBlockPool(testBcR, 101, []BpPeer{{ID: "P1", Height: 120}},
  512. map[int64]tPBlocks{101: {"P1", true}}),
  513. },
  514. {
  515. name: "multiple peers",
  516. pool: makeBlockPool(testBcR, 100,
  517. []BpPeer{{ID: "P1", Height: 120}, {ID: "P2", Height: 120}, {ID: "P3", Height: 130}},
  518. map[int64]tPBlocks{
  519. 100: {"P1", true}, 104: {"P1", true}, 105: {"P1", false},
  520. 101: {"P2", true}, 103: {"P2", false},
  521. 102: {"P3", true}, 106: {"P3", true}}),
  522. poolWanted: makeBlockPool(testBcR, 101,
  523. []BpPeer{{ID: "P1", Height: 120}, {ID: "P2", Height: 120}, {ID: "P3", Height: 130}},
  524. map[int64]tPBlocks{
  525. 104: {"P1", true}, 105: {"P1", false},
  526. 101: {"P2", true}, 103: {"P2", false},
  527. 102: {"P3", true}, 106: {"P3", true}}),
  528. },
  529. }
  530. for _, tt := range tests {
  531. t.Run(tt.name, func(t *testing.T) {
  532. tt.pool.ProcessedCurrentHeightBlock()
  533. assertBlockPoolEquivalent(t, tt.poolWanted, tt.pool)
  534. })
  535. }
  536. }
  537. func TestRemovePeerAtCurrentHeight(t *testing.T) {
  538. testBcR := newTestBcR()
  539. tests := []struct {
  540. name string
  541. pool *BlockPool
  542. poolWanted *BlockPool
  543. }{
  544. {
  545. name: "one peer, remove peer for block at H",
  546. pool: makeBlockPool(testBcR, 100, []BpPeer{{ID: "P1", Height: 120}},
  547. map[int64]tPBlocks{100: {"P1", false}, 101: {"P1", true}}),
  548. poolWanted: makeBlockPool(testBcR, 100, []BpPeer{}, map[int64]tPBlocks{}),
  549. },
  550. {
  551. name: "one peer, remove peer for block at H+1",
  552. pool: makeBlockPool(testBcR, 100, []BpPeer{{ID: "P1", Height: 120}},
  553. map[int64]tPBlocks{100: {"P1", true}, 101: {"P1", false}}),
  554. poolWanted: makeBlockPool(testBcR, 100, []BpPeer{}, map[int64]tPBlocks{}),
  555. },
  556. {
  557. name: "multiple peers, remove peer for block at H",
  558. pool: makeBlockPool(testBcR, 100,
  559. []BpPeer{{ID: "P1", Height: 120}, {ID: "P2", Height: 120}, {ID: "P3", Height: 130}},
  560. map[int64]tPBlocks{
  561. 100: {"P1", false}, 104: {"P1", true}, 105: {"P1", false},
  562. 101: {"P2", true}, 103: {"P2", false},
  563. 102: {"P3", true}, 106: {"P3", true}}),
  564. poolWanted: makeBlockPool(testBcR, 100,
  565. []BpPeer{{ID: "P2", Height: 120}, {ID: "P3", Height: 130}},
  566. map[int64]tPBlocks{
  567. 101: {"P2", true}, 103: {"P2", false},
  568. 102: {"P3", true}, 106: {"P3", true}}),
  569. },
  570. {
  571. name: "multiple peers, remove peer for block at H+1",
  572. pool: makeBlockPool(testBcR, 100,
  573. []BpPeer{{ID: "P1", Height: 120}, {ID: "P2", Height: 120}, {ID: "P3", Height: 130}},
  574. map[int64]tPBlocks{
  575. 100: {"P1", true}, 104: {"P1", true}, 105: {"P1", false},
  576. 101: {"P2", false}, 103: {"P2", false},
  577. 102: {"P3", true}, 106: {"P3", true}}),
  578. poolWanted: makeBlockPool(testBcR, 100,
  579. []BpPeer{{ID: "P1", Height: 120}, {ID: "P3", Height: 130}},
  580. map[int64]tPBlocks{
  581. 100: {"P1", true}, 104: {"P1", true}, 105: {"P1", false},
  582. 102: {"P3", true}, 106: {"P3", true}}),
  583. },
  584. }
  585. for _, tt := range tests {
  586. t.Run(tt.name, func(t *testing.T) {
  587. tt.pool.RemovePeerAtCurrentHeights(errNoPeerResponse)
  588. assertBlockPoolEquivalent(t, tt.poolWanted, tt.pool)
  589. })
  590. }
  591. }