Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spv send relay #4

Merged
merged 3 commits into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/net/pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -1126,8 +1126,12 @@ class Pool extends EventEmitter {
this.handleConnect(peer);
});

peer.once('open', () => {
this.handleOpen(peer);
peer.once('open', async () => {
try {
await this.handleOpen(peer);
} catch (e) {
this.emit('error', e);
}
});

peer.once('close', (connected) => {
Expand Down
34 changes: 27 additions & 7 deletions lib/node/spvnode.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const {Lock} = require('bmutex');
const Chain = require('../blockchain/chain');
const Pool = require('../net/pool');
const Node = require('./node');
const {VerifyError} = require('../protocol/errors');
const HTTP = require('./http');
const RPC = require('./rpc');

Expand Down Expand Up @@ -273,15 +274,30 @@ class SPVNode extends Node {
}

/**
* Broadcast a transaction (note that this will _not_ be verified
* by the mempool - use with care, lest you get banned from
* bitcoind nodes).
* Add transaction to txFilter, broadcast (note that this will
* _not_ be verified by the mempool - use with care, lest you
* get banned from bitcoind nodes).
* @param {TX} tx
* @returns {Promise}
*/

sendTX(tx) {
return this.broadcast(tx);
async sendTX(tx) {
const [valid, reason, score] = tx.checkSanity();
if (score === 0) {
if (!valid) {
this.error(new VerifyError(tx, 'invalid', reason, score));
this.logger.warning('Verification failed for tx: %s.', tx.txid());
this.logger.warning('Attempting to broadcast anyway...');
}

if (tx.isWatched(this.pool.spvFilter)) {
this.emit('tx', tx);
}

// We need to announce by hand since
// spv nodes are always selfish
this.broadcast(tx);
}
}

/**
Expand All @@ -290,8 +306,12 @@ class SPVNode extends Node {
* @returns {Promise}
*/

relay(tx) {
return this.broadcast(tx);
async relay(tx) {
try {
await this.sendTX(tx);
} catch (e) {
this.error(e);
}
}

/**
Expand Down
23 changes: 23 additions & 0 deletions test/node-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,29 @@ describe('Node', function() {
assert.strictEqual(meta.tx.txid(), tx2.txid());
});

it('should wake listeners of \'tx\' when sending', async () => {
let notified = false;
const notifier = () => {
notified = true;
};
node.on('tx', notifier);

const addr = Address.fromString(
'17SXrfEbhsLme7HSspiLSoWGMGTchMMr3P');
const mtx = await wallet.createTX({
outputs: [{
value: 10 * 1e8,
address: addr.toString(node.network)
}]
}, false);
await wallet.sign(mtx);

await node.sendTX(mtx.toTX());

assert(notified);
node.removeListener('tx', notifier);
});

it('should cleanup', async () => {
consensus.COINBASE_MATURITY = 100;
await node.close();
Expand Down
61 changes: 61 additions & 0 deletions test/spvnode-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* eslint-env mocha */
/* eslint prefer-arrow-callback: "off" */

'use strict';

const assert = require('bsert');
const SPVNode = require('../lib/node/spvnode');
const Input = require('../lib/primitives/input');
const Outpoint = require('../lib/primitives/outpoint');
const MTX = require('../lib/primitives/mtx');
const random = require('bcrypto/lib/random');

function dummyInput() {
const hash = random.randomBytes(32);
return Input.fromOutpoint(new Outpoint(hash, 0));
}

const node = new SPVNode({
network: 'regtest',
plugins: [require('../lib/wallet/plugin')]
});

const {wdb} = node.require('walletdb');

let wallet = null;

describe.only('SPV Node', function() {
this.timeout(process.browser ? 20000 : 5000);

if (process.browser)
return;

it('should open node', async () => {
await node.open();
});

it('should open walletdb', async () => {
wallet = await wdb.create();
});

it('should wake listeners of \'tx\' when sending', async () => {
let notified = false;
const notifier = () => {
notified = true;
};
node.on('tx', notifier);

const mtx = new MTX();
mtx.addInput(dummyInput());
mtx.addOutput(await wallet.receiveAddress(), 5460);

await node.sendTX(mtx.toTX());

assert(notified);
node.removeListener('tx', notifier);
});

it('should cleanup', async () => {
await node.close();
});
});