@acala-network/chopsticks-core 0.14.2-1 → 0.15.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.
@@ -19,11 +19,12 @@ _export(exports, {
19
19
  return _index.default;
20
20
  },
21
21
  substrate: function() {
22
- return _index1.default;
22
+ return _index2.default;
23
23
  }
24
24
  });
25
25
  const _index = /*#__PURE__*/ _interop_require_default(require("./dev/index.js"));
26
- const _index1 = /*#__PURE__*/ _interop_require_default(require("./substrate/index.js"));
26
+ const _index1 = /*#__PURE__*/ _interop_require_default(require("./rpc-spec/index.js"));
27
+ const _index2 = /*#__PURE__*/ _interop_require_default(require("./substrate/index.js"));
27
28
  const _shared = require("./shared.js");
28
29
  function _interop_require_default(obj) {
29
30
  return obj && obj.__esModule ? obj : {
@@ -31,6 +32,7 @@ function _interop_require_default(obj) {
31
32
  };
32
33
  }
33
34
  const allHandlers = {
35
+ ..._index2.default,
34
36
  ..._index1.default,
35
37
  ..._index.default,
36
38
  rpc_methods: async ()=>Promise.resolve({
@@ -0,0 +1,79 @@
1
+ import { Handler } from '../shared.js';
2
+ import { HexString } from '@polkadot/util/types';
3
+ /**
4
+ * Start a chainHead follow subscription
5
+ *
6
+ * @param context
7
+ * @param params - [`withRuntime`]
8
+ * @param subscriptionManager
9
+ *
10
+ * @return subscription id
11
+ */
12
+ export declare const chainHead_v1_follow: Handler<[boolean], string>;
13
+ /**
14
+ * Stop a chainHead follow subscription
15
+ *
16
+ * @param context
17
+ * @param params - [`followSubscription`]
18
+ * @param subscriptionManager
19
+ */
20
+ export declare const chainHead_v1_unfollow: Handler<[string], null>;
21
+ /**
22
+ * Retrieve the header for a specific block
23
+ *
24
+ * @param context
25
+ * @param params - [`followSubscription`, `hash`]
26
+ *
27
+ * @return SCALE-encoded header, or null if the block is not found.
28
+ */
29
+ export declare const chainHead_v1_header: Handler<[string, HexString], HexString | null>;
30
+ type OperationStarted = {
31
+ result: 'started';
32
+ operationId: string;
33
+ };
34
+ /**
35
+ * Perform a runtime call for a block
36
+ *
37
+ * @param context
38
+ * @param params - [`followSubscription`, `hash`, `function`, `callParameters`]
39
+ *
40
+ * @return OperationStarted event with operationId to receive the result on the follow subscription
41
+ */
42
+ export declare const chainHead_v1_call: Handler<[string, HexString, string, HexString], OperationStarted>;
43
+ export type StorageStarted = OperationStarted & {
44
+ discardedItems: number;
45
+ };
46
+ export interface StorageItemRequest {
47
+ key: HexString;
48
+ type: 'value' | 'hash' | 'closestDescendantMerkleValue' | 'descendantsValues' | 'descendantsHashes';
49
+ }
50
+ /**
51
+ * Query the storage for a given block
52
+ *
53
+ * @param context
54
+ * @param params - [`followSubscription`, `hash`, `items`, `childTrie`]
55
+ *
56
+ * @return OperationStarted event with operationId to receive the result on the follow subscription
57
+ */
58
+ export declare const chainHead_v1_storage: Handler<[
59
+ string,
60
+ HexString,
61
+ StorageItemRequest[],
62
+ HexString | null
63
+ ], StorageStarted>;
64
+ export type LimitReached = {
65
+ result: 'limitReached';
66
+ };
67
+ /**
68
+ * Retrieve the body of a specific block
69
+ *
70
+ * @param context
71
+ * @param params - [`followSubscription`, `hash`]
72
+ *
73
+ * @return OperationStarted event with operationId to receive the result on the follow subscription
74
+ */
75
+ export declare const chainHead_v1_body: Handler<[string, HexString], OperationStarted | LimitReached>;
76
+ export declare const chainHead_v1_continue: Handler<[string, HexString], null>;
77
+ export declare const chainHead_v1_stopOperation: Handler<[string, HexString], null>;
78
+ export declare const chainHead_v1_unpin: Handler<[string, HexString | HexString[]], null>;
79
+ export {};
@@ -0,0 +1,245 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ function _export(target, all) {
6
+ for(var name in all)Object.defineProperty(target, name, {
7
+ enumerable: true,
8
+ get: all[name]
9
+ });
10
+ }
11
+ _export(exports, {
12
+ chainHead_v1_body: function() {
13
+ return chainHead_v1_body;
14
+ },
15
+ chainHead_v1_call: function() {
16
+ return chainHead_v1_call;
17
+ },
18
+ chainHead_v1_continue: function() {
19
+ return chainHead_v1_continue;
20
+ },
21
+ chainHead_v1_follow: function() {
22
+ return chainHead_v1_follow;
23
+ },
24
+ chainHead_v1_header: function() {
25
+ return chainHead_v1_header;
26
+ },
27
+ chainHead_v1_stopOperation: function() {
28
+ return chainHead_v1_stopOperation;
29
+ },
30
+ chainHead_v1_storage: function() {
31
+ return chainHead_v1_storage;
32
+ },
33
+ chainHead_v1_unfollow: function() {
34
+ return chainHead_v1_unfollow;
35
+ },
36
+ chainHead_v1_unpin: function() {
37
+ return chainHead_v1_unpin;
38
+ }
39
+ });
40
+ const _shared = require("../shared.js");
41
+ const _logger = require("../../logger.js");
42
+ const logger = _logger.defaultLogger.child({
43
+ name: 'rpc-chainHead_v1'
44
+ });
45
+ const callbacks = new Map();
46
+ async function afterResponse(fn) {
47
+ await new Promise((resolve)=>setTimeout(resolve, 0));
48
+ fn();
49
+ }
50
+ const chainHead_v1_follow = async (context, [withRuntime], { subscribe })=>{
51
+ const update = async (block)=>{
52
+ logger.trace({
53
+ hash: block.hash
54
+ }, 'chainHead_v1_follow');
55
+ const getNewRuntime = async ()=>{
56
+ const [runtime, previousRuntime] = await Promise.all([
57
+ block.runtimeVersion,
58
+ block.parentBlock.then((b)=>b?.runtimeVersion)
59
+ ]);
60
+ const hasNewRuntime = runtime.implVersion !== previousRuntime?.implVersion || runtime.specVersion !== previousRuntime.specVersion;
61
+ return hasNewRuntime ? runtime : null;
62
+ };
63
+ const newRuntime = withRuntime ? await getNewRuntime() : null;
64
+ callback({
65
+ event: 'newBlock',
66
+ blockHash: block.hash,
67
+ parentBlockHash: (await block.parentBlock)?.hash,
68
+ newRuntime
69
+ });
70
+ callback({
71
+ event: 'bestBlockChanged',
72
+ bestBlockHash: block.hash
73
+ });
74
+ callback({
75
+ event: 'finalized',
76
+ finalizedBlockHashes: [
77
+ block.hash
78
+ ],
79
+ prunedBlockHashes: []
80
+ });
81
+ };
82
+ const id = context.chain.headState.subscribeHead(update);
83
+ const cleanup = ()=>{
84
+ context.chain.headState.unsubscribeHead(id);
85
+ callbacks.delete(id);
86
+ };
87
+ const callback = subscribe('chainHead_v1_followEvent', id, cleanup);
88
+ callbacks.set(id, callback);
89
+ afterResponse(async ()=>{
90
+ callback({
91
+ event: 'initialized',
92
+ finalizedBlockHashes: [
93
+ context.chain.head.hash
94
+ ],
95
+ finalizedBlockRuntime: withRuntime ? await context.chain.head.runtimeVersion : null
96
+ });
97
+ });
98
+ return id;
99
+ };
100
+ const chainHead_v1_unfollow = async (_, [followSubscription], { unsubscribe })=>{
101
+ unsubscribe(followSubscription);
102
+ return null;
103
+ };
104
+ const chainHead_v1_header = async (context, [followSubscription, hash])=>{
105
+ if (!callbacks.has(followSubscription)) return null;
106
+ const block = await context.chain.getBlock(hash);
107
+ return block ? (await block.header).toHex() : null;
108
+ };
109
+ const operationStarted = (operationId)=>({
110
+ result: 'started',
111
+ operationId
112
+ });
113
+ const randomId = ()=>Math.random().toString(36).substring(2);
114
+ const chainHead_v1_call = async (context, [followSubscription, hash, method, callParameters])=>{
115
+ const operationId = randomId();
116
+ afterResponse(async ()=>{
117
+ const block = await context.chain.getBlock(hash);
118
+ if (!block) {
119
+ callbacks.get(followSubscription)?.({
120
+ event: 'operationError',
121
+ operationId,
122
+ error: `Block ${hash} not found`
123
+ });
124
+ } else {
125
+ try {
126
+ const resp = await block.call(method, [
127
+ callParameters
128
+ ]);
129
+ callbacks.get(followSubscription)?.({
130
+ event: 'operationCallDone',
131
+ operationId,
132
+ output: resp.result
133
+ });
134
+ } catch (ex) {
135
+ callbacks.get(followSubscription)?.({
136
+ event: 'operationError',
137
+ operationId,
138
+ error: ex.message
139
+ });
140
+ }
141
+ }
142
+ });
143
+ return operationStarted(operationId);
144
+ };
145
+ const chainHead_v1_storage = async (context, [followSubscription, hash, items, _childTrie])=>{
146
+ const operationId = randomId();
147
+ afterResponse(async ()=>{
148
+ const block = await context.chain.getBlock(hash);
149
+ if (!block) {
150
+ callbacks.get(followSubscription)?.({
151
+ event: 'operationError',
152
+ operationId,
153
+ error: 'Block not found'
154
+ });
155
+ return;
156
+ }
157
+ const handleStorageItemRequest = async (sir)=>{
158
+ switch(sir.type){
159
+ case 'value':
160
+ {
161
+ const value = await block.get(sir.key);
162
+ if (value) {
163
+ callbacks.get(followSubscription)?.({
164
+ event: 'operationStorageItems',
165
+ operationId,
166
+ items: [
167
+ {
168
+ key: sir.key,
169
+ value
170
+ }
171
+ ]
172
+ });
173
+ }
174
+ break;
175
+ }
176
+ case 'descendantsValues':
177
+ {
178
+ // TODO expose pagination
179
+ const pageSize = 100;
180
+ let startKey = '0x';
181
+ while(startKey){
182
+ const keys = await block.getKeysPaged({
183
+ prefix: sir.key,
184
+ pageSize,
185
+ startKey
186
+ });
187
+ startKey = keys[pageSize - 1] ?? null;
188
+ const items = await Promise.all(keys.map((key)=>block.get(key).then((value)=>({
189
+ key,
190
+ value
191
+ }))));
192
+ callbacks.get(followSubscription)?.({
193
+ event: 'operationStorageItems',
194
+ operationId,
195
+ items
196
+ });
197
+ break;
198
+ }
199
+ break;
200
+ }
201
+ default:
202
+ // TODO
203
+ console.warn(`Storage type not implemented ${sir.type}`);
204
+ }
205
+ };
206
+ await Promise.all(items.map(handleStorageItemRequest));
207
+ callbacks.get(followSubscription)?.({
208
+ event: 'operationStorageDone',
209
+ operationId
210
+ });
211
+ });
212
+ return {
213
+ ...operationStarted(operationId),
214
+ discardedItems: 0
215
+ };
216
+ };
217
+ const limitReached = {
218
+ result: 'limitReached'
219
+ };
220
+ const chainHead_v1_body = async (context, [followSubscription, hash])=>{
221
+ if (!callbacks.has(followSubscription)) return limitReached;
222
+ const block = await context.chain.getBlock(hash);
223
+ if (!block) {
224
+ throw new _shared.ResponseError(-32801, 'Block not found');
225
+ }
226
+ const operationId = randomId();
227
+ afterResponse(async ()=>{
228
+ const body = await block.extrinsics;
229
+ callbacks.get(followSubscription)?.({
230
+ event: 'operationBodyDone',
231
+ operationId,
232
+ value: body
233
+ });
234
+ });
235
+ return operationStarted(operationId);
236
+ };
237
+ const chainHead_v1_continue = async (_context, [_followSubscription, _operationId])=>{
238
+ return null;
239
+ };
240
+ const chainHead_v1_stopOperation = async (_context, [_followSubscription, _operationId])=>{
241
+ return null;
242
+ };
243
+ const chainHead_v1_unpin = async (_context, [_followSubscription, _hashOrHashes])=>{
244
+ return null;
245
+ };
@@ -0,0 +1,6 @@
1
+ import { ChainProperties } from '../../index.js';
2
+ import { Handler } from '../shared.js';
3
+ import { HexString } from '@polkadot/util/types';
4
+ export declare const chainSpec_v1_chainName: Handler<[], string>;
5
+ export declare const chainSpec_v1_genesisHash: Handler<[], HexString>;
6
+ export declare const chainSpec_v1_properties: Handler<[], ChainProperties>;
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ function _export(target, all) {
6
+ for(var name in all)Object.defineProperty(target, name, {
7
+ enumerable: true,
8
+ get: all[name]
9
+ });
10
+ }
11
+ _export(exports, {
12
+ chainSpec_v1_chainName: function() {
13
+ return chainSpec_v1_chainName;
14
+ },
15
+ chainSpec_v1_genesisHash: function() {
16
+ return chainSpec_v1_genesisHash;
17
+ },
18
+ chainSpec_v1_properties: function() {
19
+ return chainSpec_v1_properties;
20
+ }
21
+ });
22
+ const _shared = require("../shared.js");
23
+ const chainSpec_v1_chainName = async (context)=>{
24
+ return context.chain.api.getSystemChain();
25
+ };
26
+ const chainSpec_v1_genesisHash = async (context)=>{
27
+ const genesisHash = await context.chain.api.getBlockHash(0);
28
+ if (genesisHash === null) {
29
+ throw new _shared.ResponseError(1, 'Unexpected null genesis hash');
30
+ }
31
+ return genesisHash;
32
+ };
33
+ const chainSpec_v1_properties = async (context)=>{
34
+ return context.chain.api.getSystemProperties();
35
+ };
@@ -0,0 +1,27 @@
1
+ import * as ChainHeadV1RPC from './chainHead_v1.js';
2
+ import * as ChainSpecV1RPC from './chainSpec_v1.js';
3
+ import * as TransactionV1RPC from './transaction_v1.js';
4
+ export { ChainHeadV1RPC, TransactionV1RPC, ChainSpecV1RPC };
5
+ declare const handlers: {
6
+ chainSpec_v1_chainName: import("../shared.js").Handler<[], string>;
7
+ chainSpec_v1_genesisHash: import("../shared.js").Handler<[], import("@polkadot/util/types").HexString>;
8
+ chainSpec_v1_properties: import("../shared.js").Handler<[], import("../../index.js").ChainProperties>;
9
+ transaction_v1_broadcast: import("../shared.js").Handler<[import("@polkadot/util/types").HexString], string | null>;
10
+ transaction_v1_stop: import("../shared.js").Handler<[string], null>;
11
+ chainHead_v1_follow: import("../shared.js").Handler<[boolean], string>;
12
+ chainHead_v1_unfollow: import("../shared.js").Handler<[string], null>;
13
+ chainHead_v1_header: import("../shared.js").Handler<[string, import("@polkadot/util/types").HexString], import("@polkadot/util/types").HexString | null>;
14
+ chainHead_v1_call: import("../shared.js").Handler<[string, import("@polkadot/util/types").HexString, string, import("@polkadot/util/types").HexString], {
15
+ result: "started";
16
+ operationId: string;
17
+ }>;
18
+ chainHead_v1_storage: import("../shared.js").Handler<[string, import("@polkadot/util/types").HexString, ChainHeadV1RPC.StorageItemRequest[], import("@polkadot/util/types").HexString | null], ChainHeadV1RPC.StorageStarted>;
19
+ chainHead_v1_body: import("../shared.js").Handler<[string, import("@polkadot/util/types").HexString], {
20
+ result: "started";
21
+ operationId: string;
22
+ } | ChainHeadV1RPC.LimitReached>;
23
+ chainHead_v1_continue: import("../shared.js").Handler<[string, import("@polkadot/util/types").HexString], null>;
24
+ chainHead_v1_stopOperation: import("../shared.js").Handler<[string, import("@polkadot/util/types").HexString], null>;
25
+ chainHead_v1_unpin: import("../shared.js").Handler<[string, import("@polkadot/util/types").HexString | import("@polkadot/util/types").HexString[]], null>;
26
+ };
27
+ export default handlers;
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ function _export(target, all) {
6
+ for(var name in all)Object.defineProperty(target, name, {
7
+ enumerable: true,
8
+ get: all[name]
9
+ });
10
+ }
11
+ _export(exports, {
12
+ ChainHeadV1RPC: function() {
13
+ return _chainHead_v1;
14
+ },
15
+ ChainSpecV1RPC: function() {
16
+ return _chainSpec_v1;
17
+ },
18
+ TransactionV1RPC: function() {
19
+ return _transaction_v1;
20
+ },
21
+ default: function() {
22
+ return _default;
23
+ }
24
+ });
25
+ const _chainHead_v1 = /*#__PURE__*/ _interop_require_wildcard(require("./chainHead_v1.js"));
26
+ const _chainSpec_v1 = /*#__PURE__*/ _interop_require_wildcard(require("./chainSpec_v1.js"));
27
+ const _transaction_v1 = /*#__PURE__*/ _interop_require_wildcard(require("./transaction_v1.js"));
28
+ function _getRequireWildcardCache(nodeInterop) {
29
+ if (typeof WeakMap !== "function") return null;
30
+ var cacheBabelInterop = new WeakMap();
31
+ var cacheNodeInterop = new WeakMap();
32
+ return (_getRequireWildcardCache = function(nodeInterop) {
33
+ return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
34
+ })(nodeInterop);
35
+ }
36
+ function _interop_require_wildcard(obj, nodeInterop) {
37
+ if (!nodeInterop && obj && obj.__esModule) {
38
+ return obj;
39
+ }
40
+ if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
41
+ return {
42
+ default: obj
43
+ };
44
+ }
45
+ var cache = _getRequireWildcardCache(nodeInterop);
46
+ if (cache && cache.has(obj)) {
47
+ return cache.get(obj);
48
+ }
49
+ var newObj = {
50
+ __proto__: null
51
+ };
52
+ var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
53
+ for(var key in obj){
54
+ if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
55
+ var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
56
+ if (desc && (desc.get || desc.set)) {
57
+ Object.defineProperty(newObj, key, desc);
58
+ } else {
59
+ newObj[key] = obj[key];
60
+ }
61
+ }
62
+ }
63
+ newObj.default = obj;
64
+ if (cache) {
65
+ cache.set(obj, newObj);
66
+ }
67
+ return newObj;
68
+ }
69
+ const handlers = {
70
+ ..._chainHead_v1,
71
+ ..._transaction_v1,
72
+ ..._chainSpec_v1
73
+ };
74
+ const _default = handlers;
@@ -0,0 +1,16 @@
1
+ import { Handler } from '../shared.js';
2
+ import { HexString } from '@polkadot/util/types';
3
+ /**
4
+ * Submit the extrinsic to the transaction pool
5
+ *
6
+ * @param context
7
+ * @param params - [`extrinsic`]
8
+ *
9
+ * @return operation id
10
+ */
11
+ export declare const transaction_v1_broadcast: Handler<[HexString], string | null>;
12
+ /**
13
+ * Stop broadcasting the transaction to other nodes.
14
+ *
15
+ */
16
+ export declare const transaction_v1_stop: Handler<[string], null>;
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ function _export(target, all) {
6
+ for(var name in all)Object.defineProperty(target, name, {
7
+ enumerable: true,
8
+ get: all[name]
9
+ });
10
+ }
11
+ _export(exports, {
12
+ transaction_v1_broadcast: function() {
13
+ return transaction_v1_broadcast;
14
+ },
15
+ transaction_v1_stop: function() {
16
+ return transaction_v1_stop;
17
+ }
18
+ });
19
+ const _logger = require("../../logger.js");
20
+ const logger = _logger.defaultLogger.child({
21
+ name: 'rpc-transaction_v1'
22
+ });
23
+ const randomId = ()=>Math.random().toString(36).substring(2);
24
+ const transaction_v1_broadcast = async (context, [extrinsic])=>{
25
+ await context.chain.submitExtrinsic(extrinsic).catch((err)=>{
26
+ // As per the spec, the invalid transaction errors should be ignored.
27
+ logger.warn('Submit extrinsic failed', err);
28
+ });
29
+ return randomId();
30
+ };
31
+ const transaction_v1_stop = async (_context, [_operationId])=>{
32
+ // Chopsticks doesn't have any process to broadcast the transaction through P2P
33
+ // so stopping doesn't have any effect.
34
+ return null;
35
+ };
@@ -1,7 +1,9 @@
1
1
  import dev from './dev/index.js';
2
+ import rpcSpec from './rpc-spec/index.js';
2
3
  import substrate from './substrate/index.js';
3
4
  export const allHandlers = {
4
5
  ...substrate,
6
+ ...rpcSpec,
5
7
  ...dev,
6
8
  rpc_methods: async ()=>Promise.resolve({
7
9
  version: 1,
@@ -0,0 +1,79 @@
1
+ import { Handler } from '../shared.js';
2
+ import { HexString } from '@polkadot/util/types';
3
+ /**
4
+ * Start a chainHead follow subscription
5
+ *
6
+ * @param context
7
+ * @param params - [`withRuntime`]
8
+ * @param subscriptionManager
9
+ *
10
+ * @return subscription id
11
+ */
12
+ export declare const chainHead_v1_follow: Handler<[boolean], string>;
13
+ /**
14
+ * Stop a chainHead follow subscription
15
+ *
16
+ * @param context
17
+ * @param params - [`followSubscription`]
18
+ * @param subscriptionManager
19
+ */
20
+ export declare const chainHead_v1_unfollow: Handler<[string], null>;
21
+ /**
22
+ * Retrieve the header for a specific block
23
+ *
24
+ * @param context
25
+ * @param params - [`followSubscription`, `hash`]
26
+ *
27
+ * @return SCALE-encoded header, or null if the block is not found.
28
+ */
29
+ export declare const chainHead_v1_header: Handler<[string, HexString], HexString | null>;
30
+ type OperationStarted = {
31
+ result: 'started';
32
+ operationId: string;
33
+ };
34
+ /**
35
+ * Perform a runtime call for a block
36
+ *
37
+ * @param context
38
+ * @param params - [`followSubscription`, `hash`, `function`, `callParameters`]
39
+ *
40
+ * @return OperationStarted event with operationId to receive the result on the follow subscription
41
+ */
42
+ export declare const chainHead_v1_call: Handler<[string, HexString, string, HexString], OperationStarted>;
43
+ export type StorageStarted = OperationStarted & {
44
+ discardedItems: number;
45
+ };
46
+ export interface StorageItemRequest {
47
+ key: HexString;
48
+ type: 'value' | 'hash' | 'closestDescendantMerkleValue' | 'descendantsValues' | 'descendantsHashes';
49
+ }
50
+ /**
51
+ * Query the storage for a given block
52
+ *
53
+ * @param context
54
+ * @param params - [`followSubscription`, `hash`, `items`, `childTrie`]
55
+ *
56
+ * @return OperationStarted event with operationId to receive the result on the follow subscription
57
+ */
58
+ export declare const chainHead_v1_storage: Handler<[
59
+ string,
60
+ HexString,
61
+ StorageItemRequest[],
62
+ HexString | null
63
+ ], StorageStarted>;
64
+ export type LimitReached = {
65
+ result: 'limitReached';
66
+ };
67
+ /**
68
+ * Retrieve the body of a specific block
69
+ *
70
+ * @param context
71
+ * @param params - [`followSubscription`, `hash`]
72
+ *
73
+ * @return OperationStarted event with operationId to receive the result on the follow subscription
74
+ */
75
+ export declare const chainHead_v1_body: Handler<[string, HexString], OperationStarted | LimitReached>;
76
+ export declare const chainHead_v1_continue: Handler<[string, HexString], null>;
77
+ export declare const chainHead_v1_stopOperation: Handler<[string, HexString], null>;
78
+ export declare const chainHead_v1_unpin: Handler<[string, HexString | HexString[]], null>;
79
+ export {};
@@ -0,0 +1,250 @@
1
+ import { ResponseError } from '../shared.js';
2
+ import { defaultLogger } from '../../logger.js';
3
+ const logger = defaultLogger.child({
4
+ name: 'rpc-chainHead_v1'
5
+ });
6
+ const callbacks = new Map();
7
+ async function afterResponse(fn) {
8
+ await new Promise((resolve)=>setTimeout(resolve, 0));
9
+ fn();
10
+ }
11
+ /**
12
+ * Start a chainHead follow subscription
13
+ *
14
+ * @param context
15
+ * @param params - [`withRuntime`]
16
+ * @param subscriptionManager
17
+ *
18
+ * @return subscription id
19
+ */ export const chainHead_v1_follow = async (context, [withRuntime], { subscribe })=>{
20
+ const update = async (block)=>{
21
+ logger.trace({
22
+ hash: block.hash
23
+ }, 'chainHead_v1_follow');
24
+ const getNewRuntime = async ()=>{
25
+ const [runtime, previousRuntime] = await Promise.all([
26
+ block.runtimeVersion,
27
+ block.parentBlock.then((b)=>b?.runtimeVersion)
28
+ ]);
29
+ const hasNewRuntime = runtime.implVersion !== previousRuntime?.implVersion || runtime.specVersion !== previousRuntime.specVersion;
30
+ return hasNewRuntime ? runtime : null;
31
+ };
32
+ const newRuntime = withRuntime ? await getNewRuntime() : null;
33
+ callback({
34
+ event: 'newBlock',
35
+ blockHash: block.hash,
36
+ parentBlockHash: (await block.parentBlock)?.hash,
37
+ newRuntime
38
+ });
39
+ callback({
40
+ event: 'bestBlockChanged',
41
+ bestBlockHash: block.hash
42
+ });
43
+ callback({
44
+ event: 'finalized',
45
+ finalizedBlockHashes: [
46
+ block.hash
47
+ ],
48
+ prunedBlockHashes: []
49
+ });
50
+ };
51
+ const id = context.chain.headState.subscribeHead(update);
52
+ const cleanup = ()=>{
53
+ context.chain.headState.unsubscribeHead(id);
54
+ callbacks.delete(id);
55
+ };
56
+ const callback = subscribe('chainHead_v1_followEvent', id, cleanup);
57
+ callbacks.set(id, callback);
58
+ afterResponse(async ()=>{
59
+ callback({
60
+ event: 'initialized',
61
+ finalizedBlockHashes: [
62
+ context.chain.head.hash
63
+ ],
64
+ finalizedBlockRuntime: withRuntime ? await context.chain.head.runtimeVersion : null
65
+ });
66
+ });
67
+ return id;
68
+ };
69
+ /**
70
+ * Stop a chainHead follow subscription
71
+ *
72
+ * @param context
73
+ * @param params - [`followSubscription`]
74
+ * @param subscriptionManager
75
+ */ export const chainHead_v1_unfollow = async (_, [followSubscription], { unsubscribe })=>{
76
+ unsubscribe(followSubscription);
77
+ return null;
78
+ };
79
+ /**
80
+ * Retrieve the header for a specific block
81
+ *
82
+ * @param context
83
+ * @param params - [`followSubscription`, `hash`]
84
+ *
85
+ * @return SCALE-encoded header, or null if the block is not found.
86
+ */ export const chainHead_v1_header = async (context, [followSubscription, hash])=>{
87
+ if (!callbacks.has(followSubscription)) return null;
88
+ const block = await context.chain.getBlock(hash);
89
+ return block ? (await block.header).toHex() : null;
90
+ };
91
+ const operationStarted = (operationId)=>({
92
+ result: 'started',
93
+ operationId
94
+ });
95
+ const randomId = ()=>Math.random().toString(36).substring(2);
96
+ /**
97
+ * Perform a runtime call for a block
98
+ *
99
+ * @param context
100
+ * @param params - [`followSubscription`, `hash`, `function`, `callParameters`]
101
+ *
102
+ * @return OperationStarted event with operationId to receive the result on the follow subscription
103
+ */ export const chainHead_v1_call = async (context, [followSubscription, hash, method, callParameters])=>{
104
+ const operationId = randomId();
105
+ afterResponse(async ()=>{
106
+ const block = await context.chain.getBlock(hash);
107
+ if (!block) {
108
+ callbacks.get(followSubscription)?.({
109
+ event: 'operationError',
110
+ operationId,
111
+ error: `Block ${hash} not found`
112
+ });
113
+ } else {
114
+ try {
115
+ const resp = await block.call(method, [
116
+ callParameters
117
+ ]);
118
+ callbacks.get(followSubscription)?.({
119
+ event: 'operationCallDone',
120
+ operationId,
121
+ output: resp.result
122
+ });
123
+ } catch (ex) {
124
+ callbacks.get(followSubscription)?.({
125
+ event: 'operationError',
126
+ operationId,
127
+ error: ex.message
128
+ });
129
+ }
130
+ }
131
+ });
132
+ return operationStarted(operationId);
133
+ };
134
+ /**
135
+ * Query the storage for a given block
136
+ *
137
+ * @param context
138
+ * @param params - [`followSubscription`, `hash`, `items`, `childTrie`]
139
+ *
140
+ * @return OperationStarted event with operationId to receive the result on the follow subscription
141
+ */ export const chainHead_v1_storage = async (context, [followSubscription, hash, items, _childTrie])=>{
142
+ const operationId = randomId();
143
+ afterResponse(async ()=>{
144
+ const block = await context.chain.getBlock(hash);
145
+ if (!block) {
146
+ callbacks.get(followSubscription)?.({
147
+ event: 'operationError',
148
+ operationId,
149
+ error: 'Block not found'
150
+ });
151
+ return;
152
+ }
153
+ const handleStorageItemRequest = async (sir)=>{
154
+ switch(sir.type){
155
+ case 'value':
156
+ {
157
+ const value = await block.get(sir.key);
158
+ if (value) {
159
+ callbacks.get(followSubscription)?.({
160
+ event: 'operationStorageItems',
161
+ operationId,
162
+ items: [
163
+ {
164
+ key: sir.key,
165
+ value
166
+ }
167
+ ]
168
+ });
169
+ }
170
+ break;
171
+ }
172
+ case 'descendantsValues':
173
+ {
174
+ // TODO expose pagination
175
+ const pageSize = 100;
176
+ let startKey = '0x';
177
+ while(startKey){
178
+ const keys = await block.getKeysPaged({
179
+ prefix: sir.key,
180
+ pageSize,
181
+ startKey
182
+ });
183
+ startKey = keys[pageSize - 1] ?? null;
184
+ const items = await Promise.all(keys.map((key)=>block.get(key).then((value)=>({
185
+ key,
186
+ value
187
+ }))));
188
+ callbacks.get(followSubscription)?.({
189
+ event: 'operationStorageItems',
190
+ operationId,
191
+ items
192
+ });
193
+ break;
194
+ }
195
+ break;
196
+ }
197
+ default:
198
+ // TODO
199
+ console.warn(`Storage type not implemented ${sir.type}`);
200
+ }
201
+ };
202
+ await Promise.all(items.map(handleStorageItemRequest));
203
+ callbacks.get(followSubscription)?.({
204
+ event: 'operationStorageDone',
205
+ operationId
206
+ });
207
+ });
208
+ return {
209
+ ...operationStarted(operationId),
210
+ discardedItems: 0
211
+ };
212
+ };
213
+ const limitReached = {
214
+ result: 'limitReached'
215
+ };
216
+ /**
217
+ * Retrieve the body of a specific block
218
+ *
219
+ * @param context
220
+ * @param params - [`followSubscription`, `hash`]
221
+ *
222
+ * @return OperationStarted event with operationId to receive the result on the follow subscription
223
+ */ export const chainHead_v1_body = async (context, [followSubscription, hash])=>{
224
+ if (!callbacks.has(followSubscription)) return limitReached;
225
+ const block = await context.chain.getBlock(hash);
226
+ if (!block) {
227
+ throw new ResponseError(-32801, 'Block not found');
228
+ }
229
+ const operationId = randomId();
230
+ afterResponse(async ()=>{
231
+ const body = await block.extrinsics;
232
+ callbacks.get(followSubscription)?.({
233
+ event: 'operationBodyDone',
234
+ operationId,
235
+ value: body
236
+ });
237
+ });
238
+ return operationStarted(operationId);
239
+ };
240
+ // Currently no-ops, will come into play when pagination is implemented
241
+ export const chainHead_v1_continue = async (_context, [_followSubscription, _operationId])=>{
242
+ return null;
243
+ };
244
+ export const chainHead_v1_stopOperation = async (_context, [_followSubscription, _operationId])=>{
245
+ return null;
246
+ };
247
+ // no-op, since there's no concept of unpinning in chopsticks
248
+ export const chainHead_v1_unpin = async (_context, [_followSubscription, _hashOrHashes])=>{
249
+ return null;
250
+ };
@@ -0,0 +1,6 @@
1
+ import { ChainProperties } from '../../index.js';
2
+ import { Handler } from '../shared.js';
3
+ import { HexString } from '@polkadot/util/types';
4
+ export declare const chainSpec_v1_chainName: Handler<[], string>;
5
+ export declare const chainSpec_v1_genesisHash: Handler<[], HexString>;
6
+ export declare const chainSpec_v1_properties: Handler<[], ChainProperties>;
@@ -0,0 +1,14 @@
1
+ import { ResponseError } from '../shared.js';
2
+ export const chainSpec_v1_chainName = async (context)=>{
3
+ return context.chain.api.getSystemChain();
4
+ };
5
+ export const chainSpec_v1_genesisHash = async (context)=>{
6
+ const genesisHash = await context.chain.api.getBlockHash(0);
7
+ if (genesisHash === null) {
8
+ throw new ResponseError(1, 'Unexpected null genesis hash');
9
+ }
10
+ return genesisHash;
11
+ };
12
+ export const chainSpec_v1_properties = async (context)=>{
13
+ return context.chain.api.getSystemProperties();
14
+ };
@@ -0,0 +1,27 @@
1
+ import * as ChainHeadV1RPC from './chainHead_v1.js';
2
+ import * as ChainSpecV1RPC from './chainSpec_v1.js';
3
+ import * as TransactionV1RPC from './transaction_v1.js';
4
+ export { ChainHeadV1RPC, TransactionV1RPC, ChainSpecV1RPC };
5
+ declare const handlers: {
6
+ chainSpec_v1_chainName: import("../shared.js").Handler<[], string>;
7
+ chainSpec_v1_genesisHash: import("../shared.js").Handler<[], import("@polkadot/util/types").HexString>;
8
+ chainSpec_v1_properties: import("../shared.js").Handler<[], import("../../index.js").ChainProperties>;
9
+ transaction_v1_broadcast: import("../shared.js").Handler<[import("@polkadot/util/types").HexString], string | null>;
10
+ transaction_v1_stop: import("../shared.js").Handler<[string], null>;
11
+ chainHead_v1_follow: import("../shared.js").Handler<[boolean], string>;
12
+ chainHead_v1_unfollow: import("../shared.js").Handler<[string], null>;
13
+ chainHead_v1_header: import("../shared.js").Handler<[string, import("@polkadot/util/types").HexString], import("@polkadot/util/types").HexString | null>;
14
+ chainHead_v1_call: import("../shared.js").Handler<[string, import("@polkadot/util/types").HexString, string, import("@polkadot/util/types").HexString], {
15
+ result: "started";
16
+ operationId: string;
17
+ }>;
18
+ chainHead_v1_storage: import("../shared.js").Handler<[string, import("@polkadot/util/types").HexString, ChainHeadV1RPC.StorageItemRequest[], import("@polkadot/util/types").HexString | null], ChainHeadV1RPC.StorageStarted>;
19
+ chainHead_v1_body: import("../shared.js").Handler<[string, import("@polkadot/util/types").HexString], {
20
+ result: "started";
21
+ operationId: string;
22
+ } | ChainHeadV1RPC.LimitReached>;
23
+ chainHead_v1_continue: import("../shared.js").Handler<[string, import("@polkadot/util/types").HexString], null>;
24
+ chainHead_v1_stopOperation: import("../shared.js").Handler<[string, import("@polkadot/util/types").HexString], null>;
25
+ chainHead_v1_unpin: import("../shared.js").Handler<[string, import("@polkadot/util/types").HexString | import("@polkadot/util/types").HexString[]], null>;
26
+ };
27
+ export default handlers;
@@ -0,0 +1,10 @@
1
+ import * as ChainHeadV1RPC from './chainHead_v1.js';
2
+ import * as ChainSpecV1RPC from './chainSpec_v1.js';
3
+ import * as TransactionV1RPC from './transaction_v1.js';
4
+ export { ChainHeadV1RPC, TransactionV1RPC, ChainSpecV1RPC };
5
+ const handlers = {
6
+ ...ChainHeadV1RPC,
7
+ ...TransactionV1RPC,
8
+ ...ChainSpecV1RPC
9
+ };
10
+ export default handlers;
@@ -0,0 +1,16 @@
1
+ import { Handler } from '../shared.js';
2
+ import { HexString } from '@polkadot/util/types';
3
+ /**
4
+ * Submit the extrinsic to the transaction pool
5
+ *
6
+ * @param context
7
+ * @param params - [`extrinsic`]
8
+ *
9
+ * @return operation id
10
+ */
11
+ export declare const transaction_v1_broadcast: Handler<[HexString], string | null>;
12
+ /**
13
+ * Stop broadcasting the transaction to other nodes.
14
+ *
15
+ */
16
+ export declare const transaction_v1_stop: Handler<[string], null>;
@@ -0,0 +1,27 @@
1
+ import { defaultLogger } from '../../logger.js';
2
+ const logger = defaultLogger.child({
3
+ name: 'rpc-transaction_v1'
4
+ });
5
+ const randomId = ()=>Math.random().toString(36).substring(2);
6
+ /**
7
+ * Submit the extrinsic to the transaction pool
8
+ *
9
+ * @param context
10
+ * @param params - [`extrinsic`]
11
+ *
12
+ * @return operation id
13
+ */ export const transaction_v1_broadcast = async (context, [extrinsic])=>{
14
+ await context.chain.submitExtrinsic(extrinsic).catch((err)=>{
15
+ // As per the spec, the invalid transaction errors should be ignored.
16
+ logger.warn('Submit extrinsic failed', err);
17
+ });
18
+ return randomId();
19
+ };
20
+ /**
21
+ * Stop broadcasting the transaction to other nodes.
22
+ *
23
+ */ export const transaction_v1_stop = async (_context, [_operationId])=>{
24
+ // Chopsticks doesn't have any process to broadcast the transaction through P2P
25
+ // so stopping doesn't have any effect.
26
+ return null;
27
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@acala-network/chopsticks-core",
3
- "version": "0.14.2-1",
3
+ "version": "0.15.0",
4
4
  "author": "Acala Developers <hello@acala.network>",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",
@@ -11,7 +11,7 @@
11
11
  "depcheck": "npx depcheck"
12
12
  },
13
13
  "dependencies": {
14
- "@acala-network/chopsticks-executor": "0.14.2-1",
14
+ "@acala-network/chopsticks-executor": "0.15.0",
15
15
  "@polkadot/rpc-provider": "^12.3.1",
16
16
  "@polkadot/types": "^12.3.1",
17
17
  "@polkadot/types-codec": "^12.3.1",