Skip to content

Commit bf60150

Browse files
committed
fix<node>: headers-sync and refactor
1 parent 9750e82 commit bf60150

File tree

16 files changed

+120
-300
lines changed

16 files changed

+120
-300
lines changed

bin/bcoin-cli

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -129,22 +129,6 @@ class CLI {
129129
this.log(filter);
130130
}
131131

132-
async getFilterHeader() {
133-
let hash = this.config.str(0, '');
134-
135-
if (hash.length !== 64)
136-
hash = parseInt(hash, 10);
137-
138-
const filterHeader = await this.client.getFilterHeader(hash);
139-
140-
if (!filterHeader) {
141-
this.log('Filter header not found.');
142-
return;
143-
}
144-
145-
this.log(filterHeader);
146-
}
147-
148132
async estimateFee() {
149133
const blocks = this.config.uint(0, 1);
150134

@@ -262,9 +246,6 @@ class CLI {
262246
case 'filter':
263247
await this.getFilter();
264248
break;
265-
case 'filterheader':
266-
await this.getFilterHeader();
267-
break;
268249
case 'fee':
269250
await this.estimateFee();
270251
break;
@@ -282,7 +263,6 @@ class CLI {
282263
this.log(' $ coin [hash+index/address]: View coins.');
283264
this.log(' $ fee [target]: Estimate smart fee.');
284265
this.log(' $ filter [hash/height]: View filter.');
285-
this.log(' $ filterheader [hash/height]: View filter header.');
286266
this.log(' $ header [hash/height]: View block header.');
287267
this.log(' $ info: Get server info.');
288268
this.log(' $ mempool: Get mempool snapshot.');

lib/blockchain/chain.js

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1791,24 +1791,6 @@ class Chain extends AsyncEmitter {
17911791
return this.hasEntry(hash);
17921792
}
17931793

1794-
async getCFHeaderHeight() {
1795-
return await this.db.getCFHeaderHeight();
1796-
}
1797-
1798-
async saveCFHeaderHeight(height) {
1799-
this.db.neutrinoState.headerHeight = height;
1800-
await this.db.saveNeutrinoState();
1801-
}
1802-
1803-
async getCFilterHeight() {
1804-
return await this.db.getCFilterHeight();
1805-
}
1806-
1807-
async saveCFilterHeight(height) {
1808-
this.db.neutrinoState.filterHeight = height;
1809-
await this.db.saveNeutrinoState();
1810-
}
1811-
18121794
/**
18131795
* Find the corresponding block entry by hash or height.
18141796
* @param {Hash|Number} hash/height
@@ -2021,12 +2003,17 @@ class Chain extends AsyncEmitter {
20212003
if (this.synced)
20222004
return;
20232005

2024-
if (this.options.checkpoints)
2006+
if (this.options.checkpoints) {
20252007
if (this.height < this.network.lastCheckpoint)
20262008
return;
2009+
}
2010+
2011+
if (this.tip.time < util.now() - this.network.block.maxTipAge)
2012+
return;
20272013

20282014
if (!this.hasChainwork())
20292015
return;
2016+
20302017
this.synced = true;
20312018
this.emit('full');
20322019
}
@@ -2629,7 +2616,6 @@ class ChainOptions {
26292616
this.compression = true;
26302617

26312618
this.spv = false;
2632-
this.neutrino = false;
26332619
this.bip91 = false;
26342620
this.bip148 = false;
26352621
this.prune = false;
@@ -2676,11 +2662,6 @@ class ChainOptions {
26762662
this.spv = options.spv;
26772663
}
26782664

2679-
if (options.neutrino != null) {
2680-
assert(typeof options.neutrino === 'boolean');
2681-
this.neutrino = options.neutrino;
2682-
}
2683-
26842665
if (options.prefix != null) {
26852666
assert(typeof options.prefix === 'string');
26862667
this.prefix = options.prefix;

lib/blockchain/chaindb.js

Lines changed: 1 addition & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ class ChainDB {
4646
this.state = new ChainState();
4747
this.pending = null;
4848
this.current = null;
49-
this.neutrinoState = null;
5049

5150
this.cacheHash = new LRU(this.options.entryCache, null, BufferMap);
5251
this.cacheHeight = new LRU(this.options.entryCache);
@@ -91,11 +90,6 @@ class ChainDB {
9190
this.logger.info('ChainDB successfully initialized.');
9291
}
9392

94-
if (this.options.neutrino) {
95-
if (!this.neutrinoState)
96-
this.neutrinoState = await this.getNeutrinoState();
97-
}
98-
9993
this.logger.info(
10094
'Chain State: hash=%h tx=%d coin=%d value=%s.',
10195
this.state.tip,
@@ -1007,7 +1001,7 @@ class ChainDB {
10071001
*/
10081002

10091003
async getRawBlock(block) {
1010-
if (this.options.spv && !this.options.neutrino)
1004+
if (this.options.spv)
10111005
return null;
10121006

10131007
const hash = await this.getHash(block);
@@ -1676,39 +1670,6 @@ class ChainDB {
16761670
b.put(layout.O.encode(), flags.toRaw());
16771671
return b.write();
16781672
}
1679-
1680-
/**
1681-
* Get Neutrino State
1682-
* @returns {Promise<NeutrinoState>} - Returns neutrino state
1683-
*/
1684-
1685-
async getNeutrinoState() {
1686-
const data = await this.db.get(layout.N.encode());
1687-
if (!data)
1688-
return new NeutrinoState();
1689-
return NeutrinoState.fromRaw(data);
1690-
}
1691-
1692-
async getCFHeaderHeight() {
1693-
const state = await this.getNeutrinoState();
1694-
return state.headerHeight;
1695-
}
1696-
1697-
async getCFilterHeight() {
1698-
const state = await this.getNeutrinoState();
1699-
return state.filterHeight;
1700-
}
1701-
1702-
/**
1703-
* Save Neutrino State
1704-
* @returns {void}
1705-
*/
1706-
async saveNeutrinoState() {
1707-
const state = this.neutrinoState.toRaw();
1708-
const b = this.db.batch();
1709-
b.put(layout.N.encode(), state);
1710-
return b.write();
1711-
}
17121673
}
17131674

17141675
/**
@@ -1991,28 +1952,6 @@ function fromU32(num) {
19911952
return data;
19921953
}
19931954

1994-
class NeutrinoState {
1995-
constructor() { // TODO: do we add support for multiple filters?
1996-
this.headerHeight = 0;
1997-
this.filterHeight = 0;
1998-
}
1999-
2000-
toRaw() {
2001-
const bw = bio.write(8);
2002-
bw.writeU32(this.headerHeight);
2003-
bw.writeU32(this.filterHeight);
2004-
return bw.render();
2005-
}
2006-
2007-
static fromRaw(data) {
2008-
const state = new NeutrinoState();
2009-
const br = bio.read(data);
2010-
state.headerHeight = br.readU32();
2011-
state.filterHeight = br.readU32();
2012-
return state;
2013-
}
2014-
}
2015-
20161955
/*
20171956
* Expose
20181957
*/

lib/blockchain/layout.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ const layout = {
3434
O: bdb.key('O'),
3535
R: bdb.key('R'),
3636
D: bdb.key('D'),
37-
N: bdb.key('N'),
3837
F: bdb.key('H', ['hash256']),
3938
e: bdb.key('e', ['hash256']),
4039
h: bdb.key('h', ['hash256']),

lib/client/node.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,6 @@ class NodeClient extends Client {
169169
return this.get(`/filter/${block}`);
170170
}
171171

172-
getFilterHeader(block) {
173-
assert(typeof block === 'string' || typeof block === 'number');
174-
return this.get(`/filterheader/${block}`);
175-
}
176-
177172
getBlockPeer(hash) {
178173
return this.call('get block peer', hash);
179174
}

lib/indexer/filterindexer.js

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -85,27 +85,6 @@ class FilterIndexer extends Indexer {
8585
this.put(layout.f.encode(hash), gcsFilter.hash());
8686
}
8787

88-
/**
89-
* save filter header
90-
* @param {Hash} blockHash
91-
* @param {Hash} filterHeader
92-
* @param {Hash} filterHash
93-
* @returns {Promise}
94-
*/
95-
96-
async saveFilterHeader(blockHash, filterHeader, filterHash) {
97-
assert(blockHash);
98-
assert(filterHeader);
99-
assert(filterHash);
100-
101-
const filter = new Filter();
102-
filter.header = filterHeader;
103-
104-
await this.blocks.writeFilter(blockHash, filter.toRaw(), this.filterType);
105-
// console.log(layout.f.encode(blockHash));
106-
this.put(layout.f.encode(blockHash), filterHash);
107-
}
108-
10988
/**
11089
* Save filter
11190
* @param {Hash} blockHash
@@ -114,8 +93,9 @@ class FilterIndexer extends Indexer {
11493
* @returns {Promise}
11594
*/
11695

117-
async saveFilter(blockHash, basicFilter, filterHeader) {
96+
async saveFilter(blockHash, blockHeight, basicFilter, filterHeader) {
11897
assert(blockHash);
98+
assert(blockHeight);
11999
assert(basicFilter);
120100
assert(filterHeader);
121101

@@ -124,8 +104,8 @@ class FilterIndexer extends Indexer {
124104
filter.header = filterHeader;
125105

126106
await this.blocks.writeFilter(blockHash, filter.toRaw(), this.filterType);
127-
// console.log(layout.f.encode(blockHash));
128107
this.put(layout.f.encode(blockHash), basicFilter.hash());
108+
await super.syncHeight(blockHash, blockHeight);
129109
}
130110

131111
/**

lib/indexer/indexer.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,12 @@ class Indexer extends EventEmitter {
198198
this.height = 0;
199199
}
200200

201+
async syncHeight(hash, height) {
202+
const meta = new BlockMeta(hash, height);
203+
await this._setTip(meta);
204+
this.height = height;
205+
}
206+
201207
/**
202208
* Bind to chain events and save listeners for removal on close
203209
* @private

lib/net/peer.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1680,6 +1680,11 @@ class Peer extends EventEmitter {
16801680
this.compactWitness = packet.version === 2;
16811681
}
16821682

1683+
sendSendHeaders() {
1684+
const packet = new packets.SendHeadersPacket();
1685+
this.send(packet);
1686+
}
1687+
16831688
/**
16841689
* Send `getheaders` to peer. Note that unlike
16851690
* `getblocks`, `getheaders` can have a null locator.

0 commit comments

Comments
 (0)