Skip to content

Commit

Permalink
feat(ipfs): normalize node height
Browse files Browse the repository at this point in the history
  • Loading branch information
bludnic committed Jan 11, 2025
1 parent 488094d commit 4312f57
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/lib/nodes/ipfs/IpfsNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { NodeOfflineError } from '@/lib/nodes/utils/errors'
import axios, { AxiosInstance, AxiosProgressEvent, AxiosRequestConfig, ResponseType } from 'axios'
import { Node } from '@/lib/nodes/abstract.node'
import { NODE_LABELS } from '@/lib/nodes/constants'
import { normalizeHeight } from './utils'

type FetchNodeInfoResult = {
availableSizeInMb: number
Expand Down Expand Up @@ -117,9 +118,10 @@ export class IpfsNode extends Node<AxiosInstance> {
protected async checkHealth() {
const time = Date.now()
const { timestamp } = await this.fetchNodeInfo()
this.height = normalizeHeight(timestamp)

return {
height: timestamp,
height: this.height,
ping: Date.now() - time
}
}
Expand Down
14 changes: 14 additions & 0 deletions src/lib/nodes/ipfs/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Normalize the height to 8 digits.
* Rounds the timestamp to seconds and removes the first two digits.
* @param timestamp Timestamp in milliseconds
*/
export const normalizeHeight = (timestamp: number) => {
if (!timestamp) return 0

return Number(
Math.ceil(timestamp / 1000)
.toString()
.substring(2)
)
}

0 comments on commit 4312f57

Please sign in to comment.