ulc_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // Copyright 2018 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. "fmt"
  20. "net"
  21. "sync/atomic"
  22. "testing"
  23. "time"
  24. "github.com/ethereum/go-ethereum/crypto"
  25. "github.com/ethereum/go-ethereum/p2p"
  26. "github.com/ethereum/go-ethereum/p2p/enode"
  27. )
  28. func TestULCAnnounceThresholdLes2(t *testing.T) { testULCAnnounceThreshold(t, 2) }
  29. func TestULCAnnounceThresholdLes3(t *testing.T) { testULCAnnounceThreshold(t, 3) }
  30. func testULCAnnounceThreshold(t *testing.T, protocol int) {
  31. // todo figure out why it takes fetcher so longer to fetcher the announced header.
  32. t.Skip("Sometimes it can failed")
  33. // newTestLightPeer creates node with light sync mode
  34. newTestLightPeer := func(t *testing.T, protocol int, ulcServers []string, ulcFraction int) (*testClient, func()) {
  35. netconfig := testnetConfig{
  36. protocol: protocol,
  37. ulcServers: ulcServers,
  38. ulcFraction: ulcFraction,
  39. nopruning: true,
  40. }
  41. _, c, teardown := newClientServerEnv(t, netconfig)
  42. return c, teardown
  43. }
  44. var cases = []struct {
  45. height []int
  46. threshold int
  47. expect uint64
  48. }{
  49. {[]int{1}, 100, 1},
  50. {[]int{0, 0, 0}, 100, 0},
  51. {[]int{1, 2, 3}, 30, 3},
  52. {[]int{1, 2, 3}, 60, 2},
  53. {[]int{3, 2, 1}, 67, 1},
  54. {[]int{3, 2, 1}, 100, 1},
  55. }
  56. for _, testcase := range cases {
  57. var (
  58. servers []*testServer
  59. teardowns []func()
  60. nodes []*enode.Node
  61. ids []string
  62. )
  63. for i := 0; i < len(testcase.height); i++ {
  64. s, n, teardown := newTestServerPeer(t, 0, protocol, nil)
  65. servers = append(servers, s)
  66. nodes = append(nodes, n)
  67. teardowns = append(teardowns, teardown)
  68. ids = append(ids, n.String())
  69. }
  70. c, teardown := newTestLightPeer(t, protocol, ids, testcase.threshold)
  71. // Connect all servers.
  72. for i := 0; i < len(servers); i++ {
  73. connect(servers[i].handler, nodes[i].ID(), c.handler, protocol, false)
  74. }
  75. for i := 0; i < len(servers); i++ {
  76. for j := 0; j < testcase.height[i]; j++ {
  77. servers[i].backend.Commit()
  78. }
  79. }
  80. time.Sleep(1500 * time.Millisecond) // Ensure the fetcher has done its work.
  81. head := c.handler.backend.blockchain.CurrentHeader().Number.Uint64()
  82. if head != testcase.expect {
  83. t.Fatalf("chain height mismatch, want %d, got %d", testcase.expect, head)
  84. }
  85. // Release all servers and client resources.
  86. teardown()
  87. for i := 0; i < len(teardowns); i++ {
  88. teardowns[i]()
  89. }
  90. }
  91. }
  92. func connect(server *serverHandler, serverId enode.ID, client *clientHandler, protocol int, noInitAnnounce bool) (*serverPeer, *clientPeer, error) {
  93. // Create a message pipe to communicate through
  94. app, net := p2p.MsgPipe()
  95. var id enode.ID
  96. rand.Read(id[:])
  97. peer1 := newServerPeer(protocol, NetworkId, true, p2p.NewPeer(serverId, "", nil), net) // Mark server as trusted
  98. peer2 := newClientPeer(protocol, NetworkId, p2p.NewPeer(id, "", nil), app)
  99. // Start the peerLight on a new thread
  100. errc1 := make(chan error, 1)
  101. errc2 := make(chan error, 1)
  102. go func() {
  103. select {
  104. case <-server.closeCh:
  105. errc1 <- p2p.DiscQuitting
  106. case errc1 <- server.handle(peer2):
  107. }
  108. }()
  109. go func() {
  110. select {
  111. case <-client.closeCh:
  112. errc1 <- p2p.DiscQuitting
  113. case errc1 <- client.handle(peer1, noInitAnnounce):
  114. }
  115. }()
  116. // Ensure the connection is established or exits when any error occurs
  117. for {
  118. select {
  119. case err := <-errc1:
  120. return nil, nil, fmt.Errorf("failed to establish protocol connection %v", err)
  121. case err := <-errc2:
  122. return nil, nil, fmt.Errorf("failed to establish protocol connection %v", err)
  123. default:
  124. }
  125. if atomic.LoadUint32(&peer1.serving) == 1 && atomic.LoadUint32(&peer2.serving) == 1 {
  126. break
  127. }
  128. time.Sleep(50 * time.Millisecond)
  129. }
  130. return peer1, peer2, nil
  131. }
  132. // newTestServerPeer creates server peer.
  133. func newTestServerPeer(t *testing.T, blocks int, protocol int, indexFn indexerCallback) (*testServer, *enode.Node, func()) {
  134. netconfig := testnetConfig{
  135. blocks: blocks,
  136. protocol: protocol,
  137. indexFn: indexFn,
  138. nopruning: true,
  139. }
  140. s, _, teardown := newClientServerEnv(t, netconfig)
  141. key, err := crypto.GenerateKey()
  142. if err != nil {
  143. t.Fatal("generate key err:", err)
  144. }
  145. s.handler.server.privateKey = key
  146. n := enode.NewV4(&key.PublicKey, net.ParseIP("127.0.0.1"), 35000, 35000)
  147. return s, n, teardown
  148. }