@digicatapult/sqnc-process-management 2.2.0
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/LICENSE +190 -0
- package/README.md +213 -0
- package/build/package.json +64 -0
- package/build/src/index.d.ts +2 -0
- package/build/src/index.js +127 -0
- package/build/src/lib/process/_tests_/unit.test.d.ts +1 -0
- package/build/src/lib/process/_tests_/unit.test.js +112 -0
- package/build/src/lib/process/api.d.ts +7 -0
- package/build/src/lib/process/api.js +112 -0
- package/build/src/lib/process/constants.d.ts +3 -0
- package/build/src/lib/process/constants.js +3 -0
- package/build/src/lib/process/hex.d.ts +2 -0
- package/build/src/lib/process/hex.js +9 -0
- package/build/src/lib/process/index.d.ts +21 -0
- package/build/src/lib/process/index.js +205 -0
- package/build/src/lib/types/error.d.ts +14 -0
- package/build/src/lib/types/error.js +26 -0
- package/build/src/lib/types/validation.d.ts +5472 -0
- package/build/src/lib/types/validation.js +128 -0
- package/build/src/lib/utils/polkadot.d.ts +1 -0
- package/build/src/lib/utils/polkadot.js +25 -0
- package/build/src/version.d.ts +2 -0
- package/build/src/version.js +3 -0
- package/build/tests/fixtures/processes.d.ts +281 -0
- package/build/tests/fixtures/processes.js +1066 -0
- package/build/tests/fixtures/programs.d.ts +14 -0
- package/build/tests/fixtures/programs.js +211 -0
- package/build/tests/helpers/substrateHelper.d.ts +2 -0
- package/build/tests/helpers/substrateHelper.js +25 -0
- package/build/tests/integration/command-functions.test.d.ts +1 -0
- package/build/tests/integration/command-functions.test.js +327 -0
- package/package.json +64 -0
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { expect } from 'chai';
|
|
11
|
+
import { utf8ToHex, hexToUtf8 } from '../hex.js';
|
|
12
|
+
import { listTransforming, handleVerbose } from '../index.js';
|
|
13
|
+
import sample from '../../../../tests/fixtures/processes.js';
|
|
14
|
+
const defaultPolkadot = {
|
|
15
|
+
port: '9044',
|
|
16
|
+
user: 'alice',
|
|
17
|
+
host: 'localhost',
|
|
18
|
+
file: './exampleProcess.json',
|
|
19
|
+
};
|
|
20
|
+
describe('utf8ToHex', () => {
|
|
21
|
+
it('converts a utf8 string to hexadecimal', () => {
|
|
22
|
+
expect(utf8ToHex('test123')).to.equal('0x74657374313233');
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
describe('hexToUtf8', () => {
|
|
26
|
+
it('converts a prefixed string to hex', () => {
|
|
27
|
+
expect(hexToUtf8('0x74657374313233')).to.equal('test123');
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
describe('listTranforming', () => {
|
|
31
|
+
it('returns all transformed processes without a program (--verbose=false)', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
32
|
+
const enriched = Object.assign(Object.assign({}, sample[0]), { name: '123', status: 'Enabled', createdAtHash: 'abc', initialU8aLength: '32' });
|
|
33
|
+
const res = listTransforming([enriched], Object.assign(Object.assign({}, defaultPolkadot), { verbose: false }));
|
|
34
|
+
expect(res[0]).to.deep.contain({
|
|
35
|
+
name: '123',
|
|
36
|
+
status: 'Enabled',
|
|
37
|
+
version: 1,
|
|
38
|
+
});
|
|
39
|
+
}));
|
|
40
|
+
it('returns all transformed processes with a program (--verbose=true)', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
41
|
+
const enriched = Object.assign(Object.assign({}, sample[0]), { name: '123', status: 'Disabled', createdAtHash: 'abc', initialU8aLength: '32' });
|
|
42
|
+
const res = listTransforming([enriched], Object.assign(Object.assign({}, defaultPolkadot), { verbose: true }));
|
|
43
|
+
expect(res[0]).to.deep.contain({
|
|
44
|
+
name: '123',
|
|
45
|
+
status: 'Disabled',
|
|
46
|
+
version: 1,
|
|
47
|
+
});
|
|
48
|
+
}));
|
|
49
|
+
it('returns all options active', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
50
|
+
const enriched = Object.assign(Object.assign({}, sample[0]), { name: '123', status: 'Enabled', createdAtHash: 'abc', initialU8aLength: '32' });
|
|
51
|
+
const res = listTransforming([enriched], Object.assign(Object.assign({}, defaultPolkadot), { active: true }));
|
|
52
|
+
expect(res[0]).to.deep.contain({
|
|
53
|
+
name: '123',
|
|
54
|
+
status: 'Enabled',
|
|
55
|
+
version: 1,
|
|
56
|
+
});
|
|
57
|
+
}));
|
|
58
|
+
it('returns all options disabled but with active true', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
59
|
+
const enriched = Object.assign(Object.assign({}, sample[0]), { name: '123', status: 'Disabled', createdAtHash: 'abc', initialU8aLength: '32' });
|
|
60
|
+
const res = listTransforming([enriched], Object.assign(Object.assign({}, defaultPolkadot), { active: true }));
|
|
61
|
+
expect(res[0]).to.equal(undefined);
|
|
62
|
+
}));
|
|
63
|
+
it('returns all options disabled', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
64
|
+
const enriched = Object.assign(Object.assign({}, sample[0]), { name: '123', status: 'Disabled', createdAtHash: 'abc', initialU8aLength: '32' });
|
|
65
|
+
const res = listTransforming([enriched], Object.assign({}, defaultPolkadot));
|
|
66
|
+
expect(res[0]).to.deep.contain({
|
|
67
|
+
name: '123',
|
|
68
|
+
status: 'Disabled',
|
|
69
|
+
version: 1,
|
|
70
|
+
});
|
|
71
|
+
}));
|
|
72
|
+
it('returns all options active with status enabled', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
73
|
+
const enriched = Object.assign(Object.assign({}, sample[0]), { name: '123', status: 'Disabled', createdAtHash: 'abc', initialU8aLength: '32' });
|
|
74
|
+
const res = listTransforming([enriched], Object.assign(Object.assign({}, defaultPolkadot), { active: false }));
|
|
75
|
+
expect(res[0]).to.deep.contain({
|
|
76
|
+
name: '123',
|
|
77
|
+
status: 'Disabled',
|
|
78
|
+
version: 1,
|
|
79
|
+
});
|
|
80
|
+
}));
|
|
81
|
+
it('returns additional properties with raw', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
82
|
+
const enriched = Object.assign(Object.assign({}, sample[0]), { name: '123', status: 'Disabled', createdAtHash: 'abc', initialU8aLength: '32' });
|
|
83
|
+
const res = listTransforming([enriched], Object.assign(Object.assign({}, defaultPolkadot), { active: false, raw: true }));
|
|
84
|
+
expect(res[0]).to.deep.contain({
|
|
85
|
+
name: '0x313233',
|
|
86
|
+
status: 'Disabled',
|
|
87
|
+
version: 1,
|
|
88
|
+
createdAtHash: 'abc',
|
|
89
|
+
initialU8aLength: '32',
|
|
90
|
+
});
|
|
91
|
+
}));
|
|
92
|
+
});
|
|
93
|
+
describe('handleVerbose', function () {
|
|
94
|
+
it('should remove program when verbose == false', function () {
|
|
95
|
+
const result = handleVerbose({
|
|
96
|
+
name: 'test',
|
|
97
|
+
status: 'Enabled',
|
|
98
|
+
version: 1,
|
|
99
|
+
program: [],
|
|
100
|
+
}, false);
|
|
101
|
+
expect(result).to.deep.equal({ name: 'test', status: 'Enabled', version: 1 });
|
|
102
|
+
});
|
|
103
|
+
it('should include program when verbose == false', function () {
|
|
104
|
+
const result = handleVerbose({
|
|
105
|
+
name: 'test',
|
|
106
|
+
status: 'Enabled',
|
|
107
|
+
version: 1,
|
|
108
|
+
program: [],
|
|
109
|
+
}, true);
|
|
110
|
+
expect(result).to.deep.equal({ name: 'test', status: 'Enabled', version: 1, program: [] });
|
|
111
|
+
});
|
|
112
|
+
});
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare const createProcessTransaction: (polkadot: Polkadot.Polkadot, processId: Process.Hex, program: ValidationProgram, options: Polkadot.Options) => Promise<Process.Payload>;
|
|
2
|
+
export declare const disableProcessTransaction: (polkadot: Polkadot.Polkadot, processId: Process.Hex, version: number, options: Polkadot.Options) => Promise<Process.Payload>;
|
|
3
|
+
type GetAllFn = (options: Polkadot.Options) => Promise<Process.RawPayload[]>;
|
|
4
|
+
export declare const getAll: GetAllFn;
|
|
5
|
+
export declare const getVersion: (polkadot: Polkadot.Polkadot, processId: Process.Hex) => Promise<number>;
|
|
6
|
+
export declare const getProcess: (polkadot: Polkadot.Polkadot, processId: Process.Hex, version: number) => Promise<Process.Payload>;
|
|
7
|
+
export {};
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { ProgramError } from '../types/error.js';
|
|
11
|
+
import * as api from '../utils/polkadot.js';
|
|
12
|
+
import { hexToUtf8 } from './hex.js';
|
|
13
|
+
// TODO - refactor to validate payload?
|
|
14
|
+
// for some reason reduce did not work with Process.Program or ProgramStep[] type due to symbol.iterator
|
|
15
|
+
const isProgramValid = (program, out = { ops: 0, restrictions: -1 }) => {
|
|
16
|
+
program.forEach((step) => {
|
|
17
|
+
out = Object.hasOwn(step, 'Op') ? Object.assign(Object.assign({}, out), { ops: out.ops + 1 }) : Object.assign(Object.assign({}, out), { restrictions: out.restrictions + 1 });
|
|
18
|
+
});
|
|
19
|
+
return out.ops === out.restrictions;
|
|
20
|
+
};
|
|
21
|
+
// TODO refactor since api.ts other should be a util
|
|
22
|
+
// since createNodeApi, set's all routes we could use in process.index.ts
|
|
23
|
+
export const createProcessTransaction = (polkadot, processId, program, options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
24
|
+
const sudo = polkadot.keyring.addFromUri(options.USER_URI);
|
|
25
|
+
const supportsManualSeal = !!polkadot.api.rpc.engine.createBlock;
|
|
26
|
+
if (!isProgramValid(program))
|
|
27
|
+
throw new ProgramError('invalid program');
|
|
28
|
+
return new Promise((resolve, reject) => __awaiter(void 0, void 0, void 0, function* () {
|
|
29
|
+
try {
|
|
30
|
+
const unsub = yield polkadot.api.tx.sudo
|
|
31
|
+
.sudo(polkadot.api.tx.processValidation.createProcess(processId, program))
|
|
32
|
+
.signAndSend(sudo, (result) => {
|
|
33
|
+
if (result.status.isFinalized) {
|
|
34
|
+
const { event } = result.events.find(({ event: { method } }) => method === 'ProcessCreated');
|
|
35
|
+
const data = event.data;
|
|
36
|
+
const newProcess = {
|
|
37
|
+
name: data[0].toHuman(),
|
|
38
|
+
version: data[1].toNumber(),
|
|
39
|
+
status: 'Enabled',
|
|
40
|
+
program,
|
|
41
|
+
};
|
|
42
|
+
unsub();
|
|
43
|
+
resolve(newProcess);
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
if (supportsManualSeal) {
|
|
47
|
+
yield polkadot.api.rpc.engine.createBlock(true, true);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
catch (err) {
|
|
51
|
+
reject(err);
|
|
52
|
+
}
|
|
53
|
+
}));
|
|
54
|
+
});
|
|
55
|
+
export const disableProcessTransaction = (polkadot, processId, version, options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
56
|
+
const sudo = polkadot.keyring.addFromUri(options.USER_URI);
|
|
57
|
+
const supportsManualSeal = !!polkadot.api.rpc.engine.createBlock;
|
|
58
|
+
return new Promise((resolve, reject) => __awaiter(void 0, void 0, void 0, function* () {
|
|
59
|
+
try {
|
|
60
|
+
const unsub = yield polkadot.api.tx.sudo
|
|
61
|
+
.sudo(polkadot.api.tx.processValidation.disableProcess(processId, version))
|
|
62
|
+
.signAndSend(sudo, (result) => {
|
|
63
|
+
if (result.status.isFinalized) {
|
|
64
|
+
const { event } = result.events.find(({ event: { method } }) => method === 'ProcessDisabled');
|
|
65
|
+
const data = event.data;
|
|
66
|
+
const disabledProcess = {
|
|
67
|
+
name: data[0].toHuman(),
|
|
68
|
+
version: data[1].toNumber(),
|
|
69
|
+
status: 'Disabled',
|
|
70
|
+
};
|
|
71
|
+
unsub();
|
|
72
|
+
resolve(disabledProcess);
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
if (supportsManualSeal) {
|
|
76
|
+
yield polkadot.api.rpc.engine.createBlock(true, true);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
catch (err) {
|
|
80
|
+
reject(err);
|
|
81
|
+
}
|
|
82
|
+
}));
|
|
83
|
+
});
|
|
84
|
+
export const getAll = (options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
85
|
+
const polkadot = yield api.createNodeApi(options);
|
|
86
|
+
const processesRaw = yield polkadot.api.query.processValidation.processModel.entries();
|
|
87
|
+
return processesRaw.map(([idRaw, data]) => {
|
|
88
|
+
const id = idRaw.toHuman();
|
|
89
|
+
return {
|
|
90
|
+
name: id[0],
|
|
91
|
+
version: parseInt(id[1]),
|
|
92
|
+
status: data.status.toString(),
|
|
93
|
+
program: data.program.toJSON(),
|
|
94
|
+
createdAtHash: data.createdAtHash.toHuman(),
|
|
95
|
+
initialU8aLength: data.initialU8aLength,
|
|
96
|
+
};
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
export const getVersion = (polkadot, processId) => __awaiter(void 0, void 0, void 0, function* () {
|
|
100
|
+
const id = yield polkadot.api.query.processValidation.versionModel(processId);
|
|
101
|
+
return Number(id.toString());
|
|
102
|
+
});
|
|
103
|
+
export const getProcess = (polkadot, processId, version) => __awaiter(void 0, void 0, void 0, function* () {
|
|
104
|
+
const result = yield polkadot.api.query.processValidation.processModel(processId, version);
|
|
105
|
+
const data = Object(result.toHuman());
|
|
106
|
+
return {
|
|
107
|
+
name: hexToUtf8(processId),
|
|
108
|
+
version,
|
|
109
|
+
status: data.status,
|
|
110
|
+
program: data.program,
|
|
111
|
+
};
|
|
112
|
+
});
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export const utf8ToHex = (str) => {
|
|
2
|
+
const buffer = Buffer.from(str, 'utf8');
|
|
3
|
+
const bufferHex = buffer.toString('hex');
|
|
4
|
+
return `0x${bufferHex}`;
|
|
5
|
+
};
|
|
6
|
+
export const hexToUtf8 = (str) => {
|
|
7
|
+
const stripped = str.substring(2);
|
|
8
|
+
return Buffer.from(stripped, 'hex').toString('utf8');
|
|
9
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { CliInputParseError } from '../types/error.js';
|
|
3
|
+
export declare const defaultOptions: Polkadot.Options;
|
|
4
|
+
export declare const sanitizeInput: (data: string) => Process.Result<{
|
|
5
|
+
name: string;
|
|
6
|
+
}[], CliInputParseError>;
|
|
7
|
+
export declare const loadProcesses: ({ data, options, dryRun, verbose, }: {
|
|
8
|
+
data: string;
|
|
9
|
+
options?: Polkadot.Options | undefined;
|
|
10
|
+
dryRun?: boolean | undefined;
|
|
11
|
+
verbose?: boolean | undefined;
|
|
12
|
+
}) => Promise<Process.Response>;
|
|
13
|
+
export declare const listTransforming: (res: Process.RawPayload[], options: Process.CLIOptions) => {
|
|
14
|
+
program?: any;
|
|
15
|
+
name: string;
|
|
16
|
+
version: number;
|
|
17
|
+
status: "Enabled" | "Disabled" | "Enabled (dry-run)" | "Disabled (dry-run)";
|
|
18
|
+
}[];
|
|
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<Process.ProcessResponse>;
|
|
21
|
+
export declare const disableProcess: (name: string, processVersion: number, dryRun?: boolean, options?: Polkadot.Options) => Promise<Process.ProcessResponse>;
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
11
|
+
var t = {};
|
|
12
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
13
|
+
t[p] = s[p];
|
|
14
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
15
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
16
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
17
|
+
t[p[i]] = s[p[i]];
|
|
18
|
+
}
|
|
19
|
+
return t;
|
|
20
|
+
};
|
|
21
|
+
import { createNodeApi } from '../utils/polkadot.js';
|
|
22
|
+
import { createProcessTransaction, disableProcessTransaction, getVersion, getProcess } from './api.js';
|
|
23
|
+
import { utf8ToHex } from './hex.js';
|
|
24
|
+
import { processValidation, simpleProcesssValidation } from '../types/validation.js';
|
|
25
|
+
import { CliInputParseError, DisableError, ProgramError, VersionError } from '../types/error.js';
|
|
26
|
+
import { ZodError } from 'zod';
|
|
27
|
+
export const defaultOptions = {
|
|
28
|
+
API_HOST: 'localhost',
|
|
29
|
+
API_PORT: 9944,
|
|
30
|
+
USER_URI: '//Alice',
|
|
31
|
+
};
|
|
32
|
+
const textify = (obj) => {
|
|
33
|
+
return JSON.stringify(obj, (_key, val) => {
|
|
34
|
+
// convert snake case to camel case
|
|
35
|
+
if (val && typeof val === 'object') {
|
|
36
|
+
return Object.fromEntries(Object.entries(val).map(([k, v]) => {
|
|
37
|
+
return [
|
|
38
|
+
[...k]
|
|
39
|
+
.map((c, i) => (k[i - 1] === '_' ? c.toUpperCase() : c))
|
|
40
|
+
.filter((c) => c != '_')
|
|
41
|
+
.join(''),
|
|
42
|
+
v,
|
|
43
|
+
];
|
|
44
|
+
}));
|
|
45
|
+
}
|
|
46
|
+
if (typeof val === 'number')
|
|
47
|
+
return val.toString();
|
|
48
|
+
return val;
|
|
49
|
+
});
|
|
50
|
+
};
|
|
51
|
+
export const sanitizeInput = (data) => {
|
|
52
|
+
try {
|
|
53
|
+
return {
|
|
54
|
+
type: 'ok',
|
|
55
|
+
result: simpleProcesssValidation.parse(JSON.parse(data)),
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
catch (err) {
|
|
59
|
+
if (err instanceof Error) {
|
|
60
|
+
return {
|
|
61
|
+
type: 'error',
|
|
62
|
+
error: new CliInputParseError(err),
|
|
63
|
+
message: err.message,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
throw err;
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
export const loadProcesses = ({ data, options, dryRun, verbose, }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
70
|
+
const parsedRes = sanitizeInput(data);
|
|
71
|
+
if (parsedRes.type === 'error') {
|
|
72
|
+
return parsedRes;
|
|
73
|
+
}
|
|
74
|
+
const processes = parsedRes.result;
|
|
75
|
+
const [result, successCount] = yield processes.reduce((acc, process) => __awaiter(void 0, void 0, void 0, function* () {
|
|
76
|
+
const [result, oldCount] = yield acc;
|
|
77
|
+
const processResult = yield createProcess(process, dryRun, options, verbose);
|
|
78
|
+
const newCount = processResult.type === 'ok' ? oldCount + 1 : oldCount;
|
|
79
|
+
result[process.name] = processResult;
|
|
80
|
+
return [result, newCount];
|
|
81
|
+
}), Promise.resolve([{}, 0]));
|
|
82
|
+
return {
|
|
83
|
+
type: 'ok',
|
|
84
|
+
result,
|
|
85
|
+
message: `Successfully loaded ${successCount}/${processes.length} processes`,
|
|
86
|
+
};
|
|
87
|
+
});
|
|
88
|
+
export const listTransforming = (res, options) => {
|
|
89
|
+
let processes;
|
|
90
|
+
if (options.active) {
|
|
91
|
+
processes = res.filter(({ status }) => status === 'Enabled');
|
|
92
|
+
}
|
|
93
|
+
else if (options.disabled) {
|
|
94
|
+
processes = res.filter(({ status }) => status === 'Disabled');
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
processes = res;
|
|
98
|
+
}
|
|
99
|
+
if (options.raw) {
|
|
100
|
+
return processes.map((_a) => {
|
|
101
|
+
var { name, program } = _a, p = __rest(_a, ["name", "program"]);
|
|
102
|
+
return (Object.assign(Object.assign({ name: utf8ToHex(name) }, (options.verbose ? { program } : {})), p));
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
return processes.map((p) => {
|
|
107
|
+
return Object.assign({ name: p.name, version: p.version, status: p.status }, (options.verbose ? { program: p.program } : {}));
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
export const handleVerbose = (res, verbose) => {
|
|
112
|
+
if (verbose) {
|
|
113
|
+
return res;
|
|
114
|
+
}
|
|
115
|
+
const { program } = res, rest = __rest(res, ["program"]);
|
|
116
|
+
return rest;
|
|
117
|
+
};
|
|
118
|
+
export const createProcess = (processRaw, dryRun = false, options = defaultOptions, verbose = false) => __awaiter(void 0, void 0, void 0, function* () {
|
|
119
|
+
try {
|
|
120
|
+
const { name, version, program } = processValidation.parse(processRaw);
|
|
121
|
+
const processId = utf8ToHex(name);
|
|
122
|
+
const polkadot = yield createNodeApi(options);
|
|
123
|
+
const currentVersion = yield getVersion(polkadot, processId);
|
|
124
|
+
const expectedVersion = currentVersion + 1;
|
|
125
|
+
if (version > expectedVersion || version < currentVersion)
|
|
126
|
+
throw new VersionError(version, expectedVersion, name);
|
|
127
|
+
if (version === currentVersion) {
|
|
128
|
+
const process = yield getProcess(polkadot, processId, version);
|
|
129
|
+
if (program.length !== process.program.length)
|
|
130
|
+
throw new ProgramError('existing: programs are different lengths', process);
|
|
131
|
+
if (!program.every((step, i) => textify(step) === textify(process.program[i])))
|
|
132
|
+
throw new ProgramError('existing: program steps did not match', process);
|
|
133
|
+
return {
|
|
134
|
+
type: 'ok',
|
|
135
|
+
message: `Skipping: process ${name} is already created.`,
|
|
136
|
+
result: handleVerbose({
|
|
137
|
+
name,
|
|
138
|
+
version,
|
|
139
|
+
program,
|
|
140
|
+
status: process.status,
|
|
141
|
+
}, verbose),
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
if (dryRun)
|
|
145
|
+
return {
|
|
146
|
+
type: 'ok',
|
|
147
|
+
message: 'Dry run: transaction has not been created',
|
|
148
|
+
result: handleVerbose({
|
|
149
|
+
name,
|
|
150
|
+
version,
|
|
151
|
+
program,
|
|
152
|
+
status: 'Enabled (dry-run)',
|
|
153
|
+
}, verbose),
|
|
154
|
+
};
|
|
155
|
+
return {
|
|
156
|
+
type: 'ok',
|
|
157
|
+
message: `Transaction for new process ${name} has been successfully submitted`,
|
|
158
|
+
result: handleVerbose(yield createProcessTransaction(polkadot, processId, program, options), verbose),
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
catch (err) {
|
|
162
|
+
// err is basically from errors.ts or any exception
|
|
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;
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
export const disableProcess = (name, processVersion, dryRun = false, options = defaultOptions) => __awaiter(void 0, void 0, void 0, function* () {
|
|
183
|
+
const processId = utf8ToHex(name);
|
|
184
|
+
const polkadot = yield createNodeApi(options);
|
|
185
|
+
const currentProcess = yield getProcess(polkadot, processId, processVersion);
|
|
186
|
+
if (currentProcess.status === 'Disabled') {
|
|
187
|
+
throw new DisableError(`${name} with version ${processVersion} doesn't exist or is already disabled`);
|
|
188
|
+
}
|
|
189
|
+
if (dryRun) {
|
|
190
|
+
return {
|
|
191
|
+
type: 'ok',
|
|
192
|
+
message: `This will DISABLE the following process ${name}`,
|
|
193
|
+
result: {
|
|
194
|
+
name,
|
|
195
|
+
version: processVersion,
|
|
196
|
+
status: 'Disabled (dry-run)',
|
|
197
|
+
},
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
return {
|
|
201
|
+
type: 'ok',
|
|
202
|
+
message: 'Process has been disabled',
|
|
203
|
+
result: yield disableProcessTransaction(polkadot, processId, processVersion, options),
|
|
204
|
+
};
|
|
205
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export declare class DisableError extends Error {
|
|
2
|
+
constructor(m: string);
|
|
3
|
+
}
|
|
4
|
+
export declare class VersionError extends Error {
|
|
5
|
+
constructor(version: number, expected: number, name: string);
|
|
6
|
+
}
|
|
7
|
+
export declare class CliInputParseError extends Error {
|
|
8
|
+
baseError: Error;
|
|
9
|
+
constructor(baseError: Error);
|
|
10
|
+
}
|
|
11
|
+
export declare class ProgramError extends Error {
|
|
12
|
+
process?: Process.Payload;
|
|
13
|
+
constructor(m: string, process?: Process.Payload);
|
|
14
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export class DisableError extends Error {
|
|
2
|
+
constructor(m) {
|
|
3
|
+
super(m);
|
|
4
|
+
Object.setPrototypeOf(this, DisableError.prototype);
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
export class VersionError extends Error {
|
|
8
|
+
constructor(version, expected, name) {
|
|
9
|
+
super(`Process version ${version} is invalid. If you are trying to create a new version of process ${name} version should be ${expected}`);
|
|
10
|
+
Object.setPrototypeOf(this, VersionError.prototype);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
export class CliInputParseError extends Error {
|
|
14
|
+
constructor(baseError) {
|
|
15
|
+
super(`Error parsing input: ${baseError.message}`);
|
|
16
|
+
this.baseError = baseError;
|
|
17
|
+
Object.setPrototypeOf(this, CliInputParseError.prototype);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
export class ProgramError extends Error {
|
|
21
|
+
constructor(m, process) {
|
|
22
|
+
super(m);
|
|
23
|
+
this.process = process;
|
|
24
|
+
Object.setPrototypeOf(this, ProgramError.prototype);
|
|
25
|
+
}
|
|
26
|
+
}
|