-
Notifications
You must be signed in to change notification settings - Fork 4
/
bts-cache.js
370 lines (317 loc) · 10.3 KB
/
bts-cache.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
'use strict';
const bitsharesjs = require('bitsharesjs');
const bsws = require('bitsharesjs-ws');
const Apis = bsws.Apis;
const promiseRetry = require('promise-retry');
const debug = console.log;
const btsApiNodes = require('./apinodes');
function getApiNode() {
const maxIndex = btsApiNodes.length;
var i = Math.floor(Math.random() * maxIndex);
return btsApiNodes[i].url;
}
var conStatus = 'closed';
Apis.setRpcConnectionStatusCallback(function (status) {
conStatus = status;
if (status == 'open') {
connectChain().then(set_subscribe_callback);
}
else if (status == 'closed' || status == 'error') {
console.log('cache: reconnecting to api instance');
connectChain();
}
})
var lastPromise = null;
function connectChain() {
function _rawConnectChain() {
var connect = conStatus == 'closed' || conStatus == 'error';
if (connect) {
conStatus = 'connecting';
var nodeAddress = getApiNode();
console.log('api', nodeAddress);
lastPromise = Apis.instance(nodeAddress, true).init_promise.catch(connectChain);
}
return lastPromise;
}
return _rawConnectChain();
}
// 关注的帐号缓存
var account_cache = {
}
// 帐号相关的统计 缓存
var statistic_cache = {
}
//
var asset_cache = {
}
var all_caches = {
global_dynamic: {}
}
function isDiff2statistic(s0, s1) {
for (var key in s0) {
if (s0[key] !== s1[key]) {
return true;
}
}
return false;
}
function copyStatistic(s0, s1) {
for (var key in s1) {
s0[key] = s1[key];
}
}
function objectChange(obj) {
// console.log(obj.id);
if (statistic_cache[obj.id] && isDiff2statistic(statistic_cache[obj.id], obj)) {
console.log('cache hit', statistic_cache[obj.id], obj);
_get_account_from_chain(statistic_cache[obj.id].owner, true)
.then((accountObj) => {
copyStatistic(statistic_cache[obj.id], obj);
full_account_change_callbacks.forEach(cb => {
cb(accountObj.account.name);
})
})
.catch(console.error);
}
else if (obj.id == '2.1.0') {
all_caches.global_dynamic = obj;
}
else if (obj.id.startsWith('1.3.')) { // assets
asset_cache[obj.id] = obj;
asset_cache[obj.symbol] = obj;
}
else if (obj.id.startsWith('2.4.')) { // bitassets
asset_cache[obj.id] = obj;
var asset_id = obj.current_feed.core_exchange_rate.base.asset_id;
if (asset_cache[asset_id]) {
asset_cache[asset_id].core_exchange_rate = obj.current_feed.core_exchange_rate;
}
}
}
function _get_account_from_chain(account, cache) {
function fetch_account() {
return connectChain().then(() => {
return Apis.instance().db_api().exec('get_full_accounts', [[account], false])
}).then((accs) => {
if (accs[0] && accs[0][1]) {
return accs[0][1];
}
else {
throw new Error('no such account');
}
});
}
return promiseRetry(function (retry, number) {
console.log('attempt number', number);
return fetch_account()
.catch(function (err) {
console.error(err);
if (err.message != 'no such account') {
retry(err);
}
else {
throw err;
}
});
}, { retries: 8 }).then(function (account_object) {
if (cache) {
account_cache[account_object.account.id] = account_object;
account_cache[account_object.account.name] = account_object;
if (!statistic_cache[account_object.statistics.id]) {
statistic_cache[account_object.statistics.id] = account_object.statistics;
}
Apis.instance().db_api().exec('get_objects', [[account_object.statistics.id]]);
}
else {
setTimeout(() => {
account_fetching[account] = null;
delete account_fetching[account];
}, 10 * 1000);
}
return account_object;
});
}
function _get_objects_from_chain(obj_ids) {
function fetch_objs() {
return connectChain().then(() => {
return Apis.instance().db_api().exec('get_objects', [obj_ids])
}).then((accs) => {
return accs;
});
}
return promiseRetry(function (retry, number) {
debug('attempt number', number);
return fetch_objs()
.catch(function (err) {
debug(err);
retry(err);
});
}).then(function (objs) {
return objs;
});
}
function _get_assets_from_chain(asset_symbols) {
function fetch_asset() {
return connectChain().then(() => {
return Apis.instance().db_api().exec('lookup_asset_symbols', [asset_symbols])
}).then((accs) => {
return accs;
});
}
return promiseRetry(function (retry, number) {
return fetch_asset()
.catch(function (err) {
console.error(err);
retry(err);
});
}).then(function (assets) {
var id_array = assets.map(asset => asset.id);
assets.forEach(asset => {
if (asset.bitasset_data_id) {
id_array.push(asset.bitasset_data_id);
}
})
Apis.instance().db_api().exec('get_objects', [id_array])
.then(objects => {
objects.forEach(objectChange);
});
return assets;
});
}
function _get_account_history_from_chain(account_id) {
function get_history() {
return connectChain().then(() => {
return Apis.instance().history_api().exec('get_account_history',
[account_id, '1.11.0', 100, '1.11.9999999999999'])
}).then((transes) => {
console.log(transes);
return transes;
});
}
return promiseRetry(function (retry, number) {
return get_history()
.catch(function (err) {
console.error(err);
retry(err);
});
}).then(function (transes) {
return transes;
});
}
function chainUpdate(update_objects) {
update_objects.forEach((cs) => {
cs.forEach(objectChange);
});
}
function set_subscribe_callback() {
Apis.instance().db_api().exec("set_subscribe_callback", [chainUpdate, false])
.then(() => {
var s = new Set();
var key;
for (key in statistic_cache) {
var statistic_object = statistic_cache[key];
s.add(statistic_object.id);
}
s.add('2.1.0');
for (key in asset_cache) {
s.add(asset_cache[key].id);
}
Apis.instance().db_api().exec('get_objects', [Array.from(s)])
.then(results => {
results.forEach(objectChange);
})
})
.catch(error => {
console.error(error);
});
}
var account_fetching = {
}
function get_full_account(account, cache = false) {
// console.log(account);
if (account_cache[account]) {
return Promise.resolve(account_cache[account]);
}
if (!account_fetching[account]) {
account_fetching[account] = _get_account_from_chain(account, cache);
}
return account_fetching[account];
}
var assets_fetching = {
// [ promise, index]
}
function get_asset(asset_symbol_or_id) {
if (asset_cache[asset_symbol_or_id]) {
return Promise.resolve(asset_cache[asset_symbol_or_id]);
}
else if (!assets_fetching[asset_symbol_or_id]) {
if (asset_symbol_or_id.startsWith('1.3.')) {
assets_fetching[asset_symbol_or_id] = {};
assets_fetching[asset_symbol_or_id].promise = _get_objects_from_chain([asset_symbol_or_id]);
assets_fetching[asset_symbol_or_id].index = 0;
}
else {
assets_fetching[asset_symbol_or_id] = {};
assets_fetching[asset_symbol_or_id].promise = _get_assets_from_chain([asset_symbol_or_id]);
assets_fetching[asset_symbol_or_id].index = 0;
}
}
return assets_fetching[asset_symbol_or_id].promise.then(
s => s[assets_fetching[asset_symbol_or_id].index]
);
}
function get_assets(asset_symbol_or_id_array) {
var in_cache = asset_symbol_or_id_array.filter(a => asset_cache[a] || assets_fetching[a]);
var not_in_cache = asset_symbol_or_id_array.filter(a => in_cache.indexOf(a) < 0);
if (not_in_cache.length > 0) {
var apromise = _get_assets_from_chain(not_in_cache);
for (var i in not_in_cache) {
var a = not_in_cache[i]
assets_fetching[a] = {}
assets_fetching[a].promise = apromise;
assets_fetching[a].index = i;
}
}
return Promise.all(asset_symbol_or_id_array.map(get_asset));
}
function get_bitasset_feed(asset_symbol_or_id) {
let assetPromise = get_asset(asset_symbol_or_id);
return assetPromise.then((asset) => {
if (asset_cache[asset.bitasset_data_id]) {
return asset_cache[asset.bitasset_data_id];
}
else {
return _get_objects_from_chain([asset.bitasset_data_id])
.then(s => s[0]);
}
})
}
var full_account_change_callbacks = [];
function add_full_account_change_callback(cb) {
if (full_account_change_callbacks.indexOf(cb) < 0)
full_account_change_callbacks.push(cb);
}
function get_global_dynamic() {
function fetch_global() {
return connectChain().then(() => {
return Apis.instance().db_api().exec('get_objects', [['2.1.0']])
}).then((objs) => {
return objs[0];
});
}
if (all_caches.global_dynamic.head_block_number) {
return Promise.resolve(all_caches.global_dynamic);
}
return fetch_global();
}
module.exports = {
connectChain: connectChain,
get_full_account: get_full_account,
add_full_account_change_callback: add_full_account_change_callback,
get_global_dynamic: get_global_dynamic,
get_asset: get_asset,
get_assets: get_assets,
get_bitasset_feed: get_bitasset_feed,
/* for test */
get_account_history_from_chain: _get_account_history_from_chain,
}