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 dd7f8aa8d1
commit 792fa050ef
4 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,36 @@ 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());
}
}
}
});
return accounts;
};
exports.Meta = Meta;