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