itns-sidechain/test/node-test.js

709 lines
17 KiB
JavaScript
Raw Normal View History

2017-07-30 16:49:24 -07:00
/* eslint-env mocha */
2017-07-31 00:34:42 -07:00
/* eslint prefer-arrow-callback: "off" */
2017-07-30 16:49:24 -07:00
2017-01-30 18:37:28 -08:00
'use strict';
2017-08-10 11:17:10 -07:00
const assert = require('./util/assert');
2017-06-29 20:54:07 -07:00
const consensus = require('../lib/protocol/consensus');
const Coin = require('../lib/primitives/coin');
const Script = require('../lib/script/script');
const Opcode = require('../lib/script/opcode');
2017-06-29 20:54:07 -07:00
const FullNode = require('../lib/node/fullnode');
const MTX = require('../lib/primitives/mtx');
const TX = require('../lib/primitives/tx');
const Address = require('../lib/primitives/address');
2017-01-30 18:37:28 -08:00
2017-08-09 15:28:03 -07:00
const node = new FullNode({
memory: true,
2017-08-09 15:28:03 -07:00
apiKey: 'foo',
network: 'regtest',
workers: true,
plugins: [require('../lib/wallet/plugin')]
});
2017-01-30 18:37:28 -08:00
2017-08-09 15:28:03 -07:00
const chain = node.chain;
const miner = node.miner;
2017-10-26 11:58:50 -07:00
const {wdb} = node.require('walletdb');
2017-01-30 18:37:28 -08:00
2017-08-09 15:28:03 -07:00
let wallet = null;
let tip1 = null;
let tip2 = null;
let cb1 = null;
let cb2 = null;
let tx1 = null;
let tx2 = null;
2017-08-09 15:28:03 -07:00
async function mineBlock(tip, tx) {
const job = await miner.createJob(tip);
2017-01-30 18:37:28 -08:00
2017-08-09 15:28:03 -07:00
if (!tx)
return await job.mineAsync();
2017-01-30 18:37:28 -08:00
2017-08-09 15:28:03 -07:00
const spend = new MTX();
2017-01-30 18:37:28 -08:00
2017-08-09 15:28:03 -07:00
spend.addTX(tx, 0);
2017-01-30 18:37:28 -08:00
2017-10-17 12:20:24 -07:00
spend.addOutput(await wallet.receiveAddress(), 25 * 1e8);
spend.addOutput(await wallet.changeAddress(), 5 * 1e8);
2017-01-30 18:37:28 -08:00
2017-08-09 15:28:03 -07:00
spend.setLocktime(chain.height);
2017-01-30 18:37:28 -08:00
2017-08-09 15:28:03 -07:00
await wallet.sign(spend);
2017-01-30 18:37:28 -08:00
2017-08-09 15:28:03 -07:00
job.addTX(spend.toTX(), spend.view);
job.refresh();
2017-01-30 18:37:28 -08:00
2017-08-09 15:28:03 -07:00
return await job.mineAsync();
}
2017-01-30 18:37:28 -08:00
2017-08-09 15:28:03 -07:00
async function mineCSV(fund) {
const job = await miner.createJob();
const spend = new MTX();
spend.addOutput({
script: [
Opcode.fromInt(1),
Opcode.fromSymbol('checksequenceverify')
2017-08-09 15:28:03 -07:00
],
value: 10 * 1e8
});
spend.addTX(fund, 0);
spend.setLocktime(chain.height);
await wallet.sign(spend);
const [tx, view] = spend.commit();
job.addTX(tx, view);
job.refresh();
return await job.mineAsync();
}
describe('Node', function() {
this.timeout(5000);
2017-01-30 18:37:28 -08:00
2017-06-29 20:54:07 -07:00
it('should open chain and miner', async () => {
2017-01-30 18:37:28 -08:00
miner.mempool = null;
consensus.COINBASE_MATURITY = 0;
await node.open();
});
2017-01-30 18:37:28 -08:00
2017-06-29 20:54:07 -07:00
it('should open walletdb', async () => {
2017-07-25 15:56:02 -07:00
wallet = await wdb.create();
2017-01-30 18:37:28 -08:00
miner.addresses.length = 0;
2017-10-17 12:20:24 -07:00
miner.addAddress(await wallet.receiveAddress());
});
2017-01-30 18:37:28 -08:00
2017-06-29 20:54:07 -07:00
it('should mine a block', async () => {
2017-07-27 09:27:00 -07:00
const block = await miner.mineBlock();
2017-01-30 18:37:28 -08:00
assert(block);
await chain.add(block);
});
2017-01-30 18:37:28 -08:00
2017-06-29 20:54:07 -07:00
it('should mine competing chains', async () => {
for (let i = 0; i < 10; i++) {
const block1 = await mineBlock(tip1, cb1);
2017-01-30 18:37:28 -08:00
cb1 = block1.txs[0];
const block2 = await mineBlock(tip2, cb2);
2017-01-30 18:37:28 -08:00
cb2 = block2.txs[0];
await chain.add(block1);
2017-01-30 18:37:28 -08:00
await chain.add(block2);
2017-01-30 18:37:28 -08:00
2017-08-09 15:38:33 -07:00
assert.strictEqual(chain.tip.hash, block1.hash('hex'));
2017-01-30 18:37:28 -08:00
tip1 = await chain.getEntry(block1.hash('hex'));
tip2 = await chain.getEntry(block2.hash('hex'));
2017-01-30 18:37:28 -08:00
assert(tip1);
assert(tip2);
assert(!await chain.isMainChain(tip2));
2017-01-30 18:37:28 -08:00
2017-11-01 14:12:18 -07:00
await new Promise(setImmediate);
2017-01-30 18:37:28 -08:00
}
});
2017-01-30 18:37:28 -08:00
2017-06-29 20:54:07 -07:00
it('should have correct chain value', () => {
2017-08-09 15:28:03 -07:00
assert.strictEqual(chain.db.state.value, 55000000000);
assert.strictEqual(chain.db.state.coin, 20);
assert.strictEqual(chain.db.state.tx, 21);
2017-01-30 18:37:28 -08:00
});
2017-06-29 20:54:07 -07:00
it('should have correct balance', async () => {
2017-11-01 14:12:18 -07:00
await new Promise(r => setTimeout(r, 100));
2017-01-30 18:37:28 -08:00
const balance = await wallet.getBalance();
2017-08-09 15:28:03 -07:00
assert.strictEqual(balance.unconfirmed, 550 * 1e8);
assert.strictEqual(balance.confirmed, 550 * 1e8);
});
2017-01-30 18:37:28 -08:00
2017-06-29 20:54:07 -07:00
it('should handle a reorg', async () => {
2017-08-09 15:28:03 -07:00
assert.strictEqual(wdb.state.height, chain.height);
assert.strictEqual(chain.height, 11);
2017-01-30 18:37:28 -08:00
const entry = await chain.getEntry(tip2.hash);
2017-01-30 18:37:28 -08:00
assert(entry);
2017-08-09 15:38:33 -07:00
assert.strictEqual(chain.height, entry.height);
2017-01-30 18:37:28 -08:00
const block = await miner.mineBlock(entry);
2017-01-30 18:37:28 -08:00
assert(block);
let forked = false;
2017-06-29 20:54:07 -07:00
chain.once('reorganize', () => {
2017-01-30 18:37:28 -08:00
forked = true;
});
await chain.add(block);
2017-01-30 18:37:28 -08:00
assert(forked);
2017-08-09 15:38:33 -07:00
assert.strictEqual(chain.tip.hash, block.hash('hex'));
2017-09-06 22:14:04 -07:00
assert(chain.tip.chainwork.gt(tip1.chainwork));
});
2017-01-30 18:37:28 -08:00
2017-06-29 20:54:07 -07:00
it('should have correct chain value', () => {
2017-08-09 15:28:03 -07:00
assert.strictEqual(chain.db.state.value, 60000000000);
assert.strictEqual(chain.db.state.coin, 21);
assert.strictEqual(chain.db.state.tx, 22);
2017-01-30 18:37:28 -08:00
});
2017-06-29 20:54:07 -07:00
it('should have correct balance', async () => {
2017-11-01 14:12:18 -07:00
await new Promise(r => setTimeout(r, 100));
2017-01-30 18:37:28 -08:00
const balance = await wallet.getBalance();
2017-08-09 15:28:03 -07:00
assert.strictEqual(balance.unconfirmed, 1100 * 1e8);
assert.strictEqual(balance.confirmed, 600 * 1e8);
});
2017-01-30 18:37:28 -08:00
2017-06-29 20:54:07 -07:00
it('should check main chain', async () => {
const result = await chain.isMainChain(tip1);
2017-01-30 18:37:28 -08:00
assert(!result);
});
2017-01-30 18:37:28 -08:00
2017-06-29 20:54:07 -07:00
it('should mine a block after a reorg', async () => {
2017-07-27 09:27:00 -07:00
const block = await mineBlock(null, cb2);
2017-01-30 18:37:28 -08:00
await chain.add(block);
2017-01-30 18:37:28 -08:00
const entry = await chain.getEntry(block.hash('hex'));
2017-01-30 18:37:28 -08:00
assert(entry);
2017-08-09 15:38:33 -07:00
assert.strictEqual(chain.tip.hash, entry.hash);
2017-01-30 18:37:28 -08:00
const result = await chain.isMainChain(entry);
2017-01-30 18:37:28 -08:00
assert(result);
});
2017-01-30 18:37:28 -08:00
2017-06-29 20:54:07 -07:00
it('should prevent double spend on new chain', async () => {
2017-07-27 09:27:00 -07:00
const block = await mineBlock(null, cb2);
const tip = chain.tip;
2017-01-30 18:37:28 -08:00
2017-08-09 15:28:03 -07:00
let err;
2017-01-30 18:37:28 -08:00
try {
await chain.add(block);
2017-01-30 18:37:28 -08:00
} catch (e) {
err = e;
}
assert(err);
2017-08-09 15:28:03 -07:00
assert.strictEqual(err.reason, 'bad-txns-inputs-missingorspent');
2017-08-09 15:38:33 -07:00
assert.strictEqual(chain.tip, tip);
});
2017-01-30 18:37:28 -08:00
2017-07-31 00:34:42 -07:00
it('should fail to mine block with coins on an alternate chain', async () => {
2017-07-27 09:27:00 -07:00
const block = await mineBlock(null, cb1);
const tip = chain.tip;
2017-01-30 18:37:28 -08:00
2017-08-09 15:28:03 -07:00
let err;
2017-01-30 18:37:28 -08:00
try {
await chain.add(block);
2017-01-30 18:37:28 -08:00
} catch (e) {
err = e;
}
assert(err);
2017-08-09 15:28:03 -07:00
assert.strictEqual(err.reason, 'bad-txns-inputs-missingorspent');
2017-08-09 15:38:33 -07:00
assert.strictEqual(chain.tip, tip);
});
2017-01-30 18:37:28 -08:00
2017-06-29 20:54:07 -07:00
it('should have correct chain value', () => {
2017-08-09 15:28:03 -07:00
assert.strictEqual(chain.db.state.value, 65000000000);
assert.strictEqual(chain.db.state.coin, 23);
assert.strictEqual(chain.db.state.tx, 24);
2017-01-30 18:37:28 -08:00
});
2017-06-29 20:54:07 -07:00
it('should get coin', async () => {
2017-08-09 15:28:03 -07:00
const block1 = await mineBlock();
await chain.add(block1);
2017-01-30 18:37:28 -08:00
2017-08-09 15:28:03 -07:00
const block2 = await mineBlock(null, block1.txs[0]);
await chain.add(block2);
2017-01-30 18:37:28 -08:00
2017-08-09 15:28:03 -07:00
const tx = block2.txs[1];
const output = Coin.fromTX(tx, 1, chain.height);
2017-01-30 18:37:28 -08:00
const coin = await chain.getCoin(tx.hash('hex'), 1);
2017-01-30 18:37:28 -08:00
2017-08-10 11:17:10 -07:00
assert.bufferEqual(coin.toRaw(), output.toRaw());
});
2017-01-30 18:37:28 -08:00
2017-06-29 20:54:07 -07:00
it('should get balance', async () => {
2017-11-01 14:12:18 -07:00
await new Promise(r => setTimeout(r, 100));
2017-01-30 18:37:28 -08:00
const balance = await wallet.getBalance();
2017-08-09 15:28:03 -07:00
assert.strictEqual(balance.unconfirmed, 1250 * 1e8);
assert.strictEqual(balance.confirmed, 750 * 1e8);
2017-01-30 18:37:28 -08:00
2017-10-17 12:20:24 -07:00
assert((await wallet.receiveDepth()) >= 7);
assert((await wallet.changeDepth()) >= 6);
2017-01-30 18:37:28 -08:00
2017-08-09 15:28:03 -07:00
assert.strictEqual(wdb.state.height, chain.height);
2017-01-30 18:37:28 -08:00
const txs = await wallet.getHistory();
2017-08-09 15:28:03 -07:00
assert.strictEqual(txs.length, 45);
});
2017-01-30 18:37:28 -08:00
2017-06-29 20:54:07 -07:00
it('should get tips and remove chains', async () => {
2017-08-09 15:28:03 -07:00
{
const tips = await chain.db.getTips();
2017-01-30 18:37:28 -08:00
2017-08-09 15:28:03 -07:00
assert.notStrictEqual(tips.indexOf(chain.tip.hash), -1);
assert.strictEqual(tips.length, 2);
}
2017-01-30 18:37:28 -08:00
await chain.db.removeChains();
2017-01-30 18:37:28 -08:00
2017-08-09 15:28:03 -07:00
{
const tips = await chain.db.getTips();
2017-01-30 18:37:28 -08:00
2017-08-09 15:28:03 -07:00
assert.notStrictEqual(tips.indexOf(chain.tip.hash), -1);
assert.strictEqual(tips.length, 1);
}
});
2017-01-30 18:37:28 -08:00
2017-06-29 20:54:07 -07:00
it('should rescan for transactions', async () => {
let total = 0;
2017-01-30 18:37:28 -08:00
await chain.scan(0, wdb.filter, async (block, txs) => {
2017-01-30 18:37:28 -08:00
total += txs.length;
});
2017-08-09 15:28:03 -07:00
assert.strictEqual(total, 26);
});
2017-01-30 18:37:28 -08:00
2017-06-29 20:54:07 -07:00
it('should activate csv', async () => {
2017-07-27 09:27:00 -07:00
const deployments = chain.network.deployments;
2017-01-30 18:37:28 -08:00
const prev = await chain.getPrevious(chain.tip);
const state = await chain.getState(prev, deployments.csv);
2017-08-09 15:38:33 -07:00
assert.strictEqual(state, 0);
2017-01-30 18:37:28 -08:00
for (let i = 0; i < 417; i++) {
const block = await miner.mineBlock();
await chain.add(block);
2017-01-30 18:37:28 -08:00
switch (chain.height) {
case 144: {
const prev = await chain.getPrevious(chain.tip);
const state = await chain.getState(prev, deployments.csv);
2017-08-09 15:38:33 -07:00
assert.strictEqual(state, 1);
2017-01-30 18:37:28 -08:00
break;
}
case 288: {
const prev = await chain.getPrevious(chain.tip);
const state = await chain.getState(prev, deployments.csv);
2017-08-09 15:38:33 -07:00
assert.strictEqual(state, 2);
2017-01-30 18:37:28 -08:00
break;
}
case 432: {
const prev = await chain.getPrevious(chain.tip);
const state = await chain.getState(prev, deployments.csv);
2017-08-09 15:38:33 -07:00
assert.strictEqual(state, 3);
2017-01-30 18:37:28 -08:00
break;
}
2017-01-30 18:37:28 -08:00
}
}
2017-08-09 15:38:33 -07:00
assert.strictEqual(chain.height, 432);
2017-01-30 18:37:28 -08:00
assert(chain.state.hasCSV());
const cache = await chain.db.getStateCache();
2017-08-09 15:28:03 -07:00
assert.deepStrictEqual(cache, chain.db.stateCache);
assert.strictEqual(chain.db.stateCache.updates.length, 0);
assert(await chain.db.verifyDeployments());
});
2017-01-30 18:37:28 -08:00
2017-06-29 20:54:07 -07:00
it('should test csv', async () => {
const tx = (await chain.getBlock(chain.height)).txs[0];
2017-08-09 15:28:03 -07:00
const csvBlock = await mineCSV(tx);
2017-01-30 18:37:28 -08:00
2017-08-09 15:28:03 -07:00
await chain.add(csvBlock);
2017-01-30 18:37:28 -08:00
2017-08-09 15:28:03 -07:00
const csv = csvBlock.txs[1];
2017-01-30 18:37:28 -08:00
2017-08-09 15:28:03 -07:00
const spend = new MTX();
2017-01-30 18:37:28 -08:00
2017-08-09 15:28:03 -07:00
spend.addOutput({
2017-01-30 18:37:28 -08:00
script: [
Opcode.fromInt(2),
Opcode.fromSymbol('checksequenceverify')
2017-01-30 18:37:28 -08:00
],
value: 10 * 1e8
});
2017-08-09 15:28:03 -07:00
spend.addTX(csv, 0);
spend.setSequence(0, 1, false);
2017-01-30 18:37:28 -08:00
const job = await miner.createJob();
2017-01-30 18:37:28 -08:00
2017-08-09 15:28:03 -07:00
job.addTX(spend.toTX(), spend.view);
2017-03-10 03:57:44 -08:00
job.refresh();
2017-01-30 18:37:28 -08:00
2017-08-09 15:28:03 -07:00
const block = await job.mineAsync();
2017-01-30 18:37:28 -08:00
await chain.add(block);
});
2017-01-30 18:37:28 -08:00
2017-06-29 20:54:07 -07:00
it('should fail csv with bad sequence', async () => {
const csv = (await chain.getBlock(chain.height)).txs[1];
2017-08-09 15:28:03 -07:00
const spend = new MTX();
2017-01-30 18:37:28 -08:00
2017-08-09 15:28:03 -07:00
spend.addOutput({
2017-01-30 18:37:28 -08:00
script: [
Opcode.fromInt(1),
Opcode.fromSymbol('checksequenceverify')
2017-01-30 18:37:28 -08:00
],
value: 10 * 1e8
});
2017-08-09 15:28:03 -07:00
spend.addTX(csv, 0);
spend.setSequence(0, 1, false);
2017-01-30 18:37:28 -08:00
const job = await miner.createJob();
2017-01-30 18:37:28 -08:00
2017-08-09 15:28:03 -07:00
job.addTX(spend.toTX(), spend.view);
2017-03-10 03:57:44 -08:00
job.refresh();
2017-01-30 18:37:28 -08:00
const block = await job.mineAsync();
2017-01-30 18:37:28 -08:00
let err;
2017-01-30 18:37:28 -08:00
try {
await chain.add(block);
2017-01-30 18:37:28 -08:00
} catch (e) {
err = e;
}
assert(err);
assert(err.reason, 'mandatory-script-verify-flag-failed');
});
2017-01-30 18:37:28 -08:00
2017-06-29 20:54:07 -07:00
it('should mine a block', async () => {
2017-07-27 09:27:00 -07:00
const block = await miner.mineBlock();
2017-01-30 18:37:28 -08:00
assert(block);
await chain.add(block);
});
2017-01-30 18:37:28 -08:00
2017-06-29 20:54:07 -07:00
it('should fail csv lock checks', async () => {
const tx = (await chain.getBlock(chain.height)).txs[0];
2017-08-09 15:28:03 -07:00
const csvBlock = await mineCSV(tx);
2017-01-30 18:37:28 -08:00
2017-08-09 15:28:03 -07:00
await chain.add(csvBlock);
2017-01-30 18:37:28 -08:00
2017-08-09 15:28:03 -07:00
const csv = csvBlock.txs[1];
2017-01-30 18:37:28 -08:00
2017-08-09 15:28:03 -07:00
const spend = new MTX();
2017-01-30 18:37:28 -08:00
2017-08-09 15:28:03 -07:00
spend.addOutput({
2017-01-30 18:37:28 -08:00
script: [
Opcode.fromInt(2),
Opcode.fromSymbol('checksequenceverify')
2017-01-30 18:37:28 -08:00
],
value: 10 * 1e8
});
2017-08-09 15:28:03 -07:00
spend.addTX(csv, 0);
spend.setSequence(0, 2, false);
2017-01-30 18:37:28 -08:00
const job = await miner.createJob();
2017-01-30 18:37:28 -08:00
2017-08-09 15:28:03 -07:00
job.addTX(spend.toTX(), spend.view);
2017-03-10 03:57:44 -08:00
job.refresh();
2017-01-30 18:37:28 -08:00
2017-08-09 15:28:03 -07:00
const block = await job.mineAsync();
2017-01-30 18:37:28 -08:00
let err;
2017-01-30 18:37:28 -08:00
try {
await chain.add(block);
2017-01-30 18:37:28 -08:00
} catch (e) {
err = e;
}
assert(err);
2017-08-09 15:28:03 -07:00
assert.strictEqual(err.reason, 'bad-txns-nonfinal');
});
2017-01-30 18:37:28 -08:00
2017-06-29 20:54:07 -07:00
it('should rescan for transactions', async () => {
2017-07-25 15:56:02 -07:00
await wdb.rescan(0);
2017-10-17 12:20:24 -07:00
assert.strictEqual((await wallet.getBalance()).confirmed, 1289250000000);
});
2017-01-30 18:37:28 -08:00
2017-06-29 20:54:07 -07:00
it('should reset miner mempool', async () => {
2017-03-15 20:21:24 -07:00
miner.mempool = node.mempool;
});
2017-03-15 20:21:24 -07:00
2017-06-29 20:54:07 -07:00
it('should not get a block template', async () => {
2017-07-27 09:27:00 -07:00
const json = await node.rpc.call({
2017-03-15 20:21:24 -07:00
method: 'getblocktemplate'
}, {});
assert(json.error);
2017-08-09 15:28:03 -07:00
assert.strictEqual(json.error.code, -8);
});
2017-03-15 20:21:24 -07:00
2017-06-29 20:54:07 -07:00
it('should get a block template', async () => {
const json = await node.rpc.call({
2017-03-15 20:21:24 -07:00
method: 'getblocktemplate',
params: [
{rules: ['segwit']}
],
id: '1'
}, {});
2017-08-11 18:25:54 -07:00
assert.typeOf(json.result, 'object');
assert.typeOf(json.result.curtime, 'number');
assert.typeOf(json.result.mintime, 'number');
assert.typeOf(json.result.maxtime, 'number');
assert.typeOf(json.result.expires, 'number');
2017-03-15 20:21:24 -07:00
assert.deepStrictEqual(json, {
result: {
2017-07-31 00:34:42 -07:00
capabilities: ['proposal'],
mutable: ['time', 'transactions', 'prevblock'],
2017-03-15 20:21:24 -07:00
version: 536870912,
2017-07-31 00:34:42 -07:00
rules: ['csv', '!segwit', 'testdummy'],
2017-03-15 20:21:24 -07:00
vbavailable: {},
vbrequired: 0,
height: 437,
previousblockhash: node.chain.tip.rhash(),
2017-07-31 00:34:42 -07:00
target:
'7fffff0000000000000000000000000000000000000000000000000000000000',
2017-03-15 20:21:24 -07:00
bits: '207fffff',
noncerange: '00000000ffffffff',
curtime: json.result.curtime,
mintime: json.result.mintime,
maxtime: json.result.maxtime,
expires: json.result.expires,
sigoplimit: 80000,
sizelimit: 4000000,
weightlimit: 4000000,
2017-10-26 04:07:36 -07:00
longpollid: node.chain.tip.rhash() + '00000000',
2017-03-15 20:21:24 -07:00
submitold: false,
coinbaseaux: { flags: '6d696e65642062792062636f696e' },
coinbasevalue: 1250000000,
coinbasetxn: undefined,
2017-07-31 00:34:42 -07:00
default_witness_commitment:
'6a24aa21a9ede2f61c3f71d1defd3fa999dfa36953755c690689799962'
+ 'b48bebd836974e8cf9',
2017-03-15 20:21:24 -07:00
transactions: []
},
error: null,
id: '1'
});
});
2017-03-15 20:21:24 -07:00
2017-06-29 20:54:07 -07:00
it('should send a block template proposal', async () => {
2017-07-27 09:27:00 -07:00
const attempt = await node.miner.createBlock();
2017-03-15 20:21:24 -07:00
attempt.refresh();
const block = attempt.toBlock();
2017-03-15 20:21:24 -07:00
const hex = block.toRaw().toString('hex');
2017-03-15 20:21:24 -07:00
const json = await node.rpc.call({
2017-03-15 20:21:24 -07:00
method: 'getblocktemplate',
params: [{
mode: 'proposal',
data: hex
}]
}, {});
assert(!json.error);
2017-08-09 15:38:33 -07:00
assert.strictEqual(json.result, null);
});
2017-03-15 20:21:24 -07:00
2017-06-29 20:54:07 -07:00
it('should submit a block', async () => {
2017-07-27 09:27:00 -07:00
const block = await node.miner.mineBlock();
const hex = block.toRaw().toString('hex');
2017-03-15 20:21:24 -07:00
const json = await node.rpc.call({
2017-03-15 20:21:24 -07:00
method: 'submitblock',
params: [hex]
}, {});
assert(!json.error);
2017-08-09 15:38:33 -07:00
assert.strictEqual(json.result, null);
2017-08-09 15:28:03 -07:00
assert.strictEqual(node.chain.tip.hash, block.hash('hex'));
});
2017-03-15 20:21:24 -07:00
2017-06-29 20:54:07 -07:00
it('should validate an address', async () => {
2017-07-27 09:27:00 -07:00
const addr = new Address();
2017-03-15 20:21:24 -07:00
const json = await node.rpc.call({
2017-03-15 20:21:24 -07:00
method: 'validateaddress',
params: [addr.toString(node.network)]
2017-03-15 20:21:24 -07:00
}, {});
assert.deepStrictEqual(json.result, {
2017-06-24 14:35:10 -07:00
isvalid: true,
address: addr.toString(node.network),
scriptPubKey: Script.fromAddress(addr, node.network).toJSON(),
2017-06-24 14:35:10 -07:00
ismine: false,
iswatchonly: false
});
});
2017-03-15 20:21:24 -07:00
2017-06-29 20:54:07 -07:00
it('should add transaction to mempool', async () => {
const mtx = await wallet.createTX({
2017-03-15 20:21:24 -07:00
rate: 100000,
outputs: [{
value: 100000,
2017-10-17 12:20:24 -07:00
address: await wallet.receiveAddress()
2017-03-15 20:21:24 -07:00
}]
});
await wallet.sign(mtx);
2017-03-15 20:21:24 -07:00
assert(mtx.isSigned());
const tx = mtx.toTX();
2017-03-15 20:21:24 -07:00
2017-10-17 12:20:24 -07:00
await wdb.addTX(tx);
2017-03-15 20:21:24 -07:00
const missing = await node.mempool.addTX(tx);
2017-08-11 18:25:54 -07:00
assert(!missing);
2017-03-15 20:21:24 -07:00
2017-08-09 15:28:03 -07:00
assert.strictEqual(node.mempool.map.size, 1);
tx1 = mtx;
});
2017-03-15 20:21:24 -07:00
2017-06-29 20:54:07 -07:00
it('should add lesser transaction to mempool', async () => {
const mtx = await wallet.createTX({
2017-03-15 20:21:24 -07:00
rate: 1000,
outputs: [{
value: 50000,
2017-10-17 12:20:24 -07:00
address: await wallet.receiveAddress()
2017-03-15 20:21:24 -07:00
}]
});
await wallet.sign(mtx);
2017-03-15 20:21:24 -07:00
assert(mtx.isSigned());
const tx = mtx.toTX();
2017-03-15 20:21:24 -07:00
2017-10-17 12:20:24 -07:00
await wdb.addTX(tx);
2017-03-15 20:21:24 -07:00
const missing = await node.mempool.addTX(tx);
2017-08-11 18:25:54 -07:00
assert(!missing);
2017-03-15 20:21:24 -07:00
2017-08-09 15:28:03 -07:00
assert.strictEqual(node.mempool.map.size, 2);
tx2 = mtx;
});
2017-03-15 20:21:24 -07:00
2017-06-29 20:54:07 -07:00
it('should get a block template', async () => {
2017-03-15 20:21:24 -07:00
node.rpc.refreshBlock();
const json = await node.rpc.call({
2017-03-15 20:21:24 -07:00
method: 'getblocktemplate',
params: [
{rules: ['segwit']}
],
id: '1'
}, {});
assert(!json.error);
assert(json.result);
const result = json.result;
2017-03-15 20:21:24 -07:00
let fees = 0;
let weight = 0;
for (const item of result.transactions) {
2017-03-15 20:21:24 -07:00
fees += item.fee;
weight += item.weight;
}
2017-08-09 15:28:03 -07:00
assert.strictEqual(result.transactions.length, 2);
assert.strictEqual(fees, tx1.getFee() + tx2.getFee());
assert.strictEqual(weight, tx1.getWeight() + tx2.getWeight());
assert.strictEqual(result.transactions[0].hash, tx1.txid());
assert.strictEqual(result.transactions[1].hash, tx2.txid());
assert.strictEqual(result.coinbasevalue, 125e7 + fees);
});
2017-03-15 20:21:24 -07:00
2017-06-29 20:54:07 -07:00
it('should get raw transaction', async () => {
const json = await node.rpc.call({
2017-03-15 20:21:24 -07:00
method: 'getrawtransaction',
params: [tx2.txid()],
id: '1'
}, {});
assert(!json.error);
const tx = TX.fromRaw(json.result, 'hex');
2017-08-09 15:28:03 -07:00
assert.strictEqual(tx.txid(), tx2.txid());
});
2017-03-15 20:21:24 -07:00
2017-06-29 20:54:07 -07:00
it('should prioritise transaction', async () => {
const json = await node.rpc.call({
2017-03-15 20:21:24 -07:00
method: 'prioritisetransaction',
params: [tx2.txid(), 0, 10000000],
id: '1'
}, {});
assert(!json.error);
2017-08-09 15:38:33 -07:00
assert.strictEqual(json.result, true);
});
2017-03-15 20:21:24 -07:00
2017-06-29 20:54:07 -07:00
it('should get a block template', async () => {
let fees = 0;
let weight = 0;
2017-03-15 20:21:24 -07:00
node.rpc.refreshBlock();
const json = await node.rpc.call({
2017-03-15 20:21:24 -07:00
method: 'getblocktemplate',
params: [
{rules: ['segwit']}
],
id: '1'
}, {});
assert(!json.error);
assert(json.result);
const result = json.result;
2017-03-15 20:21:24 -07:00
for (const item of result.transactions) {
2017-03-15 20:21:24 -07:00
fees += item.fee;
weight += item.weight;
}
2017-08-09 15:28:03 -07:00
assert.strictEqual(result.transactions.length, 2);
assert.strictEqual(fees, tx1.getFee() + tx2.getFee());
assert.strictEqual(weight, tx1.getWeight() + tx2.getWeight());
assert.strictEqual(result.transactions[0].hash, tx2.txid());
assert.strictEqual(result.transactions[1].hash, tx1.txid());
assert.strictEqual(result.coinbasevalue, 125e7 + fees);
});
2017-03-15 20:21:24 -07:00
2017-06-29 20:54:07 -07:00
it('should cleanup', async () => {
2017-01-30 18:37:28 -08:00
consensus.COINBASE_MATURITY = 100;
await node.close();
});
2017-01-30 18:37:28 -08:00
});