JS: Correctly calculate affected accounts when routing account events.

This commit is contained in:
Stefan Thomas
2013-02-15 14:43:24 +01:00
parent 0213ec7d84
commit 82f14395fb
3 changed files with 49 additions and 11 deletions

View File

@@ -1,4 +1,6 @@
var extend = require('extend');
var UInt160 = require('./uint160').UInt160;
var Amount = require('./amount').Amount;
/**
* Meta data processing facility.
@@ -71,6 +73,38 @@ Meta.prototype.each = function (fn)
for (var i = 0, l = this.nodes.length; i < l; i++) {
fn(this.nodes[i], i);
}
}
};
var amountFieldsAffectingIssuer = [
"LowLimit", "HighLimit", "TakerPays", "TakerGets"
];
Meta.prototype.getAffectedAccounts = function ()
{
var accounts = [];
// This code should match the behavior of the C++ method:
// TransactionMetaSet::getAffectedAccounts
this.each(function (an) {
var fields = (an.diffType === "CreatedNode") ? an.fieldsNew : an.fieldsFinal;
for (var i in fields) {
var field = fields[i];
if ("string" === typeof field && UInt160.is_valid(field)) {
accounts.push(field);
} else if (amountFieldsAffectingIssuer.indexOf(i) !== -1) {
var amount = Amount.from_json(field);
var issuer = amount.issuer();
if (issuer.is_valid() && !issuer.is_zero()) {
accounts.push(issuer.to_json());
}
}
}
});
console.log("AFFECTS", accounts);
return accounts;
};
exports.Meta = Meta;

View File

@@ -573,14 +573,15 @@ Remote.prototype._connect_message = function (ws, json) {
case 'account':
// XXX If not trusted, need proof.
if (this.trace) utils.logObject("remote: account: %s", message);
// Process metadata
message.mmeta = new Meta(message.meta);
// Pass the event on to any related Account objects
message.mmeta.each(function (an) {
if (an.entryType === 'AccountRoot') {
var account = self._accounts[an.fields.Account];
var affected = message.mmeta.getAffectedAccounts();
for (var i = 0, l = affected.length; i < l; i++) {
var account = self._accounts[affected[i]];
// Only trigger the event if the account object is actually
// subscribed - this prevents some weird phantom events from
@@ -589,7 +590,6 @@ Remote.prototype._connect_message = function (ws, json) {
account.emit('transaction', message);
}
}
});
this.emit('account', message);
break;

View File

@@ -92,6 +92,10 @@ UInt.prototype.is_valid = function () {
return this._value instanceof BigInteger;
};
UInt.prototype.is_zero = function () {
return this._value.equals(BigInteger.ZERO);
};
// value = NaN on error.
UInt.prototype.parse_generic = function (j) {
// Canonicalize and validate