/
meta.js
263 lines (217 loc) · 5.82 KB
/
meta.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
'use strict';
const extend = require('extend');
const utils = require('./utils');
const UInt160 = require('./uint160').UInt160;
const Amount = require('./amount').Amount;
const ACCOUNT_ZERO = require('./constants').ACCOUNT_ZERO;
const {isValidAddress} = require('ripple-address-codec');
/**
* Meta data processing facility
*
* @constructor
* @param {Object} transaction metadata
*/
function Meta(data) {
this.nodes = [ ];
if (typeof data !== 'object') {
throw new TypeError('Missing metadata');
}
if (!Array.isArray(data.AffectedNodes)) {
throw new TypeError('Metadata missing AffectedNodes');
}
data.AffectedNodes.forEach(this.addNode, this);
}
Meta.NODE_TYPES = [
'CreatedNode',
'ModifiedNode',
'DeletedNode'
];
Meta.AMOUNT_FIELDS_AFFECTING_ISSUER = [
'LowLimit',
'HighLimit',
'TakerPays',
'TakerGets'
];
Meta.ACCOUNT_FIELDS = [
'Account',
'Owner',
'Destination',
'Issuer',
'Target'
];
/**
* @param {Object} node
* @api private
*/
Meta.prototype.getNodeType = function(node) {
let result = null;
for (let i = 0; i < Meta.NODE_TYPES.length; i++) {
const type = Meta.NODE_TYPES[i];
if (node.hasOwnProperty(type)) {
result = type;
break;
}
}
return result;
};
/**
* @param {String} field
* @api private
*/
Meta.prototype.isAccountField = function(field) {
return Meta.ACCOUNT_FIELDS.indexOf(field) !== -1;
};
/**
* Add node to metadata
*
* @param {Object} node
* @api private
*/
Meta.prototype.addNode = function(node) {
this._affectedAccounts = undefined;
this._affectedBooks = undefined;
const result = { };
result.nodeType = this.getNodeType(node);
if (result.nodeType) {
const _node = node[result.nodeType];
result.diffType = result.nodeType;
result.entryType = _node.LedgerEntryType;
result.ledgerIndex = _node.LedgerIndex;
result.fields = extend({ }, _node.PreviousFields,
_node.NewFields, _node.FinalFields);
result.fieldsPrev = _node.PreviousFields || { };
result.fieldsNew = _node.NewFields || { };
result.fieldsFinal = _node.FinalFields || { };
// getAffectedBooks will set this
// result.bookKey = undefined;
this.nodes.push(result);
}
};
/**
* Get affected nodes array
*
* @param {Object} filter options
* @return {Array} nodes
*/
Meta.prototype.getNodes = function(options) {
if (typeof options === 'object') {
return this.nodes.filter(function(node) {
if (options.nodeType && options.nodeType !== node.nodeType) {
return false;
}
if (options.entryType && options.entryType !== node.entryType) {
return false;
}
if (options.bookKey && options.bookKey !== node.bookKey) {
return false;
}
return true;
});
}
return this.nodes;
};
Meta.prototype.getAffectedAccounts = function() {
if (this._affectedAccounts) {
return this._affectedAccounts;
}
const accounts = [ ];
// This code should match the behavior of the C++ method:
// TransactionMetaSet::getAffectedAccounts
for (let i = 0; i < this.nodes.length; i++) {
const node = this.nodes[i];
const fields = (node.nodeType === 'CreatedNode')
? node.fieldsNew
: node.fieldsFinal;
for (const fieldName in fields) {
const field = fields[fieldName];
if (this.isAccountField(fieldName) && UInt160.is_valid(field)) {
accounts.push(field);
} else if (
Meta.AMOUNT_FIELDS_AFFECTING_ISSUER.indexOf(fieldName) !== -1) {
const amount = Amount.from_json(field);
const issuer = amount.issuer();
if (isValidAddress(issuer) && issuer !== ACCOUNT_ZERO) {
accounts.push(issuer);
}
}
}
}
this._affectedAccounts = utils.arrayUnique(accounts);
return this._affectedAccounts;
};
Meta.prototype.getAffectedBooks = function() {
if (this._affectedBooks) {
return this._affectedBooks;
}
const books = [ ];
for (let i = 0; i < this.nodes.length; i++) {
const node = this.nodes[i];
if (node.entryType !== 'Offer') {
continue;
}
const gets = Amount.from_json(node.fields.TakerGets);
const pays = Amount.from_json(node.fields.TakerPays);
let getsKey = gets.currency().to_json();
let paysKey = pays.currency().to_json();
if (getsKey !== 'XRP') {
getsKey += '/' + gets.issuer();
}
if (paysKey !== 'XRP') {
paysKey += '/' + pays.issuer();
}
const key = getsKey + ':' + paysKey;
// Hell of a lot of work, so we are going to cache this. We can use this
// later to good effect in OrderBook.notify to make sure we only process
// pertinent offers.
node.bookKey = key;
books.push(key);
}
this._affectedBooks = utils.arrayUnique(books);
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;