@abtnode/core 1.16.1-beta-cd775909 → 1.16.2-beta-992f57fb

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.
@@ -3,11 +3,13 @@ const fs = require('fs-extra');
3
3
  const pick = require('lodash/pick');
4
4
  const pRetry = require('p-retry');
5
5
  const Client = require('@ocap/client');
6
+ const urlFriendly = require('@blocklet/meta/lib/url-friendly').default;
7
+ const { slugify } = require('transliteration');
6
8
 
7
9
  const logger = require('@abtnode/logger')('@abtnode/core:migrate-application-to-struct-v2');
8
10
 
9
11
  const { forEachBlockletSync, getSharedConfigObj, getBlockletChainInfo } = require('@blocklet/meta/lib/util');
10
- const { SLOT_FOR_IP_DNS_SITE } = require('@abtnode/constant');
12
+ const { SLOT_FOR_IP_DNS_SITE, MAIN_CHAIN_ENDPOINT } = require('@abtnode/constant');
11
13
 
12
14
  const {
13
15
  BlockletStatus,
@@ -95,65 +97,89 @@ const getChainHost = (blocklet) => {
95
97
  return getBlockletChainInfo(blocklet).host;
96
98
  };
97
99
 
100
+ const ensureAccountOnMainChain = async (blocklet, newWallet) => {
101
+ // ensure new account on main chain
102
+ const mainChainClient = new Client(MAIN_CHAIN_ENDPOINT);
103
+ const newResultOnMainChain = await mainChainClient.getAccountState({ address: newWallet.address });
104
+ if (!newResultOnMainChain.state) {
105
+ logger.info('declare account on main chain', { address: newWallet.address, did: blocklet.meta.did });
106
+ const hash = await mainChainClient.declare({
107
+ moniker: (urlFriendly(slugify(blocklet.meta.title || blocklet.meta.name)) || 'application').toLowerCase(),
108
+ wallet: newWallet,
109
+ });
110
+ logger.info('declare account on main chain done', { did: blocklet.meta.did, hash });
111
+ }
112
+ };
113
+
98
114
  const migrateAppOnChain = async (blocklet, oldSk, newSk) => {
115
+ if (process.env.NODE_ENV === 'test') {
116
+ return;
117
+ }
118
+
99
119
  logger.info('Preparing for on-chain migration', { did: blocklet.meta.did });
100
120
  if (!oldSk) {
121
+ // should not be here
101
122
  logger.info('on-chain migration aborted because oldSk is empty', { did: blocklet.meta.did });
102
123
  return;
103
124
  }
104
125
 
105
126
  if (!newSk) {
127
+ // should not be here
106
128
  logger.info('on-chain migration aborted because newSk is empty', { did: blocklet.meta.did });
107
129
  return;
108
130
  }
109
131
 
110
- // ensure chain host
111
- const chainHost = getChainHost(blocklet);
112
- if (!chainHost || chainHost === 'none') {
113
- logger.info('on-chain migration aborted because CHAIN_HOST is empty', { did: blocklet.meta.did });
114
- return;
115
- }
116
-
117
- logger.info('on-chain migration for chain ', { did: blocklet.meta.did, host: chainHost });
118
-
119
132
  // ensure account changed
120
- const type = blocklet.configObj.BLOCKLET_WALLET_TYPE;
133
+ const type = blocklet.configObj?.BLOCKLET_WALLET_TYPE;
121
134
  const oldWallet = getBlockletWallet(oldSk, undefined, type);
122
135
  const newWallet = getBlockletWallet(newSk, undefined, type);
123
136
  if (oldWallet.address === newWallet.address) {
137
+ // should not be here
124
138
  logger.info('on-chain migration aborted because newSk same with oldSk', { did: blocklet.meta.did });
139
+ await ensureAccountOnMainChain(blocklet, newWallet);
125
140
  return;
126
141
  }
127
142
 
128
- // ensure old account exist on chain
129
- const client = new Client(chainHost);
130
- const oldResult = await client.getAccountState({ address: oldWallet.address });
131
- if (!oldResult.state) {
132
- logger.info('on-chain migration aborted because oldSk not declared on chain', { did: blocklet.meta.did });
143
+ // ensure chain host
144
+ const chainHost = getChainHost(blocklet);
145
+ if (!chainHost || chainHost === 'none') {
146
+ logger.info('on-chain migration aborted because CHAIN_HOST is empty', { did: blocklet.meta.did });
147
+ await ensureAccountOnMainChain(blocklet, newWallet);
133
148
  return;
134
149
  }
135
150
 
136
- // ensure new account not exist on chain
151
+ // migrate on chain
152
+ logger.info('on-chain migration for chain ', { did: blocklet.meta.did, host: chainHost });
153
+ const client = new Client(chainHost);
154
+ const oldResult = await client.getAccountState({ address: oldWallet.address });
137
155
  const newResult = await client.getAccountState({ address: newWallet.address });
138
- if (newResult.state) {
139
- logger.info('on-chain migration aborted because newSk declared on chain', { did: blocklet.meta.did });
140
- return;
141
- }
142
156
 
143
- logger.info('on-chain migration for wallets', { oldAddress: oldWallet.address, newAddress: newWallet.address });
157
+ if (oldResult.state && !newResult.state) {
158
+ logger.info('on-chain migration for wallets', { oldAddress: oldWallet.address, newAddress: newWallet.address });
144
159
 
145
- // migrate old account to new account
146
- const tx = await client.signAccountMigrateTx({
147
- tx: {
148
- itx: {
149
- address: newWallet.address,
150
- pk: newWallet.publicKey,
160
+ // migrate old account to new account
161
+ const tx = await client.signAccountMigrateTx({
162
+ tx: {
163
+ itx: {
164
+ address: newWallet.address,
165
+ pk: newWallet.publicKey,
166
+ },
151
167
  },
152
- },
153
- wallet: oldWallet,
154
- });
155
- const hash = await client.sendAccountMigrateTx({ tx, wallet: oldWallet });
156
- logger.info('on-chain migration done', { did: blocklet.meta.did, hash });
168
+ wallet: oldWallet,
169
+ });
170
+ const hash = await client.sendAccountMigrateTx({ tx, wallet: oldWallet });
171
+ logger.info('on-chain migration done', { did: blocklet.meta.did, hash });
172
+ } else {
173
+ if (!oldResult.state) {
174
+ logger.info('on-chain migration aborted because oldSk not declared on chain', { did: blocklet.meta.did });
175
+ }
176
+
177
+ if (newResult.state) {
178
+ logger.info('on-chain migration aborted because newSk declared on chain', { did: blocklet.meta.did });
179
+ }
180
+ }
181
+
182
+ await ensureAccountOnMainChain(blocklet, newWallet);
157
183
  };
158
184
 
159
185
  const migrateApplicationToStructV2 = async ({ did, appSk: newAppSk, context = {}, manager, states }) => {
package/lib/event.js CHANGED
@@ -5,6 +5,7 @@ const { wipeSensitiveData } = require('@blocklet/meta/lib/util');
5
5
  const logger = require('@abtnode/logger')('@abtnode/core:event');
6
6
  const { BLOCKLET_MODES, BlockletStatus, BlockletSource, BlockletEvents } = require('@blocklet/constant');
7
7
  const { EVENTS } = require('@abtnode/constant');
8
+ const { NodeMonitSender } = require('./monitor/node-monit-sender');
8
9
  const handleInstanceInStore = require('./util/public-to-store');
9
10
 
10
11
  const eventHub =
@@ -33,6 +34,7 @@ module.exports = ({
33
34
  }) => {
34
35
  const notificationState = states.notification;
35
36
  const nodeState = states.node;
37
+ const nodeMonitSender = new NodeMonitSender({ node });
36
38
 
37
39
  const events = new EventEmitter();
38
40
  events.setMaxListeners(0);
@@ -79,6 +81,10 @@ module.exports = ({
79
81
  if (typeof eventHandler === 'function') {
80
82
  eventHandler({ name, data });
81
83
  }
84
+
85
+ if (name === EVENTS.NODE_RUNTIME_INFO) {
86
+ nodeMonitSender.sendToWallet(data);
87
+ }
82
88
  };
83
89
 
84
90
  const handleBlockletInstall = async (name, { blocklet, context }) => {
@@ -0,0 +1,99 @@
1
+ const filesize = require('filesize');
2
+ const { sendToAppChannel } = require('@blocklet/sdk/lib/util/send-notification');
3
+ const { getAppPublicChannel } = require('@blocklet/meta/lib/channel');
4
+
5
+ const logger = require('@abtnode/logger')('@abtnode/core:monitor');
6
+
7
+ class NodeMonitSender {
8
+ constructor({ node, interval }) {
9
+ this.node = node;
10
+ this.nodeInfo = null;
11
+ this.interval = interval || 1000 * 60;
12
+ this.lastTime = 0;
13
+ }
14
+
15
+ async getNodeInfo() {
16
+ if (!this.nodeInfo) {
17
+ this.nodeInfo = await this.node.getNodeInfo();
18
+ }
19
+
20
+ return this.nodeInfo;
21
+ }
22
+
23
+ async getSender() {
24
+ const nodeInfo = await this.getNodeInfo();
25
+ return {
26
+ appDid: nodeInfo.did,
27
+ appSk: nodeInfo.sk,
28
+ };
29
+ }
30
+
31
+ /**
32
+ * @param {{
33
+ * cpu?: {
34
+ * currentLoad?: number
35
+ * };
36
+ * mem?: {
37
+ * total?: number;
38
+ * available?: number;
39
+ * };
40
+ * }} data
41
+ */
42
+ async sendToWallet(data) {
43
+ if (Date.now() - this.lastTime < this.interval) {
44
+ return;
45
+ }
46
+
47
+ if (!data || !data.cpu || !data.mem) {
48
+ return;
49
+ }
50
+
51
+ try {
52
+ const cpuPercent = data.cpu.currentLoad || 0;
53
+ const totalMem = data.mem.total || 0;
54
+ const freeMem = data.mem.available || 0;
55
+ const usedMem = totalMem - freeMem;
56
+ const cpuWanning = cpuPercent > 90;
57
+ const cpuContent = `${Math.floor(cpuPercent)}%`;
58
+ const memoryPercent = (usedMem / totalMem) * 100;
59
+ const memoryWanning = memoryPercent > 90;
60
+ const memContent = `${filesize(Math.floor(usedMem))} (${Math.floor(memoryPercent)}%)`;
61
+
62
+ const sender = await this.getSender();
63
+ const channel = getAppPublicChannel(sender.appDid);
64
+
65
+ const notification = {
66
+ type: 'feed',
67
+ feedType: 'data-tracker',
68
+ data: {
69
+ cardTitle: 'Server Usage',
70
+ items: [
71
+ {
72
+ title: 'CPU',
73
+ content: cpuContent,
74
+ content_color: cpuWanning ? '#FF1111' : '#222222',
75
+ },
76
+ {
77
+ title: 'MEM',
78
+ content: memContent,
79
+ content_color: memoryWanning ? '#FF1111' : '#222222',
80
+ },
81
+ ],
82
+ },
83
+ };
84
+
85
+ await sendToAppChannel(channel, 'message', notification, sender, process.env.ABT_NODE_SERVICE_PORT);
86
+
87
+ this.lastTime = Date.now();
88
+ } catch (error) {
89
+ delete error.request;
90
+ delete error.response;
91
+ delete error.config;
92
+ logger.error('failed to push notification to wallet', { error });
93
+ }
94
+ }
95
+ }
96
+
97
+ module.exports = {
98
+ NodeMonitSender,
99
+ };
@@ -1215,7 +1215,7 @@ const getUpdateMetaList = (oldBlocklet = {}, newBlocklet = {}) => {
1215
1215
  const res = [];
1216
1216
 
1217
1217
  forEachChildSync(newBlocklet, (b, { id }) => {
1218
- if (b.bundleSource && b.meta.version !== oldMap[id]) {
1218
+ if (b.bundleSource && semver.gt(b.meta.version, oldMap[id])) {
1219
1219
  res.push({ id, meta: b.meta });
1220
1220
  }
1221
1221
  });
@@ -4,4 +4,7 @@ const { version } = require('../../package.json');
4
4
  module.exports =
5
5
  process.env.NODE_ENV === 'test'
6
6
  ? axios
7
- : axios.create({ timeout: 10 * 1000, headers: { 'User-Agent': `ABTNode/${version}` } });
7
+ : axios.create({
8
+ timeout: 10 * 1000,
9
+ headers: { 'User-Agent': `ABTNode/${version}`, 'x-blocklet-server-version': version },
10
+ });
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.16.1-beta-cd775909",
6
+ "version": "1.16.2-beta-992f57fb",
7
7
  "description": "",
8
8
  "main": "lib/index.js",
9
9
  "files": [
@@ -19,18 +19,18 @@
19
19
  "author": "wangshijun <wangshijun2010@gmail.com> (http://github.com/wangshijun)",
20
20
  "license": "MIT",
21
21
  "dependencies": {
22
- "@abtnode/auth": "1.16.1-beta-cd775909",
23
- "@abtnode/certificate-manager": "1.16.1-beta-cd775909",
24
- "@abtnode/constant": "1.16.1-beta-cd775909",
25
- "@abtnode/cron": "1.16.1-beta-cd775909",
26
- "@abtnode/db": "1.16.1-beta-cd775909",
27
- "@abtnode/logger": "1.16.1-beta-cd775909",
28
- "@abtnode/queue": "1.16.1-beta-cd775909",
29
- "@abtnode/rbac": "1.16.1-beta-cd775909",
30
- "@abtnode/router-provider": "1.16.1-beta-cd775909",
31
- "@abtnode/static-server": "1.16.1-beta-cd775909",
32
- "@abtnode/timemachine": "1.16.1-beta-cd775909",
33
- "@abtnode/util": "1.16.1-beta-cd775909",
22
+ "@abtnode/auth": "1.16.2-beta-992f57fb",
23
+ "@abtnode/certificate-manager": "1.16.2-beta-992f57fb",
24
+ "@abtnode/constant": "1.16.2-beta-992f57fb",
25
+ "@abtnode/cron": "1.16.2-beta-992f57fb",
26
+ "@abtnode/db": "1.16.2-beta-992f57fb",
27
+ "@abtnode/logger": "1.16.2-beta-992f57fb",
28
+ "@abtnode/queue": "1.16.2-beta-992f57fb",
29
+ "@abtnode/rbac": "1.16.2-beta-992f57fb",
30
+ "@abtnode/router-provider": "1.16.2-beta-992f57fb",
31
+ "@abtnode/static-server": "1.16.2-beta-992f57fb",
32
+ "@abtnode/timemachine": "1.16.2-beta-992f57fb",
33
+ "@abtnode/util": "1.16.2-beta-992f57fb",
34
34
  "@arcblock/did": "1.18.65",
35
35
  "@arcblock/did-motif": "^1.1.10",
36
36
  "@arcblock/did-util": "1.18.65",
@@ -38,9 +38,9 @@
38
38
  "@arcblock/jwt": "^1.18.65",
39
39
  "@arcblock/pm2-events": "^0.0.5",
40
40
  "@arcblock/vc": "1.18.65",
41
- "@blocklet/constant": "1.16.1-beta-cd775909",
42
- "@blocklet/meta": "1.16.1-beta-cd775909",
43
- "@blocklet/sdk": "1.16.1-beta-cd775909",
41
+ "@blocklet/constant": "1.16.2-beta-992f57fb",
42
+ "@blocklet/meta": "1.16.2-beta-992f57fb",
43
+ "@blocklet/sdk": "1.16.2-beta-992f57fb",
44
44
  "@did-space/client": "^0.2.45",
45
45
  "@fidm/x509": "^1.2.1",
46
46
  "@ocap/client": "1.18.65",
@@ -57,6 +57,7 @@
57
57
  "detect-port": "^1.5.1",
58
58
  "escape-string-regexp": "^4.0.0",
59
59
  "fast-glob": "^3.2.12",
60
+ "filesize": "^6.4.0",
60
61
  "flat": "^5.0.2",
61
62
  "fs-extra": "^11.1.0",
62
63
  "get-port": "^5.1.1",
@@ -79,6 +80,7 @@
79
80
  "stream-to-promise": "^3.0.0",
80
81
  "systeminformation": "^5.12.6",
81
82
  "tar": "^6.1.11",
83
+ "transliteration": "^2.3.5",
82
84
  "ua-parser-js": "^1.0.2",
83
85
  "unzipper": "^0.10.11",
84
86
  "url-join": "^4.0.1",
@@ -91,5 +93,5 @@
91
93
  "express": "^4.18.2",
92
94
  "jest": "^27.5.1"
93
95
  },
94
- "gitHead": "9fd68e48cd87d5d8feafcf37dac1b586c1dccd8f"
96
+ "gitHead": "c2bbe98efb7ec70476263319053aa648a45c5a6a"
95
97
  }