-
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: l2 metrics service #63
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./l2Metrics.service.js"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { ZKChainProvider } from "@zkchainhub/chain-providers"; | ||
import { ILogger } from "@zkchainhub/shared"; | ||
|
||
/** | ||
* Acts as a wrapper around Viem library to provide methods to interact with zkSync chains. | ||
*/ | ||
export class L2MetricsService { | ||
constructor( | ||
private readonly provider: ZKChainProvider, | ||
private readonly logger: ILogger, | ||
) {} | ||
|
||
/** | ||
* Retrieves the transactions per second (TPS) from the provider. | ||
* | ||
* @returns A promise that resolves to the number of transactions per second. | ||
*/ | ||
async tps(): Promise<number> { | ||
return this.provider.tps(); | ||
} | ||
|
||
/** | ||
* Retrieves the average block time from the provider. | ||
* | ||
* @returns A promise that resolves to the average block time as a number. | ||
*/ | ||
async avgBlockTime(): Promise<number> { | ||
return this.provider.avgBlockTime(); | ||
} | ||
|
||
/** | ||
* Retrieves the number of the last block in the chain. | ||
* | ||
* @returns A promise that resolves to a bigint representing the number of the last block. | ||
*/ | ||
async lastBlock(): Promise<bigint> { | ||
return this.provider.getBlockNumber(); | ||
} | ||
|
||
/** | ||
* Retrieves the last verified block based on the given lastVerifiedBatch. | ||
* | ||
* @param lastVerifiedBatch The number representing the last verified batch. | ||
* @returns A Promise that resolves to the number of the last verified block, or undefined if an error occurs. | ||
*/ | ||
async getLastVerifiedBlock(lastVerifiedBatch: number): Promise<number | undefined> { | ||
const [, endBlock] = await this.provider.getL1BatchBlockRange(lastVerifiedBatch); | ||
return endBlock; | ||
} | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
||
import { ZKChainProvider } from "@zkchainhub/chain-providers"; | ||
import { ILogger } from "@zkchainhub/shared"; | ||
|
||
import { L2MetricsService } from "../../../src/l2/l2Metrics.service"; | ||
|
||
describe("L2MetricsService", () => { | ||
let service: L2MetricsService; | ||
let provider: ZKChainProvider; | ||
let logger: ILogger; | ||
|
||
beforeEach(() => { | ||
provider = { | ||
tps: vi.fn(), | ||
avgBlockTime: vi.fn(), | ||
getBlockNumber: vi.fn(), | ||
getL1BatchBlockRange: vi.fn(), | ||
} as unknown as ZKChainProvider; | ||
logger = { | ||
info: vi.fn(), | ||
warn: vi.fn(), | ||
error: vi.fn(), | ||
debug: vi.fn(), | ||
} as unknown as ILogger; | ||
service = new L2MetricsService(provider, logger); | ||
}); | ||
|
||
it("should create an instance of L2MetricsService", () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
|
||
describe("tps", () => { | ||
it("should return the TPS value", async () => { | ||
const expectedTps = 100; | ||
vi.spyOn(provider, "tps").mockResolvedValue(expectedTps); | ||
|
||
const result = await service.tps(); | ||
|
||
expect(result).toBe(expectedTps); | ||
expect(provider.tps).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe("avgBlockTime", () => { | ||
it("return the average block time", async () => { | ||
const expectedAvgBlockTime = 10; | ||
vi.spyOn(provider, "avgBlockTime").mockResolvedValue(expectedAvgBlockTime); | ||
|
||
const result = await service.avgBlockTime(); | ||
|
||
expect(result).toBe(expectedAvgBlockTime); | ||
expect(provider.avgBlockTime).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe("lastBlock", () => { | ||
it("return the last block number", async () => { | ||
const expectedLastBlock = 1000n; | ||
vi.spyOn(provider, "getBlockNumber").mockResolvedValue(expectedLastBlock); | ||
|
||
const result = await service.lastBlock(); | ||
|
||
expect(result).toBe(expectedLastBlock); | ||
expect(provider.getBlockNumber).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe("getLastVerifiedBlock", () => { | ||
it("return the end block of the last verified batch", async () => { | ||
const lastVerifiedBatch = 5; | ||
const expectedEndBlock = 100; | ||
vi.spyOn(provider, "getL1BatchBlockRange").mockResolvedValue([0, expectedEndBlock]); | ||
|
||
const result = await service.getLastVerifiedBlock(lastVerifiedBatch); | ||
|
||
expect(result).toBe(expectedEndBlock); | ||
expect(provider.getL1BatchBlockRange).toHaveBeenCalledWith(lastVerifiedBatch); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Any non-happy paths worth testing?