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.

289 lines
7.0 KiB

  1. package statesync
  2. import (
  3. "context"
  4. "fmt"
  5. "strings"
  6. "sync"
  7. "testing"
  8. "time"
  9. "github.com/fortytw2/leaktest"
  10. "github.com/stretchr/testify/assert"
  11. "github.com/stretchr/testify/require"
  12. "github.com/tendermint/tendermint/internal/p2p"
  13. "github.com/tendermint/tendermint/internal/test/factory"
  14. ssproto "github.com/tendermint/tendermint/proto/tendermint/statesync"
  15. "github.com/tendermint/tendermint/types"
  16. )
  17. func TestDispatcherBasic(t *testing.T) {
  18. t.Cleanup(leaktest.Check(t))
  19. const numPeers = 5
  20. ch := make(chan p2p.Envelope, 100)
  21. closeCh := make(chan struct{})
  22. defer close(closeCh)
  23. d := NewDispatcher(ch)
  24. go handleRequests(t, d, ch, closeCh)
  25. peers := createPeerSet(numPeers)
  26. wg := sync.WaitGroup{}
  27. // make a bunch of async requests and require that the correct responses are
  28. // given
  29. for i := 0; i < numPeers; i++ {
  30. wg.Add(1)
  31. go func(height int64) {
  32. defer wg.Done()
  33. lb, err := d.LightBlock(context.Background(), height, peers[height-1])
  34. require.NoError(t, err)
  35. require.NotNil(t, lb)
  36. require.Equal(t, lb.Height, height)
  37. }(int64(i + 1))
  38. }
  39. wg.Wait()
  40. // assert that all calls were responded to
  41. assert.Empty(t, d.calls)
  42. }
  43. func TestDispatcherReturnsNoBlock(t *testing.T) {
  44. t.Cleanup(leaktest.Check(t))
  45. ch := make(chan p2p.Envelope, 100)
  46. d := NewDispatcher(ch)
  47. doneCh := make(chan struct{})
  48. peer := factory.NodeID("a")
  49. go func() {
  50. <-ch
  51. require.NoError(t, d.Respond(nil, peer))
  52. close(doneCh)
  53. }()
  54. lb, err := d.LightBlock(context.Background(), 1, peer)
  55. <-doneCh
  56. require.Nil(t, lb)
  57. require.Nil(t, err)
  58. }
  59. func TestDispatcherTimeOutWaitingOnLightBlock(t *testing.T) {
  60. t.Cleanup(leaktest.Check(t))
  61. ch := make(chan p2p.Envelope, 100)
  62. d := NewDispatcher(ch)
  63. peer := factory.NodeID("a")
  64. ctx, cancelFunc := context.WithTimeout(context.Background(), 10*time.Millisecond)
  65. defer cancelFunc()
  66. lb, err := d.LightBlock(ctx, 1, peer)
  67. require.Error(t, err)
  68. require.Equal(t, context.DeadlineExceeded, err)
  69. require.Nil(t, lb)
  70. }
  71. func TestDispatcherProviders(t *testing.T) {
  72. t.Cleanup(leaktest.Check(t))
  73. ch := make(chan p2p.Envelope, 100)
  74. chainID := "test-chain"
  75. closeCh := make(chan struct{})
  76. defer close(closeCh)
  77. d := NewDispatcher(ch)
  78. go handleRequests(t, d, ch, closeCh)
  79. peers := createPeerSet(5)
  80. providers := make([]*BlockProvider, len(peers))
  81. for idx, peer := range peers {
  82. providers[idx] = NewBlockProvider(peer, chainID, d)
  83. }
  84. require.Len(t, providers, 5)
  85. for i, p := range providers {
  86. assert.Equal(t, string(peers[i]), p.String(), i)
  87. lb, err := p.LightBlock(context.Background(), 10)
  88. assert.NoError(t, err)
  89. assert.NotNil(t, lb)
  90. }
  91. }
  92. func TestPeerListBasic(t *testing.T) {
  93. t.Cleanup(leaktest.Check(t))
  94. peerList := newPeerList()
  95. assert.Zero(t, peerList.Len())
  96. numPeers := 10
  97. peerSet := createPeerSet(numPeers)
  98. for _, peer := range peerSet {
  99. peerList.Append(peer)
  100. }
  101. for idx, peer := range peerList.All() {
  102. assert.Equal(t, peer, peerSet[idx])
  103. }
  104. assert.Equal(t, numPeers, peerList.Len())
  105. half := numPeers / 2
  106. for i := 0; i < half; i++ {
  107. assert.Equal(t, peerSet[i], peerList.Pop(ctx))
  108. }
  109. assert.Equal(t, half, peerList.Len())
  110. // removing a peer that doesn't exist should not change the list
  111. peerList.Remove(types.NodeID("lp"))
  112. assert.Equal(t, half, peerList.Len())
  113. // removing a peer that exists should decrease the list size by one
  114. peerList.Remove(peerSet[half])
  115. assert.Equal(t, numPeers-half-1, peerList.Len())
  116. // popping the next peer should work as expected
  117. assert.Equal(t, peerSet[half+1], peerList.Pop(ctx))
  118. assert.Equal(t, numPeers-half-2, peerList.Len())
  119. // append the two peers back
  120. peerList.Append(peerSet[half])
  121. peerList.Append(peerSet[half+1])
  122. assert.Equal(t, half, peerList.Len())
  123. }
  124. func TestPeerListBlocksWhenEmpty(t *testing.T) {
  125. t.Cleanup(leaktest.Check(t))
  126. peerList := newPeerList()
  127. require.Zero(t, peerList.Len())
  128. doneCh := make(chan struct{})
  129. ctx, cancel := context.WithCancel(context.Background())
  130. defer cancel()
  131. go func() {
  132. peerList.Pop(ctx)
  133. close(doneCh)
  134. }()
  135. select {
  136. case <-doneCh:
  137. t.Error("empty peer list should not have returned result")
  138. case <-time.After(100 * time.Millisecond):
  139. }
  140. }
  141. func TestEmptyPeerListReturnsWhenContextCanceled(t *testing.T) {
  142. t.Cleanup(leaktest.Check(t))
  143. peerList := newPeerList()
  144. require.Zero(t, peerList.Len())
  145. doneCh := make(chan struct{})
  146. ctx := context.Background()
  147. wrapped, cancel := context.WithCancel(ctx)
  148. go func() {
  149. peerList.Pop(wrapped)
  150. close(doneCh)
  151. }()
  152. select {
  153. case <-doneCh:
  154. t.Error("empty peer list should not have returned result")
  155. case <-time.After(100 * time.Millisecond):
  156. }
  157. cancel()
  158. select {
  159. case <-doneCh:
  160. case <-time.After(100 * time.Millisecond):
  161. t.Error("peer list should have returned after context canceled")
  162. }
  163. }
  164. func TestPeerListConcurrent(t *testing.T) {
  165. t.Cleanup(leaktest.Check(t))
  166. peerList := newPeerList()
  167. numPeers := 10
  168. wg := sync.WaitGroup{}
  169. // we run a set of goroutines requesting the next peer in the list. As the
  170. // peer list hasn't been populated each these go routines should block
  171. for i := 0; i < numPeers/2; i++ {
  172. go func() {
  173. _ = peerList.Pop(ctx)
  174. wg.Done()
  175. }()
  176. }
  177. // now we add the peers to the list, this should allow the previously
  178. // blocked go routines to unblock
  179. for _, peer := range createPeerSet(numPeers) {
  180. wg.Add(1)
  181. peerList.Append(peer)
  182. }
  183. // we request the second half of the peer set
  184. for i := 0; i < numPeers/2; i++ {
  185. go func() {
  186. _ = peerList.Pop(ctx)
  187. wg.Done()
  188. }()
  189. }
  190. // we use a context with cancel and a separate go routine to wait for all
  191. // the other goroutines to close.
  192. ctx, cancel := context.WithCancel(context.Background())
  193. go func() { wg.Wait(); cancel() }()
  194. select {
  195. case <-time.After(time.Second):
  196. // not all of the blocked go routines waiting on peers have closed after
  197. // one second. This likely means the list got blocked.
  198. t.Failed()
  199. case <-ctx.Done():
  200. // there should be no peers remaining
  201. require.Equal(t, 0, peerList.Len())
  202. }
  203. }
  204. func TestPeerListRemove(t *testing.T) {
  205. peerList := newPeerList()
  206. numPeers := 10
  207. peerSet := createPeerSet(numPeers)
  208. for _, peer := range peerSet {
  209. peerList.Append(peer)
  210. }
  211. for _, peer := range peerSet {
  212. peerList.Remove(peer)
  213. for _, p := range peerList.All() {
  214. require.NotEqual(t, p, peer)
  215. }
  216. numPeers--
  217. require.Equal(t, numPeers, peerList.Len())
  218. }
  219. }
  220. // handleRequests is a helper function usually run in a separate go routine to
  221. // imitate the expected responses of the reactor wired to the dispatcher
  222. func handleRequests(t *testing.T, d *Dispatcher, ch chan p2p.Envelope, closeCh chan struct{}) {
  223. t.Helper()
  224. for {
  225. select {
  226. case request := <-ch:
  227. height := request.Message.(*ssproto.LightBlockRequest).Height
  228. peer := request.To
  229. resp := mockLBResp(t, peer, int64(height), time.Now())
  230. block, _ := resp.block.ToProto()
  231. require.NoError(t, d.Respond(block, resp.peer))
  232. case <-closeCh:
  233. return
  234. }
  235. }
  236. }
  237. func createPeerSet(num int) []types.NodeID {
  238. peers := make([]types.NodeID, num)
  239. for i := 0; i < num; i++ {
  240. peers[i], _ = types.NewNodeID(strings.Repeat(fmt.Sprintf("%d", i), 2*types.NodeIDByteLength))
  241. }
  242. return peers
  243. }