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.

207 lines
4.7 KiB

  1. package p2p_test
  2. import (
  3. "sync"
  4. "testing"
  5. "github.com/gogo/protobuf/proto"
  6. "github.com/stretchr/testify/mock"
  7. "github.com/stretchr/testify/require"
  8. "github.com/tendermint/tendermint/config"
  9. "github.com/tendermint/tendermint/p2p"
  10. p2pmocks "github.com/tendermint/tendermint/p2p/mocks"
  11. ssproto "github.com/tendermint/tendermint/proto/tendermint/statesync"
  12. )
  13. var (
  14. channelID1 = byte(0x01)
  15. channelID2 = byte(0x02)
  16. p2pCfg = config.DefaultP2PConfig()
  17. testChannelShims = map[p2p.ChannelID]*p2p.ChannelDescriptorShim{
  18. p2p.ChannelID(channelID1): {
  19. MsgType: new(ssproto.Message),
  20. Descriptor: &p2p.ChannelDescriptor{
  21. ID: channelID1,
  22. Priority: 3,
  23. SendQueueCapacity: 10,
  24. RecvMessageCapacity: int(4e6),
  25. },
  26. },
  27. p2p.ChannelID(channelID2): {
  28. MsgType: new(ssproto.Message),
  29. Descriptor: &p2p.ChannelDescriptor{
  30. ID: channelID2,
  31. Priority: 1,
  32. SendQueueCapacity: 4,
  33. RecvMessageCapacity: int(16e6),
  34. },
  35. },
  36. }
  37. )
  38. type reactorShimTestSuite struct {
  39. shim *p2p.ReactorShim
  40. sw *p2p.Switch
  41. }
  42. func setup(t *testing.T, peers []p2p.Peer) *reactorShimTestSuite {
  43. t.Helper()
  44. rts := &reactorShimTestSuite{
  45. shim: p2p.NewReactorShim("TestShim", testChannelShims),
  46. }
  47. rts.sw = p2p.MakeSwitch(p2pCfg, 1, "testing", "123.123.123", func(_ int, sw *p2p.Switch) *p2p.Switch {
  48. for _, peer := range peers {
  49. p2p.AddPeerToSwitchPeerSet(sw, peer)
  50. }
  51. sw.AddReactor(rts.shim.Name, rts.shim)
  52. return sw
  53. })
  54. // start the reactor shim
  55. require.NoError(t, rts.shim.Start())
  56. t.Cleanup(func() {
  57. require.NoError(t, rts.shim.Stop())
  58. for _, chs := range rts.shim.Channels {
  59. chs.Channel.Close()
  60. }
  61. })
  62. return rts
  63. }
  64. func simplePeer(t *testing.T, id string) (*p2pmocks.Peer, p2p.PeerID) {
  65. t.Helper()
  66. peer := &p2pmocks.Peer{}
  67. peer.On("ID").Return(p2p.ID(id))
  68. pID, err := p2p.PeerIDFromString(string(peer.ID()))
  69. require.NoError(t, err)
  70. return peer, pID
  71. }
  72. func TestReactorShim_GetChannel(t *testing.T) {
  73. rts := setup(t, nil)
  74. p2pCh := rts.shim.GetChannel(p2p.ChannelID(channelID1))
  75. require.NotNil(t, p2pCh)
  76. require.Equal(t, p2pCh.ID(), p2p.ChannelID(channelID1))
  77. p2pCh = rts.shim.GetChannel(p2p.ChannelID(byte(0x03)))
  78. require.Nil(t, p2pCh)
  79. }
  80. func TestReactorShim_GetChannels(t *testing.T) {
  81. rts := setup(t, nil)
  82. p2pChs := rts.shim.GetChannels()
  83. require.Len(t, p2pChs, 2)
  84. require.Equal(t, p2p.ChannelID(p2pChs[0].ID), p2p.ChannelID(channelID1))
  85. require.Equal(t, p2p.ChannelID(p2pChs[1].ID), p2p.ChannelID(channelID2))
  86. }
  87. func TestReactorShim_AddPeer(t *testing.T) {
  88. peerA, peerIDA := simplePeer(t, "aa")
  89. rts := setup(t, []p2p.Peer{peerA})
  90. var wg sync.WaitGroup
  91. wg.Add(1)
  92. var peerUpdate p2p.PeerUpdate
  93. go func() {
  94. peerUpdate = <-rts.shim.PeerUpdates.Updates()
  95. wg.Done()
  96. }()
  97. rts.shim.AddPeer(peerA)
  98. wg.Wait()
  99. require.Equal(t, peerIDA, peerUpdate.PeerID)
  100. require.Equal(t, p2p.PeerStatusUp, peerUpdate.Status)
  101. }
  102. func TestReactorShim_RemovePeer(t *testing.T) {
  103. peerA, peerIDA := simplePeer(t, "aa")
  104. rts := setup(t, []p2p.Peer{peerA})
  105. var wg sync.WaitGroup
  106. wg.Add(1)
  107. var peerUpdate p2p.PeerUpdate
  108. go func() {
  109. peerUpdate = <-rts.shim.PeerUpdates.Updates()
  110. wg.Done()
  111. }()
  112. rts.shim.RemovePeer(peerA, "test reason")
  113. wg.Wait()
  114. require.Equal(t, peerIDA, peerUpdate.PeerID)
  115. require.Equal(t, p2p.PeerStatusDown, peerUpdate.Status)
  116. }
  117. func TestReactorShim_Receive(t *testing.T) {
  118. peerA, peerIDA := simplePeer(t, "aa")
  119. rts := setup(t, []p2p.Peer{peerA})
  120. msg := &ssproto.Message{
  121. Sum: &ssproto.Message_ChunkRequest{
  122. ChunkRequest: &ssproto.ChunkRequest{Height: 1, Format: 1, Index: 1},
  123. },
  124. }
  125. bz, err := proto.Marshal(msg)
  126. require.NoError(t, err)
  127. var wg sync.WaitGroup
  128. var response *ssproto.Message
  129. peerA.On("Send", channelID1, mock.Anything).Run(func(args mock.Arguments) {
  130. m := &ssproto.Message{}
  131. require.NoError(t, proto.Unmarshal(args[1].([]byte), m))
  132. response = m
  133. wg.Done()
  134. }).Return(true)
  135. p2pCh := rts.shim.Channels[p2p.ChannelID(channelID1)]
  136. wg.Add(2)
  137. // Simulate receiving the envelope in some real reactor and replying back with
  138. // the same envelope and then closing the Channel.
  139. go func() {
  140. e := <-p2pCh.Channel.In()
  141. require.Equal(t, peerIDA, e.From)
  142. require.NotNil(t, e.Message)
  143. p2pCh.Channel.Out() <- p2p.Envelope{To: e.From, Message: e.Message}
  144. p2pCh.Channel.Close()
  145. wg.Done()
  146. }()
  147. rts.shim.Receive(channelID1, peerA, bz)
  148. // wait until the mock peer called Send and we (fake) proxied the envelope
  149. wg.Wait()
  150. require.NotNil(t, response)
  151. m, err := response.Unwrap()
  152. require.NoError(t, err)
  153. require.Equal(t, msg.GetChunkRequest(), m)
  154. // Since p2pCh was closed in the simulated reactor above, calling Receive
  155. // should not block.
  156. rts.shim.Receive(channelID1, peerA, bz)
  157. require.Empty(t, p2pCh.Channel.In())
  158. peerA.AssertExpectations(t)
  159. }