Skip to content

Commit

Permalink
Merge PR #752 from 'rithvikvibhu/spv-net-fixes'
Browse files Browse the repository at this point in the history
  • Loading branch information
pinheadmz committed Jul 3, 2022
1 parent 5bcd6b0 commit 6bc818d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 22 deletions.
3 changes: 0 additions & 3 deletions lib/net/peer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1083,9 +1083,6 @@ class Peer extends EventEmitter {
return;

switch (packet.type) {
case packetTypes.MEMPOOL:
this.request(packetTypes.INV, timeout);
break;
case packetTypes.GETBLOCKS:
if (!this.options.isFull())
this.request(packetTypes.INV, timeout);
Expand Down
8 changes: 7 additions & 1 deletion lib/net/pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,11 @@ class Pool extends EventEmitter {
if (!force && peer.syncing)
continue;

if (force) {
peer.lastTip = consensus.ZERO_HASH;
peer.lastStop = consensus.ZERO_HASH;
}

this.sendLocator(locator, peer);
}
}
Expand Down Expand Up @@ -2862,7 +2867,8 @@ class Pool extends EventEmitter {
items.push(new InvItem(invTypes.TX, hash));

this.logger.debug(
'Sending mempool snapshot (%s).',
'Peer requested mempool snapshot, queing %d items (%s).',
items.length,
peer.hostname());

peer.queueInv(items);
Expand Down
6 changes: 3 additions & 3 deletions lib/protocol/networks.js
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ main.selfConnect = false;
* @const {Boolean}
*/

main.requestMempool = false;
main.requestMempool = true;

/**
* DNSSEC ownership prefix.
Expand Down Expand Up @@ -749,7 +749,7 @@ testnet.identityKey = null;

testnet.selfConnect = false;

testnet.requestMempool = false;
testnet.requestMempool = true;

testnet.claimPrefix = 'hns-testnet:';

Expand Down Expand Up @@ -1042,7 +1042,7 @@ simnet.identityKey = Buffer.from(

simnet.selfConnect = true;

simnet.requestMempool = false;
simnet.requestMempool = true;

simnet.claimPrefix = 'hns-simnet:';

Expand Down
33 changes: 18 additions & 15 deletions test/net-spv-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const NameState = require('../lib/covenants/namestate');
const {Resource} = require('../lib/dns/resource');
const {types: packetTypes} = require('../lib/net/packets');
const {types: urkelTypes} = require('urkel').Proof;
const {forValue} = require('./util/common');

const network = Network.get('regtest');
const {
Expand Down Expand Up @@ -95,14 +96,8 @@ describe('SPV', function() {

it('should generate blocks', async () => {
addr = await wallet.receiveAddress(0);
const waiter = new Promise((res, rej) => {
spv.on('connect', (entry) => {
if (entry.height === 10)
res();
});
});
await mineBlocks(10);
await waiter;
await forValue(spv.chain, 'height', 10);
assert.strictEqual(full.chain.height, spv.chain.height);
});

Expand Down Expand Up @@ -209,18 +204,26 @@ describe('SPV', function() {
const proofType = await waiter1;
assert.strictEqual(proofType, urkelTypes.TYPE_EXISTS);

// Restore
const waiter2 = new Promise((res, rej) => {
spv.on('connect', (entry) => {
if (entry.height === full.chain.height)
res();
});
});
await spv.chain.removeInvalid(entry.hash);
await waiter2;
await forValue(spv.chain, 'height', full.chain.height);
assert.strictEqual(full.chain.height, spv.chain.height);
});

it('should resync multiple times', async () => {
const resetHeight = 1;
const fullNodeHeight = full.chain.height;

// Rescan once
await spv.chain.reset(resetHeight);
await forValue(spv.chain, 'height', fullNodeHeight);
assert(spv.chain.height === fullNodeHeight);

// Rescan again
await spv.chain.reset(resetHeight);
await forValue(spv.chain, 'height', fullNodeHeight);
assert(spv.chain.height === fullNodeHeight);
});

it('should request name data with unknown tree root', async () => {
// SPV node teleports to a parallel dimension
await mineSPVFork(100, full.chain.tip);
Expand Down

0 comments on commit 6bc818d

Please sign in to comment.