@acala-network/chopsticks 0.2.2 → 0.2.3
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 +201 -0
- package/dist/bindings.d.ts +2 -0
- package/dist/bindings.js +31 -0
- package/dist/blockchain/block.d.ts +7 -5
- package/dist/blockchain/block.js +23 -63
- package/dist/blockchain/index.d.ts +7 -5
- package/dist/blockchain/index.js +7 -3
- package/dist/blockchain/inherents.d.ts +5 -7
- package/dist/blockchain/inherents.js +10 -9
- package/dist/blockchain/txpool.d.ts +2 -1
- package/dist/blockchain/txpool.js +5 -5
- package/dist/executor.d.ts +8 -3
- package/dist/executor.js +20 -12
- package/dist/executor.test.js +0 -12
- package/dist/genesis-provider.js +8 -1
- package/dist/index.d.ts +0 -3
- package/dist/index.js +17 -29
- package/dist/logger.d.ts +3 -1
- package/dist/logger.js +5 -1
- package/dist/rpc/index.js +0 -2
- package/dist/rpc/shared.d.ts +0 -2
- package/dist/schema/index.d.ts +2 -5
- package/dist/schema/index.js +0 -1
- package/dist/utils/set-storage.js +1 -1
- package/package.json +5 -6
- package/dist/rpc/exec.d.ts +0 -3
- package/dist/rpc/exec.js +0 -44
- package/dist/task.d.ts +0 -38
- package/dist/task.js +0 -67
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/dist/bindings.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.setupBindings = void 0;
|
|
4
|
+
const logger_1 = require("./logger");
|
|
5
|
+
const logger = logger_1.defaultLogger.child({ name: 'binding' });
|
|
6
|
+
const setupBindings = (chain) => {
|
|
7
|
+
global._chopsticks_binding_ = {
|
|
8
|
+
getStorage: async function (blockHash, key) {
|
|
9
|
+
const block = await chain.getBlock(blockHash);
|
|
10
|
+
if (!block)
|
|
11
|
+
throw Error(`Block not found ${blockHash}`);
|
|
12
|
+
const value = await block.get(key);
|
|
13
|
+
logger.trace({ blockHash, key, value: value && (0, logger_1.truncate)(value) }, 'exec_storageGet');
|
|
14
|
+
return value;
|
|
15
|
+
},
|
|
16
|
+
getPrefixKeys: async function (blockHash, key) {
|
|
17
|
+
const block = await chain.getBlock(blockHash);
|
|
18
|
+
if (!block)
|
|
19
|
+
throw Error(`Block not found ${blockHash}`);
|
|
20
|
+
return block.getKeysPaged({ prefix: key, pageSize: 1000, startKey: key });
|
|
21
|
+
},
|
|
22
|
+
getNextKey: async function (blockHash, key) {
|
|
23
|
+
const block = await chain.getBlock(blockHash);
|
|
24
|
+
if (!block)
|
|
25
|
+
throw Error(`Block not found ${blockHash}`);
|
|
26
|
+
const keys = await block.getKeysPaged({ prefix: key, pageSize: 1, startKey: key });
|
|
27
|
+
return keys.length > 0 ? keys[0] : '0x';
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
exports.setupBindings = setupBindings;
|
|
@@ -4,19 +4,22 @@ import { TypeRegistry } from '@polkadot/types';
|
|
|
4
4
|
import type { HexString } from '@polkadot/util/types';
|
|
5
5
|
import { Blockchain } from '.';
|
|
6
6
|
import { StorageLayer, StorageLayerProvider } from './storage-layer';
|
|
7
|
-
import { TaskResponseCall } from '../task';
|
|
8
7
|
import type { RuntimeVersion } from '../executor';
|
|
8
|
+
export type TaskCallResponse = {
|
|
9
|
+
result: HexString;
|
|
10
|
+
storageDiff: [HexString, HexString | null][];
|
|
11
|
+
};
|
|
9
12
|
export declare class Block {
|
|
10
13
|
#private;
|
|
11
14
|
readonly number: number;
|
|
12
15
|
readonly hash: string;
|
|
13
16
|
constructor(chain: Blockchain, number: number, hash: string, parentBlock?: Block, block?: {
|
|
14
17
|
header: Header;
|
|
15
|
-
extrinsics:
|
|
18
|
+
extrinsics: HexString[];
|
|
16
19
|
storage?: StorageLayerProvider;
|
|
17
20
|
});
|
|
18
21
|
get header(): Header | Promise<Header>;
|
|
19
|
-
get extrinsics():
|
|
22
|
+
get extrinsics(): HexString[] | Promise<HexString[]>;
|
|
20
23
|
get parentBlock(): undefined | Block | Promise<Block | undefined>;
|
|
21
24
|
get storage(): StorageLayerProvider;
|
|
22
25
|
get(key: string): Promise<string | undefined>;
|
|
@@ -33,7 +36,6 @@ export declare class Block {
|
|
|
33
36
|
get registry(): Promise<TypeRegistry>;
|
|
34
37
|
get runtimeVersion(): Promise<RuntimeVersion>;
|
|
35
38
|
get metadata(): Promise<HexString>;
|
|
36
|
-
withAvoidTasks<T>(fn: () => Promise<T>): Promise<T>;
|
|
37
39
|
get meta(): Promise<DecoratedMeta>;
|
|
38
|
-
call(method: string, args:
|
|
40
|
+
call(method: string, args: HexString, storage?: [HexString, HexString | null][]): Promise<TaskCallResponse>;
|
|
39
41
|
}
|
package/dist/blockchain/block.js
CHANGED
|
@@ -6,7 +6,6 @@ const metadata_1 = require("@polkadot/types/metadata");
|
|
|
6
6
|
const util_1 = require("@polkadot/types-known/util");
|
|
7
7
|
const util_2 = require("@polkadot/util");
|
|
8
8
|
const storage_layer_1 = require("./storage-layer");
|
|
9
|
-
const shared_1 = require("../rpc/shared");
|
|
10
9
|
const utils_1 = require("../utils");
|
|
11
10
|
const executor_1 = require("../executor");
|
|
12
11
|
class Block {
|
|
@@ -23,7 +22,6 @@ class Block {
|
|
|
23
22
|
#meta;
|
|
24
23
|
#baseStorage;
|
|
25
24
|
#storages;
|
|
26
|
-
#avoidTasks = false;
|
|
27
25
|
constructor(chain, number, hash, parentBlock, block) {
|
|
28
26
|
this.number = number;
|
|
29
27
|
this.hash = hash;
|
|
@@ -140,51 +138,20 @@ class Block {
|
|
|
140
138
|
}
|
|
141
139
|
get metadata() {
|
|
142
140
|
if (!this.#metadata) {
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
blockHash: this.hash,
|
|
152
|
-
wasm,
|
|
153
|
-
calls: [['Metadata_metadata', '0x']],
|
|
154
|
-
},
|
|
155
|
-
}, (r) => {
|
|
156
|
-
if ('Call' in r) {
|
|
157
|
-
resolve((0, utils_1.compactHex)((0, util_2.hexToU8a)(r.Call.result)));
|
|
158
|
-
}
|
|
159
|
-
else if ('Error' in r) {
|
|
160
|
-
reject(new shared_1.ResponseError(1, r.Error));
|
|
161
|
-
}
|
|
162
|
-
else {
|
|
163
|
-
reject(new shared_1.ResponseError(1, 'Unexpected response'));
|
|
164
|
-
}
|
|
165
|
-
});
|
|
166
|
-
});
|
|
167
|
-
return res;
|
|
141
|
+
this.#metadata = this.wasm.then(async (wasm) => {
|
|
142
|
+
const response = await (0, executor_1.runTask)({
|
|
143
|
+
blockHash: this.hash,
|
|
144
|
+
wasm,
|
|
145
|
+
calls: [['Metadata_metadata', '0x']],
|
|
146
|
+
storage: [],
|
|
147
|
+
mockSignatureHost: this.#chain.mockSignatureHost,
|
|
148
|
+
allowUnresolvedImports: this.#chain.allowUnresolvedImports,
|
|
168
149
|
});
|
|
169
|
-
|
|
150
|
+
return (0, utils_1.compactHex)((0, util_2.hexToU8a)(response.Call.result));
|
|
151
|
+
});
|
|
170
152
|
}
|
|
171
153
|
return this.#metadata;
|
|
172
154
|
}
|
|
173
|
-
// TODO: avoid this hack
|
|
174
|
-
// we cannot use this.#chain.tasks during initialization phase
|
|
175
|
-
// but we want to use it after initialization
|
|
176
|
-
async withAvoidTasks(fn) {
|
|
177
|
-
const old = this.#avoidTasks;
|
|
178
|
-
this.#avoidTasks = true;
|
|
179
|
-
try {
|
|
180
|
-
return await fn();
|
|
181
|
-
}
|
|
182
|
-
finally {
|
|
183
|
-
this.#avoidTasks = old;
|
|
184
|
-
this.#meta = undefined;
|
|
185
|
-
this.#metadata = undefined;
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
155
|
get meta() {
|
|
189
156
|
if (!this.#meta) {
|
|
190
157
|
this.#meta = Promise.all([this.registry, this.metadata]).then(([registry, metadataStr]) => {
|
|
@@ -194,28 +161,21 @@ class Block {
|
|
|
194
161
|
}
|
|
195
162
|
return this.#meta;
|
|
196
163
|
}
|
|
197
|
-
async call(method, args) {
|
|
164
|
+
async call(method, args, storage = []) {
|
|
198
165
|
const wasm = await this.wasm;
|
|
199
|
-
const
|
|
200
|
-
this
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
}, (r) => {
|
|
207
|
-
if ('Call' in r) {
|
|
208
|
-
resolve(r.Call);
|
|
209
|
-
}
|
|
210
|
-
else if ('Error' in r) {
|
|
211
|
-
reject(new shared_1.ResponseError(1, r.Error));
|
|
212
|
-
}
|
|
213
|
-
else {
|
|
214
|
-
reject(new shared_1.ResponseError(1, 'Unexpected response'));
|
|
215
|
-
}
|
|
216
|
-
});
|
|
166
|
+
const response = await (0, executor_1.runTask)({
|
|
167
|
+
blockHash: this.hash,
|
|
168
|
+
wasm,
|
|
169
|
+
calls: [[method, args]],
|
|
170
|
+
storage,
|
|
171
|
+
mockSignatureHost: this.#chain.mockSignatureHost,
|
|
172
|
+
allowUnresolvedImports: this.#chain.allowUnresolvedImports,
|
|
217
173
|
});
|
|
218
|
-
|
|
174
|
+
if (response.Call)
|
|
175
|
+
return response.Call;
|
|
176
|
+
if (response.Error)
|
|
177
|
+
throw Error(response.Error);
|
|
178
|
+
throw Error('Unexpected response');
|
|
219
179
|
}
|
|
220
180
|
}
|
|
221
181
|
exports.Block = Block;
|
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
import { DataSource } from 'typeorm';
|
|
2
2
|
import { Header } from '@polkadot/types/interfaces';
|
|
3
|
+
import { HexString } from '@polkadot/util/types';
|
|
3
4
|
import { Api } from '../api';
|
|
4
5
|
import { Block } from './block';
|
|
5
6
|
import { BuildBlockMode } from './txpool';
|
|
6
7
|
import { HeadState } from './head-state';
|
|
7
8
|
import { InherentProvider } from './inherents';
|
|
8
|
-
import { TaskManager } from '../task';
|
|
9
9
|
export interface Options {
|
|
10
10
|
api: Api;
|
|
11
|
-
tasks: TaskManager;
|
|
12
11
|
buildBlockMode?: BuildBlockMode;
|
|
13
12
|
inherentProvider: InherentProvider;
|
|
14
13
|
db?: DataSource;
|
|
@@ -16,20 +15,23 @@ export interface Options {
|
|
|
16
15
|
number: number;
|
|
17
16
|
hash: string;
|
|
18
17
|
};
|
|
18
|
+
mockSignatureHost?: boolean;
|
|
19
|
+
allowUnresolvedImports?: boolean;
|
|
19
20
|
}
|
|
20
21
|
export declare class Blockchain {
|
|
21
22
|
#private;
|
|
22
23
|
readonly api: Api;
|
|
23
|
-
readonly tasks: TaskManager;
|
|
24
24
|
readonly db: DataSource | undefined;
|
|
25
|
+
readonly mockSignatureHost: boolean;
|
|
26
|
+
readonly allowUnresolvedImports: boolean;
|
|
25
27
|
readonly headState: HeadState;
|
|
26
|
-
constructor({ api,
|
|
28
|
+
constructor({ api, buildBlockMode, inherentProvider, db, header, mockSignatureHost, allowUnresolvedImports, }: Options);
|
|
27
29
|
get head(): Block;
|
|
28
30
|
getBlockAt(number?: number): Promise<Block | undefined>;
|
|
29
31
|
getBlock(hash?: string): Promise<Block | undefined>;
|
|
30
32
|
newTempBlock(parent: Block, header: Header): Block;
|
|
31
33
|
unregisterBlock(block: Block): void;
|
|
32
34
|
setHead(block: Block): void;
|
|
33
|
-
submitExtrinsic(extrinsic:
|
|
35
|
+
submitExtrinsic(extrinsic: HexString): Promise<HexString>;
|
|
34
36
|
newBlock(): Promise<Block>;
|
|
35
37
|
}
|
package/dist/blockchain/index.js
CHANGED
|
@@ -8,22 +8,26 @@ const txpool_1 = require("./txpool");
|
|
|
8
8
|
const head_state_1 = require("./head-state");
|
|
9
9
|
const shared_1 = require("../rpc/shared");
|
|
10
10
|
const logger_1 = require("../logger");
|
|
11
|
+
const bindings_1 = require("../bindings");
|
|
11
12
|
const logger = logger_1.defaultLogger.child({ name: 'blockchain' });
|
|
12
13
|
class Blockchain {
|
|
13
14
|
api;
|
|
14
|
-
tasks;
|
|
15
15
|
db;
|
|
16
|
+
mockSignatureHost;
|
|
17
|
+
allowUnresolvedImports;
|
|
16
18
|
#txpool;
|
|
17
19
|
#head;
|
|
18
20
|
#blocksByNumber = [];
|
|
19
21
|
#blocksByHash = {};
|
|
20
22
|
headState;
|
|
21
|
-
constructor({ api,
|
|
23
|
+
constructor({ api, buildBlockMode, inherentProvider, db, header, mockSignatureHost = false, allowUnresolvedImports = false, }) {
|
|
22
24
|
this.api = api;
|
|
23
|
-
this.tasks = tasks;
|
|
24
25
|
this.db = db;
|
|
26
|
+
this.mockSignatureHost = mockSignatureHost;
|
|
27
|
+
this.allowUnresolvedImports = allowUnresolvedImports;
|
|
25
28
|
this.#head = new block_1.Block(this, header.number, header.hash);
|
|
26
29
|
this.#registerBlock(this.#head);
|
|
30
|
+
(0, bindings_1.setupBindings)(this);
|
|
27
31
|
this.#txpool = new txpool_1.TxPool(this, inherentProvider, buildBlockMode);
|
|
28
32
|
this.headState = new head_state_1.HeadState(this.#head);
|
|
29
33
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { Block } from './block';
|
|
2
2
|
import { DecoratedMeta } from '@polkadot/types/metadata/decorate/types';
|
|
3
|
-
import {
|
|
3
|
+
import { HexString } from '@polkadot/util/types';
|
|
4
4
|
export interface CreateInherents {
|
|
5
|
-
createInherents(meta: DecoratedMeta, timestamp: number, parent: Block): Promise<
|
|
5
|
+
createInherents(meta: DecoratedMeta, timestamp: number, parent: Block): Promise<HexString[]>;
|
|
6
6
|
}
|
|
7
7
|
export interface InherentProvider extends CreateInherents {
|
|
8
8
|
getTimestamp(blockNumber: number): number;
|
|
@@ -10,17 +10,15 @@ export interface InherentProvider extends CreateInherents {
|
|
|
10
10
|
export declare class SetTimestamp implements InherentProvider {
|
|
11
11
|
#private;
|
|
12
12
|
constructor(getTimestamp?: (blockNumber: number) => number);
|
|
13
|
-
createInherents(meta: DecoratedMeta, timestamp: number, _parent: Block): Promise<
|
|
13
|
+
createInherents(meta: DecoratedMeta, timestamp: number, _parent: Block): Promise<HexString[]>;
|
|
14
14
|
getTimestamp(blockNumber: number): number;
|
|
15
15
|
}
|
|
16
16
|
export declare class InherentProviders implements InherentProvider {
|
|
17
17
|
#private;
|
|
18
18
|
constructor(base: InherentProvider, providers: CreateInherents[]);
|
|
19
|
-
createInherents(meta: DecoratedMeta, timestamp: number, parent: Block): Promise<
|
|
19
|
+
createInherents(meta: DecoratedMeta, timestamp: number, parent: Block): Promise<HexString[]>;
|
|
20
20
|
getTimestamp(blockNumber: number): number;
|
|
21
21
|
}
|
|
22
22
|
export declare class SetValidationData implements CreateInherents {
|
|
23
|
-
|
|
24
|
-
constructor(tasks: TaskManager, expectedIndex: number);
|
|
25
|
-
createInherents(meta: DecoratedMeta, _timestamp: number, parent: Block): Promise<string[]>;
|
|
23
|
+
createInherents(meta: DecoratedMeta, _timestamp: number, parent: Block): Promise<HexString[]>;
|
|
26
24
|
}
|
|
@@ -61,17 +61,10 @@ class InherentProviders {
|
|
|
61
61
|
}
|
|
62
62
|
exports.InherentProviders = InherentProviders;
|
|
63
63
|
class SetValidationData {
|
|
64
|
-
#tasks;
|
|
65
|
-
#expectedIndex;
|
|
66
|
-
constructor(tasks, expectedIndex) {
|
|
67
|
-
this.#tasks = tasks;
|
|
68
|
-
this.#expectedIndex = expectedIndex;
|
|
69
|
-
}
|
|
70
64
|
async createInherents(meta, _timestamp, parent) {
|
|
71
65
|
if (!meta.tx.parachainSystem?.setValidationData) {
|
|
72
66
|
return [];
|
|
73
67
|
}
|
|
74
|
-
void this.#tasks; // TODO
|
|
75
68
|
const parentBlock = await parent.parentBlock;
|
|
76
69
|
if (!parentBlock) {
|
|
77
70
|
throw new Error('Parent block not found');
|
|
@@ -83,8 +76,16 @@ class SetValidationData {
|
|
|
83
76
|
newData = MOCK_VALIDATION_DATA;
|
|
84
77
|
}
|
|
85
78
|
else {
|
|
86
|
-
const
|
|
87
|
-
|
|
79
|
+
const validationDataExtrinsic = extrinsics.find((extrinsic) => {
|
|
80
|
+
const firstArg = meta.registry.createType('GenericExtrinsic', extrinsic)?.args?.[0];
|
|
81
|
+
return firstArg && 'validationData' in firstArg;
|
|
82
|
+
});
|
|
83
|
+
if (!validationDataExtrinsic) {
|
|
84
|
+
throw new Error('Missing validation data from block');
|
|
85
|
+
}
|
|
86
|
+
const extrinsic = meta.registry
|
|
87
|
+
.createType('GenericExtrinsic', validationDataExtrinsic)
|
|
88
|
+
.args[0].toJSON();
|
|
88
89
|
const newEntries = [];
|
|
89
90
|
const upgrade = await parentBlock.get((0, utils_1.compactHex)(meta.query.parachainSystem.pendingValidationCode()));
|
|
90
91
|
if (upgrade) {
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { HexString } from '@polkadot/util/types';
|
|
1
2
|
import { Blockchain } from '.';
|
|
2
3
|
import { InherentProvider } from './inherents';
|
|
3
4
|
export declare enum BuildBlockMode {
|
|
@@ -8,6 +9,6 @@ export declare enum BuildBlockMode {
|
|
|
8
9
|
export declare class TxPool {
|
|
9
10
|
#private;
|
|
10
11
|
constructor(chain: Blockchain, inherentProvider: InherentProvider, mode?: BuildBlockMode);
|
|
11
|
-
submitExtrinsic(extrinsic:
|
|
12
|
+
submitExtrinsic(extrinsic: HexString): void;
|
|
12
13
|
buildBlock(): Promise<void>;
|
|
13
14
|
}
|
|
@@ -95,7 +95,7 @@ class TxPool {
|
|
|
95
95
|
expectedSlot,
|
|
96
96
|
}, 'Building block');
|
|
97
97
|
const resp = await newBlock.call('Core_initialize_block', header.toHex());
|
|
98
|
-
logger.trace(resp.storageDiff, 'Initialize block');
|
|
98
|
+
logger.trace((0, logger_1.truncateStorageDiff)(resp.storageDiff), 'Initialize block');
|
|
99
99
|
newBlock.pushStorageLayer().setAll(resp.storageDiff);
|
|
100
100
|
if (meta.query.babe?.currentSlot) {
|
|
101
101
|
// TODO: figure out how to generate a valid babe slot digest instead of just modify the data
|
|
@@ -108,7 +108,7 @@ class TxPool {
|
|
|
108
108
|
try {
|
|
109
109
|
const resp = await newBlock.call('BlockBuilder_apply_extrinsic', extrinsic);
|
|
110
110
|
newBlock.pushStorageLayer().setAll(resp.storageDiff);
|
|
111
|
-
logger.trace(resp.storageDiff, 'Applied inherent');
|
|
111
|
+
logger.trace((0, logger_1.truncateStorageDiff)(resp.storageDiff), 'Applied inherent');
|
|
112
112
|
}
|
|
113
113
|
catch (e) {
|
|
114
114
|
logger.warn('Failed to apply inherents %o %s', e, e);
|
|
@@ -119,7 +119,7 @@ class TxPool {
|
|
|
119
119
|
try {
|
|
120
120
|
const resp = await newBlock.call('BlockBuilder_apply_extrinsic', extrinsic);
|
|
121
121
|
newBlock.pushStorageLayer().setAll(resp.storageDiff);
|
|
122
|
-
logger.trace(resp.storageDiff, 'Applied extrinsic');
|
|
122
|
+
logger.trace((0, logger_1.truncateStorageDiff)(resp.storageDiff), 'Applied extrinsic');
|
|
123
123
|
}
|
|
124
124
|
catch (e) {
|
|
125
125
|
logger.info('Failed to apply extrinsic %o %s', e, e);
|
|
@@ -135,7 +135,7 @@ class TxPool {
|
|
|
135
135
|
}
|
|
136
136
|
const resp2 = await newBlock.call('BlockBuilder_finalize_block', '0x');
|
|
137
137
|
newBlock.pushStorageLayer().setAll(resp2.storageDiff);
|
|
138
|
-
logger.trace(resp2.storageDiff, 'Finalize block');
|
|
138
|
+
logger.trace((0, logger_1.truncateStorageDiff)(resp2.storageDiff), 'Finalize block');
|
|
139
139
|
const blockData = registry.createType('Block', {
|
|
140
140
|
header,
|
|
141
141
|
extrinsics,
|
|
@@ -146,7 +146,7 @@ class TxPool {
|
|
|
146
146
|
storage: head.storage,
|
|
147
147
|
});
|
|
148
148
|
const diff = await newBlock.storageDiff();
|
|
149
|
-
logger.trace(diff, 'Final block');
|
|
149
|
+
logger.trace(Object.entries(diff).map(([key, value]) => [key, (0, logger_1.truncate)(value)]), 'Final block');
|
|
150
150
|
finalBlock.pushStorageLayer().setAll(diff);
|
|
151
151
|
this.#chain.unregisterBlock(newBlock);
|
|
152
152
|
this.#chain.setHead(finalBlock);
|
package/dist/executor.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { HexString } from '@polkadot/util/types';
|
|
2
|
-
import { run_task } from '../executor/pkg';
|
|
3
2
|
export type RuntimeVersion = {
|
|
4
3
|
specName: string;
|
|
5
4
|
implName: string;
|
|
@@ -11,11 +10,17 @@ export type RuntimeVersion = {
|
|
|
11
10
|
stateVersion: number;
|
|
12
11
|
};
|
|
13
12
|
export declare const getRuntimeVersion: (code: HexString) => Promise<RuntimeVersion>;
|
|
14
|
-
export declare const getMetadata: (code: HexString) => Promise<HexString>;
|
|
15
13
|
export declare const calculateStateRoot: (entries: [HexString, HexString][]) => Promise<HexString>;
|
|
16
14
|
export declare const decodeProof: (trieRootHash: HexString, keys: HexString[], nodes: HexString[]) => Promise<Record<`0x${string}`, `0x${string}` | null>>;
|
|
17
15
|
export declare const createProof: (trieRootHash: HexString, nodes: HexString[], entries: [HexString, HexString][]) => Promise<{
|
|
18
16
|
trieRootHash: `0x${string}`;
|
|
19
17
|
nodes: `0x${string}`[];
|
|
20
18
|
}>;
|
|
21
|
-
export
|
|
19
|
+
export declare const runTask: (task: {
|
|
20
|
+
blockHash: HexString;
|
|
21
|
+
wasm: HexString;
|
|
22
|
+
calls: [string, HexString][];
|
|
23
|
+
storage: [HexString, HexString | null][];
|
|
24
|
+
mockSignatureHost: boolean;
|
|
25
|
+
allowUnresolvedImports: boolean;
|
|
26
|
+
}) => Promise<any>;
|
package/dist/executor.js
CHANGED
|
@@ -1,26 +1,22 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.runTask = exports.createProof = exports.decodeProof = exports.calculateStateRoot = exports.
|
|
3
|
+
exports.runTask = exports.createProof = exports.decodeProof = exports.calculateStateRoot = exports.getRuntimeVersion = void 0;
|
|
4
4
|
const ws_1 = require("ws");
|
|
5
5
|
const util_1 = require("@polkadot/util");
|
|
6
6
|
global.WebSocket = ws_1.WebSocket;
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
const
|
|
7
|
+
const chopsticks_executor_1 = require("@acala-network/chopsticks-executor");
|
|
8
|
+
const logger_1 = require("./logger");
|
|
9
|
+
const logger = logger_1.defaultLogger.child({ name: 'executor' });
|
|
10
10
|
const getRuntimeVersion = async (code) => {
|
|
11
|
-
return (0,
|
|
11
|
+
return (0, chopsticks_executor_1.get_runtime_version)(code).then((version) => {
|
|
12
12
|
version.specName = (0, util_1.hexToString)(version.specName);
|
|
13
13
|
version.implName = (0, util_1.hexToString)(version.implName);
|
|
14
14
|
return version;
|
|
15
15
|
});
|
|
16
16
|
};
|
|
17
17
|
exports.getRuntimeVersion = getRuntimeVersion;
|
|
18
|
-
const getMetadata = async (code) => {
|
|
19
|
-
return (0, utils_1.compactHex)((0, util_1.hexToU8a)(await (0, pkg_1.get_metadata)(code)));
|
|
20
|
-
};
|
|
21
|
-
exports.getMetadata = getMetadata;
|
|
22
18
|
const calculateStateRoot = async (entries) => {
|
|
23
|
-
return (0,
|
|
19
|
+
return (0, chopsticks_executor_1.calculate_state_root)(entries);
|
|
24
20
|
};
|
|
25
21
|
exports.calculateStateRoot = calculateStateRoot;
|
|
26
22
|
const nodesAddLength = (nodes) => {
|
|
@@ -28,7 +24,7 @@ const nodesAddLength = (nodes) => {
|
|
|
28
24
|
return (0, util_1.u8aToHex)((0, util_1.u8aConcatStrict)([(0, util_1.compactToU8a)(nodesWithLength.length), (0, util_1.u8aConcat)(...nodesWithLength)]));
|
|
29
25
|
};
|
|
30
26
|
const decodeProof = async (trieRootHash, keys, nodes) => {
|
|
31
|
-
const decoded = await (0,
|
|
27
|
+
const decoded = await (0, chopsticks_executor_1.decode_proof)(trieRootHash, keys, nodesAddLength(nodes));
|
|
32
28
|
return decoded.reduce((accum, [key, value]) => {
|
|
33
29
|
accum[key] = value;
|
|
34
30
|
return accum;
|
|
@@ -36,7 +32,19 @@ const decodeProof = async (trieRootHash, keys, nodes) => {
|
|
|
36
32
|
};
|
|
37
33
|
exports.decodeProof = decodeProof;
|
|
38
34
|
const createProof = async (trieRootHash, nodes, entries) => {
|
|
39
|
-
const result = await (0,
|
|
35
|
+
const result = await (0, chopsticks_executor_1.create_proof)(trieRootHash, nodesAddLength(nodes), entries);
|
|
40
36
|
return { trieRootHash: result[0], nodes: result[1] };
|
|
41
37
|
};
|
|
42
38
|
exports.createProof = createProof;
|
|
39
|
+
const runTask = async (task) => {
|
|
40
|
+
logger.trace({ task: { ...task, wasm: (0, logger_1.truncate)(task.wasm) } }, 'taskRun');
|
|
41
|
+
const response = await (0, chopsticks_executor_1.run_task)(task);
|
|
42
|
+
if (response.Call) {
|
|
43
|
+
logger.trace({ result: (0, logger_1.truncate)(response.Call.result), storageDiff: (0, logger_1.truncateStorageDiff)(response.Call.storageDiff) }, 'taskResponse');
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
logger.trace({ response }, 'taskResponse');
|
|
47
|
+
}
|
|
48
|
+
return response;
|
|
49
|
+
};
|
|
50
|
+
exports.runTask = runTask;
|
package/dist/executor.test.js
CHANGED
|
@@ -3,25 +3,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const metadata_1 = require("@polkadot/types/metadata");
|
|
7
6
|
const types_1 = require("@polkadot/types");
|
|
8
|
-
const decorate_1 = require("@polkadot/types/metadata/decorate");
|
|
9
7
|
const vitest_1 = require("vitest");
|
|
10
8
|
const node_fs_1 = require("node:fs");
|
|
11
9
|
const node_path_1 = __importDefault(require("node:path"));
|
|
12
10
|
const proof_1 = require("./utils/proof");
|
|
13
11
|
const executor_1 = require("./executor");
|
|
14
12
|
(0, vitest_1.describe)('wasm', () => {
|
|
15
|
-
(0, vitest_1.it)('get metadata from wasm runtime', async () => {
|
|
16
|
-
const code = String((0, node_fs_1.readFileSync)(node_path_1.default.join(__dirname, 'runtime.example'))).trim();
|
|
17
|
-
(0, vitest_1.expect)(code.length).toBeGreaterThan(2);
|
|
18
|
-
const registry = new types_1.TypeRegistry();
|
|
19
|
-
const metadata = new metadata_1.Metadata(registry, await (0, executor_1.getMetadata)(code));
|
|
20
|
-
registry.setMetadata(metadata);
|
|
21
|
-
(0, vitest_1.expect)(registry.metadata.pallets.length).toBeGreaterThan(0);
|
|
22
|
-
const storage = (0, decorate_1.decorateStorage)(registry, metadata.asLatest, metadata.version);
|
|
23
|
-
(0, vitest_1.expect)(storage.system).toBeTruthy;
|
|
24
|
-
});
|
|
25
13
|
(0, vitest_1.it)('get runtime version from wasm runtime', async () => {
|
|
26
14
|
const code = String((0, node_fs_1.readFileSync)(node_path_1.default.join(__dirname, 'runtime.example'))).trim();
|
|
27
15
|
(0, vitest_1.expect)(code.length).toBeGreaterThan(2);
|
package/dist/genesis-provider.js
CHANGED
|
@@ -114,7 +114,14 @@ class GenesisProvider {
|
|
|
114
114
|
return this.#genesis.name;
|
|
115
115
|
case 'state_getMetadata': {
|
|
116
116
|
const code = this.#genesis.genesis.raw.top[(0, util_1.stringToHex)(':code')];
|
|
117
|
-
return (0, executor_1.
|
|
117
|
+
return (0, executor_1.runTask)({
|
|
118
|
+
blockHash: this.blockHash,
|
|
119
|
+
wasm: code,
|
|
120
|
+
calls: [['Metadata_metadata', '0x']],
|
|
121
|
+
storage: [],
|
|
122
|
+
mockSignatureHost: false,
|
|
123
|
+
allowUnresolvedImports: true,
|
|
124
|
+
});
|
|
118
125
|
}
|
|
119
126
|
case 'chain_getHeader':
|
|
120
127
|
return this.getHeader();
|
package/dist/index.d.ts
CHANGED
|
@@ -3,19 +3,16 @@ import { ProviderInterface } from '@polkadot/rpc-provider/types';
|
|
|
3
3
|
import { Api } from './api';
|
|
4
4
|
import { Blockchain } from './blockchain';
|
|
5
5
|
import { Config } from './schema';
|
|
6
|
-
import { TaskManager } from './task';
|
|
7
6
|
export declare const setup: (argv: Config) => Promise<{
|
|
8
7
|
chain: Blockchain;
|
|
9
8
|
api: Api;
|
|
10
9
|
ws: ProviderInterface;
|
|
11
|
-
tasks: TaskManager;
|
|
12
10
|
}>;
|
|
13
11
|
export declare const setupWithServer: (argv: Config) => Promise<{
|
|
14
12
|
close: () => Promise<void>;
|
|
15
13
|
chain: Blockchain;
|
|
16
14
|
api: Api;
|
|
17
15
|
ws: ProviderInterface;
|
|
18
|
-
tasks: TaskManager;
|
|
19
16
|
}>;
|
|
20
17
|
export declare const runBlock: (argv: Config) => Promise<void>;
|
|
21
18
|
export declare const decodeKey: (argv: any) => Promise<void>;
|
package/dist/index.js
CHANGED
|
@@ -17,12 +17,12 @@ const txpool_1 = require("./blockchain/txpool");
|
|
|
17
17
|
const schema_1 = require("./schema");
|
|
18
18
|
const genesis_provider_1 = require("./genesis-provider");
|
|
19
19
|
const inherents_1 = require("./blockchain/inherents");
|
|
20
|
-
const task_1 = require("./task");
|
|
21
20
|
const server_1 = require("./server");
|
|
22
21
|
const logger_1 = require("./logger");
|
|
23
22
|
const rpc_1 = require("./rpc");
|
|
24
23
|
const import_storage_1 = require("./utils/import-storage");
|
|
25
24
|
const db_1 = require("./db");
|
|
25
|
+
const executor_1 = require("./executor");
|
|
26
26
|
const setup = async (argv) => {
|
|
27
27
|
let provider;
|
|
28
28
|
if (argv.genesis) {
|
|
@@ -54,17 +54,14 @@ const setup = async (argv) => {
|
|
|
54
54
|
db = await (0, db_1.openDb)(argv.db);
|
|
55
55
|
}
|
|
56
56
|
const header = await api.getHeader(blockHash);
|
|
57
|
-
const port = argv.port || Number(process.env.PORT) || 8000;
|
|
58
|
-
const tasks = new task_1.TaskManager(port, argv['mock-signature-host'], argv['executor-cmd']);
|
|
59
57
|
const blockNumber = +header.number;
|
|
60
58
|
const timestamp = argv.timestamp ?? Date.now();
|
|
61
59
|
const setTimestamp = new inherents_1.SetTimestamp((newBlockNumber) => {
|
|
62
60
|
return timestamp + (newBlockNumber - blockNumber) * 12000; // TODO: make this more flexible
|
|
63
61
|
});
|
|
64
|
-
const inherents = new inherents_1.InherentProviders(setTimestamp, [new inherents_1.SetValidationData(
|
|
62
|
+
const inherents = new inherents_1.InherentProviders(setTimestamp, [new inherents_1.SetValidationData()]);
|
|
65
63
|
const chain = new blockchain_1.Blockchain({
|
|
66
64
|
api,
|
|
67
|
-
tasks,
|
|
68
65
|
buildBlockMode: argv['build-block-mode'],
|
|
69
66
|
inherentProvider: inherents,
|
|
70
67
|
db,
|
|
@@ -73,7 +70,7 @@ const setup = async (argv) => {
|
|
|
73
70
|
number: Number(header.number),
|
|
74
71
|
},
|
|
75
72
|
});
|
|
76
|
-
const context = { chain, api, ws: provider
|
|
73
|
+
const context = { chain, api, ws: provider };
|
|
77
74
|
await (0, import_storage_1.importStorage)(chain, argv['import-storage']);
|
|
78
75
|
await (0, import_storage_1.overrideWasm)(chain, argv['wasm-override']);
|
|
79
76
|
return context;
|
|
@@ -82,8 +79,7 @@ exports.setup = setup;
|
|
|
82
79
|
const setupWithServer = async (argv) => {
|
|
83
80
|
const context = await (0, exports.setup)(argv);
|
|
84
81
|
const port = argv.port || Number(process.env.PORT) || 8000;
|
|
85
|
-
const {
|
|
86
|
-
context.tasks.updateListeningPort(await listeningPort);
|
|
82
|
+
const { close } = (0, server_1.createServer)(port, (0, rpc_1.handler)(context));
|
|
87
83
|
if (argv.genesis) {
|
|
88
84
|
// mine 1st block when starting from genesis to set some mock validation data
|
|
89
85
|
await context.chain.newBlock();
|
|
@@ -105,20 +101,20 @@ const runBlock = async (argv) => {
|
|
|
105
101
|
calls.push(['BlockBuilder_apply_extrinsic', extrinsic]);
|
|
106
102
|
}
|
|
107
103
|
calls.push(['BlockBuilder_finalize_block', '0x']);
|
|
108
|
-
await
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
if (argv['output-path']) {
|
|
116
|
-
(0, node_fs_1.writeFileSync)(argv['output-path'], JSON.stringify(output, null, 2));
|
|
117
|
-
}
|
|
118
|
-
else {
|
|
119
|
-
console.dir(output, { depth: null, colors: false });
|
|
120
|
-
}
|
|
104
|
+
const result = await (0, executor_1.runTask)({
|
|
105
|
+
blockHash: parent,
|
|
106
|
+
wasm,
|
|
107
|
+
calls,
|
|
108
|
+
storage: [],
|
|
109
|
+
mockSignatureHost: false,
|
|
110
|
+
allowUnresolvedImports: false,
|
|
121
111
|
});
|
|
112
|
+
if (argv['output-path']) {
|
|
113
|
+
(0, node_fs_1.writeFileSync)(argv['output-path'], JSON.stringify(result, null, 2));
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
console.dir(result, { depth: null, colors: false });
|
|
117
|
+
}
|
|
122
118
|
await context.close();
|
|
123
119
|
setTimeout(() => process.exit(0), 50);
|
|
124
120
|
};
|
|
@@ -180,10 +176,6 @@ const defaultOptions = {
|
|
|
180
176
|
desc: 'Port to listen on',
|
|
181
177
|
number: true,
|
|
182
178
|
},
|
|
183
|
-
'executor-cmd': {
|
|
184
|
-
desc: 'Command to execute the executor',
|
|
185
|
-
string: true,
|
|
186
|
-
},
|
|
187
179
|
'output-path': {
|
|
188
180
|
desc: 'File path to print output',
|
|
189
181
|
string: true,
|
|
@@ -200,10 +192,6 @@ const defaultOptions = {
|
|
|
200
192
|
desc: 'Port to listen on',
|
|
201
193
|
number: true,
|
|
202
194
|
},
|
|
203
|
-
'executor-cmd': {
|
|
204
|
-
desc: 'Command to execute the executor',
|
|
205
|
-
string: true,
|
|
206
|
-
},
|
|
207
195
|
'build-block-mode': {
|
|
208
196
|
desc: 'Build block mode. Default to Batch',
|
|
209
197
|
enum: [txpool_1.BuildBlockMode.Batch, txpool_1.BuildBlockMode.Manual, txpool_1.BuildBlockMode.Instant],
|
package/dist/logger.d.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
import { HexString } from '@polkadot/util/types';
|
|
1
2
|
export declare const defaultLogger: import("pino").Logger<{
|
|
2
3
|
level: string;
|
|
3
4
|
transport: {
|
|
4
5
|
target: string;
|
|
5
6
|
};
|
|
6
7
|
}>;
|
|
7
|
-
export declare const truncate: (str
|
|
8
|
+
export declare const truncate: (str: string | null) => string | null;
|
|
9
|
+
export declare const truncateStorageDiff: (diff: [HexString, HexString | null][]) => [HexString, string | null][];
|
package/dist/logger.js
CHANGED
|
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.truncate = exports.defaultLogger = void 0;
|
|
6
|
+
exports.truncateStorageDiff = exports.truncate = exports.defaultLogger = void 0;
|
|
7
7
|
const pino_1 = __importDefault(require("pino"));
|
|
8
8
|
exports.defaultLogger = (0, pino_1.default)({
|
|
9
9
|
level: process.env.LOG_LEVEL || 'info',
|
|
@@ -23,3 +23,7 @@ const truncate = (str) => {
|
|
|
23
23
|
}
|
|
24
24
|
};
|
|
25
25
|
exports.truncate = truncate;
|
|
26
|
+
const truncateStorageDiff = (diff) => {
|
|
27
|
+
return diff.map(([key, value]) => [key, (0, exports.truncate)(value)]);
|
|
28
|
+
};
|
|
29
|
+
exports.truncateStorageDiff = truncateStorageDiff;
|
package/dist/rpc/index.js
CHANGED
|
@@ -6,10 +6,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.handler = void 0;
|
|
7
7
|
const shared_1 = require("./shared");
|
|
8
8
|
const dev_1 = __importDefault(require("./dev"));
|
|
9
|
-
const exec_1 = __importDefault(require("./exec"));
|
|
10
9
|
const substrate_1 = __importDefault(require("./substrate"));
|
|
11
10
|
const allHandlers = {
|
|
12
|
-
...exec_1.default,
|
|
13
11
|
...substrate_1.default,
|
|
14
12
|
...dev_1.default,
|
|
15
13
|
};
|
package/dist/rpc/shared.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { ProviderInterface } from '@polkadot/rpc-provider/types';
|
|
2
2
|
import { Api } from '../api';
|
|
3
3
|
import { Blockchain } from '../blockchain';
|
|
4
|
-
import { TaskManager } from '../task';
|
|
5
4
|
export declare const logger: import("pino").default.Logger<{
|
|
6
5
|
level: string;
|
|
7
6
|
transport: {
|
|
@@ -20,7 +19,6 @@ export interface Context {
|
|
|
20
19
|
chain: Blockchain;
|
|
21
20
|
api: Api;
|
|
22
21
|
ws: ProviderInterface;
|
|
23
|
-
tasks: TaskManager;
|
|
24
22
|
}
|
|
25
23
|
export interface SubscriptionManager {
|
|
26
24
|
subscribe: (method: string, subid: string, onCancel?: () => void) => (data: any) => void;
|
package/dist/schema/index.d.ts
CHANGED
|
@@ -65,7 +65,6 @@ export declare const configSchema: z.ZodObject<{
|
|
|
65
65
|
port: z.ZodOptional<z.ZodNumber>;
|
|
66
66
|
endpoint: z.ZodOptional<z.ZodString>;
|
|
67
67
|
block: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodNumber]>>;
|
|
68
|
-
'executor-cmd': z.ZodOptional<z.ZodString>;
|
|
69
68
|
'build-block-mode': z.ZodOptional<z.ZodNativeEnum<typeof BuildBlockMode>>;
|
|
70
69
|
'import-storage': z.ZodOptional<z.ZodAny>;
|
|
71
70
|
'mock-signature-host': z.ZodOptional<z.ZodBoolean>;
|
|
@@ -133,7 +132,6 @@ export declare const configSchema: z.ZodObject<{
|
|
|
133
132
|
}>]>>;
|
|
134
133
|
timestamp: z.ZodOptional<z.ZodNumber>;
|
|
135
134
|
}, "strict", z.ZodTypeAny, {
|
|
136
|
-
port?: number | undefined;
|
|
137
135
|
timestamp?: number | undefined;
|
|
138
136
|
db?: string | undefined;
|
|
139
137
|
genesis?: string | {
|
|
@@ -150,15 +148,14 @@ export declare const configSchema: z.ZodObject<{
|
|
|
150
148
|
};
|
|
151
149
|
};
|
|
152
150
|
} | undefined;
|
|
151
|
+
port?: number | undefined;
|
|
153
152
|
endpoint?: string | undefined;
|
|
154
153
|
block?: string | number | undefined;
|
|
155
|
-
'executor-cmd'?: string | undefined;
|
|
156
154
|
'build-block-mode'?: BuildBlockMode | undefined;
|
|
157
155
|
'import-storage'?: any;
|
|
158
156
|
'mock-signature-host'?: boolean | undefined;
|
|
159
157
|
'wasm-override'?: string | undefined;
|
|
160
158
|
}, {
|
|
161
|
-
port?: number | undefined;
|
|
162
159
|
timestamp?: number | undefined;
|
|
163
160
|
db?: string | undefined;
|
|
164
161
|
genesis?: string | {
|
|
@@ -175,9 +172,9 @@ export declare const configSchema: z.ZodObject<{
|
|
|
175
172
|
};
|
|
176
173
|
};
|
|
177
174
|
} | undefined;
|
|
175
|
+
port?: number | undefined;
|
|
178
176
|
endpoint?: string | undefined;
|
|
179
177
|
block?: string | number | undefined;
|
|
180
|
-
'executor-cmd'?: string | undefined;
|
|
181
178
|
'build-block-mode'?: BuildBlockMode | undefined;
|
|
182
179
|
'import-storage'?: any;
|
|
183
180
|
'mock-signature-host'?: boolean | undefined;
|
package/dist/schema/index.js
CHANGED
|
@@ -18,7 +18,6 @@ exports.configSchema = zod_1.z
|
|
|
18
18
|
port: zod_1.z.number().optional(),
|
|
19
19
|
endpoint: zod_1.z.string().optional(),
|
|
20
20
|
block: zod_1.z.union([zod_1.z.string(), zod_1.z.number()]).optional(),
|
|
21
|
-
'executor-cmd': zod_1.z.string().optional(),
|
|
22
21
|
'build-block-mode': zod_1.z.nativeEnum(txpool_1.BuildBlockMode).optional(),
|
|
23
22
|
'import-storage': zod_1.z.any().optional(),
|
|
24
23
|
'mock-signature-host': zod_1.z.boolean().optional(),
|
|
@@ -49,7 +49,7 @@ const setStorage = async (chain, storage, blockHash) => {
|
|
|
49
49
|
storageItems = storage;
|
|
50
50
|
}
|
|
51
51
|
else {
|
|
52
|
-
storageItems = objectToStorageItems(await block.
|
|
52
|
+
storageItems = objectToStorageItems(await block.meta, storage);
|
|
53
53
|
}
|
|
54
54
|
block.pushStorageLayer().setAll(storageItems);
|
|
55
55
|
return block.hash;
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@acala-network/chopsticks",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.3",
|
|
4
4
|
"main": "./dist/index.js",
|
|
5
5
|
"types": "./dist/index.d.ts",
|
|
6
6
|
"author": "Bryan Chen <xlchen1291@gmail.com>",
|
|
7
|
-
"license": "
|
|
7
|
+
"license": "Apache-2.0",
|
|
8
8
|
"packageManager": "yarn@3.2.4",
|
|
9
9
|
"workspaces": [
|
|
10
10
|
".",
|
|
@@ -17,18 +17,17 @@
|
|
|
17
17
|
"prepare": "husky install",
|
|
18
18
|
"start": "ts-node --transpile-only src/index.ts",
|
|
19
19
|
"build": "rimraf dist && tsc -p tsconfig.prod.json",
|
|
20
|
-
"build-wasm": "
|
|
20
|
+
"build-wasm": "wasm-pack build executor --target nodejs --scope acala-network",
|
|
21
|
+
"build-wasm-logging": "yarn build-wasm --features=logging",
|
|
21
22
|
"check": "cd executor && cargo check --locked",
|
|
22
23
|
"test": "vitest --silent",
|
|
23
|
-
"test:native": "EXECUTOR_CMD='cargo run --manifest-path executor/Cargo.toml --' vitest",
|
|
24
24
|
"test:dev": "LOG_LEVEL=trace vitest --inspect",
|
|
25
25
|
"dev": "LOG_LEVEL=trace ts-node-dev --transpile-only --inspect --notify=false src/index.ts -- dev --config=configs/dev.yml",
|
|
26
|
-
"dev:native": "yarn dev --executor-cmd='cargo run --manifest-path executor/Cargo.toml --'",
|
|
27
26
|
"dev:karura": "ts-node-dev --transpile-only --inspect --notify=false src/index.ts -- dev --config=configs/karura.yml",
|
|
28
27
|
"dev:acala": "ts-node-dev --transpile-only --inspect --notify=false src/index.ts -- dev --config=configs/acala.yml"
|
|
29
28
|
},
|
|
30
29
|
"dependencies": {
|
|
31
|
-
"@acala-network/chopsticks-executor": "0.2.
|
|
30
|
+
"@acala-network/chopsticks-executor": "0.2.3",
|
|
32
31
|
"@polkadot/api": "^9.10.1",
|
|
33
32
|
"@polkadot/rpc-provider": "^9.10.1",
|
|
34
33
|
"@polkadot/types": "^9.10.1",
|
package/dist/rpc/exec.d.ts
DELETED
package/dist/rpc/exec.js
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const shared_1 = require("./shared");
|
|
4
|
-
const logger_1 = require("../logger");
|
|
5
|
-
const logger = logger_1.defaultLogger.child({ name: 'rpc-exec' });
|
|
6
|
-
const handlers = {
|
|
7
|
-
exec_storageGet: async (context, [_taskId, blockHash, key]) => {
|
|
8
|
-
const block = await context.chain.getBlock(blockHash);
|
|
9
|
-
if (!block) {
|
|
10
|
-
throw new shared_1.ResponseError(1, `Block not found ${blockHash}`);
|
|
11
|
-
}
|
|
12
|
-
const value = await block.get(key);
|
|
13
|
-
logger.trace({ blockHash, key, value: (0, logger_1.truncate)(value) }, 'exec_storageGet');
|
|
14
|
-
return value;
|
|
15
|
-
},
|
|
16
|
-
exec_prefixKeys: async (context, [_taskId, blockHash, key]) => {
|
|
17
|
-
const block = await context.chain.getBlock(blockHash);
|
|
18
|
-
if (!block) {
|
|
19
|
-
throw new shared_1.ResponseError(1, `Block not found ${blockHash}`);
|
|
20
|
-
}
|
|
21
|
-
return block.getKeysPaged({ prefix: key, pageSize: 1000, startKey: key });
|
|
22
|
-
},
|
|
23
|
-
exec_nextKey: async (context, [_taskId, blockHash, key]) => {
|
|
24
|
-
const block = await context.chain.getBlock(blockHash);
|
|
25
|
-
if (!block) {
|
|
26
|
-
throw new shared_1.ResponseError(1, `Block not found ${blockHash}`);
|
|
27
|
-
}
|
|
28
|
-
const res = await block.getKeysPaged({ prefix: key, pageSize: 1, startKey: key });
|
|
29
|
-
return res[0] || null;
|
|
30
|
-
},
|
|
31
|
-
exec_getTask: async (context, [taskId]) => {
|
|
32
|
-
logger.trace({ taskId }, 'exec_getTask');
|
|
33
|
-
const task = context.tasks.getTask(Number(taskId));
|
|
34
|
-
if (!task) {
|
|
35
|
-
throw new shared_1.ResponseError(1, `Task not found ${taskId}`);
|
|
36
|
-
}
|
|
37
|
-
return task.task;
|
|
38
|
-
},
|
|
39
|
-
exec_taskResult: async (context, [taskId, resp]) => {
|
|
40
|
-
logger.trace({ taskId }, 'exec_taskResult');
|
|
41
|
-
context.tasks.getTask(Number(taskId))?.callback(resp);
|
|
42
|
-
},
|
|
43
|
-
};
|
|
44
|
-
exports.default = handlers;
|
package/dist/task.d.ts
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
export interface TaskResponseCall {
|
|
2
|
-
Call: {
|
|
3
|
-
result: `0x${string}}`;
|
|
4
|
-
storageDiff: [`0x${string}}`, `0x${string}}` | null][];
|
|
5
|
-
};
|
|
6
|
-
}
|
|
7
|
-
export interface TaskResponseError {
|
|
8
|
-
Error: string;
|
|
9
|
-
}
|
|
10
|
-
export type TaskResponse = TaskResponseCall | TaskResponseError;
|
|
11
|
-
interface TaskCall {
|
|
12
|
-
wasm: string;
|
|
13
|
-
blockHash: string;
|
|
14
|
-
calls: [string, string][];
|
|
15
|
-
mockSignatureHost?: boolean;
|
|
16
|
-
allowUnresolvedImports?: boolean;
|
|
17
|
-
}
|
|
18
|
-
interface TaskCalculateStateRoot {
|
|
19
|
-
entries: [string, string][];
|
|
20
|
-
}
|
|
21
|
-
type Task = {
|
|
22
|
-
Call: TaskCall;
|
|
23
|
-
} | {
|
|
24
|
-
CalculateStateRoot: TaskCalculateStateRoot;
|
|
25
|
-
};
|
|
26
|
-
export declare class TaskManager {
|
|
27
|
-
#private;
|
|
28
|
-
constructor(listeningPort: number, mockSignatureHost?: boolean, executorCmd?: string, allowUnresolvedImports?: boolean);
|
|
29
|
-
updateListeningPort(port: number): void;
|
|
30
|
-
addTask(task: Task, callback?: (res: TaskResponse) => any): number;
|
|
31
|
-
getTask(taskId: number): {
|
|
32
|
-
task: Task;
|
|
33
|
-
callback: (res: TaskResponse) => any;
|
|
34
|
-
};
|
|
35
|
-
runTask(taskId: number): Promise<void>;
|
|
36
|
-
addAndRunTask(task: Task, callback?: (res: TaskResponse) => any): Promise<void>;
|
|
37
|
-
}
|
|
38
|
-
export {};
|
package/dist/task.js
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.TaskManager = void 0;
|
|
4
|
-
const node_child_process_1 = require("node:child_process");
|
|
5
|
-
const logger_1 = require("./logger");
|
|
6
|
-
const executor_1 = require("./executor");
|
|
7
|
-
const logger = logger_1.defaultLogger.child({ name: 'task' });
|
|
8
|
-
class TaskManager {
|
|
9
|
-
#tasks = [];
|
|
10
|
-
#listeningPort;
|
|
11
|
-
#mockSignatureHost;
|
|
12
|
-
#executorCmd;
|
|
13
|
-
#allowUnresolvedImports;
|
|
14
|
-
constructor(listeningPort, mockSignatureHost = false, executorCmd, allowUnresolvedImports = false) {
|
|
15
|
-
this.#listeningPort = listeningPort;
|
|
16
|
-
this.#mockSignatureHost = mockSignatureHost;
|
|
17
|
-
this.#executorCmd = executorCmd;
|
|
18
|
-
this.#allowUnresolvedImports = allowUnresolvedImports;
|
|
19
|
-
if (this.#mockSignatureHost) {
|
|
20
|
-
logger.info('Mock signature host enabled');
|
|
21
|
-
}
|
|
22
|
-
if (this.#allowUnresolvedImports) {
|
|
23
|
-
logger.info('Allow unresolved imports enabled. Wasm may expect more host functions, but will not fail on load');
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
updateListeningPort(port) {
|
|
27
|
-
this.#listeningPort = port;
|
|
28
|
-
}
|
|
29
|
-
addTask(task, callback = () => { }) {
|
|
30
|
-
logger.debug({
|
|
31
|
-
kind: Object.keys(task)[0],
|
|
32
|
-
port: this.#listeningPort,
|
|
33
|
-
}, 'AddTask');
|
|
34
|
-
if ('Call' in task && task.Call.mockSignatureHost === undefined) {
|
|
35
|
-
task.Call.mockSignatureHost = this.#mockSignatureHost;
|
|
36
|
-
}
|
|
37
|
-
if ('Call' in task && task.Call.allowUnresolvedImports === undefined) {
|
|
38
|
-
task.Call.allowUnresolvedImports = this.#allowUnresolvedImports;
|
|
39
|
-
}
|
|
40
|
-
this.#tasks.push({ task, callback });
|
|
41
|
-
return this.#tasks.length - 1;
|
|
42
|
-
}
|
|
43
|
-
getTask(taskId) {
|
|
44
|
-
return this.#tasks[taskId];
|
|
45
|
-
}
|
|
46
|
-
runTask(taskId) {
|
|
47
|
-
if (this.#executorCmd) {
|
|
48
|
-
const cmd = `${this.#executorCmd} --runner-url=ws://localhost:${this.#listeningPort} --task-id=${taskId}`;
|
|
49
|
-
logger.info({ taskId, cmd }, 'RunTask');
|
|
50
|
-
const p = (0, node_child_process_1.spawn)(cmd, { shell: true, stdio: 'inherit' });
|
|
51
|
-
return new Promise((resolve) => {
|
|
52
|
-
p.once('exit', (code) => {
|
|
53
|
-
logger.debug({ taskId, code }, 'RunTask done');
|
|
54
|
-
resolve();
|
|
55
|
-
});
|
|
56
|
-
});
|
|
57
|
-
}
|
|
58
|
-
else {
|
|
59
|
-
return (0, executor_1.runTask)(taskId, `ws://localhost:${this.#listeningPort}`);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
async addAndRunTask(task, callback = () => { }) {
|
|
63
|
-
const taskId = this.addTask(task, callback);
|
|
64
|
-
await this.runTask(taskId);
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
exports.TaskManager = TaskManager;
|