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.

152 lines
3.8 KiB

9 years ago
9 years ago
9 years ago
7 years ago
p2p: introduce peerConn to simplify peer creation (#1226) * expose AuthEnc in the P2P config if AuthEnc is true, dialed peers must have a node ID in the address and it must match the persistent pubkey from the secret handshake. Refs #1157 * fixes after my own review * fix docs * fix build failure ``` p2p/pex/pex_reactor_test.go:288:88: cannot use seed.NodeInfo().NetAddress() (type *p2p.NetAddress) as type string in array or slice literal ``` * p2p: introduce peerConn to simplify peer creation * Introduce `peerConn` containing the known fields of `peer` * `peer` only created in `sw.addPeer` once handshake is complete and NodeInfo is checked * Eliminates some mutable variables and makes the code flow better * Simplifies the `newXxxPeer` funcs * Use ID instead of PubKey where possible. * SetPubKeyFilter -> SetIDFilter * nodeInfo.Validate takes ID * remove peer.PubKey() * persistent node ids * fixes from review * test: use ip_plus_id.sh more * fix invalid memory panic during fast_sync test ``` 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: panic: runtime error: invalid memory address or nil pointer dereference 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: [signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x98dd3e] 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: goroutine 3432 [running]: 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.newOutboundPeerConn(0xc423fd1380, 0xc420933e00, 0x1, 0x1239a60, 0 xc420128c40, 0x2, 0x42caf6, 0xc42001f300, 0xc422831d98, 0xc4227951c0, ...) 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/peer.go:123 +0x31e 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.(*Switch).addOutboundPeerWithConfig(0xc4200ad040, 0xc423fd1380, 0 xc420933e00, 0xc423f48801, 0x28, 0x2) 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:455 +0x12b 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.(*Switch).DialPeerWithAddress(0xc4200ad040, 0xc423fd1380, 0x1, 0x 0, 0x0) 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:371 +0xdc 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.(*Switch).reconnectToPeer(0xc4200ad040, 0x123e000, 0xc42007bb00) 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:290 +0x25f 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: created by github.com/tendermint/tendermint/p2p.(*Switch).StopPeerForError 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:256 +0x1b7 ```
7 years ago
7 years ago
p2p: introduce peerConn to simplify peer creation (#1226) * expose AuthEnc in the P2P config if AuthEnc is true, dialed peers must have a node ID in the address and it must match the persistent pubkey from the secret handshake. Refs #1157 * fixes after my own review * fix docs * fix build failure ``` p2p/pex/pex_reactor_test.go:288:88: cannot use seed.NodeInfo().NetAddress() (type *p2p.NetAddress) as type string in array or slice literal ``` * p2p: introduce peerConn to simplify peer creation * Introduce `peerConn` containing the known fields of `peer` * `peer` only created in `sw.addPeer` once handshake is complete and NodeInfo is checked * Eliminates some mutable variables and makes the code flow better * Simplifies the `newXxxPeer` funcs * Use ID instead of PubKey where possible. * SetPubKeyFilter -> SetIDFilter * nodeInfo.Validate takes ID * remove peer.PubKey() * persistent node ids * fixes from review * test: use ip_plus_id.sh more * fix invalid memory panic during fast_sync test ``` 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: panic: runtime error: invalid memory address or nil pointer dereference 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: [signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x98dd3e] 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: goroutine 3432 [running]: 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.newOutboundPeerConn(0xc423fd1380, 0xc420933e00, 0x1, 0x1239a60, 0 xc420128c40, 0x2, 0x42caf6, 0xc42001f300, 0xc422831d98, 0xc4227951c0, ...) 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/peer.go:123 +0x31e 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.(*Switch).addOutboundPeerWithConfig(0xc4200ad040, 0xc423fd1380, 0 xc420933e00, 0xc423f48801, 0x28, 0x2) 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:455 +0x12b 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.(*Switch).DialPeerWithAddress(0xc4200ad040, 0xc423fd1380, 0x1, 0x 0, 0x0) 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:371 +0xdc 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.(*Switch).reconnectToPeer(0xc4200ad040, 0x123e000, 0xc42007bb00) 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:290 +0x25f 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: created by github.com/tendermint/tendermint/p2p.(*Switch).StopPeerForError 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:256 +0x1b7 ```
7 years ago
9 years ago
9 years ago
7 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
7 years ago
9 years ago
9 years ago
  1. package p2p
  2. import (
  3. "math/rand"
  4. "sync"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. crypto "github.com/tendermint/go-crypto"
  8. cmn "github.com/tendermint/tmlibs/common"
  9. )
  10. // Returns an empty kvstore peer
  11. func randPeer() *peer {
  12. pubKey := crypto.GenPrivKeyEd25519().Wrap().PubKey()
  13. return &peer{
  14. nodeInfo: NodeInfo{
  15. ListenAddr: cmn.Fmt("%v.%v.%v.%v:46656", rand.Int()%256, rand.Int()%256, rand.Int()%256, rand.Int()%256),
  16. PubKey: pubKey,
  17. },
  18. }
  19. }
  20. func TestPeerSetAddRemoveOne(t *testing.T) {
  21. t.Parallel()
  22. peerSet := NewPeerSet()
  23. var peerList []Peer
  24. for i := 0; i < 5; i++ {
  25. p := randPeer()
  26. if err := peerSet.Add(p); err != nil {
  27. t.Error(err)
  28. }
  29. peerList = append(peerList, p)
  30. }
  31. n := len(peerList)
  32. // 1. Test removing from the front
  33. for i, peerAtFront := range peerList {
  34. peerSet.Remove(peerAtFront)
  35. wantSize := n - i - 1
  36. for j := 0; j < 2; j++ {
  37. assert.Equal(t, false, peerSet.Has(peerAtFront.ID()), "#%d Run #%d: failed to remove peer", i, j)
  38. assert.Equal(t, wantSize, peerSet.Size(), "#%d Run #%d: failed to remove peer and decrement size", i, j)
  39. // Test the route of removing the now non-existent element
  40. peerSet.Remove(peerAtFront)
  41. }
  42. }
  43. // 2. Next we are testing removing the peer at the end
  44. // a) Replenish the peerSet
  45. for _, peer := range peerList {
  46. if err := peerSet.Add(peer); err != nil {
  47. t.Error(err)
  48. }
  49. }
  50. // b) In reverse, remove each element
  51. for i := n - 1; i >= 0; i-- {
  52. peerAtEnd := peerList[i]
  53. peerSet.Remove(peerAtEnd)
  54. assert.Equal(t, false, peerSet.Has(peerAtEnd.ID()), "#%d: failed to remove item at end", i)
  55. assert.Equal(t, i, peerSet.Size(), "#%d: differing sizes after peerSet.Remove(atEndPeer)", i)
  56. }
  57. }
  58. func TestPeerSetAddRemoveMany(t *testing.T) {
  59. t.Parallel()
  60. peerSet := NewPeerSet()
  61. peers := []Peer{}
  62. N := 100
  63. for i := 0; i < N; i++ {
  64. peer := randPeer()
  65. if err := peerSet.Add(peer); err != nil {
  66. t.Errorf("Failed to add new peer")
  67. }
  68. if peerSet.Size() != i+1 {
  69. t.Errorf("Failed to add new peer and increment size")
  70. }
  71. peers = append(peers, peer)
  72. }
  73. for i, peer := range peers {
  74. peerSet.Remove(peer)
  75. if peerSet.Has(peer.ID()) {
  76. t.Errorf("Failed to remove peer")
  77. }
  78. if peerSet.Size() != len(peers)-i-1 {
  79. t.Errorf("Failed to remove peer and decrement size")
  80. }
  81. }
  82. }
  83. func TestPeerSetAddDuplicate(t *testing.T) {
  84. t.Parallel()
  85. peerSet := NewPeerSet()
  86. peer := randPeer()
  87. n := 20
  88. errsChan := make(chan error)
  89. // Add the same asynchronously to test the
  90. // concurrent guarantees of our APIs, and
  91. // our expectation in the end is that only
  92. // one addition succeeded, but the rest are
  93. // instances of ErrSwitchDuplicatePeer.
  94. for i := 0; i < n; i++ {
  95. go func() {
  96. errsChan <- peerSet.Add(peer)
  97. }()
  98. }
  99. // Now collect and tally the results
  100. errsTally := make(map[error]int)
  101. for i := 0; i < n; i++ {
  102. err := <-errsChan
  103. errsTally[err] += 1
  104. }
  105. // Our next procedure is to ensure that only one addition
  106. // succeeded and that the rest are each ErrSwitchDuplicatePeer.
  107. wantErrCount, gotErrCount := n-1, errsTally[ErrSwitchDuplicatePeer]
  108. assert.Equal(t, wantErrCount, gotErrCount, "invalid ErrSwitchDuplicatePeer count")
  109. wantNilErrCount, gotNilErrCount := 1, errsTally[nil]
  110. assert.Equal(t, wantNilErrCount, gotNilErrCount, "invalid nil errCount")
  111. }
  112. func TestPeerSetGet(t *testing.T) {
  113. t.Parallel()
  114. peerSet := NewPeerSet()
  115. peer := randPeer()
  116. assert.Nil(t, peerSet.Get(peer.ID()), "expecting a nil lookup, before .Add")
  117. if err := peerSet.Add(peer); err != nil {
  118. t.Fatalf("Failed to add new peer: %v", err)
  119. }
  120. var wg sync.WaitGroup
  121. for i := 0; i < 10; i++ {
  122. // Add them asynchronously to test the
  123. // concurrent guarantees of our APIs.
  124. wg.Add(1)
  125. go func(i int) {
  126. defer wg.Done()
  127. got, want := peerSet.Get(peer.ID()), peer
  128. assert.Equal(t, got, want, "#%d: got=%v want=%v", i, got, want)
  129. }(i)
  130. }
  131. wg.Wait()
  132. }