Merge pull request #1270 from isocolsky/fix/remove_spent

Removed proposals with spent inputs
This commit is contained in:
Gustavo Maximiliano Cortez 2014-09-04 18:25:11 -03:00
commit db3ca4ceef
6 changed files with 144 additions and 0 deletions

View file

@ -98,6 +98,9 @@ FakeWallet.prototype.getBalance = function(cb) {
return cb(null, this.balance, this.balanceByAddr, this.safeBalance);
};
FakeWallet.prototype.removeTxWithSpentInputs = function (cb) {
};
FakeWallet.prototype.setEnc = function(enc) {
this.enc = enc;
};

View file

@ -65,6 +65,21 @@ describe('TxProposals', function() {
txps.getNtxids().should.deep.equal(['a','b']);
});
});
describe('#deleteOne', function() {
it('should delete specified ntxid', function() {
var txps = new TxProposals();
txps.txps = {a:1, b:2};
txps.deleteOne('a');
txps.getNtxids().should.deep.equal(['b']);
});
it('should fail on non-existent ntxid', function() {
var txps = new TxProposals();
txps.txps = {a:1, b:2};
(function () {
txps.deleteOne('c');
}).should.throw('Unknown TXP: c');
});
});
describe('#toObj', function() {
it('should an object', function() {
var txps = TxProposals.fromObj({

View file

@ -809,6 +809,79 @@ describe('Wallet model', function() {
});
});
describe('removeTxWithSpentInputs', function () {
it('should remove pending TxProposal with spent inputs', function(done) {
var w = cachedCreateW2();
var utxo = createUTXO(w);
chai.expect(w.getTxProposals().length).to.equal(0);
w.blockchain.fixUnspent(utxo);
w.createTx(toAddress, amountSatStr, null, function(ntxid) {
w.sendTxProposal(ntxid);
chai.expect(w.getTxProposals().length).to.equal(1);
// Inputs are still available, txp still valid
w.removeTxWithSpentInputs();
chai.expect(w.getTxProposals().length).to.equal(1);
// Simulate input spent. txp should be removed from txps list
w.blockchain.fixUnspent([]);
w.removeTxWithSpentInputs();
chai.expect(w.getTxProposals().length).to.equal(0);
done();
});
});
it('should remove pending TxProposal with at least 1 spent input', function(done) {
var w = cachedCreateW2();
var utxo = [createUTXO(w)[0], createUTXO(w)[0]];
utxo[0].amount = 80000;
utxo[1].amount = 80000;
utxo[1].vout = 1;
chai.expect(w.getTxProposals().length).to.equal(0);
w.blockchain.fixUnspent(utxo);
w.createTx(toAddress, '100000', null, function(ntxid) {
w.sendTxProposal(ntxid);
chai.expect(w.getTxProposals().length).to.equal(1);
// Inputs are still available, txp still valid
w.removeTxWithSpentInputs();
chai.expect(w.getTxProposals().length).to.equal(1);
// Simulate 1 input spent. txp should be removed from txps list
w.blockchain.fixUnspent([utxo[0]]);
w.removeTxWithSpentInputs();
chai.expect(w.getTxProposals().length).to.equal(0);
done();
});
});
it('should not remove complete TxProposal', function(done) {
var w = cachedCreateW2();
var utxo = createUTXO(w);
chai.expect(w.getTxProposals().length).to.equal(0);
w.blockchain.fixUnspent(utxo);
w.createTx(toAddress, amountSatStr, null, function(ntxid) {
w.sendTxProposal(ntxid);
chai.expect(w.getTxProposals().length).to.equal(1);
// Inputs are still available, txp still valid
w.removeTxWithSpentInputs();
chai.expect(w.getTxProposals().length).to.equal(1);
// Simulate input spent. txp should be removed from txps list
w.blockchain.fixUnspent([]);
var txp = w.txProposals.get(ntxid);
sinon.stub(txp, 'isPending', function () { return false; })
w.removeTxWithSpentInputs();
chai.expect(w.getTxProposals().length).to.equal(1);
done();
});
});
});
describe('#send', function() {
it('should call this.network.send', function() {
var w = cachedCreateW2();