@@ -429,6 +429,8 @@ class Peer {
429429
430430 this . needsSync = false
431431 this . syncsProcessing = 0
432+ this . lastUpgradableLength = 0
433+ this . lastUpgradableFork = 0
432434
433435 this . _remoteContiguousLength = 0
434436
@@ -662,6 +664,10 @@ class Peer {
662664
663665 if ( length === this . remoteLength && fork === this . core . state . fork ) {
664666 this . canUpgrade = upgrade
667+ if ( upgrade ) {
668+ this . lastUpgradableFork = fork
669+ this . lastUpgradableLength = length
670+ }
665671 }
666672
667673 if ( -- this . syncsProcessing !== 0 ) return // ie not latest
@@ -1204,7 +1210,7 @@ class Peer {
12041210
12051211 if ( this . _remoteHasBlock ( index ) === false ) continue
12061212 if ( this . core . bitfield . get ( index ) === true ) continue
1207- if ( ! this . _hasTreeParent ( index ) ) continue
1213+ if ( ! this . _canRequest ( index ) ) continue
12081214
12091215 // Check if this block is currently inflight - if so pick another
12101216 const b = this . replicator . _blocks . get ( index )
@@ -1233,42 +1239,11 @@ class Peer {
12331239 return false
12341240 }
12351241
1236- _hasTreeParent ( index ) {
1242+ _canRequest ( index ) {
1243+ if ( ! ( index >= 0 ) ) throw ASSERTION ( 'bad index to _canRequest: ' + index )
12371244 if ( this . remoteLength >= this . core . state . length ) return true
1238- if ( ! ( index >= 0 ) ) throw ASSERTION ( 'bad index to _hasTreeParent: ' + index )
1239-
1240- const ite = flatTree . iterator ( index * 2 )
1241-
1242- let span = 2
1243- let length = 0
1244- let cnt = 0
1245-
1246- while ( true ) {
1247- if ( ++ cnt >= 64 ) {
1248- throw ASSERTION ( '_hasTreeParent is stuck, index=' + index + ', length=' + length +
1249- ', span=' + span + ', remoteLength=' + this . remoteLength + ', length=' + this . core . state . length )
1250- }
1251-
1252- ite . parent ( )
1253-
1254- const left = ( ite . index - ite . factor / 2 + 1 ) / 2
1255- length = left + span
1256-
1257- // if larger than local AND larger than remote - they share the root so its ok
1258- if ( length > this . core . state . length ) {
1259- if ( length > this . remoteLength ) return true
1260- break
1261- }
1262-
1263- // its less than local but larger than remote so skip it
1264- if ( length > this . remoteLength ) break
1265-
1266- span *= 2
1267- const first = this . core . bitfield . findFirst ( true , left )
1268- if ( first > - 1 && first < length ) return true
1269- }
1270-
1271- // TODO: push to async queue and check against our local merkle tree if we actually can request this block
1245+ if ( index < this . lastUpgradableLength && this . lastUpgradableFork === this . core . state . fork ) return true
1246+ if ( this . canUpgrade && this . syncsProcessing === 0 ) return true
12721247 return false
12731248 }
12741249
@@ -1293,9 +1268,7 @@ class Peer {
12931268 return false
12941269 }
12951270
1296- if ( ! this . _hasTreeParent ( b . index ) ) {
1297- return false
1298- }
1271+ if ( ! this . _canRequest ( b . index ) ) return false
12991272
13001273 const req = this . _makeRequest ( b . index >= length , b . priority , b . index + 1 )
13011274 if ( req === null ) return false
@@ -1306,7 +1279,7 @@ class Peer {
13061279 }
13071280
13081281 _requestRangeBlock ( index , length ) {
1309- if ( this . core . bitfield . get ( index ) === true || ! this . _hasTreeParent ( index ) ) return false
1282+ if ( this . core . bitfield . get ( index ) === true || ! this . _canRequest ( index ) ) return false
13101283
13111284 const b = this . replicator . _blocks . add ( index , PRIORITY . NORMAL )
13121285 if ( b . inflight . length > 0 ) {
0 commit comments