5959 errNetworkLayerUnhealthy = errors .New ("network layer is unhealthy" )
6060)
6161
62- // Network Upgrade
63- var minimumUnmaskedVersion = version .NewDefaultVersion (constants .PlatformName , 1 , 1 , 0 )
64-
6562func init () { rand .Seed (time .Now ().UnixNano ()) }
6663
6764// Network defines the functionality of the networking library.
@@ -118,7 +115,7 @@ type network struct {
118115 id ids.ShortID
119116 ip utils.DynamicIPDesc
120117 networkID uint32
121- msgVersion version.Version
118+ versionCompatibility version.Compatibility
122119 parser version.Parser
123120 listener net.Listener
124121 dialer Dialer
@@ -149,7 +146,6 @@ type network struct {
149146 connMeterMaxConns int
150147 connMeter ConnMeter
151148 b Builder
152- apricotPhase0Time time.Time
153149
154150 // stateLock should never be held when grabbing a peer senderLock
155151 stateLock sync.RWMutex
@@ -215,7 +211,7 @@ func NewDefaultNetwork(
215211 id ids.ShortID ,
216212 ip utils.DynamicIPDesc ,
217213 networkID uint32 ,
218- version version.Version ,
214+ versionCompatibility version.Compatibility ,
219215 parser version.Parser ,
220216 listener net.Listener ,
221217 dialer Dialer ,
@@ -230,7 +226,6 @@ func NewDefaultNetwork(
230226 restartOnDisconnected bool ,
231227 disconnectedCheckFreq time.Duration ,
232228 disconnectedRestartTimeout time.Duration ,
233- apricotPhase0Time time.Time ,
234229 sendQueueSize uint32 ,
235230 healthConfig HealthConfig ,
236231 benchlistManager benchlist.Manager ,
@@ -242,7 +237,7 @@ func NewDefaultNetwork(
242237 id ,
243238 ip ,
244239 networkID ,
245- version ,
240+ versionCompatibility ,
246241 parser ,
247242 listener ,
248243 dialer ,
@@ -275,7 +270,6 @@ func NewDefaultNetwork(
275270 restartOnDisconnected ,
276271 disconnectedCheckFreq ,
277272 disconnectedRestartTimeout ,
278- apricotPhase0Time ,
279273 healthConfig ,
280274 benchlistManager ,
281275 peerAliasTimeout ,
@@ -289,7 +283,7 @@ func NewNetwork(
289283 id ids.ShortID ,
290284 ip utils.DynamicIPDesc ,
291285 networkID uint32 ,
292- version version.Version ,
286+ versionCompatibility version.Compatibility ,
293287 parser version.Parser ,
294288 listener net.Listener ,
295289 dialer Dialer ,
@@ -322,26 +316,25 @@ func NewNetwork(
322316 restartOnDisconnected bool ,
323317 disconnectedCheckFreq time.Duration ,
324318 disconnectedRestartTimeout time.Duration ,
325- apricotPhase0Time time.Time ,
326319 healthConfig HealthConfig ,
327320 benchlistManager benchlist.Manager ,
328321 peerAliasTimeout time.Duration ,
329322) Network {
330323 // #nosec G404
331324 netw := & network {
332- log : log ,
333- id : id ,
334- ip : ip ,
335- networkID : networkID ,
336- msgVersion : version ,
337- parser : parser ,
338- listener : listener ,
339- dialer : dialer ,
340- serverUpgrader : serverUpgrader ,
341- clientUpgrader : clientUpgrader ,
342- vdrs : vdrs ,
343- beacons : beacons ,
344- router : router ,
325+ log : log ,
326+ id : id ,
327+ ip : ip ,
328+ networkID : networkID ,
329+ versionCompatibility : versionCompatibility ,
330+ parser : parser ,
331+ listener : listener ,
332+ dialer : dialer ,
333+ serverUpgrader : serverUpgrader ,
334+ clientUpgrader : clientUpgrader ,
335+ vdrs : vdrs ,
336+ beacons : beacons ,
337+ router : router ,
345338 // This field just makes sure we don't connect to ourselves when TLS is
346339 // disabled. So, cryptographically secure random number generation isn't
347340 // used here.
@@ -377,7 +370,6 @@ func NewNetwork(
377370 disconnectedCheckFreq : disconnectedCheckFreq ,
378371 connectedMeter : timer.TimedMeter {Duration : disconnectedRestartTimeout },
379372 restarter : restarter ,
380- apricotPhase0Time : apricotPhase0Time ,
381373 healthConfig : healthConfig ,
382374 benchlistManager : benchlistManager ,
383375 }
@@ -406,7 +398,7 @@ func (n *network) GetAcceptedFrontier(validatorIDs ids.ShortSet, chainID ids.ID,
406398 for _ , peerElement := range n .getPeers (validatorIDs ) {
407399 peer := peerElement .peer
408400 vID := peerElement .id
409- if peer == nil || ! peer .connected .GetValue () || ! peer .Send (msg ) {
401+ if peer == nil || ! peer .connected .GetValue () || ! peer .compatible . GetValue () || ! peer . Send (msg ) {
410402 n .log .Debug ("failed to send GetAcceptedFrontier(%s, %s, %d)" ,
411403 vID ,
412404 chainID ,
@@ -440,7 +432,7 @@ func (n *network) AcceptedFrontier(validatorID ids.ShortID, chainID ids.ID, requ
440432 }
441433
442434 peer := n .getPeer (validatorID )
443- if peer == nil || ! peer .connected .GetValue () || ! peer .Send (msg ) {
435+ if peer == nil || ! peer .connected .GetValue () || ! peer .compatible . GetValue () || ! peer . Send (msg ) {
444436 n .log .Debug ("failed to send AcceptedFrontier(%s, %s, %d, %s)" ,
445437 validatorID ,
446438 chainID ,
@@ -475,7 +467,7 @@ func (n *network) GetAccepted(validatorIDs ids.ShortSet, chainID ids.ID, request
475467 for _ , peerElement := range n .getPeers (validatorIDs ) {
476468 peer := peerElement .peer
477469 vID := peerElement .id
478- if peer == nil || ! peer .connected .GetValue () || ! peer .Send (msg ) {
470+ if peer == nil || ! peer .connected .GetValue () || ! peer .compatible . GetValue () || ! peer . Send (msg ) {
479471 n .log .Debug ("failed to send GetAccepted(%s, %s, %d, %s)" ,
480472 vID ,
481473 chainID ,
@@ -510,7 +502,7 @@ func (n *network) Accepted(validatorID ids.ShortID, chainID ids.ID, requestID ui
510502 }
511503
512504 peer := n .getPeer (validatorID )
513- if peer == nil || ! peer .connected .GetValue () || ! peer .Send (msg ) {
505+ if peer == nil || ! peer .connected .GetValue () || ! peer .compatible . GetValue () || ! peer . Send (msg ) {
514506 n .log .Debug ("failed to send Accepted(%s, %s, %d, %s)" ,
515507 validatorID ,
516508 chainID ,
@@ -538,7 +530,7 @@ func (n *network) GetAncestors(validatorID ids.ShortID, chainID ids.ID, requestI
538530 }
539531
540532 peer := n .getPeer (validatorID )
541- if peer == nil || ! peer .connected .GetValue () || ! peer .Send (msg ) {
533+ if peer == nil || ! peer .connected .GetValue () || ! peer .compatible . GetValue () || ! peer . Send (msg ) {
542534 n .log .Debug ("failed to send GetAncestors(%s, %s, %d, %s)" ,
543535 validatorID ,
544536 chainID ,
@@ -567,7 +559,7 @@ func (n *network) MultiPut(validatorID ids.ShortID, chainID ids.ID, requestID ui
567559 }
568560
569561 peer := n .getPeer (validatorID )
570- if peer == nil || ! peer .connected .GetValue () || ! peer .Send (msg ) {
562+ if peer == nil || ! peer .connected .GetValue () || ! peer .compatible . GetValue () || ! peer . Send (msg ) {
571563 n .log .Debug ("failed to send MultiPut(%s, %s, %d, %d)" ,
572564 validatorID ,
573565 chainID ,
@@ -591,7 +583,7 @@ func (n *network) Get(validatorID ids.ShortID, chainID ids.ID, requestID uint32,
591583 n .log .AssertNoError (err )
592584
593585 peer := n .getPeer (validatorID )
594- if peer == nil || ! peer .connected .GetValue () || ! peer .Send (msg ) {
586+ if peer == nil || ! peer .connected .GetValue () || ! peer .compatible . GetValue () || ! peer . Send (msg ) {
595587 n .log .Debug ("failed to send Get(%s, %s, %d, %s)" ,
596588 validatorID ,
597589 chainID ,
@@ -625,7 +617,7 @@ func (n *network) Put(validatorID ids.ShortID, chainID ids.ID, requestID uint32,
625617 }
626618
627619 peer := n .getPeer (validatorID )
628- if peer == nil || ! peer .connected .GetValue () || ! peer .Send (msg ) {
620+ if peer == nil || ! peer .connected .GetValue () || ! peer .compatible . GetValue () || ! peer . Send (msg ) {
629621 n .log .Debug ("failed to send Put(%s, %s, %d, %s)" ,
630622 validatorID ,
631623 chainID ,
@@ -663,7 +655,7 @@ func (n *network) PushQuery(validatorIDs ids.ShortSet, chainID ids.ID, requestID
663655 for _ , peerElement := range n .getPeers (validatorIDs ) {
664656 peer := peerElement .peer
665657 vID := peerElement .id
666- if peer == nil || ! peer .connected .GetValue () || ! peer .Send (msg ) {
658+ if peer == nil || ! peer .connected .GetValue () || ! peer .compatible . GetValue () || ! peer . Send (msg ) {
667659 n .log .Debug ("failed to send PushQuery(%s, %s, %d, %s)" ,
668660 vID ,
669661 chainID ,
@@ -694,7 +686,7 @@ func (n *network) PullQuery(validatorIDs ids.ShortSet, chainID ids.ID, requestID
694686 for _ , peerElement := range n .getPeers (validatorIDs ) {
695687 peer := peerElement .peer
696688 vID := peerElement .id
697- if peer == nil || ! peer .connected .GetValue () || ! peer .Send (msg ) {
689+ if peer == nil || ! peer .connected .GetValue () || ! peer .compatible . GetValue () || ! peer . Send (msg ) {
698690 n .log .Debug ("failed to send PullQuery(%s, %s, %d, %s)" ,
699691 vID ,
700692 chainID ,
@@ -729,7 +721,7 @@ func (n *network) Chits(validatorID ids.ShortID, chainID ids.ID, requestID uint3
729721 }
730722
731723 peer := n .getPeer (validatorID )
732- if peer == nil || ! peer .connected .GetValue () || ! peer .Send (msg ) {
724+ if peer == nil || ! peer .connected .GetValue () || ! peer .compatible . GetValue () || ! peer . Send (msg ) {
733725 n .log .Debug ("failed to send Chits(%s, %s, %d, %s)" ,
734726 validatorID ,
735727 chainID ,
@@ -799,7 +791,7 @@ func (n *network) upgradeIncoming(remoteAddr string) (bool, error) {
799791func (n * network ) Dispatch () error {
800792 go n .gossip () // Periodically gossip peers
801793 go func () {
802- duration := time .Until (n .apricotPhase0Time )
794+ duration := time .Until (n .versionCompatibility . MaskTime () )
803795 time .Sleep (duration )
804796
805797 n .stateLock .Lock ()
@@ -894,6 +886,7 @@ func (n *network) Peers(nodeIDs []ids.ShortID) []PeerID {
894886 PublicIP : peer .getIP ().String (),
895887 ID : peer .id .PrefixedString (constants .NodeIDPrefix ),
896888 Version : peer .versionStr .GetValue ().(string ),
889+ Up : peer .compatible .GetValue (),
897890 LastSent : time .Unix (atomic .LoadInt64 (& peer .lastSent ), 0 ),
898891 LastReceived : time .Unix (atomic .LoadInt64 (& peer .lastReceived ), 0 ),
899892 Benched : n .benchlistManager .GetBenched (peer .id ),
@@ -910,6 +903,7 @@ func (n *network) Peers(nodeIDs []ids.ShortID) []PeerID {
910903 PublicIP : peer .getIP ().String (),
911904 ID : peer .id .PrefixedString (constants .NodeIDPrefix ),
912905 Version : peer .versionStr .GetValue ().(string ),
906+ Up : peer .compatible .GetValue (),
913907 LastSent : time .Unix (atomic .LoadInt64 (& peer .lastSent ), 0 ),
914908 LastReceived : time .Unix (atomic .LoadInt64 (& peer .lastReceived ), 0 ),
915909 Benched : n .benchlistManager .GetBenched (peer .id ),
@@ -1057,7 +1051,7 @@ func (n *network) gossip() {
10571051 ! ip .IsZero () &&
10581052 n .vdrs .Contains (peer .id ) {
10591053 peerVersion := peer .versionStruct .GetValue ().(version.Version )
1060- if ! peerVersion . Before ( minimumUnmaskedVersion ) || time . Since ( n . apricotPhase0Time ) < 0 {
1054+ if n . versionCompatibility . Unmaskable ( peerVersion ) == nil {
10611055 ips = append (ips , ip )
10621056 }
10631057 }
@@ -1300,7 +1294,7 @@ func (n *network) validatorIPs() []utils.IPDesc {
13001294 ip := peer .getIP ()
13011295 if peer .connected .GetValue () && ! ip .IsZero () && n .vdrs .Contains (peer .id ) {
13021296 peerVersion := peer .versionStruct .GetValue ().(version.Version )
1303- if ! peerVersion . Before ( minimumUnmaskedVersion ) || time . Since ( n . apricotPhase0Time ) < 0 {
1297+ if n . versionCompatibility . Unmaskable ( peerVersion ) == nil {
13041298 ips = append (ips , ip )
13051299 }
13061300 }
@@ -1320,7 +1314,7 @@ func (n *network) connected(p *peer) {
13201314 peerVersion := p .versionStruct .GetValue ().(version.Version )
13211315
13221316 if n .hasMasked {
1323- if peerVersion . Before ( minimumUnmaskedVersion ) {
1317+ if n . versionCompatibility . Unmaskable ( peerVersion ) != nil {
13241318 if err := n .vdrs .MaskValidator (p .id ); err != nil {
13251319 n .log .Error ("failed to mask validator %s due to %s" , p .id , err )
13261320 }
@@ -1331,7 +1325,7 @@ func (n *network) connected(p *peer) {
13311325 }
13321326 n .log .Verbo ("The new staking set is:\n %s" , n .vdrs )
13331327 } else {
1334- if peerVersion . Before ( minimumUnmaskedVersion ) {
1328+ if n . versionCompatibility . WontMask ( peerVersion ) != nil {
13351329 n .maskedValidators .Add (p .id )
13361330 } else {
13371331 n .maskedValidators .Remove (p .id )
@@ -1349,7 +1343,12 @@ func (n *network) connected(p *peer) {
13491343 n .connectedIPs [str ] = struct {}{}
13501344 }
13511345
1352- n .router .Connected (p .id )
1346+ compatible := n .versionCompatibility .Compatible (peerVersion ) == nil
1347+ p .compatible .SetValue (compatible )
1348+
1349+ if compatible {
1350+ n .router .Connected (p .id )
1351+ }
13531352}
13541353
13551354// should only be called after the peer is marked as connected.
@@ -1376,7 +1375,8 @@ func (n *network) disconnected(p *peer) {
13761375 n .track (ip )
13771376 }
13781377
1379- if p .connected .GetValue () {
1378+ if p .compatible .GetValue () {
1379+ p .compatible .SetValue (false )
13801380 n .router .Disconnected (p .id )
13811381 }
13821382}
0 commit comments