ulc_test.go 4.5 KB

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