-
Notifications
You must be signed in to change notification settings - Fork 287
/
manager.go
2034 lines (1787 loc) · 67.6 KB
/
manager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2013-2016 The btcsuite developers
// Copyright (c) 2015-2023 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package netsync
import (
"context"
"errors"
"fmt"
"math"
"sync"
"time"
"github.com/decred/dcrd/blockchain/stake/v5"
"github.com/decred/dcrd/chaincfg/chainhash"
"github.com/decred/dcrd/chaincfg/v3"
"github.com/decred/dcrd/container/apbf"
"github.com/decred/dcrd/database/v3"
"github.com/decred/dcrd/dcrutil/v4"
"github.com/decred/dcrd/internal/blockchain"
"github.com/decred/dcrd/internal/mempool"
"github.com/decred/dcrd/internal/progresslog"
"github.com/decred/dcrd/math/uint256"
"github.com/decred/dcrd/mixing"
"github.com/decred/dcrd/mixing/mixpool"
peerpkg "github.com/decred/dcrd/peer/v3"
"github.com/decred/dcrd/wire"
)
const (
// minInFlightBlocks is the minimum number of blocks that should be
// in the request queue before requesting more.
minInFlightBlocks = 10
// maxInFlightBlocks is the maximum number of blocks to allow in the sync
// peer request queue.
maxInFlightBlocks = 16
// maxRejectedTxns specifies the maximum number of recently rejected
// transactions to track. This is primarily used to avoid wasting a bunch
// of bandwidth from requesting transactions that are already known to be
// invalid again from multiple peers, however, it also doubles as DoS
// protection against malicious peers.
//
// Recall that there are 125 connection slots by default. Assuming the
// default setting of 8 outbound connections, which attackers cannot
// control, that leaves 117 max inbound connections which could potentially
// be malicious. maxRejectedTxns is set to target tracking the maximum
// number of rejected transactions that would result from 120 connections
// with malicious peers. 120 is used since it is strictly greater than the
// aforementioned 117 max inbound connections while still providing for the
// possibility of a few happenstance malicious outbound connections as well.
//
// It's also worth noting that even if attackers were to manage to exceed
// the configured value, the result is not catastrophic as it would only
// result in increased bandwidth usage versus not exceeding it.
//
// rejectedTxnsFPRate is the false positive rate to use for the APBF used to
// track recently rejected transactions. It is set to a rate of 1 per 10
// million to make it incredibly unlikely that any transactions that haven't
// actually been rejected are incorrectly treated as if they had.
//
// These values result in about 568 KiB memory usage including overhead.
maxRejectedTxns = 62500
rejectedTxnsFPRate = 0.0000001
// maxRejectedMixMsgs specifies the maximum number of recently
// rejected mixing messages to track, and rejectedMixMsgsFPRate is the
// false positive rate for the APBF. These values have not been tuned
// specifically for the mixing messages, and the equivalent constants
// for handling rejected transactions are used.
maxRejectedMixMsgs = maxRejectedTxns
rejectedMixMsgsFPRate = rejectedTxnsFPRate
// maxRequestedBlocks is the maximum number of requested block
// hashes to store in memory.
maxRequestedBlocks = wire.MaxInvPerMsg
// maxRequestedTxns is the maximum number of requested transactions
// hashes to store in memory.
maxRequestedTxns = wire.MaxInvPerMsg
// maxRequestedMixMsgs is the maximum number of hashes of in-flight
// mixing messages.
maxRequestedMixMsgs = wire.MaxInvPerMsg
// maxExpectedHeaderAnnouncementsPerMsg is the maximum number of headers in
// a single message that is expected when determining when the message
// appears to be announcing new blocks.
maxExpectedHeaderAnnouncementsPerMsg = 12
// maxConsecutiveOrphanHeaders is the maximum number of consecutive header
// messages that contain headers which do not connect a peer can send before
// it is deemed to have diverged so far it is no longer useful.
maxConsecutiveOrphanHeaders = 10
// headerSyncStallTimeoutSecs is the number of seconds to wait for progress
// during the header sync process before stalling the sync and disconnecting
// the peer.
headerSyncStallTimeoutSecs = (3 + wire.MaxBlockHeadersPerMsg/1000) * 2
)
// zeroHash is the zero value hash (all zeros). It is defined as a convenience.
var zeroHash chainhash.Hash
// peerConnectedMsg signifies a newly connected peer to the event handler.
type peerConnectedMsg struct {
peer *Peer
}
// blockMsg packages a Decred block message and the peer it came from together
// so the event handler has access to that information.
type blockMsg struct {
block *dcrutil.Block
peer *Peer
reply chan struct{}
}
// invMsg packages a Decred inv message and the peer it came from together
// so the event handler has access to that information.
type invMsg struct {
inv *wire.MsgInv
peer *Peer
}
// headersMsg packages a Decred headers message and the peer it came from
// together so the event handler has access to that information.
type headersMsg struct {
headers *wire.MsgHeaders
peer *Peer
}
// notFoundMsg packages a Decred notfound message and the peer it came from
// together so the event handler has access to that information.
type notFoundMsg struct {
notFound *wire.MsgNotFound
peer *Peer
}
// peerDisconnectedMsg signifies a newly disconnected peer to the event handler.
type peerDisconnectedMsg struct {
peer *Peer
}
// txMsg packages a Decred tx message and the peer it came from together
// so the event handler has access to that information.
type txMsg struct {
tx *dcrutil.Tx
peer *Peer
reply chan struct{}
}
// getSyncPeerMsg is a message type to be sent across the message channel for
// retrieving the current sync peer.
type getSyncPeerMsg struct {
reply chan int32
}
// requestFromPeerMsg is a message type to be sent across the message channel
// for requesting either blocks or transactions from a given peer. It routes
// this through the sync manager so the sync manager doesn't ban the peer
// when it sends this information back.
type requestFromPeerMsg struct {
peer *Peer
blocks []chainhash.Hash
voteHashes []chainhash.Hash
tSpendHashes []chainhash.Hash
mixHashes []chainhash.Hash
reply chan requestFromPeerResponse
}
// requestFromPeerResponse is a response sent to the reply channel of a
// requestFromPeerMsg query.
type requestFromPeerResponse struct {
err error
}
// processBlockResponse is a response sent to the reply channel of a
// processBlockMsg.
type processBlockResponse struct {
forkLen int64
err error
}
// processBlockMsg is a message type to be sent across the message channel
// for requested a block is processed. Note this call differs from blockMsg
// above in that blockMsg is intended for blocks that came from peers and have
// extra handling whereas this message essentially is just a concurrent safe
// way to call ProcessBlock on the internal block chain instance.
type processBlockMsg struct {
block *dcrutil.Block
reply chan processBlockResponse
}
// mixMsg is a message type to be sent across the message channel for requesting
// a message's acceptance to the mixing pool.
type mixMsg struct {
msg mixing.Message
peer *Peer
reply chan error
}
// Peer extends a common peer to maintain additional state needed by the sync
// manager. The internals are intentionally unexported to create an opaque
// type.
type Peer struct {
*peerpkg.Peer
syncCandidate bool
requestedTxns map[chainhash.Hash]struct{}
requestedBlocks map[chainhash.Hash]struct{}
requestedMixMsgs map[chainhash.Hash]struct{}
// initialStateRequested tracks whether or not the initial state data has
// been requested from the peer.
initialStateRequested bool
// numConsecutiveOrphanHeaders tracks the number of consecutive header
// messages sent by the peer that contain headers which do not connect. It
// is used to detect peers that have either diverged so far they are no
// longer useful or are otherwise being malicious.
numConsecutiveOrphanHeaders int32
lastAnnouncedBlock *chainhash.Hash
}
// NewPeer returns a new instance of a peer that wraps the provided underlying
// common peer with additional state that is used throughout the package.
func NewPeer(peer *peerpkg.Peer) *Peer {
isSyncCandidate := peer.Services()&wire.SFNodeNetwork == wire.SFNodeNetwork
return &Peer{
Peer: peer,
syncCandidate: isSyncCandidate,
requestedTxns: make(map[chainhash.Hash]struct{}),
requestedBlocks: make(map[chainhash.Hash]struct{}),
requestedMixMsgs: make(map[chainhash.Hash]struct{}),
}
}
// headerSyncState houses the state used to track the header sync progress and
// related stall handling.
type headerSyncState struct {
// headersSynced tracks whether or not the headers are synced to a point
// that is recent enough to start downloading blocks.
headersSynced bool
// These fields are used to implement a progress stall timeout that can be
// reset at any time without needing to create a new one and the associated
// extra garbage.
//
// stallTimer is an underlying timer that is used to implement the timeout.
//
// stallChanDrained indicates whether or not the channel for the stall timer
// has already been read and is used when resetting the timer to ensure the
// channel is drained when the timer is stopped as described in the timer
// documentation.
stallTimer *time.Timer
stallChanDrained bool
}
// makeHeaderSyncState returns a header sync state that is ready to use.
func makeHeaderSyncState() headerSyncState {
stallTimer := time.NewTimer(math.MaxInt64)
stallTimer.Stop()
return headerSyncState{
stallTimer: stallTimer,
stallChanDrained: true,
}
}
// stopStallTimeout stops the progress stall timer while ensuring to read from
// the timer's channel in the case the timer already expired which can happen
// due to the fact the stop happens in between channel reads. This behavior is
// well documented in the Timer docs.
//
// NOTE: This function must not be called concurrent with any other receives on
// the timer's channel.
func (state *headerSyncState) stopStallTimeout() {
t := state.stallTimer
if !t.Stop() && !state.stallChanDrained {
<-t.C
}
state.stallChanDrained = true
}
// resetStallTimeout resets the progress stall timer while ensuring to read from
// the timer's channel in the case the timer already expired which can happen
// due to the fact the reset happens in between channel reads. This behavior is
// well documented in the Timer docs.
//
// NOTE: This function must not be called concurrent with any other receives on
// the timer's channel.
func (state *headerSyncState) resetStallTimeout() {
state.stopStallTimeout()
state.stallTimer.Reset(headerSyncStallTimeoutSecs * time.Second)
state.stallChanDrained = false
}
// SyncManager provides a concurrency safe sync manager for handling all
// incoming blocks.
type SyncManager struct {
// quit is used for lifecycle management of the sync manager.
quit chan struct{}
// cfg specifies the configuration of the sync manager and is set at
// creation time and treated as immutable after that.
cfg Config
// minKnownWork houses the minimum known work from the associated network
// params converted to a uint256 so the conversion only needs to be
// performed once when the sync manager is initialized. Ideally, the chain
// params should be updated to use the new type, but that will be a major
// version bump, so a one-time conversion is a good tradeoff in the mean
// time.
minKnownWork *uint256.Uint256
rejectedTxns *apbf.Filter
rejectedMixMsgs *apbf.Filter
requestedTxns map[chainhash.Hash]struct{}
requestedBlocks map[chainhash.Hash]struct{}
requestedMixMsgs map[chainhash.Hash]struct{}
progressLogger *progresslog.Logger
syncPeer *Peer
msgChan chan interface{}
peers map[*Peer]struct{}
// hdrSyncState houses the state used to track the initial header sync
// process and related stall handling.
hdrSyncState headerSyncState
// The following fields are used to track the height being synced to from
// peers.
syncHeightMtx sync.Mutex
syncHeight int64
// The following fields are used to track whether or not the manager
// believes it is fully synced to the network.
isCurrentMtx sync.Mutex
isCurrent bool
// The following fields are used to track the list of the next blocks to
// download in the branch leading up to the best known header.
//
// nextBlocksHeader is the hash of the best known header when the list was
// last updated.
//
// nextBlocksBuf houses an overall list of blocks needed (up to the size of
// the array) regardless of whether or not they have been requested and
// provides what is effectively a reusable lookahead buffer. Note that
// since it is a fixed size and acts as a backing array, not all entries
// will necessarily refer to valid data, especially once the chain is
// synced. nextNeededBlocks slices into the valid part of the array.
//
// nextNeededBlocks subslices into nextBlocksBuf such that it provides an
// upper bound on the entries of the backing array that are valid and also
// acts as a list of needed blocks that are not already known to be in
// flight.
nextBlocksHeader chainhash.Hash
nextBlocksBuf [512]chainhash.Hash
nextNeededBlocks []chainhash.Hash
}
// SyncHeight returns latest known block being synced to.
func (m *SyncManager) SyncHeight() int64 {
m.syncHeightMtx.Lock()
syncHeight := m.syncHeight
m.syncHeightMtx.Unlock()
return syncHeight
}
// chainBlockLocatorToHashes converts a block locator from chain to a slice
// of hashes.
func chainBlockLocatorToHashes(locator blockchain.BlockLocator) []chainhash.Hash {
if len(locator) == 0 {
return nil
}
result := make([]chainhash.Hash, 0, len(locator))
for _, hash := range locator {
result = append(result, *hash)
}
return result
}
// maybeUpdateNextNeededBlocks potentially updates the list of the next blocks
// to download in the branch leading up to the best known header.
//
// This function is NOT safe for concurrent access. It must be called from the
// event handler goroutine.
func (m *SyncManager) maybeUpdateNextNeededBlocks() {
// Update the list if the best known header changed since the last time it
// was updated or it is not empty, is getting short, and does not already
// end at the best known header.
chain := m.cfg.Chain
bestHeader, _ := chain.BestHeader()
numNeeded := len(m.nextNeededBlocks)
needsUpdate := m.nextBlocksHeader != bestHeader || (numNeeded > 0 &&
numNeeded < minInFlightBlocks &&
m.nextNeededBlocks[numNeeded-1] != bestHeader)
if needsUpdate {
m.nextNeededBlocks = chain.PutNextNeededBlocks(m.nextBlocksBuf[:])
m.nextBlocksHeader = bestHeader
}
}
// fetchNextBlocks creates and sends a request to the provided peer for the next
// blocks to be downloaded based on the current headers.
func (m *SyncManager) fetchNextBlocks(peer *Peer) {
// Nothing to do if the target maximum number of blocks to request from the
// peer at the same time are already in flight.
numInFlight := len(peer.requestedBlocks)
if numInFlight >= maxInFlightBlocks {
return
}
// Potentially update the list of the next blocks to download in the branch
// leading up to the best known header.
m.maybeUpdateNextNeededBlocks()
// Build and send a getdata request for the needed blocks.
numNeeded := len(m.nextNeededBlocks)
if numNeeded == 0 {
return
}
maxNeeded := maxInFlightBlocks - numInFlight
if numNeeded > maxNeeded {
numNeeded = maxNeeded
}
gdmsg := wire.NewMsgGetDataSizeHint(uint(numNeeded))
for i := 0; i < numNeeded && len(gdmsg.InvList) < wire.MaxInvPerMsg; i++ {
// The block is either going to be skipped because it has already been
// requested or it will be requested, but in either case, the block is
// no longer needed for future iterations.
hash := &m.nextNeededBlocks[0]
m.nextNeededBlocks = m.nextNeededBlocks[1:]
// Skip blocks that have already been requested. The needed blocks
// might have been updated above thereby potentially repopulating some
// blocks that are still in flight.
if _, ok := m.requestedBlocks[*hash]; ok {
continue
}
iv := wire.NewInvVect(wire.InvTypeBlock, hash)
m.requestedBlocks[*hash] = struct{}{}
peer.requestedBlocks[*hash] = struct{}{}
gdmsg.AddInvVect(iv)
}
if len(gdmsg.InvList) > 0 {
peer.QueueMessage(gdmsg, nil)
}
}
// startSync will choose the best peer among the available candidate peers to
// download/sync the blockchain from. When syncing is already running, it
// simply returns. It also examines the candidates for any which are no longer
// candidates and removes them as needed.
func (m *SyncManager) startSync() {
// Nothing more to do when already syncing.
if m.syncPeer != nil {
return
}
chain := m.cfg.Chain
best := chain.BestSnapshot()
var bestPeer *Peer
for peer := range m.peers {
if !peer.syncCandidate {
continue
}
// Remove sync candidate peers that are no longer candidates due
// to passing their latest known block. NOTE: The < is
// intentional as opposed to <=. While technically the peer
// doesn't have a later block when it's equal, it will likely
// have one soon so it is a reasonable choice. It also allows
// the case where both are at 0 such as during regression test.
if peer.LastBlock() < best.Height {
peer.syncCandidate = false
continue
}
// The best sync candidate is the most updated peer.
if bestPeer == nil {
bestPeer = peer
}
if bestPeer.LastBlock() < peer.LastBlock() {
bestPeer = peer
}
}
// Update the state of whether or not the manager believes the chain is
// fully synced to whatever the chain believes when there is no candidate
// for a sync peer.
//
// Also, return now when there isn't a sync peer candidate as there is
// nothing more to do without one.
if bestPeer == nil {
m.isCurrentMtx.Lock()
m.isCurrent = chain.IsCurrent()
m.isCurrentMtx.Unlock()
log.Warnf("No sync peer candidates available")
return
}
// Start syncing from the best peer.
// Clear the requestedBlocks if the sync peer changes, otherwise
// we may ignore blocks we need that the last sync peer failed
// to send.
m.requestedBlocks = make(map[chainhash.Hash]struct{})
syncHeight := bestPeer.LastBlock()
headersSynced := m.hdrSyncState.headersSynced
if !headersSynced {
log.Infof("Syncing headers to block height %d from peer %v", syncHeight,
bestPeer)
}
// The chain is not synced whenever the current best height is less than the
// height to sync to.
if best.Height < syncHeight {
m.isCurrentMtx.Lock()
m.isCurrent = false
m.isCurrentMtx.Unlock()
}
// Request headers to discover any blocks that are not already known
// starting from the parent of the best known header for the local chain.
// The parent is used as a means to accurately discover the best known block
// of the remote peer in the case both tips are the same where it would
// otherwise result in an empty response.
bestHeaderHash, _ := chain.BestHeader()
parentHash := bestHeaderHash
header, err := chain.HeaderByHash(&bestHeaderHash)
if err == nil {
parentHash = header.PrevBlock
}
blkLocator := chain.BlockLocatorFromHash(&parentHash)
locator := chainBlockLocatorToHashes(blkLocator)
bestPeer.PushGetHeadersMsg(locator, &zeroHash)
// Track the sync peer and update the sync height when it is higher than the
// currently best known value.
m.syncPeer = bestPeer
m.syncHeightMtx.Lock()
if syncHeight > m.syncHeight {
m.syncHeight = syncHeight
}
m.syncHeightMtx.Unlock()
// Start the header sync progress stall timeout when the initial headers
// sync is not already done.
if !headersSynced {
m.hdrSyncState.resetStallTimeout()
}
// Download any blocks needed to catch the local chain up to the best
// known header (if any) when the initial headers sync is already done.
//
// This is done in addition to the header request above to avoid waiting
// for the round trip when there are still blocks that are needed
// regardless of the headers response.
if headersSynced {
m.fetchNextBlocks(m.syncPeer)
}
}
// maybeRequestInitialState potentially requests initial state information from
// the provided peer by sending it an appropriate initial state sync message
// dependending on the protocol version.
//
// The request will not be sent more than once or when the peer is in the
// process of being removed.
func maybeRequestInitialState(peer *Peer) {
// Don't request the initial state more than once or when the peer is in the
// process of being removed.
if peer.initialStateRequested || !peer.Connected() {
return
}
// Choose which initial state sync p2p messages to use based on the protocol
// version. Protocol versions prior to the init state version use
// getminingstate and miningstate while those after use getinitstate and
// initstate.
var msg wire.Message
if peer.ProtocolVersion() < wire.InitStateVersion {
msg = wire.NewMsgGetMiningState()
} else {
m := wire.NewMsgGetInitState()
err := m.AddTypes(wire.InitStateHeadBlocks,
wire.InitStateHeadBlockVotes,
wire.InitStateTSpends)
if err != nil {
log.Errorf("Unexpected error building getinitstate msg: %v", err)
return
}
msg = m
}
peer.QueueMessage(msg, nil)
peer.initialStateRequested = true
}
// onInitialChainSyncDone is invoked when the initial chain sync process
// completes.
func (m *SyncManager) onInitialChainSyncDone() {
best := m.cfg.Chain.BestSnapshot()
log.Infof("Initial chain sync complete (hash %s, height %d)",
best.Hash, best.Height)
// Request initial state from all peers that are marked as needing it now
// that the initial chain sync is done when enabled.
if !m.cfg.NoMiningStateSync {
for peer := range m.peers {
maybeRequestInitialState(peer)
}
}
}
// handlePeerConnectedMsg deals with new peers that have signalled they may
// be considered as a sync peer (they have already successfully negotiated). It
// also starts syncing if needed. It is invoked from the syncHandler goroutine.
func (m *SyncManager) handlePeerConnectedMsg(ctx context.Context, peer *Peer) {
select {
case <-ctx.Done():
default:
}
log.Infof("New valid peer %s (%s)", peer, peer.UserAgent())
m.peers[peer] = struct{}{}
// Start syncing by choosing the best candidate if needed.
if peer.syncCandidate && m.syncPeer == nil {
m.startSync()
}
// Request the initial state from this peer now when enabled and the manager
// believes the chain is fully synced. Otherwise, it will be requested when
// the initial chain sync process is complete.
if !m.cfg.NoMiningStateSync && m.IsCurrent() {
maybeRequestInitialState(peer)
}
}
// handlePeerDisconnectedMsg deals with peers that have signalled they are done.
// It removes the peer as a candidate for syncing and in the case where it was
// the current sync peer, attempts to select a new best peer to sync from. It
// is invoked from the syncHandler goroutine.
func (m *SyncManager) handlePeerDisconnectedMsg(peer *Peer) {
// Remove the peer from the list of candidate peers.
delete(m.peers, peer)
// Re-request in-flight blocks and transactions that were not received
// by the disconnected peer if the data was announced by another peer.
// Remove the data from the manager's requested data maps if no other
// peers have announced the data.
requestQueues := make(map[*Peer][]wire.InvVect)
var inv wire.InvVect
inv.Type = wire.InvTypeTx
TxHashes:
for txHash := range peer.requestedTxns {
inv.Hash = txHash
for pp := range m.peers {
if !pp.IsKnownInventory(&inv) {
continue
}
invs := append(requestQueues[pp], inv)
requestQueues[pp] = invs
pp.requestedTxns[txHash] = struct{}{}
continue TxHashes
}
// No peers found that have announced this data.
delete(m.requestedTxns, txHash)
}
inv.Type = wire.InvTypeBlock
BlockHashes:
for blockHash := range peer.requestedBlocks {
inv.Hash = blockHash
for pp := range m.peers {
if !pp.IsKnownInventory(&inv) {
continue
}
invs := append(requestQueues[pp], inv)
requestQueues[pp] = invs
pp.requestedBlocks[blockHash] = struct{}{}
continue BlockHashes
}
// No peers found that have announced this data.
delete(m.requestedBlocks, blockHash)
}
inv.Type = wire.InvTypeMix
MixHashes:
for mixHash := range peer.requestedMixMsgs {
inv.Hash = mixHash
for pp := range m.peers {
if !pp.IsKnownInventory(&inv) {
continue
}
invs := append(requestQueues[pp], inv)
requestQueues[pp] = invs
pp.requestedMixMsgs[mixHash] = struct{}{}
continue MixHashes
}
// No peers found that have announced this data.
delete(m.requestedMixMsgs, mixHash)
}
for pp, requestQueue := range requestQueues {
var numRequested int32
gdmsg := wire.NewMsgGetData()
for i := range requestQueue {
// Note the copy is intentional here to avoid keeping a reference
// into the request queue map from the message queue since that
// reference could potentially prevent the map from being garbage
// collected for an extended period of time.
ivCopy := requestQueue[i]
gdmsg.AddInvVect(&ivCopy)
numRequested++
if numRequested == wire.MaxInvPerMsg {
// Send full getdata message and reset.
pp.QueueMessage(gdmsg, nil)
gdmsg = wire.NewMsgGetData()
numRequested = 0
}
}
if len(gdmsg.InvList) > 0 {
pp.QueueMessage(gdmsg, nil)
}
}
// Attempt to find a new peer to sync from and reset the final requested
// block when the quitting peer is the sync peer.
if m.syncPeer == peer {
m.syncPeer = nil
m.startSync()
}
}
// handleTxMsg handles transaction messages from all peers.
func (m *SyncManager) handleTxMsg(tmsg *txMsg) {
peer := tmsg.peer
// NOTE: BitcoinJ, and possibly other wallets, don't follow the spec of
// sending an inventory message and allowing the remote peer to decide
// whether or not they want to request the transaction via a getdata
// message. Unfortunately, the reference implementation permits
// unrequested data, so it has allowed wallets that don't follow the
// spec to proliferate. While this is not ideal, there is no check here
// to disconnect peers for sending unsolicited transactions to provide
// interoperability.
txHash := tmsg.tx.Hash()
// Ignore transactions that have already been rejected. The transaction was
// unsolicited if it was already previously rejected.
if m.rejectedTxns.Contains(txHash[:]) {
log.Debugf("Ignoring unsolicited previously rejected transaction %v "+
"from %s", txHash, peer)
return
}
// Process the transaction to include validation, insertion in the
// memory pool, orphan handling, etc.
allowOrphans := m.cfg.MaxOrphanTxs > 0
acceptedTxs, err := m.cfg.TxMemPool.ProcessTransaction(tmsg.tx,
allowOrphans, true, mempool.Tag(peer.ID()))
// Remove transaction from request maps. Either the mempool/chain
// already knows about it and as such we shouldn't have any more
// instances of trying to fetch it, or we failed to insert and thus
// we'll retry next time we get an inv.
delete(peer.requestedTxns, *txHash)
delete(m.requestedTxns, *txHash)
if err != nil {
// Do not request this transaction again until a new block has been
// processed.
m.rejectedTxns.Add(txHash[:])
// When the error is a rule error, it means the transaction was
// simply rejected as opposed to something actually going wrong,
// so log it as such. Otherwise, something really did go wrong,
// so log it as an actual error.
var rErr mempool.RuleError
if errors.As(err, &rErr) {
log.Debugf("Rejected transaction %v from %s: %v", txHash, peer, err)
} else {
log.Errorf("Failed to process transaction %v: %v", txHash, err)
}
return
}
m.cfg.PeerNotifier.AnnounceNewTransactions(acceptedTxs)
}
// handleMixMsg handles mixing messages from all peers.
func (m *SyncManager) handleMixMsg(mmsg *mixMsg) error {
peer := mmsg.peer
mixHash := mmsg.msg.Hash()
// Ignore transactions that have already been rejected. The transaction was
// unsolicited if it was already previously rejected.
if m.rejectedMixMsgs.Contains(mixHash[:]) {
log.Debugf("Ignoring unsolicited previously rejected mix message %v "+
"from %s", &mixHash, peer)
return nil
}
accepted, err := m.cfg.MixPool.AcceptMessage(mmsg.msg)
// Remove message from request maps. Either the mixpool already knows
// about it and as such we shouldn't have any more instances of trying
// to fetch it, or we failed to insert and thus we'll retry next time
// we get an inv.
delete(peer.requestedMixMsgs, mixHash)
delete(m.requestedMixMsgs, mixHash)
if err != nil {
// Do not request this message again until a new block has
// been processed. If the message is an orphan KE, it is
// tracked internally by mixpool as an orphan; there is no
// need to request it again after requesting the unknown PR.
m.rejectedMixMsgs.Add(mixHash[:])
// When the error is a rule error, it means the message was
// simply rejected as opposed to something actually going wrong,
// so log it as such.
//
// When the error is an orphan KE with unknown PR, the PR will be
// requested from the peer submitting the KE. This is a normal
// occurrence, and will be logged at debug instead at error level.
//
// Otherwise, something really did go wrong, so log it as an
// actual error.
var rErr *mixpool.RuleError
var missingPRErr *mixpool.MissingOwnPRError
if errors.As(err, &rErr) || errors.As(err, &missingPRErr) {
log.Debugf("Rejected %T mixing message %v from %s: %v",
mmsg.msg, &mixHash, peer, err)
} else {
log.Errorf("Failed to process %T mixing message %v: %v",
mmsg.msg, &mixHash, err)
}
return err
}
if len(accepted) == 0 {
return nil
}
m.cfg.PeerNotifier.AnnounceMixMessages(accepted)
return nil
}
// maybeUpdateIsCurrent potentially updates the manager to signal it believes
// the chain is considered synced.
//
// This function MUST be called with the is current mutex held (for writes).
func (m *SyncManager) maybeUpdateIsCurrent() {
// Nothing to do when already considered synced.
if m.isCurrent {
return
}
// The chain is considered synced once both the blockchain believes it is
// current and the sync height is reached or exceeded.
best := m.cfg.Chain.BestSnapshot()
syncHeight := m.SyncHeight()
if best.Height >= syncHeight && m.cfg.Chain.IsCurrent() {
m.isCurrent = true
}
}
// processBlock processes the provided block using the internal chain instance.
//
// When no errors occurred during processing, the first return value indicates
// the length of the fork the block extended. In the case it either extended
// the best chain or is now the tip of the best chain due to causing a
// reorganize, the fork length will be 0. Orphans are rejected and can be
// detected by checking if the error is blockchain.ErrMissingParent.
func (m *SyncManager) processBlock(block *dcrutil.Block) (int64, error) {
// Process the block to include validation, best chain selection, etc.
forkLen, err := m.cfg.Chain.ProcessBlock(block)
if err != nil {
return 0, err
}
// Update the sync height when the block is higher than the currently best
// known value and it extends the main chain.
onMainChain := forkLen == 0
if onMainChain {
m.syncHeightMtx.Lock()
blockHeight := int64(block.MsgBlock().Header.Height)
if blockHeight > m.syncHeight {
m.syncHeight = blockHeight
}
m.syncHeightMtx.Unlock()
}
m.isCurrentMtx.Lock()
m.maybeUpdateIsCurrent()
m.isCurrentMtx.Unlock()
return forkLen, nil
}
// handleBlockMsg handles block messages from all peers.
func (m *SyncManager) handleBlockMsg(bmsg *blockMsg) {
peer := bmsg.peer
// The remote peer is misbehaving when the block was not requested.
blockHash := bmsg.block.Hash()
if _, exists := peer.requestedBlocks[*blockHash]; !exists {
log.Warnf("Got unrequested block %v from %s -- disconnecting",
blockHash, peer)
peer.Disconnect()
return
}
// Save whether or not the chain believes it is current prior to processing
// the block for use below in determining logging behavior.
chain := m.cfg.Chain
wasChainCurrent := chain.IsCurrent()
// Process the block to include validation, best chain selection, etc.
//
// Also, remove the block from the request maps once it has been processed.
// This ensures chain is aware of the block before it is removed from the
// maps in order to help prevent duplicate requests.
forkLen, err := m.processBlock(bmsg.block)
delete(peer.requestedBlocks, *blockHash)
delete(m.requestedBlocks, *blockHash)
if err != nil {
// Ideally there should never be any requests for duplicate blocks, but
// ignore any that manage to make it through.
if errors.Is(err, blockchain.ErrDuplicateBlock) {
return
}
// When the error is a rule error, it means the block was simply
// rejected as opposed to something actually going wrong, so log it as
// such. Otherwise, something really did go wrong, so log it as an
// actual error.
//
// Note that orphan blocks are never requested so there is no need to
// test for that rule error separately.
var rErr blockchain.RuleError
if errors.As(err, &rErr) {
log.Infof("Rejected block %v from %s: %v", blockHash, peer, err)
} else {
log.Errorf("Failed to process block %v: %v", blockHash, err)
}
if errors.Is(err, database.ErrCorruption) ||
errors.Is(err, blockchain.ErrUtxoBackendCorruption) {
log.Errorf("Critical failure: %v", err)
}
return
}
// Log information about the block. Use the progress logger when the chain
// was not already current prior to processing the block to provide nicer
// periodic logging with a progress percentage. Otherwise, log the block
// individually along with some stats.
msgBlock := bmsg.block.MsgBlock()
header := &msgBlock.Header
if !wasChainCurrent {
forceLog := int64(header.Height) >= m.SyncHeight()
m.progressLogger.LogProgress(msgBlock, forceLog, chain.VerifyProgress)
if chain.IsCurrent() {
m.onInitialChainSyncDone()
}
} else {
var interval string
prevBlockHeader, err := chain.HeaderByHash(&header.PrevBlock)
if err == nil {
diff := header.Timestamp.Sub(prevBlockHeader.Timestamp)
interval = ", interval " + diff.Round(time.Second).String()
}
numTxns := uint64(len(msgBlock.Transactions))
numTickets := uint64(header.FreshStake)
numVotes := uint64(header.Voters)
numRevokes := uint64(header.Revocations)
log.Infof("New block %s (%d %s, %d %s, %d %s, %d %s, height %d%s)",
blockHash, numTxns, pickNoun(numTxns, "transaction", "transactions"),
numTickets, pickNoun(numTickets, "ticket", "tickets"),
numVotes, pickNoun(numVotes, "vote", "votes"),
numRevokes, pickNoun(numRevokes, "revocation", "revocations"),
header.Height, interval)
}
// Perform some additional processing when the block extended the main
// chain.
onMainChain := forkLen == 0