@polkadot-api/ws-middleware 0.3.2-canary.32abdef → 0.3.2-canary.fa37019
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.
|
@@ -162,7 +162,10 @@ const createChainHead = (upstream, reply, err, notification) => {
|
|
|
162
162
|
if (method === follow) return _follow(rId);
|
|
163
163
|
const [followId, ...rest] = params;
|
|
164
164
|
const ctx = subscriptions.get(followId);
|
|
165
|
-
if (!ctx)
|
|
165
|
+
if (!ctx) {
|
|
166
|
+
console.log("Invalid followSubscription", rId, method, params);
|
|
167
|
+
return err(rId, -32602, "Ivalid followSubscription");
|
|
168
|
+
}
|
|
166
169
|
const innerReply = (value) => {
|
|
167
170
|
reply(rId, value);
|
|
168
171
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"chain-head.js","sources":["../../../src/legacy/downstream/chain-head.ts"],"sourcesContent":["import { noop } from \"@polkadot-api/utils\"\nimport { finalize, Subscription } from \"rxjs\"\nimport { createUpstream } from \"../upstream/upstream\"\nimport { createOpaqueToken } from \"../utils/create-opaque-token\"\nimport { areItemsValid, getStg$ } from \"./storage\"\nimport { getMsgFromErr } from \"../utils/message-from-error\"\nimport { chainHead } from \"../../methods\"\n\nconst { follow, header, storage, body, call, unfollow, stopOperation, unpin } =\n chainHead\n\nexport const createChainHead = (\n upstream: ReturnType<typeof createUpstream>,\n reply: (id: string, result: any) => void,\n err: (id: string, code: number, msg: string) => void,\n notification: (method: string, subscription: string, result: any) => void,\n) => {\n type SubCtx = {\n id: string\n up: ReturnType<\n ReturnType<(typeof upstream)[\"subscribeBlocks\"]>[\"getBlocks\"]\n >\n operations: Map<string, () => void>\n cleanUp: () => void\n }\n const subscriptions = new Map<string, SubCtx>()\n\n const _follow = (rId: string) => {\n if (subscriptions.size === 2) {\n return err(rId, -32800, \"Limit reached\")\n }\n const token = createOpaqueToken()\n const fNotification = (result: any) =>\n notification(\"chainHead_v1_followEvent\", token, result)\n const up = upstream.subscribeBlocks().getBlocks(token)\n const operations = new Map<string, () => void>()\n subscriptions.set(token, {\n id: token,\n up,\n operations,\n cleanUp: () => {\n cleanUp()\n },\n })\n\n reply(rId, token)\n\n let subscription: Subscription | null = null\n let cleanUp = () => {\n cleanUp = noop\n subscription?.unsubscribe()\n subscription = null as any\n operations.forEach((cb) => {\n cb()\n })\n operations.clear()\n subscriptions.delete(token)\n }\n\n subscription = up.blocks$.subscribe({\n next(v) {\n fNotification(v)\n },\n error() {\n cleanUp()\n fNotification({ event: \"stop\" })\n },\n })\n if (subscription.closed) subscription = null\n }\n\n const _unfollow = (rId: string, followId: string) => {\n subscriptions.get(followId)?.cleanUp()\n reply(rId, \"null\")\n }\n\n const _stopOperation = (\n rId: string,\n followId: string,\n operationId: string,\n ) => {\n const cb = subscriptions.get(followId)?.operations.get(operationId)\n if (cb) cb()\n reply(rId, \"null\")\n }\n\n const _header = (\n { up: { getHeader } }: SubCtx,\n reply: (x: any) => void,\n at: string,\n ) => reply(getHeader(at))\n\n const _unpin = (\n { up: { unpin: innerUnpin } }: SubCtx,\n reply: (x: any) => void,\n hashOrHashes: string | string[],\n ) => {\n const hashes =\n typeof hashOrHashes === \"string\" ? [hashOrHashes] : hashOrHashes\n hashes.forEach(innerUnpin)\n reply(null)\n }\n\n const _call = (\n { operations, id: followId }: SubCtx,\n reply: (x: any) => void,\n at: string,\n method: string,\n args: string,\n ) => {\n const operationId = createOpaqueToken()\n reply({ result: \"started\", operationId })\n const cNotification = (output: any, isErr = false) =>\n notification(\n call,\n followId,\n isErr\n ? {\n operationId,\n event: \"operationError\",\n error: output,\n }\n : {\n operationId,\n event: \"operationCallDone\",\n output,\n },\n )\n const subscription = upstream.runtimeCall(at, method, args).subscribe(\n (output) => {\n operations.delete(operationId)\n cNotification(output)\n },\n (e) => {\n operations.delete(operationId)\n cNotification(getMsgFromErr(e), true)\n },\n )\n if (!subscription.closed)\n operations.set(operationId, () => {\n subscription.unsubscribe()\n operations.delete(operationId)\n })\n }\n\n const _body = (\n { operations, id: followId }: SubCtx,\n reply: (x: any) => void,\n at: string,\n ) => {\n const operationId = createOpaqueToken()\n reply({ result: \"started\", operationId })\n const subscription = upstream.getBody(at).subscribe(\n ({ block: { extrinsics: value } }) => {\n operations.delete(operationId)\n notification(body, followId, {\n event: \"operationBodyDone\",\n operationId,\n value,\n })\n },\n (e) => {\n operations.delete(operationId)\n notification(body, followId, {\n event: \"operationError\",\n operationId,\n error: getMsgFromErr(e),\n })\n },\n )\n\n if (!subscription.closed)\n operations.set(operationId, () => {\n subscription.unsubscribe()\n operations.delete(operationId)\n })\n }\n\n const _stg = (\n { operations, id: followId }: SubCtx,\n reply: (x: any) => void,\n at: string,\n items: Array<{\n key: string\n type:\n | \"value\"\n | \"hash\"\n | \"descendantsValues\"\n | \"descendantsHashes\"\n | \"closestDescendantMerkleValue\"\n }>,\n ) => {\n const operationId = createOpaqueToken()\n reply({ result: \"started\", operationId })\n const innerNotifiaction = (msg: any) => {\n notification(storage, followId, msg)\n }\n const subscription = getStg$(upstream, at, items)\n .pipe(\n finalize(() => {\n operations.delete(operationId)\n }),\n )\n .subscribe(\n (items) => {\n innerNotifiaction({\n event: \"operationStorageItems\",\n operationId,\n items,\n })\n },\n (e) => {\n innerNotifiaction({\n event: \"operationError\",\n operationId,\n error: getMsgFromErr(e),\n })\n },\n () => {\n innerNotifiaction({\n event: \"operationStorageDone\",\n operationId,\n })\n },\n )\n\n if (!subscription.closed)\n operations.set(operationId, () => {\n subscription.unsubscribe()\n })\n }\n\n const result = (rId: string, method: string, params: Array<any>) => {\n if (method === follow) return _follow(rId)\n const [followId, ...rest] = params as [string, ...any[]]\n const ctx = subscriptions.get(followId)\n if (!ctx) return err(rId, -32602, \"Ivalid followSubscription\")\n\n const innerReply = (value: any) => {\n reply(rId, value)\n }\n\n switch (method) {\n case unfollow:\n return _unfollow(rId, followId)\n case stopOperation:\n return _stopOperation(rId, followId, rest[0])\n case unpin: {\n const [hashOrHashes] = rest\n if (\n (Array.isArray(hashOrHashes) ? hashOrHashes : [hashOrHashes]).some(\n (hash) => typeof hash !== \"string\",\n )\n )\n return err(rId, -32602, \"Invalid args\")\n return _unpin(ctx, innerReply, hashOrHashes)\n }\n default: {\n const [at, ...other] = rest as [string, ...any[]]\n if (!ctx.up.isPinned(at)) return err(rId, -32801, \"Block not pinned\")\n\n switch (method) {\n case header:\n return _header(ctx, innerReply, at)\n case body:\n return _body(ctx, innerReply, at)\n case call: {\n const [method, data] = other\n if (typeof method !== \"string\" || typeof data !== \"string\")\n return err(rId, -32602, \"Invalid args\")\n return _call(ctx, innerReply, at, method, data)\n }\n case storage: {\n const [items] = other\n return areItemsValid(items)\n ? _stg(ctx, innerReply, at, items)\n : err(rId, -32602, \"Invalid args\")\n }\n }\n }\n }\n throw null\n }\n\n result.stop = () => {\n subscriptions.forEach((x) => {\n x.cleanUp()\n })\n }\n return result\n}\n"],"names":["result","reply","items","method"],"mappings":";;;;;;;AAQA,MAAM,EAAE,QAAQ,MAAA,EAAQ,OAAA,EAAS,MAAM,IAAA,EAAM,QAAA,EAAU,aAAA,EAAe,KAAA,EAAM,GAC1E,SAAA;AAEK,MAAM,eAAA,GAAkB,CAC7B,QAAA,EACA,KAAA,EACA,KACA,YAAA,KACG;AASH,EAAA,MAAM,aAAA,uBAAoB,GAAA,EAAoB;AAE9C,EAAA,MAAM,OAAA,GAAU,CAAC,GAAA,KAAgB;AAC/B,IAAA,IAAI,aAAA,CAAc,SAAS,CAAA,EAAG;AAC5B,MAAA,OAAO,GAAA,CAAI,GAAA,EAAK,MAAA,EAAQ,eAAe,CAAA;AAAA,IACzC;AACA,IAAA,MAAM,QAAQ,iBAAA,EAAkB;AAChC,IAAA,MAAM,gBAAgB,CAACA,OAAAA,KACrB,YAAA,CAAa,0BAAA,EAA4B,OAAOA,OAAM,CAAA;AACxD,IAAA,MAAM,EAAA,GAAK,QAAA,CAAS,eAAA,EAAgB,CAAE,UAAU,KAAK,CAAA;AACrD,IAAA,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EAAQ;AAAA,IACZ,CAAC,CAAA;AAAA,EACH,CAAA;AACA,EAAA,OAAO,MAAA;AACT;;;;"}
|
|
1
|
+
{"version":3,"file":"chain-head.js","sources":["../../../src/legacy/downstream/chain-head.ts"],"sourcesContent":["import { noop } from \"@polkadot-api/utils\"\nimport { finalize, Subscription } from \"rxjs\"\nimport { createUpstream } from \"../upstream/upstream\"\nimport { createOpaqueToken } from \"../utils/create-opaque-token\"\nimport { areItemsValid, getStg$ } from \"./storage\"\nimport { getMsgFromErr } from \"../utils/message-from-error\"\nimport { chainHead } from \"../../methods\"\n\nconst { follow, header, storage, body, call, unfollow, stopOperation, unpin } =\n chainHead\n\nexport const createChainHead = (\n upstream: ReturnType<typeof createUpstream>,\n reply: (id: string, result: any) => void,\n err: (id: string, code: number, msg: string) => void,\n notification: (method: string, subscription: string, result: any) => void,\n) => {\n type SubCtx = {\n id: string\n up: ReturnType<\n ReturnType<(typeof upstream)[\"subscribeBlocks\"]>[\"getBlocks\"]\n >\n operations: Map<string, () => void>\n cleanUp: () => void\n }\n const subscriptions = new Map<string, SubCtx>()\n\n const _follow = (rId: string) => {\n if (subscriptions.size === 2) {\n return err(rId, -32800, \"Limit reached\")\n }\n const token = createOpaqueToken()\n const fNotification = (result: any) =>\n notification(\"chainHead_v1_followEvent\", token, result)\n const up = upstream.subscribeBlocks().getBlocks(token)\n const operations = new Map<string, () => void>()\n subscriptions.set(token, {\n id: token,\n up,\n operations,\n cleanUp: () => {\n cleanUp()\n },\n })\n\n reply(rId, token)\n\n let subscription: Subscription | null = null\n let cleanUp = () => {\n cleanUp = noop\n subscription?.unsubscribe()\n subscription = null as any\n operations.forEach((cb) => {\n cb()\n })\n operations.clear()\n subscriptions.delete(token)\n }\n\n subscription = up.blocks$.subscribe({\n next(v) {\n fNotification(v)\n },\n error() {\n cleanUp()\n fNotification({ event: \"stop\" })\n },\n })\n if (subscription.closed) subscription = null\n }\n\n const _unfollow = (rId: string, followId: string) => {\n subscriptions.get(followId)?.cleanUp()\n reply(rId, \"null\")\n }\n\n const _stopOperation = (\n rId: string,\n followId: string,\n operationId: string,\n ) => {\n const cb = subscriptions.get(followId)?.operations.get(operationId)\n if (cb) cb()\n reply(rId, \"null\")\n }\n\n const _header = (\n { up: { getHeader } }: SubCtx,\n reply: (x: any) => void,\n at: string,\n ) => reply(getHeader(at))\n\n const _unpin = (\n { up: { unpin: innerUnpin } }: SubCtx,\n reply: (x: any) => void,\n hashOrHashes: string | string[],\n ) => {\n const hashes =\n typeof hashOrHashes === \"string\" ? [hashOrHashes] : hashOrHashes\n hashes.forEach(innerUnpin)\n reply(null)\n }\n\n const _call = (\n { operations, id: followId }: SubCtx,\n reply: (x: any) => void,\n at: string,\n method: string,\n args: string,\n ) => {\n const operationId = createOpaqueToken()\n reply({ result: \"started\", operationId })\n const cNotification = (output: any, isErr = false) =>\n notification(\n call,\n followId,\n isErr\n ? {\n operationId,\n event: \"operationError\",\n error: output,\n }\n : {\n operationId,\n event: \"operationCallDone\",\n output,\n },\n )\n const subscription = upstream.runtimeCall(at, method, args).subscribe(\n (output) => {\n operations.delete(operationId)\n cNotification(output)\n },\n (e) => {\n operations.delete(operationId)\n cNotification(getMsgFromErr(e), true)\n },\n )\n if (!subscription.closed)\n operations.set(operationId, () => {\n subscription.unsubscribe()\n operations.delete(operationId)\n })\n }\n\n const _body = (\n { operations, id: followId }: SubCtx,\n reply: (x: any) => void,\n at: string,\n ) => {\n const operationId = createOpaqueToken()\n reply({ result: \"started\", operationId })\n const subscription = upstream.getBody(at).subscribe(\n ({ block: { extrinsics: value } }) => {\n operations.delete(operationId)\n notification(body, followId, {\n event: \"operationBodyDone\",\n operationId,\n value,\n })\n },\n (e) => {\n operations.delete(operationId)\n notification(body, followId, {\n event: \"operationError\",\n operationId,\n error: getMsgFromErr(e),\n })\n },\n )\n\n if (!subscription.closed)\n operations.set(operationId, () => {\n subscription.unsubscribe()\n operations.delete(operationId)\n })\n }\n\n const _stg = (\n { operations, id: followId }: SubCtx,\n reply: (x: any) => void,\n at: string,\n items: Array<{\n key: string\n type:\n | \"value\"\n | \"hash\"\n | \"descendantsValues\"\n | \"descendantsHashes\"\n | \"closestDescendantMerkleValue\"\n }>,\n ) => {\n const operationId = createOpaqueToken()\n reply({ result: \"started\", operationId })\n const innerNotifiaction = (msg: any) => {\n notification(storage, followId, msg)\n }\n const subscription = getStg$(upstream, at, items)\n .pipe(\n finalize(() => {\n operations.delete(operationId)\n }),\n )\n .subscribe(\n (items) => {\n innerNotifiaction({\n event: \"operationStorageItems\",\n operationId,\n items,\n })\n },\n (e) => {\n innerNotifiaction({\n event: \"operationError\",\n operationId,\n error: getMsgFromErr(e),\n })\n },\n () => {\n innerNotifiaction({\n event: \"operationStorageDone\",\n operationId,\n })\n },\n )\n\n if (!subscription.closed)\n operations.set(operationId, () => {\n subscription.unsubscribe()\n })\n }\n\n const result = (rId: string, method: string, params: Array<any>) => {\n if (method === follow) return _follow(rId)\n const [followId, ...rest] = params as [string, ...any[]]\n const ctx = subscriptions.get(followId)\n if (!ctx) {\n console.log(\"Invalid followSubscription\", rId, method, params)\n return err(rId, -32602, \"Ivalid followSubscription\")\n }\n\n const innerReply = (value: any) => {\n reply(rId, value)\n }\n\n switch (method) {\n case unfollow:\n return _unfollow(rId, followId)\n case stopOperation:\n return _stopOperation(rId, followId, rest[0])\n case unpin: {\n const [hashOrHashes] = rest\n if (\n (Array.isArray(hashOrHashes) ? hashOrHashes : [hashOrHashes]).some(\n (hash) => typeof hash !== \"string\",\n )\n )\n return err(rId, -32602, \"Invalid args\")\n return _unpin(ctx, innerReply, hashOrHashes)\n }\n default: {\n const [at, ...other] = rest as [string, ...any[]]\n if (!ctx.up.isPinned(at)) return err(rId, -32801, \"Block not pinned\")\n\n switch (method) {\n case header:\n return _header(ctx, innerReply, at)\n case body:\n return _body(ctx, innerReply, at)\n case call: {\n const [method, data] = other\n if (typeof method !== \"string\" || typeof data !== \"string\")\n return err(rId, -32602, \"Invalid args\")\n return _call(ctx, innerReply, at, method, data)\n }\n case storage: {\n const [items] = other\n return areItemsValid(items)\n ? _stg(ctx, innerReply, at, items)\n : err(rId, -32602, \"Invalid args\")\n }\n }\n }\n }\n throw null\n }\n\n result.stop = () => {\n subscriptions.forEach((x) => {\n x.cleanUp()\n })\n }\n return result\n}\n"],"names":["result","reply","items","method"],"mappings":";;;;;;;AAQA,MAAM,EAAE,QAAQ,MAAA,EAAQ,OAAA,EAAS,MAAM,IAAA,EAAM,QAAA,EAAU,aAAA,EAAe,KAAA,EAAM,GAC1E,SAAA;AAEK,MAAM,eAAA,GAAkB,CAC7B,QAAA,EACA,KAAA,EACA,KACA,YAAA,KACG;AASH,EAAA,MAAM,aAAA,uBAAoB,GAAA,EAAoB;AAE9C,EAAA,MAAM,OAAA,GAAU,CAAC,GAAA,KAAgB;AAC/B,IAAA,IAAI,aAAA,CAAc,SAAS,CAAA,EAAG;AAC5B,MAAA,OAAO,GAAA,CAAI,GAAA,EAAK,MAAA,EAAQ,eAAe,CAAA;AAAA,IACzC;AACA,IAAA,MAAM,QAAQ,iBAAA,EAAkB;AAChC,IAAA,MAAM,gBAAgB,CAACA,OAAAA,KACrB,YAAA,CAAa,0BAAA,EAA4B,OAAOA,OAAM,CAAA;AACxD,IAAA,MAAM,EAAA,GAAK,QAAA,CAAS,eAAA,EAAgB,CAAE,UAAU,KAAK,CAAA;AACrD,IAAA,MAAM,UAAA,uBAAiB,GAAA,EAAwB;AAC/C,IAAA,aAAA,CAAc,IAAI,KAAA,EAAO;AAAA,MACvB,EAAA,EAAI,KAAA;AAAA,MACJ,EAAA;AAAA,MACA,UAAA;AAAA,MACA,SAAS,MAAM;AACb,QAAA,OAAA,EAAQ;AAAA,MACV;AAAA,KACD,CAAA;AAED,IAAA,KAAA,CAAM,KAAK,KAAK,CAAA;AAEhB,IAAA,IAAI,YAAA,GAAoC,IAAA;AACxC,IAAA,IAAI,UAAU,MAAM;AAClB,MAAA,OAAA,GAAU,IAAA;AACV,MAAA,YAAA,EAAc,WA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AAA,QAAQ,MAAA;AAAQ,UACd,KAAK,MAAA;AACH,YAAA,OAAO,OAAA,CAAQ,GAAA,EAAK,UAAA,EAAY,EAAE,CAAA;AAAA,UACpC,KAAK,IAAA;AACH,YAAA,OAAO,KAAA,CAAM,GAAA,EAAK,UAAA,EAAY,EAAE,CAAA;AAAA,UAClC,KAAK,IAAA,EAAM;AACT,YAAA,MAAM,CAACC,OAAAA,EAAQ,IAAI,CAAA,GAAI,KAAA;AACvB,YAAA,IAAI,OAAOA,OAAAA,KAAW,QAAA,IAAY,OAAO,IAAA,KAAS,QAAA;AAChD,cAAA,OAAO,GAAA,CAAI,GAAA,EAAK,MAAA,EAAQ,cAAc,CAAA;AACxC,YAAA,OAAO,KAAA,CAAM,GAAA,EAAK,UAAA,EAAY,EAAA,EAAIA,SAAQ,IAAI,CAAA;AAAA,UAChD;AAAA,UACA,KAAK,OAAA,EAAS;AACZ,YAAA,MAAM,CAAC,KAAK,CAAA,GAAI,KAAA;AAChB,YAAA,OAAO,aAAA,CAAc,KAAK,CAAA,GACtB,IAAA,CAAK,GAAA,EAAK,UAAA,EAAY,EAAA,EAAI,KAAK,CAAA,GAC/B,GAAA,CAAI,GAAA,EAAK,MAAA,EAAQ,cAAc,CAAA;AAAA,UACrC;AAAA;AACF,MACF;AAAA;AAEF,IAAA,MAAM,IAAA;AAAA,EACR,CAAA;AAEA,EAAA,MAAA,CAAO,OAAO,MAAM;AAClB,IAAA,aAAA,CAAc,OAAA,CAAQ,CAAC,CAAA,KAAM;AAC3B,MAAA,CAAA,CAAE,OAAA,EAAQ;AAAA,IACZ,CAAC,CAAA;AAAA,EACH,CAAA;AACA,EAAA,OAAO,MAAA;AACT;;;;"}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
import { Binary } from '@polkadot-api/substrate-bindings';
|
|
1
2
|
import { toHex, fromHex } from '@polkadot-api/utils';
|
|
2
|
-
import { Observable, map, mergeMap, EMPTY, merge, of
|
|
3
|
-
import { getBlocks$ } from './blocks/index.js';
|
|
3
|
+
import { Observable, map, mergeMap, catchError, EMPTY, merge, of } from 'rxjs';
|
|
4
4
|
import { withLatestFromBp } from '../utils/with-latest-from-bp.js';
|
|
5
|
+
import { getBlocks$ } from './blocks/index.js';
|
|
5
6
|
import { createClosestDescendantMerkleValue } from './proofs.js';
|
|
6
7
|
|
|
7
8
|
const createUpstream = (request) => {
|
|
@@ -69,12 +70,28 @@ const createUpstream = (request) => {
|
|
|
69
70
|
)
|
|
70
71
|
);
|
|
71
72
|
const stgClosestDescendant = createClosestDescendantMerkleValue(obsRequest);
|
|
72
|
-
const [stgValue,
|
|
73
|
+
const [stgValue, rawStgHash] = [
|
|
74
|
+
"state_getStorage",
|
|
75
|
+
"state_getStorageHash"
|
|
76
|
+
].map(
|
|
73
77
|
(method) => (atBlock, key) => obsRequest(method, [
|
|
74
78
|
key,
|
|
75
79
|
atBlock
|
|
76
80
|
])
|
|
77
81
|
);
|
|
82
|
+
const stgHash = (atBlock, key) => rawStgHash(atBlock, key).pipe(
|
|
83
|
+
catchError((ex) => {
|
|
84
|
+
if (ex instanceof Error && ex.message === "Method not found") {
|
|
85
|
+
return stgValue(atBlock, key).pipe(
|
|
86
|
+
withLatestFromBp(subscribeBlocks().hasher$),
|
|
87
|
+
map(
|
|
88
|
+
([hasher, value]) => value == null ? null : Binary.toHex(hasher(Binary.fromHex(value)))
|
|
89
|
+
)
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
throw ex;
|
|
93
|
+
})
|
|
94
|
+
);
|
|
78
95
|
const methods = obsRequest("rpc_methods", []);
|
|
79
96
|
const chainName = obsRequest("system_name", []);
|
|
80
97
|
const properties = obsRequest("system_properties", []);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"upstream.js","sources":["../../../src/legacy/upstream/upstream.ts"],"sourcesContent":["import { ClientRequest } from \"@polkadot-api/raw-client\"\nimport { fromHex, toHex } from \"@polkadot-api/utils\"\nimport { catchError, EMPTY, map, merge, mergeMap, Observable, of } from \"rxjs\"\nimport {
|
|
1
|
+
{"version":3,"file":"upstream.js","sources":["../../../src/legacy/upstream/upstream.ts"],"sourcesContent":["import { ClientRequest } from \"@polkadot-api/raw-client\"\nimport { Binary } from \"@polkadot-api/substrate-bindings\"\nimport { fromHex, toHex } from \"@polkadot-api/utils\"\nimport { catchError, EMPTY, map, merge, mergeMap, Observable, of } from \"rxjs\"\nimport { withLatestFromBp } from \"../utils/with-latest-from-bp\"\nimport { getBlocks$ } from \"./blocks\"\nimport { createClosestDescendantMerkleValue } from \"./proofs\"\n\nexport const createUpstream = (request: ClientRequest<any, any>) => {\n const simpleRequest = <Args extends Array<any>, Payload>(\n method: string,\n params: Args,\n onSuccess: (value: Payload) => void,\n onError: (e: any) => void,\n ): (() => void) => request(method, params, { onSuccess, onError })\n\n const obsRequest = <Args extends Array<any>, Payload>(\n method: string,\n params: Args,\n ): Observable<Payload> =>\n new Observable((observer) =>\n simpleRequest<Args, Payload>(\n method,\n params,\n (v) => {\n observer.next(v)\n observer.complete()\n },\n (e) => {\n observer.error(e)\n },\n ),\n )\n\n let blockSubscription: ReturnType<typeof getBlocks$> | null = null\n const subscribeBlocks = () => {\n blockSubscription ??= getBlocks$(request, obsRequest)\n return {\n ...blockSubscription,\n getBlocks: blockSubscription.upstream,\n }\n }\n const clean = () => blockSubscription?.clean()\n\n const runtimeCall = (atBlock: string, method: string, data: string) =>\n obsRequest<[string, string, string], string | null>(\"state_call\", [\n method,\n data,\n atBlock,\n ])\n\n const stgDescendantValues = (\n at: string,\n rootKey: string,\n ): Observable<Array<[string, string]>> => {\n const PAGE_SIZE = 1000\n const getKeys = (startAtKey?: string): Observable<string[]> => {\n return obsRequest<[string, number, string | undefined, string], string[]>(\n \"state_getKeysPaged\",\n [rootKey, PAGE_SIZE, startAtKey || undefined, at],\n ).pipe(\n mergeMap((receivedKeys) => {\n const keys =\n receivedKeys[0] === startAtKey\n ? receivedKeys.slice(1)\n : receivedKeys\n\n const continuation =\n receivedKeys.length < PAGE_SIZE\n ? EMPTY\n : getKeys(receivedKeys.at(-1))\n\n return merge(of(keys), continuation)\n }),\n )\n }\n\n const getValues = (\n keys: string[],\n nSplits = 0,\n ): Observable<Array<[string, string]>> =>\n keys.length\n ? obsRequest<\n [string[], string],\n [{ block: string; changes: Array<[string, string]> }]\n >(\"state_queryStorageAt\", [keys, at]).pipe(\n map(([{ changes }]) => changes),\n catchError((e: any) => {\n if (nSplits > 3) throw e\n\n const midIdx = Math.floor(keys.length / 2)\n return merge(\n getValues(keys.slice(0, midIdx), ++nSplits),\n getValues(keys.slice(midIdx), nSplits),\n )\n }),\n )\n : EMPTY\n\n return getKeys().pipe(mergeMap((keys) => getValues(keys)))\n }\n\n const stgDescendantHashes = (at: string, rootKey: string) =>\n stgDescendantValues(at, rootKey).pipe(\n withLatestFromBp(subscribeBlocks().hasher$),\n map(([hasher, results]) =>\n results.map(\n ([key, value]) =>\n [key, toHex(hasher(fromHex(value)))] as [string, string],\n ),\n ),\n )\n\n const stgClosestDescendant = createClosestDescendantMerkleValue(obsRequest)\n\n const [stgValue, rawStgHash] = [\n \"state_getStorage\",\n \"state_getStorageHash\",\n ].map(\n (method) => (atBlock: string, key: string) =>\n obsRequest<[string, string | undefined], string | null>(method, [\n key,\n atBlock,\n ]),\n )\n\n const stgHash = (atBlock: string, key: string) =>\n rawStgHash(atBlock, key).pipe(\n catchError((ex) => {\n if (ex instanceof Error && ex.message === \"Method not found\") {\n return stgValue(atBlock, key).pipe(\n withLatestFromBp(subscribeBlocks().hasher$),\n map(([hasher, value]) =>\n value == null\n ? null\n : Binary.toHex(hasher(Binary.fromHex(value))),\n ),\n )\n }\n throw ex\n }),\n )\n\n const methods = obsRequest<[], { methods: string[] }>(\"rpc_methods\", [])\n const chainName = obsRequest<[], string>(\"system_name\", [])\n const properties = obsRequest<[], {}>(\"system_properties\", [])\n const getBody = (at: string) =>\n obsRequest<[string], { block: { extrinsics: Array<string> } }>(\n \"chain_getBlock\",\n [at],\n )\n\n const getBlockHash$ = (height: number) =>\n obsRequest<[height: number], string>(\"chain_getBlockHash\", [height])\n const genesisHash = getBlockHash$(0)\n\n return {\n subscribeBlocks,\n getBlockHash$,\n stgValue,\n stgHash,\n stgDescendantValues,\n stgDescendantHashes,\n stgClosestDescendant,\n runtimeCall,\n getBody,\n chainName,\n properties,\n genesisHash,\n methods,\n request: simpleRequest,\n obsRequest,\n clean,\n }\n}\n"],"names":[],"mappings":";;;;;;;AAQO,MAAM,cAAA,GAAiB,CAAC,OAAA,KAAqC;AAClE,EAAA,MAAM,aAAA,GAAgB,CACpB,MAAA,EACA,MAAA,EACA,SAAA,EACA,OAAA,KACiB,OAAA,CAAQ,MAAA,EAAQ,MAAA,EAAQ,EAAE,SAAA,EAAW,OAAA,EAAS,CAAA;AAEjE,EAAA,MAAM,UAAA,GAAa,CACjB,MAAA,EACA,MAAA,KAEA,IAAI,UAAA;AAAA,IAAW,CAAC,QAAA,KACd,aAAA;AAAA,MACE,MAAA;AAAA,MACA,MAAA;AAAA,MACA,CAAC,CAAA,KAAM;AACL,QAAA,QAAA,CAAS,KAAK,CAAC,CAAA;AACf,QAAA,QAAA,CAAS,QAAA,EAAS;AAAA,MACpB,CAAA;AAAA,MACA,CAAC,CAAA,KAAM;AACL,QAAA,QAAA,CAAS,MAAM,CAAC,CAAA;AAAA,MAClB;AAAA;AACF,GACF;AAEF,EAAA,IAAI,iBAAA,GAA0D,IAAA;AAC9D,EAAA,MAAM,kBAAkB,MAAM;AAC5B,IAAA,iBAAA,KAAA,iBAAA,GAAsB,UAAA,CAAW,SAAS,UAAU,CAAA,CAAA;AACpD,IAAA,OAAO;AAAA,MACL,GAAG,iBAAA;AAAA,MACH,WAAW,iBAAA,CAAkB;AAAA,KAC/B;AAAA,EACF,CAAA;AACA,EAAA,MAAM,KAAA,GAAQ,MAAM,iBAAA,EAAmB,KAAA,EAAM;AAE7C,EAAA,MAAM,cAAc,CAAC,OAAA,EAAiB,MAAA,EAAgB,IAAA,KACp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.3.2-canary.
|
|
3
|
+
"version": "0.3.2-canary.fa37019",
|
|
4
4
|
"author": "Josep M Sobrepere (https://github.com/josepot)",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -28,11 +28,11 @@
|
|
|
28
28
|
"rxjs": ">=7.8.0"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@polkadot-api/json-rpc-provider": "0.
|
|
32
|
-
"@polkadot-api/json-rpc-provider
|
|
33
|
-
"@polkadot-api/substrate-bindings": "0.20.2-canary.
|
|
34
|
-
"@polkadot-api/raw-client": "0.3.1-canary.
|
|
35
|
-
"@polkadot-api/utils": "0.4.1-canary.
|
|
31
|
+
"@polkadot-api/json-rpc-provider-proxy": "0.4.1-canary.fa37019",
|
|
32
|
+
"@polkadot-api/json-rpc-provider": "0.2.1-canary.fa37019",
|
|
33
|
+
"@polkadot-api/substrate-bindings": "0.20.2-canary.fa37019",
|
|
34
|
+
"@polkadot-api/raw-client": "0.3.1-canary.fa37019",
|
|
35
|
+
"@polkadot-api/utils": "0.4.1-canary.fa37019"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
38
|
"rxjs": "^7.8.2"
|