-
Notifications
You must be signed in to change notification settings - Fork 0
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
chore: enable strict type checking #80
base: main
Are you sure you want to change the base?
Changes from all commits
c1ecd45
f3621d6
ca64e6d
819d822
ec40349
a2f4e92
71b844a
6861130
3882f4b
194bdca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ import { convertBlockEventToActiveDealDbEntry } from './utils.js' | |
/** | ||
* @param {number} blockHeight | ||
* @param {Queryable} pgPool | ||
* @param {(method:string,params:object) => object} makeRpcRequest | ||
* @param {(method:string,params:any[]) => Promise<any>} makeRpcRequest | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the code we are validating the response from the RPC request with typebox, so there is no need to infer a type with the function |
||
* @returns {Promise<void>} | ||
*/ | ||
export async function observeBuiltinActorEvents (blockHeight, pgPool, makeRpcRequest) { | ||
|
@@ -94,11 +94,11 @@ export async function storeActiveDeals (activeDeals, pgPool) { | |
/** | ||
* @param {Queryable} pgPool | ||
* @param {string} query | ||
* @param {Array} args | ||
* @param {Array<number | string>} args | ||
* @returns {Promise<Array<Static <typeof ActiveDealDbEntry>>>} | ||
*/ | ||
export async function loadDeals (pgPool, query, args = []) { | ||
const result = (await pgPool.query(query, args)).rows.map(deal => { | ||
const result = (await pgPool.query(query, args)).rows.map((/** @type {any} */ deal) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The deal is validated shortly after this call using typebox. This means that it is safe to use any type. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @bajtos what do you think about introducing a new type, called |
||
// SQL used null, typebox needs undefined for null values | ||
Object.keys(deal).forEach(key => { | ||
if (deal[key] === null) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,10 +38,17 @@ const RpcRespone = Type.Object({ | |
result: Type.Any() | ||
}) | ||
|
||
const ChainHead = Type.Object({ | ||
Height: Type.Number(), | ||
Blocks: Type.Any(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We are not really interested in Blocks or CIDs, but only the Height of the Chainhead. We could follow this up with a PR that adds explicit types for Blocks and Cids, but I believe it is low priority at this point. |
||
Cids: Type.Any() | ||
}) | ||
|
||
export { | ||
ClaimEvent, | ||
Entry, | ||
RawActorEvent, | ||
BlockEvent, | ||
RpcRespone | ||
RpcRespone, | ||
ChainHead | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ import { encode as cborEncode } from '@ipld/dag-cbor' | |
import { rawEventEntriesToEvent } from './utils.js' | ||
import { Value } from '@sinclair/typebox/value' | ||
import * as util from 'node:util' | ||
import { ClaimEvent, RawActorEvent, BlockEvent, RpcRespone } from './data-types.js' | ||
import { ClaimEvent, RawActorEvent, BlockEvent, RpcRespone, ChainHead } from './data-types.js' | ||
import pRetry from 'p-retry' | ||
/** @import { Static } from '@sinclair/typebox' */ | ||
|
||
|
@@ -40,8 +40,9 @@ export const rpcRequest = async (method, params) => { | |
} | ||
} | ||
/** | ||
* @param {object} actorEventFilter | ||
* @param {{fromHeight:number,toHeight:number,fields: any}} actorEventFilter | ||
* Returns actor events filtered by the given actorEventFilter | ||
* @param {(method: string, params: any[]) => Promise<any>} makeRpcRequest | ||
* @returns {Promise<Array<Static<typeof BlockEvent>>>} | ||
*/ | ||
export async function getActorEvents (actorEventFilter, makeRpcRequest) { | ||
|
@@ -52,7 +53,7 @@ export async function getActorEvents (actorEventFilter, makeRpcRequest) { | |
} | ||
// TODO: handle reverted events | ||
// https://github.com/filecoin-station/deal-observer/issues/22 | ||
const typedRawEventEntries = rawEvents.map((rawEvent) => Value.Parse(RawActorEvent, rawEvent)) | ||
const typedRawEventEntries = rawEvents.map((/** @type {any} */ rawEvent) => Value.Parse(RawActorEvent, rawEvent)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rawEvents are parsed right after this point, so it is safe to use any. |
||
// An emitted event contains the height at which it was emitted, the emitter and the event itself | ||
const emittedEvents = [] | ||
for (const typedEventEntries of typedRawEventEntries) { | ||
|
@@ -81,10 +82,15 @@ export async function getActorEvents (actorEventFilter, makeRpcRequest) { | |
|
||
/** | ||
* @param {function} makeRpcRequest | ||
* @returns {Promise<object>} | ||
* @returns {Promise<Static<typeof ChainHead>>} | ||
*/ | ||
export async function getChainHead (makeRpcRequest) { | ||
return await makeRpcRequest('Filecoin.ChainHead', []) | ||
const result = await makeRpcRequest('Filecoin.ChainHead', []) | ||
try { | ||
return Value.Parse(ChainHead, result) | ||
} catch (e) { | ||
throw Error(util.format('Failed to parse chain head: %o', result)) | ||
} | ||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,10 @@ import { base64pad } from 'multiformats/bases/base64' | |
import { decode as cborDecode } from '@ipld/dag-cbor' | ||
import * as util from 'node:util' | ||
|
||
/** | ||
* @param {string} data | ||
* @returns | ||
*/ | ||
const decodeCborInBase64 = (data) => { | ||
return cborDecode(base64pad.baseDecode(data)) | ||
} | ||
|
@@ -14,6 +18,7 @@ const decodeCborInBase64 = (data) => { | |
*/ | ||
const rawEventEntriesToEvent = (rawEventEntries) => { | ||
// Each event is defined by a list of event entries which will parsed into a typed event | ||
/** @type {Record<string, any>} */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We do not yet know the event type at this point, which is why I used any as a type declaration here. |
||
const event = {} | ||
let eventType | ||
for (const entry of rawEventEntries) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ import * as Sentry from '@sentry/node' | |
* | ||
* @param {PgPool} pgPool | ||
* @param {number} batchSize | ||
* @param {(eligibleDeals: Array) => Promise<{ingested: number; skipped: number}>} submitDeals | ||
* @param {(eligibleDeals: Array<any>) => Promise<{ingested: number; skipped: number}>} submitDeals | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would suggest following up the explicit typing for eligible deals with another PR, as this would make the existing PR a lot bigger. WDYT? |
||
* @returns {Promise<{submitted: number; ingested: number; skipped: number;}>} Number of deals submitted, ingested and skipped | ||
*/ | ||
export const findAndSubmitUnsubmittedDeals = async (pgPool, batchSize, submitDeals) => { | ||
|
@@ -45,7 +45,7 @@ export const findAndSubmitUnsubmittedDeals = async (pgPool, batchSize, submitDea | |
* | ||
* @param {PgPool} pgPool | ||
* @param {number} batchSize | ||
* @returns {AsyncGenerator<Array>} | ||
* @returns {AsyncGenerator<Array<any>>} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as here. |
||
*/ | ||
const findUnsubmittedDeals = async function * (pgPool, batchSize) { | ||
const client = await pgPool.connect() | ||
|
@@ -82,7 +82,7 @@ const findUnsubmittedDeals = async function * (pgPool, batchSize) { | |
* Mark deals as submitted. | ||
* | ||
* @param {Queryable} pgPool | ||
* @param {Array} eligibleDeals | ||
* @param {Array<any>} eligibleDeals | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as here. |
||
*/ | ||
const markDealsAsSubmitted = async (pgPool, eligibleDeals) => { | ||
await pgPool.query(` | ||
|
@@ -112,7 +112,7 @@ const markDealsAsSubmitted = async (pgPool, eligibleDeals) => { | |
* | ||
* @param {string} sparkApiBaseURL | ||
* @param {string} sparkApiToken | ||
* @param {Array} deals | ||
* @param {Array<any>} deals | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as here. |
||
* @returns {Promise<{ingested: number; skipped: number}>} | ||
*/ | ||
export const submitDealsToSparkApi = async (sparkApiBaseURL, sparkApiToken, deals) => { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The RPC call can return multiple different types of responses which are usually validated using TypeBox right after they return.
The parameters also differ in each request which is why the any type was used.