peer_test.go 4.6 KB

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