Skip to content
This repository has been archived by the owner on Sep 14, 2023. It is now read-only.

chore: use new RPC impl #356

Merged
merged 33 commits into from
Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
5e8dc25
first pass
harrysolovay Nov 3, 2022
2bea357
fix reference counting issue
harrysolovay Nov 3, 2022
77916c5
continue cleanup
harrysolovay Nov 3, 2022
80d71fd
clean up test ctx and exposed clients
harrysolovay Nov 3, 2022
0a533cc
test ctx cleanup in progress
harrysolovay Nov 4, 2022
c562d22
continue local test chain cleanup
harrysolovay Nov 4, 2022
9891bc2
debugging
harrysolovay Nov 4, 2022
7fd0a1a
add new zones import specifier
harrysolovay Nov 4, 2022
a13e589
get rid of logged noise
harrysolovay Nov 4, 2022
01e3dec
test cleanup
harrysolovay Nov 4, 2022
75320cc
continue debugging
harrysolovay Nov 4, 2022
ac85ccb
continue debugging
harrysolovay Nov 5, 2022
ec33bbd
get rid of unused deps
harrysolovay Nov 5, 2022
04cea66
continued
harrysolovay Nov 5, 2022
9a44369
continued
harrysolovay Nov 5, 2022
1eb4346
incorrectly named zone
harrysolovay Nov 5, 2022
e55d0d1
continued
harrysolovay Nov 6, 2022
72f3885
temporarily ignore batch example from being run in ci
harrysolovay Nov 6, 2022
6300814
continued
harrysolovay Nov 6, 2022
b711ae4
continued
harrysolovay Nov 6, 2022
f64c817
continued
harrysolovay Nov 6, 2022
eb690be
get rid of clients obj export from test util
harrysolovay Nov 6, 2022
220f49f
update download task
harrysolovay Nov 6, 2022
fa5854e
fix root export name conflict
harrysolovay Nov 6, 2022
fca5fff
restore zones import specifier
harrysolovay Nov 6, 2022
ce8f411
ignore all.ts example
harrysolovay Nov 6, 2022
efa72b6
continued
harrysolovay Nov 7, 2022
2858767
continued
harrysolovay Nov 7, 2022
1960387
fix failing test
harrysolovay Nov 7, 2022
e9d5d3e
fix conflict
harrysolovay Nov 7, 2022
20bb703
Update examples/raw_rpc_client_subscription.ts
harrysolovay Nov 7, 2022
f38b663
respond to matias feedback
harrysolovay Nov 7, 2022
8a57552
fix lint issue
harrysolovay Nov 7, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 28 additions & 22 deletions _tasks/download_frame_metadata.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
import * as fs from "../deps/std/fs.ts";
import * as path from "../deps/std/path.ts";
import { assert } from "../deps/std/testing/asserts.ts";
import { acala, kusama, moonbeam, polkadot, statemint, subsocial, westend } from "../known/mod.ts";
import * as rpc from "../rpc/mod.ts";
import * as Z from "../deps/zones.ts";
import * as knownClients from "../known/clients.ts";
import * as C from "../mod.ts";
import * as U from "../util/mod.ts";

const outDir = path.join(Deno.cwd(), "frame_metadata", "_downloaded");
await fs.emptyDir(outDir);
await Promise.all(
Object.entries({ acala, kusama, moonbeam, polkadot, statemint, subsocial, westend }).map(
async ([name, config]) => {
const client = U.throwIfError(await rpc.proxyClient(config));
try {
const metadata = await client.call("state_getMetadata", []);
assert(metadata.result);
const outPath = path.join(outDir, `${name}.scale`);
console.log(`Downloading ${name} metadata to "${outPath}".`);
await Deno.writeTextFile(outPath, metadata.result);
} catch (e) {
console.error(`Encountered error downloading frame metadata for ${name}.`);
console.error(e);
Deno.exit(1);
}
await client.close();
},
),
);
U.throwIfError(await Z.ls(...Object.entries(knownClients).map(download)).run());

function download<Name extends Z.$<string>, Client extends Z.$<C.rpc.Client>>(
entry: [name: Name, client: Client],
) {
return Z.call(Z.ls(...entry), async ([name, client]) => {
try {
const metadataHex = U.throwIfError(await C.state.getMetadata(client)().run());
const outPath = path.join(outDir, `${name}.scale`);
console.log(`Downloading ${name} metadata to "${outPath}".`);
await Deno.writeTextFile(outPath, metadataHex);
return;
} catch (cause) {
return new MetadataDownloadError(name, { cause });
}
});
}

class MetadataDownloadError extends Error {
override readonly name = "MetadataDownloadError";

constructor(readonly chainName: string, options: ErrorOptions) {
super(undefined, options);
}
}
14 changes: 4 additions & 10 deletions codegen.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { codegen } from "./codegen/mod.ts";
import { Config } from "./config/mod.ts";
import { parse } from "./deps/std/flags.ts";
import * as path from "./deps/std/path.ts";
import { unimplemented } from "./deps/std/testing/asserts.ts";
import * as M from "./frame_metadata/mod.ts";
import { proxyClient } from "./rpc/providers/proxy.ts";
import * as C from "./mod.ts";
import * as U from "./util/mod.ts";

const args = parse(Deno.args, {
Expand All @@ -30,13 +28,9 @@ await codegen({
}).write(args.out);

// Should disallow .scale as input?
async function getMetadata(src: string): Promise<M.Metadata> {
async function getMetadata(src: string): Promise<C.M.Metadata> {
if (src.startsWith("ws")) {
const client = U.throwIfError(await proxyClient(new Config(() => src)));
const metadata = U.throwIfError(await client.call("state_getMetadata", []));
U.throwIfError(await client.close());
if (metadata.error) fail();
return M.fromPrefixedHex(metadata.result);
return U.throwIfError(await C.metadata(C.rpcClient(C.rpc.proxyProvider, src))().run());
} else if (path.isAbsolute(src)) {
return await loadMetadata(src);
} else {
Expand All @@ -52,7 +46,7 @@ async function loadMetadata(src: string) {
const ext = path.extname(src);
switch (ext) {
case ".scale": {
return M.fromPrefixedHex(await Deno.readTextFile(src));
return C.M.fromPrefixedHex(await Deno.readTextFile(src));
}
case ".json": {
return unimplemented();
Expand Down
22 changes: 16 additions & 6 deletions codegen/codecVisitor.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
import { Codec } from "../deps/scale.ts";
import * as path from "../deps/std/path.ts";
import { assertEquals } from "../deps/std/testing/asserts.ts";
import * as M from "../frame_metadata/mod.ts";
import * as C from "../mod.ts";
import * as T from "../test_util/mod.ts";
import * as testClients from "../test_util/clients/mod.ts";
import * as U from "../util/mod.ts";
import { codegen } from "./mod.ts";

for (const config of T.configs) {
Deno.test(config.runtimeName, async () => {
const metadata = U.throwIfError(await C.run(C.metadata(config)));
const codegen = await T.importCodegen(config);
const currentDir = path.dirname(path.fromFileUrl(import.meta.url));
const codegenTestDir = path.join(currentDir, "../target/codegen");

for (const [runtime, client] of Object.entries(testClients)) {
Deno.test(runtime, async () => {
const metadata = U.throwIfError(await C.metadata(client)().run());
const outDir = path.join(codegenTestDir, runtime);
await codegen({
importSpecifier: "../../../mod.ts",
metadata,
}).write(outDir);
const codegened = await import(path.toFileUrl(path.join(outDir, "mod.ts")).toString());
const deriveCodec = M.DeriveCodec(metadata.tys);
const derivedCodecs = metadata.tys.map(deriveCodec);
const codegenCodecs = codegen._metadata.types;
const codegenCodecs = codegened._metadata.types;
const origInspect = Codec.prototype["_inspect"]!;
let inspecting = 0;
Codec.prototype["_inspect"] = function(inspect) {
Expand Down
12 changes: 0 additions & 12 deletions config/mod.ts

This file was deleted.

1 change: 0 additions & 1 deletion deps/case.ts

This file was deleted.

1 change: 0 additions & 1 deletion deps/code_block_writer.ts

This file was deleted.

1 change: 0 additions & 1 deletion deps/conditional_type_checks.ts

This file was deleted.

1 change: 0 additions & 1 deletion deps/ss58_registry.ts

This file was deleted.

1 change: 0 additions & 1 deletion deps/std/dotenv/load.ts

This file was deleted.

1 change: 0 additions & 1 deletion deps/std/fmt/colors.ts

This file was deleted.

2 changes: 1 addition & 1 deletion deps/zones.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from "https://deno.land/x/[email protected].8/mod.ts";
export * from "https://deno.land/x/[email protected].11/mod.ts";
42 changes: 20 additions & 22 deletions effects/blockRead.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
import { Config } from "../config/mod.ts";
import * as Z from "../deps/zones.ts";
import * as rpc from "../rpc/mod.ts";
import * as U from "../util/mod.ts";
import { $extrinsic } from "./core/$extrinsic.ts";
import { deriveCodec } from "./core/deriveCodec.ts";
import { metadata } from "./metadata.ts";
import { rpcCall } from "./rpcCall.ts";
import { chain } from "./rpc/known.ts";

export function blockRead<Rest extends [blockHash?: Z.$<U.HexHash | undefined>]>(
config: Config,
...[blockHash]: [...Rest]
) {
const metadata_ = metadata(config, blockHash);
const $extrinsic_ = $extrinsic(deriveCodec(metadata_), metadata_, undefined!);
const call = rpcCall(config, "chain_getBlock", [blockHash]);
const decoded = Z.call(Z.ls($extrinsic_, call), function mapExtrinsicCall([$extrinsic_, call]) {
const { block: { extrinsics, header }, justifications } = call.result;
return {
justifications,
block: {
header,
extrinsics: extrinsics.map((extrinsic: U.Hex) => {
return $extrinsic_.decode(U.hex.decode(extrinsic));
}),
},
};
});
return Z.wrap(decoded, "block");
export function blockRead<Client extends Z.$<rpc.Client>>(client: Client) {
return <Rest extends [blockHash?: Z.$<U.HexHash | undefined>]>(...[blockHash]: [...Rest]) => {
const metadata_ = metadata(client)(blockHash);
const $extrinsic_ = $extrinsic(deriveCodec(metadata_), metadata_, undefined!);
const call = chain.getBlock(client)(blockHash);
return Z.call(Z.ls($extrinsic_, call), function mapExtrinsicCall([$extrinsic_, call]) {
const { block: { extrinsics, header }, justifications } = call;
return {
justifications,
block: {
header,
extrinsics: extrinsics.map((extrinsic: U.Hex) => {
return $extrinsic_.decode(U.hex.decode(extrinsic));
}),
},
};
}).zoned("BlockRead");
};
}
39 changes: 16 additions & 23 deletions effects/blockWatch.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,22 @@
import { Config } from "../config/mod.ts";
import * as Z from "../deps/zones.ts";
import * as M from "../frame_metadata/mod.ts";
import * as known from "../known/mod.ts";
import * as rpc from "../rpc/mod.ts";
import * as U from "../util/mod.ts";
import { blockRead } from "./blockRead.ts";
import { rpcCall } from "./rpcCall.ts";
import { rpcSubscription } from "./rpcSubscription.ts";
import { run } from "./run.ts";
import { chain } from "./rpc/known.ts";

export function blockWatch(
config: Config,
createWatchHandler: U.CreateListener<known.SignedBlock>,
) {
return rpcSubscription(
config,
"chain_subscribeNewHeads",
[],
function subscribeNewHeadsHandler(stop) {
const watchHandler = createWatchHandler(stop);
return async (result) => {
const blockNum: number = result.params.result.number;
const blockHash = rpcCall(config, "chain_getBlockHash", [blockNum]).access("result");
// TODO: zones-level solution
const block = U.throwIfError(await run(blockRead(config, blockHash)));
watchHandler(block.block);
export function blockWatch<Client extends Z.$<rpc.Client>>(client: Client) {
return (listener: U.Listener<known.SignedBlock<M.Extrinsic>, rpc.ClientSubscribeContext>) => {
const listenerMapped = Z.call(listener, function mapBlockWatchListener(listener, env) {
return async function(this: rpc.ClientSubscribeContext, header: known.Header) {
const blockHash = chain.getBlockHash(client)(header.number);
const block = await blockRead(client)(blockHash).bind(env)();
if (block instanceof Error) throw block;
listener.apply(this, [block]);
};
},
(ok) => rpcCall(config, "chain_unsubscribeNewHead", [ok.result]),
);
});
const subscriptionId = chain.subscribeNewHeads(client)([], listenerMapped);
return chain.unsubscribeNewHeads(client)(subscriptionId).zoned("BlockWatch");
};
}
21 changes: 0 additions & 21 deletions effects/common.ts

This file was deleted.

39 changes: 20 additions & 19 deletions effects/const.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
import { Config } from "../config/mod.ts";
import * as Z from "../deps/zones.ts";
import * as rpc from "../rpc/mod.ts";
import * as U from "../util/mod.ts";
import { codec } from "./core/codec.ts";
import { decoded } from "./core/decoded.ts";
import { deriveCodec } from "./core/deriveCodec.ts";
import { constMetadata, metadata, palletMetadata } from "./metadata.ts";

function const_<
PalletName extends Z.$<string>,
ConstName extends Z.$<string>,
Rest extends [blockHash?: Z.$<U.HexHash | undefined>],
>(
config: Config,
palletName: PalletName,
constName: ConstName,
...[blockHash]: [...Rest]
) {
const metadata_ = metadata(config, blockHash);
const deriveCodec_ = deriveCodec(metadata_);
const palletMetadata_ = palletMetadata(metadata_, palletName);
const constMetadata_ = constMetadata(palletMetadata_, constName);
const entryValueTypeI = constMetadata_.access("ty").access("id");
const constValue = constMetadata_.access("value");
const $const = codec(deriveCodec_, entryValueTypeI);
return decoded($const, constValue, "value");
export function const_<Client extends Z.$<rpc.Client>>(client: Client) {
return <
PalletName extends Z.$<string>,
ConstName extends Z.$<string>,
Rest extends [blockHash?: Z.$<U.HexHash | undefined>],
>(
palletName: PalletName,
constName: ConstName,
...[blockHash]: [...Rest]
) => {
const metadata_ = metadata(client)(blockHash);
const deriveCodec_ = deriveCodec(metadata_);
const palletMetadata_ = palletMetadata(metadata_, palletName);
const constMetadata_ = constMetadata(palletMetadata_, constName);
const entryValueTypeI = constMetadata_.access("ty").access("id");
const constValue = constMetadata_.access("value");
const $const = codec(deriveCodec_, entryValueTypeI);
return decoded($const, constValue, "value").zoned("Const");
};
}
Object.defineProperty(const_, "name", {
value: "const",
Expand Down
9 changes: 0 additions & 9 deletions effects/core/rpcClient.ts

This file was deleted.

50 changes: 25 additions & 25 deletions effects/entryRead.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Config } from "../config/mod.ts";
import * as Z from "../deps/zones.ts";
import * as rpc from "../rpc/mod.ts";
import * as U from "../util/mod.ts";
import { $storageKey } from "./core/$storageKey.ts";
import { codec } from "./core/codec.ts";
Expand All @@ -8,29 +8,29 @@ import { deriveCodec } from "./core/deriveCodec.ts";
import { hexDecode } from "./core/hex.ts";
import { storageKey } from "./core/storageKey.ts";
import { entryMetadata, metadata, palletMetadata } from "./metadata.ts";
import { rpcCall } from "./rpcCall.ts";
import { state } from "./rpc/known.ts";

export function entryRead<
PalletName extends Z.$<string>,
EntryName extends Z.$<string>,
Keys extends unknown[],
Rest extends [blockHash?: Z.$<U.HexHash | undefined>],
>(
config: Config,
palletName: PalletName,
entryName: EntryName,
keys: [...Keys],
...[blockHash]: [...Rest]
) {
const metadata_ = metadata(config, blockHash);
const deriveCodec_ = deriveCodec(metadata_);
const palletMetadata_ = palletMetadata(metadata_, palletName);
const entryMetadata_ = entryMetadata(palletMetadata_, entryName);
const $storageKey_ = $storageKey(deriveCodec_, palletMetadata_, entryMetadata_);
const storageKey_ = storageKey($storageKey_, ...keys);
const storageCall = rpcCall(config, "state_getStorage", [storageKey_, blockHash]);
const entryValueTypeI = entryMetadata_.access("value");
const $entry = codec(deriveCodec_, entryValueTypeI);
const result = storageCall.access("result");
return decoded($entry, hexDecode(result), "value");
export function entryRead<Client extends Z.$<rpc.Client>>(client: Client) {
return <
PalletName extends Z.$<string>,
EntryName extends Z.$<string>,
Keys extends unknown[],
Rest extends [blockHash?: Z.$<U.HexHash | undefined>],
>(
palletName: PalletName,
entryName: EntryName,
keys: [...Keys],
...[blockHash]: [...Rest]
) => {
const metadata_ = metadata(client)(blockHash);
const deriveCodec_ = deriveCodec(metadata_);
const palletMetadata_ = palletMetadata(metadata_, palletName);
const entryMetadata_ = entryMetadata(palletMetadata_, entryName);
const $storageKey_ = $storageKey(deriveCodec_, palletMetadata_, entryMetadata_);
const storageKey_ = storageKey($storageKey_, ...keys);
const storageValueHex = state.getStorage(client)(storageKey_, blockHash);
const entryValueTypeI = entryMetadata_.access("value");
const $entry = codec(deriveCodec_, entryValueTypeI);
return decoded($entry, hexDecode(storageValueHex), "value").zoned("EntryRead");
};
}
Loading