From 8896ff9665b34180bfb1b3fda6792a9a9a5efdea Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sun, 23 Oct 2016 15:04:55 -0700 Subject: [PATCH] test: clean up wallet tests. --- test/wallet-test.js | 93 ++++++++++----------------------------------- 1 file changed, 20 insertions(+), 73 deletions(-) diff --git a/test/wallet-test.js b/test/wallet-test.js index 952e9cde..8a2f216c 100644 --- a/test/wallet-test.js +++ b/test/wallet-test.js @@ -10,6 +10,7 @@ var assert = require('assert'); var scriptTypes = constants.scriptTypes; var co = require('../lib/utils/co'); var cob = co.cob; +var WalletBlock = require('../lib/wallet/walletblock'); var KEY1 = 'xprv9s21ZrQH143K3Aj6xQBymM31Zb4BVc7wxqfUhMZrzewdDVCt' + 'qUP9iWfcHgJofs25xbaUpCps9GDXj83NiWvQCAkWQhVj5J4CorfnpKX94AZ'; @@ -17,6 +18,15 @@ var KEY1 = 'xprv9s21ZrQH143K3Aj6xQBymM31Zb4BVc7wxqfUhMZrzewdDVCt' var KEY2 = 'xprv9s21ZrQH143K3mqiSThzPtWAabQ22Pjp3uSNnZ53A5bQ4udp' + '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) { hash = hash || crypto.randomBytes(32).toString('hex'); return { @@ -192,9 +202,6 @@ describe('Wallet', function() { .addOutput(w.getAddress(), 1000); t1.addInput(dummy()); t1.ts = utils.now(); - t1.height = 1; - t1.block = constants.NULL_HASH; - t1.index = 0; // balance: 51000 yield w.sign(t1); @@ -290,12 +297,6 @@ describe('Wallet', function() { assert(txs.some(function(tx) { 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* () { @@ -304,10 +305,6 @@ describe('Wallet', function() { tx = bcoin.mtx().addOutput(w.getAddress(), 5000); tx.addInput(doubleSpend.coin); - tx.ts = utils.now(); - tx.height = 1; - tx.block = constants.NULL_HASH; - tx.index = 0; txs = yield w.getHistory(); assert.equal(txs.length, 5); @@ -357,10 +354,6 @@ describe('Wallet', function() { .addOutput(w.getAddress(), 50000) .addOutput(w.getAddress(), 1000); t1.addInput(dummy()); - t1.ts = utils.now(); - t1.height = 1; - t1.block = constants.NULL_HASH; - t1.index = 0; // balance: 51000 yield w.sign(t1); @@ -455,37 +448,19 @@ describe('Wallet', function() { 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); - t3.ts = utils.now(); - t3.height = 1; - t3.block = constants.NULL_HASH; - t3.index = 0; yield walletdb.addTX(t3); - t4.ts = utils.now(); - t4.height = 1; - t4.block = constants.NULL_HASH; - t4.index = 0; yield walletdb.addTX(t4); - f1.ts = utils.now(); - f1.height = 1; - f1.block = constants.NULL_HASH; - f1.index = 0; yield walletdb.addTX(f1); balance = yield w.getBalance(); assert.equal(balance.unconfirmed, 11000); - assert.equal(balance.confirmed, 11000); balance = yield f.getBalance(); assert.equal(balance.unconfirmed, 10000); - assert.equal(balance.confirmed, 10000); })); it('should fill tx with inputs', cob(function* () { @@ -733,16 +708,15 @@ describe('Wallet', function() { utx = utx.toTX(); // Simulate a confirmation - utx.ts = 1; - utx.height = 1; - utx.block = constants.NULL_HASH; + var block = nextBlock(); + utx.ts = block.ts; + utx.height = block.height; + utx.block = block.hash; utx.index = 0; assert.equal(w1[depth], 1); - yield walletdb.addTX(utx); - yield walletdb.addTX(utx); - yield walletdb.addTX(utx); + yield walletdb.addBlock(block, [utx]); assert.equal(w1[depth], 2); @@ -777,14 +751,13 @@ describe('Wallet', function() { assert.equal(w3.change.getAddress('base58'), change); // Simulate a confirmation - send.ts = 1; - send.height = 1; - send.block = constants.NULL_HASH; + var block = nextBlock(); + send.ts = block.ts; + send.height = block.height; + send.block = block.hash; send.index = 0; - yield walletdb.addTX(send); - yield walletdb.addTX(send); - yield walletdb.addTX(send); + yield walletdb.addBlock(block, [send]); assert.equal(w1[depth], 2); assert.equal(w1.changeDepth, 2); @@ -1219,10 +1192,6 @@ describe('Wallet', function() { t1 = bcoin.mtx() .addOutput(addr, 50000); t1.addInput(dummy()); - t1.ts = utils.now(); - t1.block = constants.NULL_HASH; - t1.height = 1; - t1.index = 0; yield alice.sign(t1); t1 = t1.toTX(); @@ -1260,20 +1229,11 @@ describe('Wallet', function() { yield bob.add(t3); assert.equal((yield alice.getBalance()).unconfirmed, 30000); - // assert.equal((yield bob.getBalance()).unconfirmed, 80000); // 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); // 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); assert.equal((yield bob.getBalance()).unconfirmed, 30000); @@ -1299,10 +1259,6 @@ describe('Wallet', function() { t1 = bcoin.mtx() .addOutput(addr, 50000); t1.addInput(dummy()); - t1.ts = utils.now(); - t1.block = constants.NULL_HASH; - t1.height = 1; - t1.index = 0; yield alice.sign(t1); t1 = t1.toTX(); @@ -1351,20 +1307,11 @@ describe('Wallet', function() { yield bob.add(t3); assert.equal((yield alice.getBalance()).unconfirmed, 30000); - // assert.equal((yield bob.getBalance()).unconfirmed, 80000); // 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); // 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); assert.equal((yield bob.getBalance()).unconfirmed, 30000);