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 BlockHeaders DataProvider #2035

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

type BlockHeadersArgs =
SdkTransport.SubscriptionArguments<SdkTransport.SubscriptionTopic.BLOCK_HEADERS>

type BlockHeadersData =
SdkTransport.SubscriptionData<SdkTransport.SubscriptionTopic.BLOCK_HEADERS>

type BlockHeadersArgsDto = BlockArgsModel

type BlockHeadersDataDto = {
// TODO: We do not know the data model types yet
header: {
id: string
height: number
timestamp: string
chain_id: string
parent_id: string
collection_guarantees: {
collection_id: string
signer_ids: string[]
}[]
block_seals: {
block_id: string
result_id: string
}[]
}
}

export const blockHeadersHandler = createSubscriptionHandler<{
Topic: SdkTransport.SubscriptionTopic.BLOCK_HEADERS
Args: BlockHeadersArgs
Data: BlockHeadersData
ArgsDto: BlockHeadersArgsDto
DataDto: BlockHeadersDataDto
}>({
topic: SdkTransport.SubscriptionTopic.BLOCK_HEADERS,
createSubscriber: (initialArgs, onData, onError) => {
let resumeArgs: BlockHeadersArgs = {
...initialArgs,
}

return {
onData(data: BlockHeadersDataDto) {
// Parse the raw data
const parsedData: BlockHeadersData = {
// TODO: We do not know the data model types yet
blockHeader: {
id: data.header.id,
height: data.header.height,
timestamp: data.header.timestamp,
chainId: data.header.chain_id,
} as any,
}

// Update the resume args
resumeArgs = {
blockStatus: resumeArgs.blockStatus,
startBlockHeight: data.header.height + 1,
}

onData(parsedData)
},
onError(error: Error) {
onError(error)
},
argsToDto(args: BlockHeadersArgs) {
let encodedArgs: BlockHeadersArgsDto = {
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
},
}
},
})
7 changes: 6 additions & 1 deletion packages/transport-http/src/subscribe/subscribe.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import {SdkTransport} from "@onflow/typedefs"
import {SubscriptionManager} from "./subscription-manager"
import {blocksHandler} from "./handlers/blocks"
import {blockHeadersHandler} from "./handlers/block-headers"
import {blockDigestsHandler} from "./handlers/block-digests"

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

// Map of SubscriptionManager instances by access node URL
let subscriptionManagerMap: Map<
Expand Down
60 changes: 60 additions & 0 deletions packages/typedefs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,66 @@ export type BlockDigest = {
*/
timestamp: string
}
/**
* Header contains all meta-data for a block, as well as a hash representing
* the combined payload of the entire block. It is what consensus nodes agree
* on after validating the contents against the payload hash.
*/
// TODO: We do not know the data model types yet and are waiting for the AN team.
export type BlockHeader = {
/**
* TA chain-specific value to prevent replay attacks.
*/
chainId: string
/**
* - The id of the block
*/
id: string
/**
* - The id of the parent block
*/
parentId: string
/**
* - The height of the block
*/
height: number
/**
* - The hash of the block's payload
*/
payloadHash: string
/**
* - The timestamp of the block
*/
timestamp: string
/**
* - The view of the block
*/
view: number
/**
* - The view of the parent block
*/
parentView: number
/**
* - The bitvector that represents all the voters for the parent block
*/
parentVoterIndices: string
/**
* - The aggregated signature over the parent block
*/
parentVoterSigData: string
/**
* - The proposer id of the block
*/
proposerId: string
/**
* - The aggregated signature over the block
*/
proposerSigData: string
/**
* - The last view timeout certificate
*/
lastViewTC: any
}
export type CompositeSignature = {
/**
* - A type identifier used internally by FCL
Expand Down
9 changes: 8 additions & 1 deletion packages/typedefs/src/sdk-transport/subscriptions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Block, BlockDigest} from ".."
import {Block, BlockDigest, BlockHeader} from ".."

export type SubscriptionSchema = {
[SubscriptionTopic.BLOCKS]: SchemaItem<
Expand All @@ -13,11 +13,18 @@ export type SubscriptionSchema = {
blockDigest: BlockDigest
}
>
[SubscriptionTopic.BLOCK_HEADERS]: SchemaItem<
BlockArgs,
{
blockHeader: BlockHeader
}
>
}

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

type BlockArgs =
Expand Down
Loading