refactor. tests.
This commit is contained in:
parent
0b1b6fdb33
commit
3efdc904e7
@ -30,13 +30,11 @@ function Mempool(node, options) {
|
|||||||
this.options = options;
|
this.options = options;
|
||||||
this.node = node;
|
this.node = node;
|
||||||
this.chain = node.chain;
|
this.chain = node.chain;
|
||||||
this.db = node.chain.db.db;
|
// this.db = node.chain.db.db;
|
||||||
|
|
||||||
if (this.options.memory) {
|
this.db = bcoin.ldb('mempool', {
|
||||||
this.db = bcoin.ldb('mempool', {
|
db: 'memdown'
|
||||||
db: 'memdown'
|
});
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
this.tx = new bcoin.txdb('m', this.db, {
|
this.tx = new bcoin.txdb('m', this.db, {
|
||||||
indexSpent: true,
|
indexSpent: true,
|
||||||
@ -46,21 +44,19 @@ function Mempool(node, options) {
|
|||||||
verify: false
|
verify: false
|
||||||
});
|
});
|
||||||
|
|
||||||
this.txs = {};
|
|
||||||
this.spent = {};
|
|
||||||
this.addresses = {};
|
|
||||||
this.size = 0;
|
|
||||||
this.count = 0;
|
|
||||||
this.locked = false;
|
|
||||||
this.loaded = false;
|
this.loaded = false;
|
||||||
|
|
||||||
this.jobs = [];
|
this.jobs = [];
|
||||||
this.busy = false;
|
this.busy = false;
|
||||||
|
|
||||||
this.pending = [];
|
this.pending = [];
|
||||||
this.pendingTX = {};
|
this.pendingTX = {};
|
||||||
this.pendingSize = 0;
|
this.pendingSize = 0;
|
||||||
this.pendingLimit = 20 << 20;
|
this.pendingLimit = 20 << 20;
|
||||||
|
|
||||||
this.freeCount = 0;
|
this.freeCount = 0;
|
||||||
this.lastTime = 0;
|
this.lastTime = 0;
|
||||||
|
|
||||||
this.limitFreeRelay = this.options.limitFreeRelay || 15;
|
this.limitFreeRelay = this.options.limitFreeRelay || 15;
|
||||||
this.requireStandard = this.options.requireStandard !== false;
|
this.requireStandard = this.options.requireStandard !== false;
|
||||||
this.limitFree = this.options.limitFree !== false;
|
this.limitFree = this.options.limitFree !== false;
|
||||||
@ -290,11 +286,8 @@ Mempool.prototype.addTX = function addTX(tx, peer, callback, force) {
|
|||||||
|
|
||||||
self.verify(tx, function(err) {
|
self.verify(tx, function(err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
if (err.type === 'VerifyError') {
|
if (err.type === 'VerifyError' && err.score >= 0)
|
||||||
if (err.score !== -1)
|
peer.sendReject(tx, err.reason, err.score);
|
||||||
peer.sendReject(tx, err.reason, err.score);
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
return callback(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
154
test/node-test.js
Normal file
154
test/node-test.js
Normal file
@ -0,0 +1,154 @@
|
|||||||
|
var assert = require('assert');
|
||||||
|
var bn = require('bn.js');
|
||||||
|
var bcoin = require('../');
|
||||||
|
var constants = bcoin.protocol.constants;
|
||||||
|
var utils = bcoin.utils;
|
||||||
|
|
||||||
|
describe('Wallet', function() {
|
||||||
|
process.env.BCOIN_DB = 'memdown';
|
||||||
|
var node = new bcoin.fullnode();
|
||||||
|
node.on('error', function() {});
|
||||||
|
|
||||||
|
it('should open node', function(cb) {
|
||||||
|
node.open(cb);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should have wallet', function(cb) {
|
||||||
|
delete process.env.BCOIN_DB;
|
||||||
|
node.getWallet('primary', function(err, wallet) {
|
||||||
|
if (err)
|
||||||
|
return cb(err);
|
||||||
|
|
||||||
|
node.wallet = wallet;
|
||||||
|
|
||||||
|
cb();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should have TX pool and be serializable', function(cb) {
|
||||||
|
var w = node.wallet;
|
||||||
|
|
||||||
|
// Coinbase
|
||||||
|
var t1 = bcoin.mtx().addOutput(w, 50000).addOutput(w, 10000); // 10000 instead of 1000
|
||||||
|
var prev = new bcoin.script([w.publicKey, 'checksig']);
|
||||||
|
var dummyInput = {
|
||||||
|
prevout: {
|
||||||
|
hash: constants.oneHash,
|
||||||
|
index: 0
|
||||||
|
},
|
||||||
|
output: {
|
||||||
|
version: 1,
|
||||||
|
height: 0,
|
||||||
|
value: new bn(70000),
|
||||||
|
script: prev,
|
||||||
|
coinbase: false,
|
||||||
|
hash: constants.oneHash,
|
||||||
|
index: 0
|
||||||
|
},
|
||||||
|
script: new bcoin.script([]),
|
||||||
|
sequence: 0xffffffff
|
||||||
|
};
|
||||||
|
t1.addInput(dummyInput);
|
||||||
|
t1.inputs[0].script = new bcoin.script([t1.createSignature(0, prev, w.privateKey, 'all', 0)]),
|
||||||
|
|
||||||
|
// balance: 51000
|
||||||
|
w.sign(t1);
|
||||||
|
var t2 = bcoin.mtx().addInput(t1, 0) // 50000
|
||||||
|
.addOutput(w, 20000)
|
||||||
|
.addOutput(w, 20000);
|
||||||
|
// balance: 49000
|
||||||
|
w.sign(t2);
|
||||||
|
var t3 = bcoin.mtx().addInput(t1, 1) // 10000
|
||||||
|
.addInput(t2, 0) // 20000
|
||||||
|
.addOutput(w, 23000);
|
||||||
|
// balance: 47000
|
||||||
|
w.sign(t3);
|
||||||
|
var t4 = bcoin.mtx().addInput(t2, 1) // 24000
|
||||||
|
.addInput(t3, 0) // 23000
|
||||||
|
.addOutput(w, 11000)
|
||||||
|
.addOutput(w, 11000);
|
||||||
|
// balance: 22000
|
||||||
|
w.sign(t4);
|
||||||
|
var f1 = bcoin.mtx().addInput(t4, 1) // 11000
|
||||||
|
.addOutput(new bcoin.wallet(), 9000);
|
||||||
|
// balance: 11000
|
||||||
|
w.sign(f1);
|
||||||
|
var fake = bcoin.mtx().addInput(t1, 1) // 1000 (already redeemed)
|
||||||
|
.addOutput(w, 6000); // 6000 instead of 500
|
||||||
|
// Script inputs but do not sign
|
||||||
|
w.scriptInputs(fake);
|
||||||
|
// Fake signature
|
||||||
|
fake.inputs[0].script.code[0] = new Buffer([0,0,0,0,0,0,0,0,0]);
|
||||||
|
// balance: 11000
|
||||||
|
[t2, t3, t4, f1, fake].forEach(function(tx) {
|
||||||
|
tx.inputs.forEach(function(input) {
|
||||||
|
delete input.output;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// Just for debugging
|
||||||
|
t1.hint = 't1';
|
||||||
|
t2.hint = 't2';
|
||||||
|
t3.hint = 't3';
|
||||||
|
t4.hint = 't4';
|
||||||
|
f1.hint = 'f1';
|
||||||
|
fake.hint = 'fake';
|
||||||
|
|
||||||
|
var peer = { sendReject: function() {} };
|
||||||
|
|
||||||
|
node.mempool.addTX(fake, peer, function(err) {
|
||||||
|
if (err)
|
||||||
|
throw err;
|
||||||
|
assert(!err);
|
||||||
|
node.mempool.addTX(t4, peer, function(err) {
|
||||||
|
assert(!err);
|
||||||
|
node.mempool.getBalance(function(err, balance) {
|
||||||
|
assert(!err);
|
||||||
|
assert.equal(balance.toString(10), '0');
|
||||||
|
node.mempool.addTX(t1, peer, function(err) {
|
||||||
|
if (err)
|
||||||
|
throw err;
|
||||||
|
node.mempool.getBalance(function(err, balance) {
|
||||||
|
assert(!err);
|
||||||
|
assert.equal(balance.toString(10), '60000');
|
||||||
|
node.mempool.addTX(t2, peer, function(err) {
|
||||||
|
if (err)
|
||||||
|
throw err;
|
||||||
|
assert(!err);
|
||||||
|
node.mempool.getBalance(function(err, balance) {
|
||||||
|
assert(!err);
|
||||||
|
assert.equal(balance.toString(10), '50000');
|
||||||
|
node.mempool.addTX(t3, peer, function(err) {
|
||||||
|
assert(!err);
|
||||||
|
node.mempool.getBalance(function(err, balance) {
|
||||||
|
assert(!err);
|
||||||
|
assert.equal(balance.toString(10), '22000');
|
||||||
|
node.mempool.addTX(f1, peer, function(err) {
|
||||||
|
if (err)
|
||||||
|
throw err;
|
||||||
|
assert(!err);
|
||||||
|
node.mempool.getBalance(function(err, balance) {
|
||||||
|
if (err)
|
||||||
|
throw err;
|
||||||
|
assert(!err);
|
||||||
|
assert.equal(balance.toString(10), '20000');
|
||||||
|
node.mempool.getAll(function(err, txs) {
|
||||||
|
assert(txs.some(function(tx) {
|
||||||
|
return tx.hash('hex') === f1.hash('hex');
|
||||||
|
}));
|
||||||
|
|
||||||
|
cb();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
Loading…
Reference in New Issue
Block a user