@ocap/state 1.13.69 → 1.13.73
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/states/rollup.js +61 -9
- package/package.json +8 -8
package/lib/states/rollup.js
CHANGED
|
@@ -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('
|
|
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('
|
|
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('
|
|
126
|
+
throw new Error('INVALID_ROLLUP_UPDATE', 'blockHeight must be incremented');
|
|
123
127
|
}
|
|
124
128
|
if (updates.blockHash === state.blockHash) {
|
|
125
|
-
throw new Error('
|
|
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('
|
|
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('
|
|
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(
|
|
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 };
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"publishConfig": {
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
|
-
"version": "1.13.
|
|
6
|
+
"version": "1.13.73",
|
|
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.
|
|
20
|
-
"@ocap/contract": "1.13.
|
|
21
|
-
"@ocap/mcrypto": "1.13.
|
|
22
|
-
"@ocap/message": "1.13.
|
|
23
|
-
"@ocap/util": "1.13.
|
|
24
|
-
"@ocap/validator": "1.13.
|
|
19
|
+
"@arcblock/did": "1.13.73",
|
|
20
|
+
"@ocap/contract": "1.13.73",
|
|
21
|
+
"@ocap/mcrypto": "1.13.73",
|
|
22
|
+
"@ocap/message": "1.13.73",
|
|
23
|
+
"@ocap/util": "1.13.73",
|
|
24
|
+
"@ocap/validator": "1.13.73",
|
|
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": "
|
|
34
|
+
"gitHead": "ffce10eaa7c72fe6d6fdc534f7e15cbdb3e6a917"
|
|
35
35
|
}
|