add preconditions and tests

This commit is contained in:
Manuel Araoz 2014-06-18 10:09:40 -03:00
commit 889edf4b92
5 changed files with 87 additions and 69 deletions

View file

@ -54,8 +54,7 @@ TxProposal.getSentTs = function() {
TxProposal.prototype.merge = function(other) {
var ret = {};
ret.events = this.mergeMetadata(other);
ret.hasChanged = this.mergeBuilder(other); // TODO: use this?
ret.hasChanged = this.mergeBuilder(other);
return ret;
};
@ -151,6 +150,7 @@ TxProposals.prototype.getNtxids = function() {
};
TxProposals.prototype.toObj = function(onlyThisNtxid) {
if (onlyThisNtxid) throw new Error();
var ret = [];
for (var id in this.txps) {
@ -172,14 +172,15 @@ TxProposals.prototype.merge = function(inTxp) {
var myTxps = this.txps;
var ntxid = inTxp.getID();
var ret;
var ret = {};
ret.events = [];
ret.events.hasChanged = false;
if (myTxps[ntxid]) {
var v0 = myTxps[ntxid];
var v1 = inTxp;
ret = v0.merge(v1);
} else {
ret.events = {};
ret.hasChanged = true;
ret.events.push({
type: 'new',

View file

@ -5,6 +5,7 @@ var imports = require('soop').imports();
var http = require('http');
var EventEmitter = imports.EventEmitter || require('events').EventEmitter;
var async = require('async');
var preconditions = require('preconditions').instance();
var bitcore = require('bitcore');
var bignum = bitcore.Bignum;
@ -126,7 +127,7 @@ Wallet.prototype._handleTxProposal = function(senderId, data) {
if (mergeInfo.hasChanged || added) {
this.log('### BROADCASTING txProposals. ');
this.sendTxProposals(null, inTxp.getID());
this.sendTxProposal(inTxp.getID());
}
this.emit('txProposalsUpdated');
@ -153,7 +154,7 @@ Wallet.prototype._handleData = function(senderId, data, isInbound) {
break;
case 'walletReady':
this.sendPublicKeyRing(senderId);
this.sendTxProposals(senderId); // send old
this.sendAllTxProposals(senderId); // send old txps
break;
case 'publicKeyRing':
this._handlePublicKeyRing(senderId, data, isInbound);
@ -355,12 +356,20 @@ Wallet.prototype.toEncryptedObj = function() {
return this.storage.export(walletObj);
};
Wallet.prototype.sendTxProposal = function(recipients, ntxid) {
this.log('### SENDING txProposals TO:', recipients || 'All', this.txProposals);
var id = toSend[i];
Wallet.prototype.sendAllTxProposals = function(recipients) {
var ntxids = this.txProposals.getNtxids();
for (var i in ntxids) {
var ntxid = ntxids[i];
this.sendTxProposal(ntxid, recipients);
}
};
Wallet.prototype.sendTxProposal = function(ntxid, recipients) {
preconditions.checkArgument(ntxid);
this.log('### SENDING txProposal '+ntxid+' TO:', recipients || 'All', this.txProposals);
this.network.send(recipients, {
type: 'txProposal',
txProposals: this.txProposals.toObj(id),
txProposal: this.txProposals.txps[ntxid].toObj(),
walletId: this.id,
});
};
@ -450,7 +459,7 @@ Wallet.prototype.reject = function(ntxid) {
}
txp.rejectedBy[myId] = Date.now();
this.sendTxProposals(null, ntxid);
this.sendTxProposal(ntxid);
this.store();
this.emit('txProposalsUpdated');
};
@ -475,7 +484,7 @@ Wallet.prototype.sign = function(ntxid, cb) {
var ret = false;
if (b.signaturesAdded > before) {
txp.signedBy[myId] = Date.now();
self.sendTxProposals(null, ntxid);
self.sendTxProposal(ntxid);
self.store();
self.emit('txProposalsUpdated');
ret = true;
@ -503,7 +512,7 @@ Wallet.prototype.sendTx = function(ntxid, cb) {
self.log('BITCOIND txid:', txid);
if (txid) {
self.txProposals.setSent(ntxid, txid);
self.sendTxProposals(null, ntxid);
self.sendTxProposal(ntxid);
self.store();
}
return cb(txid);
@ -630,7 +639,7 @@ Wallet.prototype.createTx = function(toAddress, amountSatStr, comment, opts, cb)
var ntxid = self.createTxSync(toAddress, amountSatStr, comment, safeUnspent, opts);
if (ntxid) {
self.sendIndexes();
self.sendTxProposals(null, ntxid);
self.sendTxProposal(ntxid);
self.store();
self.emit('txProposalsUpdated');
}