fetcher_test.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. // Copyright 2020 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. "math/big"
  19. "testing"
  20. "time"
  21. "github.com/ethereum/go-ethereum/consensus/ethash"
  22. "github.com/ethereum/go-ethereum/core"
  23. "github.com/ethereum/go-ethereum/core/rawdb"
  24. "github.com/ethereum/go-ethereum/core/types"
  25. "github.com/ethereum/go-ethereum/p2p/enode"
  26. )
  27. // verifyImportEvent verifies that one single event arrive on an import channel.
  28. func verifyImportEvent(t *testing.T, imported chan interface{}, arrive bool) {
  29. if arrive {
  30. select {
  31. case <-imported:
  32. case <-time.After(time.Second):
  33. t.Fatalf("import timeout")
  34. }
  35. } else {
  36. select {
  37. case <-imported:
  38. t.Fatalf("import invoked")
  39. case <-time.After(20 * time.Millisecond):
  40. }
  41. }
  42. }
  43. // verifyImportDone verifies that no more events are arriving on an import channel.
  44. func verifyImportDone(t *testing.T, imported chan interface{}) {
  45. select {
  46. case <-imported:
  47. t.Fatalf("extra block imported")
  48. case <-time.After(50 * time.Millisecond):
  49. }
  50. }
  51. // verifyChainHeight verifies the chain height is as expected.
  52. func verifyChainHeight(t *testing.T, fetcher *lightFetcher, height uint64) {
  53. local := fetcher.chain.CurrentHeader().Number.Uint64()
  54. if local != height {
  55. t.Fatalf("chain height mismatch, got %d, want %d", local, height)
  56. }
  57. }
  58. func TestSequentialAnnouncementsLes2(t *testing.T) { testSequentialAnnouncements(t, 2) }
  59. func TestSequentialAnnouncementsLes3(t *testing.T) { testSequentialAnnouncements(t, 3) }
  60. func testSequentialAnnouncements(t *testing.T, protocol int) {
  61. netconfig := testnetConfig{
  62. blocks: 4,
  63. protocol: protocol,
  64. nopruning: true,
  65. }
  66. s, c, teardown := newClientServerEnv(t, netconfig)
  67. defer teardown()
  68. // Create connected peer pair.
  69. c.handler.fetcher.noAnnounce = true // Ignore the first announce from peer which can trigger a resync.
  70. p1, _, err := newTestPeerPair("peer", protocol, s.handler, c.handler)
  71. if err != nil {
  72. t.Fatalf("Failed to create peer pair %v", err)
  73. }
  74. c.handler.fetcher.noAnnounce = false
  75. importCh := make(chan interface{})
  76. c.handler.fetcher.newHeadHook = func(header *types.Header) {
  77. importCh <- header
  78. }
  79. for i := uint64(1); i <= s.backend.Blockchain().CurrentHeader().Number.Uint64(); i++ {
  80. header := s.backend.Blockchain().GetHeaderByNumber(i)
  81. hash, number := header.Hash(), header.Number.Uint64()
  82. td := rawdb.ReadTd(s.db, hash, number)
  83. announce := announceData{hash, number, td, 0, nil}
  84. if p1.cpeer.announceType == announceTypeSigned {
  85. announce.sign(s.handler.server.privateKey)
  86. }
  87. p1.cpeer.sendAnnounce(announce)
  88. verifyImportEvent(t, importCh, true)
  89. }
  90. verifyImportDone(t, importCh)
  91. verifyChainHeight(t, c.handler.fetcher, 4)
  92. }
  93. func TestGappedAnnouncementsLes2(t *testing.T) { testGappedAnnouncements(t, 2) }
  94. func TestGappedAnnouncementsLes3(t *testing.T) { testGappedAnnouncements(t, 3) }
  95. func testGappedAnnouncements(t *testing.T, protocol int) {
  96. netconfig := testnetConfig{
  97. blocks: 4,
  98. protocol: protocol,
  99. nopruning: true,
  100. }
  101. s, c, teardown := newClientServerEnv(t, netconfig)
  102. defer teardown()
  103. // Create connected peer pair.
  104. c.handler.fetcher.noAnnounce = true // Ignore the first announce from peer which can trigger a resync.
  105. peer, _, err := newTestPeerPair("peer", protocol, s.handler, c.handler)
  106. if err != nil {
  107. t.Fatalf("Failed to create peer pair %v", err)
  108. }
  109. c.handler.fetcher.noAnnounce = false
  110. done := make(chan *types.Header, 1)
  111. c.handler.fetcher.newHeadHook = func(header *types.Header) { done <- header }
  112. // Prepare announcement by latest header.
  113. latest := s.backend.Blockchain().CurrentHeader()
  114. hash, number := latest.Hash(), latest.Number.Uint64()
  115. td := rawdb.ReadTd(s.db, hash, number)
  116. // Sign the announcement if necessary.
  117. announce := announceData{hash, number, td, 0, nil}
  118. if peer.cpeer.announceType == announceTypeSigned {
  119. announce.sign(s.handler.server.privateKey)
  120. }
  121. peer.cpeer.sendAnnounce(announce)
  122. <-done // Wait syncing
  123. verifyChainHeight(t, c.handler.fetcher, 4)
  124. // Send a reorged announcement
  125. var newAnno = make(chan struct{}, 1)
  126. c.handler.fetcher.noAnnounce = true
  127. c.handler.fetcher.newAnnounce = func(*serverPeer, *announceData) {
  128. newAnno <- struct{}{}
  129. }
  130. blocks, _ := core.GenerateChain(rawdb.ReadChainConfig(s.db, s.backend.Blockchain().Genesis().Hash()), s.backend.Blockchain().GetBlockByNumber(3),
  131. ethash.NewFaker(), s.db, 2, func(i int, gen *core.BlockGen) {
  132. gen.OffsetTime(-9) // higher block difficulty
  133. })
  134. s.backend.Blockchain().InsertChain(blocks)
  135. <-newAnno
  136. c.handler.fetcher.noAnnounce = false
  137. c.handler.fetcher.newAnnounce = nil
  138. latest = blocks[len(blocks)-1].Header()
  139. hash, number = latest.Hash(), latest.Number.Uint64()
  140. td = rawdb.ReadTd(s.db, hash, number)
  141. announce = announceData{hash, number, td, 1, nil}
  142. if peer.cpeer.announceType == announceTypeSigned {
  143. announce.sign(s.handler.server.privateKey)
  144. }
  145. peer.cpeer.sendAnnounce(announce)
  146. <-done // Wait syncing
  147. verifyChainHeight(t, c.handler.fetcher, 5)
  148. }
  149. func TestTrustedAnnouncementsLes2(t *testing.T) { testTrustedAnnouncement(t, 2) }
  150. func TestTrustedAnnouncementsLes3(t *testing.T) { testTrustedAnnouncement(t, 3) }
  151. func testTrustedAnnouncement(t *testing.T, protocol int) {
  152. var (
  153. servers []*testServer
  154. teardowns []func()
  155. nodes []*enode.Node
  156. ids []string
  157. cpeers []*clientPeer
  158. speers []*serverPeer
  159. )
  160. for i := 0; i < 10; i++ {
  161. s, n, teardown := newTestServerPeer(t, 10, protocol)
  162. servers = append(servers, s)
  163. nodes = append(nodes, n)
  164. teardowns = append(teardowns, teardown)
  165. // A half of them are trusted servers.
  166. if i < 5 {
  167. ids = append(ids, n.String())
  168. }
  169. }
  170. netconfig := testnetConfig{
  171. protocol: protocol,
  172. nopruning: true,
  173. ulcServers: ids,
  174. ulcFraction: 60,
  175. }
  176. _, c, teardown := newClientServerEnv(t, netconfig)
  177. defer teardown()
  178. defer func() {
  179. for i := 0; i < len(teardowns); i++ {
  180. teardowns[i]()
  181. }
  182. }()
  183. c.handler.fetcher.noAnnounce = true // Ignore the first announce from peer which can trigger a resync.
  184. // Connect all server instances.
  185. for i := 0; i < len(servers); i++ {
  186. sp, cp, err := connect(servers[i].handler, nodes[i].ID(), c.handler, protocol)
  187. if err != nil {
  188. t.Fatalf("connect server and client failed, err %s", err)
  189. }
  190. cpeers = append(cpeers, cp)
  191. speers = append(speers, sp)
  192. }
  193. c.handler.fetcher.noAnnounce = false
  194. newHead := make(chan *types.Header, 1)
  195. c.handler.fetcher.newHeadHook = func(header *types.Header) { newHead <- header }
  196. check := func(height []uint64, expected uint64, callback func()) {
  197. for i := 0; i < len(height); i++ {
  198. for j := 0; j < len(servers); j++ {
  199. h := servers[j].backend.Blockchain().GetHeaderByNumber(height[i])
  200. hash, number := h.Hash(), h.Number.Uint64()
  201. td := rawdb.ReadTd(servers[j].db, hash, number)
  202. // Sign the announcement if necessary.
  203. announce := announceData{hash, number, td, 0, nil}
  204. p := cpeers[j]
  205. if p.announceType == announceTypeSigned {
  206. announce.sign(servers[j].handler.server.privateKey)
  207. }
  208. p.sendAnnounce(announce)
  209. }
  210. }
  211. if callback != nil {
  212. callback()
  213. }
  214. verifyChainHeight(t, c.handler.fetcher, expected)
  215. }
  216. check([]uint64{1}, 1, func() { <-newHead }) // Sequential announcements
  217. check([]uint64{4}, 4, func() { <-newHead }) // ULC-style light syncing, rollback untrusted headers
  218. check([]uint64{10}, 10, func() { <-newHead }) // Sync the whole chain.
  219. }
  220. func TestInvalidAnnouncesLES2(t *testing.T) { testInvalidAnnounces(t, lpv2) }
  221. func TestInvalidAnnouncesLES3(t *testing.T) { testInvalidAnnounces(t, lpv3) }
  222. func TestInvalidAnnouncesLES4(t *testing.T) { testInvalidAnnounces(t, lpv4) }
  223. func testInvalidAnnounces(t *testing.T, protocol int) {
  224. netconfig := testnetConfig{
  225. blocks: 4,
  226. protocol: protocol,
  227. nopruning: true,
  228. }
  229. s, c, teardown := newClientServerEnv(t, netconfig)
  230. defer teardown()
  231. // Create connected peer pair.
  232. c.handler.fetcher.noAnnounce = true // Ignore the first announce from peer which can trigger a resync.
  233. peer, _, err := newTestPeerPair("peer", lpv3, s.handler, c.handler)
  234. if err != nil {
  235. t.Fatalf("Failed to create peer pair %v", err)
  236. }
  237. c.handler.fetcher.noAnnounce = false
  238. done := make(chan *types.Header, 1)
  239. c.handler.fetcher.newHeadHook = func(header *types.Header) { done <- header }
  240. // Prepare announcement by latest header.
  241. headerOne := s.backend.Blockchain().GetHeaderByNumber(1)
  242. hash, number := headerOne.Hash(), headerOne.Number.Uint64()
  243. td := big.NewInt(200) // bad td
  244. // Sign the announcement if necessary.
  245. announce := announceData{hash, number, td, 0, nil}
  246. if peer.cpeer.announceType == announceTypeSigned {
  247. announce.sign(s.handler.server.privateKey)
  248. }
  249. peer.cpeer.sendAnnounce(announce)
  250. <-done // Wait syncing
  251. // Ensure the bad peer is evicited
  252. if c.handler.backend.peers.len() != 0 {
  253. t.Fatalf("Failed to evict invalid peer")
  254. }
  255. }