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

feat: add smoldot known methods types #405

Merged
merged 3 commits into from
Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions rpc/known/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export * from "./mmr.ts"
export * from "./mod.ts"
export * from "./offchain.ts"
export * from "./payment.ts"
export * as smoldot from "./smoldot.ts"
export * from "./state.ts"
export * from "./statemigration.ts"
export * from "./system.ts"
Expand Down
123 changes: 123 additions & 0 deletions rpc/known/smoldot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import { Hash, Hex, RpcResult, Subscription } from "./utils.ts"

export type NetworkConfig = {
totalAttempts: number
maxParallel: number
timeoutMs: number
}

export type MaybeRuntimeSpec = {
type: "valid"
spec: RuntimeSpec
} | {
type: "invalid"
error: string
}
export type RuntimeSpec = {
specName: string
implName: string
authoringVersion: number
specVersion: number
implVersion: number
transactionVersion?: number
apis: Record<Hex, number>
}

export type ChainHeadUnstableFollowEvent = {
event: "initialized"
finalizedBlockHash: Hash
finalizedBlockRuntime?: MaybeRuntimeSpec
} | {
event: "newBlock"
blockHash: Hash
parentBlockHash: Hash
newRuntime?: MaybeRuntimeSpec
} | {
event: "bestBlockChanged"
bestBlockHash: Hash
} | {
event: "finalized"
finalizedBlockHashes: Hash[]
prunedBlockHashes: Hash[]
} | {
event: "stop"
}

export type TransactionWatchEvent = {
event: "validated"
} | {
event: "broadcasted"
numPeers: number
} | {
event: "bestChainBlockIncluded"
block?: TransactionWatchEventBlock
} | {
event: "finalized"
block: TransactionWatchEventBlock
} | {
event: "error"
error: string
} | {
event: "invalid"
error: string
} | {
event: "dropped"
broadcasted: boolean
error: string
}

export type TransactionWatchEventBlock = {
hash: Hash
index: string
}

export type SmoldotRpc = {
chainHead_unstable_body(
followSubscription: string,
hash: Hash,
networkConfig?: NetworkConfig,
): RpcResult<string>
chainHead_unstable_call(
followSubscription: string,
hash: Hash,
fn: string,
callParameters: Hex,
networkConfig?: NetworkConfig,
): RpcResult<string>
chainHead_unstable_follow(
runtimeUpdates: boolean,
): RpcResult<Subscription<"chainHead_unstable_follow", ChainHeadUnstableFollowEvent>>
chainHead_unstable_genesisHash(): RpcResult<Hash>
chainHead_unstable_header(followSubscription: string, hash: Hash): RpcResult<Hex>
chainHead_unstable_stopBody(subscription: string): RpcResult<void>
chainHead_unstable_stopCall(subscription: string): RpcResult<void>
chainHead_unstable_stopStorage(subscription: string): RpcResult<void>
chainHead_unstable_storage(
followSubscription: string,
hash: Hash,
key: Hex,
childKey?: Hex,
networkConfig?: NetworkConfig,
): RpcResult<string>
chainHead_unstable_unfollow(followSubscription: string): RpcResult<void>
chainHead_unstable_unpin(followSubscription: string, hash: Hash): RpcResult<void>
chainSpec_unstable_chainName(): RpcResult<string>
chainSpec_unstable_genesisHash(): RpcResult<Hash>
chainSpec_unstable_properties(): RpcResult<Record<string, unknown>>
sudo_unstable_p2pDiscover(multiaddr: string): RpcResult<void>
sudo_unstable_version(): RpcResult<string>
transaction_unstable_submitAndWatch(
transaction: Hex,
): RpcResult<Subscription<"transaction_unstable_submitAndWatch", TransactionWatchEvent>>
transaction_unstable_unwatch(subscription: string): RpcResult<void>
chainHead_unstable_finalizedDatabase(maxSizeBytes?: bigint): RpcResult<string>
}

// TODO: do we even care about narrowing error code?
export type ParseErrorCode = -32700
export type InvalidRequestCode = -32600
export type MethodNotFoundCode = -32601
export type InvalidParamsCode = -32602
export type InternalErrorCode = -32603
export type ServerErrorCode = number /*[-32099..=-32000]*/
export type MethodErrorCode = number /* [-32700..=-32000]*/
7 changes: 5 additions & 2 deletions rpc/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ export interface OkMessage<Result = any> extends JsonRpcVersionBearer {
error?: never
}

export interface ErrorMessage<Data = any> extends JsonRpcVersionBearer {
export interface ErrorMessage<
Code extends number = number,
Data = any,
> extends JsonRpcVersionBearer {
id: string
error: {
code: number
code: Code
message: string
data: Data
}
Expand Down