test: clean up wallet tests.

This commit is contained in:
Christopher Jeffrey 2016-10-23 15:04:55 -07:00
parent 6e1c4d27b5
commit 8896ff9665
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -10,6 +10,7 @@ var assert = require('assert');
var scriptTypes = constants.scriptTypes; var scriptTypes = constants.scriptTypes;
var co = require('../lib/utils/co'); var co = require('../lib/utils/co');
var cob = co.cob; var cob = co.cob;
var WalletBlock = require('../lib/wallet/walletblock');
var KEY1 = 'xprv9s21ZrQH143K3Aj6xQBymM31Zb4BVc7wxqfUhMZrzewdDVCt' var KEY1 = 'xprv9s21ZrQH143K3Aj6xQBymM31Zb4BVc7wxqfUhMZrzewdDVCt'
+ 'qUP9iWfcHgJofs25xbaUpCps9GDXj83NiWvQCAkWQhVj5J4CorfnpKX94AZ'; + 'qUP9iWfcHgJofs25xbaUpCps9GDXj83NiWvQCAkWQhVj5J4CorfnpKX94AZ';
@ -17,6 +18,15 @@ var KEY1 = 'xprv9s21ZrQH143K3Aj6xQBymM31Zb4BVc7wxqfUhMZrzewdDVCt'
var KEY2 = 'xprv9s21ZrQH143K3mqiSThzPtWAabQ22Pjp3uSNnZ53A5bQ4udp' var KEY2 = 'xprv9s21ZrQH143K3mqiSThzPtWAabQ22Pjp3uSNnZ53A5bQ4udp'
+ 'faKekc2m4AChLYH1XDzANhrSdxHYWUeTWjYJwFwWFyHkTMnMeAcW4JyRCZa'; + 'faKekc2m4AChLYH1XDzANhrSdxHYWUeTWjYJwFwWFyHkTMnMeAcW4JyRCZa';
var globalHeight = 1;
function nextBlock(height) {
var hash;
height = height != null ? height : globalHeight++;
hash = crypto.hash256(utils.U32(height)).toString('hex');
return new WalletBlock(hash, height++, utils.now());
}
function dummy(hash) { function dummy(hash) {
hash = hash || crypto.randomBytes(32).toString('hex'); hash = hash || crypto.randomBytes(32).toString('hex');
return { return {
@ -192,9 +202,6 @@ describe('Wallet', function() {
.addOutput(w.getAddress(), 1000); .addOutput(w.getAddress(), 1000);
t1.addInput(dummy()); t1.addInput(dummy());
t1.ts = utils.now(); t1.ts = utils.now();
t1.height = 1;
t1.block = constants.NULL_HASH;
t1.index = 0;
// balance: 51000 // balance: 51000
yield w.sign(t1); yield w.sign(t1);
@ -290,12 +297,6 @@ describe('Wallet', function() {
assert(txs.some(function(tx) { assert(txs.some(function(tx) {
return tx.hash('hex') === f1.hash('hex'); return tx.hash('hex') === f1.hash('hex');
})); }));
t2.ts = utils.now();
t2.height = 1;
t2.block = constants.NULL_HASH;
t2.index = 0;
yield walletdb.addTX(t2);
})); }));
it('should cleanup spenders after double-spend', cob(function* () { it('should cleanup spenders after double-spend', cob(function* () {
@ -304,10 +305,6 @@ describe('Wallet', function() {
tx = bcoin.mtx().addOutput(w.getAddress(), 5000); tx = bcoin.mtx().addOutput(w.getAddress(), 5000);
tx.addInput(doubleSpend.coin); tx.addInput(doubleSpend.coin);
tx.ts = utils.now();
tx.height = 1;
tx.block = constants.NULL_HASH;
tx.index = 0;
txs = yield w.getHistory(); txs = yield w.getHistory();
assert.equal(txs.length, 5); assert.equal(txs.length, 5);
@ -357,10 +354,6 @@ describe('Wallet', function() {
.addOutput(w.getAddress(), 50000) .addOutput(w.getAddress(), 50000)
.addOutput(w.getAddress(), 1000); .addOutput(w.getAddress(), 1000);
t1.addInput(dummy()); t1.addInput(dummy());
t1.ts = utils.now();
t1.height = 1;
t1.block = constants.NULL_HASH;
t1.index = 0;
// balance: 51000 // balance: 51000
yield w.sign(t1); yield w.sign(t1);
@ -455,37 +448,19 @@ describe('Wallet', function() {
return tx.hash('hex') === f1.hash('hex'); return tx.hash('hex') === f1.hash('hex');
})); }));
t2.ts = utils.now();
t2.height = 1;
t2.block = constants.NULL_HASH;
t2.index = 0;
yield walletdb.addTX(t2); yield walletdb.addTX(t2);
t3.ts = utils.now();
t3.height = 1;
t3.block = constants.NULL_HASH;
t3.index = 0;
yield walletdb.addTX(t3); yield walletdb.addTX(t3);
t4.ts = utils.now();
t4.height = 1;
t4.block = constants.NULL_HASH;
t4.index = 0;
yield walletdb.addTX(t4); yield walletdb.addTX(t4);
f1.ts = utils.now();
f1.height = 1;
f1.block = constants.NULL_HASH;
f1.index = 0;
yield walletdb.addTX(f1); yield walletdb.addTX(f1);
balance = yield w.getBalance(); balance = yield w.getBalance();
assert.equal(balance.unconfirmed, 11000); assert.equal(balance.unconfirmed, 11000);
assert.equal(balance.confirmed, 11000);
balance = yield f.getBalance(); balance = yield f.getBalance();
assert.equal(balance.unconfirmed, 10000); assert.equal(balance.unconfirmed, 10000);
assert.equal(balance.confirmed, 10000);
})); }));
it('should fill tx with inputs', cob(function* () { it('should fill tx with inputs', cob(function* () {
@ -733,16 +708,15 @@ describe('Wallet', function() {
utx = utx.toTX(); utx = utx.toTX();
// Simulate a confirmation // Simulate a confirmation
utx.ts = 1; var block = nextBlock();
utx.height = 1; utx.ts = block.ts;
utx.block = constants.NULL_HASH; utx.height = block.height;
utx.block = block.hash;
utx.index = 0; utx.index = 0;
assert.equal(w1[depth], 1); assert.equal(w1[depth], 1);
yield walletdb.addTX(utx); yield walletdb.addBlock(block, [utx]);
yield walletdb.addTX(utx);
yield walletdb.addTX(utx);
assert.equal(w1[depth], 2); assert.equal(w1[depth], 2);
@ -777,14 +751,13 @@ describe('Wallet', function() {
assert.equal(w3.change.getAddress('base58'), change); assert.equal(w3.change.getAddress('base58'), change);
// Simulate a confirmation // Simulate a confirmation
send.ts = 1; var block = nextBlock();
send.height = 1; send.ts = block.ts;
send.block = constants.NULL_HASH; send.height = block.height;
send.block = block.hash;
send.index = 0; send.index = 0;
yield walletdb.addTX(send); yield walletdb.addBlock(block, [send]);
yield walletdb.addTX(send);
yield walletdb.addTX(send);
assert.equal(w1[depth], 2); assert.equal(w1[depth], 2);
assert.equal(w1.changeDepth, 2); assert.equal(w1.changeDepth, 2);
@ -1219,10 +1192,6 @@ describe('Wallet', function() {
t1 = bcoin.mtx() t1 = bcoin.mtx()
.addOutput(addr, 50000); .addOutput(addr, 50000);
t1.addInput(dummy()); t1.addInput(dummy());
t1.ts = utils.now();
t1.block = constants.NULL_HASH;
t1.height = 1;
t1.index = 0;
yield alice.sign(t1); yield alice.sign(t1);
t1 = t1.toTX(); t1 = t1.toTX();
@ -1260,20 +1229,11 @@ describe('Wallet', function() {
yield bob.add(t3); yield bob.add(t3);
assert.equal((yield alice.getBalance()).unconfirmed, 30000); assert.equal((yield alice.getBalance()).unconfirmed, 30000);
// assert.equal((yield bob.getBalance()).unconfirmed, 80000);
// Bob sees t2 on the chain. // Bob sees t2 on the chain.
t2.height = 3;
t2.ts = utils.now();
t2.block = constants.NULL_HASH;
t2.index = 0;
yield bob.add(t2); yield bob.add(t2);
// Bob sees t3 on the chain. // Bob sees t3 on the chain.
t3.height = 3;
t3.ts = utils.now();
t3.block = constants.NULL_HASH;
t3.index = 0;
yield bob.add(t3); yield bob.add(t3);
assert.equal((yield bob.getBalance()).unconfirmed, 30000); assert.equal((yield bob.getBalance()).unconfirmed, 30000);
@ -1299,10 +1259,6 @@ describe('Wallet', function() {
t1 = bcoin.mtx() t1 = bcoin.mtx()
.addOutput(addr, 50000); .addOutput(addr, 50000);
t1.addInput(dummy()); t1.addInput(dummy());
t1.ts = utils.now();
t1.block = constants.NULL_HASH;
t1.height = 1;
t1.index = 0;
yield alice.sign(t1); yield alice.sign(t1);
t1 = t1.toTX(); t1 = t1.toTX();
@ -1351,20 +1307,11 @@ describe('Wallet', function() {
yield bob.add(t3); yield bob.add(t3);
assert.equal((yield alice.getBalance()).unconfirmed, 30000); assert.equal((yield alice.getBalance()).unconfirmed, 30000);
// assert.equal((yield bob.getBalance()).unconfirmed, 80000);
// Bob sees t2 on the chain. // Bob sees t2 on the chain.
t2.height = 3;
t2.ts = utils.now();
t2.block = constants.NULL_HASH;
t2.index = 0;
yield bob.add(t2); yield bob.add(t2);
// Bob sees t3 on the chain. // Bob sees t3 on the chain.
t3.height = 3;
t3.ts = utils.now();
t3.block = constants.NULL_HASH;
t3.index = 0;
yield bob.add(t3); yield bob.add(t3);
assert.equal((yield bob.getBalance()).unconfirmed, 30000); assert.equal((yield bob.getBalance()).unconfirmed, 30000);