github.com/codysnider/go-ethereum@v1.10.18-0.20220420071915-14f4ae99222a/eth/downloader/statesync.go (about) 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 17 package downloader 18 19 import ( 20 "sync" 21 22 "github.com/ethereum/go-ethereum/common" 23 "github.com/ethereum/go-ethereum/log" 24 ) 25 26 // syncState starts downloading state with the given root hash. 27 func (d *Downloader) syncState(root common.Hash) *stateSync { 28 // Create the state sync 29 s := newStateSync(d, root) 30 select { 31 case d.stateSyncStart <- s: 32 // If we tell the statesync to restart with a new root, we also need 33 // to wait for it to actually also start -- when old requests have timed 34 // out or been delivered 35 <-s.started 36 case <-d.quitCh: 37 s.err = errCancelStateFetch 38 close(s.done) 39 } 40 return s 41 } 42 43 // stateFetcher manages the active state sync and accepts requests 44 // on its behalf. 45 func (d *Downloader) stateFetcher() { 46 for { 47 select { 48 case s := <-d.stateSyncStart: 49 for next := s; next != nil; { 50 next = d.runStateSync(next) 51 } 52 case <-d.quitCh: 53 return 54 } 55 } 56 } 57 58 // runStateSync runs a state synchronisation until it completes or another root 59 // hash is requested to be switched over to. 60 func (d *Downloader) runStateSync(s *stateSync) *stateSync { 61 log.Trace("State sync starting", "root", s.root) 62 63 go s.run() 64 defer s.Cancel() 65 66 for { 67 select { 68 case next := <-d.stateSyncStart: 69 return next 70 71 case <-s.done: 72 return nil 73 } 74 } 75 } 76 77 // stateSync schedules requests for downloading a particular state trie defined 78 // by a given state root. 79 type stateSync struct { 80 d *Downloader // Downloader instance to access and manage current peerset 81 root common.Hash // State root currently being synced 82 83 started chan struct{} // Started is signalled once the sync loop starts 84 cancel chan struct{} // Channel to signal a termination request 85 cancelOnce sync.Once // Ensures cancel only ever gets called once 86 done chan struct{} // Channel to signal termination completion 87 err error // Any error hit during sync (set before completion) 88 } 89 90 // newStateSync creates a new state trie download scheduler. This method does not 91 // yet start the sync. The user needs to call run to initiate. 92 func newStateSync(d *Downloader, root common.Hash) *stateSync { 93 return &stateSync{ 94 d: d, 95 root: root, 96 cancel: make(chan struct{}), 97 done: make(chan struct{}), 98 started: make(chan struct{}), 99 } 100 } 101 102 // run starts the task assignment and response processing loop, blocking until 103 // it finishes, and finally notifying any goroutines waiting for the loop to 104 // finish. 105 func (s *stateSync) run() { 106 close(s.started) 107 s.err = s.d.SnapSyncer.Sync(s.root, s.cancel) 108 close(s.done) 109 } 110 111 // Wait blocks until the sync is done or canceled. 112 func (s *stateSync) Wait() error { 113 <-s.done 114 return s.err 115 } 116 117 // Cancel cancels the sync and waits until it has shut down. 118 func (s *stateSync) Cancel() error { 119 s.cancelOnce.Do(func() { 120 close(s.cancel) 121 }) 122 return s.Wait() 123 }