Browse Source

refactor actions as array

activeAddress
Ivan Socolsky 10 years ago
parent
commit
619db55e6c
  1. 23
      lib/model/txproposal.js
  2. 2
      lib/storage.js
  3. 23
      test/integration/server.js
  4. 2
      test/txproposal.js

23
lib/model/txproposal.js

@ -30,7 +30,7 @@ TxProposal.create = function(opts) {
x.requiredSignatures = opts.requiredSignatures;
x.requiredRejections = opts.requiredRejections;
x.status = 'pending';
x.actions = {};
x.actions = [];
return x;
};
@ -53,9 +53,8 @@ TxProposal.fromObj = function(obj) {
x.status = obj.status;
x.txid = obj.txid;
x.inputPaths = obj.inputPaths;
x.actions = obj.actions;
_.each(x.actions, function(action, copayerId) {
x.actions[copayerId] = TxProposalAction.fromObj(action);
x.actions = _.map(obj.actions, function(action) {
return TxProposalAction.fromObj(action);
});
return x;
@ -74,8 +73,8 @@ TxProposal.prototype._updateStatus = function() {
TxProposal.prototype._getCurrentSignatures = function() {
var acceptedActions = _.filter(this.actions, function(x) {
return x && x.type == 'accept';
var acceptedActions = _.filter(this.actions, {
type: 'accept'
});
return _.map(acceptedActions, function(x) {
@ -125,7 +124,7 @@ TxProposal.prototype.getRawTx = function() {
* @return {String[]} copayerIds that performed actions in this proposal (accept / reject)
*/
TxProposal.prototype.getActors = function() {
return _.keys(this.actions);
return _.pluck(this.actions, 'copayerId');
};
@ -136,7 +135,9 @@ TxProposal.prototype.getActors = function() {
* @return {Object} type / createdOn
*/
TxProposal.prototype.getActionBy = function(copayerId) {
return this.actions[copayerId];
return _.find(this.actions, {
copayerId: copayerId
});
};
TxProposal.prototype.addAction = function(copayerId, type, comment, signatures, xpub) {
@ -147,7 +148,7 @@ TxProposal.prototype.addAction = function(copayerId, type, comment, signatures,
xpub: xpub,
comment: comment,
});
this.actions[copayerId] = action;
this.actions.push(action);
this._updateStatus();
};
@ -206,12 +207,12 @@ TxProposal.prototype.isPending = function() {
};
TxProposal.prototype.isAccepted = function() {
var votes = _.countBy(_.values(this.actions), 'type');
var votes = _.countBy(this.actions, 'type');
return votes['accept'] >= this.requiredSignatures;
};
TxProposal.prototype.isRejected = function() {
var votes = _.countBy(_.values(this.actions), 'type');
var votes = _.countBy(this.actions, 'type');
return votes['reject'] >= this.requiredRejections;
};

2
lib/storage.js

@ -121,7 +121,7 @@ Storage.prototype._completeTxData = function(walletId, txs, cb) {
if (err) return cb(err);
_.each(txList, function(tx) {
tx.creatorName = wallet.getCopayer(tx.creatorId).name;
_.each(_.values(tx.actions), function(action) {
_.each(tx.actions, function(action) {
action.copayerName = wallet.getCopayer(action.copayerId).name;
});
});

23
test/integration/server.js

@ -1126,10 +1126,9 @@ describe('Copay server', function() {
server.getTx({
id: txp.id
}, function(err, txp) {
var actions = _.values(txp.actions);
actions.length.should.equal(1);
actions[0].copayerId.should.equal(wallet.copayers[0].id);
actions[0].copayerName.should.equal(wallet.copayers[0].name);
txp.actions.length.should.equal(1);
txp.actions[0].copayerId.should.equal(wallet.copayers[0].id);
txp.actions[0].copayerName.should.equal(wallet.copayers[0].name);
done();
});
});
@ -1221,7 +1220,7 @@ describe('Copay server', function() {
should.not.exist(err);
txps.length.should.equal(1);
var txp = txps[0];
_.keys(txp.actions).should.be.empty;
txp.actions.should.be.empty;
next(null, txp);
});
},
@ -1243,8 +1242,8 @@ describe('Copay server', function() {
txp.isPending().should.be.true;
txp.isRejected().should.be.false;
txp.isAccepted().should.be.false;
_.keys(txp.actions).length.should.equal(1);
var action = txp.actions[wallet.copayers[0].id];
txp.actions.length.should.equal(1);
var action = txp.getActionBy(wallet.copayers[0].id);
action.type.should.equal('accept');
next(null, txp);
});
@ -1279,7 +1278,7 @@ describe('Copay server', function() {
txp.isAccepted().should.be.true;
txp.isBroadcasted().should.be.true;
txp.txid.should.equal('999');
_.keys(txp.actions).length.should.equal(2);
txp.actions.length.should.equal(2);
done();
});
},
@ -1304,7 +1303,7 @@ describe('Copay server', function() {
should.not.exist(err);
txps.length.should.equal(1);
var txp = txps[0];
_.keys(txp.actions).should.be.empty;
txp.actions.should.be.empty;
next();
});
},
@ -1325,8 +1324,8 @@ describe('Copay server', function() {
txp.isPending().should.be.true;
txp.isRejected().should.be.false;
txp.isAccepted().should.be.false;
_.keys(txp.actions).length.should.equal(1);
var action = txp.actions[wallet.copayers[0].id];
txp.actions.length.should.equal(1);
var action = txp.getActionBy(wallet.copayers[0].id);
action.type.should.equal('reject');
action.comment.should.equal('just because');
next();
@ -1359,7 +1358,7 @@ describe('Copay server', function() {
txp.isPending().should.be.false;
txp.isRejected().should.be.true;
txp.isAccepted().should.be.false;
_.keys(txp.actions).length.should.equal(2);
txp.actions.length.should.equal(2);
done();
});
},

2
test/txproposal.js

@ -37,7 +37,7 @@ describe('TXProposal', function() {
});
});
describe.skip('#getRawTx', function() {
describe('#getRawTx', function() {
it('should generate correct raw transaction for signed 2-2', function() {
var txp = TXP.fromObj(aTXP());
txp.sign('1', theSignatures, theXPub);

Loading…
Cancel
Save