statesync.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. // Copyright 2017 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 downloader
  17. import (
  18. "fmt"
  19. "hash"
  20. "sync"
  21. "time"
  22. "github.com/ethereum/go-ethereum/common"
  23. "github.com/ethereum/go-ethereum/core/rawdb"
  24. "github.com/ethereum/go-ethereum/core/state"
  25. "github.com/ethereum/go-ethereum/ethdb"
  26. "github.com/ethereum/go-ethereum/log"
  27. "github.com/ethereum/go-ethereum/trie"
  28. "golang.org/x/crypto/sha3"
  29. )
  30. // stateReq represents a batch of state fetch requests grouped together into
  31. // a single data retrieval network packet.
  32. type stateReq struct {
  33. items []common.Hash // Hashes of the state items to download
  34. tasks map[common.Hash]*stateTask // Download tasks to track previous attempts
  35. timeout time.Duration // Maximum round trip time for this to complete
  36. timer *time.Timer // Timer to fire when the RTT timeout expires
  37. peer *peerConnection // Peer that we're requesting from
  38. response [][]byte // Response data of the peer (nil for timeouts)
  39. dropped bool // Flag whether the peer dropped off early
  40. }
  41. // timedOut returns if this request timed out.
  42. func (req *stateReq) timedOut() bool {
  43. return req.response == nil
  44. }
  45. // stateSyncStats is a collection of progress stats to report during a state trie
  46. // sync to RPC requests as well as to display in user logs.
  47. type stateSyncStats struct {
  48. processed uint64 // Number of state entries processed
  49. duplicate uint64 // Number of state entries downloaded twice
  50. unexpected uint64 // Number of non-requested state entries received
  51. pending uint64 // Number of still pending state entries
  52. }
  53. // syncState starts downloading state with the given root hash.
  54. func (d *Downloader) syncState(root common.Hash) *stateSync {
  55. // Create the state sync
  56. s := newStateSync(d, root)
  57. select {
  58. case d.stateSyncStart <- s:
  59. // If we tell the statesync to restart with a new root, we also need
  60. // to wait for it to actually also start -- when old requests have timed
  61. // out or been delivered
  62. <-s.started
  63. case <-d.quitCh:
  64. s.err = errCancelStateFetch
  65. close(s.done)
  66. }
  67. return s
  68. }
  69. // stateFetcher manages the active state sync and accepts requests
  70. // on its behalf.
  71. func (d *Downloader) stateFetcher() {
  72. for {
  73. select {
  74. case s := <-d.stateSyncStart:
  75. for next := s; next != nil; {
  76. next = d.runStateSync(next)
  77. }
  78. case <-d.stateCh:
  79. // Ignore state responses while no sync is running.
  80. case <-d.quitCh:
  81. return
  82. }
  83. }
  84. }
  85. // runStateSync runs a state synchronisation until it completes or another root
  86. // hash is requested to be switched over to.
  87. func (d *Downloader) runStateSync(s *stateSync) *stateSync {
  88. var (
  89. active = make(map[string]*stateReq) // Currently in-flight requests
  90. finished []*stateReq // Completed or failed requests
  91. timeout = make(chan *stateReq) // Timed out active requests
  92. )
  93. // Run the state sync.
  94. log.Trace("State sync starting", "root", s.root)
  95. go s.run()
  96. defer s.Cancel()
  97. // Listen for peer departure events to cancel assigned tasks
  98. peerDrop := make(chan *peerConnection, 1024)
  99. peerSub := s.d.peers.SubscribePeerDrops(peerDrop)
  100. defer peerSub.Unsubscribe()
  101. for {
  102. // Enable sending of the first buffered element if there is one.
  103. var (
  104. deliverReq *stateReq
  105. deliverReqCh chan *stateReq
  106. )
  107. if len(finished) > 0 {
  108. deliverReq = finished[0]
  109. deliverReqCh = s.deliver
  110. }
  111. select {
  112. // The stateSync lifecycle:
  113. case next := <-d.stateSyncStart:
  114. d.spindownStateSync(active, finished, timeout, peerDrop)
  115. return next
  116. case <-s.done:
  117. d.spindownStateSync(active, finished, timeout, peerDrop)
  118. return nil
  119. // Send the next finished request to the current sync:
  120. case deliverReqCh <- deliverReq:
  121. // Shift out the first request, but also set the emptied slot to nil for GC
  122. copy(finished, finished[1:])
  123. finished[len(finished)-1] = nil
  124. finished = finished[:len(finished)-1]
  125. // Handle incoming state packs:
  126. case pack := <-d.stateCh:
  127. // Discard any data not requested (or previously timed out)
  128. req := active[pack.PeerId()]
  129. if req == nil {
  130. log.Debug("Unrequested node data", "peer", pack.PeerId(), "len", pack.Items())
  131. continue
  132. }
  133. // Finalize the request and queue up for processing
  134. req.timer.Stop()
  135. req.response = pack.(*statePack).states
  136. finished = append(finished, req)
  137. delete(active, pack.PeerId())
  138. // Handle dropped peer connections:
  139. case p := <-peerDrop:
  140. // Skip if no request is currently pending
  141. req := active[p.id]
  142. if req == nil {
  143. continue
  144. }
  145. // Finalize the request and queue up for processing
  146. req.timer.Stop()
  147. req.dropped = true
  148. finished = append(finished, req)
  149. delete(active, p.id)
  150. // Handle timed-out requests:
  151. case req := <-timeout:
  152. // If the peer is already requesting something else, ignore the stale timeout.
  153. // This can happen when the timeout and the delivery happens simultaneously,
  154. // causing both pathways to trigger.
  155. if active[req.peer.id] != req {
  156. continue
  157. }
  158. // Move the timed out data back into the download queue
  159. finished = append(finished, req)
  160. delete(active, req.peer.id)
  161. // Track outgoing state requests:
  162. case req := <-d.trackStateReq:
  163. // If an active request already exists for this peer, we have a problem. In
  164. // theory the trie node schedule must never assign two requests to the same
  165. // peer. In practice however, a peer might receive a request, disconnect and
  166. // immediately reconnect before the previous times out. In this case the first
  167. // request is never honored, alas we must not silently overwrite it, as that
  168. // causes valid requests to go missing and sync to get stuck.
  169. if old := active[req.peer.id]; old != nil {
  170. log.Warn("Busy peer assigned new state fetch", "peer", old.peer.id)
  171. // Move the previous request to the finished set
  172. old.timer.Stop()
  173. old.dropped = true
  174. finished = append(finished, old)
  175. }
  176. // Start a timer to notify the sync loop if the peer stalled.
  177. req.timer = time.AfterFunc(req.timeout, func() {
  178. select {
  179. case timeout <- req:
  180. case <-s.done:
  181. // Prevent leaking of timer goroutines in the unlikely case where a
  182. // timer is fired just before exiting runStateSync.
  183. }
  184. })
  185. active[req.peer.id] = req
  186. }
  187. }
  188. }
  189. // spindownStateSync 'drains' the outstanding requests; some will be delivered and other
  190. // will time out. This is to ensure that when the next stateSync starts working, all peers
  191. // are marked as idle and de facto _are_ idle.
  192. func (d *Downloader) spindownStateSync(active map[string]*stateReq, finished []*stateReq, timeout chan *stateReq, peerDrop chan *peerConnection) {
  193. log.Trace("State sync spinning down", "active", len(active), "finished", len(finished))
  194. for len(active) > 0 {
  195. var (
  196. req *stateReq
  197. reason string
  198. )
  199. select {
  200. // Handle (drop) incoming state packs:
  201. case pack := <-d.stateCh:
  202. req = active[pack.PeerId()]
  203. reason = "delivered"
  204. // Handle dropped peer connections:
  205. case p := <-peerDrop:
  206. req = active[p.id]
  207. reason = "peerdrop"
  208. // Handle timed-out requests:
  209. case req = <-timeout:
  210. reason = "timeout"
  211. }
  212. if req == nil {
  213. continue
  214. }
  215. req.peer.log.Trace("State peer marked idle (spindown)", "req.items", len(req.items), "reason", reason)
  216. req.timer.Stop()
  217. delete(active, req.peer.id)
  218. req.peer.SetNodeDataIdle(len(req.items))
  219. }
  220. // The 'finished' set contains deliveries that we were going to pass to processing.
  221. // Those are now moot, but we still need to set those peers as idle, which would
  222. // otherwise have been done after processing
  223. for _, req := range finished {
  224. req.peer.SetNodeDataIdle(len(req.items))
  225. }
  226. }
  227. // stateSync schedules requests for downloading a particular state trie defined
  228. // by a given state root.
  229. type stateSync struct {
  230. d *Downloader // Downloader instance to access and manage current peerset
  231. sched *trie.Sync // State trie sync scheduler defining the tasks
  232. keccak hash.Hash // Keccak256 hasher to verify deliveries with
  233. tasks map[common.Hash]*stateTask // Set of tasks currently queued for retrieval
  234. numUncommitted int
  235. bytesUncommitted int
  236. started chan struct{} // Started is signalled once the sync loop starts
  237. deliver chan *stateReq // Delivery channel multiplexing peer responses
  238. cancel chan struct{} // Channel to signal a termination request
  239. cancelOnce sync.Once // Ensures cancel only ever gets called once
  240. done chan struct{} // Channel to signal termination completion
  241. err error // Any error hit during sync (set before completion)
  242. root common.Hash
  243. }
  244. // stateTask represents a single trie node download task, containing a set of
  245. // peers already attempted retrieval from to detect stalled syncs and abort.
  246. type stateTask struct {
  247. attempts map[string]struct{}
  248. }
  249. // newStateSync creates a new state trie download scheduler. This method does not
  250. // yet start the sync. The user needs to call run to initiate.
  251. func newStateSync(d *Downloader, root common.Hash) *stateSync {
  252. return &stateSync{
  253. d: d,
  254. sched: state.NewStateSync(root, d.stateDB, d.stateBloom),
  255. keccak: sha3.NewLegacyKeccak256(),
  256. tasks: make(map[common.Hash]*stateTask),
  257. deliver: make(chan *stateReq),
  258. cancel: make(chan struct{}),
  259. done: make(chan struct{}),
  260. started: make(chan struct{}),
  261. root: root,
  262. }
  263. }
  264. // run starts the task assignment and response processing loop, blocking until
  265. // it finishes, and finally notifying any goroutines waiting for the loop to
  266. // finish.
  267. func (s *stateSync) run() {
  268. s.err = s.loop()
  269. close(s.done)
  270. }
  271. // Wait blocks until the sync is done or canceled.
  272. func (s *stateSync) Wait() error {
  273. <-s.done
  274. return s.err
  275. }
  276. // Cancel cancels the sync and waits until it has shut down.
  277. func (s *stateSync) Cancel() error {
  278. s.cancelOnce.Do(func() { close(s.cancel) })
  279. return s.Wait()
  280. }
  281. // loop is the main event loop of a state trie sync. It it responsible for the
  282. // assignment of new tasks to peers (including sending it to them) as well as
  283. // for the processing of inbound data. Note, that the loop does not directly
  284. // receive data from peers, rather those are buffered up in the downloader and
  285. // pushed here async. The reason is to decouple processing from data receipt
  286. // and timeouts.
  287. func (s *stateSync) loop() (err error) {
  288. close(s.started)
  289. // Listen for new peer events to assign tasks to them
  290. newPeer := make(chan *peerConnection, 1024)
  291. peerSub := s.d.peers.SubscribeNewPeers(newPeer)
  292. defer peerSub.Unsubscribe()
  293. defer func() {
  294. cerr := s.commit(true)
  295. if err == nil {
  296. err = cerr
  297. }
  298. }()
  299. // Keep assigning new tasks until the sync completes or aborts
  300. for s.sched.Pending() > 0 {
  301. if err = s.commit(false); err != nil {
  302. return err
  303. }
  304. s.assignTasks()
  305. // Tasks assigned, wait for something to happen
  306. select {
  307. case <-newPeer:
  308. // New peer arrived, try to assign it download tasks
  309. case <-s.cancel:
  310. return errCancelStateFetch
  311. case <-s.d.cancelCh:
  312. return errCanceled
  313. case req := <-s.deliver:
  314. // Response, disconnect or timeout triggered, drop the peer if stalling
  315. log.Trace("Received node data response", "peer", req.peer.id, "count", len(req.response), "dropped", req.dropped, "timeout", !req.dropped && req.timedOut())
  316. if len(req.items) <= 2 && !req.dropped && req.timedOut() {
  317. // 2 items are the minimum requested, if even that times out, we've no use of
  318. // this peer at the moment.
  319. log.Warn("Stalling state sync, dropping peer", "peer", req.peer.id)
  320. if s.d.dropPeer == nil {
  321. // The dropPeer method is nil when `--copydb` is used for a local copy.
  322. // Timeouts can occur if e.g. compaction hits at the wrong time, and can be ignored
  323. req.peer.log.Warn("Downloader wants to drop peer, but peerdrop-function is not set", "peer", req.peer.id)
  324. } else {
  325. s.d.dropPeer(req.peer.id)
  326. // If this peer was the master peer, abort sync immediately
  327. s.d.cancelLock.RLock()
  328. master := req.peer.id == s.d.cancelPeer
  329. s.d.cancelLock.RUnlock()
  330. if master {
  331. s.d.cancel()
  332. return errTimeout
  333. }
  334. }
  335. }
  336. // Process all the received blobs and check for stale delivery
  337. delivered, err := s.process(req)
  338. req.peer.SetNodeDataIdle(delivered)
  339. if err != nil {
  340. log.Warn("Node data write error", "err", err)
  341. return err
  342. }
  343. }
  344. }
  345. return nil
  346. }
  347. func (s *stateSync) commit(force bool) error {
  348. if !force && s.bytesUncommitted < ethdb.IdealBatchSize {
  349. return nil
  350. }
  351. start := time.Now()
  352. b := s.d.stateDB.NewBatch()
  353. if err := s.sched.Commit(b); err != nil {
  354. return err
  355. }
  356. if err := b.Write(); err != nil {
  357. return fmt.Errorf("DB write error: %v", err)
  358. }
  359. s.updateStats(s.numUncommitted, 0, 0, time.Since(start))
  360. s.numUncommitted = 0
  361. s.bytesUncommitted = 0
  362. return nil
  363. }
  364. // assignTasks attempts to assign new tasks to all idle peers, either from the
  365. // batch currently being retried, or fetching new data from the trie sync itself.
  366. func (s *stateSync) assignTasks() {
  367. // Iterate over all idle peers and try to assign them state fetches
  368. peers, _ := s.d.peers.NodeDataIdlePeers()
  369. for _, p := range peers {
  370. // Assign a batch of fetches proportional to the estimated latency/bandwidth
  371. cap := p.NodeDataCapacity(s.d.requestRTT())
  372. req := &stateReq{peer: p, timeout: s.d.requestTTL()}
  373. s.fillTasks(cap, req)
  374. // If the peer was assigned tasks to fetch, send the network request
  375. if len(req.items) > 0 {
  376. req.peer.log.Trace("Requesting new batch of data", "type", "state", "count", len(req.items), "root", s.root)
  377. select {
  378. case s.d.trackStateReq <- req:
  379. req.peer.FetchNodeData(req.items)
  380. case <-s.cancel:
  381. case <-s.d.cancelCh:
  382. }
  383. }
  384. }
  385. }
  386. // fillTasks fills the given request object with a maximum of n state download
  387. // tasks to send to the remote peer.
  388. func (s *stateSync) fillTasks(n int, req *stateReq) {
  389. // Refill available tasks from the scheduler.
  390. if len(s.tasks) < n {
  391. new := s.sched.Missing(n - len(s.tasks))
  392. for _, hash := range new {
  393. s.tasks[hash] = &stateTask{make(map[string]struct{})}
  394. }
  395. }
  396. // Find tasks that haven't been tried with the request's peer.
  397. req.items = make([]common.Hash, 0, n)
  398. req.tasks = make(map[common.Hash]*stateTask, n)
  399. for hash, t := range s.tasks {
  400. // Stop when we've gathered enough requests
  401. if len(req.items) == n {
  402. break
  403. }
  404. // Skip any requests we've already tried from this peer
  405. if _, ok := t.attempts[req.peer.id]; ok {
  406. continue
  407. }
  408. // Assign the request to this peer
  409. t.attempts[req.peer.id] = struct{}{}
  410. req.items = append(req.items, hash)
  411. req.tasks[hash] = t
  412. delete(s.tasks, hash)
  413. }
  414. }
  415. // process iterates over a batch of delivered state data, injecting each item
  416. // into a running state sync, re-queuing any items that were requested but not
  417. // delivered. Returns whether the peer actually managed to deliver anything of
  418. // value, and any error that occurred.
  419. func (s *stateSync) process(req *stateReq) (int, error) {
  420. // Collect processing stats and update progress if valid data was received
  421. duplicate, unexpected, successful := 0, 0, 0
  422. defer func(start time.Time) {
  423. if duplicate > 0 || unexpected > 0 {
  424. s.updateStats(0, duplicate, unexpected, time.Since(start))
  425. }
  426. }(time.Now())
  427. // Iterate over all the delivered data and inject one-by-one into the trie
  428. for _, blob := range req.response {
  429. _, hash, err := s.processNodeData(blob)
  430. switch err {
  431. case nil:
  432. s.numUncommitted++
  433. s.bytesUncommitted += len(blob)
  434. successful++
  435. case trie.ErrNotRequested:
  436. unexpected++
  437. case trie.ErrAlreadyProcessed:
  438. duplicate++
  439. default:
  440. return successful, fmt.Errorf("invalid state node %s: %v", hash.TerminalString(), err)
  441. }
  442. delete(req.tasks, hash)
  443. }
  444. // Put unfulfilled tasks back into the retry queue
  445. npeers := s.d.peers.Len()
  446. for hash, task := range req.tasks {
  447. // If the node did deliver something, missing items may be due to a protocol
  448. // limit or a previous timeout + delayed delivery. Both cases should permit
  449. // the node to retry the missing items (to avoid single-peer stalls).
  450. if len(req.response) > 0 || req.timedOut() {
  451. delete(task.attempts, req.peer.id)
  452. }
  453. // If we've requested the node too many times already, it may be a malicious
  454. // sync where nobody has the right data. Abort.
  455. if len(task.attempts) >= npeers {
  456. return successful, fmt.Errorf("state node %s failed with all peers (%d tries, %d peers)", hash.TerminalString(), len(task.attempts), npeers)
  457. }
  458. // Missing item, place into the retry queue.
  459. s.tasks[hash] = task
  460. }
  461. return successful, nil
  462. }
  463. // processNodeData tries to inject a trie node data blob delivered from a remote
  464. // peer into the state trie, returning whether anything useful was written or any
  465. // error occurred.
  466. func (s *stateSync) processNodeData(blob []byte) (bool, common.Hash, error) {
  467. res := trie.SyncResult{Data: blob}
  468. s.keccak.Reset()
  469. s.keccak.Write(blob)
  470. s.keccak.Sum(res.Hash[:0])
  471. committed, _, err := s.sched.Process([]trie.SyncResult{res})
  472. return committed, res.Hash, err
  473. }
  474. // updateStats bumps the various state sync progress counters and displays a log
  475. // message for the user to see.
  476. func (s *stateSync) updateStats(written, duplicate, unexpected int, duration time.Duration) {
  477. s.d.syncStatsLock.Lock()
  478. defer s.d.syncStatsLock.Unlock()
  479. s.d.syncStatsState.pending = uint64(s.sched.Pending())
  480. s.d.syncStatsState.processed += uint64(written)
  481. s.d.syncStatsState.duplicate += uint64(duplicate)
  482. s.d.syncStatsState.unexpected += uint64(unexpected)
  483. if written > 0 || duplicate > 0 || unexpected > 0 {
  484. log.Info("Imported new state entries", "count", written, "elapsed", common.PrettyDuration(duration), "processed", s.d.syncStatsState.processed, "pending", s.d.syncStatsState.pending, "retry", len(s.tasks), "duplicate", s.d.syncStatsState.duplicate, "unexpected", s.d.syncStatsState.unexpected)
  485. }
  486. if written > 0 {
  487. rawdb.WriteFastTrieProgress(s.d.stateDB, s.d.syncStatsState.processed)
  488. }
  489. }