@digicatapult/sqnc-process-management 2.2.16 → 2.2.17
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/build/package.json +1 -1
- package/build/src/lib/process/api.d.ts +3 -1
- package/build/src/lib/process/api.js +31 -24
- package/build/src/lib/process/index.d.ts +3 -1
- package/build/src/lib/process/index.js +74 -44
- package/build/tests/integration/command-functions.test.js +23 -18
- package/package.json +1 -1
package/build/package.json
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
export declare const createProcessTransaction: (polkadot: Polkadot.Polkadot, processId: Process.Hex, program: ValidationProgram, options: Polkadot.Options) => Promise<
|
|
1
|
+
export declare const createProcessTransaction: (polkadot: Polkadot.Polkadot, processId: Process.Hex, program: ValidationProgram, options: Polkadot.Options) => Promise<{
|
|
2
|
+
waitForFinal: Promise<Process.Payload>;
|
|
3
|
+
}>;
|
|
2
4
|
export declare const disableProcessTransaction: (polkadot: Polkadot.Polkadot, processId: Process.Hex, version: number, options: Polkadot.Options) => Promise<Process.Payload>;
|
|
3
5
|
type GetAllFn = (options: Polkadot.Options) => Promise<Process.RawPayload[]>;
|
|
4
6
|
export declare const getAll: GetAllFn;
|
|
@@ -25,32 +25,39 @@ export const createProcessTransaction = (polkadot, processId, program, options)
|
|
|
25
25
|
const supportsManualSeal = !!polkadot.api.rpc.engine.createBlock;
|
|
26
26
|
if (!isProgramValid(program))
|
|
27
27
|
throw new ProgramError('invalid program');
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
28
|
+
let resolve = () => { };
|
|
29
|
+
let reject = () => { };
|
|
30
|
+
const result = new Promise((res, rej) => {
|
|
31
|
+
resolve = res;
|
|
32
|
+
reject = rej;
|
|
33
|
+
});
|
|
34
|
+
try {
|
|
35
|
+
const unsub = yield polkadot.api.tx.sudo
|
|
36
|
+
.sudo(polkadot.api.tx.processValidation.createProcess(processId, program))
|
|
37
|
+
.signAndSend(sudo, { nonce: -1 }, (result) => {
|
|
38
|
+
if (result.status.isFinalized) {
|
|
39
|
+
const { event } = result.events.find(({ event: { method } }) => method === 'ProcessCreated');
|
|
40
|
+
const data = event.data;
|
|
41
|
+
const newProcess = {
|
|
42
|
+
name: data[0].toHuman(),
|
|
43
|
+
version: data[1].toNumber(),
|
|
44
|
+
status: 'Enabled',
|
|
45
|
+
program,
|
|
46
|
+
};
|
|
47
|
+
unsub();
|
|
48
|
+
resolve(newProcess);
|
|
48
49
|
}
|
|
50
|
+
});
|
|
51
|
+
if (supportsManualSeal) {
|
|
52
|
+
yield polkadot.api.rpc.engine.createBlock(true, true);
|
|
49
53
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
}
|
|
54
|
+
}
|
|
55
|
+
catch (err) {
|
|
56
|
+
reject(err);
|
|
57
|
+
}
|
|
58
|
+
return {
|
|
59
|
+
waitForFinal: result,
|
|
60
|
+
};
|
|
54
61
|
});
|
|
55
62
|
export const disableProcessTransaction = (polkadot, processId, version, options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
56
63
|
const sudo = polkadot.keyring.addFromUri(options.USER_URI);
|
|
@@ -17,5 +17,7 @@ export declare const listTransforming: (res: Process.RawPayload[], options: Proc
|
|
|
17
17
|
status: "Enabled" | "Disabled" | "Enabled (dry-run)" | "Disabled (dry-run)";
|
|
18
18
|
}[];
|
|
19
19
|
export declare const handleVerbose: (res: Process.Payload, verbose: boolean) => Process.Payload;
|
|
20
|
-
export declare const createProcess: (processRaw: ValidationProcess, dryRun?: boolean, options?: Polkadot.Options, verbose?: boolean) => Promise<
|
|
20
|
+
export declare const createProcess: (processRaw: ValidationProcess, dryRun?: boolean, options?: Polkadot.Options, verbose?: boolean) => Promise<{
|
|
21
|
+
waitForFinalised: Promise<Process.ProcessResponse>;
|
|
22
|
+
}>;
|
|
21
23
|
export declare const disableProcess: (name: string, processVersion: number, dryRun?: boolean, options?: Polkadot.Options) => Promise<Process.ProcessResponse>;
|
|
@@ -72,13 +72,22 @@ export const loadProcesses = (_a) => __awaiter(void 0, [_a], void 0, function* (
|
|
|
72
72
|
return parsedRes;
|
|
73
73
|
}
|
|
74
74
|
const processes = parsedRes.result;
|
|
75
|
-
const
|
|
76
|
-
|
|
77
|
-
const
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
75
|
+
const processTxs = new Map();
|
|
76
|
+
for (const process of processes) {
|
|
77
|
+
const { waitForFinalised } = yield createProcess(process, dryRun, options, verbose);
|
|
78
|
+
processTxs.set(process.name, waitForFinalised);
|
|
79
|
+
}
|
|
80
|
+
yield Promise.all(processTxs.values());
|
|
81
|
+
const result = {};
|
|
82
|
+
let successCount = 0;
|
|
83
|
+
for (const [key, processTxProm] of processTxs) {
|
|
84
|
+
let response = yield processTxProm;
|
|
85
|
+
result[key] = response;
|
|
86
|
+
if (response.type === 'ok') {
|
|
87
|
+
successCount = successCount + 1;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
// calculate the result and successCount
|
|
82
91
|
return {
|
|
83
92
|
type: 'ok',
|
|
84
93
|
result,
|
|
@@ -116,6 +125,32 @@ export const handleVerbose = (res, verbose) => {
|
|
|
116
125
|
return rest;
|
|
117
126
|
};
|
|
118
127
|
export const createProcess = (processRaw_1, ...args_1) => __awaiter(void 0, [processRaw_1, ...args_1], void 0, function* (processRaw, dryRun = false, options = defaultOptions, verbose = false) {
|
|
128
|
+
const handleErr = (err) => {
|
|
129
|
+
// err is basically from errors.ts or any exception
|
|
130
|
+
// process errors will contain specific messages and/or process
|
|
131
|
+
// Promise<Process.Result> is in try {} and any exception is in catch {}
|
|
132
|
+
if (err instanceof ProgramError || err instanceof VersionError || err instanceof ZodError) {
|
|
133
|
+
const result = {
|
|
134
|
+
type: 'error',
|
|
135
|
+
error: err,
|
|
136
|
+
message: err.message,
|
|
137
|
+
};
|
|
138
|
+
return {
|
|
139
|
+
waitForFinalised: Promise.resolve(result),
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
else if (err instanceof Error) {
|
|
143
|
+
const result = {
|
|
144
|
+
type: 'error',
|
|
145
|
+
error: err,
|
|
146
|
+
message: 'An unknown error occurred',
|
|
147
|
+
};
|
|
148
|
+
return {
|
|
149
|
+
waitForFinalised: Promise.resolve(result),
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
throw err;
|
|
153
|
+
};
|
|
119
154
|
try {
|
|
120
155
|
const { name, version, program } = processValidation.parse(processRaw);
|
|
121
156
|
const processId = utf8ToHex(name);
|
|
@@ -131,52 +166,47 @@ export const createProcess = (processRaw_1, ...args_1) => __awaiter(void 0, [pro
|
|
|
131
166
|
if (!program.every((step, i) => textify(step) === textify(process.program[i])))
|
|
132
167
|
throw new ProgramError('existing: program steps did not match', process);
|
|
133
168
|
return {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
169
|
+
waitForFinalised: Promise.resolve({
|
|
170
|
+
type: 'ok',
|
|
171
|
+
message: `Skipping: process ${name} is already created.`,
|
|
172
|
+
result: handleVerbose({
|
|
173
|
+
name,
|
|
174
|
+
version,
|
|
175
|
+
program,
|
|
176
|
+
status: process.status,
|
|
177
|
+
}, verbose),
|
|
178
|
+
}),
|
|
142
179
|
};
|
|
143
180
|
}
|
|
144
181
|
if (dryRun)
|
|
145
182
|
return {
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
183
|
+
waitForFinalised: Promise.resolve({
|
|
184
|
+
type: 'ok',
|
|
185
|
+
message: 'Dry run: transaction has not been created',
|
|
186
|
+
result: handleVerbose({
|
|
187
|
+
name,
|
|
188
|
+
version,
|
|
189
|
+
program,
|
|
190
|
+
status: 'Enabled (dry-run)',
|
|
191
|
+
}, verbose),
|
|
192
|
+
}),
|
|
154
193
|
};
|
|
194
|
+
const createProcessTx = yield createProcessTransaction(polkadot, processId, program, options);
|
|
155
195
|
return {
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
196
|
+
waitForFinalised: createProcessTx.waitForFinal
|
|
197
|
+
.then((process) => {
|
|
198
|
+
const result = {
|
|
199
|
+
type: 'ok',
|
|
200
|
+
message: `Transaction for new process ${name} has been successfully submitted`,
|
|
201
|
+
result: handleVerbose(process, verbose),
|
|
202
|
+
};
|
|
203
|
+
return result;
|
|
204
|
+
})
|
|
205
|
+
.catch((err) => handleErr(err).waitForFinalised),
|
|
159
206
|
};
|
|
160
207
|
}
|
|
161
208
|
catch (err) {
|
|
162
|
-
|
|
163
|
-
// process errors will comntain specific messages and/or process
|
|
164
|
-
// Promise<Process.Result> is in try {} and any exception is in catch {}
|
|
165
|
-
if (err instanceof ProgramError || err instanceof VersionError || err instanceof ZodError) {
|
|
166
|
-
return {
|
|
167
|
-
type: 'error',
|
|
168
|
-
error: err,
|
|
169
|
-
message: err.message,
|
|
170
|
-
};
|
|
171
|
-
}
|
|
172
|
-
else if (err instanceof Error) {
|
|
173
|
-
return {
|
|
174
|
-
type: 'error',
|
|
175
|
-
error: err,
|
|
176
|
-
message: 'An unknown error occurred',
|
|
177
|
-
};
|
|
178
|
-
}
|
|
179
|
-
throw err;
|
|
209
|
+
return handleErr(err);
|
|
180
210
|
}
|
|
181
211
|
});
|
|
182
212
|
export const disableProcess = (name_1, processVersion_1, ...args_2) => __awaiter(void 0, [name_1, processVersion_1, ...args_2], void 0, function* (name, processVersion, dryRun = false, options = defaultOptions) {
|
|
@@ -18,11 +18,16 @@ import { getAll } from '../../src/lib/process/api.js';
|
|
|
18
18
|
/* fixtures */
|
|
19
19
|
import processesExample from '../fixtures/processes.js';
|
|
20
20
|
const polkadotOptions = { API_HOST: 'localhost', API_PORT: 9944, USER_URI: '//Alice', MANUAL_SEAL: true };
|
|
21
|
+
const createProcessAndWait = (processRaw, dryRun, options, verbose) => __awaiter(void 0, void 0, void 0, function* () {
|
|
22
|
+
const { waitForFinalised } = yield createProcess(processRaw, dryRun, options, verbose);
|
|
23
|
+
const result = yield waitForFinalised;
|
|
24
|
+
return result;
|
|
25
|
+
});
|
|
21
26
|
describe('Process creation and deletion, listing', () => {
|
|
22
27
|
describe('Happy path', () => {
|
|
23
28
|
describe('Multiple processes', () => {
|
|
24
29
|
it('skips already created processes and creates new ones', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
25
|
-
yield
|
|
30
|
+
yield createProcessAndWait({
|
|
26
31
|
name: 'existing-process-test',
|
|
27
32
|
version: 1,
|
|
28
33
|
program: simple2,
|
|
@@ -113,7 +118,7 @@ describe('Process creation and deletion, listing', () => {
|
|
|
113
118
|
const processName = '0';
|
|
114
119
|
const currentVersion = yield getVersionHelper(processName);
|
|
115
120
|
const bumpedVersion = currentVersion + 1;
|
|
116
|
-
const newProcess = yield
|
|
121
|
+
const newProcess = yield createProcessAndWait({ name: processName, version: bumpedVersion, program: simple }, false, polkadotOptions);
|
|
117
122
|
if (newProcess.type !== 'ok') {
|
|
118
123
|
expect.fail('Expected process creation to succeed');
|
|
119
124
|
}
|
|
@@ -137,7 +142,7 @@ describe('Process creation and deletion, listing', () => {
|
|
|
137
142
|
const processName = '0';
|
|
138
143
|
const currentVersion = yield getVersionHelper(processName);
|
|
139
144
|
const bumpedVersion = currentVersion + 1;
|
|
140
|
-
const newProcess = yield
|
|
145
|
+
const newProcess = yield createProcessAndWait({ name: processName, version: bumpedVersion, program: validAllRestrictions }, true, polkadotOptions);
|
|
141
146
|
if (newProcess.type !== 'ok') {
|
|
142
147
|
expect.fail('Expected process create to succeed');
|
|
143
148
|
}
|
|
@@ -152,7 +157,7 @@ describe('Process creation and deletion, listing', () => {
|
|
|
152
157
|
const processName = '0';
|
|
153
158
|
const currentVersion = yield getVersionHelper(processName);
|
|
154
159
|
const bumpedVersion = currentVersion + 1;
|
|
155
|
-
const newProcess = yield
|
|
160
|
+
const newProcess = yield createProcessAndWait({ name: processName, version: bumpedVersion, program: validAllRestrictions }, false, polkadotOptions);
|
|
156
161
|
expect(newProcess.type).to.equal('ok');
|
|
157
162
|
const disabledProcess = yield disableProcess(processName, bumpedVersion, true, polkadotOptions);
|
|
158
163
|
if (disabledProcess.type !== 'ok') {
|
|
@@ -186,7 +191,7 @@ describe('Process creation and deletion, listing', () => {
|
|
|
186
191
|
describe('Multiple uploads', () => {
|
|
187
192
|
// TODO could prestage in before if more scenarios
|
|
188
193
|
it('skips and notifies if process programs are different', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
189
|
-
yield
|
|
194
|
+
yield createProcessAndWait({ name: 'existing-length', version: 1, program: validAllRestrictions }, false, polkadotOptions);
|
|
190
195
|
const process2Name = 'should-create-1';
|
|
191
196
|
const process2BumpedV = (yield getVersionHelper(process2Name)) + 1;
|
|
192
197
|
const res = yield loadProcesses({
|
|
@@ -208,7 +213,7 @@ describe('Process creation and deletion, listing', () => {
|
|
|
208
213
|
expect(process2Res.message).to.equal('Transaction for new process should-create-1 has been successfully submitted');
|
|
209
214
|
}));
|
|
210
215
|
it('also fails if number of steps matches but POSTFIX does not', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
211
|
-
yield
|
|
216
|
+
yield createProcessAndWait({ name: 'existing-steps', version: 1, program: simple }, false, polkadotOptions);
|
|
212
217
|
const process2Name = 'should-create-2';
|
|
213
218
|
const process2BumpedV = (yield getVersionHelper(process2Name)) + 1;
|
|
214
219
|
const res = yield loadProcesses({
|
|
@@ -224,8 +229,8 @@ describe('Process creation and deletion, listing', () => {
|
|
|
224
229
|
}));
|
|
225
230
|
});
|
|
226
231
|
it('does not create new one and notifies if programs are different length', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
227
|
-
yield
|
|
228
|
-
const res = yield
|
|
232
|
+
yield createProcessAndWait({ name: 'existing-single', version: 1, program: validAllRestrictions }, false, polkadotOptions);
|
|
233
|
+
const res = yield createProcessAndWait({ name: 'existing-single', version: 1, program: simple }, false, polkadotOptions);
|
|
229
234
|
if (res.type !== 'error') {
|
|
230
235
|
expect.fail('Expected program create with different lengths to fail');
|
|
231
236
|
}
|
|
@@ -233,8 +238,8 @@ describe('Process creation and deletion, listing', () => {
|
|
|
233
238
|
expect(res.error).to.instanceOf(ProgramError);
|
|
234
239
|
}));
|
|
235
240
|
it('does not create new one and notifies if programs same are length but do not match', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
236
|
-
yield
|
|
237
|
-
const res = yield
|
|
241
|
+
yield createProcessAndWait({ name: 'existing-steps-single', version: 1, program: simple2 }, false, polkadotOptions);
|
|
242
|
+
const res = yield createProcessAndWait({ name: 'existing-steps-single', version: 1, program: [{ Restriction: 'None' }] }, false, polkadotOptions);
|
|
238
243
|
if (res.type !== 'error') {
|
|
239
244
|
expect.fail('Expected program create with different lengths to fail');
|
|
240
245
|
}
|
|
@@ -249,7 +254,7 @@ describe('Process creation and deletion, listing', () => {
|
|
|
249
254
|
}));
|
|
250
255
|
});
|
|
251
256
|
it('fails for invalid POSTFIX notation', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
252
|
-
const res = yield
|
|
257
|
+
const res = yield createProcessAndWait({ name: validProcessName, version: validVersionNumber, program: invalidPOSIX }, false, polkadotOptions);
|
|
253
258
|
if (res.type !== 'error') {
|
|
254
259
|
expect.fail('Expected process create to fail');
|
|
255
260
|
}
|
|
@@ -257,7 +262,7 @@ describe('Process creation and deletion, listing', () => {
|
|
|
257
262
|
expect(res.message).to.equal('invalid program');
|
|
258
263
|
}));
|
|
259
264
|
it('fails for invalid restriction key', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
260
|
-
const res = yield
|
|
265
|
+
const res = yield createProcessAndWait({
|
|
261
266
|
name: validProcessName,
|
|
262
267
|
version: validVersionNumber,
|
|
263
268
|
program: invalidRestrictionKey,
|
|
@@ -273,14 +278,14 @@ describe('Process creation and deletion, listing', () => {
|
|
|
273
278
|
});
|
|
274
279
|
}));
|
|
275
280
|
it('fails for invalid restriction value', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
276
|
-
const res = yield
|
|
281
|
+
const res = yield createProcessAndWait({ name: validProcessName, version: validVersionNumber, program: invalidRestrictionValue }, false, polkadotOptions);
|
|
277
282
|
if (res.type !== 'error') {
|
|
278
283
|
expect.fail('Expected process create to fail');
|
|
279
284
|
}
|
|
280
285
|
expect(res.error).to.be.instanceOf(ZodError);
|
|
281
286
|
}));
|
|
282
287
|
it('fails for invalid json', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
283
|
-
const res = yield
|
|
288
|
+
const res = yield createProcessAndWait({ name: validProcessName, version: validVersionNumber, program: 'invalidJson' }, false, polkadotOptions);
|
|
284
289
|
if (res.type !== 'error') {
|
|
285
290
|
expect.fail('Expected process create to fail');
|
|
286
291
|
}
|
|
@@ -288,7 +293,7 @@ describe('Process creation and deletion, listing', () => {
|
|
|
288
293
|
}));
|
|
289
294
|
it('fails to create for too low version', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
290
295
|
// - 2 because -1 would make current = valid
|
|
291
|
-
const res = yield
|
|
296
|
+
const res = yield createProcessAndWait({ name: validProcessName, version: validVersionNumber - 2, program: validAllRestrictions }, false, polkadotOptions);
|
|
292
297
|
if (res.type !== 'error') {
|
|
293
298
|
expect.fail('Expected process create to fail');
|
|
294
299
|
}
|
|
@@ -296,7 +301,7 @@ describe('Process creation and deletion, listing', () => {
|
|
|
296
301
|
expect(res.error).to.be.instanceOf(VersionError);
|
|
297
302
|
}));
|
|
298
303
|
it('fails to create for too high version', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
299
|
-
const res = yield
|
|
304
|
+
const res = yield createProcessAndWait({ name: validProcessName, version: validVersionNumber + 1, program: validAllRestrictions }, false, polkadotOptions);
|
|
300
305
|
if (res.type !== 'error') {
|
|
301
306
|
expect.fail('Expected process create to fail');
|
|
302
307
|
}
|
|
@@ -305,7 +310,7 @@ describe('Process creation and deletion, listing', () => {
|
|
|
305
310
|
}));
|
|
306
311
|
it('fails to create with too long process id', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
307
312
|
const processName = '0'.repeat(Constants.PROCESS_ID_LENGTH + 1);
|
|
308
|
-
const res = yield
|
|
313
|
+
const res = yield createProcessAndWait({ name: processName, version: 1, program: validAllRestrictions }, false, polkadotOptions);
|
|
309
314
|
if (res.type !== 'error') {
|
|
310
315
|
expect.fail('Expected process create to fail');
|
|
311
316
|
}
|
|
@@ -322,7 +327,7 @@ describe('Process creation and deletion, listing', () => {
|
|
|
322
327
|
expect(err).instanceOf(DisableError);
|
|
323
328
|
}));
|
|
324
329
|
it('fails to disable process a second time', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
325
|
-
const newProcess = yield
|
|
330
|
+
const newProcess = yield createProcessAndWait({ name: validProcessName, version: validVersionNumber, program: simple }, false, polkadotOptions);
|
|
326
331
|
expect(newProcess.type).to.equal('ok');
|
|
327
332
|
const firstDisable = yield disableProcess(validProcessName, validVersionNumber, false, polkadotOptions);
|
|
328
333
|
expect(firstDisable.type).to.equal('ok');
|