mirror of https://github.com/BTCPrivate/copay.git
on reject / seen checks
This commit is contained in:
parent
32396c1ad4
commit
fb891d3f89
|
@ -197,7 +197,8 @@ Identity.prototype.storeWallet = function(wallet, cb) {
|
|||
|
||||
this.storage.setItem(key, val, function(err) {
|
||||
if (err) {
|
||||
log.debug('Wallet:' + wallet.getName() + ' couldnt be stored:', err);
|
||||
log.error('Wallet:' + wallet.getName() + ' couldnt be stored:', err);
|
||||
log.error('Wallet:' + wallet.getName() + ' Size:', JSON.stringify(wallet.sizes()));
|
||||
}
|
||||
if (cb)
|
||||
return cb(err);
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
'use strict';
|
||||
|
||||
var EventEmitter = require('events').EventEmitter;
|
||||
var _ = require('lodash');
|
||||
var preconditions = require('preconditions').singleton();
|
||||
var inherits = require('inherits');
|
||||
|
@ -358,12 +357,9 @@ Wallet.prototype._processProposalEvents = function(senderId, m) {
|
|||
type: 'signed',
|
||||
cId: m.newCopayer[0]
|
||||
};
|
||||
}
|
||||
} else {
|
||||
ev = {
|
||||
type: 'corrupt',
|
||||
cId: senderId
|
||||
};
|
||||
log.error('unknown tx proposal event:', m)
|
||||
}
|
||||
}
|
||||
if (ev)
|
||||
this.emitAndKeepAlive('txProposalEvent', ev);
|
||||
|
@ -557,17 +553,19 @@ Wallet.prototype._onTxProposal = function(senderId, data) {
|
|||
var m;
|
||||
|
||||
try {
|
||||
m = this.txProposals.merge(data.txProposal, Wallet.builderOpts);
|
||||
var keyMap = this._getKeyMap(m.txp);
|
||||
m = self.txProposals.merge(data.txProposal, Wallet.builderOpts);
|
||||
var keyMap = self._getKeyMap(m.txp);
|
||||
m.newCopayer = m.txp.setCopayers(senderId, keyMap);
|
||||
} catch (e) {
|
||||
log.error('Corrupt TX proposal received from:', senderId, e.toString());
|
||||
if (m && m.ntxid)
|
||||
this.txProposals.deleteOne(m.ntxid);
|
||||
self.txProposals.deleteOne(m.ntxid);
|
||||
m = null;
|
||||
}
|
||||
|
||||
this._processIncomingTxProposal(m, function(err) {
|
||||
if (m) {
|
||||
|
||||
self._processIncomingTxProposal(m, function(err) {
|
||||
|
||||
if (err) {
|
||||
log.error('Corrupt TX proposal received from:', senderId, err.toString());
|
||||
|
@ -581,6 +579,7 @@ Wallet.prototype._onTxProposal = function(senderId, data) {
|
|||
|
||||
self._processProposalEvents(senderId, m);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -597,11 +596,11 @@ Wallet.prototype._onReject = function(senderId, data) {
|
|||
preconditions.checkState(data.ntxid);
|
||||
log.debug('Wallet:' + this.id + ' RECV REJECT:', data);
|
||||
|
||||
try {
|
||||
var txp = this.txProposals.get(data.ntxid);
|
||||
} catch (e) {};
|
||||
|
||||
if (!txp)
|
||||
throw new Error('Received Reject for an unknown TX from:' + senderId);
|
||||
|
||||
if (txp) {
|
||||
if (txp.signedBy[senderId])
|
||||
throw new Error('Received Reject for an already signed TX from:' + senderId);
|
||||
|
||||
|
@ -611,6 +610,7 @@ Wallet.prototype._onReject = function(senderId, data) {
|
|||
cId: senderId,
|
||||
txId: data.ntxid
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -625,14 +625,17 @@ Wallet.prototype._onReject = function(senderId, data) {
|
|||
*/
|
||||
Wallet.prototype._onSeen = function(senderId, data) {
|
||||
preconditions.checkState(data.ntxid);
|
||||
try {
|
||||
var txp = this.txProposals.get(data.ntxid);
|
||||
} catch (e) {};
|
||||
if (txp) {
|
||||
txp.setSeen(senderId);
|
||||
this.emitAndKeepAlive('txProposalEvent', {
|
||||
type: 'seen',
|
||||
cId: senderId,
|
||||
txId: data.ntxid
|
||||
});
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -1083,6 +1086,23 @@ Wallet.prototype.toObj = function() {
|
|||
return walletObj;
|
||||
};
|
||||
|
||||
/**
|
||||
* @desc: returns the sizes, by component, of a wallet, in bytes.
|
||||
*
|
||||
* @return {object} sizes by component name and 'total' for the total wallet size.
|
||||
*/
|
||||
Wallet.prototype.sizes = function() {
|
||||
var obj = this.toObj();
|
||||
var sizes = {},
|
||||
total = 0;
|
||||
_.each(obj, function(val, key) {
|
||||
var s = JSON.stringify(val).length;
|
||||
sizes[key] = s;
|
||||
total += s;
|
||||
});
|
||||
sizes.total = total;
|
||||
return sizes;
|
||||
};
|
||||
|
||||
Wallet.fromUntrustedObj = function(obj, readOpts) {
|
||||
obj = _.clone(obj);
|
||||
|
@ -2573,7 +2593,7 @@ Wallet.prototype.getTransactionHistory = function(opts, cb) {
|
|||
var proposals = self.getTxProposals();
|
||||
var satToUnit = 1 / self.settings.unitToSatoshi;
|
||||
|
||||
var indexedProposals = _.indexBy(proposal,'sentTxid');
|
||||
var indexedProposals = _.indexBy(proposals, 'sentTxid');
|
||||
|
||||
|
||||
function extractInsOuts(tx) {
|
||||
|
|
|
@ -1674,15 +1674,12 @@ describe('Wallet model', function() {
|
|||
});
|
||||
|
||||
|
||||
it('should handle corrupt tx', function(done) {
|
||||
it('should handle corrupt tx', function() {
|
||||
w.txProposals.merge = sinon.stub().throws(new Error('test error'));
|
||||
|
||||
w.on('txProposalEvent', function(e) {
|
||||
e.type.should.equal('corrupt');
|
||||
w.txProposals.deleteOne.calledOnce.should.equal(false);
|
||||
done();
|
||||
});
|
||||
sinon.stub(w, 'on');
|
||||
w._onTxProposal('senderID', data);
|
||||
w.on.called.should.equal(false);
|
||||
});
|
||||
|
||||
it('should call _processIncomingTxProposal', function(done) {
|
||||
|
@ -1705,19 +1702,14 @@ describe('Wallet model', function() {
|
|||
w._onTxProposal('senderID', data);
|
||||
});
|
||||
|
||||
it('should handle corrupt tx, case2', function(done) {
|
||||
it('should handle corrupt tx, case2', function() {
|
||||
sinon.stub(w.txProposals, 'merge').returns({
|
||||
ntxid: '1'
|
||||
});
|
||||
sinon.stub(w, 'on');
|
||||
sinon.stub(w, '_getKeyMap').throws(new Error('test error'));
|
||||
|
||||
w.on('txProposalEvent', function(e) {
|
||||
e.type.should.equal('corrupt');
|
||||
w.txProposals.deleteOne.calledWith('1').should.equal(true);
|
||||
w._getKeyMap.restore();
|
||||
done();
|
||||
});
|
||||
w._onTxProposal('senderID', data);
|
||||
w.on.called.should.equal(false);
|
||||
});
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in New Issue