add purge to tx prosposals

This commit is contained in:
Matias Alejo Garcia 2014-08-21 13:12:55 -04:00
parent 60d81e6b9f
commit 4931c9d618
6 changed files with 114 additions and 6 deletions

View File

@ -24,6 +24,12 @@ angular.module('copayApp.controllers').controller('MoreController',
});
};
$scope.purge = function(deleteAll) {
var w = $rootScope.wallet;
var removed = w.purgeTxProposals(deleteAll);
notification.info('Tx Proposals Purged', removed + ' transactions proposals were purged');
};
$scope.updateIndexes = function() {
var w = $rootScope.wallet;

View File

@ -65,6 +65,17 @@ TxProposal.prototype._check = function() {
}
};
TxProposal.prototype.rejectCount = function() {
return Object.keys(this.rejectedBy);
};
TxProposal.prototype.isPending = function(maxRejectCount) {
if (this.rejectCount() < maxRejectCount || p.sentTxid)
return false;
return true;
};
TxProposal.prototype._updateSignedBy = function() {
this._inputSignatures = [];

View File

@ -36,10 +36,25 @@ TxProposals.fromObj = function(o, forceOpts) {
return ret;
};
TxProposals.prototype.length = function() {
return Object.keys(this.txps).length;
};
TxProposals.prototype.getNtxids = function() {
return Object.keys(this.txps);
};
TxProposals.prototype.deleteAll = function() {
this.txps = {};
};
TxProposals.prototype.deletePending = function(maxRejectCount) {
for (var ntxid in this.txps) {
if (this.txps[ntxid].isPending(maxRejectCount))
delete this.txps[ntxid];
};
};
TxProposals.prototype.toObj = function() {
var ret = [];
for (var id in this.txps) {
@ -153,7 +168,8 @@ TxProposals.prototype.getUsedUnspent = function(maxRejectCount) {
for (var i in this.txps) {
var u = this.txps[i].builder.getSelectedUnspent();
var p = this.getTxProposal(i);
if (p.rejectCount > maxRejectCount || p.sentTxid)
if (!p.isPending(maxRejectCount))
continue;
for (var j in u) {

View File

@ -706,6 +706,18 @@ Wallet.prototype.getTxProposals = function() {
return ret;
};
Wallet.prototype.purgeTxProposals = function(deleteAll) {
var m = this.txProposals.length();
if (deleteAll) {
this.txProposals.deleteAll();
} else {
this.txProposals.deletePending(this.maxRejectCount());
}
var n = this.txProposals.length();
return m-n;
};
Wallet.prototype.reject = function(ntxid) {
var txp = this.txProposals.reject(ntxid, this.getMyCopayerId());
@ -1484,6 +1496,17 @@ Wallet.prototype.getBalance = function(cb) {
});
};
// See
// https://github.com/bitpay/copay/issues/1056
//
// maxRejectCount should equal requiredCopayers
// strictly.
//
Wallet.prototype.maxRejectCount = function(cb) {
return this.totalCopayers - this.requiredCopayers;
};
Wallet.prototype.getUnspent = function(cb) {
var self = this;
this.blockchain.getUnspent(this.getAddressesStr(), function(err, unspentList) {
@ -1493,8 +1516,7 @@ Wallet.prototype.getUnspent = function(cb) {
}
var safeUnspendList = [];
var maxRejectCount = self.totalCopayers - self.requiredCopayers;
var uu = self.txProposals.getUsedUnspent(maxRejectCount);
var uu = self.txProposals.getUsedUnspent(self.maxRejectCount());
for (var i in unspentList) {
var u = unspentList[i];

View File

@ -357,6 +357,39 @@ describe('Wallet model', function() {
throw();
});
it.only('#maxRejectCount', function() {
var w = cachedCreateW();
w.maxRejectCount().should.equal(2);
});
describe('#purgeTxProposals', function() {
it('should delete all', function() {
var w = cachedCreateW();
var spy1 = sinon.spy(w.txProposals, 'deleteAll');
var spy2 = sinon.spy(w.txProposals, 'deletePending');
w.purgeTxProposals(1);
spy1.callCount.should.equal(1);
spy2.callCount.should.equal(0);
});
it('should delete pending', function() {
var w = cachedCreateW();
var spy1 = sinon.spy(w.txProposals, 'deleteAll');
var spy2 = sinon.spy(w.txProposals, 'deletePending');
w.purgeTxProposals();
spy1.callCount.should.equal(0);
spy2.callCount.should.equal(1);
});
it('should count deletions', function() {
var w = cachedCreateW();
var s = sinon.stub(w.txProposals, 'length').returns(10);
var n = w.purgeTxProposals();
n.should.equal(0);
});
});
//this test fails randomly
it.skip('call reconnect after interval', function(done) {
this.timeout(10000);
@ -378,6 +411,8 @@ describe('Wallet model', function() {
w.isShared().should.equal(false);
});
it('#isReady', function() {
var w = createW();
w.publicKeyRing.isComplete().should.equal(false);

View File

@ -30,7 +30,7 @@
<h3><i class="fi-minus-circle m10r"></i> Master Private Key </h3>
<p class="large-8 columns text-gray"> Your master private key contains the information to sign <b>any</b> transaction on this wallet. Handle with care.
<div class="large-4 columns">
<a class="button expand" ng-click="hidePriv=!hidePriv">
<a class="button primary expand" ng-click="hidePriv=!hidePriv">
<span ng-hide="!hidePriv">Show</span>
<span ng-hide="hidePriv">Hide</span>
</a>
@ -39,13 +39,31 @@
</div>
<div class="oh large-12 columns panel">
<h3><i class="fi-minus-circle m10r"></i> Scan Wallet Addresses </h3>
<p class="large-8 columns text-gray"> This will scan the blockchain looking for addresses derived from your wallet, in case you have funds in addresses not yet generated (e.g.: you restored an old backup).
<p class="large-8 columns text-gray"> This will scan the blockchain looking for addresses derived from your wallet, in case you have funds in addresses not yet generated (e.g.: you restored an old backup). This will also trigger a syncronization of addresses to other connected peers.
<div class="large-4 columns">
<a class="button expand" ng-click="updateIndexes()">
<a class="button primary expand" ng-click="updateIndexes()">
Scan
</a>
</div>
</div>
<div class="oh large-12 columns panel">
<h3><i class="fi-minus-circle m10r"></i> Purge Pending Transaction Proposals </h3>
<p class="large-8 columns text-gray"> Pending Transactions Proposals will be discarted. This need to be done on <b>ALL<b> peers of a wallet, to prevent the old proposals to be resynced again.
<div class="large-4 columns">
<a class="button warning expand" ng-click="purge()">
Purge
</a>
</div>
</div>
<div class="oh large-12 columns panel">
<h3><i class="fi-minus-circle m10r"></i> Purge ALL Transaction Proposals </h3>
<p class="large-8 columns text-gray"> ALL Transactions Proposals will be discarted. This need to be done on <b>ALL<b> peers of a wallet, to prevent the old proposals to be resynced again.
<div class="large-4 columns">
<a class="button warning expand" ng-click="purge(true)">
Purge All
</a>
</div>
</div>
</div>
</div>