@ocap/state 1.13.70 → 1.13.74

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.
@@ -1,4 +1,5 @@
1
1
  const pick = require('lodash/pick');
2
+ const Error = require('@ocap/util/lib/error');
2
3
 
3
4
  const Joi = require('@ocap/validator');
4
5
  const { create: createStateContext, update: updateStateContext } = require('../contexts/state');
@@ -15,6 +16,7 @@ const schema = Joi.object({
15
16
  address: Joi.DID().role('ROLE_ROLLUP').required(),
16
17
  tokenAddress: Joi.DID().role('ROLE_TOKEN').required(),
17
18
  contractAddress: Joi.DID().wallet('ethereum').required(),
19
+ migrateHistory: Joi.array().items(Joi.DID().wallet('ethereum')).default([]),
18
20
  paused: Joi.boolean().default(false),
19
21
 
20
22
  seedValidators: Joi.array().items(validator).min(1).required(),
@@ -68,6 +70,8 @@ const schema = Joi.object({
68
70
  const create = (attrs, context) => {
69
71
  const rollup = {
70
72
  context: createStateContext(context),
73
+ paused: false,
74
+ migrateHistory: [],
71
75
  blockHeight: 0,
72
76
  blockHash: '',
73
77
  ...pick(attrs, [
@@ -112,31 +116,30 @@ const update = (state, updates, context) => {
112
116
  // latest block height must be incremented on each update
113
117
  // latest block hash must be updated together with block height
114
118
  if (updates.blockHeight && !updates.blockHash) {
115
- throw new Error('Invalid rollup updates: blockHash must be updated together with blockHeight');
119
+ throw new Error('INVALID_ROLLUP_UPDATE', 'blockHash must be updated together with blockHeight');
116
120
  }
117
121
  if (updates.blockHash && !updates.blockHeight) {
118
- throw new Error('Invalid rollup updates: blockHeight must be updated together with blockHash');
122
+ throw new Error('INVALID_ROLLUP_UPDATE', 'blockHeight must be updated together with blockHash');
119
123
  }
120
124
  if (updates.blockHeight && updates.blockHash) {
121
125
  if (updates.blockHeight !== state.blockHeight + 1) {
122
- throw new Error('Invalid rollup updates: blockHeight must be incremented');
126
+ throw new Error('INVALID_ROLLUP_UPDATE', 'blockHeight must be incremented');
123
127
  }
124
128
  if (updates.blockHash === state.blockHash) {
125
- throw new Error('Invalid rollup updates: blockHash can not remain unchanged between blocks');
129
+ throw new Error('INVALID_ROLLUP_UPDATE', 'blockHash can not remain unchanged between blocks');
126
130
  }
127
131
  }
128
132
 
129
133
  if (typeof updates.blockHeight !== 'undefined' && !updates.blockHeight) {
130
- throw new Error('Invalid rollup updates: blockHeight can not be unset');
134
+ throw new Error('INVALID_ROLLUP_UPDATE', 'blockHeight can not be unset');
131
135
  }
132
136
  if (typeof updates.blockHash !== 'undefined' && !updates.blockHash) {
133
- throw new Error('Invalid rollup updates: blockHash can not be unset');
137
+ throw new Error('INVALID_ROLLUP_UPDATE', 'blockHash can not be unset');
134
138
  }
135
139
 
136
140
  const rollup = {
137
141
  ...state,
138
142
  ...pick(updates, [
139
- 'paused',
140
143
  'validators',
141
144
  'minStakeAmount',
142
145
  'maxStakeAmount',
@@ -170,10 +173,59 @@ const update = (state, updates, context) => {
170
173
  return validate(rollup);
171
174
  };
172
175
 
176
+ const pause = (state, context) => {
177
+ if (state.paused) {
178
+ throw new Error('INVALID_PAUSE_ATTEMPT', 'rollup already paused');
179
+ }
180
+
181
+ const rollup = {
182
+ ...state,
183
+ paused: true,
184
+ context: updateStateContext(state.context, context),
185
+ };
186
+
187
+ return validate(rollup);
188
+ };
189
+
190
+ const resume = (state, context) => {
191
+ if (state.paused === false) {
192
+ throw new Error('INVALID_RESUME_ATTEMPT', 'rollup not paused');
193
+ }
194
+
195
+ const rollup = {
196
+ ...state,
197
+ paused: false,
198
+ context: updateStateContext(state.context, context),
199
+ };
200
+
201
+ return validate(rollup);
202
+ };
203
+
204
+ const migrate = (state, to, context) => {
205
+ if (state.paused === false) {
206
+ throw new Error('INVALID_MIGRATE_ATTEMPT', 'rollup not paused');
207
+ }
208
+ if (state.contractAddress === to) {
209
+ throw new Error('INVALID_MIGRATE_ATTEMPT', 'can not migrate to self');
210
+ }
211
+ if (state.migrateHistory.includes(to)) {
212
+ throw new Error('INVALID_MIGRATE_ATTEMPT', 'can not migrate to history contracts');
213
+ }
214
+
215
+ const rollup = {
216
+ ...state,
217
+ contractAddress: to,
218
+ migrateHistory: [...state.migrateHistory, state.contractAddress],
219
+ context: updateStateContext(state.context, context),
220
+ };
221
+
222
+ return validate(rollup);
223
+ };
224
+
173
225
  const validate = (state) => {
174
226
  const { value, error } = schema.validate(state);
175
227
  if (error) {
176
- throw new Error(`Invalid rollup: ${error.details.map((x) => x.message).join(', ')}`);
228
+ throw new Error('INVALID_ROLLUP_PROPS', error.details.map((x) => x.message).join(', '));
177
229
  }
178
230
 
179
231
  if (!value.data) {
@@ -183,4 +235,4 @@ const validate = (state) => {
183
235
  return value;
184
236
  };
185
237
 
186
- module.exports = { create, update, validate, schema };
238
+ module.exports = { create, update, pause, resume, migrate, validate, schema };
@@ -1,4 +1,5 @@
1
1
  const pick = require('lodash/pick');
2
+ const { toChecksumAddress } = require('@arcblock/did/lib/type');
2
3
 
3
4
  const { create: createStateContext } = require('../contexts/state');
4
5
 
@@ -26,6 +27,8 @@ const create = (attrs, context) => {
26
27
  }
27
28
  if (!token.foreignToken) {
28
29
  token.foreignToken = null;
30
+ } else {
31
+ token.foreignToken.contractAddress = toChecksumAddress(token.foreignToken.contractAddress);
29
32
  }
30
33
 
31
34
  return token;
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.13.70",
6
+ "version": "1.13.74",
7
7
  "description": "",
8
8
  "main": "lib/index.js",
9
9
  "files": [
@@ -16,12 +16,12 @@
16
16
  "coverage": "npm run test -- --coverage"
17
17
  },
18
18
  "dependencies": {
19
- "@arcblock/did": "1.13.70",
20
- "@ocap/contract": "1.13.70",
21
- "@ocap/mcrypto": "1.13.70",
22
- "@ocap/message": "1.13.70",
23
- "@ocap/util": "1.13.70",
24
- "@ocap/validator": "1.13.70",
19
+ "@arcblock/did": "1.13.74",
20
+ "@ocap/contract": "1.13.74",
21
+ "@ocap/mcrypto": "1.13.74",
22
+ "@ocap/message": "1.13.74",
23
+ "@ocap/util": "1.13.74",
24
+ "@ocap/validator": "1.13.74",
25
25
  "bloom-filters": "^1.3.1",
26
26
  "lodash": "^4.17.21"
27
27
  },
@@ -31,5 +31,5 @@
31
31
  "keywords": [],
32
32
  "author": "wangshijun <wangshijun2010@gmail.com> (http://github.com/wangshijun)",
33
33
  "license": "MIT",
34
- "gitHead": "17d673049138356f4cce9ca3d59e2f531b479c2d"
34
+ "gitHead": "24531e4142d2dc888d6f6091d82d2f7e5abe22ae"
35
35
  }