peer_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. "math/big"
  20. "reflect"
  21. "sort"
  22. "testing"
  23. "time"
  24. "github.com/ethereum/go-ethereum/common"
  25. "github.com/ethereum/go-ethereum/p2p"
  26. "github.com/ethereum/go-ethereum/p2p/enode"
  27. )
  28. type testServerPeerSub struct {
  29. regCh chan *serverPeer
  30. unregCh chan *serverPeer
  31. }
  32. func newTestServerPeerSub() *testServerPeerSub {
  33. return &testServerPeerSub{
  34. regCh: make(chan *serverPeer, 1),
  35. unregCh: make(chan *serverPeer, 1),
  36. }
  37. }
  38. func (t *testServerPeerSub) registerPeer(p *serverPeer) { t.regCh <- p }
  39. func (t *testServerPeerSub) unregisterPeer(p *serverPeer) { t.unregCh <- p }
  40. func TestPeerSubscription(t *testing.T) {
  41. peers := newServerPeerSet()
  42. defer peers.close()
  43. checkIds := func(expect []string) {
  44. given := peers.ids()
  45. if len(given) == 0 && len(expect) == 0 {
  46. return
  47. }
  48. sort.Strings(given)
  49. sort.Strings(expect)
  50. if !reflect.DeepEqual(given, expect) {
  51. t.Fatalf("all peer ids mismatch, want %v, given %v", expect, given)
  52. }
  53. }
  54. checkPeers := func(peerCh chan *serverPeer) {
  55. select {
  56. case <-peerCh:
  57. case <-time.NewTimer(100 * time.Millisecond).C:
  58. t.Fatalf("timeout, no event received")
  59. }
  60. select {
  61. case <-peerCh:
  62. t.Fatalf("unexpected event received")
  63. case <-time.NewTimer(10 * time.Millisecond).C:
  64. }
  65. }
  66. checkIds([]string{})
  67. sub := newTestServerPeerSub()
  68. peers.subscribe(sub)
  69. // Generate a random id and create the peer
  70. var id enode.ID
  71. rand.Read(id[:])
  72. peer := newServerPeer(2, NetworkId, false, p2p.NewPeer(id, "name", nil), nil)
  73. peers.register(peer)
  74. checkIds([]string{peer.id})
  75. checkPeers(sub.regCh)
  76. peers.unregister(peer.id)
  77. checkIds([]string{})
  78. checkPeers(sub.unregCh)
  79. }
  80. func TestHandshake(t *testing.T) {
  81. // Create a message pipe to communicate through
  82. app, net := p2p.MsgPipe()
  83. // Generate a random id and create the peer
  84. var id enode.ID
  85. rand.Read(id[:])
  86. peer1 := newClientPeer(2, NetworkId, p2p.NewPeer(id, "name", nil), net)
  87. peer2 := newServerPeer(2, NetworkId, true, p2p.NewPeer(id, "name", nil), app)
  88. var (
  89. errCh1 = make(chan error, 1)
  90. errCh2 = make(chan error, 1)
  91. td = big.NewInt(100)
  92. head = common.HexToHash("deadbeef")
  93. headNum = uint64(10)
  94. genesis = common.HexToHash("cafebabe")
  95. )
  96. go func() {
  97. errCh1 <- peer1.handshake(td, head, headNum, genesis, func(list *keyValueList) {
  98. var announceType uint64 = announceTypeSigned
  99. *list = (*list).add("announceType", announceType)
  100. }, nil)
  101. }()
  102. go func() {
  103. errCh2 <- peer2.handshake(td, head, headNum, genesis, nil, func(recv keyValueMap) error {
  104. var reqType uint64
  105. err := recv.get("announceType", &reqType)
  106. if err != nil {
  107. t.Fatal(err)
  108. }
  109. if reqType != announceTypeSigned {
  110. t.Fatal("Expected announceTypeSigned")
  111. }
  112. return nil
  113. })
  114. }()
  115. for i := 0; i < 2; i++ {
  116. select {
  117. case err := <-errCh1:
  118. if err != nil {
  119. t.Fatalf("handshake failed, %v", err)
  120. }
  121. case err := <-errCh2:
  122. if err != nil {
  123. t.Fatalf("handshake failed, %v", err)
  124. }
  125. case <-time.NewTimer(100 * time.Millisecond).C:
  126. t.Fatalf("timeout")
  127. }
  128. }
  129. }