diff --git a/src/js/ripple/meta.js b/src/js/ripple/meta.js index 66a81279..5ad3bd5b 100644 --- a/src/js/ripple/meta.js +++ b/src/js/ripple/meta.js @@ -209,4 +209,53 @@ Meta.prototype.getAffectedBooks = function() { return this._affectedBooks; }; + +/** + * 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 + * nodeType: '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 nodeType. + * fields: {...}, + * + * // Old fields (before the change) + * fieldsPrev: {...}, + * + * // New fields (that have been added) + * fieldsNew: {...}, + * + * // Changed fields + * fieldsFinal: {...} + * } + */ + +[ + 'forEach', + 'map', + 'filter', + 'every', + 'some', + 'reduce' +].forEach(function(fn) { + Meta.prototype[fn] = function() { + return Array.prototype[fn].apply(this.nodes, arguments); + }; +}); + +Meta.prototype.each = Meta.prototype.forEach; + exports.Meta = Meta; diff --git a/test/fixtures/payment-iou.json b/test/fixtures/payment-iou.json new file mode 100644 index 00000000..6ed1c600 --- /dev/null +++ b/test/fixtures/payment-iou.json @@ -0,0 +1,123 @@ +{ + "engine_result": "tesSUCCESS", + "engine_result_code": 0, + "engine_result_message": "The transaction was applied.", + "ledger_hash": "F3F1416BF2E813396AB01FAA38E9F1023AC4D2368D94B0D52B2BC603CEEC01C3", + "ledger_index": 10459371, + "status": "closed", + "type": "transaction", + "validated": true, + "metadata": { + "AffectedNodes": [ + { + "ModifiedNode": { + "FinalFields": { + "Balance": { + "currency": "USD", + "issuer": "rrrrrrrrrrrrrrrrrrrrBZbvji", + "value": "1.525330905250352" + }, + "Flags": 1114112, + "HighLimit": { + "currency": "USD", + "issuer": "rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q", + "value": "0" + }, + "HighNode": "00000000000001E8", + "LowLimit": { + "currency": "USD", + "issuer": "rKmBGxocj9Abgy25J51Mk1iqFzW9aVF9Tc", + "value": "1000000000" + }, + "LowNode": "0000000000000000" + }, + "LedgerEntryType": "RippleState", + "LedgerIndex": "2F323020B4288ACD4066CC64C89DAD2E4D5DFC2D44571942A51C005BF79D6E25", + "PreviousFields": { + "Balance": { + "currency": "USD", + "issuer": "rrrrrrrrrrrrrrrrrrrrBZbvji", + "value": "1.535330905250352" + } + }, + "PreviousTxnID": "DC061E6F47B1B6E9A496A31B1AF87194B4CB24B2EBF8A59F35E31E12509238BD", + "PreviousTxnLgrSeq": 10459364 + } + }, + { + "ModifiedNode": { + "FinalFields": { + "Balance": { + "currency": "USD", + "issuer": "rrrrrrrrrrrrrrrrrrrrBZbvji", + "value": "0.02" + }, + "Flags": 1114112, + "HighLimit": { + "currency": "USD", + "issuer": "rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q", + "value": "0" + }, + "HighNode": "00000000000001E8", + "LowLimit": { + "currency": "USD", + "issuer": "rLDYrujdKUfVx28T9vRDAbyJ7G2WVXKo4K", + "value": "1000000000" + }, + "LowNode": "0000000000000000" + }, + "LedgerEntryType": "RippleState", + "LedgerIndex": "AAE13AF5192EFBFD49A8EEE5869595563FEB73228C0B38FED9CC3D20EE74F399", + "PreviousFields": { + "Balance": { + "currency": "USD", + "issuer": "rrrrrrrrrrrrrrrrrrrrBZbvji", + "value": "0.01" + } + }, + "PreviousTxnID": "DC061E6F47B1B6E9A496A31B1AF87194B4CB24B2EBF8A59F35E31E12509238BD", + "PreviousTxnLgrSeq": 10459364 + } + }, + { + "ModifiedNode": { + "FinalFields": { + "Account": "rKmBGxocj9Abgy25J51Mk1iqFzW9aVF9Tc", + "Balance": "239555992", + "Flags": 0, + "OwnerCount": 1, + "Sequence": 38 + }, + "LedgerEntryType": "AccountRoot", + "LedgerIndex": "E9A39B0BA8703D5FFD05D9EAD01EE6C0E7A15CF33C2C6B7269107BD2BD535818", + "PreviousFields": { + "Balance": "239567992", + "Sequence": 37 + }, + "PreviousTxnID": "DC061E6F47B1B6E9A496A31B1AF87194B4CB24B2EBF8A59F35E31E12509238BD", + "PreviousTxnLgrSeq": 10459364 + } + } + ], + "TransactionIndex": 2, + "TransactionResult": "tesSUCCESS" + }, + "tx_json": { + "Account": "rKmBGxocj9Abgy25J51Mk1iqFzW9aVF9Tc", + "Amount": { + "currency": "USD", + "issuer": "rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q", + "value": "0.01" + }, + "Destination": "rLDYrujdKUfVx28T9vRDAbyJ7G2WVXKo4K", + "Fee": "12000", + "Flags": 2147483648, + "LastLedgerSequence": 10459379, + "Sequence": 37, + "SigningPubKey": "03F16A52EBDCA6EBF5D99828E1E6918C64D45E6F136476A8F4757512FE553D18F0", + "TransactionType": "Payment", + "TxnSignature": "3044022031D6AB55CDFD17E06DA0BAD6D6B7DC9B5CA8FFF50405F2FCD3ED8D3893B1835E02200524CC1E7D70AE3F00C9F94405C55EE179C363F534905168AE8B5BA01CF568A0", + "date": 471644150, + "hash": "34671C179737CC89E0F8BBAA83C313885ED1733488FC0F3088BAE16A3D9A5B1B" + } +} diff --git a/test/metadata-test.js b/test/metadata-test.js new file mode 100644 index 00000000..a8c3c4fa --- /dev/null +++ b/test/metadata-test.js @@ -0,0 +1,37 @@ +var assert = require('assert'); +var Meta = require('ripple-lib').Meta; + +describe('Meta', function() { + var meta = new Meta(require('./fixtures/payment-iou.json').metadata); + + function callback(el, idx, ary) { + assert.strictEqual(meta.nodes[idx],el); + } + + it('forEach', function() { + meta.forEach(callback); + }); + + it('map', function() { + meta.map(callback); + }); + + it('filter', function() { + meta.filter(callback); + }); + + it('every', function() { + meta.every(callback); + }); + + it('some', function() { + meta.some(callback); + }); + + it('reduce', function() { + meta.reduce(function(prev,curr,idx,ary) { + assert.strictEqual(meta.nodes[idx], curr); + }, []); + }); + +});