Skip to content

Commit

Permalink
Update decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
jribbink committed Nov 29, 2024
1 parent 088eeac commit 42a95f4
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 19 deletions.
56 changes: 46 additions & 10 deletions packages/transport-http/src/subscribe/handlers/blocks.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,66 @@
import {SdkTransport} from "@onflow/typedefs"
import {createSubscriptionHandler} from "./types"

type BlockDataModel = {
block: {
id: string
parent_id: string
height: number
timestamp: string
collection_guarantees: {
collection_id: string
signer_ids: string[]
}[]
block_seals: {
block_id: string
result_id: string
}[]
}
}

export const blocksHandler = createSubscriptionHandler<{
Topic: SdkTransport.SubscriptionTopic.BLOCKS
Args: SdkTransport.SubscriptionArguments<SdkTransport.SubscriptionTopic.BLOCKS>
Data: SdkTransport.SubscriptionData<SdkTransport.SubscriptionTopic.BLOCKS>
RawData: {
data: SdkTransport.SubscriptionData<SdkTransport.SubscriptionTopic.BLOCKS>
}
RawData: BlockDataModel
}>({
topic: SdkTransport.SubscriptionTopic.BLOCKS,
createSubscriber: (initialArgs, onData, onError) => {
let resumeArgs: SdkTransport.SubscriptionArguments<SdkTransport.SubscriptionTopic.BLOCKS> =
initialArgs
{
...initialArgs,
}

return {
sendData(data: {
data: SdkTransport.SubscriptionData<SdkTransport.SubscriptionTopic.BLOCKS>
}) {
const parsedData = data.data
sendData(data: BlockDataModel) {
// Parse the raw data
const parsedData: SdkTransport.SubscriptionData<SdkTransport.SubscriptionTopic.BLOCKS> =
{
block: {
id: data.block.id,
parentId: data.block.parent_id,
height: data.block.height,
timestamp: data.block.timestamp,
collectionGuarantees: data.block.collection_guarantees.map(
guarantee => ({
collectionId: guarantee.collection_id,
signerIds: guarantee.signer_ids,
})
),
blockSeals: data.block.block_seals.map(seal => ({
blockId: seal.block_id,
executionReceiptId: seal.result_id,
})),
},
}

// Update the resume args
resumeArgs = {
start: parsedData.placeholder.length,
block_status: resumeArgs.block_status,
start_block_id: data.block.id,
}

onData(data.data)
onData(parsedData)
},
sendError(error: Error) {
onError(error)
Expand Down
6 changes: 1 addition & 5 deletions packages/typedefs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,6 @@ export type Block = {
* - The details of which nodes executed and sealed the blocks
*/
blockSeals: Array<BlockSeal>
/**
* - The cryptographic signature of the block
*/
signatures: Array<number>
}
export type CollectionGuarantee = {
/**
Expand All @@ -112,7 +108,7 @@ export type CollectionGuarantee = {
/**
* - The signer ids of the block
*/
signerIds: Array<object>
signerIds: Array<string>
}
export type BlockSeal = {
/**
Expand Down
16 changes: 12 additions & 4 deletions packages/typedefs/src/sdk-transport/subscriptions.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
import {Block} from ".."

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

// TODO: PLACEHOLDER - Replace with actual subscription topics
export enum SubscriptionTopic {
BLOCKS = "blocks",
}

export type SubscriptionSchema = {
[SubscriptionTopic.BLOCKS]: SchemaItem<
{
start: number
},
block_status?: number
} & (
| {
start_block_id?: string
}
| {
start_block_height?: number
}
),
{
placeholder: string
block: Block
}
>
}
Expand Down

0 comments on commit 42a95f4

Please sign in to comment.