Skip to content

Commit

Permalink
Add database method
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastijankuzner committed Jan 10, 2025
1 parent f19ea60 commit 55e42dd
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ export class EthGetTransactionByBlockNumberAndIndex implements Contracts.Api.RPC
maxItems: 2,
minItems: 2,

prefixItems: [{ $ref: "prefixedHex" }, { $ref: "prefixedHex" }], // TODO: Use block id & limit sequence
prefixItems: [{ $ref: "prefixedHex" }, { $ref: "prefixedHex" }], // TODO: Limit sequence
type: "array",
};

public async handle(parameters: [string, string]): Promise<any> {
const transaction = await this.databaseService.getTransactionByBlockIdAndIndex(
parameters[0].slice(2),
const transaction = await this.databaseService.getTransactionByBlockHeightAndIndex(
Number.parseInt(parameters[0]),
Number.parseInt(parameters[1]),
);

Expand Down
1 change: 1 addition & 0 deletions packages/contracts/source/contracts/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export interface DatabaseService {

getTransactionById(id: string): Promise<Transaction | undefined>;
getTransactionByBlockIdAndIndex(blockId: string, index: number): Promise<Transaction | undefined>;
getTransactionByBlockHeightAndIndex(height: number, index: number): Promise<Transaction | undefined>;

addCommit(block: Commit): void;
persist(): Promise<void>;
Expand Down
19 changes: 19 additions & 0 deletions packages/database/source/database-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,25 @@ export class DatabaseService implements Contracts.Database.DatabaseService {
return this.#readTransaction(`${height}-${index}`);
}

public async getTransactionByBlockHeightAndIndex(
height: number,
index: number,
): Promise<Contracts.Crypto.Transaction | undefined> {
// Get TX from cache
if (this.#commitCache.has(height)) {
const block = this.#commitCache.get(height)!.block;

if (block.transactions.length <= index) {
return undefined;
}

return block.transactions[index];
}

// Get TX from storage
return this.#readTransaction(`${height}-${index}`);
}

public async *readCommits(start: number, end: number): AsyncGenerator<Contracts.Crypto.Commit> {
for (let height = start; height <= end; height++) {
const data = await this.#readCommitBytes(height);
Expand Down

0 comments on commit 55e42dd

Please sign in to comment.