Skip to content

Commit

Permalink
feat(evm-api): implement eth_getUncleCountByBlockHash (#787)
Browse files Browse the repository at this point in the history
* Add EthGetUncleCountByBlockHash

* Fix error handler

* Add hasCommitById

* Add block check

* Slice block id

* style: resolve style guide violations

---------

Co-authored-by: sebastijankuzner <[email protected]>
  • Loading branch information
sebastijankuzner and sebastijankuzner authored Dec 2, 2024
1 parent c616cfa commit 7f6c7d0
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 3 deletions.
8 changes: 6 additions & 2 deletions packages/api-common/source/rcp/processor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Hapi from "@hapi/hapi";
import { inject, injectable } from "@mainsail/container";
import { Contracts, Identifiers } from "@mainsail/contracts";
import { Contracts, Exceptions, Identifiers } from "@mainsail/contracts";

import { getRcpId, prepareRcpError } from "./utils.js";

Expand Down Expand Up @@ -37,7 +37,11 @@ export class Processor implements Contracts.Api.RPC.Processor {
jsonrpc: "2.0",
result: await action.handle(payload.params),
};
} catch {
} catch (error) {
if (error instanceof Exceptions.RpcError) {
return prepareRcpError(getRcpId(request), error.code, error.message);
}

return prepareRcpError(getRcpId(request), Contracts.Api.RPC.ErrorCode.InternalError);
}
}
Expand Down
3 changes: 2 additions & 1 deletion packages/api-common/source/rcp/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ export const errorMessageMap = {
export const prepareRcpError = (
id: Contracts.Api.RPC.Id,
errorCode: Contracts.Api.RPC.ErrorCode,
message?: string,
): Contracts.Api.RPC.Error => ({
error: {
code: errorCode,
message: errorMessageMap[errorCode],
message: message ?? errorMessageMap[errorCode],
},
id,
jsonrpc: "2.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { inject, injectable } from "@mainsail/container";
import { Contracts, Exceptions, Identifiers } from "@mainsail/contracts";

@injectable()
export class EthGetUncleCountByBlockHash implements Contracts.Api.RPC.Action {
@inject(Identifiers.Database.Service)
private readonly databaseService!: Contracts.Database.DatabaseService;

public readonly name: string = "eth_getUncleCountByBlockHash";

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

prefixItems: [{ $ref: "prefixedHex" }], // TODO: Replace prefixedHex with prefixedBlockId
type: "array",
};

public async handle(parameters: [string]): Promise<string> {
if (!this.databaseService.hasCommitById(parameters[0].slice(2))) {
throw new Exceptions.RpcError("Block not found");
}

return `0x0`;
}
}
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 @@ -6,6 +6,7 @@ export * from "./eth-get-block-by-number.js";
export * from "./eth-get-code.js";
export * from "./eth-get-storage-at.js";
export * from "./eth-get-transaction-count.js";
export * from "./eth-get-uncle-count-by-block-hash.js";
export * from "./net-listening.js";
export * from "./net-peer-count.js";
export * from "./web3-client-version.js";
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 @@ -11,6 +11,7 @@ import {
EthGetCodeAction,
EthGetStorageAtAction,
EthGetTransactionCount,
EthGetUncleCountByBlockHash,
NetListeningAction,
NetPeerCountAction,
Web3ClientVersionAction,
Expand Down Expand Up @@ -72,6 +73,7 @@ export class ServiceProvider extends AbstractServiceProvider<Server> {
this.app.resolve(EthGetCodeAction),
this.app.resolve(EthGetStorageAtAction),
this.app.resolve(EthGetTransactionCount),
this.app.resolve(EthGetUncleCountByBlockHash),
this.app.resolve(NetListeningAction),
this.app.resolve(NetPeerCountAction),
this.app.resolve(Web3ClientVersionAction),
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 @@ -13,6 +13,7 @@ export interface DatabaseService {
getState(): State;
getCommit(height: number): Promise<Commit | undefined>;
getCommitById(id: string): Promise<Commit | undefined>;
hasCommitById(id: string): boolean;
findCommitBuffers(start: number, end: number): Promise<Buffer[]>;
readCommits(start: number, end: number): AsyncGenerator<Commit>;
findBlocks(start: number, end: number): Promise<Block[]>;
Expand Down
4 changes: 4 additions & 0 deletions packages/database/source/database-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ export class DatabaseService implements Contracts.Database.DatabaseService {
return undefined;
}

public hasCommitById(id: string): boolean {
return this.#getHeightById(id) !== undefined;
}

public async findCommitBuffers(start: number, end: number): Promise<Buffer[]> {
const heights: number[] = [];

Expand Down

0 comments on commit 7f6c7d0

Please sign in to comment.