-
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
feat: add blockmeta block number provider #37
Changes from all commits
da24531
719df23
e2d952f
4819329
2b793cb
c56901f
80ee81f
80e05bf
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export class BlockmetaConnectionFailed extends Error { | ||
constructor() { | ||
super(`Could not establish connection to blockmeta.`); | ||
|
||
this.name = "BlockmetaConnectionFailed"; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export class UndefinedBlockNumber extends Error { | ||
constructor(isoTimestamp: string) { | ||
super(`Undefined block number at ${isoTimestamp}.`); | ||
|
||
this.name = "UndefinedBlockNumber"; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,214 @@ | ||
import { ILogger, Timestamp } from "@ebo-agent/shared"; | ||
import axios, { | ||
AxiosInstance, | ||
AxiosResponse, | ||
InternalAxiosRequestConfig, | ||
isAxiosError, | ||
} from "axios"; | ||
import { InvalidTokenError, jwtDecode } from "jwt-decode"; | ||
|
||
import { BlockmetaConnectionFailed } from "../exceptions/blockmetaConnectionFailed.js"; | ||
import { UndefinedBlockNumber } from "../exceptions/undefinedBlockNumber.js"; | ||
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 guess we should import these from index.js but we can clean up later 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. Yep, got a task already for that! 👌 |
||
import { BlockNumberProvider } from "./blockNumberProvider.js"; | ||
|
||
type BlockByTimeResponse = { | ||
num: string; | ||
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. maybe this should be blockNum so it's a bit more descriptive--I had to see where it's being used to understand the 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. This is based on the blockmeta |
||
id: string; | ||
time: string; | ||
}; | ||
|
||
export type BlockmetaClientConfig = { | ||
baseUrl: URL; | ||
servicePaths: { | ||
block: string; | ||
blockByTime: string; | ||
}; | ||
bearerToken: string; | ||
bearerTokenExpirationWindow: number; | ||
}; | ||
|
||
/** | ||
* Consumes the blockmeta.BlockByTime substreams' service via HTTP POST JSON requests to provide | ||
* block numbers based on timestamps | ||
* | ||
* Refer to these web pages for more information: | ||
* * https://thegraph.market/ | ||
* * https://substreams.streamingfast.io/documentation/consume/authentication | ||
*/ | ||
export class BlockmetaJsonBlockNumberProvider implements BlockNumberProvider { | ||
private readonly axios: AxiosInstance; | ||
|
||
constructor( | ||
private readonly clientConfig: BlockmetaClientConfig, | ||
private readonly logger: ILogger, | ||
) { | ||
const { baseUrl, bearerToken } = clientConfig; | ||
|
||
this.axios = axios.create({ | ||
baseURL: baseUrl.toString(), | ||
headers: { | ||
common: { | ||
"Content-Type": "application/json", | ||
Authorization: `Bearer ${bearerToken}`, | ||
}, | ||
}, | ||
}); | ||
|
||
this.axios.interceptors.request.use((config) => { | ||
return this.validateBearerToken(config); | ||
}); | ||
} | ||
|
||
public static async initialize( | ||
config: BlockmetaClientConfig, | ||
logger: ILogger, | ||
): Promise<BlockmetaJsonBlockNumberProvider> { | ||
const provider = new BlockmetaJsonBlockNumberProvider(config, logger); | ||
|
||
const connectedSuccessfully = await provider.testConnection(); | ||
|
||
if (!connectedSuccessfully) throw new BlockmetaConnectionFailed(); | ||
|
||
return provider; | ||
} | ||
|
||
async testConnection(): Promise<boolean> { | ||
const blockPath = this.clientConfig.servicePaths.block; | ||
|
||
try { | ||
await this.axios.post(`${blockPath}/Head`); | ||
|
||
return true; | ||
} catch (err) { | ||
if (isAxiosError(err)) return false; | ||
|
||
throw err; | ||
} | ||
} | ||
|
||
private validateBearerToken(requestConfig: InternalAxiosRequestConfig<any>) { | ||
const authorizationHeader = requestConfig.headers.Authorization?.toString(); | ||
const matches = /^Bearer (.*)$/.exec(authorizationHeader || ""); | ||
const token = matches ? matches[1] : undefined; | ||
|
||
try { | ||
const decodedToken = jwtDecode(token || ""); | ||
const currentTime = Date.now(); | ||
const expTime = decodedToken.exp; | ||
|
||
// No expiration time, token will be forever valid. | ||
if (!expTime) { | ||
this.logger.debug("JWT token being used has no expiration time."); | ||
|
||
return requestConfig; | ||
} | ||
|
||
const expirationWindow = this.clientConfig.bearerTokenExpirationWindow; | ||
|
||
if (currentTime + expirationWindow >= expTime) { | ||
const timeRemaining = expTime - currentTime; | ||
|
||
this.logger.warn(`Token will expire soon in ${timeRemaining}`); | ||
|
||
// TODO: notify | ||
} | ||
|
||
return requestConfig; | ||
} catch (err) { | ||
if (err instanceof InvalidTokenError) { | ||
this.logger.error("Invalid JWT token."); | ||
|
||
// TODO: notify | ||
} | ||
|
||
return requestConfig; | ||
} | ||
} | ||
|
||
/** @inheritdoc */ | ||
async getEpochBlockNumber(timestamp: Timestamp): Promise<bigint> { | ||
if (timestamp > Number.MAX_SAFE_INTEGER || timestamp < 0) | ||
throw new RangeError(`Timestamp ${timestamp.toString()} cannot be casted to a Number.`); | ||
|
||
const timestampNumber = Number(timestamp); | ||
const timestampDate = new Date(timestampNumber); | ||
|
||
try { | ||
// Try to get the block number at a specific timestamp | ||
const blockNumberAt = await this.getBlockNumberAt(timestampDate); | ||
|
||
return blockNumberAt; | ||
} catch (err) { | ||
const isAxios404 = isAxiosError(err) && err.response?.status === 404; | ||
const isUndefinedBlockNumber = !!(err instanceof UndefinedBlockNumber); | ||
|
||
if (!isAxios404 && !isUndefinedBlockNumber) throw err; | ||
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. would we want to also handle Unauthorized errors? 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. thinking on this, we could use some ep on constructor (like an echo get or smth simple) to check that credentials are working or else throw on constructor, wdyt? 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. That's a nice idea. I'm generally reluctant about async-ing constructors but I think that by using a static method we could do something like: // BlockmetaJsonBlockNumberProvider.ts
async static initialize(config, ...) {
const provider = new BlockmetaJsonBlockNumberProvider();
const successfulConnection = await provider.testConnection();
if (successfulConnection) return provider;
else throw new Error("BANG");
} Wdyt? 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. There is a pattern on our best practices :) https://dev.to/somedood/the-proper-way-to-write-async-constructors-in-javascript-1o8c 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. Lol we are aligned without even trying |
||
|
||
// If no block has its timestamp exactly equal to the specified timestamp, | ||
// try to get the most recent block before the specified timestamp. | ||
const blockNumberBefore = await this.getBlockNumberBefore(timestampDate); | ||
|
||
return blockNumberBefore; | ||
} | ||
} | ||
|
||
/** | ||
* Gets the block number at a specific timestamp. | ||
* | ||
* @param date timestamp date | ||
* @throws { UndefinedBlockNumber } if request was successful but block number is invalid/not present | ||
* @throws { AxiosError } if request fails | ||
* @returns a promise with the block number at the timestamp | ||
*/ | ||
private async getBlockNumberAt(date: Date): Promise<bigint> { | ||
const isoTimestamp = date.toISOString(); | ||
|
||
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. should we validate isoTimestamp here? 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. Ended up passing around |
||
const blockByTimePath = this.clientConfig.servicePaths.blockByTime; | ||
|
||
const response = await this.axios.post(`${blockByTimePath}/At`, { time: isoTimestamp }); | ||
|
||
return this.parseBlockByTimeResponse(response, isoTimestamp); | ||
} | ||
|
||
/** | ||
* Gets the most recent block number before the specified timestamp. | ||
* | ||
* @param date timestamp date | ||
* @throws { UndefinedBlockNumber } if request was successful but block number is invalid/not present | ||
* @throws { AxiosError } if request fails | ||
* @returns a promise with the most recent block number before the specified timestamp | ||
*/ | ||
private async getBlockNumberBefore(date: Date): Promise<bigint> { | ||
const isoTimestamp = date.toISOString(); | ||
|
||
const blockByTimePath = this.clientConfig.servicePaths.blockByTime; | ||
|
||
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. ditto |
||
const response = await this.axios.post(`${blockByTimePath}/Before`, { time: isoTimestamp }); | ||
|
||
return this.parseBlockByTimeResponse(response, isoTimestamp); | ||
} | ||
|
||
/** | ||
* Parse the BlockByTime response and extracts the block number. | ||
* | ||
* @param response an AxiosResponse of a request to BlockByTime endpoint | ||
* @param isoTimestamp the timestamp that was sent in the request | ||
* @returns the block number inside a BlockByTime service response | ||
*/ | ||
private parseBlockByTimeResponse( | ||
response: AxiosResponse<unknown>, | ||
isoTimestamp: string, | ||
): bigint { | ||
const { data } = response; | ||
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 should specify what we're returning in the function def right? |
||
// TODO: validate with zod instead | ||
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. hehehe |
||
const blockNumber = (data as BlockByTimeResponse)["num"]; | ||
|
||
if (blockNumber === undefined) { | ||
this.logger.error(`Couldn't find a block number for timestamp ${isoTimestamp}`); | ||
|
||
throw new UndefinedBlockNumber(isoTimestamp); | ||
} | ||
|
||
return BigInt(blockNumber); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export * from "./blockNumberProvider.js"; | ||
export * from "./blockNumberProviderFactory.js"; | ||
export * from "./blockmetaJsonBlockNumberProvider.js"; | ||
export * from "./evmBlockNumberProvider.js"; |
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.
Solana 👀 going shopping for those 200GB ram for running validators xd
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.
xd