Skip to content

Commit

Permalink
begin adding blockheader type
Browse files Browse the repository at this point in the history
  • Loading branch information
jribbink committed Nov 29, 2024
1 parent 9229a65 commit ad02897
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 2 deletions.
93 changes: 93 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,93 @@
import {SdkTransport} from "@onflow/typedefs"
import {BlockArgsModel, createSubscriptionHandler} from "./types"

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

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

type BlockHeaderArgsModel = BlockArgsModel

type BlockHeaderDataModel = {
// 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 blockHeaderHandler = createSubscriptionHandler<{
Topic: SdkTransport.SubscriptionTopic.BLOCK_HEADERS
Args: BlockHeaderArgs
Data: BlockHeaderData
ArgsModel: BlockHeaderArgsModel
DataModel: BlockHeaderDataModel
}>({
topic: SdkTransport.SubscriptionTopic.BLOCK_HEADERS,
createSubscriber: (initialArgs, onData, onError) => {
let resumeArgs: BlockHeaderArgs = {
...initialArgs,
}

return {
sendData(data: BlockHeaderDataModel) {
// Parse the raw data
const parsedData: BlockHeaderData = {
blockHeader: {
id: data.header.id,
height: data.header.height,
timestamp: data.header.timestamp,
chainId: data.header.chain_id,
},
}

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

onData(parsedData)
},
sendError(error: Error) {
onError(error)
},
encodeArgs(args: BlockHeaderArgs) {
let encodedArgs: BlockHeaderArgsModel = {
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
Expand Up @@ -2,8 +2,13 @@ import {SdkTransport} from "@onflow/typedefs"
import {SubscriptionManager} from "./subscription-manager"
import {blocksHandler} from "./handlers/blocks"
import {blockDigestsHandler} from "./handlers/block_digests"
import {blockHeaderHandler} from "./handlers/block-headers"

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

// 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

0 comments on commit ad02897

Please sign in to comment.