Skip to content
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(api-evm): implement eth_getTransactionByBlockHashAndIndex #815

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { inject, injectable } from "@mainsail/container";
import { Contracts, Identifiers } from "@mainsail/contracts";

import { TransactionResource } from "../resources/index.js";

@injectable()
export class EthGetTransactionByBlockHashAndIndex implements Contracts.Api.RPC.Action {
@inject(Identifiers.Application.Instance)
private readonly app!: Contracts.Kernel.Application;

@inject(Identifiers.Database.Service)
private readonly databaseService!: Contracts.Database.DatabaseService;

public readonly name: string = "eth_getTransactionByBlockHashAndIndex";

public readonly schema = {
$id: `jsonRpc_${this.name}`,

maxItems: 2,
minItems: 2,

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

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

if (!transaction) {
return null;
}

return this.app.resolve(TransactionResource).transform(transaction.data);
}
}
1 change: 1 addition & 0 deletions packages/api-evm/source/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export * from "./eth-get-block-transaction-count-by-hash.js";
export * from "./eth-get-block-transaction-count-by-number.js";
export * from "./eth-get-code.js";
export * from "./eth-get-storage-at.js";
export * from "./eth-get-transaction-by-block-hash-and-index.js";
export * from "./eth-get-transaction-by-hash.js";
export * from "./eth-get-transaction-count.js";
export * from "./eth-get-uncle-by-block-hash-and-index.js";
Expand Down
2 changes: 2 additions & 0 deletions packages/api-evm/source/service-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
EthGetBlockTransactionCountByNumber,
EthGetCodeAction,
EthGetStorageAtAction,
EthGetTransactionByBlockHashAndIndex,
EthGetTransactionByHash,
EthGetTransactionCount,
EthGetUncleByBlockHashAndIndex,
Expand Down Expand Up @@ -82,6 +83,7 @@ export class ServiceProvider extends AbstractServiceProvider<Server> {
this.app.resolve(EthGetBlockTransactionCountByNumber),
this.app.resolve(EthGetCodeAction),
this.app.resolve(EthGetStorageAtAction),
this.app.resolve(EthGetTransactionByBlockHashAndIndex),
this.app.resolve(EthGetTransactionByHash),
this.app.resolve(EthGetTransactionCount),
this.app.resolve(EthGetUncleByBlockHashAndIndex),
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 @@ -26,6 +26,7 @@ export interface DatabaseService {
getBlockHeaderById(id: string): Promise<BlockHeader | undefined>;

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

addCommit(block: Commit): void;
persist(): Promise<void>;
Expand Down
55 changes: 42 additions & 13 deletions packages/database/source/database-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,23 +186,32 @@ export class DatabaseService implements Contracts.Database.DatabaseService {
return undefined;
}

const transactionBytes: Buffer | undefined = this.transactionStorage.get(key);
Utils.assert.defined<Buffer>(transactionBytes);
return await this.#readTransaction(key);
}

const buffer = ByteBuffer.fromBuffer(transactionBytes);
const height = buffer.readUint32();
const sequence = buffer.readUint32();
const transaction = await this.transactionFactory.fromBytes(buffer.getRemainder());
public async getTransactionByBlockIdAndIndex(
blockId: string,
index: number,
): Promise<Contracts.Crypto.Transaction | undefined> {
// Verify if the block exists
const height = this.#getHeightById(blockId);
if (height === undefined) {
return undefined;
}

transaction.data.sequence = sequence;
transaction.data.blockHeight = height;
// Get TX from cache
if (this.#commitCache.has(height)) {
const block = this.#commitCache.get(height)!.block;

const blockBuffer = this.#readBlockHeaderBytes(height);
Utils.assert.defined<Buffer>(blockBuffer);
const block = await this.blockDeserializer.deserializeHeader(blockBuffer);
transaction.data.blockId = block.id;
if (block.transactions.length <= index) {
return undefined;
}

return transaction;
return block.transactions[index];
}

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

public async *readCommits(start: number, end: number): AsyncGenerator<Contracts.Crypto.Commit> {
Expand Down Expand Up @@ -346,6 +355,26 @@ export class DatabaseService implements Contracts.Database.DatabaseService {
return this.blockStorage.get(height);
}

async #readTransaction(key): Promise<Contracts.Crypto.Transaction | undefined> {
const transactionBytes: Buffer | undefined = this.transactionStorage.get(key);
Utils.assert.defined<Buffer>(transactionBytes);

const buffer = ByteBuffer.fromBuffer(transactionBytes);
const height = buffer.readUint32();
const sequence = buffer.readUint32();
const transaction = await this.transactionFactory.fromBytes(buffer.getRemainder());

transaction.data.sequence = sequence;
transaction.data.blockHeight = height;

const blockBuffer = this.#readBlockHeaderBytes(height);
Utils.assert.defined<Buffer>(blockBuffer);
const block = await this.blockDeserializer.deserializeHeader(blockBuffer);
transaction.data.blockId = block.id;

return transaction;
}

async #map<T>(data: unknown[], callback: (...arguments_: any[]) => Promise<T>): Promise<T[]> {
const result: T[] = [];
for (const [index, datum] of data.entries()) {
Expand Down
Loading