downloader.go 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613
  1. // Copyright 2015 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 contains the manual full chain synchronisation.
  17. package downloader
  18. import (
  19. "errors"
  20. "math"
  21. "math/big"
  22. "strings"
  23. "sync"
  24. "sync/atomic"
  25. "time"
  26. "github.com/ethereum/go-ethereum/common"
  27. "github.com/ethereum/go-ethereum/core/types"
  28. "github.com/ethereum/go-ethereum/ethdb"
  29. "github.com/ethereum/go-ethereum/event"
  30. "github.com/ethereum/go-ethereum/logger"
  31. "github.com/ethereum/go-ethereum/logger/glog"
  32. "github.com/rcrowley/go-metrics"
  33. )
  34. var (
  35. MaxHashFetch = 512 // Amount of hashes to be fetched per retrieval request
  36. MaxBlockFetch = 128 // Amount of blocks to be fetched per retrieval request
  37. MaxHeaderFetch = 192 // Amount of block headers to be fetched per retrieval request
  38. MaxBodyFetch = 128 // Amount of block bodies to be fetched per retrieval request
  39. MaxReceiptFetch = 256 // Amount of transaction receipts to allow fetching per request
  40. MaxStateFetch = 384 // Amount of node state values to allow fetching per request
  41. hashTTL = 5 * time.Second // [eth/61] Time it takes for a hash request to time out
  42. blockSoftTTL = 3 * time.Second // [eth/61] Request completion threshold for increasing or decreasing a peer's bandwidth
  43. blockHardTTL = 3 * blockSoftTTL // [eth/61] Maximum time allowance before a block request is considered expired
  44. headerTTL = 5 * time.Second // [eth/62] Time it takes for a header request to time out
  45. bodySoftTTL = 3 * time.Second // [eth/62] Request completion threshold for increasing or decreasing a peer's bandwidth
  46. bodyHardTTL = 3 * bodySoftTTL // [eth/62] Maximum time allowance before a block body request is considered expired
  47. receiptSoftTTL = 3 * time.Second // [eth/63] Request completion threshold for increasing or decreasing a peer's bandwidth
  48. receiptHardTTL = 3 * receiptSoftTTL // [eth/63] Maximum time allowance before a receipt request is considered expired
  49. stateSoftTTL = 2 * time.Second // [eth/63] Request completion threshold for increasing or decreasing a peer's bandwidth
  50. stateHardTTL = 3 * stateSoftTTL // [eth/63] Maximum time allowance before a node data request is considered expired
  51. maxQueuedHashes = 256 * 1024 // [eth/61] Maximum number of hashes to queue for import (DOS protection)
  52. maxQueuedHeaders = 256 * 1024 // [eth/62] Maximum number of headers to queue for import (DOS protection)
  53. maxQueuedStates = 256 * 1024 // [eth/63] Maximum number of state requests to queue (DOS protection)
  54. maxResultsProcess = 256 // Number of download results to import at once into the chain
  55. headerCheckFrequency = 64 // Verification frequency of the downloaded headers during fast sync
  56. minCheckedHeaders = 1024 // Number of headers to verify fully when approaching the chain head
  57. minFullBlocks = 1024 // Number of blocks to retrieve fully even in fast sync
  58. )
  59. var (
  60. errBusy = errors.New("busy")
  61. errUnknownPeer = errors.New("peer is unknown or unhealthy")
  62. errBadPeer = errors.New("action from bad peer ignored")
  63. errStallingPeer = errors.New("peer is stalling")
  64. errNoPeers = errors.New("no peers to keep download active")
  65. errPendingQueue = errors.New("pending items in queue")
  66. errTimeout = errors.New("timeout")
  67. errEmptyHashSet = errors.New("empty hash set by peer")
  68. errEmptyHeaderSet = errors.New("empty header set by peer")
  69. errPeersUnavailable = errors.New("no peers available or all tried for download")
  70. errAlreadyInPool = errors.New("hash already in pool")
  71. errInvalidChain = errors.New("retrieved hash chain is invalid")
  72. errInvalidBlock = errors.New("retrieved block is invalid")
  73. errInvalidBody = errors.New("retrieved block body is invalid")
  74. errInvalidReceipt = errors.New("retrieved receipt is invalid")
  75. errCancelHashFetch = errors.New("hash download canceled (requested)")
  76. errCancelBlockFetch = errors.New("block download canceled (requested)")
  77. errCancelHeaderFetch = errors.New("block header download canceled (requested)")
  78. errCancelBodyFetch = errors.New("block body download canceled (requested)")
  79. errCancelReceiptFetch = errors.New("receipt download canceled (requested)")
  80. errNoSyncActive = errors.New("no sync active")
  81. )
  82. type Downloader struct {
  83. mode SyncMode // Synchronisation mode defining the strategies used
  84. mux *event.TypeMux // Event multiplexer to announce sync operation events
  85. queue *queue // Scheduler for selecting the hashes to download
  86. peers *peerSet // Set of active peers from which download can proceed
  87. interrupt int32 // Atomic boolean to signal termination
  88. // Statistics
  89. syncStatsChainOrigin uint64 // Origin block number where syncing started at
  90. syncStatsChainHeight uint64 // Highest block number known when syncing started
  91. syncStatsStateTotal uint64 // Total number of node state entries known so far
  92. syncStatsStateDone uint64 // Number of state trie entries already pulled
  93. syncStatsLock sync.RWMutex // Lock protecting the sync stats fields
  94. // Callbacks
  95. hasHeader headerCheckFn // Checks if a header is present in the chain
  96. hasBlock blockCheckFn // Checks if a block is present in the chain
  97. getHeader headerRetrievalFn // Retrieves a header from the chain
  98. getBlock blockRetrievalFn // Retrieves a block from the chain
  99. headHeader headHeaderRetrievalFn // Retrieves the head header from the chain
  100. headBlock headBlockRetrievalFn // Retrieves the head block from the chain
  101. headFastBlock headFastBlockRetrievalFn // Retrieves the head fast-sync block from the chain
  102. commitHeadBlock headBlockCommitterFn // Commits a manually assembled block as the chain head
  103. getTd tdRetrievalFn // Retrieves the TD of a block from the chain
  104. insertHeaders headerChainInsertFn // Injects a batch of headers into the chain
  105. insertBlocks blockChainInsertFn // Injects a batch of blocks into the chain
  106. insertReceipts receiptChainInsertFn // Injects a batch of blocks and their receipts into the chain
  107. dropPeer peerDropFn // Drops a peer for misbehaving
  108. // Status
  109. synchroniseMock func(id string, hash common.Hash) error // Replacement for synchronise during testing
  110. synchronising int32
  111. processing int32
  112. notified int32
  113. // Channels
  114. newPeerCh chan *peer
  115. hashCh chan dataPack // [eth/61] Channel receiving inbound hashes
  116. blockCh chan dataPack // [eth/61] Channel receiving inbound blocks
  117. headerCh chan dataPack // [eth/62] Channel receiving inbound block headers
  118. bodyCh chan dataPack // [eth/62] Channel receiving inbound block bodies
  119. receiptCh chan dataPack // [eth/63] Channel receiving inbound receipts
  120. stateCh chan dataPack // [eth/63] Channel receiving inbound node state data
  121. blockWakeCh chan bool // [eth/61] Channel to signal the block fetcher of new tasks
  122. bodyWakeCh chan bool // [eth/62] Channel to signal the block body fetcher of new tasks
  123. receiptWakeCh chan bool // [eth/63] Channel to signal the receipt fetcher of new tasks
  124. stateWakeCh chan bool // [eth/63] Channel to signal the state fetcher of new tasks
  125. cancelCh chan struct{} // Channel to cancel mid-flight syncs
  126. cancelLock sync.RWMutex // Lock to protect the cancel channel in delivers
  127. // Testing hooks
  128. syncInitHook func(uint64, uint64) // Method to call upon initiating a new sync run
  129. bodyFetchHook func([]*types.Header) // Method to call upon starting a block body fetch
  130. receiptFetchHook func([]*types.Header) // Method to call upon starting a receipt fetch
  131. chainInsertHook func([]*fetchResult) // Method to call upon inserting a chain of blocks (possibly in multiple invocations)
  132. }
  133. // New creates a new downloader to fetch hashes and blocks from remote peers.
  134. func New(mode SyncMode, stateDb ethdb.Database, mux *event.TypeMux, hasHeader headerCheckFn, hasBlock blockCheckFn, getHeader headerRetrievalFn,
  135. getBlock blockRetrievalFn, headHeader headHeaderRetrievalFn, headBlock headBlockRetrievalFn, headFastBlock headFastBlockRetrievalFn,
  136. commitHeadBlock headBlockCommitterFn, getTd tdRetrievalFn, insertHeaders headerChainInsertFn, insertBlocks blockChainInsertFn,
  137. insertReceipts receiptChainInsertFn, dropPeer peerDropFn) *Downloader {
  138. return &Downloader{
  139. mode: mode,
  140. mux: mux,
  141. queue: newQueue(stateDb),
  142. peers: newPeerSet(),
  143. hasHeader: hasHeader,
  144. hasBlock: hasBlock,
  145. getHeader: getHeader,
  146. getBlock: getBlock,
  147. headHeader: headHeader,
  148. headBlock: headBlock,
  149. headFastBlock: headFastBlock,
  150. commitHeadBlock: commitHeadBlock,
  151. getTd: getTd,
  152. insertHeaders: insertHeaders,
  153. insertBlocks: insertBlocks,
  154. insertReceipts: insertReceipts,
  155. dropPeer: dropPeer,
  156. newPeerCh: make(chan *peer, 1),
  157. hashCh: make(chan dataPack, 1),
  158. blockCh: make(chan dataPack, 1),
  159. headerCh: make(chan dataPack, 1),
  160. bodyCh: make(chan dataPack, 1),
  161. receiptCh: make(chan dataPack, 1),
  162. stateCh: make(chan dataPack, 1),
  163. blockWakeCh: make(chan bool, 1),
  164. bodyWakeCh: make(chan bool, 1),
  165. receiptWakeCh: make(chan bool, 1),
  166. stateWakeCh: make(chan bool, 1),
  167. }
  168. }
  169. // Boundaries retrieves the synchronisation boundaries, specifically the origin
  170. // block where synchronisation started at (may have failed/suspended) and the
  171. // latest known block which the synchonisation targets.
  172. func (d *Downloader) Boundaries() (uint64, uint64) {
  173. d.syncStatsLock.RLock()
  174. defer d.syncStatsLock.RUnlock()
  175. return d.syncStatsChainOrigin, d.syncStatsChainHeight
  176. }
  177. // Synchronising returns whether the downloader is currently retrieving blocks.
  178. func (d *Downloader) Synchronising() bool {
  179. return atomic.LoadInt32(&d.synchronising) > 0
  180. }
  181. // RegisterPeer injects a new download peer into the set of block source to be
  182. // used for fetching hashes and blocks from.
  183. func (d *Downloader) RegisterPeer(id string, version int, head common.Hash,
  184. getRelHashes relativeHashFetcherFn, getAbsHashes absoluteHashFetcherFn, getBlocks blockFetcherFn, // eth/61 callbacks, remove when upgrading
  185. getRelHeaders relativeHeaderFetcherFn, getAbsHeaders absoluteHeaderFetcherFn, getBlockBodies blockBodyFetcherFn,
  186. getReceipts receiptFetcherFn, getNodeData stateFetcherFn) error {
  187. glog.V(logger.Detail).Infoln("Registering peer", id)
  188. if err := d.peers.Register(newPeer(id, version, head, getRelHashes, getAbsHashes, getBlocks, getRelHeaders, getAbsHeaders, getBlockBodies, getReceipts, getNodeData)); err != nil {
  189. glog.V(logger.Error).Infoln("Register failed:", err)
  190. return err
  191. }
  192. return nil
  193. }
  194. // UnregisterPeer remove a peer from the known list, preventing any action from
  195. // the specified peer. An effort is also made to return any pending fetches into
  196. // the queue.
  197. func (d *Downloader) UnregisterPeer(id string) error {
  198. glog.V(logger.Detail).Infoln("Unregistering peer", id)
  199. if err := d.peers.Unregister(id); err != nil {
  200. glog.V(logger.Error).Infoln("Unregister failed:", err)
  201. return err
  202. }
  203. d.queue.Revoke(id)
  204. return nil
  205. }
  206. // Synchronise tries to sync up our local block chain with a remote peer, both
  207. // adding various sanity checks as well as wrapping it with various log entries.
  208. func (d *Downloader) Synchronise(id string, head common.Hash, td *big.Int) {
  209. glog.V(logger.Detail).Infof("Attempting synchronisation: %v, head [%x…], TD %v", id, head[:4], td)
  210. switch err := d.synchronise(id, head, td); err {
  211. case nil:
  212. glog.V(logger.Detail).Infof("Synchronisation completed")
  213. case errBusy:
  214. glog.V(logger.Detail).Infof("Synchronisation already in progress")
  215. case errTimeout, errBadPeer, errStallingPeer, errEmptyHashSet, errEmptyHeaderSet, errPeersUnavailable, errInvalidChain:
  216. glog.V(logger.Debug).Infof("Removing peer %v: %v", id, err)
  217. d.dropPeer(id)
  218. case errPendingQueue:
  219. glog.V(logger.Debug).Infoln("Synchronisation aborted:", err)
  220. default:
  221. glog.V(logger.Warn).Infof("Synchronisation failed: %v", err)
  222. }
  223. }
  224. // synchronise will select the peer and use it for synchronising. If an empty string is given
  225. // it will use the best peer possible and synchronize if it's TD is higher than our own. If any of the
  226. // checks fail an error will be returned. This method is synchronous
  227. func (d *Downloader) synchronise(id string, hash common.Hash, td *big.Int) error {
  228. // Mock out the synchonisation if testing
  229. if d.synchroniseMock != nil {
  230. return d.synchroniseMock(id, hash)
  231. }
  232. // Make sure only one goroutine is ever allowed past this point at once
  233. if !atomic.CompareAndSwapInt32(&d.synchronising, 0, 1) {
  234. return errBusy
  235. }
  236. defer atomic.StoreInt32(&d.synchronising, 0)
  237. // Post a user notification of the sync (only once per session)
  238. if atomic.CompareAndSwapInt32(&d.notified, 0, 1) {
  239. glog.V(logger.Info).Infoln("Block synchronisation started")
  240. }
  241. // Abort if the queue still contains some leftover data
  242. if d.queue.GetHeadResult() != nil {
  243. return errPendingQueue
  244. }
  245. // Reset the queue, peer set and wake channels to clean any internal leftover state
  246. d.queue.Reset()
  247. d.peers.Reset()
  248. for _, ch := range []chan bool{d.blockWakeCh, d.bodyWakeCh, d.receiptWakeCh, d.stateWakeCh} {
  249. select {
  250. case <-ch:
  251. default:
  252. }
  253. }
  254. // Reset and ephemeral sync statistics
  255. d.syncStatsLock.Lock()
  256. d.syncStatsStateTotal = 0
  257. d.syncStatsStateDone = 0
  258. d.syncStatsLock.Unlock()
  259. // Create cancel channel for aborting mid-flight
  260. d.cancelLock.Lock()
  261. d.cancelCh = make(chan struct{})
  262. d.cancelLock.Unlock()
  263. // Retrieve the origin peer and initiate the downloading process
  264. p := d.peers.Peer(id)
  265. if p == nil {
  266. return errUnknownPeer
  267. }
  268. return d.syncWithPeer(p, hash, td)
  269. }
  270. /*
  271. // Has checks if the downloader knows about a particular hash, meaning that its
  272. // either already downloaded of pending retrieval.
  273. func (d *Downloader) Has(hash common.Hash) bool {
  274. return d.queue.Has(hash)
  275. }
  276. */
  277. // syncWithPeer starts a block synchronization based on the hash chain from the
  278. // specified peer and head hash.
  279. func (d *Downloader) syncWithPeer(p *peer, hash common.Hash, td *big.Int) (err error) {
  280. d.mux.Post(StartEvent{})
  281. defer func() {
  282. // reset on error
  283. if err != nil {
  284. d.cancel()
  285. d.mux.Post(FailedEvent{err})
  286. } else {
  287. d.mux.Post(DoneEvent{})
  288. }
  289. }()
  290. glog.V(logger.Debug).Infof("Synchronising with the network using: %s [eth/%d]", p.id, p.version)
  291. defer func(start time.Time) {
  292. glog.V(logger.Debug).Infof("Synchronisation terminated after %v", time.Since(start))
  293. }(time.Now())
  294. switch {
  295. case p.version == 61:
  296. // Look up the sync boundaries: the common ancestor and the target block
  297. latest, err := d.fetchHeight61(p)
  298. if err != nil {
  299. return err
  300. }
  301. origin, err := d.findAncestor61(p)
  302. if err != nil {
  303. return err
  304. }
  305. d.syncStatsLock.Lock()
  306. if d.syncStatsChainHeight <= origin || d.syncStatsChainOrigin > origin {
  307. d.syncStatsChainOrigin = origin
  308. }
  309. d.syncStatsChainHeight = latest
  310. d.syncStatsLock.Unlock()
  311. // Initiate the sync using a concurrent hash and block retrieval algorithm
  312. if d.syncInitHook != nil {
  313. d.syncInitHook(origin, latest)
  314. }
  315. d.queue.Prepare(origin+1, d.mode, 0)
  316. errc := make(chan error, 2)
  317. go func() { errc <- d.fetchHashes61(p, td, origin+1) }()
  318. go func() { errc <- d.fetchBlocks61(origin + 1) }()
  319. // If any fetcher fails, cancel the other
  320. if err := <-errc; err != nil {
  321. d.cancel()
  322. <-errc
  323. return err
  324. }
  325. return <-errc
  326. case p.version >= 62:
  327. // Look up the sync boundaries: the common ancestor and the target block
  328. latest, err := d.fetchHeight(p)
  329. if err != nil {
  330. return err
  331. }
  332. origin, err := d.findAncestor(p)
  333. if err != nil {
  334. return err
  335. }
  336. d.syncStatsLock.Lock()
  337. if d.syncStatsChainHeight <= origin || d.syncStatsChainOrigin > origin {
  338. d.syncStatsChainOrigin = origin
  339. }
  340. d.syncStatsChainHeight = latest
  341. d.syncStatsLock.Unlock()
  342. // Initiate the sync using a concurrent header and content retrieval algorithm
  343. pivot := uint64(0)
  344. if latest > uint64(minFullBlocks) {
  345. pivot = latest - uint64(minFullBlocks)
  346. }
  347. d.queue.Prepare(origin+1, d.mode, pivot)
  348. if d.syncInitHook != nil {
  349. d.syncInitHook(origin, latest)
  350. }
  351. errc := make(chan error, 4)
  352. go func() { errc <- d.fetchHeaders(p, td, origin+1) }() // Headers are always retrieved
  353. go func() { errc <- d.fetchBodies(origin + 1) }() // Bodies are retrieved during normal and fast sync
  354. go func() { errc <- d.fetchReceipts(origin + 1) }() // Receipts are retrieved during fast sync
  355. go func() { errc <- d.fetchNodeData() }() // Node state data is retrieved during fast sync
  356. // If any fetcher fails, cancel the others
  357. var fail error
  358. for i := 0; i < cap(errc); i++ {
  359. if err := <-errc; err != nil {
  360. if fail == nil {
  361. fail = err
  362. d.cancel()
  363. }
  364. }
  365. }
  366. return fail
  367. default:
  368. // Something very wrong, stop right here
  369. glog.V(logger.Error).Infof("Unsupported eth protocol: %d", p.version)
  370. return errBadPeer
  371. }
  372. return nil
  373. }
  374. // cancel cancels all of the operations and resets the queue. It returns true
  375. // if the cancel operation was completed.
  376. func (d *Downloader) cancel() {
  377. // Close the current cancel channel
  378. d.cancelLock.Lock()
  379. if d.cancelCh != nil {
  380. select {
  381. case <-d.cancelCh:
  382. // Channel was already closed
  383. default:
  384. close(d.cancelCh)
  385. }
  386. }
  387. d.cancelLock.Unlock()
  388. // Reset the queue
  389. d.queue.Reset()
  390. }
  391. // Terminate interrupts the downloader, canceling all pending operations.
  392. func (d *Downloader) Terminate() {
  393. atomic.StoreInt32(&d.interrupt, 1)
  394. d.cancel()
  395. }
  396. // fetchHeight61 retrieves the head block of the remote peer to aid in estimating
  397. // the total time a pending synchronisation would take.
  398. func (d *Downloader) fetchHeight61(p *peer) (uint64, error) {
  399. glog.V(logger.Debug).Infof("%v: retrieving remote chain height", p)
  400. // Request the advertised remote head block and wait for the response
  401. go p.getBlocks([]common.Hash{p.head})
  402. timeout := time.After(blockSoftTTL)
  403. for {
  404. select {
  405. case <-d.cancelCh:
  406. return 0, errCancelBlockFetch
  407. case <-d.headerCh:
  408. // Out of bounds eth/62 block headers received, ignore them
  409. case <-d.bodyCh:
  410. // Out of bounds eth/62 block bodies received, ignore them
  411. case <-d.hashCh:
  412. // Out of bounds hashes received, ignore them
  413. case packet := <-d.blockCh:
  414. // Discard anything not from the origin peer
  415. if packet.PeerId() != p.id {
  416. glog.V(logger.Debug).Infof("Received blocks from incorrect peer(%s)", packet.PeerId())
  417. break
  418. }
  419. // Make sure the peer actually gave something valid
  420. blocks := packet.(*blockPack).blocks
  421. if len(blocks) != 1 {
  422. glog.V(logger.Debug).Infof("%v: invalid number of head blocks: %d != 1", p, len(blocks))
  423. return 0, errBadPeer
  424. }
  425. return blocks[0].NumberU64(), nil
  426. case <-timeout:
  427. glog.V(logger.Debug).Infof("%v: head block timeout", p)
  428. return 0, errTimeout
  429. }
  430. }
  431. }
  432. // findAncestor61 tries to locate the common ancestor block of the local chain and
  433. // a remote peers blockchain. In the general case when our node was in sync and
  434. // on the correct chain, checking the top N blocks should already get us a match.
  435. // In the rare scenario when we ended up on a long reorganization (i.e. none of
  436. // the head blocks match), we do a binary search to find the common ancestor.
  437. func (d *Downloader) findAncestor61(p *peer) (uint64, error) {
  438. glog.V(logger.Debug).Infof("%v: looking for common ancestor", p)
  439. // Request out head blocks to short circuit ancestor location
  440. head := d.headBlock().NumberU64()
  441. from := int64(head) - int64(MaxHashFetch) + 1
  442. if from < 0 {
  443. from = 0
  444. }
  445. go p.getAbsHashes(uint64(from), MaxHashFetch)
  446. // Wait for the remote response to the head fetch
  447. number, hash := uint64(0), common.Hash{}
  448. timeout := time.After(hashTTL)
  449. for finished := false; !finished; {
  450. select {
  451. case <-d.cancelCh:
  452. return 0, errCancelHashFetch
  453. case packet := <-d.hashCh:
  454. // Discard anything not from the origin peer
  455. if packet.PeerId() != p.id {
  456. glog.V(logger.Debug).Infof("Received hashes from incorrect peer(%s)", packet.PeerId())
  457. break
  458. }
  459. // Make sure the peer actually gave something valid
  460. hashes := packet.(*hashPack).hashes
  461. if len(hashes) == 0 {
  462. glog.V(logger.Debug).Infof("%v: empty head hash set", p)
  463. return 0, errEmptyHashSet
  464. }
  465. // Check if a common ancestor was found
  466. finished = true
  467. for i := len(hashes) - 1; i >= 0; i-- {
  468. if d.hasBlock(hashes[i]) {
  469. number, hash = uint64(from)+uint64(i), hashes[i]
  470. break
  471. }
  472. }
  473. case <-d.blockCh:
  474. // Out of bounds blocks received, ignore them
  475. case <-d.headerCh:
  476. // Out of bounds eth/62 block headers received, ignore them
  477. case <-d.bodyCh:
  478. // Out of bounds eth/62 block bodies received, ignore them
  479. case <-timeout:
  480. glog.V(logger.Debug).Infof("%v: head hash timeout", p)
  481. return 0, errTimeout
  482. }
  483. }
  484. // If the head fetch already found an ancestor, return
  485. if !common.EmptyHash(hash) {
  486. glog.V(logger.Debug).Infof("%v: common ancestor: #%d [%x…]", p, number, hash[:4])
  487. return number, nil
  488. }
  489. // Ancestor not found, we need to binary search over our chain
  490. start, end := uint64(0), head
  491. for start+1 < end {
  492. // Split our chain interval in two, and request the hash to cross check
  493. check := (start + end) / 2
  494. timeout := time.After(hashTTL)
  495. go p.getAbsHashes(uint64(check), 1)
  496. // Wait until a reply arrives to this request
  497. for arrived := false; !arrived; {
  498. select {
  499. case <-d.cancelCh:
  500. return 0, errCancelHashFetch
  501. case packet := <-d.hashCh:
  502. // Discard anything not from the origin peer
  503. if packet.PeerId() != p.id {
  504. glog.V(logger.Debug).Infof("Received hashes from incorrect peer(%s)", packet.PeerId())
  505. break
  506. }
  507. // Make sure the peer actually gave something valid
  508. hashes := packet.(*hashPack).hashes
  509. if len(hashes) != 1 {
  510. glog.V(logger.Debug).Infof("%v: invalid search hash set (%d)", p, len(hashes))
  511. return 0, errBadPeer
  512. }
  513. arrived = true
  514. // Modify the search interval based on the response
  515. block := d.getBlock(hashes[0])
  516. if block == nil {
  517. end = check
  518. break
  519. }
  520. if block.NumberU64() != check {
  521. glog.V(logger.Debug).Infof("%v: non requested hash #%d [%x…], instead of #%d", p, block.NumberU64(), block.Hash().Bytes()[:4], check)
  522. return 0, errBadPeer
  523. }
  524. start = check
  525. case <-d.blockCh:
  526. // Out of bounds blocks received, ignore them
  527. case <-d.headerCh:
  528. // Out of bounds eth/62 block headers received, ignore them
  529. case <-d.bodyCh:
  530. // Out of bounds eth/62 block bodies received, ignore them
  531. case <-timeout:
  532. glog.V(logger.Debug).Infof("%v: search hash timeout", p)
  533. return 0, errTimeout
  534. }
  535. }
  536. }
  537. return start, nil
  538. }
  539. // fetchHashes61 keeps retrieving hashes from the requested number, until no more
  540. // are returned, potentially throttling on the way.
  541. func (d *Downloader) fetchHashes61(p *peer, td *big.Int, from uint64) error {
  542. glog.V(logger.Debug).Infof("%v: downloading hashes from #%d", p, from)
  543. // Create a timeout timer, and the associated hash fetcher
  544. request := time.Now() // time of the last fetch request
  545. timeout := time.NewTimer(0) // timer to dump a non-responsive active peer
  546. <-timeout.C // timeout channel should be initially empty
  547. defer timeout.Stop()
  548. getHashes := func(from uint64) {
  549. glog.V(logger.Detail).Infof("%v: fetching %d hashes from #%d", p, MaxHashFetch, from)
  550. go p.getAbsHashes(from, MaxHashFetch)
  551. request = time.Now()
  552. timeout.Reset(hashTTL)
  553. }
  554. // Start pulling hashes, until all are exhausted
  555. getHashes(from)
  556. gotHashes := false
  557. for {
  558. select {
  559. case <-d.cancelCh:
  560. return errCancelHashFetch
  561. case <-d.headerCh:
  562. // Out of bounds eth/62 block headers received, ignore them
  563. case <-d.bodyCh:
  564. // Out of bounds eth/62 block bodies received, ignore them
  565. case packet := <-d.hashCh:
  566. // Make sure the active peer is giving us the hashes
  567. if packet.PeerId() != p.id {
  568. glog.V(logger.Debug).Infof("Received hashes from incorrect peer(%s)", packet.PeerId())
  569. break
  570. }
  571. hashReqTimer.UpdateSince(request)
  572. timeout.Stop()
  573. // If no more hashes are inbound, notify the block fetcher and return
  574. if packet.Items() == 0 {
  575. glog.V(logger.Debug).Infof("%v: no available hashes", p)
  576. select {
  577. case d.blockWakeCh <- false:
  578. case <-d.cancelCh:
  579. }
  580. // If no hashes were retrieved at all, the peer violated it's TD promise that it had a
  581. // better chain compared to ours. The only exception is if it's promised blocks were
  582. // already imported by other means (e.g. fecher):
  583. //
  584. // R <remote peer>, L <local node>: Both at block 10
  585. // R: Mine block 11, and propagate it to L
  586. // L: Queue block 11 for import
  587. // L: Notice that R's head and TD increased compared to ours, start sync
  588. // L: Import of block 11 finishes
  589. // L: Sync begins, and finds common ancestor at 11
  590. // L: Request new hashes up from 11 (R's TD was higher, it must have something)
  591. // R: Nothing to give
  592. if !gotHashes && td.Cmp(d.getTd(d.headBlock().Hash())) > 0 {
  593. return errStallingPeer
  594. }
  595. return nil
  596. }
  597. gotHashes = true
  598. hashes := packet.(*hashPack).hashes
  599. // Otherwise insert all the new hashes, aborting in case of junk
  600. glog.V(logger.Detail).Infof("%v: scheduling %d hashes from #%d", p, len(hashes), from)
  601. inserts := d.queue.Schedule61(hashes, true)
  602. if len(inserts) != len(hashes) {
  603. glog.V(logger.Debug).Infof("%v: stale hashes", p)
  604. return errBadPeer
  605. }
  606. // Notify the block fetcher of new hashes, but stop if queue is full
  607. if d.queue.PendingBlocks() < maxQueuedHashes {
  608. // We still have hashes to fetch, send continuation wake signal (potential)
  609. select {
  610. case d.blockWakeCh <- true:
  611. default:
  612. }
  613. } else {
  614. // Hash limit reached, send a termination wake signal (enforced)
  615. select {
  616. case d.blockWakeCh <- false:
  617. case <-d.cancelCh:
  618. }
  619. return nil
  620. }
  621. // Queue not yet full, fetch the next batch
  622. from += uint64(len(hashes))
  623. getHashes(from)
  624. case <-timeout.C:
  625. glog.V(logger.Debug).Infof("%v: hash request timed out", p)
  626. hashTimeoutMeter.Mark(1)
  627. return errTimeout
  628. }
  629. }
  630. }
  631. // fetchBlocks61 iteratively downloads the scheduled hashes, taking any available
  632. // peers, reserving a chunk of blocks for each, waiting for delivery and also
  633. // periodically checking for timeouts.
  634. func (d *Downloader) fetchBlocks61(from uint64) error {
  635. glog.V(logger.Debug).Infof("Downloading blocks from #%d", from)
  636. defer glog.V(logger.Debug).Infof("Block download terminated")
  637. // Create a timeout timer for scheduling expiration tasks
  638. ticker := time.NewTicker(100 * time.Millisecond)
  639. defer ticker.Stop()
  640. update := make(chan struct{}, 1)
  641. // Fetch blocks until the hash fetcher's done
  642. finished := false
  643. for {
  644. select {
  645. case <-d.cancelCh:
  646. return errCancelBlockFetch
  647. case <-d.headerCh:
  648. // Out of bounds eth/62 block headers received, ignore them
  649. case <-d.bodyCh:
  650. // Out of bounds eth/62 block bodies received, ignore them
  651. case packet := <-d.blockCh:
  652. // If the peer was previously banned and failed to deliver it's pack
  653. // in a reasonable time frame, ignore it's message.
  654. if peer := d.peers.Peer(packet.PeerId()); peer != nil {
  655. // Deliver the received chunk of blocks, and demote in case of errors
  656. blocks := packet.(*blockPack).blocks
  657. err := d.queue.DeliverBlocks(peer.id, blocks)
  658. switch err {
  659. case nil:
  660. // If no blocks were delivered, demote the peer (need the delivery above)
  661. if len(blocks) == 0 {
  662. peer.Demote()
  663. peer.SetBlocksIdle()
  664. glog.V(logger.Detail).Infof("%s: no blocks delivered", peer)
  665. break
  666. }
  667. // All was successful, promote the peer and potentially start processing
  668. peer.Promote()
  669. peer.SetBlocksIdle()
  670. glog.V(logger.Detail).Infof("%s: delivered %d blocks", peer, len(blocks))
  671. go d.process()
  672. case errInvalidChain:
  673. // The hash chain is invalid (blocks are not ordered properly), abort
  674. return err
  675. case errNoFetchesPending:
  676. // Peer probably timed out with its delivery but came through
  677. // in the end, demote, but allow to to pull from this peer.
  678. peer.Demote()
  679. peer.SetBlocksIdle()
  680. glog.V(logger.Detail).Infof("%s: out of bound delivery", peer)
  681. case errStaleDelivery:
  682. // Delivered something completely else than requested, usually
  683. // caused by a timeout and delivery during a new sync cycle.
  684. // Don't set it to idle as the original request should still be
  685. // in flight.
  686. peer.Demote()
  687. glog.V(logger.Detail).Infof("%s: stale delivery", peer)
  688. default:
  689. // Peer did something semi-useful, demote but keep it around
  690. peer.Demote()
  691. peer.SetBlocksIdle()
  692. glog.V(logger.Detail).Infof("%s: delivery partially failed: %v", peer, err)
  693. go d.process()
  694. }
  695. }
  696. // Blocks arrived, try to update the progress
  697. select {
  698. case update <- struct{}{}:
  699. default:
  700. }
  701. case cont := <-d.blockWakeCh:
  702. // The hash fetcher sent a continuation flag, check if it's done
  703. if !cont {
  704. finished = true
  705. }
  706. // Hashes arrive, try to update the progress
  707. select {
  708. case update <- struct{}{}:
  709. default:
  710. }
  711. case <-ticker.C:
  712. // Sanity check update the progress
  713. select {
  714. case update <- struct{}{}:
  715. default:
  716. }
  717. case <-update:
  718. // Short circuit if we lost all our peers
  719. if d.peers.Len() == 0 {
  720. return errNoPeers
  721. }
  722. // Check for block request timeouts and demote the responsible peers
  723. for _, pid := range d.queue.ExpireBlocks(blockHardTTL) {
  724. if peer := d.peers.Peer(pid); peer != nil {
  725. peer.Demote()
  726. glog.V(logger.Detail).Infof("%s: block delivery timeout", peer)
  727. }
  728. }
  729. // If there's nothing more to fetch, wait or terminate
  730. if d.queue.PendingBlocks() == 0 {
  731. if !d.queue.InFlightBlocks() && finished {
  732. glog.V(logger.Debug).Infof("Block fetching completed")
  733. return nil
  734. }
  735. break
  736. }
  737. // Send a download request to all idle peers, until throttled
  738. throttled := false
  739. idles, total := d.peers.BlockIdlePeers()
  740. for _, peer := range idles {
  741. // Short circuit if throttling activated
  742. if d.queue.ThrottleBlocks() {
  743. throttled = true
  744. break
  745. }
  746. // Reserve a chunk of hashes for a peer. A nil can mean either that
  747. // no more hashes are available, or that the peer is known not to
  748. // have them.
  749. request := d.queue.ReserveBlocks(peer, peer.BlockCapacity())
  750. if request == nil {
  751. continue
  752. }
  753. if glog.V(logger.Detail) {
  754. glog.Infof("%s: requesting %d blocks", peer, len(request.Hashes))
  755. }
  756. // Fetch the chunk and make sure any errors return the hashes to the queue
  757. if err := peer.Fetch61(request); err != nil {
  758. glog.V(logger.Error).Infof("%v: fetch failed, rescheduling", peer)
  759. d.queue.CancelBlocks(request)
  760. }
  761. }
  762. // Make sure that we have peers available for fetching. If all peers have been tried
  763. // and all failed throw an error
  764. if !throttled && !d.queue.InFlightBlocks() && len(idles) == total {
  765. return errPeersUnavailable
  766. }
  767. }
  768. }
  769. }
  770. // fetchHeight retrieves the head header of the remote peer to aid in estimating
  771. // the total time a pending synchronisation would take.
  772. func (d *Downloader) fetchHeight(p *peer) (uint64, error) {
  773. glog.V(logger.Debug).Infof("%v: retrieving remote chain height", p)
  774. // Request the advertised remote head block and wait for the response
  775. go p.getRelHeaders(p.head, 1, 0, false)
  776. timeout := time.After(headerTTL)
  777. for {
  778. select {
  779. case <-d.cancelCh:
  780. return 0, errCancelBlockFetch
  781. case packet := <-d.headerCh:
  782. // Discard anything not from the origin peer
  783. if packet.PeerId() != p.id {
  784. glog.V(logger.Debug).Infof("Received headers from incorrect peer(%s)", packet.PeerId())
  785. break
  786. }
  787. // Make sure the peer actually gave something valid
  788. headers := packet.(*headerPack).headers
  789. if len(headers) != 1 {
  790. glog.V(logger.Debug).Infof("%v: invalid number of head headers: %d != 1", p, len(headers))
  791. return 0, errBadPeer
  792. }
  793. return headers[0].Number.Uint64(), nil
  794. case <-d.bodyCh:
  795. // Out of bounds block bodies received, ignore them
  796. case <-d.hashCh:
  797. // Out of bounds eth/61 hashes received, ignore them
  798. case <-d.blockCh:
  799. // Out of bounds eth/61 blocks received, ignore them
  800. case <-timeout:
  801. glog.V(logger.Debug).Infof("%v: head header timeout", p)
  802. return 0, errTimeout
  803. }
  804. }
  805. }
  806. // findAncestor tries to locate the common ancestor link of the local chain and
  807. // a remote peers blockchain. In the general case when our node was in sync and
  808. // on the correct chain, checking the top N links should already get us a match.
  809. // In the rare scenario when we ended up on a long reorganization (i.e. none of
  810. // the head links match), we do a binary search to find the common ancestor.
  811. func (d *Downloader) findAncestor(p *peer) (uint64, error) {
  812. glog.V(logger.Debug).Infof("%v: looking for common ancestor", p)
  813. // Request our head headers to short circuit ancestor location
  814. head := d.headHeader().Number.Uint64()
  815. if d.mode == FullSync {
  816. head = d.headBlock().NumberU64()
  817. } else if d.mode == FastSync {
  818. head = d.headFastBlock().NumberU64()
  819. }
  820. from := int64(head) - int64(MaxHeaderFetch) + 1
  821. if from < 0 {
  822. from = 0
  823. }
  824. go p.getAbsHeaders(uint64(from), MaxHeaderFetch, 0, false)
  825. // Wait for the remote response to the head fetch
  826. number, hash := uint64(0), common.Hash{}
  827. timeout := time.After(hashTTL)
  828. for finished := false; !finished; {
  829. select {
  830. case <-d.cancelCh:
  831. return 0, errCancelHashFetch
  832. case packet := <-d.headerCh:
  833. // Discard anything not from the origin peer
  834. if packet.PeerId() != p.id {
  835. glog.V(logger.Debug).Infof("Received headers from incorrect peer(%s)", packet.PeerId())
  836. break
  837. }
  838. // Make sure the peer actually gave something valid
  839. headers := packet.(*headerPack).headers
  840. if len(headers) == 0 {
  841. glog.V(logger.Debug).Infof("%v: empty head header set", p)
  842. return 0, errEmptyHeaderSet
  843. }
  844. // Check if a common ancestor was found
  845. finished = true
  846. for i := len(headers) - 1; i >= 0; i-- {
  847. if (d.mode != LightSync && d.hasBlock(headers[i].Hash())) || (d.mode == LightSync && d.hasHeader(headers[i].Hash())) {
  848. number, hash = headers[i].Number.Uint64(), headers[i].Hash()
  849. break
  850. }
  851. }
  852. case <-d.bodyCh:
  853. // Out of bounds block bodies received, ignore them
  854. case <-d.hashCh:
  855. // Out of bounds eth/61 hashes received, ignore them
  856. case <-d.blockCh:
  857. // Out of bounds eth/61 blocks received, ignore them
  858. case <-timeout:
  859. glog.V(logger.Debug).Infof("%v: head header timeout", p)
  860. return 0, errTimeout
  861. }
  862. }
  863. // If the head fetch already found an ancestor, return
  864. if !common.EmptyHash(hash) {
  865. glog.V(logger.Debug).Infof("%v: common ancestor: #%d [%x…]", p, number, hash[:4])
  866. return number, nil
  867. }
  868. // Ancestor not found, we need to binary search over our chain
  869. start, end := uint64(0), head
  870. for start+1 < end {
  871. // Split our chain interval in two, and request the hash to cross check
  872. check := (start + end) / 2
  873. timeout := time.After(hashTTL)
  874. go p.getAbsHeaders(uint64(check), 1, 0, false)
  875. // Wait until a reply arrives to this request
  876. for arrived := false; !arrived; {
  877. select {
  878. case <-d.cancelCh:
  879. return 0, errCancelHashFetch
  880. case packer := <-d.headerCh:
  881. // Discard anything not from the origin peer
  882. if packer.PeerId() != p.id {
  883. glog.V(logger.Debug).Infof("Received headers from incorrect peer(%s)", packer.PeerId())
  884. break
  885. }
  886. // Make sure the peer actually gave something valid
  887. headers := packer.(*headerPack).headers
  888. if len(headers) != 1 {
  889. glog.V(logger.Debug).Infof("%v: invalid search header set (%d)", p, len(headers))
  890. return 0, errBadPeer
  891. }
  892. arrived = true
  893. // Modify the search interval based on the response
  894. if (d.mode == FullSync && !d.hasBlock(headers[0].Hash())) || (d.mode != FullSync && !d.hasHeader(headers[0].Hash())) {
  895. end = check
  896. break
  897. }
  898. header := d.getHeader(headers[0].Hash()) // Independent of sync mode, header surely exists
  899. if header.Number.Uint64() != check {
  900. glog.V(logger.Debug).Infof("%v: non requested header #%d [%x…], instead of #%d", p, header.Number, header.Hash().Bytes()[:4], check)
  901. return 0, errBadPeer
  902. }
  903. start = check
  904. case <-d.bodyCh:
  905. // Out of bounds block bodies received, ignore them
  906. case <-d.hashCh:
  907. // Out of bounds eth/61 hashes received, ignore them
  908. case <-d.blockCh:
  909. // Out of bounds eth/61 blocks received, ignore them
  910. case <-timeout:
  911. glog.V(logger.Debug).Infof("%v: search header timeout", p)
  912. return 0, errTimeout
  913. }
  914. }
  915. }
  916. return start, nil
  917. }
  918. // fetchHeaders keeps retrieving headers from the requested number, until no more
  919. // are returned, potentially throttling on the way.
  920. //
  921. // The queue parameter can be used to switch between queuing headers for block
  922. // body download too, or directly import as pure header chains.
  923. func (d *Downloader) fetchHeaders(p *peer, td *big.Int, from uint64) error {
  924. glog.V(logger.Debug).Infof("%v: downloading headers from #%d", p, from)
  925. defer glog.V(logger.Debug).Infof("%v: header download terminated", p)
  926. // Create a timeout timer, and the associated hash fetcher
  927. request := time.Now() // time of the last fetch request
  928. timeout := time.NewTimer(0) // timer to dump a non-responsive active peer
  929. <-timeout.C // timeout channel should be initially empty
  930. defer timeout.Stop()
  931. getHeaders := func(from uint64) {
  932. glog.V(logger.Detail).Infof("%v: fetching %d headers from #%d", p, MaxHeaderFetch, from)
  933. go p.getAbsHeaders(from, MaxHeaderFetch, 0, false)
  934. request = time.Now()
  935. timeout.Reset(headerTTL)
  936. }
  937. // Start pulling headers, until all are exhausted
  938. getHeaders(from)
  939. gotHeaders := false
  940. for {
  941. select {
  942. case <-d.cancelCh:
  943. return errCancelHeaderFetch
  944. case <-d.hashCh:
  945. // Out of bounds eth/61 hashes received, ignore them
  946. case <-d.blockCh:
  947. // Out of bounds eth/61 blocks received, ignore them
  948. case packet := <-d.headerCh:
  949. // Make sure the active peer is giving us the headers
  950. if packet.PeerId() != p.id {
  951. glog.V(logger.Debug).Infof("Received headers from incorrect peer (%s)", packet.PeerId())
  952. break
  953. }
  954. headerReqTimer.UpdateSince(request)
  955. timeout.Stop()
  956. // If no more headers are inbound, notify the content fetchers and return
  957. if packet.Items() == 0 {
  958. glog.V(logger.Debug).Infof("%v: no available headers", p)
  959. for _, ch := range []chan bool{d.bodyWakeCh, d.receiptWakeCh, d.stateWakeCh} {
  960. select {
  961. case ch <- false:
  962. case <-d.cancelCh:
  963. }
  964. }
  965. // If no headers were retrieved at all, the peer violated it's TD promise that it had a
  966. // better chain compared to ours. The only exception is if it's promised blocks were
  967. // already imported by other means (e.g. fecher):
  968. //
  969. // R <remote peer>, L <local node>: Both at block 10
  970. // R: Mine block 11, and propagate it to L
  971. // L: Queue block 11 for import
  972. // L: Notice that R's head and TD increased compared to ours, start sync
  973. // L: Import of block 11 finishes
  974. // L: Sync begins, and finds common ancestor at 11
  975. // L: Request new headers up from 11 (R's TD was higher, it must have something)
  976. // R: Nothing to give
  977. if !gotHeaders && td.Cmp(d.getTd(d.headBlock().Hash())) > 0 {
  978. return errStallingPeer
  979. }
  980. return nil
  981. }
  982. gotHeaders = true
  983. headers := packet.(*headerPack).headers
  984. // Otherwise insert all the new headers, aborting in case of junk
  985. glog.V(logger.Detail).Infof("%v: schedule %d headers from #%d", p, len(headers), from)
  986. if d.mode == FastSync || d.mode == LightSync {
  987. if n, err := d.insertHeaders(headers, headerCheckFrequency); err != nil {
  988. glog.V(logger.Debug).Infof("%v: invalid header #%d [%x…]: %v", p, headers[n].Number, headers[n].Hash().Bytes()[:4], err)
  989. return errInvalidChain
  990. }
  991. }
  992. if d.mode == FullSync || d.mode == FastSync {
  993. inserts := d.queue.Schedule(headers, from)
  994. if len(inserts) != len(headers) {
  995. glog.V(logger.Debug).Infof("%v: stale headers", p)
  996. return errBadPeer
  997. }
  998. }
  999. // Notify the content fetchers of new headers, but stop if queue is full
  1000. cont := d.queue.PendingBlocks() < maxQueuedHeaders || d.queue.PendingReceipts() < maxQueuedHeaders
  1001. for _, ch := range []chan bool{d.bodyWakeCh, d.receiptWakeCh, d.stateWakeCh} {
  1002. if cont {
  1003. // We still have headers to fetch, send continuation wake signal (potential)
  1004. select {
  1005. case ch <- true:
  1006. default:
  1007. }
  1008. } else {
  1009. // Header limit reached, send a termination wake signal (enforced)
  1010. select {
  1011. case ch <- false:
  1012. case <-d.cancelCh:
  1013. }
  1014. return nil
  1015. }
  1016. }
  1017. // Queue not yet full, fetch the next batch
  1018. from += uint64(len(headers))
  1019. getHeaders(from)
  1020. case <-timeout.C:
  1021. // Header retrieval timed out, consider the peer bad and drop
  1022. glog.V(logger.Debug).Infof("%v: header request timed out", p)
  1023. headerTimeoutMeter.Mark(1)
  1024. d.dropPeer(p.id)
  1025. // Finish the sync gracefully instead of dumping the gathered data though
  1026. for _, ch := range []chan bool{d.bodyWakeCh, d.receiptWakeCh, d.stateWakeCh} {
  1027. select {
  1028. case ch <- false:
  1029. case <-d.cancelCh:
  1030. }
  1031. }
  1032. return nil
  1033. }
  1034. }
  1035. }
  1036. // fetchBodies iteratively downloads the scheduled block bodies, taking any
  1037. // available peers, reserving a chunk of blocks for each, waiting for delivery
  1038. // and also periodically checking for timeouts.
  1039. func (d *Downloader) fetchBodies(from uint64) error {
  1040. glog.V(logger.Debug).Infof("Downloading block bodies from #%d", from)
  1041. var (
  1042. deliver = func(packet dataPack) error {
  1043. pack := packet.(*bodyPack)
  1044. return d.queue.DeliverBodies(pack.peerId, pack.transactions, pack.uncles)
  1045. }
  1046. expire = func() []string { return d.queue.ExpireBodies(bodyHardTTL) }
  1047. fetch = func(p *peer, req *fetchRequest) error { return p.FetchBodies(req) }
  1048. capacity = func(p *peer) int { return p.BlockCapacity() }
  1049. getIdles = func() ([]*peer, int) { return d.peers.BodyIdlePeers() }
  1050. setIdle = func(p *peer) { p.SetBlocksIdle() }
  1051. )
  1052. err := d.fetchParts(errCancelBodyFetch, d.bodyCh, deliver, d.bodyWakeCh, expire,
  1053. d.queue.PendingBlocks, d.queue.InFlightBlocks, d.queue.ThrottleBlocks, d.queue.ReserveBodies,
  1054. d.bodyFetchHook, fetch, d.queue.CancelBodies, capacity, getIdles, setIdle, "Body")
  1055. glog.V(logger.Debug).Infof("Block body download terminated: %v", err)
  1056. return err
  1057. }
  1058. // fetchReceipts iteratively downloads the scheduled block receipts, taking any
  1059. // available peers, reserving a chunk of receipts for each, waiting for delivery
  1060. // and also periodically checking for timeouts.
  1061. func (d *Downloader) fetchReceipts(from uint64) error {
  1062. glog.V(logger.Debug).Infof("Downloading receipts from #%d", from)
  1063. var (
  1064. deliver = func(packet dataPack) error {
  1065. pack := packet.(*receiptPack)
  1066. return d.queue.DeliverReceipts(pack.peerId, pack.receipts)
  1067. }
  1068. expire = func() []string { return d.queue.ExpireReceipts(bodyHardTTL) }
  1069. fetch = func(p *peer, req *fetchRequest) error { return p.FetchReceipts(req) }
  1070. capacity = func(p *peer) int { return p.ReceiptCapacity() }
  1071. setIdle = func(p *peer) { p.SetReceiptsIdle() }
  1072. )
  1073. err := d.fetchParts(errCancelReceiptFetch, d.receiptCh, deliver, d.receiptWakeCh, expire,
  1074. d.queue.PendingReceipts, d.queue.InFlightReceipts, d.queue.ThrottleReceipts, d.queue.ReserveReceipts,
  1075. d.receiptFetchHook, fetch, d.queue.CancelReceipts, capacity, d.peers.ReceiptIdlePeers, setIdle, "Receipt")
  1076. glog.V(logger.Debug).Infof("Receipt download terminated: %v", err)
  1077. return err
  1078. }
  1079. // fetchNodeData iteratively downloads the scheduled state trie nodes, taking any
  1080. // available peers, reserving a chunk of nodes for each, waiting for delivery and
  1081. // also periodically checking for timeouts.
  1082. func (d *Downloader) fetchNodeData() error {
  1083. glog.V(logger.Debug).Infof("Downloading node state data")
  1084. var (
  1085. deliver = func(packet dataPack) error {
  1086. start := time.Now()
  1087. return d.queue.DeliverNodeData(packet.PeerId(), packet.(*statePack).states, func(err error, delivered int) {
  1088. if err != nil {
  1089. // If the node data processing failed, the root hash is very wrong, abort
  1090. glog.V(logger.Error).Infof("peer %d: state processing failed: %v", packet.PeerId(), err)
  1091. d.cancel()
  1092. return
  1093. }
  1094. // Processing succeeded, notify state fetcher and processor of continuation
  1095. if d.queue.PendingNodeData() == 0 {
  1096. go d.process()
  1097. } else {
  1098. select {
  1099. case d.stateWakeCh <- true:
  1100. default:
  1101. }
  1102. }
  1103. // Log a message to the user and return
  1104. d.syncStatsLock.Lock()
  1105. defer d.syncStatsLock.Unlock()
  1106. d.syncStatsStateDone += uint64(delivered)
  1107. glog.V(logger.Info).Infof("imported %d state entries in %v: processed %d in total", delivered, time.Since(start), d.syncStatsStateDone)
  1108. })
  1109. }
  1110. expire = func() []string { return d.queue.ExpireNodeData(stateHardTTL) }
  1111. throttle = func() bool { return false }
  1112. reserve = func(p *peer, count int) (*fetchRequest, bool, error) {
  1113. return d.queue.ReserveNodeData(p, count), false, nil
  1114. }
  1115. fetch = func(p *peer, req *fetchRequest) error { return p.FetchNodeData(req) }
  1116. capacity = func(p *peer) int { return p.NodeDataCapacity() }
  1117. setIdle = func(p *peer) { p.SetNodeDataIdle() }
  1118. )
  1119. err := d.fetchParts(errCancelReceiptFetch, d.stateCh, deliver, d.stateWakeCh, expire,
  1120. d.queue.PendingNodeData, d.queue.InFlightNodeData, throttle, reserve, nil, fetch,
  1121. d.queue.CancelNodeData, capacity, d.peers.ReceiptIdlePeers, setIdle, "State")
  1122. glog.V(logger.Debug).Infof("Node state data download terminated: %v", err)
  1123. return err
  1124. }
  1125. // fetchParts iteratively downloads scheduled block parts, taking any available
  1126. // peers, reserving a chunk of fetch requests for each, waiting for delivery and
  1127. // also periodically checking for timeouts.
  1128. func (d *Downloader) fetchParts(errCancel error, deliveryCh chan dataPack, deliver func(packet dataPack) error, wakeCh chan bool,
  1129. expire func() []string, pending func() int, inFlight func() bool, throttle func() bool, reserve func(*peer, int) (*fetchRequest, bool, error),
  1130. fetchHook func([]*types.Header), fetch func(*peer, *fetchRequest) error, cancel func(*fetchRequest), capacity func(*peer) int,
  1131. idle func() ([]*peer, int), setIdle func(*peer), kind string) error {
  1132. // Create a ticker to detect expired retreival tasks
  1133. ticker := time.NewTicker(100 * time.Millisecond)
  1134. defer ticker.Stop()
  1135. update := make(chan struct{}, 1)
  1136. // Prepare the queue and fetch block parts until the block header fetcher's done
  1137. finished := false
  1138. for {
  1139. select {
  1140. case <-d.cancelCh:
  1141. return errCancel
  1142. case <-d.hashCh:
  1143. // Out of bounds eth/61 hashes received, ignore them
  1144. case <-d.blockCh:
  1145. // Out of bounds eth/61 blocks received, ignore them
  1146. case packet := <-deliveryCh:
  1147. // If the peer was previously banned and failed to deliver it's pack
  1148. // in a reasonable time frame, ignore it's message.
  1149. if peer := d.peers.Peer(packet.PeerId()); peer != nil {
  1150. // Deliver the received chunk of data, and demote in case of errors
  1151. switch err := deliver(packet); err {
  1152. case nil:
  1153. // If no blocks were delivered, demote the peer (need the delivery above to clean internal queue!)
  1154. if packet.Items() == 0 {
  1155. peer.Demote()
  1156. setIdle(peer)
  1157. glog.V(logger.Detail).Infof("%s: no %s delivered", peer, strings.ToLower(kind))
  1158. break
  1159. }
  1160. // All was successful, promote the peer and potentially start processing
  1161. peer.Promote()
  1162. setIdle(peer)
  1163. glog.V(logger.Detail).Infof("%s: delivered %s %s(s)", peer, packet.Stats(), strings.ToLower(kind))
  1164. go d.process()
  1165. case errInvalidChain:
  1166. // The hash chain is invalid (blocks are not ordered properly), abort
  1167. return err
  1168. case errInvalidBody, errInvalidReceipt:
  1169. // The peer delivered something very bad, drop immediately
  1170. glog.V(logger.Error).Infof("%s: delivered invalid %s, dropping", peer, strings.ToLower(kind))
  1171. d.dropPeer(peer.id)
  1172. case errNoFetchesPending:
  1173. // Peer probably timed out with its delivery but came through
  1174. // in the end, demote, but allow to to pull from this peer.
  1175. peer.Demote()
  1176. setIdle(peer)
  1177. glog.V(logger.Detail).Infof("%s: out of bound %s delivery", peer, strings.ToLower(kind))
  1178. case errStaleDelivery:
  1179. // Delivered something completely else than requested, usually
  1180. // caused by a timeout and delivery during a new sync cycle.
  1181. // Don't set it to idle as the original request should still be
  1182. // in flight.
  1183. peer.Demote()
  1184. glog.V(logger.Detail).Infof("%s: %s stale delivery", peer, strings.ToLower(kind))
  1185. default:
  1186. // Peer did something semi-useful, demote but keep it around
  1187. peer.Demote()
  1188. setIdle(peer)
  1189. glog.V(logger.Detail).Infof("%s: %s delivery partially failed: %v", peer, strings.ToLower(kind), err)
  1190. go d.process()
  1191. }
  1192. }
  1193. // Blocks assembled, try to update the progress
  1194. select {
  1195. case update <- struct{}{}:
  1196. default:
  1197. }
  1198. case cont := <-wakeCh:
  1199. // The header fetcher sent a continuation flag, check if it's done
  1200. if !cont {
  1201. finished = true
  1202. }
  1203. // Headers arrive, try to update the progress
  1204. select {
  1205. case update <- struct{}{}:
  1206. default:
  1207. }
  1208. case <-ticker.C:
  1209. // Sanity check update the progress
  1210. select {
  1211. case update <- struct{}{}:
  1212. default:
  1213. }
  1214. case <-update:
  1215. // Short circuit if we lost all our peers
  1216. if d.peers.Len() == 0 {
  1217. return errNoPeers
  1218. }
  1219. // Check for fetch request timeouts and demote the responsible peers
  1220. for _, pid := range expire() {
  1221. if peer := d.peers.Peer(pid); peer != nil {
  1222. peer.Demote()
  1223. setIdle(peer)
  1224. glog.V(logger.Detail).Infof("%s: %s delivery timeout", peer, strings.ToLower(kind))
  1225. }
  1226. }
  1227. // If there's nothing more to fetch, wait or terminate
  1228. if pending() == 0 {
  1229. if !inFlight() && finished {
  1230. glog.V(logger.Debug).Infof("%s fetching completed", kind)
  1231. return nil
  1232. }
  1233. break
  1234. }
  1235. // Send a download request to all idle peers, until throttled
  1236. progressed, throttled, running := false, false, inFlight()
  1237. idles, total := idle()
  1238. for _, peer := range idles {
  1239. // Short circuit if throttling activated
  1240. if throttle() {
  1241. throttled = true
  1242. break
  1243. }
  1244. // Reserve a chunk of fetches for a peer. A nil can mean either that
  1245. // no more headers are available, or that the peer is known not to
  1246. // have them.
  1247. request, progress, err := reserve(peer, capacity(peer))
  1248. if err != nil {
  1249. return err
  1250. }
  1251. if progress {
  1252. progressed = true
  1253. go d.process()
  1254. }
  1255. if request == nil {
  1256. continue
  1257. }
  1258. if glog.V(logger.Detail) {
  1259. if len(request.Headers) > 0 {
  1260. glog.Infof("%s: requesting %d %s(s), first at #%d", peer, len(request.Headers), strings.ToLower(kind), request.Headers[0].Number)
  1261. } else {
  1262. glog.Infof("%s: requesting %d %s(s)", peer, len(request.Hashes), strings.ToLower(kind))
  1263. }
  1264. }
  1265. // Fetch the chunk and make sure any errors return the hashes to the queue
  1266. if fetchHook != nil {
  1267. fetchHook(request.Headers)
  1268. }
  1269. if err := fetch(peer, request); err != nil {
  1270. glog.V(logger.Error).Infof("%v: %s fetch failed, rescheduling", peer, strings.ToLower(kind))
  1271. cancel(request)
  1272. }
  1273. running = true
  1274. }
  1275. // Make sure that we have peers available for fetching. If all peers have been tried
  1276. // and all failed throw an error
  1277. if !progressed && !throttled && !running && len(idles) == total && pending() > 0 {
  1278. return errPeersUnavailable
  1279. }
  1280. }
  1281. }
  1282. }
  1283. // process takes fetch results from the queue and tries to import them into the
  1284. // chain. The type of import operation will depend on the result contents:
  1285. // -
  1286. //
  1287. // The algorithmic flow is as follows:
  1288. // - The `processing` flag is swapped to 1 to ensure singleton access
  1289. // - The current `cancel` channel is retrieved to detect sync abortions
  1290. // - Blocks are iteratively taken from the cache and inserted into the chain
  1291. // - When the cache becomes empty, insertion stops
  1292. // - The `processing` flag is swapped back to 0
  1293. // - A post-exit check is made whether new blocks became available
  1294. // - This step is important: it handles a potential race condition between
  1295. // checking for no more work, and releasing the processing "mutex". In
  1296. // between these state changes, a block may have arrived, but a processing
  1297. // attempt denied, so we need to re-enter to ensure the block isn't left
  1298. // to idle in the cache.
  1299. func (d *Downloader) process() {
  1300. // Make sure only one goroutine is ever allowed to process blocks at once
  1301. if !atomic.CompareAndSwapInt32(&d.processing, 0, 1) {
  1302. return
  1303. }
  1304. // If the processor just exited, but there are freshly pending items, try to
  1305. // reenter. This is needed because the goroutine spinned up for processing
  1306. // the fresh results might have been rejected entry to to this present thread
  1307. // not yet releasing the `processing` state.
  1308. defer func() {
  1309. if atomic.LoadInt32(&d.interrupt) == 0 && d.queue.GetHeadResult() != nil {
  1310. d.process()
  1311. }
  1312. }()
  1313. // Release the lock upon exit (note, before checking for reentry!)
  1314. // the import statistics to zero.
  1315. defer atomic.StoreInt32(&d.processing, 0)
  1316. // Repeat the processing as long as there are results to process
  1317. for {
  1318. // Fetch the next batch of results
  1319. results := d.queue.TakeResults()
  1320. if len(results) == 0 {
  1321. return
  1322. }
  1323. if d.chainInsertHook != nil {
  1324. d.chainInsertHook(results)
  1325. }
  1326. // Actually import the blocks
  1327. if glog.V(logger.Debug) {
  1328. first, last := results[0].Header, results[len(results)-1].Header
  1329. glog.Infof("Inserting chain with %d items (#%d [%x…] - #%d [%x…])", len(results), first.Number, first.Hash().Bytes()[:4], last.Number, last.Hash().Bytes()[:4])
  1330. }
  1331. for len(results) != 0 {
  1332. // Check for any termination requests
  1333. if atomic.LoadInt32(&d.interrupt) == 1 {
  1334. return
  1335. }
  1336. // Retrieve the a batch of results to import
  1337. var (
  1338. headers = make([]*types.Header, 0, maxResultsProcess)
  1339. blocks = make([]*types.Block, 0, maxResultsProcess)
  1340. receipts = make([]types.Receipts, 0, maxResultsProcess)
  1341. )
  1342. items := int(math.Min(float64(len(results)), float64(maxResultsProcess)))
  1343. for _, result := range results[:items] {
  1344. switch {
  1345. case d.mode == FullSync:
  1346. blocks = append(blocks, types.NewBlockWithHeader(result.Header).WithBody(result.Transactions, result.Uncles))
  1347. case d.mode == FastSync:
  1348. blocks = append(blocks, types.NewBlockWithHeader(result.Header).WithBody(result.Transactions, result.Uncles))
  1349. if result.Header.Number.Uint64() <= d.queue.fastSyncPivot {
  1350. receipts = append(receipts, result.Receipts)
  1351. }
  1352. case d.mode == LightSync:
  1353. headers = append(headers, result.Header)
  1354. }
  1355. }
  1356. // Try to process the results, aborting if there's an error
  1357. var (
  1358. err error
  1359. index int
  1360. )
  1361. switch {
  1362. case len(headers) > 0:
  1363. index, err = d.insertHeaders(headers, headerCheckFrequency)
  1364. case len(receipts) > 0:
  1365. index, err = d.insertReceipts(blocks, receipts)
  1366. if err == nil && blocks[len(blocks)-1].NumberU64() == d.queue.fastSyncPivot {
  1367. index, err = len(blocks)-1, d.commitHeadBlock(blocks[len(blocks)-1].Hash())
  1368. }
  1369. default:
  1370. index, err = d.insertBlocks(blocks)
  1371. }
  1372. if err != nil {
  1373. glog.V(logger.Debug).Infof("Result #%d [%x…] processing failed: %v", results[index].Header.Number, results[index].Header.Hash().Bytes()[:4], err)
  1374. d.cancel()
  1375. return
  1376. }
  1377. // Shift the results to the next batch
  1378. results = results[items:]
  1379. }
  1380. }
  1381. }
  1382. // DeliverHashes injects a new batch of hashes received from a remote node into
  1383. // the download schedule. This is usually invoked through the BlockHashesMsg by
  1384. // the protocol handler.
  1385. func (d *Downloader) DeliverHashes(id string, hashes []common.Hash) (err error) {
  1386. return d.deliver(id, d.hashCh, &hashPack{id, hashes}, hashInMeter, hashDropMeter)
  1387. }
  1388. // DeliverBlocks injects a new batch of blocks received from a remote node.
  1389. // This is usually invoked through the BlocksMsg by the protocol handler.
  1390. func (d *Downloader) DeliverBlocks(id string, blocks []*types.Block) (err error) {
  1391. return d.deliver(id, d.blockCh, &blockPack{id, blocks}, blockInMeter, blockDropMeter)
  1392. }
  1393. // DeliverHeaders injects a new batch of blck headers received from a remote
  1394. // node into the download schedule.
  1395. func (d *Downloader) DeliverHeaders(id string, headers []*types.Header) (err error) {
  1396. return d.deliver(id, d.headerCh, &headerPack{id, headers}, headerInMeter, headerDropMeter)
  1397. }
  1398. // DeliverBodies injects a new batch of block bodies received from a remote node.
  1399. func (d *Downloader) DeliverBodies(id string, transactions [][]*types.Transaction, uncles [][]*types.Header) (err error) {
  1400. return d.deliver(id, d.bodyCh, &bodyPack{id, transactions, uncles}, bodyInMeter, bodyDropMeter)
  1401. }
  1402. // DeliverReceipts injects a new batch of receipts received from a remote node.
  1403. func (d *Downloader) DeliverReceipts(id string, receipts [][]*types.Receipt) (err error) {
  1404. return d.deliver(id, d.receiptCh, &receiptPack{id, receipts}, receiptInMeter, receiptDropMeter)
  1405. }
  1406. // DeliverNodeData injects a new batch of node state data received from a remote node.
  1407. func (d *Downloader) DeliverNodeData(id string, data [][]byte) (err error) {
  1408. return d.deliver(id, d.stateCh, &statePack{id, data}, stateInMeter, stateDropMeter)
  1409. }
  1410. // deliver injects a new batch of data received from a remote node.
  1411. func (d *Downloader) deliver(id string, destCh chan dataPack, packet dataPack, inMeter, dropMeter metrics.Meter) (err error) {
  1412. // Update the delivery metrics for both good and failed deliveries
  1413. inMeter.Mark(int64(packet.Items()))
  1414. defer func() {
  1415. if err != nil {
  1416. dropMeter.Mark(int64(packet.Items()))
  1417. }
  1418. }()
  1419. // Make sure the downloader is active
  1420. if atomic.LoadInt32(&d.synchronising) == 0 {
  1421. return errNoSyncActive
  1422. }
  1423. // Deliver or abort if the sync is canceled while queuing
  1424. d.cancelLock.RLock()
  1425. cancel := d.cancelCh
  1426. d.cancelLock.RUnlock()
  1427. select {
  1428. case destCh <- packet:
  1429. return nil
  1430. case <-cancel:
  1431. return errNoSyncActive
  1432. }
  1433. }