From efbc62b126ef7c56226948b3e142bf7d19ff193b Mon Sep 17 00:00:00 2001 From: Stefan Thomas Date: Fri, 15 Feb 2013 14:51:52 +0100 Subject: [PATCH] Git fix: Re-add meta.js --- src/js/meta.js | 108 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 src/js/meta.js diff --git a/src/js/meta.js b/src/js/meta.js new file mode 100644 index 000000000..46c0e46cb --- /dev/null +++ b/src/js/meta.js @@ -0,0 +1,108 @@ +var extend = require('extend'); +var UInt160 = require('./uint160').UInt160; +var Amount = require('./amount').Amount; + +/** + * Meta data processing facility. + */ +var Meta = function (raw_data) +{ + this.nodes = []; + + for (var i = 0, l = raw_data.AffectedNodes.length; i < l; i++) { + var an = raw_data.AffectedNodes[i], + result = {}; + + ["CreatedNode", "ModifiedNode", "DeletedNode"].forEach(function (x) { + if (an[x]) result.diffType = x; + }); + + if (!result.diffType) return null; + + an = an[result.diffType]; + + result.entryType = an.LedgerEntryType; + result.ledgerIndex = an.LedgerIndex; + + result.fields = extend({}, an.PreviousFields, an.NewFields, an.FinalFields); + result.fieldsPrev = an.PreviousFields || {}; + result.fieldsNew = an.NewFields || {}; + result.fieldsFinal = an.FinalFields || {}; + + this.nodes.push(result); + } +}; + +/** + * Execute a function on each affected node. + * + * The callback is passed two parameters. The first is a node object which looks + * like this: + * + * { + * // Type of diff, e.g. CreatedNode, ModifiedNode + * diffType: 'CreatedNode' + * + * // Type of node affected, e.g. RippleState, AccountRoot + * entryType: 'RippleState', + * + * // Index of the ledger this change occurred in + * ledgerIndex: '01AB01AB...', + * + * // Contains all fields with later versions taking precedence + * // + * // This is a shorthand for doing things like checking which account + * // this affected without having to check the diffType. + * fields: {...}, + * + * // Old fields (before the change) + * fieldsPrev: {...}, + * + * // New fields (that have been added) + * fieldsNew: {...}, + * + * // Changed fields + * fieldsFinal: {...} + * } + * + * The second parameter to the callback is the index of the node in the metadata + * (first entry is index 0). + */ +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;