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