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

chore: enable strict type checking #80

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

NikolasHaimerl
Copy link
Contributor

@NikolasHaimerl NikolasHaimerl commented Feb 4, 2025

This PR proposes the following changes:

  1. Enable strict type checking on the deal observer repository

Closes #51 .

The proposed solution enables strict type checking for the entire repository.
Wherever possible explicit types are used. In some cases the any time was used and I commented on my reasoning for doing so in this PR.

@@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 makeRpcRequest.

* @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) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bajtos what do you think about introducing a new type, called ToBeParsed or similar, which equals any? This way we don't have any any, and can forbid it, but at the same time, signal that this type needs to be parsed, and will be parsed.

@@ -38,10 +38,17 @@ const RpcRespone = Type.Object({
result: Type.Any()
})

const ChainHead = Type.Object({
Height: Type.Number(),
Blocks: Type.Any(),
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.
To keep this already big PR reviewable, I set this to Any.

@@ -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))
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

@@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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?

@@ -45,7 +45,7 @@ export const findAndSubmitUnsubmittedDeals = async (pgPool, batchSize, submitDea
*
* @param {PgPool} pgPool
* @param {number} batchSize
* @returns {AsyncGenerator<Array>}
* @returns {AsyncGenerator<Array<any>>}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as here.

@@ -82,7 +82,7 @@ const findUnsubmittedDeals = async function * (pgPool, batchSize) {
* Mark deals as submitted.
*
* @param {Queryable} pgPool
* @param {Array} eligibleDeals
* @param {Array<any>} eligibleDeals
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as here.

@@ -112,7 +112,7 @@ const markDealsAsSubmitted = async (pgPool, eligibleDeals) => {
*
* @param {string} sparkApiBaseURL
* @param {string} sparkApiToken
* @param {Array} deals
* @param {Array<any>} deals
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as here.

@@ -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>} */
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

@NikolasHaimerl NikolasHaimerl marked this pull request as ready for review February 4, 2025 10:59
@NikolasHaimerl NikolasHaimerl requested review from juliangruber and bajtos and removed request for bajtos February 4, 2025 11:00
@juliangruber
Copy link
Member

See #51.

@NikolasHaimerl if you reword this to Closes #51, then GitHub will close the issue when this PR gets merged.

@@ -37,6 +38,10 @@ assert(finalityEpochs <= maxPastEpochs)
const pgPool = await createPgPool()
const { recordTelemetry } = createInflux(INFLUXDB_TOKEN)

/**
* @param {(method:string,params:any[]) => Promise<any>} makeRpcRequest
Copy link
Contributor Author

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.

@juliangruber
Copy link
Member

I suggest to let TS Wiz @bajtos handle this review 🙏

@juliangruber juliangruber removed their request for review February 5, 2025 15:18
@NikolasHaimerl NikolasHaimerl mentioned this pull request Feb 7, 2025
59 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Enable strict TypeScript checks
2 participants