-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# 🤖 Linear Closes ZKS-228 ## Description - `L2MetricsService` implementation on `metrics` package - add zk provider method for fetching blocks range for a given L1 Batch
- Loading branch information
Showing
11 changed files
with
197 additions
and
18 deletions.
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