@ocap/resolver 1.19.5 → 1.19.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.js CHANGED
@@ -1286,10 +1286,11 @@ module.exports = class OCAPResolver {
1286
1286
  * @param {string} param.dataKey Key to access data in response
1287
1287
  * @returns {Promise<Array>} Array of fetched items
1288
1288
  */
1289
- listChunks(fn, { total, concurrency = 3, chunkSize = 2000, pageSize = 100, timeKey, dataKey }) {
1289
+ listChunks(fn, { total, concurrency = 3, chunkSize = 2100, pageSize = 100, timeKey, dataKey }) {
1290
1290
  let done = false;
1291
1291
  let time;
1292
1292
  const totalPage = Math.ceil(total / pageSize);
1293
+ let curPage = 0;
1293
1294
 
1294
1295
  const next = async () => {
1295
1296
  if (done) {
@@ -1299,7 +1300,7 @@ module.exports = class OCAPResolver {
1299
1300
  let results = [];
1300
1301
 
1301
1302
  // next pages
1302
- for (let curPage = 0; curPage < totalPage; curPage += concurrency) {
1303
+ for (; curPage < totalPage; curPage += concurrency) {
1303
1304
  const batchResults = await Promise.all(
1304
1305
  new Array(Math.min(concurrency, totalPage - curPage)).fill(true).map(async (_, i) => {
1305
1306
  const { [dataKey]: list } = await fn({
@@ -1315,7 +1316,7 @@ module.exports = class OCAPResolver {
1315
1316
  // finish
1316
1317
  if (!flatResults.length) {
1317
1318
  done = true;
1318
- return [];
1319
+ return results;
1319
1320
  }
1320
1321
 
1321
1322
  results = results.concat(flatResults);
@@ -1337,7 +1338,7 @@ module.exports = class OCAPResolver {
1337
1338
  };
1338
1339
  }
1339
1340
 
1340
- async listTransactionsChunks(args = {}, { chunkSize = 2000, pageSize = 100 } = {}) {
1341
+ async listTransactionsChunks(args = {}, { chunkSize = 2100, pageSize = 100 } = {}) {
1341
1342
  return this.listChunks(
1342
1343
  ({ size, time, cursor }) =>
1343
1344
  this.listTransactions({
@@ -1360,7 +1361,7 @@ module.exports = class OCAPResolver {
1360
1361
  );
1361
1362
  }
1362
1363
 
1363
- async listStakeChunks(args = {}, { chunkSize = 2000, pageSize = 100 } = {}) {
1364
+ async listStakeChunks(args = {}, { chunkSize = 2100, pageSize = 100 } = {}) {
1364
1365
  return this.listChunks(
1365
1366
  ({ size, time, cursor }) =>
1366
1367
  this.listStakes({
@@ -16,6 +16,7 @@ class TokenDistributionManager {
16
16
  constructor(resolver) {
17
17
  this.resolver = resolver;
18
18
  this.indexdb = resolver.indexdb;
19
+ this.isProcessing = false;
19
20
  }
20
21
 
21
22
  formatDistribution(distribution) {
@@ -69,6 +70,11 @@ class TokenDistributionManager {
69
70
  async updateByToken(tokenAddress, force) {
70
71
  const { logger, config } = this.resolver;
71
72
 
73
+ if (this.isProcessing) {
74
+ logger?.logger('Token distribution is already in progress', { tokenAddress, force });
75
+ return;
76
+ }
77
+
72
78
  const distribution = force
73
79
  ? this.formatDistribution({ tokenAddress })
74
80
  : this.formatDistribution((await this.getDistribution(tokenAddress)) || { tokenAddress });
@@ -78,44 +84,52 @@ class TokenDistributionManager {
78
84
  distribution: createIndexedTokenDistribution(distribution),
79
85
  });
80
86
 
81
- if (force && isDefaultToken) {
82
- this.handleModerator(distribution);
83
- }
87
+ this.isProcessing = true;
84
88
 
85
- const { next } = await this.resolver.listTransactionsChunks({
86
- timeFilter: { startDateTime: distribution.txTime },
87
- // Default token is used for gas payment and may appear in any transaction, so we cannot filter by tokenFilter
88
- tokenFilter: isDefaultToken ? {} : { tokenFilter: { tokens: [tokenAddress] } },
89
- });
90
- let nextData = await next();
89
+ try {
90
+ if (force && isDefaultToken) {
91
+ this.handleModerator(distribution);
92
+ }
91
93
 
92
- // Process transactions in chunks and update indexdb
93
- while (nextData.length) {
94
- logger?.info('Updating token distribution in chunks', {
95
- chunkSize: nextData.length,
96
- startTime: nextData[0].time,
97
- startHash: nextData[0].hash,
98
- endTime: nextData[nextData.length - 1].time,
94
+ const { next } = await this.resolver.listTransactionsChunks({
95
+ timeFilter: { startDateTime: distribution.txTime },
96
+ // Default token is used for gas payment and may appear in any transaction, so we cannot filter by tokenFilter
97
+ tokenFilter: isDefaultToken ? {} : { tokenFilter: { tokens: [tokenAddress] } },
99
98
  });
99
+ let nextData = await next();
100
+
101
+ // Process transactions in chunks and update indexdb
102
+ while (nextData.length) {
103
+ logger?.info('Updating token distribution in chunks', {
104
+ chunkSize: nextData.length,
105
+ startTime: nextData[0].time,
106
+ startHash: nextData[0].hash,
107
+ endTime: nextData[nextData.length - 1].time,
108
+ });
109
+
110
+ const handlePromises = nextData.map((tx) => this.handleTx(tx, distribution));
111
+ await Promise.all(handlePromises);
112
+
113
+ // update indexdb
114
+ await this.saveDistribution(distribution, false);
115
+ nextData = await next();
116
+ }
100
117
 
101
- const handlePromises = nextData.map((tx) => this.handleTx(tx, distribution));
102
- await Promise.all(handlePromises);
103
-
104
- // update indexdb
118
+ // We cannot distinguish between revokedStake and stake from tx receipts here,
119
+ // so we need to read all stake transactions and recalculate token distribution based on their revokeTokens and tokens
120
+ await this.splitStake(distribution);
105
121
  await this.saveDistribution(distribution, false);
106
- nextData = await next();
107
- }
108
-
109
- // We cannot distinguish between revokedStake and stake from tx receipts here,
110
- // so we need to read all stake transactions and recalculate token distribution based on their revokeTokens and tokens
111
- await this.splitStake(distribution, force);
112
- await this.saveDistribution(distribution, false);
113
-
114
- const result = createIndexedTokenDistribution(distribution);
115
122
 
116
- logger.info(`Token distribution update completed (${tokenAddress})`, { distribution: result });
123
+ const result = createIndexedTokenDistribution(distribution);
117
124
 
118
- return result;
125
+ logger.info(`Token distribution update completed (${tokenAddress})`, { distribution: result });
126
+ return result;
127
+ } catch (e) {
128
+ logger?.error('Token distribution update failed', { error: e });
129
+ return null;
130
+ } finally {
131
+ this.isProcessing = false;
132
+ }
119
133
  }
120
134
 
121
135
  /**
@@ -175,6 +189,13 @@ class TokenDistributionManager {
175
189
  * @returns {Promise<Object>} The updated token distributions
176
190
  */
177
191
  async updateByTx(tx, context) {
192
+ const { logger } = this.resolver;
193
+
194
+ if (this.isProcessing) {
195
+ logger?.logger('Token distribution is already in progress', { tx });
196
+ return;
197
+ }
198
+
178
199
  const formattedTx = await this.resolver.formatTx(tx);
179
200
  const tokens = formattedTx.tokenSymbols.map(({ address }) => address);
180
201
 
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.19.5",
6
+ "version": "1.19.7",
7
7
  "description": "GraphQL resolver built upon ocap statedb and GQL layer",
8
8
  "main": "lib/index.js",
9
9
  "files": [
@@ -22,18 +22,18 @@
22
22
  "jest": "^29.7.0"
23
23
  },
24
24
  "dependencies": {
25
- "@arcblock/did": "1.19.5",
26
- "@arcblock/did-util": "1.19.5",
27
- "@arcblock/validator": "1.19.5",
28
- "@ocap/config": "1.19.5",
29
- "@ocap/indexdb": "1.19.5",
30
- "@ocap/mcrypto": "1.19.5",
31
- "@ocap/message": "1.19.5",
32
- "@ocap/state": "1.19.5",
33
- "@ocap/tx-protocols": "1.19.5",
34
- "@ocap/util": "1.19.5",
25
+ "@arcblock/did": "1.19.7",
26
+ "@arcblock/did-util": "1.19.7",
27
+ "@arcblock/validator": "1.19.7",
28
+ "@ocap/config": "1.19.7",
29
+ "@ocap/indexdb": "1.19.7",
30
+ "@ocap/mcrypto": "1.19.7",
31
+ "@ocap/message": "1.19.7",
32
+ "@ocap/state": "1.19.7",
33
+ "@ocap/tx-protocols": "1.19.7",
34
+ "@ocap/util": "1.19.7",
35
35
  "debug": "^4.3.6",
36
36
  "lodash": "^4.17.21"
37
37
  },
38
- "gitHead": "9f93acb129f6fee316149f3921a98b88dcccfac3"
38
+ "gitHead": "b3a8e11bd01dd1dc6e2d222bc7dd2476f278a9f7"
39
39
  }