ulc_test.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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/ecdsa"
  19. "fmt"
  20. "math/big"
  21. "net"
  22. "reflect"
  23. "testing"
  24. "time"
  25. "github.com/ethereum/go-ethereum/common/mclock"
  26. "github.com/ethereum/go-ethereum/core/rawdb"
  27. "github.com/ethereum/go-ethereum/crypto"
  28. "github.com/ethereum/go-ethereum/light"
  29. "github.com/ethereum/go-ethereum/p2p"
  30. "github.com/ethereum/go-ethereum/p2p/enode"
  31. )
  32. func TestULCSyncWithOnePeer(t *testing.T) {
  33. f := newFullPeerPair(t, 1, 4)
  34. l := newLightPeer(t, []string{f.Node.String()}, 100)
  35. if reflect.DeepEqual(f.PM.blockchain.CurrentHeader().Hash(), l.PM.blockchain.CurrentHeader().Hash()) {
  36. t.Fatal("blocks are equal")
  37. }
  38. _, _, err := connectPeers(f, l, 2)
  39. if err != nil {
  40. t.Fatal(err)
  41. }
  42. l.PM.fetcher.lock.Lock()
  43. l.PM.fetcher.nextRequest()
  44. l.PM.fetcher.lock.Unlock()
  45. if !reflect.DeepEqual(f.PM.blockchain.CurrentHeader().Hash(), l.PM.blockchain.CurrentHeader().Hash()) {
  46. t.Fatal("sync doesn't work")
  47. }
  48. }
  49. func TestULCReceiveAnnounce(t *testing.T) {
  50. f := newFullPeerPair(t, 1, 4)
  51. l := newLightPeer(t, []string{f.Node.String()}, 100)
  52. fPeer, lPeer, err := connectPeers(f, l, 2)
  53. if err != nil {
  54. t.Fatal(err)
  55. }
  56. l.PM.synchronise(fPeer)
  57. //check that the sync is finished correctly
  58. if !reflect.DeepEqual(f.PM.blockchain.CurrentHeader().Hash(), l.PM.blockchain.CurrentHeader().Hash()) {
  59. t.Fatal("sync doesn't work")
  60. }
  61. l.PM.peers.lock.Lock()
  62. if len(l.PM.peers.peers) == 0 {
  63. t.Fatal("peer list should not be empty")
  64. }
  65. l.PM.peers.lock.Unlock()
  66. time.Sleep(time.Second)
  67. //send a signed announce message(payload doesn't matter)
  68. td := f.PM.blockchain.GetTd(l.PM.blockchain.CurrentHeader().Hash(), l.PM.blockchain.CurrentHeader().Number.Uint64())
  69. announce := announceData{
  70. Number: l.PM.blockchain.CurrentHeader().Number.Uint64() + 1,
  71. Td: td.Add(td, big.NewInt(1)),
  72. }
  73. announce.sign(f.Key)
  74. lPeer.SendAnnounce(announce)
  75. }
  76. func TestULCShouldNotSyncWithTwoPeersOneHaveEmptyChain(t *testing.T) {
  77. f1 := newFullPeerPair(t, 1, 4)
  78. f2 := newFullPeerPair(t, 2, 0)
  79. l := newLightPeer(t, []string{f1.Node.String(), f2.Node.String()}, 100)
  80. _, _, err := connectPeers(f1, l, 2)
  81. if err != nil {
  82. t.Fatal(err)
  83. }
  84. _, _, err = connectPeers(f2, l, 2)
  85. if err != nil {
  86. t.Fatal(err)
  87. }
  88. l.PM.fetcher.lock.Lock()
  89. l.PM.fetcher.nextRequest()
  90. l.PM.fetcher.lock.Unlock()
  91. if reflect.DeepEqual(f2.PM.blockchain.CurrentHeader().Hash(), l.PM.blockchain.CurrentHeader().Hash()) {
  92. t.Fatal("Incorrect hash: second peer has empty chain")
  93. }
  94. }
  95. func TestULCShouldNotSyncWithThreePeersOneHaveEmptyChain(t *testing.T) {
  96. f1 := newFullPeerPair(t, 1, 3)
  97. f2 := newFullPeerPair(t, 2, 4)
  98. f3 := newFullPeerPair(t, 3, 0)
  99. l := newLightPeer(t, []string{f1.Node.String(), f2.Node.String(), f3.Node.String()}, 60)
  100. _, _, err := connectPeers(f1, l, 2)
  101. if err != nil {
  102. t.Fatal(err)
  103. }
  104. _, _, err = connectPeers(f2, l, 2)
  105. if err != nil {
  106. t.Fatal(err)
  107. }
  108. _, _, err = connectPeers(f3, l, 2)
  109. if err != nil {
  110. t.Fatal(err)
  111. }
  112. l.PM.fetcher.lock.Lock()
  113. l.PM.fetcher.nextRequest()
  114. l.PM.fetcher.lock.Unlock()
  115. if !reflect.DeepEqual(f1.PM.blockchain.CurrentHeader().Hash(), l.PM.blockchain.CurrentHeader().Hash()) {
  116. t.Fatal("Incorrect hash")
  117. }
  118. }
  119. type pairPeer struct {
  120. Name string
  121. Node *enode.Node
  122. PM *ProtocolManager
  123. Key *ecdsa.PrivateKey
  124. }
  125. func connectPeers(full, light pairPeer, version int) (*peer, *peer, error) {
  126. // Create a message pipe to communicate through
  127. app, net := p2p.MsgPipe()
  128. peerLight := full.PM.newPeer(version, NetworkId, p2p.NewPeer(light.Node.ID(), light.Name, nil), net)
  129. peerFull := light.PM.newPeer(version, NetworkId, p2p.NewPeer(full.Node.ID(), full.Name, nil), app)
  130. // Start the peerLight on a new thread
  131. errc1 := make(chan error, 1)
  132. errc2 := make(chan error, 1)
  133. go func() {
  134. select {
  135. case light.PM.newPeerCh <- peerFull:
  136. errc1 <- light.PM.handle(peerFull)
  137. case <-light.PM.quitSync:
  138. errc1 <- p2p.DiscQuitting
  139. }
  140. }()
  141. go func() {
  142. select {
  143. case full.PM.newPeerCh <- peerLight:
  144. errc2 <- full.PM.handle(peerLight)
  145. case <-full.PM.quitSync:
  146. errc2 <- p2p.DiscQuitting
  147. }
  148. }()
  149. select {
  150. case <-time.After(time.Millisecond * 100):
  151. case err := <-errc1:
  152. return nil, nil, fmt.Errorf("peerLight handshake error: %v", err)
  153. case err := <-errc2:
  154. return nil, nil, fmt.Errorf("peerFull handshake error: %v", err)
  155. }
  156. return peerFull, peerLight, nil
  157. }
  158. // newFullPeerPair creates node with full sync mode
  159. func newFullPeerPair(t *testing.T, index int, numberOfblocks int) pairPeer {
  160. db := rawdb.NewMemoryDatabase()
  161. pmFull, _ := newTestProtocolManagerMust(t, false, numberOfblocks, nil, nil, nil, db, nil, 0)
  162. peerPairFull := pairPeer{
  163. Name: "full node",
  164. PM: pmFull,
  165. }
  166. key, err := crypto.GenerateKey()
  167. if err != nil {
  168. t.Fatal("generate key err:", err)
  169. }
  170. peerPairFull.Key = key
  171. peerPairFull.Node = enode.NewV4(&key.PublicKey, net.ParseIP("127.0.0.1"), 35000, 35000)
  172. return peerPairFull
  173. }
  174. // newLightPeer creates node with light sync mode
  175. func newLightPeer(t *testing.T, ulcServers []string, ulcFraction int) pairPeer {
  176. peers := newPeerSet()
  177. dist := newRequestDistributor(peers, make(chan struct{}), &mclock.System{})
  178. rm := newRetrieveManager(peers, dist, nil)
  179. ldb := rawdb.NewMemoryDatabase()
  180. odr := NewLesOdr(ldb, light.DefaultClientIndexerConfig, rm)
  181. pmLight, _ := newTestProtocolManagerMust(t, true, 0, odr, nil, peers, ldb, ulcServers, ulcFraction)
  182. peerPairLight := pairPeer{
  183. Name: "ulc node",
  184. PM: pmLight,
  185. }
  186. key, err := crypto.GenerateKey()
  187. if err != nil {
  188. t.Fatal("generate key err:", err)
  189. }
  190. peerPairLight.Key = key
  191. peerPairLight.Node = enode.NewV4(&key.PublicKey, net.IP{}, 35000, 35000)
  192. return peerPairLight
  193. }