Skip to content

Commit

Permalink
chore: cleaned commit history
Browse files Browse the repository at this point in the history
  • Loading branch information
manavdesai27 committed Jul 7, 2023
1 parent 6bbe0ab commit 37b61f1
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 42 deletions.
8 changes: 4 additions & 4 deletions lib/blockchain/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -1804,17 +1804,17 @@ class Chain extends AsyncEmitter {
return this.hasEntry(hash);
}

getCFHeaderHeight() {
return this.db.neutrinoState.headerHeight;
async getCFHeaderHeight() {
return await this.db.getCFHeaderHeight();
}

async saveCFHeaderHeight(height) {
this.db.neutrinoState.headerHeight = height;
await this.db.saveNeutrinoState();
}

getCFilterHeight() {
return this.db.neutrinoState.filterHeight;
async getCFilterHeight() {
return await this.db.getCFilterHeight();
}

async saveCFilterHeight(height) {
Expand Down
10 changes: 10 additions & 0 deletions lib/blockchain/chaindb.js
Original file line number Diff line number Diff line change
Expand Up @@ -1698,6 +1698,16 @@ class ChainDB {
return NeutrinoState.fromRaw(data);
}

async getCFHeaderHeight() {
const state = await this.getNeutrinoState();
return state.headerHeight;
}

async getCFilterHeight() {
const state = await this.getNeutrinoState();
return state.filterHeight;
}

/**
* Save Neutrino State
* @returns {void}
Expand Down
41 changes: 21 additions & 20 deletions lib/net/pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ class Pool extends EventEmitter {
this.checkpoints = false;
this.neutrino = this.options.neutrino;
this.headerChain = new List();
this.filterHeight = 0;
this.headerNext = null;
this.headerTip = null;

Expand Down Expand Up @@ -743,18 +742,18 @@ class Pool extends EventEmitter {
return;

this.filterSyncing = true;
const startHeight = this.chain.getCFHeaderHeight()
? this.chain.getCFHeaderHeight() : 1;
const chainHeight = await this.chain.tip.height;
const cFHeaderHeight = await this.chain.getCFHeaderHeight();
const startHeight = cFHeaderHeight
? cFHeaderHeight : 1;
const chainHeight = await this.chain.height;
const stopHeight = chainHeight > 2000 ? 2000 : chainHeight;
const stopHash = await this.chain.getHash(stopHeight);
this.peers.load.sendGetCFHeaders(
this.getcfheadersFilterType = common.FILTERS.BASIC;
this.getcfheadersStopHash = stopHash;
await this.peers.load.sendGetCFHeaders(
common.FILTERS.BASIC,
startHeight,
stopHash);

this.getcfheadersFilterType = common.FILTERS.BASIC;
this.getcfheadersStopHash = stopHash;
}

/**
Expand All @@ -768,18 +767,19 @@ class Pool extends EventEmitter {
return;

this.filterSyncing = true;
const startHeight = this.chain.getCFilterHeight()
? this.chain.getCFilterHeight() : 0;
const chainHeight = await this.chain.tip.height;
const cFilterHeight = await this.chain.getCFilterHeight();
const startHeight = cFilterHeight
? cFilterHeight : 0;
const chainHeight = await this.chain.height;
const stopHeight = chainHeight > 1000 ? 1000 : chainHeight;
const stopHash = await this.chain.getHash(stopHeight);
this.peers.load.sendGetCFilters(
common.FILTERS.BASIC,
startHeight,
stopHash);
this.getcfiltersFilterType = common.FILTERS.BASIC;
this.getcfiltersStartHeight = startHeight;
this.getcfiltersStopHash = stopHash;
await this.peers.load.sendGetCFilters(
common.FILTERS.BASIC,
startHeight,
stopHash);
}

/**
Expand Down Expand Up @@ -2220,27 +2220,26 @@ class Pool extends EventEmitter {
const filterType = packet.filterType;
const filter = packet.filterBytes;

this.filterHeight += 1;

// todo: verify the filter
assert(filterType === this.getcfheadersFilterType);
const blockHeight = await this.chain.getHeight(blockHash);
await this.chain.saveCFilterHeight(blockHeight);
const stopHeight = await this.chain.getHeight(this.getcfiltersStopHash);

assert(blockHeight >= this.getcfiltersStartHeight
&& blockHeight <= stopHeight);

await this.chain.saveCFilterHeight(blockHeight);
const cFilterHeight = await this.chain.getCFilterHeight();
// todo: save the filter
const basicFilter = new BasicFilter();
const gcsFilter = basicFilter.fromNBytes(filter);
this.emit('cfilter', blockHash, gcsFilter);
const startHeight = stopHeight + 1;
let nextStopHeight;
if (this.filterHeight === stopHeight + 1
if (cFilterHeight === stopHeight
&& stopHeight < this.chain.height) {
if (startHeight + 1000 < this.chain.height) {
nextStopHeight = startHeight + 1000;
nextStopHeight = stopHeight + 1000;
const stopHash = await this.chain.getHash(nextStopHeight);
this.getcfiltersStartHeight = startHeight;
this.getcfiltersStopHash = stopHash;
Expand All @@ -2261,6 +2260,8 @@ class Pool extends EventEmitter {
);
return;
}
} else {
this.logger.info('CFilters sync complete');
}
}

Expand Down
8 changes: 4 additions & 4 deletions lib/node/neutrino.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,17 +164,17 @@ class Neutrino extends Node {
this.emit('reset', tip);
});

this.chain.on('headersFull', () => {
this.chain.on('headersFull', async () => {
if (this.chain.height === 0)
return;
this.logger.info('Block Headers are fully synced');
// this.pool.startFilterCheckPtSync(); // TODO: Maybe implement this later
this.pool.startFilterHeadersSync();
await this.pool.startFilterHeadersSync();
});

this.pool.on('cfheaders', () => {
this.pool.on('cfheaders', async () => {
this.logger.info('CF Headers Synced');
this.pool.startFilterSync();
await this.pool.startFilterSync();
});

this.loadPlugins();
Expand Down
6 changes: 4 additions & 2 deletions lib/node/rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -632,14 +632,16 @@ class RPC extends RPCBase {
if (help || args.length !== 0)
throw new RPCError(errs.MISC_ERROR, 'getfiltercount');

return this.chain.getCFilterHeight();
const height = await this.chain.getCFilterHeight();
return height;
}

async getFilterHeaderCount(args, help) {
if (help || args.length !== 0)
throw new RPCError(errs.MISC_ERROR, 'getfilterheadercount');

return this.chain.getCFHeaderHeight();
const height = await this.chain.getCFHeaderHeight();
return height;
}

async getBlock(args, help) {
Expand Down
9 changes: 6 additions & 3 deletions lib/wallet/walletdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -586,10 +586,11 @@ class WalletDB extends EventEmitter {

await piter.each(async (key) => {
const [data] = layout.p.decode(key);
// todo: check filter
const match = filter.match(gcsKey, data);
if (match)
if (match) {
await this.client.getBlockFromNode(blockHash, filter);
return;
}
});

const oiter = this.db.iterator({
Expand All @@ -602,8 +603,10 @@ class WalletDB extends EventEmitter {
const outpoint = new Outpoint(hash, index);
const data = outpoint.toRaw();
const match = filter.match(gcsKey, data);
if (match)
if (match) {
await this.client.getBlockFromNode(blockHash, filter);
return;
}
});
}

Expand Down
19 changes: 10 additions & 9 deletions test/neutrino-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,24 +55,25 @@ describe('neutrino', function () {
await node2.close();
});

describe('getheaders', () => {
it('should getheaders', async () => {
assert.equal(node1.chain.height, node2.chain.height);
});
});

describe('getcfheaders', () => {
it('should getcfheaders', async () => {
const headerHeight = node1.chain.getCFHeaderHeight();
await new Promise(resolve => setTimeout(resolve, 400));
const headerHeight = await node1.chain.getCFHeaderHeight();
assert.equal(headerHeight, node1.chain.height);
});
});

describe('getcfilters', () => {
it('should getcfilters', async () => {
const filterHeight = node1.chain.getCFHeaderHeight();
await new Promise(resolve => setTimeout(resolve, 400));
const filterHeight = await node1.chain.getCFilterHeight();
assert.equal(filterHeight, node1.chain.height);
});
});

describe('getheaders', () => {
it('should getheaders', async () => {
await mineBlocks(30);
assert.equal(node1.chain.height, node2.chain.height);
});
});
});
113 changes: 113 additions & 0 deletions test/wallet-neutrino-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
'use strict';

const FullNode = require('../lib/node/fullnode');
const Neutrino = require('../lib/node/neutrino');
const MTX = require('../lib/primitives/mtx');
const assert = require('bsert');
const { consensus } = require('../lib/protocol');
const { forValue } = require('./util/common');

const node1 = new FullNode({
network: 'regtest',
memory: true,
listen: true,
indexFilter: true,
plugins: [require('../lib/wallet/plugin')],
bip157: true
});

const node2 = new Neutrino({
network: 'regtest',
memory: true,
port: 10000,
httpPort: 20000,
neutrino: true,
only: '127.0.0.1',
plugins: [require('../lib/wallet/plugin')],
env: {
'BCOIN_WALLET_HTTP_PORT': '12221'
}
});

const chain = node1.chain;
const miner = node1.miner;
const wdb1 = node1.require('walletdb').wdb;
const wdb2 = node2.require('walletdb').wdb;

let wallet1 = null;
let wallet2 = null;
let cb = null;

async function mineBlock(tx) {
const job = await miner.createJob();

if (!tx)
return await job.mineAsync();

const spend = new MTX();
spend.addTX(tx, 0);

spend.addOutput(await wallet2.receiveAddress(), 25 * 1e8);
spend.addOutput(await wallet2.changeAddress(), 5 * 1e8);

spend.setLocktime(chain.height);
await wallet1.sign(spend);

job.addTX(spend.toTX(), spend.view);
job.refresh();

return await job.mineAsync();
}

describe('wallet-neutrino', function() {
it('should open chain and miner', async () => {
miner.mempool = null;
consensus.COINBASE_MATURITY = 0;
await node1.open();
await node2.open();
});

it('should open walletdb', async () => {
wallet1 = await wdb1.create();
wallet2 = await wdb2.create();
miner.addresses.length = 0;
miner.addAddress(await wallet1.receiveAddress());
});

it('should mine 10 blocks', async () => {
let n = 10;
while (n) {
const block = await mineBlock(cb);
cb = block.txs[0];
await node1.chain.add(block);
n--;
}
});

it('should connect nodes', async () => {
await node1.connect();
await node2.connect();
});

it('should start sync chain', async () => {
node1.startSync();
node2.startSync();
await forValue(node2.chain, 'height', node1.chain.height);
});

it('should getheaders', async () => {
assert.equal(node1.chain.height, node2.chain.height);
});

it('should getcfheaders', async () => {
await new Promise(resolve => setTimeout(resolve, 400));
const headerHeight = await node2.chain.getCFHeaderHeight();
assert.equal(headerHeight, node2.chain.height);
});

it('should getcfilters', async () => {
await new Promise(resolve => setTimeout(resolve, 400));
const filterHeight = await node2.chain.getCFilterHeight();
assert.equal(filterHeight, node2.chain.height);
});
});

0 comments on commit 37b61f1

Please sign in to comment.