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

feat: RPC client re-write #341

Merged
merged 72 commits into from
Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from 64 commits
Commits
Show all changes
72 commits
Select commit Hold shift + click to select a range
e20a3a9
continued
harrysolovay Oct 26, 2022
e0e7ad8
cleanup
harrysolovay Oct 27, 2022
393b427
move send error into listener
harrysolovay Oct 27, 2022
bbc8535
cleanup
harrysolovay Oct 27, 2022
bda31ec
whitespace deletion
harrysolovay Oct 27, 2022
09df54d
some renames
harrysolovay Oct 27, 2022
cce1462
Feat/rpc client (#339)
kratico Oct 28, 2022
01bc77b
fix: CreateListener types
kratico Oct 28, 2022
7827663
DRAFT
kratico Oct 28, 2022
6479853
fix: bound listener type
kratico Oct 28, 2022
0c909b8
feat: remove first listener
kratico Oct 28, 2022
cb44ec8
fix: incomming subscription message after subscription.stop()
kratico Oct 28, 2022
da7a67b
misc cleanup
harrysolovay Oct 28, 2022
f046930
type cleanup, renames and ids creation method
harrysolovay Oct 29, 2022
9e6c1b1
misc cleanup
harrysolovay Oct 29, 2022
3c959d8
misc cleanup
harrysolovay Oct 29, 2022
b07b3fb
reverse type param order
harrysolovay Oct 29, 2022
448ea03
misc cleanup
harrysolovay Oct 29, 2022
f8471ff
misc cleanup
harrysolovay Oct 30, 2022
994aa7a
a file rename and rpc mod.ts
harrysolovay Oct 30, 2022
1d98f6d
fix runtime error
harrysolovay Oct 30, 2022
ae6f987
close -> discard
harrysolovay Oct 30, 2022
dba44e7
remove redundant type annotations
harrysolovay Oct 30, 2022
a27a7eb
get rid of mod in provider dir
harrysolovay Oct 30, 2022
2690c5e
some renames
harrysolovay Oct 30, 2022
bd46b34
clean up example
harrysolovay Oct 30, 2022
707c26e
refine example
harrysolovay Oct 30, 2022
9a13dec
export provider error ctors
harrysolovay Oct 30, 2022
3c77c42
add means of persisting state in listener via ctx
harrysolovay Oct 30, 2022
c5a16cb
clean up example
harrysolovay Oct 30, 2022
42b0a79
rename member
harrysolovay Oct 30, 2022
b62f86a
cleanup
harrysolovay Oct 30, 2022
182a785
hi matias
harrysolovay Oct 30, 2022
fb77a6a
cleanup
harrysolovay Oct 30, 2022
9c26219
feat: add proxy provider unit test
kratico Oct 31, 2022
c89d913
feat: add rpc client unit test
kratico Oct 31, 2022
f0797a5
feat: bypass sanitizeOps checks on individual test steps in RPC client
kratico Oct 31, 2022
9d173a9
fix: incomming subscription message after subscription.stop()
kratico Oct 31, 2022
4e6fa4b
fix: cleanup activeSubscriptionByMessageId
kratico Oct 31, 2022
17b41c6
feat: cleanup client state
kratico Oct 31, 2022
da16b73
feat: add subscription cleanup
kratico Nov 1, 2022
bc8b783
fix: client effect dependencies
kratico Nov 1, 2022
e78e9a6
fix: client effect return value
kratico Nov 1, 2022
87c5145
feat: update subscription api
kratico Nov 1, 2022
c55dcee
clean up typings and add todos
harrysolovay Nov 1, 2022
037b591
add header typing to subscription example
harrysolovay Nov 1, 2022
00012e1
fix typo
harrysolovay Nov 1, 2022
eeb752b
continued
harrysolovay Nov 1, 2022
494f645
continued
harrysolovay Nov 1, 2022
d9678d8
add zones rc usage, misc. cleanup
harrysolovay Nov 2, 2022
ad64b8c
type cleanup
harrysolovay Nov 2, 2022
51a11a7
continued
harrysolovay Nov 2, 2022
7a65ab6
clean up known and add note about known/rpc
harrysolovay Nov 2, 2022
3317f48
new import specifier
harrysolovay Nov 2, 2022
97a2159
fix watch_blocks_iter example
harrysolovay Nov 2, 2022
891e7e0
smoldot provider impl
harrysolovay Nov 2, 2022
fe1173b
continued
harrysolovay Nov 2, 2022
ec0b194
delete chainspec
harrysolovay Nov 2, 2022
897c39c
get rid of unused import
harrysolovay Nov 2, 2022
b7951d7
misc
harrysolovay Nov 2, 2022
6e0ad8c
misc
harrysolovay Nov 2, 2022
c06e32d
misc
harrysolovay Nov 2, 2022
c9877c8
misc
harrysolovay Nov 2, 2022
2e884fd
misc cleanup
harrysolovay Nov 2, 2022
0b6fcb5
Update rpc_new/provider/base.ts
harrysolovay Nov 3, 2022
cd5d1e9
Update rpc_new/provider/smoldot.ts
harrysolovay Nov 3, 2022
6cb643d
address some of T6's feedback
harrysolovay Nov 3, 2022
aa94baf
fix type errors relating to void vs. undefined
harrysolovay Nov 3, 2022
bc0fe3b
feat: update smoldot unit test
kratico Nov 3, 2022
8782f4c
tiny changes
harrysolovay Nov 3, 2022
68d7667
Merge branch 'main' into rpc-round-2
kratico Nov 3, 2022
3b427eb
tiny changes
harrysolovay Nov 3, 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
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