peer_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Copyright 2019 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package les
  17. import (
  18. "crypto/rand"
  19. "errors"
  20. "math/big"
  21. "reflect"
  22. "sort"
  23. "testing"
  24. "time"
  25. "github.com/ethereum/go-ethereum/common"
  26. "github.com/ethereum/go-ethereum/p2p"
  27. "github.com/ethereum/go-ethereum/p2p/enode"
  28. )
  29. type testServerPeerSub struct {
  30. regCh chan *serverPeer
  31. unregCh chan *serverPeer
  32. }
  33. func newTestServerPeerSub() *testServerPeerSub {
  34. return &testServerPeerSub{
  35. regCh: make(chan *serverPeer, 1),
  36. unregCh: make(chan *serverPeer, 1),
  37. }
  38. }
  39. func (t *testServerPeerSub) registerPeer(p *serverPeer) { t.regCh <- p }
  40. func (t *testServerPeerSub) unregisterPeer(p *serverPeer) { t.unregCh <- p }
  41. func TestPeerSubscription(t *testing.T) {
  42. peers := newServerPeerSet()
  43. defer peers.close()
  44. checkIds := func(expect []string) {
  45. given := peers.ids()
  46. if len(given) == 0 && len(expect) == 0 {
  47. return
  48. }
  49. sort.Strings(given)
  50. sort.Strings(expect)
  51. if !reflect.DeepEqual(given, expect) {
  52. t.Fatalf("all peer ids mismatch, want %v, given %v", expect, given)
  53. }
  54. }
  55. checkPeers := func(peerCh chan *serverPeer) {
  56. select {
  57. case <-peerCh:
  58. case <-time.NewTimer(100 * time.Millisecond).C:
  59. t.Fatalf("timeout, no event received")
  60. }
  61. select {
  62. case <-peerCh:
  63. t.Fatalf("unexpected event received")
  64. case <-time.NewTimer(10 * time.Millisecond).C:
  65. }
  66. }
  67. checkIds([]string{})
  68. sub := newTestServerPeerSub()
  69. peers.subscribe(sub)
  70. // Generate a random id and create the peer
  71. var id enode.ID
  72. rand.Read(id[:])
  73. peer := newServerPeer(2, NetworkId, false, p2p.NewPeer(id, "name", nil), nil)
  74. peers.register(peer)
  75. checkIds([]string{peer.id})
  76. checkPeers(sub.regCh)
  77. peers.unregister(peer.id)
  78. checkIds([]string{})
  79. checkPeers(sub.unregCh)
  80. }
  81. func TestHandshake(t *testing.T) {
  82. // Create a message pipe to communicate through
  83. app, net := p2p.MsgPipe()
  84. // Generate a random id and create the peer
  85. var id enode.ID
  86. rand.Read(id[:])
  87. peer1 := newClientPeer(2, NetworkId, p2p.NewPeer(id, "name", nil), net)
  88. peer2 := newServerPeer(2, NetworkId, true, p2p.NewPeer(id, "name", nil), app)
  89. var (
  90. errCh1 = make(chan error, 1)
  91. errCh2 = make(chan error, 1)
  92. td = big.NewInt(100)
  93. head = common.HexToHash("deadbeef")
  94. headNum = uint64(10)
  95. genesis = common.HexToHash("cafebabe")
  96. )
  97. go func() {
  98. errCh1 <- peer1.handshake(td, head, headNum, genesis, func(list *keyValueList) {
  99. var announceType uint64 = announceTypeSigned
  100. *list = (*list).add("announceType", announceType)
  101. }, nil)
  102. }()
  103. go func() {
  104. errCh2 <- peer2.handshake(td, head, headNum, genesis, nil, func(recv keyValueMap) error {
  105. var reqType uint64
  106. err := recv.get("announceType", &reqType)
  107. if err != nil {
  108. return err
  109. }
  110. if reqType != announceTypeSigned {
  111. return errors.New("Expected announceTypeSigned")
  112. }
  113. return nil
  114. })
  115. }()
  116. for i := 0; i < 2; i++ {
  117. select {
  118. case err := <-errCh1:
  119. if err != nil {
  120. t.Fatalf("handshake failed, %v", err)
  121. }
  122. case err := <-errCh2:
  123. if err != nil {
  124. t.Fatalf("handshake failed, %v", err)
  125. }
  126. case <-time.After(time.Second):
  127. t.Fatalf("timeout")
  128. }
  129. }
  130. }