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.

272 lines
8.9 KiB

  1. package v2
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/tendermint/tendermint/p2p"
  7. )
  8. func TestScheduleInit(t *testing.T) {
  9. var (
  10. initHeight int64 = 5
  11. sc = newSchedule(initHeight)
  12. )
  13. assert.Equal(t, blockStateNew, sc.getStateAtHeight(initHeight))
  14. assert.Equal(t, blockStateProcessed, sc.getStateAtHeight(initHeight-1))
  15. assert.Equal(t, blockStateUnknown, sc.getStateAtHeight(initHeight+1))
  16. }
  17. func TestAddPeer(t *testing.T) {
  18. var (
  19. initHeight int64 = 5
  20. peerID p2p.ID = "1"
  21. peerIDTwo p2p.ID = "2"
  22. sc = newSchedule(initHeight)
  23. )
  24. assert.Nil(t, sc.addPeer(peerID))
  25. assert.Nil(t, sc.addPeer(peerIDTwo))
  26. assert.Error(t, sc.addPeer(peerID))
  27. }
  28. func TestTouchPeer(t *testing.T) {
  29. var (
  30. initHeight int64 = 5
  31. peerID p2p.ID = "1"
  32. sc = newSchedule(initHeight)
  33. now = time.Now()
  34. )
  35. assert.Error(t, sc.touchPeer(peerID, now),
  36. "Touching an unknown peer should return errPeerNotFound")
  37. assert.Nil(t, sc.addPeer(peerID),
  38. "Adding a peer should return no error")
  39. assert.Nil(t, sc.touchPeer(peerID, now),
  40. "Touching a peer should return no error")
  41. threshold := 10 * time.Second
  42. assert.Empty(t, sc.peersInactiveSince(threshold, now.Add(9*time.Second)),
  43. "Expected no peers to have been touched over 9 seconds")
  44. assert.Containsf(t, sc.peersInactiveSince(threshold, now.Add(11*time.Second)), peerID,
  45. "Expected one %s to have been touched over 10 seconds ago", peerID)
  46. }
  47. func TestPeerHeight(t *testing.T) {
  48. var (
  49. initHeight int64 = 5
  50. peerID p2p.ID = "1"
  51. peerHeight int64 = 20
  52. sc = newSchedule(initHeight)
  53. )
  54. assert.NoError(t, sc.addPeer(peerID),
  55. "Adding a peer should return no error")
  56. assert.NoError(t, sc.setPeerHeight(peerID, peerHeight))
  57. for i := initHeight; i <= peerHeight; i++ {
  58. assert.Equal(t, sc.getStateAtHeight(i), blockStateNew,
  59. "Expected all blocks to be in blockStateNew")
  60. peerIDs := []p2p.ID{}
  61. for _, peer := range sc.getPeersAtHeight(i) {
  62. peerIDs = append(peerIDs, peer.peerID)
  63. }
  64. assert.Containsf(t, peerIDs, peerID,
  65. "Expected %s to have block %d", peerID, i)
  66. }
  67. }
  68. func TestTransitionPending(t *testing.T) {
  69. var (
  70. initHeight int64 = 5
  71. peerID p2p.ID = "1"
  72. peerIDTwo p2p.ID = "2"
  73. peerHeight int64 = 20
  74. sc = newSchedule(initHeight)
  75. now = time.Now()
  76. )
  77. assert.NoError(t, sc.addPeer(peerID),
  78. "Adding a peer should return no error")
  79. assert.Nil(t, sc.addPeer(peerIDTwo),
  80. "Adding a peer should return no error")
  81. assert.Error(t, sc.markPending(peerID, peerHeight, now),
  82. "Expected scheduling a block from a peer in peerStateNew to fail")
  83. assert.NoError(t, sc.setPeerHeight(peerID, peerHeight),
  84. "Expected setPeerHeight to return no error")
  85. assert.NoError(t, sc.setPeerHeight(peerIDTwo, peerHeight),
  86. "Expected setPeerHeight to return no error")
  87. assert.NoError(t, sc.markPending(peerID, peerHeight, now),
  88. "Expected markingPending new block to succeed")
  89. assert.Error(t, sc.markPending(peerIDTwo, peerHeight, now),
  90. "Expected markingPending by a second peer to fail")
  91. assert.Equal(t, blockStatePending, sc.getStateAtHeight(peerHeight),
  92. "Expected the block to to be in blockStatePending")
  93. assert.NoError(t, sc.removePeer(peerID),
  94. "Expected removePeer to return no error")
  95. assert.Equal(t, blockStateNew, sc.getStateAtHeight(peerHeight),
  96. "Expected the block to to be in blockStateNew")
  97. assert.Error(t, sc.markPending(peerID, peerHeight, now),
  98. "Expected markingPending removed peer to fail")
  99. assert.NoError(t, sc.markPending(peerIDTwo, peerHeight, now),
  100. "Expected markingPending on a ready peer to succeed")
  101. assert.Equal(t, blockStatePending, sc.getStateAtHeight(peerHeight),
  102. "Expected the block to to be in blockStatePending")
  103. }
  104. func TestTransitionReceived(t *testing.T) {
  105. var (
  106. initHeight int64 = 5
  107. peerID p2p.ID = "1"
  108. peerIDTwo p2p.ID = "2"
  109. peerHeight int64 = 20
  110. blockSize int64 = 1024
  111. sc = newSchedule(initHeight)
  112. now = time.Now()
  113. receivedAt = now.Add(1 * time.Second)
  114. )
  115. assert.NoError(t, sc.addPeer(peerID),
  116. "Expected adding peer %s to succeed", peerID)
  117. assert.NoError(t, sc.addPeer(peerIDTwo),
  118. "Expected adding peer %s to succeed", peerIDTwo)
  119. assert.NoError(t, sc.setPeerHeight(peerID, peerHeight),
  120. "Expected setPeerHeight to return no error")
  121. assert.NoErrorf(t, sc.setPeerHeight(peerIDTwo, peerHeight),
  122. "Expected setPeerHeight on %s to %d to succeed", peerIDTwo, peerHeight)
  123. assert.NoError(t, sc.markPending(peerID, initHeight, now),
  124. "Expected markingPending new block to succeed")
  125. assert.Error(t, sc.markReceived(peerIDTwo, initHeight, blockSize, receivedAt),
  126. "Expected marking markReceived from a non requesting peer to fail")
  127. assert.NoError(t, sc.markReceived(peerID, initHeight, blockSize, receivedAt),
  128. "Expected marking markReceived on a pending block to succeed")
  129. assert.Error(t, sc.markReceived(peerID, initHeight, blockSize, receivedAt),
  130. "Expected marking markReceived on received block to fail")
  131. assert.Equalf(t, blockStateReceived, sc.getStateAtHeight(initHeight),
  132. "Expected block %d to be blockHeightReceived", initHeight)
  133. assert.NoErrorf(t, sc.removePeer(peerID),
  134. "Expected removePeer removing %s to succeed", peerID)
  135. assert.Equalf(t, blockStateNew, sc.getStateAtHeight(initHeight),
  136. "Expected block %d to be blockStateNew", initHeight)
  137. assert.NoErrorf(t, sc.markPending(peerIDTwo, initHeight, now),
  138. "Expected markingPending %d from %s to succeed", initHeight, peerIDTwo)
  139. assert.NoErrorf(t, sc.markReceived(peerIDTwo, initHeight, blockSize, receivedAt),
  140. "Expected marking markReceived %d from %s to succeed", initHeight, peerIDTwo)
  141. assert.Equalf(t, blockStateReceived, sc.getStateAtHeight(initHeight),
  142. "Expected block %d to be blockStateReceived", initHeight)
  143. }
  144. func TestTransitionProcessed(t *testing.T) {
  145. var (
  146. initHeight int64 = 5
  147. peerID p2p.ID = "1"
  148. peerHeight int64 = 20
  149. blockSize int64 = 1024
  150. sc = newSchedule(initHeight)
  151. now = time.Now()
  152. receivedAt = now.Add(1 * time.Second)
  153. )
  154. assert.NoError(t, sc.addPeer(peerID),
  155. "Expected adding peer %s to succeed", peerID)
  156. assert.NoErrorf(t, sc.setPeerHeight(peerID, peerHeight),
  157. "Expected setPeerHeight on %s to %d to succeed", peerID, peerHeight)
  158. assert.NoError(t, sc.markPending(peerID, initHeight, now),
  159. "Expected markingPending new block to succeed")
  160. assert.NoError(t, sc.markReceived(peerID, initHeight, blockSize, receivedAt),
  161. "Expected marking markReceived on a pending block to succeed")
  162. assert.Error(t, sc.markProcessed(initHeight+1),
  163. "Expected marking %d as processed to fail", initHeight+1)
  164. assert.NoError(t, sc.markProcessed(initHeight),
  165. "Expected marking %d as processed to succeed", initHeight)
  166. assert.Equalf(t, blockStateProcessed, sc.getStateAtHeight(initHeight),
  167. "Expected block %d to be blockStateProcessed", initHeight)
  168. assert.NoError(t, sc.removePeer(peerID),
  169. "Expected removing peer %s to succeed", peerID)
  170. assert.Equalf(t, blockStateProcessed, sc.getStateAtHeight(initHeight),
  171. "Expected block %d to be blockStateProcessed", initHeight)
  172. }
  173. func TestMinMaxHeight(t *testing.T) {
  174. var (
  175. initHeight int64 = 5
  176. peerID p2p.ID = "1"
  177. peerHeight int64 = 20
  178. sc = newSchedule(initHeight)
  179. now = time.Now()
  180. )
  181. assert.Equal(t, initHeight, sc.minHeight(),
  182. "Expected min height to be the initialized height")
  183. assert.Equal(t, initHeight, sc.maxHeight(),
  184. "Expected max height to be the initialized height")
  185. assert.NoError(t, sc.addPeer(peerID),
  186. "Adding a peer should return no error")
  187. assert.NoError(t, sc.setPeerHeight(peerID, peerHeight),
  188. "Expected setPeerHeight to return no error")
  189. assert.Equal(t, peerHeight, sc.maxHeight(),
  190. "Expected max height to increase to peerHeight")
  191. assert.Nil(t, sc.markPending(peerID, initHeight, now.Add(1*time.Second)),
  192. "Expected marking initHeight as pending to return no error")
  193. assert.Equal(t, initHeight+1, sc.minHeight(),
  194. "Expected marking initHeight as pending to move minHeight forward")
  195. }
  196. func TestPeersSlowerThan(t *testing.T) {
  197. var (
  198. initHeight int64 = 5
  199. peerID p2p.ID = "1"
  200. peerHeight int64 = 20
  201. blockSize int64 = 1024
  202. sc = newSchedule(initHeight)
  203. now = time.Now()
  204. receivedAt = now.Add(1 * time.Second)
  205. )
  206. assert.NoError(t, sc.addPeer(peerID),
  207. "Adding a peer should return no error")
  208. assert.NoError(t, sc.setPeerHeight(peerID, peerHeight),
  209. "Expected setPeerHeight to return no error")
  210. assert.NoError(t, sc.markPending(peerID, peerHeight, now),
  211. "Expected markingPending on to return no error")
  212. assert.NoError(t, sc.markReceived(peerID, peerHeight, blockSize, receivedAt),
  213. "Expected markingPending on to return no error")
  214. assert.Empty(t, sc.peersSlowerThan(blockSize-1),
  215. "expected no peers to be slower than blockSize-1 bytes/sec")
  216. assert.Containsf(t, sc.peersSlowerThan(blockSize+1), peerID,
  217. "expected %s to be slower than blockSize+1 bytes/sec", peerID)
  218. }