Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PKG -- [transport-http] Add BlockDigests DataProvider #2034

Draft
wants to merge 6 commits into
base: jribbink/blocks
Choose a base branch
from
Draft
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
82 changes: 82 additions & 0 deletions packages/transport-http/src/subscribe/handlers/block-digests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import {SdkTransport} from "@onflow/typedefs"
import {BlockArgsModel, createSubscriptionHandler} from "./types"

type BlockDigestsArgs =
SdkTransport.SubscriptionArguments<SdkTransport.SubscriptionTopic.BLOCK_DIGESTS>

type BlockDigestsData =
SdkTransport.SubscriptionData<SdkTransport.SubscriptionTopic.BLOCK_DIGESTS>

type BlockDigestsDataDto = {
// TODO: We do not know the data model types yet
block_digest: {
id: string
height: number
timestamp: string
}
}

type BlockDigestsArgsDto = BlockArgsModel

export const blockDigestsHandler = createSubscriptionHandler<{
Topic: SdkTransport.SubscriptionTopic.BLOCK_DIGESTS
Args: BlockDigestsArgs
Data: BlockDigestsData
ArgsDto: BlockDigestsArgsDto
DataDto: BlockDigestsDataDto
}>({
topic: SdkTransport.SubscriptionTopic.BLOCK_DIGESTS,
createSubscriber: (initialArgs, onData, onError) => {
let resumeArgs: BlockDigestsArgs = {
...initialArgs,
}

return {
onData(data: BlockDigestsDataDto) {
// Parse the raw data
const parsedData: BlockDigestsData = {
blockDigest: {
id: data.block_digest.id,
height: data.block_digest.height,
timestamp: data.block_digest.timestamp,
},
}

// Update the resume args
resumeArgs = {
blockStatus: resumeArgs.blockStatus,
startBlockId: data.block_digest.id + 1,
}

onData(parsedData)
},
onError(error: Error) {
onError(error)
},
argsToDto(args: BlockDigestsArgs) {
let encodedArgs: BlockDigestsArgsDto = {
block_status: args.blockStatus,
}

if ("startBlockHeight" in args) {
return {
...encodedArgs,
start_block_height: args.startBlockHeight,
}
}

if ("startBlockId" in args) {
return {
...encodedArgs,
start_block_id: args.startBlockId,
}
}

return encodedArgs
},
get connectionArgs() {
return resumeArgs
},
}
},
})
2 changes: 1 addition & 1 deletion packages/transport-http/src/subscribe/handlers/blocks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {SdkTransport} from "@onflow/typedefs"
import {createSubscriptionHandler} from "./types"
import {createSubscriptionHandler, BlockArgsModel} from "./types"

type BlocksArgs =
SdkTransport.SubscriptionArguments<SdkTransport.SubscriptionTopic.BLOCKS>
Expand Down
10 changes: 10 additions & 0 deletions packages/transport-http/src/subscribe/handlers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,13 @@ export function createSubscriptionHandler<
>(handler: SubscriptionHandler<T>): SubscriptionHandler<T> {
return handler
}

export type BlockArgsModel =
| {
block_status?: number
start_block_id?: string
}
| {
block_status?: number
start_block_height?: number
}
3 changes: 2 additions & 1 deletion packages/transport-http/src/subscribe/subscribe.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import {SdkTransport} from "@onflow/typedefs"
import {SubscriptionManager} from "./subscription-manager"
import {blocksHandler} from "./handlers/blocks"
import {blockDigestsHandler} from "./handlers/block-digests"

const SUBSCRIPTION_HANDLERS = [blocksHandler]
const SUBSCRIPTION_HANDLERS = [blocksHandler, blockDigestsHandler]

// Map of SubscriptionManager instances by access node URL
let subscriptionManagerMap: Map<
Expand Down
17 changes: 17 additions & 0 deletions packages/typedefs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,23 @@ export type BlockSeal = {
*/
executionReceiptId: string
}
/**
* BlockDigest holds lightweight block information which includes only block id, block height and block timestamp.
*/
export type BlockDigest = {
/**
* - The id of the block
*/
id: string
/**
* - The height of the block
*/
height: number
/**
* - Timestamp of the block
*/
timestamp: string
}
export type CompositeSignature = {
/**
* - A type identifier used internally by FCL
Expand Down
44 changes: 27 additions & 17 deletions packages/typedefs/src/sdk-transport/subscriptions.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,38 @@
import {Block} from ".."
import {Block, BlockDigest} from ".."

type SchemaItem<TArgs, TData> = {
args: TArgs
data: TData
export type SubscriptionSchema = {
[SubscriptionTopic.BLOCKS]: SchemaItem<
BlockArgs,
{
block: Block
}
>
[SubscriptionTopic.BLOCK_DIGESTS]: SchemaItem<
BlockArgs,
{
blockDigest: BlockDigest
}
>
}

export enum SubscriptionTopic {
BLOCKS = "blocks",
BLOCK_DIGESTS = "block_digests",
}

export type SubscriptionSchema = {
[SubscriptionTopic.BLOCKS]: SchemaItem<
| {
blockStatus?: number
startBlockId?: string
}
| {
blockStatus?: number
startBlockHeight?: number
},
{
block: Block
type BlockArgs =
| {
blockStatus?: number
startBlockId?: string
}
>
| {
blockStatus?: number
startBlockHeight?: number
}

type SchemaItem<TArgs, TData> = {
args: TArgs
data: TData
}

export type SubscriptionArguments<T extends SubscriptionTopic> =
Expand Down
Loading