This repository has been archived by the owner on Sep 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: clean up test node, begin watch-related, cleanup (#149)
- Loading branch information
1 parent
f19b58f
commit 8c68b84
Showing
21 changed files
with
166 additions
and
230 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,7 @@ export class Signed< | |
this.chain = call.chain; | ||
} | ||
|
||
send() { | ||
send = async () => { | ||
return send(this); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import * as C from "../mod.ts"; | ||
|
||
const result = await C.westend | ||
.pallet("System") | ||
.entry("Events") | ||
.watch(console.log); | ||
|
||
setTimeout(() => { | ||
if (typeof result === "function") { | ||
result(); | ||
} | ||
}, 10000); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,60 @@ | ||
import { Hashers } from "../bindings/mod.ts"; | ||
import * as core from "../core/mod.ts"; | ||
import * as M from "../frame_metadata/mod.ts"; | ||
import * as rpc from "../rpc/mod.ts"; | ||
import * as U from "../util/mod.ts"; | ||
import { globalContext } from "./Context.ts"; | ||
|
||
export type WatchTarget = core.Entry | core.KeyPage | core.Metadata | core.Header | core.Block; | ||
|
||
export declare function watch<Target extends WatchTarget>( | ||
export async function watch<Target extends WatchTarget>( | ||
target: Target, | ||
): AsyncIterableIterator<unknown>; | ||
listener: (notification: rpc.NotifMessage<U.AnyMethods>) => void, | ||
): Promise<(() => void) | Error> { | ||
const chain = await globalContext.register(target.chain.beacon as any); | ||
if (chain instanceof Error) { | ||
return chain; | ||
} | ||
const group = await chain.load(); | ||
switch (target.kind) { | ||
case "Entry": { | ||
const pallet = group.lookup.getPalletByName(target.pallet.name); | ||
const storageEntry = group.lookup.getStorageEntryByPalletAndName(pallet, target.name); | ||
const $key = M.$storageKey({ | ||
deriveCodec: group.deriveCodec, | ||
hashers: await Hashers(), | ||
pallet, | ||
storageEntry, | ||
}); | ||
const key = $key.encode(target.keys.length === 1 ? target.keys[0]! : target.keys); | ||
const keyEncoded = U.hex.encode(key) as U.HexString; | ||
const $value = group.deriveCodec(storageEntry.value); | ||
const outerListener = (message: rpc.NotifMessage<U.AnyMethods>) => { | ||
const { result } = message.params; | ||
const changes = (result as any).changes; | ||
const changesDecoded = changes.map(([key, value]: [any, any]) => { | ||
return [ | ||
$key.decode(U.hex.decode(key.substring(2))), | ||
$value.decode(U.hex.decode(value.substring(2))), | ||
]; | ||
}); | ||
listener(changesDecoded); | ||
}; | ||
const raw = await chain.rpcClient.subscribe( | ||
"state_subscribeStorage", | ||
[[keyEncoded]], | ||
outerListener, | ||
); | ||
if (typeof raw === "function") { | ||
return async () => { | ||
raw(); | ||
await chain.release(); | ||
}; | ||
} | ||
return new Error(); | ||
} | ||
default: { | ||
throw new Error(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
import { Chain } from "../core/Chain.ts"; | ||
import { KnownRpcMethods } from "../known/mod.ts"; | ||
import { ProxyBeacon } from "../rpc/mod.ts"; | ||
import { SubstrateNode } from "../util/mod.ts"; | ||
import { TestAddresses } from "./Addresses.ts"; | ||
import { TestNode } from "./node.ts"; | ||
|
||
export class TestChain extends Chain<ProxyBeacon<KnownRpcMethods>> { | ||
override address: TestAddresses<this> = new TestAddresses(this); | ||
} | ||
|
||
export function chain(process: SubstrateNode) { | ||
return new TestChain(new ProxyBeacon(process.url)); | ||
export function chain(node: TestNode) { | ||
return new TestChain(new ProxyBeacon(node.url)); | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,62 @@ | ||
import { assert } from "../_deps/asserts.ts"; | ||
import { SubstrateNode, substrateNode } from "../util/mod.ts"; | ||
import { fail } from "../_deps/asserts.ts"; | ||
import { blue } from "../_deps/colors.ts"; | ||
import { isPortAvailable } from "../util/mod.ts"; | ||
|
||
export async function node(): Promise<SubstrateNode> { | ||
const process = await substrateNode({ | ||
path: "./node-template", | ||
cwd: new URL(".", import.meta.url).pathname, | ||
dev: true, | ||
}); | ||
assert(!(process instanceof Error)); | ||
return process; | ||
export interface TestNodeConfig { | ||
cwd?: string; | ||
altRuntime?: "kusama" | "rococo" | "westend"; | ||
} | ||
|
||
export interface TestNode { | ||
config?: TestNodeConfig; | ||
inner: Deno.Process; | ||
close(): void; | ||
url: string; | ||
} | ||
|
||
let port = 9944; | ||
export async function node(config?: TestNodeConfig): Promise<TestNode> { | ||
while (!isPortAvailable(port)) { | ||
port++; | ||
} | ||
try { | ||
const process = Deno.run({ | ||
cmd: [ | ||
"polkadot", | ||
"--dev", | ||
"--ws-port", | ||
port.toString(), | ||
...config?.altRuntime ? [`--force-${config.altRuntime}`] : [], | ||
], | ||
cwd: config?.cwd, | ||
stderr: "piped", | ||
stdout: "piped", | ||
}); | ||
// For some reason, logs come in through `stderr` | ||
const logs = process.stderr.readable; | ||
await (async () => { | ||
console.log(blue(`Piping node logs:`)); | ||
for await (const log of logs) { | ||
Deno.stdout.write(log); | ||
if (new TextDecoder().decode(log).includes(" Running JSON-RPC WS server")) { | ||
console.log(blue("Chain and RPC server initialized")); | ||
console.log(blue(`Suspending node logs`)); | ||
return; | ||
} | ||
} | ||
})(); | ||
return { | ||
config, | ||
inner: process, | ||
close: () => { | ||
process.close(); | ||
}, | ||
url: `ws://127.0.0.1:${port}`, | ||
}; | ||
} catch (e) { | ||
if (e instanceof Deno.errors.NotFound) { | ||
fail("Must have Polkadot installed locally. Visit https://github.com/paritytech/polkadot."); | ||
} | ||
fail(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export function isPortAvailable(port: number): boolean { | ||
try { | ||
const listener = Deno.listen({ | ||
transport: "tcp", | ||
hostname: "127.0.0.1", | ||
port, | ||
}); | ||
listener.close(); | ||
return true; | ||
} catch (error) { | ||
if (error instanceof Deno.errors.AddrInUse) { | ||
return false; | ||
} | ||
throw error; | ||
} | ||
} |
Oops, something went wrong.