broadcast txproposal 1 by 1

This commit is contained in:
Matias Alejo Garcia 2014-05-15 17:43:41 -03:00
commit 5154dc7085
3 changed files with 53 additions and 34 deletions

View file

@ -72,10 +72,17 @@ TxProposals.fromObj = function(o) {
return ret; return ret;
}; };
TxProposals.prototype.getNtxids = function() {
return Object.keys(this.txps);
};
TxProposals.prototype.toObj = function() { TxProposals.prototype.toObj = function(onlyThisNtxid) {
var ret = []; var ret = [];
for(var id in this.txps){ for(var id in this.txps){
if (onlyThisNtxid && id != onlyThisNtxid)
continue;
var t = this.txps[id]; var t = this.txps[id];
if (!t.sent) if (!t.sent)
ret.push(t.toObj()); ret.push(t.toObj());
@ -87,7 +94,6 @@ TxProposals.prototype.toObj = function() {
}; };
}; };
TxProposals.prototype._startMerge = function(myTxps, theirTxps) { TxProposals.prototype._startMerge = function(myTxps, theirTxps) {
var fromUs=0, fromTheirs=0, merged =0; var fromUs=0, fromTheirs=0, merged =0;
var toMerge = {}, ready={}; var toMerge = {}, ready={};

View file

@ -89,16 +89,26 @@ Wallet.prototype._handleTxProposals = function(senderId, data, isInbound) {
var recipients; var recipients;
var inTxp = copay.TxProposals.fromObj(data.txProposals); var inTxp = copay.TxProposals.fromObj(data.txProposals);
var ids = inTxp.getNtxids();
if (ids.lenght>1) {
this.emit('badMessage', senderId);
this.log('Received BAD TxProposal messsage FROM:', senderId); //TODO
return;
}
var newId = ids[0];
var mergeInfo = this.txProposals.merge(inTxp, true); var mergeInfo = this.txProposals.merge(inTxp, true);
var addSeen = this.addSeenToTxProposals(); var addSeen = this.addSeenToTxProposals();
if (mergeInfo.hasChanged || addSeen) { if (mergeInfo.hasChanged || addSeen) {
this.log('### BROADCASTING txProposals. '); this.log('### BROADCASTING txProposals. ');
recipients = null; recipients = null;
TODO: only diff this.sendTxProposals(recipients, newId);
this.sendTxProposals(recipients); }
if (data.lastInBatch) {
this.emit('txProposalsUpdated', this.txProposals);
this.store();
} }
this.emit('txProposalsUpdated', this.txProposals);
this.store();
}; };
Wallet.prototype._handleData = function(senderId, data, isInbound) { Wallet.prototype._handleData = function(senderId, data, isInbound) {
@ -290,13 +300,18 @@ Wallet.prototype.toEncryptedObj = function() {
Wallet.prototype.sendTxProposals = function(recipients, ntxid) { Wallet.prototype.sendTxProposals = function(recipients, ntxid) {
this.log('### SENDING txProposals TO:', recipients || 'All', this.txProposals); this.log('### SENDING txProposals TO:', recipients || 'All', this.txProposals);
var toSend = ntxid ? [ntxid] : this.getTxProposals.getNtxids(); var toSend = ntxid ? [ntxid] : this.txProposals.getNtxids();
var last = toSend[toSend];
for(var i in toSend) { for(var i in toSend) {
var id = toSend[i]; var id = toSend[i];
var lastInBatch = (i == toSend.length - 1);
this.network.send(recipients, { this.network.send(recipients, {
type: 'txProposals', type: 'txProposals',
txProposals: this.txProposals.toObj(id), txProposals: this.txProposals.toObj(id),
walletId: this.id, walletId: this.id,
lastInBatch: lastInBatch,
}); });
} }
}; };
@ -459,15 +474,15 @@ Wallet.prototype.addressIsOwn = function(addrStr, opts) {
return ret; return ret;
}; };
Wallet.prototype.getBalance = function(safe, cb) { Wallet.prototype.getBalance = function(cb) {
var balance = 0; var balance = 0;
var safeBalance = 0;
var balanceByAddr = {}; var balanceByAddr = {};
var isMain = {};
var COIN = bitcore.util.COIN; var COIN = bitcore.util.COIN;
var f = safe ? this.getSafeUnspent.bind(this) : this.getUnspent.bind(this);
f(function(utxos) { this.getUnspent(function(safeUnspent, unspent) {
for (var i = 0; i < utxos.length; i++) { for (var i = 0; i < unspent.length; i++) {
var u = utxos[i]; var u = unspent[i];
var amt = u.amount * COIN; var amt = u.amount * COIN;
balance += amt; balance += amt;
balanceByAddr[u.address] = (balanceByAddr[u.address] || 0) + amt; balanceByAddr[u.address] = (balanceByAddr[u.address] || 0) + amt;
@ -478,30 +493,30 @@ Wallet.prototype.getBalance = function(safe, cb) {
balanceByAddr[a] = balanceByAddr[a] / COIN; balanceByAddr[a] = balanceByAddr[a] / COIN;
} }
balance = balance / COIN; balance = balance / COIN;
return cb(balance, balanceByAddr, isMain);
for (var i = 0; i < safeUnspent.length; i++) {
var u = safeUnspent[i];
var amt = u.amount * COIN;
safeBalance += amt;
}
safeBalance = safeBalance / COIN;
return cb(balance, balanceByAddr, safeBalance);
}); });
}; };
Wallet.prototype.getUnspent = function(cb) { Wallet.prototype.getUnspent = function(cb) {
this.blockchain.getUnspent(this.getAddressesStr(), function(unspentList) {
return cb(unspentList);
});
};
Wallet.prototype.getSafeUnspent = function(cb) {
var self = this; var self = this;
this.blockchain.getUnspent(this.getAddressesStr(), function(unspentList) { this.blockchain.getUnspent(this.getAddressesStr(), function(unspentList) {
var ret = []; var safeUnspendList = [];
var maxRejectCount = self.totalCopayers - self.requiredCopayers; var maxRejectCount = self.totalCopayers - self.requiredCopayers;
var uu = self.txProposals.getUsedUnspent(maxRejectCount); var uu = self.txProposals.getUsedUnspent(maxRejectCount);
for (var i in unspentList) { for (var i in unspentList) {
if (uu.indexOf(unspentList[i].txid) === -1) if (uu.indexOf(unspentList[i].txid) === -1)
ret.push(unspentList[i]); safeUnspendList.push(unspentList[i]);
} }
return cb(safeUnspendList, unspentList);
return cb(ret);
}); });
}; };
@ -518,8 +533,8 @@ Wallet.prototype.createTx = function(toAddress, amountSatStr, opts, cb) {
opts.spendUnconfirmed = this.spendUnconfirmed; opts.spendUnconfirmed = this.spendUnconfirmed;
} }
self.getSafeUnspent(function(unspentList) { self.getUnspent(function(safeUnspent) {
var ntxid = self.createTxSync(toAddress, amountSatStr, unspentList, opts); var ntxid = self.createTxSync(toAddress, amountSatStr, safeUnspent, opts);
if (ntxid) { if (ntxid) {
self.sendPublicKeyRing(); // For the new change Address self.sendPublicKeyRing(); // For the new change Address
self.sendTxProposals(null, ntxid); self.sendTxProposals(null, ntxid);

View file

@ -101,23 +101,21 @@ angular.module('copay.controllerUtils')
}; };
root.updateBalance = function(cb) { root.updateBalance = function(cb) {
console.log('Updating balance...');
$rootScope.balanceByAddr = {}; $rootScope.balanceByAddr = {};
var w = $rootScope.wallet; var w = $rootScope.wallet;
$rootScope.addrInfos = w.getAddressesInfo(); $rootScope.addrInfos = w.getAddressesInfo();
if ($rootScope.addrInfos.length === 0) return; if ($rootScope.addrInfos.length === 0) return;
$rootScope.loading = true; $rootScope.loading = true;
w.getBalance(false, function(balance, balanceByAddr) { w.getBalance(function(balance, balanceByAddr, safeBalance) {
$rootScope.loading = false;
$rootScope.totalBalance = balance; $rootScope.totalBalance = balance;
$rootScope.balanceByAddr = balanceByAddr; $rootScope.balanceByAddr = balanceByAddr;
$rootScope.selectedAddr = $rootScope.addrInfos[0].address.toString(); $rootScope.selectedAddr = $rootScope.addrInfos[0].address.toString();
$rootScope.loading = false; $rootScope.availableBalance = safeBalance;
$rootScope.$digest();
if (cb) cb();
});
w.getBalance(true, function(balance) {
$rootScope.availableBalance = balance;
$rootScope.loading = false;
$rootScope.$digest(); $rootScope.$digest();
console.log('Done updating balance.'); //TODO
if (cb) cb(); if (cb) cb();
}); });
root.setSocketHandlers(); root.setSocketHandlers();