Skip to content

Commit

Permalink
feat(api-evm): add prefixedTransactionId and prefixedBlockId sche…
Browse files Browse the repository at this point in the history
…mas (#832)

* Add prefixedTransactionId

* Add prefixedBlockId

* Remove schemas

* Remove TODO

* Extract block tag

* Extract block tag

* Fix tests
  • Loading branch information
sebastijankuzner authored Jan 22, 2025
1 parent aae8113 commit 6823f66
Show file tree
Hide file tree
Showing 30 changed files with 80 additions and 112 deletions.
3 changes: 3 additions & 0 deletions packages/api-evm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
"joi": "17.12.2"
},
"devDependencies": {
"@mainsail/crypto-address-keccak256": "workspace:*",
"@mainsail/crypto-block": "workspace:*",
"@mainsail/crypto-validation": "workspace:*",
"@mainsail/validation": "workspace:*",
"@types/semver": "7.5.8",
"uvu": "^0.5.6"
Expand Down
6 changes: 2 additions & 4 deletions packages/api-evm/source/actions/eth-call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import { inject, injectable, tagged } from "@mainsail/container";
import { Contracts, Exceptions, Identifiers } from "@mainsail/contracts";
import { ethers } from "ethers";

type BlockTag = "latest" | "finalized" | "safe";

type TxData = {
from: string;
to: string;
Expand Down Expand Up @@ -35,13 +33,13 @@ export class CallAction implements Contracts.Api.RPC.Action {
required: ["from", "to", "data"],
type: "object",
},
{ enum: ["latest", "finalized", "safe"], type: "string" },
{ $ref: "blockTag" },
],

type: "array",
};

public async handle(parameters: [TxData, BlockTag]): Promise<any> {
public async handle(parameters: [TxData, Contracts.Crypto.BlockTag]): Promise<any> {
const [data] = parameters;

const { success, output } = await this.evm.view({
Expand Down
15 changes: 4 additions & 11 deletions packages/api-evm/source/actions/eth-get-balance.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Identifiers } from "@mainsail/contracts";
import { schemas as keccak256Schemas } from "@mainsail/crypto-address-keccak256";
import { schemas as blockSchemas } from "@mainsail/crypto-block";
import { Validator } from "@mainsail/validation";

import { describe, Sandbox } from "../../../test-framework/source";
Expand Down Expand Up @@ -34,17 +36,8 @@ describe<{
});

it("schema should be array with 0 parameters", ({ action, validator }) => {
validator.addSchema({
$id: "address",
allOf: [
{
maxLength: 42,
minLength: 42,
pattern: "^0x[0123456789a-fA-F]+$",
},
],
type: "string",
});
validator.addSchema(keccak256Schemas.address);
validator.addSchema(blockSchemas.blockTag);
validator.addSchema(action.schema);

assert.undefined(
Expand Down
2 changes: 1 addition & 1 deletion packages/api-evm/source/actions/eth-get-balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class EthGetBalanceAction implements Contracts.Api.RPC.Action {
maxItems: 2,
minItems: 2,

prefixItems: [{ $ref: "address" }, { enum: ["latest", "finalized", "safe"], type: "string" }],
prefixItems: [{ $ref: "address" }, { $ref: "blockTag" }],
type: "array",
};

Expand Down
2 changes: 1 addition & 1 deletion packages/api-evm/source/actions/eth-get-block-by-hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class EthGetBlockByHashAction implements Contracts.Api.RPC.Action {
maxItems: 2,
minItems: 2,

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

Expand Down
5 changes: 1 addition & 4 deletions packages/api-evm/source/actions/eth-get-block-by-number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ export class EthGetBlockByNumberAction implements Contracts.Api.RPC.Action {
maxItems: 2,
minItems: 2,

prefixItems: [
{ oneOf: [{ $ref: "prefixedHex" }, { enum: ["latest", "finalized", "safe"], type: "string" }] }, // TODO: Extract block tag
{ type: "boolean" },
],
prefixItems: [{ oneOf: [{ $ref: "prefixedHex" }, { $ref: "blockTag" }] }, { type: "boolean" }],
type: "array",
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Identifiers } from "@mainsail/contracts";
import { schemas as cryptoBlockSchemas } from "@mainsail/crypto-block";
import { schemas as cryptoValidationSchemas } from "@mainsail/crypto-validation";
import { Validator } from "@mainsail/validation";

import { describe, Sandbox } from "../../../test-framework/source";
Expand Down Expand Up @@ -28,11 +30,8 @@ describe<{
});

it("schema should be array with 0 parameters", ({ action, validator }) => {
validator.addSchema({
$id: "prefixedHex",
pattern: "^0x[0-9a-f]+$",
type: "string",
});
validator.addSchema(cryptoValidationSchemas.prefixedHex);
validator.addSchema(cryptoBlockSchemas.prefixedBlockId);
validator.addSchema(action.schema);

assert.undefined(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class EthGetBlockTransactionCountByHash implements Contracts.Api.RPC.Acti
maxItems: 1,
minItems: 1,

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Identifiers } from "@mainsail/contracts";
import { Validator } from "@mainsail/validation";
import { schemas as cryptoValidationSchemas } from "@mainsail/crypto-validation";

import { describe, Sandbox } from "../../../test-framework/source";
import { EthGetBlockTransactionCountByNumber } from "./index.js";
Expand Down Expand Up @@ -28,11 +29,7 @@ describe<{
});

it("schema should be array with 0 parameters", ({ action, validator }) => {
validator.addSchema({
$id: "prefixedHex",
pattern: "^0x[0-9a-f]+$",
type: "string",
});
validator.addSchema(cryptoValidationSchemas.prefixedHex);
validator.addSchema(action.schema);

assert.undefined(validator.validate("jsonRpc_eth_getBlockTransactionCountByNumber", ["0x0"]).errors);
Expand Down
15 changes: 4 additions & 11 deletions packages/api-evm/source/actions/eth-get-code.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Identifiers } from "@mainsail/contracts";
import { schemas as keccak256Schemas } from "@mainsail/crypto-address-keccak256";
import { schemas as blockSchemas } from "@mainsail/crypto-block";
import { Validator } from "@mainsail/validation";

import { describe, Sandbox } from "../../../test-framework/source";
Expand Down Expand Up @@ -28,17 +30,8 @@ describe<{
});

it("schema should be ok", ({ action, validator }) => {
validator.addSchema({
$id: "address",
allOf: [
{
maxLength: 42,
minLength: 42,
pattern: "^0x[0123456789a-fA-F]+$",
},
],
type: "string",
});
validator.addSchema(keccak256Schemas.address);
validator.addSchema(blockSchemas.blockTag);
validator.addSchema(action.schema);

assert.undefined(
Expand Down
2 changes: 1 addition & 1 deletion packages/api-evm/source/actions/eth-get-code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class EthGetCodeAction implements Contracts.Api.RPC.Action {
maxItems: 2,
minItems: 2,

prefixItems: [{ $ref: "address" }, { enum: ["latest", "finalized", "safe"], type: "string" }],
prefixItems: [{ $ref: "address" }, { $ref: "blockTag" }],
type: "array",
};

Expand Down
22 changes: 6 additions & 16 deletions packages/api-evm/source/actions/eth-get-storage-at.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { Identifiers } from "@mainsail/contracts";
import { schemas as keccak256Schemas } from "@mainsail/crypto-address-keccak256";
import { schemas as blockSchemas } from "@mainsail/crypto-block";
import { schemas as cryptoValidationSchemas } from "@mainsail/crypto-validation";
import { Validator } from "@mainsail/validation";

import { describe, Sandbox } from "../../../test-framework/source";
Expand Down Expand Up @@ -28,22 +31,9 @@ describe<{
});

it("schema should be ok", ({ action, validator }) => {
validator.addSchema({
$id: "address",
allOf: [
{
maxLength: 42,
minLength: 42,
pattern: "^0x[0123456789a-fA-F]+$",
},
],
type: "string",
});
validator.addSchema({
$id: "prefixedHex",
pattern: "^0x[0-9a-f]+$",
type: "string",
});
validator.addSchema(keccak256Schemas.address);
validator.addSchema(cryptoValidationSchemas.prefixedHex);
validator.addSchema(blockSchemas.blockTag);
validator.addSchema(action.schema);

assert.undefined(
Expand Down
6 changes: 2 additions & 4 deletions packages/api-evm/source/actions/eth-get-storage-at.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { inject, injectable, tagged } from "@mainsail/container";
import { Contracts, Identifiers } from "@mainsail/contracts";

type BlockTag = "latest" | "earliest" | "pending";

@injectable()
export class EthGetStorageAtAction implements Contracts.Api.RPC.Action {
@inject(Identifiers.Evm.Instance)
Expand All @@ -20,12 +18,12 @@ export class EthGetStorageAtAction implements Contracts.Api.RPC.Action {
prefixItems: [
{ $ref: "address" },
{ allOf: [{ $ref: "prefixedHex" }, { maxLength: 66, type: "string" }] },
{ enum: ["latest", "finalized", "safe"], type: "string" },
{ $ref: "blockTag" },
],
type: "array",
};

public async handle(parameters: [string, string, BlockTag]): Promise<any> {
public async handle(parameters: [string, string, Contracts.Crypto.BlockTag]): Promise<any> {
const [address, slot] = parameters;

return await this.evm.storageAt(address, BigInt(slot));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class EthGetTransactionByBlockHashAndIndex implements Contracts.Api.RPC.A
maxItems: 2,
minItems: 2,

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class EthGetTransactionByBlockNumberAndIndex implements Contracts.Api.RPC
maxItems: 2,
minItems: 2,

prefixItems: [{ $ref: "prefixedHex" }, { $ref: "prefixedHex" }], // TODO: Limit sequence
prefixItems: [{ $ref: "prefixedHex" }, { $ref: "prefixedHex" }],
type: "array",
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class EthGetTransactionByHash implements Contracts.Api.RPC.Action {
maxItems: 1,
minItems: 1,

prefixItems: [{ $ref: "prefixedHex" }], // TODO: Use transaction id
prefixItems: [{ $ref: "prefixedTransactionId" }],
type: "array",
};

Expand Down
15 changes: 4 additions & 11 deletions packages/api-evm/source/actions/eth-get-transaction-count.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Identifiers } from "@mainsail/contracts";
import { schemas as keccak256Schemas } from "@mainsail/crypto-address-keccak256";
import { schemas as blockSchemas } from "@mainsail/crypto-block";
import { Validator } from "@mainsail/validation";

import { describe, Sandbox } from "../../../test-framework/source";
Expand Down Expand Up @@ -34,17 +36,8 @@ describe<{
});

it("schema should be array with 0 parameters", ({ action, validator }) => {
validator.addSchema({
$id: "address",
allOf: [
{
maxLength: 42,
minLength: 42,
pattern: "^0x[0123456789a-fA-F]+$",
},
],
type: "string",
});
validator.addSchema(keccak256Schemas.address);
validator.addSchema(blockSchemas.blockTag);
validator.addSchema(action.schema);

assert.undefined(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class EthGetTransactionCount implements Contracts.Api.RPC.Action {
maxItems: 2,
minItems: 2,

prefixItems: [{ $ref: "address" }, { enum: ["latest", "finalized", "safe"], type: "string" }],
prefixItems: [{ $ref: "address" }, { $ref: "blockTag" }],
type: "array",
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ export class EthGetTransactionReceipt implements Contracts.Api.RPC.Action {
maxItems: 1,
minItems: 1,

prefixItems: [
{ $ref: "prefixedHex" }, // TODO: Extract transaction id
],
prefixItems: [{ $ref: "prefixedTransactionId" }],
type: "array",
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class EthGetUncleByBlockHashAndIndex implements Contracts.Api.RPC.Action
maxItems: 2,
minItems: 2,

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class EthGetUncleCountByBlockHash implements Contracts.Api.RPC.Action {
maxItems: 1,
minItems: 1,

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

Expand Down
7 changes: 2 additions & 5 deletions packages/api-evm/source/actions/web3-sha3.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Identifiers } from "@mainsail/contracts";
import { Validator } from "@mainsail/validation";
import { schemas as cryptoValidationSchemas } from "@mainsail/crypto-validation";

import { describe, Sandbox } from "../../../test-framework/source";
import { Web3Sha3 } from "./index.js";
Expand All @@ -25,11 +26,7 @@ describe<{
});

it("schema should be ok", ({ action, validator }) => {
validator.addSchema({
$id: "prefixedHex",
pattern: "^0x[0-9a-f]+$",
type: "string",
});
validator.addSchema(cryptoValidationSchemas.prefixedHex);

assert.equal(action.schema, {
$id: `jsonRpc_web3_sha3`,
Expand Down
1 change: 0 additions & 1 deletion packages/api-evm/source/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * as Schemas from "./schemas.js";
export * from "./server.js";
export * from "./service-provider.js";
17 changes: 0 additions & 17 deletions packages/api-evm/source/schemas.ts

This file was deleted.

4 changes: 1 addition & 3 deletions packages/api-evm/source/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { inject, injectable, tagged } from "@mainsail/container";
import { Identifiers } from "@mainsail/contracts";
import { Providers } from "@mainsail/kernel";

import * as Schemas from "./schemas.js";

@injectable()
export class Server extends AbstractServer {
@inject(Identifiers.ServiceProvider.Configuration)
Expand Down Expand Up @@ -53,6 +51,6 @@ export class Server extends AbstractServer {
}

protected schemas(): any {
return Schemas;
return {};
}
}
1 change: 0 additions & 1 deletion packages/api-evm/source/service-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ export class ServiceProvider extends AbstractServiceProvider<Server> {
protected getPlugins(): any[] {
const config = this.config().get<any>("plugins");

// @TODO: Implement RPC rate limiting & whitelist
return [
{
options: {
Expand Down
Loading

0 comments on commit 6823f66

Please sign in to comment.