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

Commit

Permalink
feat: RPC client re-write (#341)
Browse files Browse the repository at this point in the history
Co-authored-by: Harry Solovay <[email protected]>
Co-authored-by: T6 <[email protected]>
  • Loading branch information
3 people authored Nov 3, 2022
1 parent e01e5a5 commit c129d6d
Show file tree
Hide file tree
Showing 45 changed files with 881 additions and 295 deletions.
2 changes: 1 addition & 1 deletion _tasks/download_frame_metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ 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.stdClient(config));
const client = U.throwIfError(await rpc.proxyClient(config));
try {
const metadata = await client.call("state_getMetadata", []);
assert(metadata.result);
Expand Down
1 change: 1 addition & 0 deletions codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ await codegen({
metadata: await getMetadata(args.src),
}).write(args.out);

// Should disallow .scale as input?
async function getMetadata(src: string): Promise<M.Metadata> {
if (src.startsWith("ws")) {
const client = U.throwIfError(await proxyClient(new Config(() => src)));
Expand Down
4 changes: 1 addition & 3 deletions deps/smoldot.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
// Smoldot is a peer dependency; we only utilize its types, never its runtime.
// @deno-types="https://esm.sh/@substrate/[email protected]/dist/index.d.ts"
export * from "./smoldot_phantom.ts";
export * from "https://deno.land/x/[email protected]/index-deno.js";
1 change: 1 addition & 0 deletions deps/smoldot/client.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "https://deno.land/x/[email protected]/client.d.ts";
1 change: 0 additions & 1 deletion deps/smoldot_phantom.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].7/mod.ts";
export * from "https://deno.land/x/[email protected].8/mod.ts";
2 changes: 1 addition & 1 deletion effects/blockWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { run } from "./run.ts";

export function blockWatch(
config: Config,
createWatchHandler: U.CreateWatchHandler<known.SignedBlock>,
createWatchHandler: U.CreateListener<known.SignedBlock>,
) {
return rpcSubscription(
config,
Expand Down
2 changes: 1 addition & 1 deletion effects/core/rpcClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import * as rpc from "../../rpc/mod.ts";

export function rpcClient<C extends Config>(config: C) {
return Z.call(config, function rpcClientImpl() {
return rpc.stdClient(config);
return rpc.proxyClient(config);
});
}
4 changes: 2 additions & 2 deletions effects/entryWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function entryWatch<
palletName: PalletName,
entryName: EntryName,
keys: Keys,
createWatchHandler: U.CreateWatchHandler<WatchEntryEvent[]>,
createWatchHandler: U.CreateListener<WatchEntryEvent[]>,
) {
const metadata_ = metadata(config);
const deriveCodec_ = deriveCodec(metadata_);
Expand All @@ -37,7 +37,7 @@ export function entryWatch<
},
);
const watchInit = Z.call($entry, function entryWatchInit($entry) {
return U.mapCreateWatchHandler(
return U.mapCreateListener(
createWatchHandler,
(message: rpc.NotifMessage) => {
return message.params.result.changes.map(([key, val]: any) => {
Expand Down
2 changes: 1 addition & 1 deletion effects/extrinsic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export class SignedExtrinsic<
}

watch<
WatchHandler extends U.CreateWatchHandler<NotifMessage<author.TransactionStatus>>,
WatchHandler extends U.CreateListener<NotifMessage<author.TransactionStatus>>,
>(watchHandler: WatchHandler) {
return rpcSubscription(
this.config,
Expand Down
9 changes: 4 additions & 5 deletions effects/rpcCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ export function rpcCall<MethodName extends Z.$<string>, Params extends Z.Ls$<unk
methodName: MethodName,
params: [...Params],
) {
const client = rpcClient(config);
const deps = Z.ls(client, methodName, ...params);
return Z.call(
Z.ls(deps, Z.rc(client, deps)),
async function rpcCallImpl([[client, methodName, ...params], rc]) {
Z.rc(rpcClient(config), methodName, ...params),
async function rpcCallImpl([[client, methodName, ...params], counter]) {
const result = await client.call(
methodName,
params,
Expand All @@ -26,7 +24,8 @@ export function rpcCall<MethodName extends Z.$<string>, Params extends Z.Ls$<unk
},
});
}
if (rc() == 1) {
counter.i--;
if (counter.i === 1) {
const close = await client.close();
if (close instanceof Error) return close;
}
Expand Down
13 changes: 7 additions & 6 deletions effects/rpcSubscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { run } from "./run.ts";
export function rpcSubscription<
MethodName extends Z.$<string>,
Params extends Z.Ls$<unknown[]>,
CreateListenerCb extends Z.$<U.CreateWatchHandler<rpc.NotifMessage>>,
CreateListenerCb extends Z.$<U.CreateListener<rpc.NotifMessage>>,
>(
config: Config,
methodName: MethodName,
Expand All @@ -18,11 +18,11 @@ export function rpcSubscription<
// TODO: improve cleanup system
cleanup?: (initOk: rpc.OkMessage) => Z.Effect,
) {
const client = rpcClient(config);
const deps = Z.ls(client, methodName, createListenerCb, ...params);
return Z.call(
Z.ls(deps, Z.rc(client, deps)),
async function rpcSubscriptionImpl([[client, methodName, createListenerCb, ...params], rc]) {
Z.rc(rpcClient(config), methodName, createListenerCb, ...params),
async function rpcSubscriptionImpl(
[[client, methodName, createListenerCb, ...params], counter],
) {
const result = await client.subscribe(
methodName,
params,
Expand All @@ -38,7 +38,8 @@ export function rpcSubscription<
},
});
}
if (rc() == 1) {
counter.i--;
if (counter.i === 1) {
const close = await client.close();
if (close instanceof Error) return close;
}
Expand Down
1 change: 1 addition & 0 deletions examples/.ignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
derived.ts
watch_blocks_iter.ts
2 changes: 1 addition & 1 deletion examples/raw_rpc_client/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as C from "../../mod.ts";
import * as rpc from "../../rpc/mod.ts";
import * as U from "../../util/mod.ts";

const client = U.throwIfError(await rpc.stdClient(C.westend));
const client = U.throwIfError(await rpc.proxyClient(C.westend));

console.log(await client.call("state_getMetadata", []));

Expand Down
2 changes: 1 addition & 1 deletion examples/raw_rpc_client/subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as C from "../../mod.ts";
import * as rpc from "../../rpc/mod.ts";
import * as U from "../../util/mod.ts";

const client = U.throwIfError(await rpc.stdClient(C.westend));
const client = U.throwIfError(await rpc.proxyClient(C.westend));

const maybeError = await client.subscribe("chain_subscribeAllHeads", [], (stop) => {
let i = 1;
Expand Down
1 change: 1 addition & 0 deletions known/rpc/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# TODO: integrate this dir into RPC re-implementation dir
2 changes: 1 addition & 1 deletion known/rpc/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ export type StateRpc = {
state_getStorageSize(key: StorageKey, at?: Hash): RpcResult<number | null>;
state_getStorageSizeAt: StateRpc["state_getStorageSize"];
/** Returns the runtime metadata as an opaque blob. */
state_getMetadata(at?: Hash): RpcResult<Hex | null>;
state_getMetadata(at?: Hash): RpcResult<Hex>;
/** Get the runtime version. */
state_getRuntimeVersion(at?: Hash): RpcResult<RuntimeVersion>;
chain_getRuntimeVersion: StateRpc["state_getRuntimeVersion"];
Expand Down
9 changes: 1 addition & 8 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,4 @@ export * from "./effects/mod.ts";
export * as M from "./frame_metadata/mod.ts";
export { $era, $null, ChainError, type Era } from "./frame_metadata/mod.ts";
export { kusama, moonbeam, polkadot, rococo, westend } from "./known/mod.ts"; // TODO: get rid of this!
export {
type CreateWatchHandler,
hex,
mapCreateWatchHandler,
type WatchHandler,
type WatchIter,
watchIter,
} from "./util/mod.ts";
export { hex, type Listener, type WatchIter, watchIter } from "./util/mod.ts";
6 changes: 3 additions & 3 deletions rpc/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export abstract class Client<
CloseError extends Error,
> {
#nextId = 0;
#listenerCbs = new Map<U.WatchHandler<msg.IngressMessage>, true>();
#listenerCbs = new Map<U.Listener<msg.IngressMessage>, true>();

/**
* Construct a new RPC client
Expand Down Expand Up @@ -48,7 +48,7 @@ export abstract class Client<
*
* @param createListenerCb the factory for the callback to be triggered upon arrival of ingress messages
*/
listen = (createListenerCb: U.CreateWatchHandler<msg.IngressMessage>): void => {
listen = (createListenerCb: U.CreateListener<msg.IngressMessage>): void => {
const stopListening = () => {
this.#listenerCbs.delete(listenerCb);
};
Expand Down Expand Up @@ -144,6 +144,6 @@ export abstract class Client<
};
}

export type CreateListenerCb = U.CreateWatchHandler<msg.NotifMessage>;
export type CreateListenerCb = U.CreateListener<msg.NotifMessage>;

export type SubscriptionCleanup = (initOk: msg.OkMessage) => Promise<void>;
38 changes: 0 additions & 38 deletions rpc/Demux.test.ts

This file was deleted.

62 changes: 0 additions & 62 deletions rpc/Demux.ts

This file was deleted.

2 changes: 0 additions & 2 deletions rpc/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,3 @@ export * from "./Base.ts";
export * from "./common.ts";
export * from "./messages.ts";
export * from "./providers/proxy.ts";
export * from "./providers/smoldot.ts";
export * from "./providers/std.ts";
Loading

0 comments on commit c129d6d

Please sign in to comment.