@@ -769,7 +769,7 @@ class Pool extends EventEmitter {
769
769
this . filterSyncing = true ;
770
770
const cFilterHeight = await this . chain . getCFilterHeight ( ) ;
771
771
const startHeight = cFilterHeight
772
- ? cFilterHeight : 0 ;
772
+ ? cFilterHeight : 1 ;
773
773
const chainHeight = await this . chain . height ;
774
774
const stopHeight = chainHeight > 1000 ? 1000 : chainHeight ;
775
775
const stopHash = await this . chain . getHash ( stopHeight ) ;
@@ -931,6 +931,8 @@ class Pool extends EventEmitter {
931
931
peer . blockTime = Date . now ( ) ;
932
932
if ( this . options . neutrino ) {
933
933
peer . sendGetHeaders ( locator ) ;
934
+ if ( ! this . syncing )
935
+ this . startFilterHeadersSync ( ) ;
934
936
return true ;
935
937
}
936
938
if ( this . checkpoints ) {
@@ -1759,7 +1761,11 @@ class Pool extends EventEmitter {
1759
1761
return ;
1760
1762
1761
1763
if ( this . neutrino ) {
1762
- this . startSync ( ) ;
1764
+ const locator = await this . chain . getLocator ( ) ;
1765
+ this . sendLocator ( locator , peer ) ;
1766
+ if ( ! this . syncing )
1767
+ this . startFilterHeadersSync ( ) ;
1768
+ return ;
1763
1769
}
1764
1770
1765
1771
// Request headers instead.
@@ -2172,30 +2178,35 @@ class Pool extends EventEmitter {
2172
2178
}
2173
2179
2174
2180
const filterType = packet . filterType ;
2175
- assert ( filterType === this . getcfheadersFilterType ) ;
2181
+
2182
+ if ( filterType !== this . getcfheadersFilterType ) {
2183
+ peer . ban ( ) ;
2184
+ peer . destroy ( ) ;
2185
+ return ;
2186
+ }
2187
+
2176
2188
const stopHash = packet . stopHash ;
2177
2189
assert ( stopHash . equals ( this . getcfheadersStopHash ) ) ;
2178
2190
let previousFilterHeader = packet . previousFilterHeader ;
2179
2191
const filterHashes = packet . filterHashes ;
2180
- assert ( filterHashes . length <= 2000 ) ;
2181
2192
let blockHeight = await this . chain . getHeight ( stopHash )
2182
- - filterHashes . length ;
2193
+ - filterHashes . length + 1 ;
2183
2194
const stopHeight = await this . chain . getHeight ( stopHash ) ;
2184
2195
for ( const filterHash of filterHashes ) {
2185
- assert ( blockHeight < stopHeight ) ;
2196
+ assert ( blockHeight <= stopHeight ) ;
2186
2197
const basicFilter = new BasicFilter ( ) ;
2187
2198
basicFilter . _hash = filterHash ;
2188
2199
const filterHeader = basicFilter . header ( previousFilterHeader ) ;
2189
2200
const lastFilterHeader = this . cfHeaderChain . tail ;
2190
2201
const cfHeaderEntry = new CFHeaderEntry (
2191
2202
filterHash , lastFilterHeader . height + 1 ) ;
2192
2203
this . cfHeaderChain . push ( cfHeaderEntry ) ;
2193
- // todo: verify the filterHeader
2194
- // todo: save the filterHeader
2204
+ const blockHash = await this . chain . getHash ( blockHeight ) ;
2205
+ const indexer = this . getFilterIndexer ( filtersByVal [ filterType ] ) ;
2206
+ await indexer . saveFilterHeader ( blockHash , filterHeader , filterHash ) ;
2195
2207
previousFilterHeader = filterHeader ;
2196
- // todo: add a function for this in chain.js
2197
- blockHeight ++ ;
2198
2208
await this . chain . saveCFHeaderHeight ( blockHeight ) ;
2209
+ blockHeight ++ ;
2199
2210
}
2200
2211
if ( this . headerChain . tail . height <= stopHeight )
2201
2212
this . emit ( 'cfheaders' ) ;
@@ -2220,20 +2231,31 @@ class Pool extends EventEmitter {
2220
2231
const filterType = packet . filterType ;
2221
2232
const filter = packet . filterBytes ;
2222
2233
2223
- // todo: verify the filter
2224
- assert ( filterType === this . getcfheadersFilterType ) ;
2234
+ if ( filterType !== this . getcfheadersFilterType ) {
2235
+ peer . ban ( ) ;
2236
+ peer . destroy ( ) ;
2237
+ return ;
2238
+ }
2239
+
2225
2240
const blockHeight = await this . chain . getHeight ( blockHash ) ;
2226
2241
const stopHeight = await this . chain . getHeight ( this . getcfiltersStopHash ) ;
2227
2242
2228
2243
assert ( blockHeight >= this . getcfiltersStartHeight
2229
2244
&& blockHeight <= stopHeight ) ;
2230
2245
2231
- await this . chain . saveCFilterHeight ( blockHeight ) ;
2232
- const cFilterHeight = await this . chain . getCFilterHeight ( ) ;
2233
- // todo: save the filter
2234
- const basicFilter = new BasicFilter ( ) ;
2235
- const gcsFilter = basicFilter . fromNBytes ( filter ) ;
2236
- this . emit ( 'cfilter' , blockHash , gcsFilter ) ;
2246
+ const cFilterHeight = await this . chain . getCFilterHeight ( ) ;
2247
+
2248
+ const indexer = this . getFilterIndexer ( filtersByVal [ filterType ] ) ;
2249
+
2250
+ const filterHeader = await indexer . getFilterHeader ( blockHash ) ;
2251
+
2252
+ const basicFilter = new BasicFilter ( ) ;
2253
+ const gcsFilter = basicFilter . fromNBytes ( filter ) ;
2254
+
2255
+ await indexer . saveFilter ( blockHash , gcsFilter , filterHeader ) ;
2256
+
2257
+ this . emit ( 'cfilter' , blockHash , gcsFilter ) ;
2258
+ await this . chain . saveCFilterHeight ( blockHeight ) ;
2237
2259
const startHeight = stopHeight + 1 ;
2238
2260
let nextStopHeight ;
2239
2261
if ( cFilterHeight === stopHeight
@@ -2436,7 +2458,7 @@ class Pool extends EventEmitter {
2436
2458
this . logger . warning (
2437
2459
'Peer sent a bad header chain (%s).' ,
2438
2460
peer . hostname ( ) ) ;
2439
- peer . destroy ( ) ;
2461
+ peer . increaseBan ( 10 ) ;
2440
2462
return ;
2441
2463
}
2442
2464
@@ -2459,7 +2481,7 @@ class Pool extends EventEmitter {
2459
2481
2460
2482
this . headerChain . push ( node ) ;
2461
2483
if ( this . options . neutrino )
2462
- await this . _addBlock ( peer , header , chainCommon . flags . VERIFY_NONE ) ;
2484
+ await this . _addBlock ( peer , header , chainCommon . flags . VERIFY_POW ) ;
2463
2485
}
2464
2486
2465
2487
this . logger . debug (
@@ -2472,7 +2494,7 @@ class Pool extends EventEmitter {
2472
2494
peer . blockTime = Date . now ( ) ;
2473
2495
2474
2496
// Request the blocks we just added.
2475
- if ( checkpoint ) {
2497
+ if ( checkpoint && ! this . options . neutrino ) {
2476
2498
this . headerChain . shift ( ) ;
2477
2499
this . resolveHeaders ( peer ) ;
2478
2500
return ;
0 commit comments