github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/neatptc/downloader/queue.go (about) 1 package downloader 2 3 import ( 4 "errors" 5 "fmt" 6 "sync" 7 "time" 8 9 "github.com/neatlab/neatio/chain/core/types" 10 "github.com/neatlab/neatio/chain/log" 11 "github.com/neatlab/neatio/utilities/common" 12 "github.com/neatlab/neatio/utilities/common/prque" 13 "github.com/neatlab/neatio/utilities/metrics" 14 ) 15 16 var ( 17 blockCacheItems = 8192 18 blockCacheMemory = 64 * 1024 * 1024 19 blockCacheSizeWeight = 0.1 20 ) 21 22 var ( 23 errNoFetchesPending = errors.New("no fetches pending") 24 errStaleDelivery = errors.New("stale delivery") 25 ) 26 27 type fetchRequest struct { 28 Peer *peerConnection 29 From uint64 30 Headers []*types.Header 31 Time time.Time 32 } 33 34 type fetchResult struct { 35 Pending int 36 Hash common.Hash 37 38 Header *types.Header 39 Uncles []*types.Header 40 Transactions types.Transactions 41 Receipts types.Receipts 42 } 43 44 type queue struct { 45 mode SyncMode 46 47 headerHead common.Hash 48 headerTaskPool map[uint64]*types.Header 49 headerTaskQueue *prque.Prque 50 headerPeerMiss map[string]map[uint64]struct{} 51 headerPendPool map[string]*fetchRequest 52 headerResults []*types.Header 53 headerProced int 54 headerOffset uint64 55 headerContCh chan bool 56 57 blockTaskPool map[common.Hash]*types.Header 58 blockTaskQueue *prque.Prque 59 blockPendPool map[string]*fetchRequest 60 blockDonePool map[common.Hash]struct{} 61 62 receiptTaskPool map[common.Hash]*types.Header 63 receiptTaskQueue *prque.Prque 64 receiptPendPool map[string]*fetchRequest 65 receiptDonePool map[common.Hash]struct{} 66 67 resultCache []*fetchResult 68 resultOffset uint64 69 resultSize common.StorageSize 70 71 lock *sync.Mutex 72 active *sync.Cond 73 closed bool 74 } 75 76 func newQueue() *queue { 77 lock := new(sync.Mutex) 78 return &queue{ 79 headerPendPool: make(map[string]*fetchRequest), 80 headerContCh: make(chan bool), 81 blockTaskPool: make(map[common.Hash]*types.Header), 82 blockTaskQueue: prque.New(nil), 83 blockPendPool: make(map[string]*fetchRequest), 84 blockDonePool: make(map[common.Hash]struct{}), 85 receiptTaskPool: make(map[common.Hash]*types.Header), 86 receiptTaskQueue: prque.New(nil), 87 receiptPendPool: make(map[string]*fetchRequest), 88 receiptDonePool: make(map[common.Hash]struct{}), 89 resultCache: make([]*fetchResult, blockCacheItems), 90 active: sync.NewCond(lock), 91 lock: lock, 92 } 93 } 94 95 func (q *queue) Reset() { 96 q.lock.Lock() 97 defer q.lock.Unlock() 98 99 q.closed = false 100 q.mode = FullSync 101 102 q.headerHead = common.Hash{} 103 q.headerPendPool = make(map[string]*fetchRequest) 104 105 q.blockTaskPool = make(map[common.Hash]*types.Header) 106 q.blockTaskQueue.Reset() 107 q.blockPendPool = make(map[string]*fetchRequest) 108 q.blockDonePool = make(map[common.Hash]struct{}) 109 110 q.receiptTaskPool = make(map[common.Hash]*types.Header) 111 q.receiptTaskQueue.Reset() 112 q.receiptPendPool = make(map[string]*fetchRequest) 113 q.receiptDonePool = make(map[common.Hash]struct{}) 114 115 q.resultCache = make([]*fetchResult, blockCacheItems) 116 q.resultOffset = 0 117 } 118 119 func (q *queue) Close() { 120 q.lock.Lock() 121 q.closed = true 122 q.lock.Unlock() 123 q.active.Broadcast() 124 } 125 126 func (q *queue) PendingHeaders() int { 127 q.lock.Lock() 128 defer q.lock.Unlock() 129 130 return q.headerTaskQueue.Size() 131 } 132 133 func (q *queue) PendingBlocks() int { 134 q.lock.Lock() 135 defer q.lock.Unlock() 136 137 return q.blockTaskQueue.Size() 138 } 139 140 func (q *queue) PendingReceipts() int { 141 q.lock.Lock() 142 defer q.lock.Unlock() 143 144 return q.receiptTaskQueue.Size() 145 } 146 147 func (q *queue) InFlightHeaders() bool { 148 q.lock.Lock() 149 defer q.lock.Unlock() 150 151 return len(q.headerPendPool) > 0 152 } 153 154 func (q *queue) InFlightBlocks() bool { 155 q.lock.Lock() 156 defer q.lock.Unlock() 157 158 return len(q.blockPendPool) > 0 159 } 160 161 func (q *queue) InFlightReceipts() bool { 162 q.lock.Lock() 163 defer q.lock.Unlock() 164 165 return len(q.receiptPendPool) > 0 166 } 167 168 func (q *queue) Idle() bool { 169 q.lock.Lock() 170 defer q.lock.Unlock() 171 172 queued := q.blockTaskQueue.Size() + q.receiptTaskQueue.Size() 173 pending := len(q.blockPendPool) + len(q.receiptPendPool) 174 cached := len(q.blockDonePool) + len(q.receiptDonePool) 175 176 return (queued + pending + cached) == 0 177 } 178 179 func (q *queue) ShouldThrottleBlocks() bool { 180 q.lock.Lock() 181 defer q.lock.Unlock() 182 183 return q.resultSlots(q.blockPendPool, q.blockDonePool) <= 0 184 } 185 186 func (q *queue) ShouldThrottleReceipts() bool { 187 q.lock.Lock() 188 defer q.lock.Unlock() 189 190 return q.resultSlots(q.receiptPendPool, q.receiptDonePool) <= 0 191 } 192 193 func (q *queue) resultSlots(pendPool map[string]*fetchRequest, donePool map[common.Hash]struct{}) int { 194 195 limit := len(q.resultCache) 196 if common.StorageSize(len(q.resultCache))*q.resultSize > common.StorageSize(blockCacheMemory) { 197 limit = int((common.StorageSize(blockCacheMemory) + q.resultSize - 1) / q.resultSize) 198 } 199 200 finished := 0 201 for _, result := range q.resultCache[:limit] { 202 if result == nil { 203 break 204 } 205 if _, ok := donePool[result.Hash]; ok { 206 finished++ 207 } 208 } 209 210 pending := 0 211 for _, request := range pendPool { 212 for _, header := range request.Headers { 213 if header.Number.Uint64() < q.resultOffset+uint64(limit) { 214 pending++ 215 } 216 } 217 } 218 219 return limit - finished - pending 220 } 221 222 func (q *queue) ScheduleSkeleton(from uint64, skeleton []*types.Header) { 223 q.lock.Lock() 224 defer q.lock.Unlock() 225 226 if q.headerResults != nil { 227 panic("skeleton assembly already in progress") 228 } 229 230 q.headerTaskPool = make(map[uint64]*types.Header) 231 q.headerTaskQueue = prque.New(nil) 232 q.headerPeerMiss = make(map[string]map[uint64]struct{}) 233 q.headerResults = make([]*types.Header, len(skeleton)*MaxHeaderFetch) 234 q.headerProced = 0 235 q.headerOffset = from 236 q.headerContCh = make(chan bool, 1) 237 238 for i, header := range skeleton { 239 index := from + uint64(i*MaxHeaderFetch) 240 241 q.headerTaskPool[index] = header 242 q.headerTaskQueue.Push(index, -int64(index)) 243 } 244 } 245 246 func (q *queue) RetrieveHeaders() ([]*types.Header, int) { 247 q.lock.Lock() 248 defer q.lock.Unlock() 249 250 headers, proced := q.headerResults, q.headerProced 251 q.headerResults, q.headerProced = nil, 0 252 253 return headers, proced 254 } 255 256 func (q *queue) Schedule(headers []*types.Header, from uint64) []*types.Header { 257 q.lock.Lock() 258 defer q.lock.Unlock() 259 260 inserts := make([]*types.Header, 0, len(headers)) 261 for _, header := range headers { 262 263 hash := header.Hash() 264 if header.Number == nil || header.Number.Uint64() != from { 265 log.Warn("Header broke chain ordering", "number", header.Number, "hash", hash, "expected", from) 266 break 267 } 268 if q.headerHead != (common.Hash{}) && q.headerHead != header.ParentHash { 269 log.Warn("Header broke chain ancestry", "number", header.Number, "hash", hash) 270 break 271 } 272 273 if _, ok := q.blockTaskPool[hash]; ok { 274 log.Warn("Header already scheduled for block fetch", "number", header.Number, "hash", hash) 275 continue 276 } 277 if _, ok := q.receiptTaskPool[hash]; ok { 278 log.Warn("Header already scheduled for receipt fetch", "number", header.Number, "hash", hash) 279 continue 280 } 281 282 q.blockTaskPool[hash] = header 283 q.blockTaskQueue.Push(header, -int64(header.Number.Uint64())) 284 285 if q.mode == FastSync { 286 q.receiptTaskPool[hash] = header 287 q.receiptTaskQueue.Push(header, -int64(header.Number.Uint64())) 288 } 289 inserts = append(inserts, header) 290 q.headerHead = hash 291 from++ 292 } 293 return inserts 294 } 295 296 func (q *queue) Results(block bool) []*fetchResult { 297 q.lock.Lock() 298 defer q.lock.Unlock() 299 300 nproc := q.countProcessableItems() 301 for nproc == 0 && !q.closed { 302 if !block { 303 return nil 304 } 305 q.active.Wait() 306 nproc = q.countProcessableItems() 307 } 308 309 if nproc > maxResultsProcess { 310 nproc = maxResultsProcess 311 } 312 results := make([]*fetchResult, nproc) 313 copy(results, q.resultCache[:nproc]) 314 if len(results) > 0 { 315 316 for _, result := range results { 317 hash := result.Header.Hash() 318 delete(q.blockDonePool, hash) 319 delete(q.receiptDonePool, hash) 320 } 321 322 copy(q.resultCache, q.resultCache[nproc:]) 323 for i := len(q.resultCache) - nproc; i < len(q.resultCache); i++ { 324 q.resultCache[i] = nil 325 } 326 327 q.resultOffset += uint64(nproc) 328 329 for _, result := range results { 330 size := result.Header.Size() 331 for _, uncle := range result.Uncles { 332 size += uncle.Size() 333 } 334 for _, receipt := range result.Receipts { 335 size += receipt.Size() 336 } 337 for _, tx := range result.Transactions { 338 size += tx.Size() 339 } 340 q.resultSize = common.StorageSize(blockCacheSizeWeight)*size + (1-common.StorageSize(blockCacheSizeWeight))*q.resultSize 341 } 342 } 343 return results 344 } 345 346 func (q *queue) countProcessableItems() int { 347 for i, result := range q.resultCache { 348 if result == nil || result.Pending > 0 { 349 return i 350 } 351 } 352 return len(q.resultCache) 353 } 354 355 func (q *queue) ReserveHeaders(p *peerConnection, count int) *fetchRequest { 356 q.lock.Lock() 357 defer q.lock.Unlock() 358 359 if _, ok := q.headerPendPool[p.id]; ok { 360 return nil 361 } 362 363 send, skip := uint64(0), []uint64{} 364 for send == 0 && !q.headerTaskQueue.Empty() { 365 from, _ := q.headerTaskQueue.Pop() 366 if q.headerPeerMiss[p.id] != nil { 367 if _, ok := q.headerPeerMiss[p.id][from.(uint64)]; ok { 368 skip = append(skip, from.(uint64)) 369 continue 370 } 371 } 372 send = from.(uint64) 373 } 374 375 for _, from := range skip { 376 q.headerTaskQueue.Push(from, -int64(from)) 377 } 378 379 if send == 0 { 380 return nil 381 } 382 request := &fetchRequest{ 383 Peer: p, 384 From: send, 385 Time: time.Now(), 386 } 387 q.headerPendPool[p.id] = request 388 return request 389 } 390 391 func (q *queue) ReserveBodies(p *peerConnection, count int) (*fetchRequest, bool, error) { 392 isNoop := func(header *types.Header) bool { 393 return header.TxHash == types.EmptyRootHash && header.UncleHash == types.EmptyUncleHash 394 } 395 q.lock.Lock() 396 defer q.lock.Unlock() 397 398 return q.reserveHeaders(p, count, q.blockTaskPool, q.blockTaskQueue, q.blockPendPool, q.blockDonePool, isNoop) 399 } 400 401 func (q *queue) ReserveReceipts(p *peerConnection, count int) (*fetchRequest, bool, error) { 402 isNoop := func(header *types.Header) bool { 403 return header.ReceiptHash == types.EmptyRootHash 404 } 405 q.lock.Lock() 406 defer q.lock.Unlock() 407 408 return q.reserveHeaders(p, count, q.receiptTaskPool, q.receiptTaskQueue, q.receiptPendPool, q.receiptDonePool, isNoop) 409 } 410 411 func (q *queue) reserveHeaders(p *peerConnection, count int, taskPool map[common.Hash]*types.Header, taskQueue *prque.Prque, 412 pendPool map[string]*fetchRequest, donePool map[common.Hash]struct{}, isNoop func(*types.Header) bool) (*fetchRequest, bool, error) { 413 414 if taskQueue.Empty() { 415 return nil, false, nil 416 } 417 if _, ok := pendPool[p.id]; ok { 418 return nil, false, nil 419 } 420 421 space := q.resultSlots(pendPool, donePool) 422 423 send := make([]*types.Header, 0, count) 424 skip := make([]*types.Header, 0) 425 426 progress := false 427 for proc := 0; proc < space && len(send) < count && !taskQueue.Empty(); proc++ { 428 header := taskQueue.PopItem().(*types.Header) 429 hash := header.Hash() 430 431 index := int(header.Number.Int64() - int64(q.resultOffset)) 432 if index >= len(q.resultCache) || index < 0 { 433 common.Report("index allocation went beyond available resultCache space") 434 return nil, false, errInvalidChain 435 } 436 if q.resultCache[index] == nil { 437 components := 1 438 if q.mode == FastSync { 439 components = 2 440 } 441 q.resultCache[index] = &fetchResult{ 442 Pending: components, 443 Hash: hash, 444 Header: header, 445 } 446 } 447 448 if isNoop(header) { 449 donePool[hash] = struct{}{} 450 delete(taskPool, hash) 451 452 space, proc = space-1, proc-1 453 q.resultCache[index].Pending-- 454 progress = true 455 continue 456 } 457 458 if p.Lacks(hash) { 459 skip = append(skip, header) 460 } else { 461 send = append(send, header) 462 } 463 } 464 465 for _, header := range skip { 466 taskQueue.Push(header, -int64(header.Number.Uint64())) 467 } 468 if progress { 469 470 q.active.Signal() 471 } 472 473 if len(send) == 0 { 474 return nil, progress, nil 475 } 476 request := &fetchRequest{ 477 Peer: p, 478 Headers: send, 479 Time: time.Now(), 480 } 481 pendPool[p.id] = request 482 483 return request, progress, nil 484 } 485 486 func (q *queue) CancelHeaders(request *fetchRequest) { 487 q.cancel(request, q.headerTaskQueue, q.headerPendPool) 488 } 489 490 func (q *queue) CancelBodies(request *fetchRequest) { 491 q.cancel(request, q.blockTaskQueue, q.blockPendPool) 492 } 493 494 func (q *queue) CancelReceipts(request *fetchRequest) { 495 q.cancel(request, q.receiptTaskQueue, q.receiptPendPool) 496 } 497 498 func (q *queue) cancel(request *fetchRequest, taskQueue *prque.Prque, pendPool map[string]*fetchRequest) { 499 q.lock.Lock() 500 defer q.lock.Unlock() 501 502 if request.From > 0 { 503 taskQueue.Push(request.From, -int64(request.From)) 504 } 505 for _, header := range request.Headers { 506 taskQueue.Push(header, -int64(header.Number.Uint64())) 507 } 508 delete(pendPool, request.Peer.id) 509 } 510 511 func (q *queue) Revoke(peerId string) { 512 q.lock.Lock() 513 defer q.lock.Unlock() 514 515 if request, ok := q.blockPendPool[peerId]; ok { 516 for _, header := range request.Headers { 517 q.blockTaskQueue.Push(header, -int64(header.Number.Uint64())) 518 } 519 delete(q.blockPendPool, peerId) 520 } 521 if request, ok := q.receiptPendPool[peerId]; ok { 522 for _, header := range request.Headers { 523 q.receiptTaskQueue.Push(header, -int64(header.Number.Uint64())) 524 } 525 delete(q.receiptPendPool, peerId) 526 } 527 } 528 529 func (q *queue) ExpireHeaders(timeout time.Duration) map[string]int { 530 q.lock.Lock() 531 defer q.lock.Unlock() 532 533 return q.expire(timeout, q.headerPendPool, q.headerTaskQueue, headerTimeoutMeter) 534 } 535 536 func (q *queue) ExpireBodies(timeout time.Duration) map[string]int { 537 q.lock.Lock() 538 defer q.lock.Unlock() 539 540 return q.expire(timeout, q.blockPendPool, q.blockTaskQueue, bodyTimeoutMeter) 541 } 542 543 func (q *queue) ExpireReceipts(timeout time.Duration) map[string]int { 544 q.lock.Lock() 545 defer q.lock.Unlock() 546 547 return q.expire(timeout, q.receiptPendPool, q.receiptTaskQueue, receiptTimeoutMeter) 548 } 549 550 func (q *queue) expire(timeout time.Duration, pendPool map[string]*fetchRequest, taskQueue *prque.Prque, timeoutMeter metrics.Meter) map[string]int { 551 552 expiries := make(map[string]int) 553 for id, request := range pendPool { 554 if time.Since(request.Time) > timeout { 555 556 timeoutMeter.Mark(1) 557 558 if request.From > 0 { 559 taskQueue.Push(request.From, -int64(request.From)) 560 } 561 for _, header := range request.Headers { 562 taskQueue.Push(header, -int64(header.Number.Uint64())) 563 } 564 565 expiries[id] = len(request.Headers) 566 } 567 } 568 569 for id := range expiries { 570 delete(pendPool, id) 571 } 572 return expiries 573 } 574 575 func (q *queue) DeliverHeaders(id string, headers []*types.Header, headerProcCh chan []*types.Header) (int, error) { 576 q.lock.Lock() 577 defer q.lock.Unlock() 578 579 request := q.headerPendPool[id] 580 if request == nil { 581 return 0, errNoFetchesPending 582 } 583 headerReqTimer.UpdateSince(request.Time) 584 delete(q.headerPendPool, id) 585 586 target := q.headerTaskPool[request.From].Hash() 587 588 accepted := len(headers) == MaxHeaderFetch 589 if accepted { 590 if headers[0].Number.Uint64() != request.From { 591 log.Trace("First header broke chain ordering", "peer", id, "number", headers[0].Number, "hash", headers[0].Hash(), request.From) 592 accepted = false 593 } else if headers[len(headers)-1].Hash() != target { 594 log.Trace("Last header broke skeleton structure ", "peer", id, "number", headers[len(headers)-1].Number, "hash", headers[len(headers)-1].Hash(), "expected", target) 595 accepted = false 596 } 597 } 598 if accepted { 599 for i, header := range headers[1:] { 600 hash := header.Hash() 601 if want := request.From + 1 + uint64(i); header.Number.Uint64() != want { 602 log.Warn("Header broke chain ordering", "peer", id, "number", header.Number, "hash", hash, "expected", want) 603 accepted = false 604 break 605 } 606 if headers[i].Hash() != header.ParentHash { 607 log.Warn("Header broke chain ancestry", "peer", id, "number", header.Number, "hash", hash) 608 accepted = false 609 break 610 } 611 } 612 } 613 614 if !accepted { 615 log.Trace("Skeleton filling not accepted", "peer", id, "from", request.From) 616 617 miss := q.headerPeerMiss[id] 618 if miss == nil { 619 q.headerPeerMiss[id] = make(map[uint64]struct{}) 620 miss = q.headerPeerMiss[id] 621 } 622 miss[request.From] = struct{}{} 623 624 q.headerTaskQueue.Push(request.From, -int64(request.From)) 625 return 0, errors.New("delivery not accepted") 626 } 627 628 copy(q.headerResults[request.From-q.headerOffset:], headers) 629 delete(q.headerTaskPool, request.From) 630 631 ready := 0 632 for q.headerProced+ready < len(q.headerResults) && q.headerResults[q.headerProced+ready] != nil { 633 ready += MaxHeaderFetch 634 } 635 if ready > 0 { 636 637 process := make([]*types.Header, ready) 638 copy(process, q.headerResults[q.headerProced:q.headerProced+ready]) 639 640 select { 641 case headerProcCh <- process: 642 log.Trace("Pre-scheduled new headers", "peer", id, "count", len(process), "from", process[0].Number) 643 q.headerProced += len(process) 644 default: 645 } 646 } 647 648 if len(q.headerTaskPool) == 0 { 649 q.headerContCh <- false 650 } 651 return len(headers), nil 652 } 653 654 func (q *queue) DeliverBodies(id string, txLists [][]*types.Transaction, uncleLists [][]*types.Header) (int, error) { 655 q.lock.Lock() 656 defer q.lock.Unlock() 657 658 reconstruct := func(header *types.Header, index int, result *fetchResult) error { 659 if types.DeriveSha(types.Transactions(txLists[index])) != header.TxHash || types.CalcUncleHash(uncleLists[index]) != header.UncleHash { 660 return errInvalidBody 661 } 662 result.Transactions = txLists[index] 663 result.Uncles = uncleLists[index] 664 return nil 665 } 666 return q.deliver(id, q.blockTaskPool, q.blockTaskQueue, q.blockPendPool, q.blockDonePool, bodyReqTimer, len(txLists), reconstruct) 667 } 668 669 func (q *queue) DeliverReceipts(id string, receiptList [][]*types.Receipt) (int, error) { 670 q.lock.Lock() 671 defer q.lock.Unlock() 672 673 reconstruct := func(header *types.Header, index int, result *fetchResult) error { 674 if types.DeriveSha(types.Receipts(receiptList[index])) != header.ReceiptHash { 675 return errInvalidReceipt 676 } 677 result.Receipts = receiptList[index] 678 return nil 679 } 680 return q.deliver(id, q.receiptTaskPool, q.receiptTaskQueue, q.receiptPendPool, q.receiptDonePool, receiptReqTimer, len(receiptList), reconstruct) 681 } 682 683 func (q *queue) deliver(id string, taskPool map[common.Hash]*types.Header, taskQueue *prque.Prque, 684 pendPool map[string]*fetchRequest, donePool map[common.Hash]struct{}, reqTimer metrics.Timer, 685 results int, reconstruct func(header *types.Header, index int, result *fetchResult) error) (int, error) { 686 687 request := pendPool[id] 688 if request == nil { 689 return 0, errNoFetchesPending 690 } 691 reqTimer.UpdateSince(request.Time) 692 delete(pendPool, id) 693 694 if results == 0 { 695 for _, header := range request.Headers { 696 request.Peer.MarkLacking(header.Hash()) 697 } 698 } 699 700 var ( 701 accepted int 702 failure error 703 useful bool 704 ) 705 for i, header := range request.Headers { 706 707 if i >= results { 708 break 709 } 710 711 index := int(header.Number.Int64() - int64(q.resultOffset)) 712 if index >= len(q.resultCache) || index < 0 || q.resultCache[index] == nil { 713 failure = errInvalidChain 714 break 715 } 716 if err := reconstruct(header, i, q.resultCache[index]); err != nil { 717 failure = err 718 break 719 } 720 hash := header.Hash() 721 722 donePool[hash] = struct{}{} 723 q.resultCache[index].Pending-- 724 useful = true 725 accepted++ 726 727 request.Headers[i] = nil 728 delete(taskPool, hash) 729 } 730 731 for _, header := range request.Headers { 732 if header != nil { 733 taskQueue.Push(header, -int64(header.Number.Uint64())) 734 } 735 } 736 737 if accepted > 0 { 738 q.active.Signal() 739 } 740 741 switch { 742 case failure == nil || failure == errInvalidChain: 743 return accepted, failure 744 case useful: 745 return accepted, fmt.Errorf("partial failure: %v", failure) 746 default: 747 return accepted, errStaleDelivery 748 } 749 } 750 751 func (q *queue) Prepare(offset uint64, mode SyncMode) { 752 q.lock.Lock() 753 defer q.lock.Unlock() 754 755 if q.resultOffset < offset { 756 q.resultOffset = offset 757 } 758 q.mode = mode 759 }