mirror of https://github.com/BTCPrivate/copay.git
following the single TxProposal changes
This commit is contained in:
parent
27de2896d8
commit
03b4d16473
|
@ -51,27 +51,38 @@ TxProposal.getSentTs = function() {
|
||||||
return this.sentTs;
|
return this.sentTs;
|
||||||
};
|
};
|
||||||
|
|
||||||
TxProposal.prototype.mergeBuilder = function(other) {
|
TxProposal.prototype.merge = function(other) {
|
||||||
var v0 = this.builder;
|
var ret = {};
|
||||||
var v1 = other.builder;
|
ret.events = this.mergeMetadata(other);
|
||||||
|
ret.hasChanged = this.mergeBuilder(other); // TODO: use this?
|
||||||
|
|
||||||
// TODO: enhance this
|
return ret;
|
||||||
var before = JSON.stringify(v0.toObj());
|
};
|
||||||
v0.merge(v1);
|
|
||||||
var after = JSON.stringify(v0.toObj());
|
TxProposal.prototype.mergeBuilder = function(other) {
|
||||||
if (after !== before) hasChanged++;
|
var b0 = this.builder;
|
||||||
|
var b1 = other.builder;
|
||||||
|
|
||||||
|
// TODO: improve this comparison
|
||||||
|
var before = JSON.stringify(b0.toObj());
|
||||||
|
b0.merge(b1);
|
||||||
|
var after = JSON.stringify(b0.toObj());
|
||||||
|
return after !== before;
|
||||||
};
|
};
|
||||||
|
|
||||||
TxProposal.prototype.mergeMetadata = function(v1) {
|
TxProposal.prototype.mergeMetadata = function(v1) {
|
||||||
var events = [];
|
var events = [];
|
||||||
var v0 = this;
|
var v0 = this;
|
||||||
|
|
||||||
|
var ntxid = this.getID();
|
||||||
|
|
||||||
Object.keys(v1.seenBy).forEach(function(k) {
|
Object.keys(v1.seenBy).forEach(function(k) {
|
||||||
if (!v0.seenBy[k]) {
|
if (!v0.seenBy[k]) {
|
||||||
v0.seenBy[k] = v1.seenBy[k];
|
v0.seenBy[k] = v1.seenBy[k];
|
||||||
events.push({
|
events.push({
|
||||||
type: 'seen',
|
type: 'seen',
|
||||||
cId: k,
|
cId: k,
|
||||||
txId: hash
|
txId: ntxid
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -82,7 +93,7 @@ TxProposal.prototype.mergeMetadata = function(v1) {
|
||||||
events.push({
|
events.push({
|
||||||
type: 'signed',
|
type: 'signed',
|
||||||
cId: k,
|
cId: k,
|
||||||
txId: hash
|
txId: ntxid
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -93,7 +104,7 @@ TxProposal.prototype.mergeMetadata = function(v1) {
|
||||||
events.push({
|
events.push({
|
||||||
type: 'rejected',
|
type: 'rejected',
|
||||||
cId: k,
|
cId: k,
|
||||||
txId: hash
|
txId: ntxid
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -103,7 +114,7 @@ TxProposal.prototype.mergeMetadata = function(v1) {
|
||||||
v0.sentTxid = v1.sentTxid;
|
v0.sentTxid = v1.sentTxid;
|
||||||
events.push({
|
events.push({
|
||||||
type: 'broadcast',
|
type: 'broadcast',
|
||||||
txId: hash
|
txId: ntxid
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -157,99 +168,39 @@ TxProposals.prototype.toObj = function(onlyThisNtxid) {
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
TxProposals.prototype._startMerge = function(myTxps, theirTxps) {
|
TxProposals.prototype._startMerge = function(inTxp) {
|
||||||
var fromUs = 0,
|
|
||||||
fromTheirs = 0,
|
|
||||||
merged = 0;
|
|
||||||
var toMerge = {},
|
|
||||||
ready = {},
|
|
||||||
events = [];
|
|
||||||
|
|
||||||
for (var hash in theirTxps) {
|
|
||||||
if (!myTxps[hash]) {
|
|
||||||
ready[hash] = theirTxps[hash]; // only in theirs;
|
|
||||||
events.push({
|
|
||||||
type: 'new',
|
|
||||||
cid: theirTxps[hash].creator,
|
|
||||||
tx: hash
|
|
||||||
});
|
|
||||||
fromTheirs++;
|
|
||||||
} else {
|
|
||||||
toMerge[hash] = theirTxps[hash]; // need Merging
|
|
||||||
merged++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (var hash in myTxps) {
|
|
||||||
if (!toMerge[hash]) {
|
|
||||||
ready[hash] = myTxps[hash]; // only in myTxps;
|
|
||||||
fromUs++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
stats: {
|
|
||||||
fromUs: fromUs,
|
|
||||||
fromTheirs: fromTheirs,
|
|
||||||
merged: merged,
|
|
||||||
},
|
|
||||||
ready: ready,
|
|
||||||
toMerge: toMerge,
|
|
||||||
events: events
|
events: events
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
// TODO add signatures.
|
|
||||||
TxProposals.prototype._mergeMetadata = function(myTxps, theirTxps, mergeInfo) {
|
|
||||||
var toMerge = mergeInfo.toMerge;
|
|
||||||
var events = [];
|
|
||||||
|
|
||||||
Object.keys(toMerge).forEach(function(hash) {
|
|
||||||
var v0 = myTxps[hash];
|
|
||||||
var v1 = toMerge[hash];
|
|
||||||
var newEvents = v0.mergeMetadata(v1);
|
|
||||||
events.concat(newEvents);
|
|
||||||
});
|
|
||||||
|
|
||||||
return {
|
|
||||||
events: events.concat(mergeInfo.events),
|
|
||||||
hasChanged: events.length
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
TxProposals.prototype._mergeBuilder = function(myTxps, theirTxps, mergeInfo) {
|
TxProposals.prototype._mergeBuilder = function(myTxps, theirTxps, mergeInfo) {
|
||||||
var toMerge = mergeInfo.toMerge;
|
var toMerge = mergeInfo.toMerge;
|
||||||
|
|
||||||
for (var hash in toMerge) {
|
for (var ntxid in toMerge) {}
|
||||||
var v0 = myTxps[hash];
|
|
||||||
var v1 = myTxps[hash];
|
|
||||||
|
|
||||||
v0.mergeBuilder(v1);
|
|
||||||
}
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
TxProposals.prototype.merge = function(t) {
|
TxProposals.prototype.merge = function(inTxp) {
|
||||||
if (this.network.name !== t.network.name)
|
|
||||||
throw new Error('network mismatch in:', t);
|
|
||||||
|
|
||||||
var myTxps = this.txps;
|
var myTxps = this.txps;
|
||||||
var theirTxps = t.txps;
|
|
||||||
|
|
||||||
var mergeInfo = this._startMerge(myTxps, theirTxps);
|
var ntxid = inTxp.getID();
|
||||||
var result = this._mergeMetadata(myTxps, theirTxps, mergeInfo);
|
var ret;
|
||||||
result.hasChanged += this._mergeBuilder(myTxps, theirTxps, mergeInfo);
|
|
||||||
|
|
||||||
Object.keys(mergeInfo.toMerge).forEach(function(hash) {
|
if (myTxps[ntxid]) {
|
||||||
mergeInfo.ready[hash] = myTxps[hash];
|
var v0 = myTxps[ntxid];
|
||||||
});
|
var v1 = inTxp;
|
||||||
|
ret = v0.merge(v1);
|
||||||
mergeInfo.stats.hasChanged = result.hasChanged;
|
} else {
|
||||||
mergeInfo.stats.events = result.events;
|
ret.events = {};
|
||||||
|
ret.hasChanged = true;
|
||||||
this.txps = mergeInfo.ready;
|
ret.events.push({
|
||||||
return mergeInfo.stats;
|
type: 'new',
|
||||||
|
cid: inTxp.creator,
|
||||||
|
tx: ntxid
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
};
|
};
|
||||||
|
|
||||||
TxProposals.prototype.add = function(data) {
|
TxProposals.prototype.add = function(data) {
|
||||||
|
|
|
@ -116,24 +116,22 @@ Wallet.prototype._handlePublicKeyRing = function(senderId, data, isInbound) {
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
Wallet.prototype._handleTxProposals = function(senderId, data, isInbound) {
|
Wallet.prototype._handleTxProposal = function(senderId, data) {
|
||||||
this.log('RECV TXPROPOSAL:', data);
|
this.log('RECV TXPROPOSAL:', data);
|
||||||
|
|
||||||
var recipients;
|
var inTxp = TxProposals.TxProposal.fromObj(data.txProposal);
|
||||||
var inTxp = TxProposal.fromObj(data.txProposal);
|
|
||||||
|
|
||||||
var newId = inTxp.getID();
|
|
||||||
var mergeInfo = this.txProposals.merge(inTxp);
|
var mergeInfo = this.txProposals.merge(inTxp);
|
||||||
var addSeen = this.addSeenToTxProposals();
|
var added = this.addSeenToTxProposals();
|
||||||
if (mergeInfo.hasChanged || addSeen) {
|
|
||||||
|
if (mergeInfo.hasChanged || added) {
|
||||||
this.log('### BROADCASTING txProposals. ');
|
this.log('### BROADCASTING txProposals. ');
|
||||||
recipients = null;
|
this.sendTxProposals(null, inTxp.getID());
|
||||||
this.sendTxProposals(recipients, newId);
|
|
||||||
}
|
|
||||||
if (data.lastInBatch) {
|
|
||||||
this.emit('txProposalsUpdated');
|
|
||||||
this.store();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.emit('txProposalsUpdated');
|
||||||
|
this.store();
|
||||||
|
|
||||||
for (var i = 0; i < mergeInfo.events.length; i++) {
|
for (var i = 0; i < mergeInfo.events.length; i++) {
|
||||||
this.emit('txProposalEvent', mergeInfo.events[i]);
|
this.emit('txProposalEvent', mergeInfo.events[i]);
|
||||||
}
|
}
|
||||||
|
@ -160,8 +158,8 @@ Wallet.prototype._handleData = function(senderId, data, isInbound) {
|
||||||
case 'publicKeyRing':
|
case 'publicKeyRing':
|
||||||
this._handlePublicKeyRing(senderId, data, isInbound);
|
this._handlePublicKeyRing(senderId, data, isInbound);
|
||||||
break;
|
break;
|
||||||
case 'txProposals':
|
case 'txProposal':
|
||||||
this._handleTxProposals(senderId, data, isInbound);
|
this._handleTxProposal(senderId, data, isInbound);
|
||||||
break;
|
break;
|
||||||
case 'indexes':
|
case 'indexes':
|
||||||
this._handleIndexes(senderId, data, isInbound);
|
this._handleIndexes(senderId, data, isInbound);
|
||||||
|
@ -357,23 +355,14 @@ Wallet.prototype.toEncryptedObj = function() {
|
||||||
return this.storage.export(walletObj);
|
return this.storage.export(walletObj);
|
||||||
};
|
};
|
||||||
|
|
||||||
Wallet.prototype.sendTxProposals = function(recipients, ntxid) {
|
Wallet.prototype.sendTxProposal = function(recipients, ntxid) {
|
||||||
this.log('### SENDING txProposals TO:', recipients || 'All', this.txProposals);
|
this.log('### SENDING txProposals TO:', recipients || 'All', this.txProposals);
|
||||||
|
var id = toSend[i];
|
||||||
var toSend = ntxid ? [ntxid] : this.txProposals.getNtxids();
|
this.network.send(recipients, {
|
||||||
|
type: 'txProposal',
|
||||||
var last = toSend[toSend];
|
txProposals: this.txProposals.toObj(id),
|
||||||
|
walletId: this.id,
|
||||||
for (var i in toSend) {
|
});
|
||||||
var id = toSend[i];
|
|
||||||
var lastInBatch = (i == toSend.length - 1);
|
|
||||||
this.network.send(recipients, {
|
|
||||||
type: 'txProposals',
|
|
||||||
txProposals: this.txProposals.toObj(id),
|
|
||||||
walletId: this.id,
|
|
||||||
lastInBatch: lastInBatch,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Wallet.prototype.sendWalletReady = function(recipients) {
|
Wallet.prototype.sendWalletReady = function(recipients) {
|
||||||
|
|
Loading…
Reference in New Issue